#!/usr/bin/env python3 """ rcoc.py - open compiler and reader for PS Vita PAF resources (.rco / .rcs). Sony's acdc.exe is not available, so this implements the format directly. It can be verified against any XML/RCO pair: `verify` recompiles the XML and compares each section with the reference RCO. Format (CXML, version 0x110): "RCOF"|"RCSF", u32 version, then 9 (offset, size) pairs, sections 16-aligned: 0 tree 7-u32 tag (name, nattr, parent, prev, next, first, last) + attrs of 4 u32 (name, type, a, b) 1 idstr (u32 owner, zero-terminated string padded to 4) 2 idhash (u32 owner or -1, u32 hash) 3 strings zero-terminated, UTF-8 4 wstrings zero-terminated, UTF-16LE 5 hashes u32 6 ints i32 7 floats f32 8 files raw blobs, 16-aligned Hash of a name: first 4 bytes of SHA-1, big endian. Usage: rcoc.py compile in.xml -o out.rco [-x names.h] [--schema tools/rco_schema.json] rcoc.py dump in.rco rcoc.py schema a.rco b.rco ... -o tools/rco_schema.json rcoc.py verify in.xml reference.rco """ import argparse import hashlib import json import os import re import struct import sys import xml.etree.ElementTree as ET NONE = 0xFFFFFFFF T_INT, T_FLOAT, T_STR, T_WSTR, T_HASH, T_INTS, T_FLOATS, T_FILE = 1, 2, 3, 4, 5, 6, 7, 8 T_IDSTR_SELF, T_IDSTR_REF, T_IDHASH_SELF, T_IDHASH_REF = 9, 10, 11, 12 SECTIONS = ("tree", "idstr", "idhash", "strings", "wstrings", "hashes", "ints", "floats", "files") def paf_hash(name): """SHA-1 prefix of a name; "#1234abcd" is taken as the hash itself, for resources of the system whose names are unknown.""" if len(name) == 9 and name[0] == "#": try: return int(name[1:], 16) except ValueError: pass return int.from_bytes(hashlib.sha1(name.encode("utf-8")).digest()[:4], "big") def to_float(v): return float(v.strip().rstrip("fF")) def align(n, a=16): return (n + a - 1) & ~(a - 1) # ---------------------------------------------------------------- reader class Rco: def __init__(self, data): self.data = data self.magic = data[:4] self.version = struct.unpack_from(" 16: blob = r.s["files"][a:a + b] if blob[:4] == b"RCSF": walk(Rco(blob)) for p in paths: walk(Rco(open(p, "rb").read())) with open(out, "w", encoding="utf-8") as f: json.dump(schema, f, indent=0, sort_keys=True) print("schema: %d elements -> %s" % (len(schema), out)) # ---------------------------------------------------------------- compiler class Compiler: def __init__(self, schema, base_dir, magic=b"RCOF"): self.schema = schema self.base = base_dir self.magic = magic self.tree = bytearray() self.idstr = bytearray() self.idhash = bytearray() self.idhash_map = {} self.strings = bytearray() self.string_map = {} self.wstrings = bytearray() self.wstring_map = {} self.hashes = [] self.hash_map = {} self.ints = [] self.floats = [] self.files = bytearray() self.file_map = {} self.names = {} self.warnings = [] # pools ------------------------------------------------------------ def str_off(self, s): if s not in self.string_map: self.string_map[s] = len(self.strings) self.strings += s.encode("utf-8") + b"\0" return self.string_map[s] def wstr_off(self, s): if s not in self.wstring_map: self.wstring_map[s] = len(self.wstrings) self.wstrings += s.encode("utf-16-le") + b"\0\0" return self.wstring_map[s] def hash_idx(self, s): h = paf_hash(s) if h not in self.hash_map: self.hash_map[h] = len(self.hashes) self.hashes.append(h) return self.hash_map[h] def idh_off(self, s, owner=None): h = paf_hash(s) if h not in self.idhash_map: self.idhash_map[h] = len(self.idhash) self.idhash += struct.pack("/name.xml.rcs" or "name.rcs". b = os.path.basename(path) stem = b[:-4] if b.endswith(".rcs") else b found = None for c in (stem, stem if stem.endswith(".xml") else stem + ".xml"): for d in ("locale", "."): t = os.path.join(self.base, d, c) if os.path.exists(t): found = t break if found: break if not found: raise SystemExit("rcoc: file not found: %s" % full) full = found key = os.path.normpath(full) if key in self.file_map: return self.file_map[key] if element == "locale" and full.endswith(".xml"): blob = compile_file(full, self.schema, b"RCSF", names=self.names) else: blob = open(full, "rb").read() while len(self.files) % 16: self.files.append(0) res = (len(self.files), len(blob)) self.files += blob self.file_map[key] = res return res # types ------------------------------------------------------------ def attr_type(self, el, name, value): ts = self.schema.get(el, {}).get(name) if ts is None: # Same attribute on another element of the same family. seen = set() for attrs in self.schema.values(): if name in attrs: seen.update(attrs[name]) if not seen: raise SystemExit("rcoc: unknown attribute <%s %s=...>; add it to the schema" % (el, name)) self.warnings.append("<%s %s> not in schema, inferred from other elements" % (el, name)) ts = sorted(seen) multi = "," in value if T_FLOATS in ts or T_FLOAT in ts or T_INTS in ts or T_INT in ts: is_float = T_FLOATS in ts or T_FLOAT in ts if is_float and multi and T_FLOATS in ts: return T_FLOATS if not is_float and multi and T_INTS in ts: return T_INTS if is_float and T_FLOAT in ts and not multi: return T_FLOAT if not is_float and T_INT in ts and not multi: return T_INT return T_FLOATS if is_float else T_INTS return ts[0] def encode(self, el, name, value, tag_off): ty = self.attr_type(el, name, value) if ty == T_INT: return ty, int(value, 0) & NONE, 0 if ty == T_FLOAT: return ty, struct.unpack("" % (ty, el, name)) # tree ------------------------------------------------------------- def emit(self, el, parent, prev): off = len(self.tree) attrs = list(el.attrib.items()) name_off = self.str_off(el.tag) self.tree += b"\0" * (28 + 16 * len(attrs)) recs = [] for i, (an, val) in enumerate(attrs): an_off = self.str_off(an) ty, a, b = self.encode(el.tag, an, val, off) struct.pack_into("<4I", self.tree, off + 28 + i * 16, an_off, ty, a, b) first = last = NONE p = NONE for child in list(el): c = self.emit(child, off, p) if p != NONE: struct.pack_into("