feat(libSceCesCs): implement Shift-JIS CP932 and CP1250 encoding conversions #7091

Merged
Bl4ckk01 merged 1 commit from upstream-pr/7091 into main 2026-10-19 17:49:49 +00:00
Bl4ckk01 commented 2026-10-17 11:02:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bl4ckk01 on 2026-10-17T11:02:29Z.


What

Implement Shift-JIS (CP932 / Windows-31J) multibyte decoding and encoding conversions mapped to JIS X 0208, and implement Windows-1250 (Central European) profile and single-byte conversions for libSceCesCs. Add exhaustive unit tests covering all 7336 assigned Shift-JIS cells and CP1250 round-trips.

Tested

Built and verified unit tests in core/libs/tests/GuestCes.cpp covering ASCII, half-width katakana, full-width Japanese text, UCS-2 conversions, buffer boundary errors, and exhaustive 7336-character round-trip verification.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899521960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091). Originally posted by [Bl4ckk01](https://github.com/Bl4ckk01) on 2026-10-17T11:02:29Z. --- ### What Implement Shift-JIS (CP932 / Windows-31J) multibyte decoding and encoding conversions mapped to JIS X 0208, and implement Windows-1250 (Central European) profile and single-byte conversions for libSceCesCs. Add exhaustive unit tests covering all 7336 assigned Shift-JIS cells and CP1250 round-trips. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Built and verified unit tests in `core/libs/tests/GuestCes.cpp` covering ASCII, half-width katakana, full-width Japanese text, UCS-2 conversions, buffer boundary errors, and exhaustive 7336-character round-trip verification. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-17 11:22:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T11:22:01Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7884 core/libs/prx/libSceCesCs/Export.cpp L251, L278
PR 8007 docs/dev/debt/libSceCesCs.md L6-7

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestCes.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233297071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6233297071). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T11:22:01Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 7884](https://www.github.com/boykopovar/AnyPS5/pull/7884) | `core/libs/prx/libSceCesCs/Export.cpp` [L251](https://github.com/boykopovar/AnyPS5/blob/ea3e930003fc7931ea274e8cae68a5d87213c987/core/libs/prx/libSceCesCs/Export.cpp#L251), [L278](https://github.com/boykopovar/AnyPS5/blob/ea3e930003fc7931ea274e8cae68a5d87213c987/core/libs/prx/libSceCesCs/Export.cpp#L278) | | | | [PR 8007](https://www.github.com/boykopovar/AnyPS5/pull/8007) | `docs/dev/debt/libSceCesCs.md` [L6-7](https://github.com/boykopovar/AnyPS5/blob/ea3e930003fc7931ea274e8cae68a5d87213c987/docs/dev/debt/libSceCesCs.md#L6-L7) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestCes.cpp` |
oneandonlydean commented 2026-10-17 12:09:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T12:09:42Z.


Thanks for this. Review of 5b34c486e: two small changes, everything else checks out against Python's codecs.

Change requests

  1. CP932 IBM extensions FA40-FC4B are refused. decodeMbcs accepts leads up to 0xFC but maps only JIS rows 1-94. So the 388 IBM-extension cells (FA40-FC4B) answer UNASSIGNED_CODE, although they are assigned in CP932: Python cp932 decodes all 388. All 388 characters are already in JisX0208::TO_UCS2 (as NEC-selected rows 89-92 or row 2), so decoding them needs no new data beyond an FA40-FC4B → Unicode (or → NEC cell) redirect. A common case: FBFC = 髙 (U+9AD9), a frequent kanji in names. Encoding is unaffected: it picks the lowest cell (EEE0 for 髙), which matches Python's cp932 encoder for every one of the 7336 characters. Ask: decode FA40-FC4B, and update the test's sjisAssigned == 7336 (it now pins the gap; it would become 7724). At minimum, if the author prefers not to, the debt entry must say that leads 0xF0-0xFC answer UNASSIGNED_CODE (the user-defined area F040-F9FC, which CP932 maps to PUA, and the IBM extensions). Today it implies the full CP932 lead range.
  2. Revert the unrelated EUC-JP hunk (if (lead == 0x8F) NotImplemented_nid_no_patch(...) → plain throw std::runtime_error). NotImplemented_nid_no_patch already throws (libc/src/General.cpp:617), so the behaviour is the same apart from the message. But it drops the project's marker for unimplemented paths, which the PR checklist itself names ("Unimplemented paths throw (NotImplemented_nid_no_patch)"), and it's off-topic for this PR.

Verified

  • CP1250 table vs Python cp1250: all 128 high bytes match, except the 5 undefined bytes (0x81, 0x83, 0x88, 0x90, 0x98), which map to the same C1 code point. That's the existing CP1251/CP1252 convention, and Microsoft's best-fit does the same. Reverse lookup is a linear scan of 128 entries, fine.
  • Shift-JIS decode (emulated in Python over all 188×188 lead/trail pairs, against the PR's table): rows 1-94 match Python cp932 exactly. The only divergences are F040-F9FC (PUA, 1880 cells; acceptable, the console's handling is unknown) and FA40-FC4B (CR 1). Trail validity (0x40-0x7E, 0x80-0xFC), the row/cell arithmetic and the SRC_BUFFER_END on a cut pair are correct.
  • Shift-JIS encode: 0 differences against Python cp932 over all 7336 table characters (lowest-cell choice). Half-width katakana U+FF61-FF9F ↔ A1-DF and ASCII identity are right.

Once fixed, full CI on the new head with guest_ces and guest_ces_module passing on Linux and Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233716255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6233716255). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T12:09:42Z. --- Thanks for this. Review of 5b34c486e: two small changes, everything else checks out against Python's codecs. ## Change requests 1. **CP932 IBM extensions FA40-FC4B are refused.** `decodeMbcs` accepts leads up to 0xFC but maps only JIS rows 1-94. So the 388 IBM-extension cells (FA40-FC4B) answer `UNASSIGNED_CODE`, although they are assigned in CP932: Python `cp932` decodes all 388. **All 388 characters are already in `JisX0208::TO_UCS2`** (as NEC-selected rows 89-92 or row 2), so decoding them needs no new data beyond an FA40-FC4B → Unicode (or → NEC cell) redirect. A common case: `FBFC` = 髙 (U+9AD9), a frequent kanji in names. Encoding is unaffected: it picks the lowest cell (`EEE0` for 髙), which matches Python's cp932 encoder for every one of the 7336 characters. **Ask:** decode FA40-FC4B, and update the test's `sjisAssigned == 7336` (it now pins the gap; it would become 7724). At minimum, if the author prefers not to, the debt entry must say that leads 0xF0-0xFC answer `UNASSIGNED_CODE` (the user-defined area F040-F9FC, which CP932 maps to PUA, and the IBM extensions). Today it implies the full CP932 lead range. 2. **Revert the unrelated EUC-JP hunk** (`if (lead == 0x8F) NotImplemented_nid_no_patch(...)` → plain `throw std::runtime_error`). `NotImplemented_nid_no_patch` already throws (`libc/src/General.cpp:617`), so the behaviour is the same apart from the message. But it drops the project's marker for unimplemented paths, which the PR checklist itself names ("Unimplemented paths throw (`NotImplemented_nid_no_patch`)"), and it's off-topic for this PR. ## Verified - **CP1250 table vs Python `cp1250`:** all 128 high bytes match, except the 5 undefined bytes (0x81, 0x83, 0x88, 0x90, 0x98), which map to the same C1 code point. That's the existing CP1251/CP1252 convention, and Microsoft's best-fit does the same. Reverse lookup is a linear scan of 128 entries, fine. - **Shift-JIS decode** (emulated in Python over all 188×188 lead/trail pairs, against the PR's table): rows 1-94 match Python `cp932` exactly. The only divergences are F040-F9FC (PUA, 1880 cells; acceptable, the console's handling is unknown) and FA40-FC4B (CR 1). Trail validity (0x40-0x7E, 0x80-0xFC), the row/cell arithmetic and the `SRC_BUFFER_END` on a cut pair are correct. - **Shift-JIS encode:** 0 differences against Python `cp932` over all 7336 table characters (lowest-cell choice). Half-width katakana U+FF61-FF9F ↔ A1-DF and ASCII identity are right. Once fixed, full CI on the new head with guest_ces and guest_ces_module passing on Linux and Windows.
Bonjourcestmatt commented 2026-10-17 23:43:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-17T23:43:06Z.


Windows run of this head, plus an independent check of the tables against Python's codecs.

Setup: Windows 11, MinGW-w64 GCC 15.2.0 UCRT, full configure with submodules, head 21be453ef (on main 673f7406c), ctest -R guest_ces.

1. guest_ces aborts on this head. guest_ces_module passes. In GuestCes.cpp, the Shift-JIS block expects "Go \xEF\xBD\xB1!" (G, o, space, half-width katakana A, !) to convert to 4 bytes, but that is 5 characters and the library returns 5: 47 6F 20 B1 21 (used=7 produced=5). So Require(used == 7 && produced == 4 && std::memcmp(mbcs, "Go \xB1!", 4) == 0) fails first (the memcmp over 4 bytes would pass; produced == 4 does not). The same off-by-one is in the lines that follow: sceCesUtf8StrGetMbcsLen ... produced == 4, and the three round-trip calls that pass mbcs, 4 and expect used == 4. The EUC-JP block just above uses the same string and correctly expects 6 bytes for 6 characters. With those five expectations changed to 5 in a scratch copy (no library change), the whole test passes, so the implementation looks right and only the expected counts are off. The mismatch does not depend on the host, so I expect the same on Linux, but I only ran Windows.

2. Differential check against Python cp932. I did not change the library; I called it through the exports, for every double-byte code (leads 0x81-0x9F and 0xE0-0xFC, trails 0x40-0xFC except 0x7F: 11280 pairs) and for every BMP code point:

  • sceCesMbcsStrToUcs2Str: 7724 codes decode to exactly the same code point as bytes.decode('cp932'), 1676 are rejected by both, and 0 differ in value. The only codes cp932 decodes and the library rejects are the 1880 user-defined ones (leads 0xF0-0xF9, U+E000-U+E757).
  • sceCesUcs2StrToMbcsStr: 7389 code points encode to the same bytes as chr(cp).encode('cp932') and none to different bytes. The 1891 that Python encodes and the library rejects are those 1880 user-defined code points plus U+0080 and U+00A2, U+00A3, U+00AC, U+2016, U+2212, U+301C (mappings Python's cp932 codec accepts for compatibility; the canonical decodes of 0x8191, 0x8192, 0x81CA, 0x8161, 0x817C, 0x8160 do match).
  • CP1250: 251 of 256 bytes match cp1250; Python leaves 0x81, 0x83, 0x88, 0x90 and 0x98 undefined and the table maps them to U+0081, U+0083, U+0088, U+0090 and U+0098.

Whether the console maps the user-defined area, those code points or those five bytes is not something I can tell from this, and I am not asking for a change; if useful, the debt entry could list them.

Not checked: Linux, macOS, PS5 hardware or a title.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243247463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6243247463). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-17T23:43:06Z. --- Windows run of this head, plus an independent check of the tables against Python's codecs. **Setup:** Windows 11, MinGW-w64 GCC 15.2.0 UCRT, full configure with submodules, head `21be453ef` (on `main` `673f7406c`), `ctest -R guest_ces`. **1. `guest_ces` aborts on this head.** `guest_ces_module` passes. In `GuestCes.cpp`, the Shift-JIS block expects `"Go \xEF\xBD\xB1!"` (G, o, space, half-width katakana A, `!`) to convert to 4 bytes, but that is 5 characters and the library returns 5: `47 6F 20 B1 21` (`used=7 produced=5`). So `Require(used == 7 && produced == 4 && std::memcmp(mbcs, "Go \xB1!", 4) == 0)` fails first (the memcmp over 4 bytes would pass; `produced == 4` does not). The same off-by-one is in the lines that follow: `sceCesUtf8StrGetMbcsLen ... produced == 4`, and the three round-trip calls that pass `mbcs, 4` and expect `used == 4`. The EUC-JP block just above uses the same string and correctly expects 6 bytes for 6 characters. With those five expectations changed to 5 in a scratch copy (no library change), the whole test passes, so the implementation looks right and only the expected counts are off. The mismatch does not depend on the host, so I expect the same on Linux, but I only ran Windows. **2. Differential check against Python `cp932`.** I did not change the library; I called it through the exports, for every double-byte code (leads 0x81-0x9F and 0xE0-0xFC, trails 0x40-0xFC except 0x7F: 11280 pairs) and for every BMP code point: - `sceCesMbcsStrToUcs2Str`: 7724 codes decode to exactly the same code point as `bytes.decode('cp932')`, 1676 are rejected by both, and 0 differ in value. The only codes `cp932` decodes and the library rejects are the 1880 user-defined ones (leads 0xF0-0xF9, U+E000-U+E757). - `sceCesUcs2StrToMbcsStr`: 7389 code points encode to the same bytes as `chr(cp).encode('cp932')` and none to different bytes. The 1891 that Python encodes and the library rejects are those 1880 user-defined code points plus U+0080 and U+00A2, U+00A3, U+00AC, U+2016, U+2212, U+301C (mappings Python's `cp932` codec accepts for compatibility; the canonical decodes of 0x8191, 0x8192, 0x81CA, 0x8161, 0x817C, 0x8160 do match). - CP1250: 251 of 256 bytes match `cp1250`; Python leaves 0x81, 0x83, 0x88, 0x90 and 0x98 undefined and the table maps them to U+0081, U+0083, U+0088, U+0090 and U+0098. Whether the console maps the user-defined area, those code points or those five bytes is not something I can tell from this, and I am not asking for a change; if useful, the debt entry could list them. Not checked: Linux, macOS, PS5 hardware or a title. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
peeldealer commented 2026-10-18 06:01:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by peeldealer on 2026-10-18T06:01:22Z.


