feat(libSceNpCommon): implement sceNpUtilParseJid and sceNpUtilJidToNpId #12825

Merged
hypermoist merged 4 commits from upstream-pr/12825 into main 2026-11-01 11:22:14 +00:00
hypermoist commented 2026-10-29 11:42:17 +00:00 (Migrated from github.com)

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


What

Implements sceNpUtilParseJid and sceNpUtilJidToNpId in place of their NotImplemented stubs. The signatures and behaviour are read from libSceNpCommon.sprx of firmware 13.60 (disassembly, not measured on a console). NpJid is the 32-byte structure the parser fills: user, domain and resource pointers plus a reserved word.

sceNpUtilParseJid(NpJid* jid, const char* text, char* buffer, size_t bufferSize, size_t* required)

  • 0x80550601 for a null text, or for no jid/buffer pair and no required.
  • With both a jid and a buffer, it clears the jid first. Otherwise it only measures.
  • Leading spaces are skipped. An empty or blank string returns 0x80550604.
  • The user is [A-Za-z0-9+._-]+ followed by @. Without that @ the user is empty and the text is a domain.
  • After an optional @, the domain is [A-Za-z0-9._-]+ with trailing dots trimmed.
  • Quirk: the module skips exactly one character after the trimmed domain, whatever it is (a trimmed dot, / or a space), and keeps the rest unchecked as the resource. So a@b./res gives resource /res, and a@b c gives resource c.
  • Each part is copied with a terminator into buffer and the jid points at the copies. *required is the total size.
  • A buffer that is too small returns 0x80550602 and clears the jid again.

sceNpUtilJidToNpId(NpIdFull* npId, const NpJid* jid)

  • 0x80550601 for a null pointer.
  • 0x80550604 unless the user is 1 to 16 bytes, the domain is XX.YY.<label>.playstation.net (the four characters letters or digits, the label possibly empty) and the resource is at most 4 bytes.
  • The npid is cleared and then gets the user as the online id, the four domain characters and the resource in opt, and reserved[0] = 1.

The debt file replaces the two "signature unknown" entries with one entry that states the source, the quirks, and that 0x80550604 has no name in any source. It also removes sceNpUtilJidToNpId from the entry that says it throws because it depends on network domain names.

Tested

