fix(plist): own XML values and numeric facts in one decoder

Interpret the validated, depth-bounded DOM directly. Collect source number
facts during the same traversal and share them with keyed archives, Interface
Builder and native plist projections. Remove the second XML parse, key aliases,
source-location rewriting and both duplicate XML numeric scanners. Preserve
character references, container/null behavior, safe own-property construction
and prototype omission accounting.

Decode signed hexadecimal integers and combine real text/CDATA. Retain exact
integer identity when a finite integer exceeds even Number's finite range,
using the shared scalar projector. Keep unsupported mixed integer syntax
conservative and report incomplete metadata.

Adapt @apitoagents's hexadecimal regression and literal-parsing proposal from
PR #3475. Native plutil controls establish the supported hex/real cases and
confirm that the synthetic 401-digit overflow case exceeds native bounds;
retaining accepted input does not claim native-format support.

Validation: 274 focused checks in 30 files pass, one skipped; build,
check:fast and docs:check pass. Built CLI/actual stdio MCP, native plist and
Interface Builder controls cover kinds, references, exact digits and digests.
Eight baseline checks fail; native binary conversion independently verifies
updated CDATA real expectations. 48 tiny completed CLI cost comparisons
preserve ordinary XML/binary and all three native replies byte for byte.
Two read-only reviews clear. No statistical speed claim, heavy profiler,
investigated-target execution or real deep-provider verification.

Fixes #3373
Fixes #4042
Fixes #4789
This commit is contained in:
morluto committed 2026-10-17 20:40:55 +08:00
1 parent 78cf6100f3
commit 7aa4b98c98
17 files changed
+542 -303

No files matched your search

