feat(libc): export _Btowc and _Wctob, which give btowc and wctob results in the console's libc #11734

Merged
verusferro merged 1 commit from upstream-pr/11734 into main 2026-10-29 11:57:41 +00:00
verusferro commented 2026-10-27 06:22:01 +00:00 (Migrated from github.com)

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


What

libc did not export Dinkumware's _Btowc and _Wctob, which the console's libc.prx exports. In the SDK 7.00 libc.prx that PPSA06416 ships in sce_module/ (0x6e0e0 and 0x57710, read from its disassembly, REVERSE_ENGINEERING source 2):

  • _Btowc(c) returns WEOF for EOF. Otherwise it converts the byte with _Mbtowc and a zeroed state, and returns the 16-bit result if _Mbtowc returned 0 or more, WEOF if not.
  • _Wctob(c) returns EOF for WEOF. Otherwise it converts the low 16 bits with _Wctomb and a zeroed state, and returns the byte if exactly one came out, EOF if not.

Run in a host process, they give the same results and errno as this library's btowc and wctob for every input below, so the new exports call those.

_Mbtowc and _Wctomb are left out: here they would call _Mbtowcx and _Wctombx, which convert through the host's mbrtowc and wcrtomb and throw for invalid or incomplete input, and I have not compared those with the module.

Tested

Ubuntu 24.04 (WSL2) x86-64, GCC 13.3, Release, full build with BUILD_TESTING=ON, on main 2227f13661:

  • The title's libc.prx, converted by the relinker and loaded in a host process, against this library's unpatched libc.prx: _Btowc for every value from -1 to 255, and _Wctob for every value from 0 to 0x10000 and for 0xffffffff. The result and errno match in all 65,795 calls. On the earlier base fe2b52c7e4 the same held for 65,808 calls that also covered INT_MIN, INT_MAX and values above 0x10000, and JJenkx found no difference against the libc.prx of 11 titles.
  • No test is added: the exports call btowc and wctob, which guest_rune_locale and guest_multibyte_locale test, and CONTRIBUTING asks for no export-only tests.
  • ctest -R '^(guest_rune_locale|guest_multibyte_locale|guest_wide_conversion|patched_libraries|conventions_unknown_nids)$': 5/5 pass.
  • tools/progress.py: +2 implemented. libc goes from 1419/1436 to 1421/1438.
  • python3 tools/check_conventions.py --base origin/main: passed. python3 tools/pr_overlap.py --branch feat/libc-btowc-wctob-internals: no conflicting lines and no same exports. Of the pull requests it could not compare, #10324 and #10749 change other lines of Api.hpp, and git merge-tree of either with this branch conflicts only in the files where they already conflict with main.

Not tested: Windows, macOS, a title that calls these functions, a console run.

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:5029571286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11734). Originally posted by [verusferro](https://github.com/verusferro) on 2026-10-27T06:22:01Z. --- ### What libc did not export Dinkumware's `_Btowc` and `_Wctob`, which the console's `libc.prx` exports. In the SDK 7.00 `libc.prx` that PPSA06416 ships in `sce_module/` (0x6e0e0 and 0x57710, read from its disassembly, [REVERSE_ENGINEERING](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/REVERSE_ENGINEERING.md) source 2): - `_Btowc(c)` returns `WEOF` for `EOF`. Otherwise it converts the byte with `_Mbtowc` and a zeroed state, and returns the 16-bit result if `_Mbtowc` returned 0 or more, `WEOF` if not. - `_Wctob(c)` returns `EOF` for `WEOF`. Otherwise it converts the low 16 bits with `_Wctomb` and a zeroed state, and returns the byte if exactly one came out, `EOF` if not. Run in a host process, they give the same results and `errno` as this library's `btowc` and `wctob` for every input below, so the new exports call those. `_Mbtowc` and `_Wctomb` are left out: here they would call `_Mbtowcx` and `_Wctombx`, which convert through the host's `mbrtowc` and `wcrtomb` and throw for invalid or incomplete input, and I have not compared those with the module. ### 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. --> Ubuntu 24.04 (WSL2) x86-64, GCC 13.3, Release, full build with `BUILD_TESTING=ON`, on `main` `2227f13661`: - The title's `libc.prx`, converted by the relinker and loaded in a host process, against this library's unpatched `libc.prx`: `_Btowc` for every value from -1 to 255, and `_Wctob` for every value from 0 to 0x10000 and for 0xffffffff. The result and `errno` match in all 65,795 calls. On the earlier base `fe2b52c7e4` the same held for 65,808 calls that also covered `INT_MIN`, `INT_MAX` and values above 0x10000, and JJenkx [found no difference](https://github.com/boykopovar/AnyPS5/pull/11734#issuecomment-6444048160) against the `libc.prx` of 11 titles. - No test is added: the exports call `btowc` and `wctob`, which `guest_rune_locale` and `guest_multibyte_locale` test, and CONTRIBUTING asks for no export-only tests. - `ctest -R '^(guest_rune_locale|guest_multibyte_locale|guest_wide_conversion|patched_libraries|conventions_unknown_nids)$'`: 5/5 pass. - `tools/progress.py`: +2 implemented. libc goes from 1419/1436 to 1421/1438. - `python3 tools/check_conventions.py --base origin/main`: passed. `python3 tools/pr_overlap.py --branch feat/libc-btowc-wctob-internals`: no conflicting lines and no same exports. Of the pull requests it could not compare, #10324 and #10749 change other lines of `Api.hpp`, and `git merge-tree` of either with this branch conflicts only in the files where they already conflict with `main`. Not tested: Windows, macOS, a title that calls these functions, a console run. ### 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
MarcZX10 commented 2026-10-27 06:30:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:30:32Z.

Review: APPROVED


Reviewed exact head d489ecd7a556b9cf217ea9214ad0cad8671dd8e2. Both internal exports are thin aliases to the existing btowc/wctob implementations, with matching parameter/result widths and no duplicated conversion logic. The description carefully limits the claim about _Mbtowc/_Wctomb, and reports exhaustive byte coverage plus all 65,536 16-bit inputs against the shipped module, including errno.

The author reports a full Ubuntu 24.04/WSL2 build and 5/5 focused locale tests. Hosted conventions, intake, labels, Linux/Windows relinker, and ASan checks pass; macOS relinker jobs remain queued and full Linux/Windows product jobs are skipped by repository policy. I did not independently rerun the module comparison or full build.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663087494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11734#pullrequestreview-5663087494). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:30:32Z. **Review:** APPROVED --- Reviewed exact head `d489ecd7a556b9cf217ea9214ad0cad8671dd8e2`. Both internal exports are thin aliases to the existing `btowc`/`wctob` implementations, with matching parameter/result widths and no duplicated conversion logic. The description carefully limits the claim about `_Mbtowc`/`_Wctomb`, and reports exhaustive byte coverage plus all 65,536 16-bit inputs against the shipped module, including `errno`. The author reports a full Ubuntu 24.04/WSL2 build and 5/5 focused locale tests. Hosted conventions, intake, labels, Linux/Windows relinker, and ASan checks pass; macOS relinker jobs remain queued and full Linux/Windows product jobs are skipped by repository policy. I did not independently rerun the module comparison or full build.
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!11734
No description provided.