Linux (CachyOS, GCC, Release): ctest --test-dir build -R 'np_common|public_headers' --output-on-failure, 70/70 passed. TestJid in GuestNpCommonRuntime.cpp covers:

  • Parsing: the split, leading spaces, the domain-only form and both skip quirks, measuring, the buffer edges, the blank and argument errors.
  • Conversion: a PSN JID, a 16-byte user with an empty label, and each rejected shape.

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). The module's strlen, strncmp, memcpy, memset and _Getpctype imports were bound to C-locale implementations.

  • Corpus: 9,680 cases. For the parser: fixed and 5,000 random strings built from user, domain and glue fragments, with leading spaces, buffer sizes 0 to 80, and each null-pointer combination. For the conversion: fixed and 3,000 random JIDs, about a fifth of them valid.
  • What was recorded: the result, every byte of the buffer or npid plus 16 or 8 bytes past it (prefilled 0xCC), where each jid pointer points, the reserved word 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 (also changes Strings.cpp, like #12818; the two merge cleanly in either order)
  • 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:5067339945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12825). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-29T11:42:17Z. --- ### What Implements `sceNpUtilParseJid` and `sceNpUtilJidToNpId` in place of their `NotImplemented` stubs. The signatures and behaviour are read from `libSceNpCommon.sprx` of firmware 13.60 (disassembly, not measured on a console). `NpJid` is the 32-byte structure the parser fills: user, domain and resource pointers plus a reserved word. **`sceNpUtilParseJid(NpJid* jid, const char* text, char* buffer, size_t bufferSize, size_t* required)`** - `0x80550601` for a null `text`, or for no `jid`/`buffer` pair and no `required`. - With both a `jid` and a `buffer`, it clears the `jid` first. Otherwise it only measures. - Leading spaces are skipped. An empty or blank string returns `0x80550604`. - The user is `[A-Za-z0-9+._-]+` followed by `@`. Without that `@` the user is empty and the text is a domain. - After an optional `@`, the domain is `[A-Za-z0-9._-]+` with trailing dots trimmed. - **Quirk:** the module skips exactly one character after the trimmed domain, whatever it is (a trimmed dot, `/` or a space), and keeps the rest unchecked as the resource. So `a@b./res` gives resource `/res`, and `a@b c` gives resource `c`. - Each part is copied with a terminator into `buffer` and the `jid` points at the copies. `*required` is the total size. - A buffer that is too small returns `0x80550602` and clears the `jid` again. **`sceNpUtilJidToNpId(NpIdFull* npId, const NpJid* jid)`** - `0x80550601` for a null pointer. - `0x80550604` unless the user is 1 to 16 bytes, the domain is `XX.YY.<label>.playstation.net` (the four characters letters or digits, the label possibly empty) and the resource is at most 4 bytes. - The npid is cleared and then gets the user as the online id, the four domain characters and the resource in `opt`, and `reserved[0] = 1`. The debt file replaces the two "signature unknown" entries with one entry that states the source, the quirks, and that `0x80550604` has no name in any source. It also removes `sceNpUtilJidToNpId` from the entry that says it throws because it depends on network domain names. ### Tested Linux (CachyOS, GCC, Release): `ctest --test-dir build -R 'np_common|public_headers' --output-on-failure`, 70/70 passed. `TestJid` in `GuestNpCommonRuntime.cpp` covers: - **Parsing:** the split, leading spaces, the domain-only form and both skip quirks, measuring, the buffer edges, the blank and argument errors. - **Conversion:** a PSN JID, a 16-byte user with an empty label, and each rejected shape. **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). The module's `strlen`, `strncmp`, `memcpy`, `memset` and `_Getpctype` imports were bound to C-locale implementations. - **Corpus:** 9,680 cases. For the parser: fixed and 5,000 random strings built from user, domain and glue fragments, with leading spaces, buffer sizes 0 to 80, and each null-pointer combination. For the conversion: fixed and 3,000 random JIDs, about a fifth of them valid. - **What was recorded:** the result, every byte of the buffer or npid plus 16 or 8 bytes past it (prefilled 0xCC), where each `jid` pointer points, the reserved word 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 (also changes `Strings.cpp`, like #12818; the two merge cleanly in either order) - [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:56:37 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

Result: TestJid passes, with no sanitizer reports. check_conventions --head, git diff --check and git merge-tree against main ba5b5330b3 are clean. It also merges cleanly with #12818. pr_overlap (1406 PRs probed) only reports #12829 and #12830. Both are stacked on this commit and declare Depends on, and both merge cleanly with it.

Mutation check: I applied 25 single-line mutants to SplitJid, sceNpUtilParseJid and sceNpUtilJidToNpId.

  • Killed (15).
  • Equivalent (2):
    • Dropping user != 0 gives the same empty user.
    • Dropping the second *jid = {} on a too-small buffer changes nothing, because the jid is cleared at entry and its fields are only assigned after the loop.
  • Survived (8, non-equivalent): each of these changes the result for some input and still passes TestJid:
    1. - removed from the user bytes.
    2. _ removed from the domain bytes.
    3. + added to the domain bytes.
    4. The trailing-dot while turned into an if, so only one dot is trimmed.
    5. The entry clear of *jid removed. A blank text then returns 0x80550604 with the caller's old pointers left in place.
    6. find_first_not_of(" \t"), so tabs would also be skipped.
    7. IsAsciiAlnum(domain[3]) not checked.
    8. strcmp turned into strncmp(..., 15), so .playstation.network would be accepted.

