Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions bplistParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,16 @@ export function parseBuffer<T = any>(buffer: Buffer): [T] {
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
}

function parseReal(): number | undefined {
function parseReal(): number {
const length = Math.pow(2, objInfo);
if (length < maxObjectSize) {
const realBuffer = buffer.slice(offset + 1, offset + 1 + length);
if (length === 4) {
return realBuffer.readFloatBE(0);
}
if (length === 8) {
return realBuffer.readDoubleBE(0);
}
} else {
throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
const realBuffer = buffer.slice(offset + 1, offset + 1 + length);
if (length === 4) {
return realBuffer.readFloatBE(0);
}
if (length === 8) {
return realBuffer.readDoubleBE(0);
}
throw new Error("Invalid binary plist. Unsupported real length: " + length);
}

function parseDate(): Date {
Expand Down
33 changes: 33 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,41 @@ describe('bplist-parser', function () {
bplist.parseBuffer(Buffer.from('bplist00'));
}, /Invalid binary plist/);
});

it('preserves negative zero reals', function () {
const payload = Buffer.alloc(8);
payload.writeDoubleBE(-0, 0);
const [parsed] = bplist.parseBuffer(makeSingleObjectPlist(Buffer.concat([Buffer.from([0x23]), payload])));
assert.ok(Object.is(parsed, -0));
});

it('rejects unsupported real widths', function () {
for (const info of [0, 1, 4, 5]) {
const object = Buffer.concat([Buffer.from([0x20 | info]), Buffer.alloc(2 ** info)]);
assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(object)), /Unsupported real 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 {
Expand Down