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 <noreply@anthropic.com>
This commit is contained in:
ccandClaude Opus 5.5 committed 2026-12-03 16:31:44 +01:00
1 parent 06f4831856
commit bcb22f4b57
3 files changed
+146 -12

No files matched your search

+1 -1
View File
@@ -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
+106
View File
@@ -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 <x></x> instead of <x/>. */
const EMPTY_OPEN_CLOSE = 1;
/** " and ' escaped as &quot; and &apos;. */
const ESCAPE_QUOTES = 2;
const TRAILING_NEWLINE = 4;
const HEAD =
'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC ' +
'"-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">' +
'<plist version="1.0">';
const TAGS = new Set([
"array",
"data",
"date",
"dict",
"false",
"integer",
"key",
"real",
"string",
"true",
]);
function escapeXML(text: string, flags: number) {
let out = text
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
if (flags & ESCAPE_QUOTES) {
out = out.replaceAll('"', "&quot;").replaceAll("'", "&apos;");
}
return out;
}
function empty(tag: string, flags: number) {
return flags & EMPTY_OPEN_CLOSE ? `<${tag}></${tag}>` : `<${tag}/>`;
}
function entries(dict: { [key: string]: EncodedValue }, flags: number) {
let out = "";
for (const key of Object.keys(dict)) {
out += `<key>${escapeXML(key, flags)}</key>${toXML(dict[key], flags)}`;
}
return out;
}
function toXML(value: EncodedValue, flags: number): string {
if (typeof value === "boolean") return value ? "<true/>" : "<false/>";
if (typeof value === "number") return `<integer>${value}</integer>`;
if (typeof value === "string") {
return value
? `<string>${escapeXML(value, flags)}</string>`
: "<string></string>";
}
if (Array.isArray(value)) {
if (!value.length) return empty("array", flags);
return `<array>${value.map((v) => toXML(v, flags)).join("")}</array>`;
}
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)}</${tag}>`
: empty(tag, flags);
}
}
if (!Object.keys(dict).length) return empty("dict", flags);
return `<dict>${entries(dict, flags)}</dict>`;
}
/** 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 += `<key>${escapeXML(key, flags)}</key>${toXML(value, flags)}`;
}
const root = pairs.length ? `<dict>${body}</dict>` : empty("dict", flags);
return `${HEAD}${root}</plist>${flags & TRAILING_NEWLINE ? "\n" : ""}`;
}
+39 -11
View File
@@ -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<T> = Record<string, T | 1>;
/**
* 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<T>(kind: "k" | "p", name: string) {
const hash = fnv1a64(name);
const dir = `${this.#base}/${kind}`;
const shard = await fetchRecord<Shard<T>>(
`${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<T>(`${dir}/${hash}.json`) : record;
}
private keyRecord(key: string) {
return fetchRecord<KeyRecord>(`${this.#base}/k/${fnv1a64(key)}.json`);
return this.shardRecord<KeyRecord>("k", key);
}
private pathRecord(path: string) {
return fetchRecord<PathRecord>(`${this.#base}/p/${fnv1a64(path)}.json`);
return this.shardRecord<PathRecord>("p", path);
}
async getPaths(build: string): Promise<string[]> {
@@ -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. */