feat(libSceNpCommon): implement sceNpUtilXmlEscape and sceNpUtilXmlUnescape #12818

Merged
hypermoist merged 4 commits from upstream-pr/12818 into main 2026-11-02 16:13:36 +00:00
hypermoist commented 2026-10-29 11:11:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-29T11:11:22Z.


What

Implements sceNpUtilXmlEscape and sceNpUtilXmlUnescape in place of their NotImplemented stubs. Both take (char* dst, size_t dstSize, const char* src, size_t srcLength, size_t* required). The signature and behaviour are read from libSceNpCommon.sprx of firmware 13.60 (disassembly, not measured on a console).

Both

  • 0x80550601 for a null src, a zero srcLength, or neither dst nor required.
  • With dst null, they only measure. Otherwise they write a terminator, and *required gets the length including it.
  • 0x80550602 when dst is too small, checked before each piece is written and again for the terminator. Bytes written before that point stay written.

sceNpUtilXmlEscape

  • " & ' < > become &quot; &amp; &apos; &lt; &gt;, and other printable ASCII, tab, LF and CR are copied.
  • 2-, 3- and 4-byte UTF-8 sequences are copied whole. Lead bytes 0xC2-0xDF, 0xEx and 0xF0-0xF7 need their continuation bytes. A 3-byte sequence isn't checked for overlong forms or surrogates.
  • NUL, the other control characters, DEL and any other byte return 0x80550608.
  • This reuses Strings.cpp's existing Utf8SequenceLength, IsValidSequence and IsAllowedTextByte, which already encode those rules for the JSON helpers.

sceNpUtilXmlUnescape

  • The five entities become their characters. Any other & and every other byte, NUL included, are copied.

Bounds, as in the module: neither function bounds an entity or a UTF-8 sequence by srcLength. A sequence whose lead byte is inside the source is copied whole, and an entity whose & is inside it is matched whole, reading the bytes after the source. The tests pin this ("&amp;" with length 1 gives &).

The debt file replaces the two "signature unknown" entries with one entry stating the source and the unmeasured points.

Tested

Linux (CachyOS, GCC, Release): ctest --test-dir build -R 'public_headers|np_common' --output-on-failure, 70/70 passed. TestXmlText in GuestNpCommonRuntime.cpp covers the entities both ways, measuring, UTF-8 and whitespace pass-through, each refused byte class, the buffer edges, the argument errors and the reads past srcLength.

Comparison against the firmware code: I relinked the 13.60 module with this repo's relinker and ran both functions under unicorn (x86-64 emulation on a PC, not a console). Neither calls an import.

  • Corpus: 10,290 cases, made of a fixed set and 4,000 random inputs per function. Inputs are built from entities, partial entities, valid, overlong and broken UTF-8, control bytes and DEL. Random bytes follow the source, and buffer sizes from 0 to 80 are used with and without dst and required.
  • What was recorded: the result, every byte of dst plus 16 bytes past dstSize (prefilled 0xCC), and *required.
  • Result: replaying the same cases through this implementation gives 0 mismatches. Corrupted expectations are detected.