Linux confirmation of @Bonjourcestmatt's point 1, which was only run on Windows.

  • Setup: Ubuntu 24.04 (WSL2), GCC 13.3.0, head 21be453ef, run on its own and merged into current main a86577e46.
  • Result: guest_ces and guest_ces_module both abort, and both pass on main without this PR.
  • Where: a Debug build stops at GuestCes.cpp:219. There the library returns used=7 produced=5 with bytes 47 6F 20 B1 21, so the failure is in the expected counts, not the library, the same as on Windows.

Correcting the counts as described there should fix both platforms. oneandonlydean's two requests (FA40-FC4B, and keeping NotImplemented_nid_no_patch for 0x8F) still apply.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6246349831 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6246349831). Originally posted by [peeldealer](https://github.com/peeldealer) on 2026-10-18T06:01:22Z. --- Linux confirmation of @Bonjourcestmatt's point 1, which was only run on Windows. - **Setup:** Ubuntu 24.04 (WSL2), GCC 13.3.0, head `21be453ef`, run on its own and merged into current `main` `a86577e46`. - **Result:** `guest_ces` and `guest_ces_module` both abort, and both pass on `main` without this PR. - **Where:** a Debug build stops at `GuestCes.cpp:219`. There the library returns `used=7 produced=5` with bytes `47 6F 20 B1 21`, so the failure is in the expected counts, not the library, the same as on Windows. Correcting the counts as described there should fix both platforms. oneandonlydean's two requests (FA40-FC4B, and keeping `NotImplemented_nid_no_patch` for `0x8F`) still apply.
Bl4ckk01 commented 2026-10-18 11:22:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bl4ckk01 on 2026-10-18T11:22:28Z.