These rows go into TestJid before the nullptr text row:

    Require(sceNpUtilParseJid(&jid, "a-b@c_d", buffer, sizeof(buffer), &length) == 0 && Equals(jid.user, "a-b") && Equals(jid.domain, "c_d"),
        "a user may hold a dash and a domain an underscore");
    Require(sceNpUtilParseJid(&jid, "a@b+c", buffer, sizeof(buffer), &length) == 0 && Equals(jid.domain, "b") && Equals(jid.resource, "c"),
        "a plus sign ends the domain");
    Require(sceNpUtilParseJid(&jid, "a@b../r", buffer, sizeof(buffer), &length) == 0 && Equals(jid.domain, "b") && Equals(jid.resource, "./r"),
        "every trailing dot leaves the domain but only one is skipped");
    Require(sceNpUtilParseJid(&jid, "\ta@b", buffer, sizeof(buffer), &length) == 0 && Equals(jid.user, "") && Equals(jid.domain, "")
            && Equals(jid.resource, "a@b"),
        "only spaces are skipped before a jid");
    jid = {buffer, buffer, buffer, 0};
    Require(sceNpUtilParseJid(&jid, "   ", buffer, sizeof(buffer), &length) == NpUtilInvalidJid && jid.user == nullptr && jid.resource == nullptr,
        "a refused jid is cleared");

These rows go before the null-pointer conversion row:

    const NpJid shapes[] = {{"u", "a1.-2.np.playstation.net", nullptr, 0}, {"u", "a1.b2.np.playstation.network", nullptr, 0}};
    for (const NpJid& bad : shapes) Require(sceNpUtilJidToNpId(&npId, &bad) == NpUtilInvalidJid, "the domain code is alphanumeric and the suffix exact");

With all of them added, the head passes, all 8 survivors are killed, and only the 2 equivalent mutants remain.

I derived every expectation from what the PR states: C-locale classes, one skipped character after the domain, the exact suffix, and the clear at entry. I couldn't check them against the module myself. Please run them through your unicorn differential before adding them. The tab row in particular rests on "leading spaces" meaning only ' '.

Nit: this is the same one as on #12818. "all test done locally on actual hardware" contradicts "Not run on a console" a few lines above. The differential ran under unicorn on a PC.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697262758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12825#pullrequestreview-5697262758). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T11:56:37Z. **Review:** COMMENTED --- Reviewed on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`) at 9b48ab5214, as a standalone build of `libSceNpCommon/Strings.cpp` and `TestJid`. I didn't run the full CMake lane. **Result:** `TestJid` passes, with no sanitizer reports. `check_conventions --head`, `git diff --check` and `git merge-tree` against main ba5b5330b3 are clean. It also merges cleanly with #12818. `pr_overlap` (1406 PRs probed) only reports #12829 and #12830. Both are stacked on this commit and declare `Depends on`, and both merge cleanly with it. **Mutation check:** I applied 25 single-line mutants to `SplitJid`, `sceNpUtilParseJid` and `sceNpUtilJidToNpId`. - **Killed (15).** - **Equivalent (2):** - Dropping `user != 0` gives the same empty user. - Dropping the second `*jid = {}` on a too-small buffer changes nothing, because the jid is cleared at entry and its fields are only assigned after the loop. - **Survived (8, non-equivalent):** each of these changes the result for some input and still passes `TestJid`: 1. `-` removed from the user bytes. 2. `_` removed from the domain bytes. 3. `+` added to the domain bytes. 4. The trailing-dot `while` turned into an `if`, so only one dot is trimmed. 5. The entry clear of `*jid` removed. A blank text then returns `0x80550604` with the caller's old pointers left in place. 6. `find_first_not_of(" \t")`, so tabs would also be skipped. 7. `IsAsciiAlnum(domain[3])` not checked. 8. `strcmp` turned into `strncmp(..., 15)`, so `.playstation.network` would be accepted. These rows go into `TestJid` before the `nullptr` text row: ```cpp Require(sceNpUtilParseJid(&jid, "a-b@c_d", buffer, sizeof(buffer), &length) == 0 && Equals(jid.user, "a-b") && Equals(jid.domain, "c_d"), "a user may hold a dash and a domain an underscore"); Require(sceNpUtilParseJid(&jid, "a@b+c", buffer, sizeof(buffer), &length) == 0 && Equals(jid.domain, "b") && Equals(jid.resource, "c"), "a plus sign ends the domain"); Require(sceNpUtilParseJid(&jid, "a@b../r", buffer, sizeof(buffer), &length) == 0 && Equals(jid.domain, "b") && Equals(jid.resource, "./r"), "every trailing dot leaves the domain but only one is skipped"); Require(sceNpUtilParseJid(&jid, "\ta@b", buffer, sizeof(buffer), &length) == 0 && Equals(jid.user, "") && Equals(jid.domain, "") && Equals(jid.resource, "a@b"), "only spaces are skipped before a jid"); jid = {buffer, buffer, buffer, 0}; Require(sceNpUtilParseJid(&jid, " ", buffer, sizeof(buffer), &length) == NpUtilInvalidJid && jid.user == nullptr && jid.resource == nullptr, "a refused jid is cleared"); ``` These rows go before the null-pointer conversion row: ```cpp const NpJid shapes[] = {{"u", "a1.-2.np.playstation.net", nullptr, 0}, {"u", "a1.b2.np.playstation.network", nullptr, 0}}; for (const NpJid& bad : shapes) Require(sceNpUtilJidToNpId(&npId, &bad) == NpUtilInvalidJid, "the domain code is alphanumeric and the suffix exact"); ``` With all of them added, the head passes, all 8 survivors are killed, and only the 2 equivalent mutants remain. I derived every expectation from what the PR states: C-locale classes, one skipped character after the domain, the exact suffix, and the clear at entry. I couldn't check them against the module myself. Please run them through your unicorn differential before adding them. The tab row in particular rests on "leading spaces" meaning only `' '`. **Nit:** this is the same one as on #12818. "all test done locally on actual hardware" contradicts "Not run on a console" a few lines above. The differential ran under unicorn on a PC.
rooklz commented 2026-10-29 12:36:49 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