Windows not built. Not run on a console.

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

Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5066924946 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12818). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-29T11:11:22Z. --- ### What Implements `sceNpUtilXmlEscape` and `sceNpUtilXmlUnescape` in place of their `NotImplemented` stubs. Both take `(char* dst, size_t dstSize, const char* src, size_t srcLength, size_t* required)`. The signature and behaviour are read from `libSceNpCommon.sprx` of firmware 13.60 (disassembly, not measured on a console). **Both** - `0x80550601` for a null `src`, a zero `srcLength`, or neither `dst` nor `required`. - With `dst` null, they only measure. Otherwise they write a terminator, and `*required` gets the length including it. - `0x80550602` when `dst` is too small, checked before each piece is written and again for the terminator. Bytes written before that point stay written. **`sceNpUtilXmlEscape`** - `"` `&` `'` `<` `>` become `&quot;` `&amp;` `&apos;` `&lt;` `&gt;`, and other printable ASCII, tab, LF and CR are copied. - 2-, 3- and 4-byte UTF-8 sequences are copied whole. Lead bytes 0xC2-0xDF, 0xEx and 0xF0-0xF7 need their continuation bytes. A 3-byte sequence isn't checked for overlong forms or surrogates. - NUL, the other control characters, DEL and any other byte return `0x80550608`. - This reuses `Strings.cpp`'s existing `Utf8SequenceLength`, `IsValidSequence` and `IsAllowedTextByte`, which already encode those rules for the JSON helpers. **`sceNpUtilXmlUnescape`** - The five entities become their characters. Any other `&` and every other byte, NUL included, are copied. **Bounds, as in the module:** neither function bounds an entity or a UTF-8 sequence by `srcLength`. A sequence whose lead byte is inside the source is copied whole, and an entity whose `&` is inside it is matched whole, reading the bytes after the source. The tests pin this (`"&amp;"` with length 1 gives `&`). The debt file replaces the two "signature unknown" entries with one entry stating the source and the unmeasured points. ### Tested Linux (CachyOS, GCC, Release): `ctest --test-dir build -R 'public_headers|np_common' --output-on-failure`, 70/70 passed. `TestXmlText` in `GuestNpCommonRuntime.cpp` covers the entities both ways, measuring, UTF-8 and whitespace pass-through, each refused byte class, the buffer edges, the argument errors and the reads past `srcLength`. **Comparison against the firmware code:** I relinked the 13.60 module with this repo's relinker and ran both functions under unicorn (x86-64 emulation on a PC, not a console). Neither calls an import. - **Corpus:** 10,290 cases, made of a fixed set and 4,000 random inputs per function. Inputs are built from entities, partial entities, valid, overlong and broken UTF-8, control bytes and DEL. Random bytes follow the source, and buffer sizes from 0 to 80 are used with and without `dst` and `required`. - **What was recorded:** the result, every byte of `dst` plus 16 bytes past `dstSize` (prefilled 0xCC), and `*required`. - **Result:** replaying the same cases through this implementation gives **0 mismatches**. Corrupted expectations are detected. Windows not built. Not run on a console. ### 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 Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
rooklz commented 2026-10-29 11:50:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T11:50:59Z.

Review: COMMENTED


Reviewed on macOS arm64 (Apple clang, ASan + UBSan, -fno-sanitize-recover=all) at 4b94c6905c, as a standalone build of libSceNpCommon/Strings.cpp and TestXmlText. I didn't run the full CMake lane.

Result: TestXmlText passes with no sanitizer reports. git merge-tree against main ba5b5330b3 is clean, check_conventions --head passes, and pr_overlap probed 1403 open PRs and found no overlap (7 share files, all merge cleanly). The provenance is the module's code (firmware 13.60 disassembly), which REVERSE_ENGINEERING.md lists as a source, and the debt entry says it wasn't measured on a console. The implementation is written from the contract rather than mirroring a disassembly.

Mutants (16 single-edit changes to Strings.cpp). The existing test kills 6. Some of the behaviour the body describes isn't pinned:

  • Escape: dropping the per-piece size check, or making it count the terminator (>=), still passes. So does "bytes written before the error stay written".
  • Unescape: dropping the per-character check, or checking length + 2, still passes.
  • Bounding an entity by srcLength in unescape still passes. The existing "&amp;"/1 row only covers the &-is-last case, where both versions copy &.
  • Escape refusing 0xF5–0xF7 leads, or refusing 3-byte surrogates (body: "not checked"), still passes.

