bplist-lossless 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,312 @@
1
+ import { PlistDate } from "../classes/plist-date.js";
2
+ import { UID } from "../classes/uid.js";
3
+ import { Utf16String } from "../classes/utf16-string.js";
4
+
5
+ export var maxObjectSize = 100 * 1000 * 1000; // 100Meg
6
+ export var maxObjectCount = 32768;
7
+
8
+ export function parseBplist(buffer: Buffer) {
9
+ // check header
10
+ const header = buffer.slice(0, 'bplist'.length).toString('utf8');
11
+ if (header !== 'bplist') {
12
+ throw new Error("Invalid binary plist. Expected 'bplist' at offset 0.");
13
+ }
14
+
15
+ // Handle trailer, last 32 bytes of the file
16
+ const trailer = buffer.slice(buffer.length - 32, buffer.length);
17
+ // 6 null bytes (index 0 to 5)
18
+ const offsetSize = trailer.readUInt8(6);
19
+ const objectRefSize = trailer.readUInt8(7);
20
+ const numObjects = toSafeNumber(trailer.readBigUInt64BE(8));
21
+ const topObject = toSafeNumber(trailer.readBigUInt64BE(16));
22
+ const offsetTableOffset = toSafeNumber(trailer.readBigUInt64BE(24));
23
+
24
+ if (numObjects > maxObjectCount) {
25
+ throw new Error("maxObjectCount exceeded");
26
+ }
27
+
28
+ // Handle offset table
29
+ const offsetTable: number[] = [];
30
+
31
+ for (let i = 0; i < numObjects; i++) {
32
+ const offsetBytes = buffer.slice(
33
+ offsetTableOffset + i * offsetSize,
34
+ offsetTableOffset + (i + 1) * offsetSize
35
+ );
36
+ offsetTable[i] = toSafeNumber(readUInt(offsetBytes, 0));
37
+ }
38
+
39
+ // Parses an object inside the currently parsed binary property list.
40
+ // For the format specification check
41
+ // <a href="https://www.opensource.apple.com/source/CF/CF-635/CFBinaryPList.c">
42
+ // Apple's binary property list parser implementation</a>.
43
+ function parseObject(tableOffset: number): any {
44
+ const offset = offsetTable[tableOffset]!;
45
+ const type = buffer[offset]!;
46
+ const objType = (type & 0xF0) >> 4; //First 4 bits
47
+ const objInfo = (type & 0x0F); //Second 4 bits
48
+ switch (objType) {
49
+ case 0x0:
50
+ return parseSimple();
51
+ case 0x1:
52
+ return parseInteger();
53
+ case 0x8:
54
+ return parseUID();
55
+ case 0x2:
56
+ return parseReal();
57
+ case 0x3:
58
+ return parseDate();
59
+ case 0x4:
60
+ return parseData();
61
+ case 0x5: // ASCII
62
+ return parsePlistString(false);
63
+ case 0x6: // UTF-16
64
+ return parsePlistString(true);
65
+ case 0xA:
66
+ return parseArray();
67
+ case 0xD:
68
+ return parseDictionary();
69
+ default:
70
+ throw new Error("Unhandled type 0x" + objType.toString(16));
71
+ }
72
+
73
+ function parseSimple() {
74
+ //Simple
75
+ switch (objInfo) {
76
+ case 0x0: // null
77
+ return null;
78
+ case 0x8: // false
79
+ return false;
80
+ case 0x9: // true
81
+ return true;
82
+ case 0xF: // filler byte
83
+ return null;
84
+ default:
85
+ throw new Error("Unhandled simple type 0x" + objType.toString(16));
86
+ }
87
+ }
88
+
89
+ function bufferToHexString(buffer: Buffer) {
90
+ let str = '';
91
+ let i;
92
+ for (i = 0; i < buffer.length; i++) {
93
+ if (buffer[i] != 0x00) {
94
+ break;
95
+ }
96
+ }
97
+ for (; i < buffer.length; i++) {
98
+ const part = '00' + buffer[i]!.toString(16);
99
+ str += part.substr(part.length - 2);
100
+ }
101
+ return str;
102
+ }
103
+
104
+ // Always return a BigInt for integers
105
+ function parseInteger() {
106
+ const length = 1 << objInfo;
107
+
108
+ if (length > maxObjectSize) {
109
+ throw new Error(
110
+ `Too little heap space available! Wanted to read ${length} bytes, but only ${maxObjectSize} are available.`
111
+ );
112
+ }
113
+
114
+ const start = Number(offset) + 1;
115
+ const end = start + length;
116
+ const data = buffer.subarray(start, end); // no copy
117
+
118
+ let value = 0n;
119
+
120
+ for (let i = 0; i < data.length; i++) {
121
+ value = (value << 8n) | BigInt(data[i]!);
122
+ }
123
+
124
+ // 🔥 handle signed (two's complement)
125
+ const bits = BigInt(length * 8);
126
+ const signBit = 1n << (bits - 1n);
127
+
128
+ if (value & signBit) {
129
+ value -= 1n << bits;
130
+ }
131
+
132
+ return value;
133
+ }
134
+
135
+ function parseUID() {
136
+ const length = objInfo + 1;
137
+ if (length < maxObjectSize) {
138
+ return new UID(
139
+ buffer.buffer as ArrayBuffer,
140
+ buffer.byteOffset + Number(offset) + 1,
141
+ length
142
+ );
143
+ }
144
+ throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
145
+ }
146
+
147
+ function parseReal() {
148
+ const length = Math.pow(2, objInfo);
149
+ if (length < maxObjectSize) {
150
+ const realBuffer = buffer.slice(offset + 1, offset + 1 + length);
151
+ if (length === 4) {
152
+ return realBuffer.readFloatBE(0);
153
+ }
154
+ if (length === 8) {
155
+ return realBuffer.readDoubleBE(0);
156
+ }
157
+ } else {
158
+ throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
159
+ }
160
+ }
161
+
162
+ function parseDate() {
163
+ if (objInfo !== 0x3) {
164
+ console.error("Unknown date type: " + objInfo + ". Parsing anyway...");
165
+ }
166
+
167
+ const raw = buffer.subarray(offset + 1, offset + 9);
168
+ return PlistDate.fromBuffer(raw);
169
+ }
170
+
171
+ function parseData() {
172
+ let dataoffset = 1;
173
+ let length = objInfo;
174
+ if (objInfo == 0xF) {
175
+ const int_type = buffer[offset + 1]!;
176
+ const intType = (int_type & 0xF0) / 0x10;
177
+ if (intType != 0x1) {
178
+ console.error("0x4: UNEXPECTED LENGTH-INT TYPE! " + intType);
179
+ }
180
+ const intInfo = int_type & 0x0F;
181
+ const intLength = Math.pow(2, intInfo);
182
+ dataoffset = 2 + intLength;
183
+ if (intLength < 3) {
184
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
185
+ } else {
186
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
187
+ }
188
+ }
189
+ if (length < maxObjectSize) {
190
+ return buffer.slice(offset + dataoffset, offset + dataoffset + length);
191
+ }
192
+ throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
193
+ }
194
+
195
+ function parsePlistString (isUtf16: boolean) {
196
+ let enc = "utf8";
197
+ let length = objInfo;
198
+ let stroffset = 1;
199
+ if (objInfo == 0xF) {
200
+ const int_type = buffer[offset + 1]!;
201
+ const intType = (int_type & 0xF0) / 0x10;
202
+ if (intType != 0x1) {
203
+ console.error("UNEXPECTED LENGTH-INT TYPE! " + intType);
204
+ }
205
+ const intInfo = int_type & 0x0F;
206
+ const intLength = Math.pow(2, intInfo);
207
+ stroffset = 2 + intLength;
208
+ if (intLength < 3) {
209
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
210
+ } else {
211
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
212
+ }
213
+ }
214
+ // length is String length -> to get byte length multiply by 2, as 1 character takes 2 bytes in UTF-16
215
+ length *= (Number(isUtf16) + 1);
216
+ if (length < maxObjectSize) {
217
+ let plistString = Buffer.from(buffer.slice(offset + stroffset, offset + stroffset + length));
218
+ if (isUtf16) {
219
+ return Utf16String.from(plistString);
220
+ } else {
221
+ return plistString.toString('utf8');
222
+ }
223
+ }
224
+ throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available.");
225
+ }
226
+
227
+ function parseArray() {
228
+ let length = objInfo;
229
+ let arrayoffset = 1;
230
+ if (objInfo == 0xF) {
231
+ const int_type = buffer[offset + 1]!;
232
+ const intType = (int_type & 0xF0) / 0x10;
233
+ if (intType != 0x1) {
234
+ console.error("0xa: UNEXPECTED LENGTH-INT TYPE! " + intType);
235
+ }
236
+ const intInfo = int_type & 0x0F;
237
+ const intLength = Math.pow(2, intInfo);
238
+ arrayoffset = 2 + intLength;
239
+ if (intLength < 3) {
240
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
241
+ } else {
242
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
243
+ }
244
+ }
245
+ if (length * objectRefSize > maxObjectSize) {
246
+ throw new Error("Too little heap space available!");
247
+ }
248
+ const array = [];
249
+ for (let i = 0; i < length; i++) {
250
+ const objRef = toSafeNumber(readUInt(buffer.slice(offset + arrayoffset + i * objectRefSize, offset + arrayoffset + (i + 1) * objectRefSize)));
251
+ array[i] = parseObject(objRef);
252
+ }
253
+ return array;
254
+ }
255
+
256
+ function parseDictionary() {
257
+ let length = objInfo;
258
+ let dictoffset = 1;
259
+ if (objInfo == 0xF) {
260
+ const int_type = buffer[offset + 1]!;
261
+ const intType = (int_type & 0xF0) / 0x10;
262
+ if (intType != 0x1) {
263
+ console.error("0xD: UNEXPECTED LENGTH-INT TYPE! " + intType);
264
+ }
265
+ const intInfo = int_type & 0x0F;
266
+ const intLength = Math.pow(2, intInfo);
267
+ dictoffset = 2 + intLength;
268
+ if (intLength < 3) {
269
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
270
+ } else {
271
+ length = toSafeNumber(readUInt(buffer.slice(offset + 2, offset + 2 + intLength)));
272
+ }
273
+ }
274
+ if (length * 2 * objectRefSize > maxObjectSize) {
275
+ throw new Error("Too little heap space available!");
276
+ }
277
+ const dict: Record<string, any> = createSafeObject();
278
+ for (let i = 0; i < length; i++) {
279
+ const keyRef = toSafeNumber(readUInt(buffer.slice(offset + dictoffset + i * objectRefSize, offset + dictoffset + (i + 1) * objectRefSize)));
280
+ const valRef = toSafeNumber(readUInt(buffer.slice(offset + dictoffset + (length * objectRefSize) + i * objectRefSize, offset + dictoffset + (length * objectRefSize) + (i + 1) * objectRefSize)));
281
+ const key = parseObject(keyRef);
282
+ const val = parseObject(valRef);
283
+ dict[key] = val;
284
+ }
285
+ return dict;
286
+ }
287
+ }
288
+
289
+ return parseObject(topObject);
290
+ };
291
+
292
+ function readUInt(buffer: Uint8Array, start = 0): bigint {
293
+ let result = 0n;
294
+
295
+ for (let i = start; i < buffer.length; i++) {
296
+ result <<= 8n;
297
+ result |= BigInt(buffer[i]!);
298
+ }
299
+
300
+ return result;
301
+ }
302
+
303
+ function toSafeNumber(x: bigint): number {
304
+ if (x > BigInt(Number.MAX_SAFE_INTEGER)) {
305
+ throw new Error('Offset too large');
306
+ }
307
+ return Number(x);
308
+ }
309
+
310
+ function createSafeObject(): Record<string, any> {
311
+ return Object.create(null);
312
+ }