fix(libSceCesCs): terminate and count MBCS string conversions as measured on a PS5 #10185

Merged
orezraey merged 23 commits from upstream-pr/10185 into main 2026-10-25 00:59:07 +00:00
orezraey commented 2026-10-23 12:13:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T12:13:02Z.


What

The MBCS string conversions (sceCesUtf8StrToMbcsStr, sceCesUcs2StrToMbcsStr, sceCesMbcsStrToUtf8Str, sceCesMbcsStrToUcs2Str and their Get*Len forms) wrote no terminator and set no length on an error. I measured them on a retail PS5 (firmware 13.60) with homebrew probes, from a payload process loading libSceCesCs.sprx, with the Cp1252 and EUC-JP profiles. The sources and console logs are in this gist (pces8, pces9, pces10).

On the console:

  • Terminator. The output always gets a terminating zero, which is not counted. A character is written only if it and the terminator fit. Converting ABC into 3 bytes gives AB, a terminator and 0x805C0031 with lengths 2 and 2. With a capacity of 0, nothing is written.
  • Errors keep what was converted. On an error mid-string, the characters before it stay written, terminated and counted. For example, A, 0x81, B in Cp1252 returns 0x805C0020 with lengths 1 and 1 and the output A, terminator.
  • Source limit 0 reads up to the terminating zero. We converted nothing.
  • Order. The lengths are cleared and a terminator is written first. Then the checks run in this order: context (0x805C0001), source (0x805C0010), output (0x805C0030).
  • Decoding.
    • UTF-8 sources decode as sceCesUtf8ToSbc does: overlong forms and surrogates give 0x805C0015, and a zero byte inside a sequence is an invalid byte.
    • A UTF-16 surrogate gives 0x805C0015.
    • EUC-JP sources decode as sceCesEucJpToUcs2 does, so 8F A2 AF gives 0x805C0014.
  • No JIS X 0212 encoding either. U+02D8, U+0386, U+0402 and U+4E02 give 0x805C0020 with the EUC-JP profile. We encoded them as 0x8F triples.

This PR implements all of the above with one conversion loop shared by the eight functions. sceCesMbcsUcsContextInit with a null profile now returns 0x805C0001 as well, as the console did; before, it stored the null profile.

Not changed, recorded in the debt file. The console encodes U+00A5, U+203E and U+0080–U+009F to EUC-JP bytes where we answer UNASSIGNED_CODE. Its Shift-JIS encoder answers 0x805C0024 for U+0080–U+009F and accepts the JIS forms of a few Microsoft characters. These table choices are a separate change. A zero-filled context converted nothing and returned 0 on the console; here it returns 0x805C0001.

Depends on the two scalar PRs, for decodeScalarUtf8 and decodeEucJpScalar. This branch carries their commits until they merge.

On top of #8967 and #8972. The UTF-16 and UTF-32 string forms from #8972 now run through the same loop, so they stop, terminate and count the same way. sceCesUtf8ToSJis, sceCesUtf8ToEucJp and the GBK and EUC-CN forms from #8976 decode UTF-8 like sceCesUtf8ToSbc. JIS X 0212 is neither decoded nor encoded, as measured, so two guest_ces_japanese checks that expected 0x8FB0A1 for U+4E02 now expect INVALID_ENCODE and UNASSIGNED_CODE.