thank you @Bonjourcestmatt and @peeldealer
Fixed in 1ff25ef: corrected the Shift-JIS half-width katakana string expectation in GuestCes.cpp from 4 to 5 bytes (47 6F 20 B1 21). Compiled and verified the full test suite locally; guest_ces now passes with exit code 0.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6249310474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6249310474). Originally posted by [Bl4ckk01](https://github.com/Bl4ckk01) on 2026-10-18T11:22:28Z. --- thank you @Bonjourcestmatt and @peeldealer Fixed in 1ff25ef: corrected the Shift-JIS half-width katakana string expectation in `GuestCes.cpp` from 4 to 5 bytes (`47 6F 20 B1 21`). Compiled and verified the full test suite locally; `guest_ces` now passes with exit code 0.
adriadam10 commented 2026-10-19 17:51:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T17:51:23Z.


Checked the new head against the earlier requests: IBM extensions FA40-FC4B now decode (first 28 cells via the head table, the rest through NEC rows 89-92; FBFC → U+9AD9, FC4B ok, FC4C unassigned), the test pins 7724 cells with round-trip to the lowest cell, the unrelated EUC-JP hunk is gone, the half-width katakana counts are 5, and the debt entry lists the user-defined area as UNASSIGNED_CODE; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6273615391 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7091#issuecomment-6273615391). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T17:51:23Z. --- Checked the new head against the earlier requests: IBM extensions FA40-FC4B now decode (first 28 cells via the head table, the rest through NEC rows 89-92; FBFC → U+9AD9, FC4B ok, FC4C unassigned), the test pins 7724 cells with round-trip to the lowest cell, the unrelated EUC-JP hunk is gone, the half-width katakana counts are 5, and the debt entry lists the user-defined area as `UNASSIGNED_CODE`; merged.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7091
No description provided.