The rows below kill all 10 non-equivalent survivors. The last 2 mutants I tried turned out to be equivalent. Head is green and every row is red on its mutant:

    struct {
        char buffer[4];
        char canary[4];
    } bounded {};
    Require(sceNpUtilXmlEscape(bounded.buffer, 3, "ab&", 3, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "ab\0", 3) == 0
            && bounded.buffer[3] == 0 && bounded.canary[0] == 0,
        "xml escaping keeps the pieces that fit and writes nothing past the buffer");
    Require(sceNpUtilXmlEscape(bounded.buffer, 4, "<", 1, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "&lt;", 4) == 0
            && bounded.canary[0] == 0,
        "an entity that fits stays written when the terminator does not");
    std::memset(&bounded, 0, sizeof(bounded));
    Require(sceNpUtilXmlUnescape(bounded.buffer, 2, "abc", 3, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "ab\0", 3) == 0
            && bounded.canary[0] == 0,
        "xml unescaping keeps the characters that fit and writes nothing past the buffer");
    Require(sceNpUtilXmlUnescape(unescaped, sizeof(unescaped), "&amp;", 2, &length) == 0 && Equals(unescaped, "&") && length == 2,
        "an entity is matched past the source length");
    Require(sceNpUtilXmlEscape(escaped, sizeof(escaped), "\xF7\xBF\xBF\xBF\xED\xA0\x80", 7, &length) == 0 && length == 8,
        "xml escaping accepts lead bytes up to 0xF7 and does not check surrogates");
    length = 99;
    Require(sceNpUtilXmlEscape(escaped, 2, "ab", 2, &length) == NpUtilBufferTooSmall && length == 99, "a failed xml escape leaves the length untouched");

(These go after the last sceNpUtilXmlUnescape argument row. Each one is a claim from your body or the debt entry, so if your unicorn corpus disagrees with any of them, the corpus wins.)

Nits

  • The body's closing line says "all test done locally on actual hardware", but Tested says "Not run on a console" and the debt entry says "not measured on a console". One of them should be fixed.
  • The unicorn differential replay is strong evidence. If you can, attach the case generator (not the module or its outputs) so the corpus can be re-run after a later change.

AI-assisted review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697201088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12818#pullrequestreview-5697201088). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T11:50:59Z. **Review:** COMMENTED --- Reviewed on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`) at 4b94c6905c, as a standalone build of `libSceNpCommon/Strings.cpp` and `TestXmlText`. I didn't run the full CMake lane. **Result:** `TestXmlText` passes with no sanitizer reports. `git merge-tree` against main ba5b5330b3 is clean, `check_conventions --head` passes, and `pr_overlap` probed 1403 open PRs and found no overlap (7 share files, all merge cleanly). The provenance is the module's code (firmware 13.60 disassembly), which `REVERSE_ENGINEERING.md` lists as a source, and the debt entry says it wasn't measured on a console. The implementation is written from the contract rather than mirroring a disassembly. **Mutants (16 single-edit changes to Strings.cpp).** The existing test kills 6. Some of the behaviour the body describes isn't pinned: - Escape: dropping the per-piece size check, or making it count the terminator (`>=`), still passes. So does "bytes written before the error stay written". - Unescape: dropping the per-character check, or checking `length + 2`, still passes. - Bounding an entity by `srcLength` in unescape still passes. The existing `"&amp;"`/1 row only covers the `&`-is-last case, where both versions copy `&`. - Escape refusing 0xF5–0xF7 leads, or refusing 3-byte surrogates (body: "not checked"), still passes. The rows below kill all 10 non-equivalent survivors. The last 2 mutants I tried turned out to be equivalent. Head is green and every row is red on its mutant: ```cpp struct { char buffer[4]; char canary[4]; } bounded {}; Require(sceNpUtilXmlEscape(bounded.buffer, 3, "ab&", 3, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "ab\0", 3) == 0 && bounded.buffer[3] == 0 && bounded.canary[0] == 0, "xml escaping keeps the pieces that fit and writes nothing past the buffer"); Require(sceNpUtilXmlEscape(bounded.buffer, 4, "<", 1, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "&lt;", 4) == 0 && bounded.canary[0] == 0, "an entity that fits stays written when the terminator does not"); std::memset(&bounded, 0, sizeof(bounded)); Require(sceNpUtilXmlUnescape(bounded.buffer, 2, "abc", 3, &length) == NpUtilBufferTooSmall && std::memcmp(bounded.buffer, "ab\0", 3) == 0 && bounded.canary[0] == 0, "xml unescaping keeps the characters that fit and writes nothing past the buffer"); Require(sceNpUtilXmlUnescape(unescaped, sizeof(unescaped), "&amp;", 2, &length) == 0 && Equals(unescaped, "&") && length == 2, "an entity is matched past the source length"); Require(sceNpUtilXmlEscape(escaped, sizeof(escaped), "\xF7\xBF\xBF\xBF\xED\xA0\x80", 7, &length) == 0 && length == 8, "xml escaping accepts lead bytes up to 0xF7 and does not check surrogates"); length = 99; Require(sceNpUtilXmlEscape(escaped, 2, "ab", 2, &length) == NpUtilBufferTooSmall && length == 99, "a failed xml escape leaves the length untouched"); ``` (These go after the last `sceNpUtilXmlUnescape` argument row. Each one is a claim from your body or the debt entry, so if your unicorn corpus disagrees with any of them, the corpus wins.) **Nits** - The body's closing line says "all test done locally on actual hardware", but Tested says "Not run on a console" and the debt entry says "not measured on a console". One of them should be fixed. - The unicorn differential replay is strong evidence. If you can, attach the case generator (not the module or its outputs) so the corpus can be re-run after a later change. AI-assisted review.
rooklz commented 2026-10-29 12:34:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T12:34:31Z.

