From c5a7a296d005ec9a7203bc3b07011dabb67a6535 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Sat, 21 Nov 2026 22:57:21 +0100 Subject: [PATCH] Preserve 64-bit UIDs as BigInt --- bplistParser.ts | 10 +++++----- test/parse.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/bplistParser.ts b/bplistParser.ts index 38c810e..63f0007 100644 --- a/bplistParser.ts +++ b/bplistParser.ts @@ -34,9 +34,9 @@ const EPOCH = 978307200000; /** Wrapper for a CoreFoundation keyed-archiver UID value. */ export class UID { - UID: number; + UID: number | bigint; - constructor(id: number) { + constructor(id: number | bigint) { this.UID = id; } } @@ -214,10 +214,10 @@ export function parseBuffer(buffer: Buffer): [T] { function parseUID(): UID { const length = objInfo + 1; - if (length < maxObjectSize) { - return new UID(readUInt(buffer.slice(offset + 1, offset + 1 + length))); + if (length > 8) { + throw new Error("Invalid binary plist. UID length must be between 1 and 8 bytes."); } - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); + return new UID(simplifyInteger(readBigUInt(buffer.slice(offset + 1, offset + 1 + length)))); } function parseReal(): number | undefined { diff --git a/test/parse.test.ts b/test/parse.test.ts index b598c66..72695e8 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -171,8 +171,49 @@ describe('bplist-parser', function () { bplist.parseBuffer(Buffer.from('bplist00')); }, /Invalid binary plist/); }); + + it('parses UIDs larger than 2^53 without losing precision', function () { + const cases = [ + { value: 0n, bytes: 1 }, + { value: 255n, bytes: 1 }, + { value: 256n, bytes: 2 }, + { value: 4294967296n, bytes: 8 }, + { value: 2n ** 63n, bytes: 8 } + ]; + + for (const {value, bytes} of cases) { + const object = Buffer.concat([Buffer.from([0x80 | (bytes - 1)]), writeUIntBE(value, bytes)]); + const [parsed] = bplist.parseBuffer(makeSingleObjectPlist(object)); + const expected = value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : value; + assert.strictEqual(parsed.UID, expected); + } + }); + + it('rejects UIDs wider than 8 bytes', function () { + const object = Buffer.concat([Buffer.from([0x88]), writeUIntBE(1n, 9)]); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(object)), /UID length/); + }); }); +function makeSingleObjectPlist(object: Buffer): Buffer { + const offsetTableOffset = 8 + object.length; + const offsetSize = byteWidth(offsetTableOffset); + const trailer = Buffer.concat([ + Buffer.alloc(6), + Buffer.from([offsetSize, 1]), + writeUIntBE(1, 8), + writeUIntBE(0, 8), + writeUIntBE(offsetTableOffset, 8) + ]); + + return Buffer.concat([ + Buffer.from('bplist00'), + object, + writeUIntBE(8, offsetSize), + trailer + ]); +} + type IntegerCase = { value: bigint; bytes: number; expected: number | bigint }; function makeIntegerArray(cases: IntegerCase[]): Buffer {