@@ -39,7 +39,6 @@ import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
import {
decodeBinaryPlistNumbers,
scanBinaryPlistNumberLiterals,
xmlPlistNumberLiterals,
} from "./PlistNumberLiterals.js";
import type { PlistNumberLiterals } from "../../domain/apple/plistNumbers.js";
import {
@@ -753,12 +752,13 @@ const decodePlist = (
const binaryNumberMetadata = binary
? scanBinaryPlistNumberLiterals(bytes)
: undefined;
const { value, omittedPrototypeKeys } = binary
const { value, omittedPrototypeKeys, numberLiterals } = binary
? {
value: parseBinary(
decodeBinaryPlistNumbers(bytes, binaryNumberMetadata!),
),
omittedPrototypeKeys: binaryPrototypeKeyObserved ? null : 0,
numberLiterals: binaryNumberMetadata!.literals,
}
: parseXmlWithinDecodeBudget(xmlText ?? decodeXmlPlistText(bytes));
const projected = projectPlistValue(value);
@@ -766,8 +766,6 @@ const decodePlist = (
value: projected.value,
omittedPrototypeKeys,
unknownDateCount: projected.unknownDateCount,
numberLiterals: binary
? binaryNumberMetadata!.literals
: xmlPlistNumberLiterals(xmlText ?? decodeXmlPlistText(bytes)),
numberLiterals,
};
};
+2 -6
View File
@@ -11,7 +11,6 @@ import { createPlistNumberProjection } from "../../domain/apple/plistNumbers.js"
import {
decodeBinaryPlistNumbers,
scanBinaryPlistNumberLiterals,
xmlPlistNumberLiterals,
} from "./PlistNumberLiterals.js";
import {
omittedPrototypeKeysLimitation,
@@ -77,6 +76,7 @@ export const decodeKeyedArchiveBytes = (
value: parseBinary(
decodeBinaryPlistNumbers(bytes, binaryNumberMetadata!),
),
numberLiterals: binaryNumberMetadata!.literals,
omittedPrototypeKeys: binaryExpansion.binaryPrototypeKeyObserved
? null
: 0,
@@ -86,11 +86,7 @@ export const decodeKeyedArchiveBytes = (
parsed.value,
selection,
);
const numbers = createPlistNumberProjection(
binary
? binaryNumberMetadata!.literals
: xmlPlistNumberLiterals(xmlText ?? ""),
);
const numbers = createPlistNumberProjection(parsed.numberLiterals);
// Classify the serialized graph before projecting values to typed decimals.
// Numeric IDs, node kinds, references and pagination retain their original meaning.
const roots = Object.fromEntries(
+7 -124
View File
@@ -1,125 +1,9 @@
import type { PlistNumberLiterals } from "../../domain/apple/plistNumbers.js";
import { decodeXmlText } from "../../domain/propertyListKeys.js";
import {
createPlistNumberCollector,
type PlistNumberLiterals,
} from "../../domain/apple/plistNumbers.js";
import { hasBinaryPlistSignature } from "../../domain/apple/appleArchiveFormat.js";
const collector = () => {
const integers = new Map<number, Set<string>>();
const reals = new Set<number>();
let unsafeIntegerLiterals = 0;
let incompleteMetadata = false;
return {
integer: (literal: string) => {
if (!/^[+-]?\d+$/u.test(literal)) {
incompleteMetadata = true;
return;
}
const exact = BigInt(literal);
if (
exact >= BigInt(Number.MIN_SAFE_INTEGER) &&
exact <= BigInt(Number.MAX_SAFE_INTEGER)
)
return;
unsafeIntegerLiterals += 1;
const rounded = Number(exact);
integers.set(
rounded,
(integers.get(rounded) ?? new Set()).add(exact.toString()),
);
},
real: (value: number) => {
reals.add(value);
},
markIncomplete: () => {
incompleteMetadata = true;
},
result: (incomplete = false): PlistNumberLiterals => ({
incomplete: incomplete || incompleteMetadata,
integers,
reals,
unsafeIntegerLiterals,
}),
};
};
// Consume quoted tag attributes and opaque comment/CDATA tokens whole.
const XML_TOKEN =
/<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<(?:[^>"']|"[^"]*"|'[^']*')*>/gu;
const OPAQUE_ELEMENTS = new Set(["string", "key", "data", "date"]);
/** Observe plist number element types without interpreting strings or class code. */
export const xmlPlistNumberLiterals = (xml: string): PlistNumberLiterals => {
const output = collector();
const elements: string[] = [];
let number:
| {
kind: "integer" | "real";
text: string;
depth: number;
plainText: boolean;
}
| undefined;
let cursor = 0;
for (const match of xml.matchAll(XML_TOKEN)) {
const index = match.index;
if (number !== undefined) number.text += xml.slice(cursor, index);
const token = match[0];
cursor = index + token.length;
if (token.startsWith("<!--") || token.startsWith("<![CDATA[")) {
if (number !== undefined) {
number.plainText = false;
output.markIncomplete();
}
continue;
}
const tag = /^<\s*(\/?)\s*([\w:-]+)/u.exec(token);
if (tag === null) {
if (number !== undefined) {
number.plainText = false;
output.markIncomplete();
}
continue;
}
const closing = tag[1] === "/";
const name = tag[2];
if (name === undefined) continue;
if (closing) {
if (
number !== undefined &&
number.kind === name &&
number.depth === elements.length
) {
if (number.plainText) {
const text = decodeXmlText(number.text).trim();
if (number.kind === "integer") output.integer(text);
else output.real(Number.parseFloat(text));
}
number = undefined;
}
elements.pop();
continue;
}
// The pinned decoder reads only the first child for integers and only text
// nodes for reals. Mixed content cannot establish an exact association.
if (number !== undefined) {
number.plainText = false;
output.markIncomplete();
}
if (/\/\s*>$/u.test(token)) continue;
if (
number === undefined &&
(name === "integer" || name === "real") &&
!elements.some((parent) => OPAQUE_ELEMENTS.has(parent))
)
number = {
kind: name,
text: "",
depth: elements.length + 1,
plainText: true,
};
elements.push(name);
}
return output.result();
};
export interface BinaryPlistNumberMetadata {
readonly literals: PlistNumberLiterals;
/** 16-byte integers the pinned plist decoder cannot decode correctly. */
@@ -179,7 +63,7 @@ const binaryObjectEnd = (
export const scanBinaryPlistNumberLiterals = (
bytes: Buffer,
): BinaryPlistNumberMetadata => {
const output = collector();
const output = createPlistNumberCollector();
const wideIntegers: { offset: number; value: bigint }[] = [];
if (bytes.length < 40 || !hasBinaryPlistSignature(bytes))
return { literals: output.result(true), wideIntegers };
@@ -236,14 +120,13 @@ export const scanBinaryPlistNumberLiterals = (
const width = 2 ** (marker & 0xf);
if (offset + 1 + width > offsetTable)
return { literals: output.result(true), wideIntegers };
if (type === 1 && width === 8)
output.integer(view.getBigInt64(offset + 1).toString());
if (type === 1 && width === 8) output.integer(view.getBigInt64(offset + 1));
else if (type === 1 && width === 16) {
const value = BigInt.asIntN(
128,
(view.getBigUint64(offset + 1) << 64n) | view.getBigUint64(offset + 9),
);
output.integer(value.toString());
output.integer(value);
wideIntegers.push({ offset, value });
} else if (type === 1 && width > 4)
return { literals: output.result(true), wideIntegers };