Review: APPROVED


Re-reviewed at 01a22cc28b on macOS arm64 (Apple clang, ASan + UBSan, -fno-sanitize-recover=all). I used the same standalone build as before: Strings.cpp plus TestXmlText and its helpers, taken verbatim from the PR.

  • Head: TestXmlText passes.
  • The 8 mutants that survived last time are now all killed: escape per-piece check dropped / >=, partial write undone, unescape per-character check dropped / length + 2, entity bounded by srcLength, 0xF5–0xF7 leads refused, 3-byte surrogates refused.
  • required written on the terminator failure is killed by the new length == 99 row. Writing it on the per-piece failure path still survives, because that row only reaches the terminator check. This is minor. If you want it pinned, length = 99; sceNpUtilXmlEscape(escaped, 1, "ab", 2, &length) → 0x80550602, length == 99 would cover it, provided your differential agrees.
  • check_conventions --head, git diff --check and git merge-tree against main 5454cd2e45 are all clean.
  • The body now says "not on an actual console", so my earlier nit is resolved.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697754952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12818#pullrequestreview-5697754952). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T12:34:31Z. **Review:** APPROVED --- Re-reviewed at 01a22cc28b on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`). I used the same standalone build as before: `Strings.cpp` plus `TestXmlText` and its helpers, taken verbatim from the PR. - Head: `TestXmlText` passes. - The 8 mutants that survived last time are now all killed: escape per-piece check dropped / `>=`, partial write undone, unescape per-character check dropped / `length + 2`, entity bounded by `srcLength`, 0xF5–0xF7 leads refused, 3-byte surrogates refused. - `required` written on the terminator failure is killed by the new `length == 99` row. Writing it on the per-piece failure path still survives, because that row only reaches the terminator check. This is minor. If you want it pinned, `length = 99; sceNpUtilXmlEscape(escaped, 1, "ab", 2, &length)` → `0x80550602`, `length == 99` would cover it, provided your differential agrees. - `check_conventions --head`, `git diff --check` and `git merge-tree` against main 5454cd2e45 are all clean. - The body now says "not on an actual console", so my earlier nit is resolved. Approving.
CnRJay commented 2026-11-01 19:54:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:04Z.

Review: COMMENTED


Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (Unblocks: none is allowed), and it contained no code review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729937180 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12818#pullrequestreview-5729937180). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:54:04Z. **Review:** COMMENTED --- Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (`Unblocks: none` is allowed), and it contained no code review.
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!12818
No description provided.