Tested

  • Head after merging main (f26434afa3, with #10706): 4c3c3231d6. The test now takes its declarations from libSceCesCs/include/Api.hpp (sceCesRefersUcsProfileCp1250 returns const void* there, so the test casts it). The 14 guest_ces* tests pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); public_headers audit and check_conventions pass.
    Windows 11, MinGW (winlibs).
  • guest_ces checks:
    • 30 measured string cases with result, both lengths and the exact output bytes up to the terminator;
    • the order and terminator cases;
    • the Get*Len forms;
    • the UCS-2 forms.
  • It fails without the change and passes with it.
  • The existing checks now expect the partial output after a failed call.
  • The EUC-KR, UHC and Big5 round trip in guest_ces_dbcs had output buffers with no room for a terminator. It now keeps that room; before, it passed without converting anything.
  • The ten guest_ces* tests pass three runs in a row.
  • tools/check_conventions.py passes.
  • tools/pr_overlap.py lists #8923, #9912 and #9210 (other code pages in Export.cpp and the tests) and #6044 (moves test files).

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: #10183, #10184, #9175, #10426, #10430, #10431; #9175, #10426, #10430, #10431 edit the same lines too (merge order only); #10718 rewrites the same EUC-JP decoder and debt entries, and #10706 removes the test declarations this PR adds to GuestCes.cpp (merge order only); #10722, #10739, #10751 edit the same lines too (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4982540716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10185). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T12:13:02Z. --- ### What The MBCS string conversions (`sceCesUtf8StrToMbcsStr`, `sceCesUcs2StrToMbcsStr`, `sceCesMbcsStrToUtf8Str`, `sceCesMbcsStrToUcs2Str` and their `Get*Len` forms) wrote no terminator and set no length on an error. I measured them on a retail PS5 (firmware 13.60) with homebrew probes, from a payload process loading `libSceCesCs.sprx`, with the Cp1252 and EUC-JP profiles. The sources and console logs are in [this gist](https://gist.github.com/orezraey/3c6d0bf19e4aee49ff7bdda88d2e9a6a) (`pces8`, `pces9`, `pces10`). On the console: - **Terminator.** The output always gets a terminating zero, which is not counted. A character is written only if it and the terminator fit. Converting `ABC` into 3 bytes gives `AB`, a terminator and `0x805C0031` with lengths 2 and 2. With a capacity of 0, nothing is written. - **Errors keep what was converted.** On an error mid-string, the characters before it stay written, terminated and counted. For example, `A`, 0x81, `B` in Cp1252 returns `0x805C0020` with lengths 1 and 1 and the output `A`, terminator. - **Source limit 0** reads up to the terminating zero. We converted nothing. - **Order.** The lengths are cleared and a terminator is written first. Then the checks run in this order: context (`0x805C0001`), source (`0x805C0010`), output (`0x805C0030`). - **Decoding.** - UTF-8 sources decode as `sceCesUtf8ToSbc` does: overlong forms and surrogates give `0x805C0015`, and a zero byte inside a sequence is an invalid byte. - A UTF-16 surrogate gives `0x805C0015`. - EUC-JP sources decode as `sceCesEucJpToUcs2` does, so `8F A2 AF` gives `0x805C0014`. - **No JIS X 0212 encoding either.** U+02D8, U+0386, U+0402 and U+4E02 give `0x805C0020` with the EUC-JP profile. We encoded them as `0x8F` triples. This PR implements all of the above with one conversion loop shared by the eight functions. `sceCesMbcsUcsContextInit` with a null profile now returns `0x805C0001` as well, as the console did; before, it stored the null profile. **Not changed, recorded in the debt file.** The console encodes U+00A5, U+203E and U+0080–U+009F to EUC-JP bytes where we answer `UNASSIGNED_CODE`. Its Shift-JIS encoder answers `0x805C0024` for U+0080–U+009F and accepts the JIS forms of a few Microsoft characters. These table choices are a separate change. A zero-filled context converted nothing and returned 0 on the console; here it returns `0x805C0001`. **Depends on** the two scalar PRs, for `decodeScalarUtf8` and `decodeEucJpScalar`. This branch carries their commits until they merge. **On top of #8967 and #8972.** The UTF-16 and UTF-32 string forms from #8972 now run through the same loop, so they stop, terminate and count the same way. `sceCesUtf8ToSJis`, `sceCesUtf8ToEucJp` and the GBK and EUC-CN forms from #8976 decode UTF-8 like `sceCesUtf8ToSbc`. JIS X 0212 is neither decoded nor encoded, as measured, so two `guest_ces_japanese` checks that expected `0x8FB0A1` for U+4E02 now expect `INVALID_ENCODE` and `UNASSIGNED_CODE`. ### 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. --> - Head after merging `main` (`f26434afa3`, with #10706): `4c3c3231d6`. The test now takes its declarations from `libSceCesCs/include/Api.hpp` (`sceCesRefersUcsProfileCp1250` returns `const void*` there, so the test casts it). The 14 `guest_ces*` tests pass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC); `public_headers` audit and `check_conventions` pass. Windows 11, MinGW (winlibs). - `guest_ces` checks: - 30 measured string cases with result, both lengths and the exact output bytes up to the terminator; - the order and terminator cases; - the `Get*Len` forms; - the UCS-2 forms. - It fails without the change and passes with it. - The existing checks now expect the partial output after a failed call. - The EUC-KR, UHC and Big5 round trip in `guest_ces_dbcs` had output buffers with no room for a terminator. It now keeps that room; before, it passed without converting anything. - The ten `guest_ces*` tests pass three runs in a row. - `tools/check_conventions.py` passes. - `tools/pr_overlap.py` lists #8923, #9912 and #9210 (other code pages in `Export.cpp` and the tests) and #6044 (moves test files). ### 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: #10183, #10184, #9175, #10426, #10430, #10431; #9175, #10426, #10430, #10431 edit the same lines too (merge order only); #10718 rewrites the same EUC-JP decoder and debt entries, and #10706 removes the test declarations this PR adds to `GuestCes.cpp` (merge order only); #10722, #10739, #10751 edit the same lines too (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 12:25:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T12:25:01Z.

Review: APPROVED


Reviewed the MBCS conversion-loop change at head 00b45d43de, stacked on #10183 and #10184. The shared loop clears lengths and initializes the terminator before validation, preserves converted prefixes on error, reserves terminator capacity, and handles sourceMax=0 as the measured NUL-terminated mode. UTF-8/EUC-JP/UCS-2 decode paths and the new focused cases align with the described firmware measurements. This exact head is included in the descendant #10186 syntax check; its hosted eligibility, conventions, intake, labels and report checks pass, while product builds are path-skipped. The author reports the ten focused tests passing three consecutive runs on Windows. I could not execute them here because full CMake configuration is blocked by the missing SDL2 development package. The declared dependencies match the branch stack.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5628166619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10185#pullrequestreview-5628166619). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T12:25:01Z. **Review:** APPROVED --- Reviewed the MBCS conversion-loop change at head 00b45d43de08fcfcdcc5940c9b6936376f3560b1, stacked on #10183 and #10184. The shared loop clears lengths and initializes the terminator before validation, preserves converted prefixes on error, reserves terminator capacity, and handles sourceMax=0 as the measured NUL-terminated mode. UTF-8/EUC-JP/UCS-2 decode paths and the new focused cases align with the described firmware measurements. This exact head is included in the descendant #10186 syntax check; its hosted eligibility, conventions, intake, labels and report checks pass, while product builds are path-skipped. The author reports the ten focused tests passing three consecutive runs on Windows. I could not execute them here because full CMake configuration is blocked by the missing SDL2 development package. The declared dependencies match the branch stack.
MarcZX10 commented 2026-10-23 13:59:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T13:59:13Z.

Review: APPROVED


Re-review of exact current head e4ed848821e95fb162923e68991d6c32d9d5cfb8. The source/test/debt diff against current main is unchanged by this head's merge-main refresh. Its checklist records the agreed #10183/#10184 prerequisites, and #10186 records #10185 as its prerequisite; #10056 then depends on #10186. Exact-head repository conventions and git diff --check pass, and GCC C++20 strict syntax-only compilation of the CES export passes. Hosted eligibility, conventions, labels and report pass; product test/build jobs are path-skipped. The author reports the focused CES tests passing three runs; no full runtime suite was available locally. Approving this exact head in the agreed stack order.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5629283047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10185#pullrequestreview-5629283047). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T13:59:13Z. **Review:** APPROVED --- Re-review of exact current head `e4ed848821e95fb162923e68991d6c32d9d5cfb8`. The source/test/debt diff against current main is unchanged by this head's merge-main refresh. Its checklist records the agreed #10183/#10184 prerequisites, and #10186 records #10185 as its prerequisite; #10056 then depends on #10186. Exact-head repository conventions and `git diff --check` pass, and GCC C++20 strict syntax-only compilation of the CES export passes. Hosted eligibility, conventions, labels and report pass; product test/build jobs are path-skipped. The author reports the focused CES tests passing three runs; no full runtime suite was available locally. Approving this exact head in the agreed stack order.
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!10185
No description provided.