From bcb22f4b57b52b379ddb35199babac7d77cdcb88 Mon Sep 17 00:00:00 2001 From: cc Date: Thu, 3 Dec 2026 16:31:44 +0100 Subject: [PATCH] feat(data): read sharded records and rebuild plist XML from pairs entdb-data now packs key and path records into 256 shards per kind ({hh}.json, keyed by name; a record over 32 KB stays in {hash}.json and is 1 in its shard), and stores each binary's plists as deduplicated top-level pairs plus style flags. plist-xml.ts ports the exporter's regen(), which rebuilds the original XML byte for byte; the exporter only uses the pairs when that round trip matches and otherwise keeps the XML string. Checked against a full export: all 43,193 plists rebuild identically and every key and path record is found through its shard. Deploy together with the matching entdb-data change. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/lib/engine/plist-xml.ts | 106 ++++++++++++++++++++++++++++++++++++ src/lib/engine/records.ts | 50 +++++++++++++---- 3 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 src/lib/engine/plist-xml.ts diff --git a/README.md b/README.md index 54ce57d..992d72c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Web frontend for the Entitlement Database — browse iOS and macOS entitlements Built as a static Next.js site deployed to GitHub Pages. -It uses pre-built static KV format (on top of JSON) with HTTP Range requests. +It reads precomputed JSON records from [entdb-data](https://github.com/ChiChou/entdb-data): one file per build, plus key and binary records packed into 256 shards per kind, so every query is one or two whole-file fetches. Binaries' entitlements are stored as deduplicated key/value pairs that rebuild the original XML byte for byte (`src/lib/engine/plist-xml.ts`). ## Related Repositories diff --git a/src/lib/engine/plist-xml.ts b/src/lib/engine/plist-xml.ts new file mode 100644 index 0000000..5243d57 --- /dev/null +++ b/src/lib/engine/plist-xml.ts @@ -0,0 +1,106 @@ +/** + * Rebuilds a binary's original entitlement XML, byte for byte, from a path + * record's pairs. Port of regen() in entdb-data's scripts/stages/plist_xml.py, + * which writes the encoding below and only uses it for a plist when this + * rebuild matches the original; keep the two in step. + * + * Values are JSON where that is unambiguous (string, boolean, integer, array, + * dict); any other plist element is a one-key object named after its tag, + * e.g. {"real": "1.5"} or {"data": "AAEC"}, and a dict whose only key is a + * tag name is wrapped as {"dict": {...}}. + */ + +export type EncodedValue = + | string + | number + | boolean + | EncodedValue[] + | { [key: string]: EncodedValue }; + +export type Pair = [string, EncodedValue]; + +/** Empty elements as instead of . */ +const EMPTY_OPEN_CLOSE = 1; +/** " and ' escaped as " and '. */ +const ESCAPE_QUOTES = 2; +const TRAILING_NEWLINE = 4; + +const HEAD = + '' + + ''; + +const TAGS = new Set([ + "array", + "data", + "date", + "dict", + "false", + "integer", + "key", + "real", + "string", + "true", +]); + +function escapeXML(text: string, flags: number) { + let out = text + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">"); + if (flags & ESCAPE_QUOTES) { + out = out.replaceAll('"', """).replaceAll("'", "'"); + } + return out; +} + +function empty(tag: string, flags: number) { + return flags & EMPTY_OPEN_CLOSE ? `<${tag}>` : `<${tag}/>`; +} + +function entries(dict: { [key: string]: EncodedValue }, flags: number) { + let out = ""; + for (const key of Object.keys(dict)) { + out += `${escapeXML(key, flags)}${toXML(dict[key], flags)}`; + } + return out; +} + +function toXML(value: EncodedValue, flags: number): string { + if (typeof value === "boolean") return value ? "" : ""; + if (typeof value === "number") return `${value}`; + if (typeof value === "string") { + return value + ? `${escapeXML(value, flags)}` + : ""; + } + if (Array.isArray(value)) { + if (!value.length) return empty("array", flags); + return `${value.map((v) => toXML(v, flags)).join("")}`; + } + let dict = value; + const keys = Object.keys(dict); + if (keys.length === 1 && TAGS.has(keys[0])) { + const [tag] = keys; + const inner = dict[tag]; + if (tag === "dict" && typeof inner === "object" && !Array.isArray(inner)) { + dict = inner; + } else if (typeof inner === "string") { + return inner + ? `<${tag}>${escapeXML(inner, flags)}` + : empty(tag, flags); + } + } + if (!Object.keys(dict).length) return empty("dict", flags); + return `${entries(dict, flags)}`; +} + +/** The plist XML for top-level pairs, in order, written in style `flags`. */ +export function regen(pairs: Pair[], flags: number): string { + let body = ""; + for (const [key, value] of pairs) { + body += `${escapeXML(key, flags)}${toXML(value, flags)}`; + } + const root = pairs.length ? `${body}` : empty("dict", flags); + return `${HEAD}${root}${flags & TRAILING_NEWLINE ? "\n" : ""}`; +} diff --git a/src/lib/engine/records.ts b/src/lib/engine/records.ts index a4db628..fabdac2 100644 --- a/src/lib/engine/records.ts +++ b/src/lib/engine/records.ts @@ -1,17 +1,20 @@ import { dataURL } from "@/lib/env"; import type { OS } from "@/lib/types"; +import { type Pair, regen } from "./plist-xml"; import type { Engine, PathHistory } from "./types"; /** * Reads the precomputed JSON records produced by entdb-data's - * scripts/stages/export_records.py. Every query is one whole-file GET: + * scripts/stages/export_records.py. Every query is one or two whole-file GETs: * * {group}/list.json builds sorted by version (index = ordinal) * {group}/b/{version}_{build}.json {paths, keys: [[key, count]]} - * {group}/k/{hash(key)}.json {key, values, rows: [[path, value, ranges]]} - * {group}/p/{hash(path)}.json {path, plists, runs: [[from, to, plist]]} + * {group}/k/{hh}.json {[key]: KeyRecord | 1} + * {group}/p/{hh}.json {[path]: PathRecord | 1} + * {group}/k|p/{hash(name)}.json a record marked 1 in its shard * - * Ranges are inclusive build ordinals, so one record covers every build. + * hh is the first two hex digits of hash(name). Ranges are inclusive build + * ordinals, so one record covers every build. */ interface BuildRecord { @@ -20,20 +23,26 @@ interface BuildRecord { } interface KeyRecord { - key: string; values: unknown[]; /** [path, index into values, flat inclusive ranges [from, to, ...]] */ rows: [string, number, number[]][]; } interface PathRecord { - path: string; - /** Original plist XML, deduplicated. */ - plists: string[]; + /** Top-level entitlement pairs, shared by the binary's plists. */ + pairs: Pair[]; + /** + * Each distinct plist: [style flags, ...indexes into pairs], rebuilt into + * the original XML by regen(); or the XML itself when it can't be. + */ + plists: ([number, ...number[]] | string)[]; /** [from, to, index into plists], inclusive ordinals. */ runs: [number, number, number][]; } +/** A shard maps names to records; 1 means the record is in its own file. */ +type Shard = Record; + /** * 64-bit FNV-1a of the UTF-8 bytes, as 16 hex digits; must match fnv1a64() * in export_records.py. Computed with 16-bit limbs (no BigInt needed): @@ -127,12 +136,25 @@ export class RecordEngine implements Engine { return record; } + /** A record by name from its shard; null when there is none. */ + private async shardRecord(kind: "k" | "p", name: string) { + const hash = fnv1a64(name); + const dir = `${this.#base}/${kind}`; + const shard = await fetchRecord>( + `${dir}/${hash.slice(0, 2)}.json`, + ); + // Own-property check: a name like "constructor" must not hit the prototype. + if (!shard || !Object.hasOwn(shard, name)) return null; + const record = shard[name]; + return record === 1 ? fetchRecord(`${dir}/${hash}.json`) : record; + } + private keyRecord(key: string) { - return fetchRecord(`${this.#base}/k/${fnv1a64(key)}.json`); + return this.shardRecord("k", key); } private pathRecord(path: string) { - return fetchRecord(`${this.#base}/p/${fnv1a64(path)}.json`); + return this.shardRecord("p", path); } async getPaths(build: string): Promise { @@ -180,7 +202,13 @@ export class RecordEngine implements Engine { ([from, to]) => ordinal >= from && ordinal <= to, ); if (!record || !run) throw new Error(`${path} not found in ${build}`); - return record.plists[run[2]]; + const plist = record.plists[run[2]]; + if (typeof plist === "string") return plist; + const [flags, ...indexes] = plist; + return regen( + indexes.map((i) => record.pairs[i]), + flags, + ); } /** Every build of the group, newest first, with whether `path` exists. */