Files
romm/backend/utils/psn.py
Matt Kaatman 414fe15d5b fix(feeds): send real content IDs and RAP keys in PKGi and PKGj feeds (#5818)
* fix(feeds): send real content IDs and RAP keys in PKGi and PKGj feeds

The rap column carried the SHA-1 of the .rap file. PKGi decodes it as the
16-byte license key, so it saved a wrong license. The content ID was always
generated (UP9644-...), so even a correct license was saved under a name the
game never looks for.

Read the content ID from the package header, and the key from the rom's .rap
file named after that content ID (or its only .rap file).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(feeds): read PSN licenses through the roms filesystem handler

Move the package content ID and .rap license lookup from the feeds
endpoint into FSRomsHandler, read at most 17 bytes of a .rap file, and
trim the get_rap_data docstring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-12-01 10:06:17 -05:00

35 lines
1.1 KiB
Python

import re
from pathlib import Path
from typing import Final
PKG_MAGIC: Final = b"\x7fPKG"
PKG_CONTENT_ID_OFFSET: Final = 0x30
PKG_CONTENT_ID_LENGTH: Final = 0x24
RAP_SIZE: Final = 16
CONTENT_ID_REGEX: Final = re.compile(r"^[A-Z]{2}\d{4}-[A-Z0-9]{9}_\d{2}-[A-Z0-9]{16}$")
def read_pkg_content_id(path: Path) -> str | None:
"""Return the content ID from the header of a PS3, PSP or PS Vita package, or None."""
try:
with path.open("rb") as f:
header = f.read(PKG_CONTENT_ID_OFFSET + PKG_CONTENT_ID_LENGTH)
except OSError:
return None
if not header.startswith(PKG_MAGIC):
return None
raw = header[PKG_CONTENT_ID_OFFSET:].split(b"\0", 1)[0]
content_id = raw.decode("ascii", "replace")
return content_id if CONTENT_ID_REGEX.match(content_id) else None
def read_rap_key(path: Path) -> str | None:
"""Return the 16-byte key of a `.rap` license as uppercase hex, or None."""
try:
with path.open("rb") as f:
data = f.read(RAP_SIZE + 1)
except OSError:
return None
return data.hex().upper() if len(data) == RAP_SIZE else None