Result: TestJid passes with no sanitizer reports. The new commit only adds the suggested rows to the test (15+/1-, GuestNpCommonRuntime.cpp). check_conventions --head, git diff --check and git merge-tree against main 5454cd2e45 are clean.

Mutation check: I re-applied the 8 mutants that survived last time. All 8 are now killed, each by the row written for it:

Mutant Killed by
- removed from user bytes dash/underscore row
_ removed from domain bytes dash/underscore row
+ added to domain bytes plus-sign row
trailing-dot while → if trailing-dot row
entry clear of *jid removed refused-jid-is-cleared row
find_first_not_of(" \t") only-spaces row
domain[3] check dropped domain-shape loop
strcmp → strncmp(..., 15) domain-shape loop

Thanks for running the rows through the 13.60 differential first. The tab row now rests on a firmware result, not just my reading of the body. The body nit is also resolved.

Cosmetic nit (optional): the rewrapped conversion line lost a space: NpUtilInvalid &&sceNpUtilJidToNpId(&npId, nullptr).

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697782053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12825#pullrequestreview-5697782053). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T12:36:49Z. **Review:** APPROVED --- Re-reviewed at 36cf6fb45f on macOS arm64 (Apple clang, ASan + UBSan, `-fno-sanitize-recover=all`), using the same standalone build as before: `Strings.cpp` plus `TestJid` and its helpers, taken verbatim from the PR. **Result:** `TestJid` passes with no sanitizer reports. The new commit only adds the suggested rows to the test (15+/1-, `GuestNpCommonRuntime.cpp`). `check_conventions --head`, `git diff --check` and `git merge-tree` against main 5454cd2e45 are clean. **Mutation check:** I re-applied the 8 mutants that survived last time. All 8 are now killed, each by the row written for it: | Mutant | Killed by | |---|---| | `-` removed from user bytes | dash/underscore row | | `_` removed from domain bytes | dash/underscore row | | `+` added to domain bytes | plus-sign row | | trailing-dot `while` → `if` | trailing-dot row | | entry clear of `*jid` removed | refused-jid-is-cleared row | | `find_first_not_of(" \t")` | only-spaces row | | `domain[3]` check dropped | domain-shape loop | | `strcmp` → `strncmp(..., 15)` | domain-shape loop | Thanks for running the rows through the 13.60 differential first. The tab row now rests on a firmware result, not just my reading of the body. The body nit is also resolved. **Cosmetic nit (optional):** the rewrapped conversion line lost a space: `NpUtilInvalid &&sceNpUtilJidToNpId(&npId, nullptr)`. Approving.
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!12825
No description provided.