react-native-update-cli 2.29.1 → 2.29.2

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.
Files changed (80) hide show
  1. package/lib/account.js +2 -4
  2. package/lib/api.d.ts +1 -1
  3. package/lib/api.js +26 -44
  4. package/lib/app.js +13 -27
  5. package/lib/auto-update.d.ts +1 -1
  6. package/lib/auto-update.js +6 -16
  7. package/lib/bin.js +4 -6
  8. package/lib/bundle-pack.js +25 -27
  9. package/lib/bundle-runner.js +55 -54
  10. package/lib/bundle.js +18 -26
  11. package/lib/diff.js +32 -91
  12. package/lib/locales/en.d.ts +0 -10
  13. package/lib/locales/en.js +0 -10
  14. package/lib/locales/zh.d.ts +0 -10
  15. package/lib/locales/zh.js +0 -10
  16. package/lib/native-package.js +20 -60
  17. package/lib/package.d.ts +6 -1
  18. package/lib/package.js +50 -70
  19. package/lib/provider.js +3 -5
  20. package/lib/symbolicate.js +1 -16
  21. package/lib/utils/add-gitignore.js +0 -1
  22. package/lib/utils/app-info-parser/aab.d.ts +5 -22
  23. package/lib/utils/app-info-parser/aab.js +87 -192
  24. package/lib/utils/app-info-parser/apk.d.ts +12 -13
  25. package/lib/utils/app-info-parser/apk.js +28 -78
  26. package/lib/utils/app-info-parser/ipa.d.ts +2 -14
  27. package/lib/utils/app-info-parser/ipa.js +9 -70
  28. package/lib/utils/app-info-parser/resource-finder.d.ts +1 -26
  29. package/lib/utils/app-info-parser/resource-finder.js +41 -183
  30. package/lib/utils/app-info-parser/xml-parser/binary.d.ts +6 -55
  31. package/lib/utils/app-info-parser/xml-parser/binary.js +152 -556
  32. package/lib/utils/app-info-parser/zip.d.ts +6 -24
  33. package/lib/utils/app-info-parser/zip.js +19 -86
  34. package/lib/utils/check-plugin.d.ts +7 -5
  35. package/lib/utils/check-plugin.js +15 -25
  36. package/lib/utils/dep-versions.d.ts +0 -6
  37. package/lib/utils/dep-versions.js +2 -27
  38. package/lib/utils/failure-fingerprint.js +3 -4
  39. package/lib/utils/fs-helpers.d.ts +5 -0
  40. package/lib/utils/fs-helpers.js +19 -0
  41. package/lib/utils/git.d.ts +0 -1
  42. package/lib/utils/git.js +0 -8
  43. package/lib/utils/hash.d.ts +3 -0
  44. package/lib/utils/hash.js +19 -0
  45. package/lib/utils/hbcTransform.d.ts +9 -2
  46. package/lib/utils/hbcTransform.js +41 -70
  47. package/lib/utils/hermes-base.d.ts +1 -5
  48. package/lib/utils/hermes-base.js +93 -156
  49. package/lib/utils/hermes-literals.d.ts +5 -3
  50. package/lib/utils/hermes-literals.js +30 -53
  51. package/lib/utils/hermes-raw.d.ts +7 -0
  52. package/lib/utils/hermes-raw.js +26 -9
  53. package/lib/utils/http-helper.d.ts +0 -1
  54. package/lib/utils/http-helper.js +0 -20
  55. package/lib/utils/i18n.js +0 -2
  56. package/lib/utils/index.d.ts +5 -19
  57. package/lib/utils/index.js +95 -189
  58. package/lib/utils/latest-version.d.ts +19 -0
  59. package/lib/utils/latest-version.js +137 -0
  60. package/lib/utils/options.d.ts +2 -1
  61. package/lib/utils/options.js +5 -7
  62. package/lib/utils/zip-entries.d.ts +23 -0
  63. package/lib/utils/zip-entries.js +125 -102
  64. package/lib/utils/zip-options.js +29 -56
  65. package/lib/utils/zip-range.js +25 -65
  66. package/lib/versions.d.ts +5 -0
  67. package/lib/versions.js +86 -153
  68. package/package.json +1 -3
  69. package/lib/utils/app-info-parser/app.d.ts +0 -4
  70. package/lib/utils/app-info-parser/app.js +0 -10
  71. package/lib/utils/app-info-parser/index.d.ts +0 -16
  72. package/lib/utils/app-info-parser/index.js +0 -46
  73. package/lib/utils/app-info-parser/utils.d.ts +0 -31
  74. package/lib/utils/app-info-parser/utils.js +0 -135
  75. package/lib/utils/app-info-parser/xml-parser/manifest.d.ts +0 -10
  76. package/lib/utils/app-info-parser/xml-parser/manifest.js +0 -202
  77. package/lib/utils/latest-version/index.d.ts +0 -149
  78. package/lib/utils/latest-version/index.js +0 -326
  79. package/lib/utils/plugin-config.d.ts +0 -9
  80. package/lib/utils/plugin-config.js +0 -31
@@ -1,572 +1,168 @@
1
1
  "use strict";
2
+ // Binary (AAPT) XML reader, trimmed from
3
+ // https://github.com/openstf/adbkit-apkreader to the element tree and the
4
+ // attribute value types an AndroidManifest.xml needs.
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BinaryXmlParser = void 0;
4
- // From https://github.com/openstf/adbkit-apkreader
5
- const NodeType = {
6
- ELEMENT_NODE: 1,
7
- ATTRIBUTE_NODE: 2,
8
- CDATA_SECTION_NODE: 4,
9
- };
10
- const ChunkType = {
11
- NULL: 0x0000,
12
- STRING_POOL: 0x0001,
13
- TABLE: 0x0002,
14
- XML: 0x0003,
15
- XML_FIRST_CHUNK: 0x0100,
16
- XML_START_NAMESPACE: 0x0100,
17
- XML_END_NAMESPACE: 0x0101,
18
- XML_START_ELEMENT: 0x0102,
19
- XML_END_ELEMENT: 0x0103,
20
- XML_CDATA: 0x0104,
21
- XML_LAST_CHUNK: 0x017f,
22
- XML_RESOURCE_MAP: 0x0180,
23
- TABLE_PACKAGE: 0x0200,
24
- TABLE_TYPE: 0x0201,
25
- TABLE_TYPE_SPEC: 0x0202,
26
- };
27
- const StringFlags = {
28
- SORTED: 1 << 0,
29
- UTF8: 1 << 8,
30
- };
31
- // Taken from android.util.TypedValue
32
- const TypedValue = {
33
- COMPLEX_MANTISSA_MASK: 0x00ffffff,
34
- COMPLEX_MANTISSA_SHIFT: 0x00000008,
35
- COMPLEX_RADIX_0p23: 0x00000003,
36
- COMPLEX_RADIX_16p7: 0x00000001,
37
- COMPLEX_RADIX_23p0: 0x00000000,
38
- COMPLEX_RADIX_8p15: 0x00000002,
39
- COMPLEX_RADIX_MASK: 0x00000003,
40
- COMPLEX_RADIX_SHIFT: 0x00000004,
41
- COMPLEX_UNIT_DIP: 0x00000001,
42
- COMPLEX_UNIT_FRACTION: 0x00000000,
43
- COMPLEX_UNIT_FRACTION_PARENT: 0x00000001,
44
- COMPLEX_UNIT_IN: 0x00000004,
45
- COMPLEX_UNIT_MASK: 0x0000000f,
46
- COMPLEX_UNIT_MM: 0x00000005,
47
- COMPLEX_UNIT_PT: 0x00000003,
48
- COMPLEX_UNIT_PX: 0x00000000,
49
- COMPLEX_UNIT_SHIFT: 0x00000000,
50
- COMPLEX_UNIT_SP: 0x00000002,
51
- DENSITY_DEFAULT: 0x00000000,
52
- DENSITY_NONE: 0x0000ffff,
53
- TYPE_ATTRIBUTE: 0x00000002,
54
- TYPE_DIMENSION: 0x00000005,
55
- TYPE_FIRST_COLOR_INT: 0x0000001c,
56
- TYPE_FIRST_INT: 0x00000010,
57
- TYPE_FLOAT: 0x00000004,
58
- TYPE_FRACTION: 0x00000006,
59
- TYPE_INT_BOOLEAN: 0x00000012,
60
- TYPE_INT_COLOR_ARGB4: 0x0000001e,
61
- TYPE_INT_COLOR_ARGB8: 0x0000001c,
62
- TYPE_INT_COLOR_RGB4: 0x0000001f,
63
- TYPE_INT_COLOR_RGB8: 0x0000001d,
64
- TYPE_INT_DEC: 0x00000010,
65
- TYPE_INT_HEX: 0x00000011,
66
- TYPE_LAST_COLOR_INT: 0x0000001f,
67
- TYPE_LAST_INT: 0x0000001f,
68
- TYPE_NULL: 0x00000000,
69
- TYPE_REFERENCE: 0x00000001,
70
- TYPE_STRING: 0x00000003,
71
- };
72
- class BinaryXmlParser {
73
- buffer;
74
- cursor = 0;
75
- strings = [];
76
- resources = [];
77
- document = null;
78
- parent = null;
79
- stack = [];
80
- debug;
81
- constructor(buffer, options = {}) {
82
- this.buffer = buffer;
83
- this.cursor = 0;
84
- this.strings = [];
85
- this.resources = [];
86
- this.document = null;
87
- this.parent = null;
88
- this.stack = [];
89
- this.debug = options.debug || false;
90
- }
91
- readU8() {
92
- this.debug && console.group('readU8');
93
- this.debug && console.debug('cursor:', this.cursor);
94
- const val = this.buffer[this.cursor];
95
- this.debug && console.debug('value:', val);
96
- this.cursor += 1;
97
- this.debug && console.groupEnd();
98
- return val;
99
- }
100
- readU16() {
101
- this.debug && console.group('readU16');
102
- this.debug && console.debug('cursor:', this.cursor);
103
- const val = this.buffer.readUInt16LE(this.cursor);
104
- this.debug && console.debug('value:', val);
105
- this.cursor += 2;
106
- this.debug && console.groupEnd();
107
- return val;
108
- }
109
- readS32() {
110
- this.debug && console.group('readS32');
111
- this.debug && console.debug('cursor:', this.cursor);
112
- const val = this.buffer.readInt32LE(this.cursor);
113
- this.debug && console.debug('value:', val);
114
- this.cursor += 4;
115
- this.debug && console.groupEnd();
116
- return val;
117
- }
118
- readU32() {
119
- this.debug && console.group('readU32');
120
- this.debug && console.debug('cursor:', this.cursor);
121
- const val = this.buffer.readUInt32LE(this.cursor);
122
- this.debug && console.debug('value:', val);
123
- this.cursor += 4;
124
- this.debug && console.groupEnd();
125
- return val;
126
- }
127
- readLength8() {
128
- this.debug && console.group('readLength8');
129
- let len = this.readU8();
130
- if (len & 0x80) {
131
- len = (len & 0x7f) << 8;
132
- len += this.readU8();
133
- }
134
- this.debug && console.debug('length:', len);
135
- this.debug && console.groupEnd();
136
- return len;
137
- }
138
- readLength16() {
139
- this.debug && console.group('readLength16');
140
- let len = this.readU16();
141
- if (len & 0x8000) {
142
- len = (len & 0x7fff) << 16;
143
- len += this.readU16();
144
- }
145
- this.debug && console.debug('length:', len);
146
- this.debug && console.groupEnd();
147
- return len;
148
- }
149
- readDimension() {
150
- this.debug && console.group('readDimension');
151
- const dimension = {
152
- value: null,
153
- unit: null,
154
- rawUnit: null,
155
- };
156
- const value = this.readU32();
157
- const unit = value & 0xff;
158
- dimension.value = value >> 8;
159
- dimension.rawUnit = unit;
160
- switch (unit) {
161
- case TypedValue.COMPLEX_UNIT_MM:
162
- dimension.unit = 'mm';
163
- break;
164
- case TypedValue.COMPLEX_UNIT_PX:
165
- dimension.unit = 'px';
166
- break;
167
- case TypedValue.COMPLEX_UNIT_DIP:
168
- dimension.unit = 'dp';
169
- break;
170
- case TypedValue.COMPLEX_UNIT_SP:
171
- dimension.unit = 'sp';
172
- break;
173
- case TypedValue.COMPLEX_UNIT_PT:
174
- dimension.unit = 'pt';
175
- break;
176
- case TypedValue.COMPLEX_UNIT_IN:
177
- dimension.unit = 'in';
178
- break;
179
- }
180
- this.debug && console.groupEnd();
181
- return dimension;
182
- }
183
- readFraction() {
184
- this.debug && console.group('readFraction');
185
- const fraction = {
186
- value: null,
187
- type: null,
188
- rawType: null,
189
- };
190
- const value = this.readU32();
191
- const type = value & 0xf;
192
- fraction.value = this.convertIntToFloat(value >> 4);
193
- fraction.rawType = type;
194
- switch (type) {
195
- case TypedValue.COMPLEX_UNIT_FRACTION:
196
- fraction.type = '%';
197
- break;
198
- case TypedValue.COMPLEX_UNIT_FRACTION_PARENT:
199
- fraction.type = '%p';
200
- break;
201
- }
202
- this.debug && console.groupEnd();
203
- return fraction;
204
- }
205
- readHex24() {
206
- this.debug && console.group('readHex24');
207
- const val = (this.readU32() & 0xffffff).toString(16);
208
- this.debug && console.groupEnd();
209
- return val;
210
- }
211
- readHex32() {
212
- this.debug && console.group('readHex32');
213
- const val = this.readU32().toString(16);
214
- this.debug && console.groupEnd();
215
- return val;
216
- }
217
- readTypedValue() {
218
- this.debug && console.group('readTypedValue');
219
- const typedValue = {
220
- value: null,
221
- type: null,
222
- rawType: null,
223
- };
224
- const start = this.cursor;
225
- let size = this.readU16();
226
- /* const zero = */ this.readU8();
227
- const dataType = this.readU8();
228
- if (size === 0) {
229
- size = 8;
230
- }
231
- typedValue.rawType = dataType;
232
- switch (dataType) {
233
- case TypedValue.TYPE_INT_DEC:
234
- typedValue.value = this.readS32();
235
- typedValue.type = 'int_dec';
236
- break;
237
- case TypedValue.TYPE_INT_HEX:
238
- typedValue.value = this.readS32();
239
- typedValue.type = 'int_hex';
240
- break;
241
- case TypedValue.TYPE_STRING: {
242
- const ref = this.readS32();
243
- typedValue.value = ref > 0 ? this.strings[ref] : '';
244
- typedValue.type = 'string';
245
- break;
6
+ exports.parseBinaryXml = parseBinaryXml;
7
+ const CHUNK_STRING_POOL = 0x0001;
8
+ const CHUNK_XML = 0x0003;
9
+ const CHUNK_XML_START_ELEMENT = 0x0102;
10
+ const CHUNK_XML_END_ELEMENT = 0x0103;
11
+ const STRING_POOL_UTF8 = 1 << 8;
12
+ // android.util.TypedValue
13
+ const TYPE_NULL = 0x00;
14
+ const TYPE_REFERENCE = 0x01;
15
+ const TYPE_STRING = 0x03;
16
+ const TYPE_INT_DEC = 0x10;
17
+ const TYPE_INT_HEX = 0x11;
18
+ const TYPE_INT_BOOLEAN = 0x12;
19
+ function parseBinaryXml(buffer) {
20
+ let cursor = 0;
21
+ const strings = [];
22
+ const u8 = () => buffer[cursor++];
23
+ const u16 = () => {
24
+ const value = buffer.readUInt16LE(cursor);
25
+ cursor += 2;
26
+ return value;
27
+ };
28
+ const u32 = () => {
29
+ const value = buffer.readUInt32LE(cursor);
30
+ cursor += 4;
31
+ return value;
32
+ };
33
+ const s32 = () => {
34
+ const value = buffer.readInt32LE(cursor);
35
+ cursor += 4;
36
+ return value;
37
+ };
38
+ const readString = (utf8) => {
39
+ let byteLength;
40
+ if (utf8) {
41
+ // character count, then byte count; each 1 or 2 bytes
42
+ if (u8() & 0x80)
43
+ u8();
44
+ byteLength = u8();
45
+ if (byteLength & 0x80) {
46
+ byteLength = ((byteLength & 0x7f) << 8) + u8();
246
47
  }
247
- case TypedValue.TYPE_REFERENCE: {
248
- const id = this.readU32();
249
- typedValue.value = `resourceId:0x${id.toString(16)}`;
250
- typedValue.type = 'reference';
251
- break;
252
- }
253
- case TypedValue.TYPE_INT_BOOLEAN:
254
- typedValue.value = this.readS32() !== 0;
255
- typedValue.type = 'boolean';
256
- break;
257
- case TypedValue.TYPE_NULL:
258
- this.readU32();
259
- typedValue.value = null;
260
- typedValue.type = 'null';
261
- break;
262
- case TypedValue.TYPE_INT_COLOR_RGB8:
263
- typedValue.value = this.readHex24();
264
- typedValue.type = 'rgb8';
265
- break;
266
- case TypedValue.TYPE_INT_COLOR_RGB4:
267
- typedValue.value = this.readHex24();
268
- typedValue.type = 'rgb4';
269
- break;
270
- case TypedValue.TYPE_INT_COLOR_ARGB8:
271
- typedValue.value = this.readHex32();
272
- typedValue.type = 'argb8';
273
- break;
274
- case TypedValue.TYPE_INT_COLOR_ARGB4:
275
- typedValue.value = this.readHex32();
276
- typedValue.type = 'argb4';
277
- break;
278
- case TypedValue.TYPE_DIMENSION:
279
- typedValue.value = this.readDimension();
280
- typedValue.type = 'dimension';
281
- break;
282
- case TypedValue.TYPE_FRACTION:
283
- typedValue.value = this.readFraction();
284
- typedValue.type = 'fraction';
285
- break;
286
- default: {
287
- const type = dataType.toString(16);
288
- console.debug(`Not sure what to do with typed value of type 0x${type}, falling back to reading an uint32.`);
289
- typedValue.value = this.readU32();
290
- typedValue.type = 'unknown';
291
- }
292
- }
293
- const end = start + size;
294
- if (this.cursor !== end) {
295
- const type = dataType.toString(16);
296
- const diff = end - this.cursor;
297
- console.debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed end \
298
- of typed value of type 0x${type}. The typed value started at offset ${start} \
299
- and is supposed to end at offset ${end}. Ignoring the rest of the value.`);
300
- this.cursor = end;
301
48
  }
302
- this.debug && console.groupEnd();
303
- return typedValue;
304
- }
305
- // https://twitter.com/kawasima/status/427730289201139712
306
- convertIntToFloat(int) {
307
- const buf = new ArrayBuffer(4);
308
- new Int32Array(buf)[0] = int;
309
- return new Float32Array(buf)[0];
310
- }
311
- readString(encoding) {
312
- this.debug && console.group('readString', encoding);
313
- switch (encoding) {
314
- case 'utf-8': {
315
- const stringLength = this.readLength8();
316
- this.debug && console.debug('stringLength:', stringLength);
317
- const byteLength = this.readLength8();
318
- this.debug && console.debug('byteLength:', byteLength);
319
- const value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength));
320
- this.debug && console.debug('value:', value);
321
- this.debug && console.groupEnd();
322
- return value;
49
+ else {
50
+ byteLength = u16();
51
+ if (byteLength & 0x8000) {
52
+ byteLength = ((byteLength & 0x7fff) << 16) + u16();
323
53
  }
324
- case 'ucs2': {
325
- const stringLength = this.readLength16();
326
- this.debug && console.debug('stringLength:', stringLength);
327
- const byteLength = stringLength * 2;
328
- this.debug && console.debug('byteLength:', byteLength);
329
- const value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength));
330
- this.debug && console.debug('value:', value);
331
- this.debug && console.groupEnd();
332
- return value;
54
+ byteLength *= 2;
55
+ }
56
+ const start = cursor;
57
+ cursor += byteLength;
58
+ return buffer.toString(utf8 ? 'utf-8' : 'ucs2', start, cursor);
59
+ };
60
+ const readStringPool = (chunkStart) => {
61
+ const count = u32();
62
+ u32(); // style count
63
+ const flags = u32();
64
+ const stringsStart = chunkStart + u32();
65
+ u32(); // styles start
66
+ const offsets = [];
67
+ for (let i = 0; i < count; i++) {
68
+ offsets.push(u32());
69
+ }
70
+ for (const offset of offsets) {
71
+ cursor = stringsStart + offset;
72
+ strings.push(readString((flags & STRING_POOL_UTF8) !== 0));
73
+ }
74
+ };
75
+ const readTypedValue = () => {
76
+ const start = cursor;
77
+ const size = u16() || 8;
78
+ u8(); // res0
79
+ const type = u8();
80
+ const data = start + 4;
81
+ cursor = start + size;
82
+ switch (type) {
83
+ case TYPE_INT_DEC:
84
+ case TYPE_INT_HEX:
85
+ return buffer.readInt32LE(data);
86
+ case TYPE_STRING: {
87
+ const ref = buffer.readInt32LE(data);
88
+ return ref > 0 ? strings[ref] : '';
333
89
  }
90
+ case TYPE_REFERENCE:
91
+ return `resourceId:0x${buffer.readUInt32LE(data).toString(16)}`;
92
+ case TYPE_INT_BOOLEAN:
93
+ return buffer.readInt32LE(data) !== 0;
94
+ case TYPE_NULL:
95
+ return null;
334
96
  default:
335
- throw new Error(`Unsupported encoding '${encoding}'`);
336
- }
337
- }
338
- readChunkHeader() {
339
- this.debug && console.group('readChunkHeader');
340
- const header = {
341
- startOffset: this.cursor,
342
- chunkType: this.readU16(),
343
- headerSize: this.readU16(),
344
- chunkSize: this.readU32(),
345
- };
346
- this.debug && console.debug('startOffset:', header.startOffset);
347
- this.debug && console.debug('chunkType:', header.chunkType);
348
- this.debug && console.debug('headerSize:', header.headerSize);
349
- this.debug && console.debug('chunkSize:', header.chunkSize);
350
- this.debug && console.groupEnd();
351
- return header;
352
- }
353
- readStringPool(header) {
354
- this.debug && console.group('readStringPool');
355
- header.stringCount = this.readU32();
356
- this.debug && console.debug('stringCount:', header.stringCount);
357
- header.styleCount = this.readU32();
358
- this.debug && console.debug('styleCount:', header.styleCount);
359
- header.flags = this.readU32();
360
- this.debug && console.debug('flags:', header.flags);
361
- header.stringsStart = this.readU32();
362
- this.debug && console.debug('stringsStart:', header.stringsStart);
363
- header.stylesStart = this.readU32();
364
- this.debug && console.debug('stylesStart:', header.stylesStart);
365
- if (header.chunkType !== ChunkType.STRING_POOL) {
366
- throw new Error('Invalid string pool header');
367
- }
368
- const offsets = [];
369
- for (let i = 0, l = header.stringCount; i < l; ++i) {
370
- this.debug && console.debug('offset:', i);
371
- offsets.push(this.readU32());
372
- }
373
- const sorted = (header.flags & StringFlags.SORTED) === StringFlags.SORTED;
374
- this.debug && console.debug('sorted:', sorted);
375
- const encoding = (header.flags & StringFlags.UTF8) === StringFlags.UTF8 ? 'utf-8' : 'ucs2';
376
- this.debug && console.debug('encoding:', encoding);
377
- const stringsStart = header.startOffset + header.stringsStart;
378
- this.cursor = stringsStart;
379
- for (let i = 0, l = header.stringCount; i < l; ++i) {
380
- this.debug && console.debug('string:', i);
381
- this.debug && console.debug('offset:', offsets[i]);
382
- this.cursor = stringsStart + offsets[i];
383
- this.strings.push(this.readString(encoding));
384
- }
385
- this.cursor = header.startOffset + header.chunkSize;
386
- this.debug && console.groupEnd();
387
- return null;
388
- }
389
- readResourceMap(header) {
390
- this.debug && console.group('readResourceMap');
391
- const count = Math.floor((header.chunkSize - header.headerSize) / 4);
392
- for (let i = 0; i < count; ++i) {
393
- this.resources.push(this.readU32());
394
- }
395
- this.debug && console.groupEnd();
396
- return null;
397
- }
398
- readXmlNamespaceStart() {
399
- this.debug && console.group('readXmlNamespaceStart');
400
- /* const line = */ this.readU32();
401
- /* const commentRef = */ this.readU32();
402
- /* const prefixRef = */ this.readS32();
403
- /* const uriRef = */ this.readS32();
404
- this.debug && console.groupEnd();
405
- return null;
406
- }
407
- readXmlNamespaceEnd() {
408
- this.debug && console.group('readXmlNamespaceEnd');
409
- /* const line = */ this.readU32();
410
- /* const commentRef = */ this.readU32();
411
- /* const prefixRef = */ this.readS32();
412
- /* const uriRef = */ this.readS32();
413
- this.debug && console.groupEnd();
414
- return null;
415
- }
416
- readXmlElementStart() {
417
- this.debug && console.group('readXmlElementStart');
418
- const node = {
419
- namespaceURI: null,
420
- nodeType: NodeType.ELEMENT_NODE,
421
- nodeName: null,
422
- attributes: [],
423
- childNodes: [],
97
+ // floats, dimensions, colors...: not needed, keep the raw bits
98
+ return buffer.readUInt32LE(data);
99
+ }
100
+ };
101
+ const readElement = () => {
102
+ u32(); // line
103
+ u32(); // comment
104
+ s32(); // namespace
105
+ const element = {
106
+ name: strings[s32()],
107
+ attributes: Object.create(null),
108
+ children: [],
424
109
  };
425
- /* const line = */ this.readU32();
426
- /* const commentRef = */ this.readU32();
427
- const nsRef = this.readS32();
428
- const nameRef = this.readS32();
429
- if (nsRef > 0) {
430
- node.namespaceURI = this.strings[nsRef];
431
- }
432
- node.nodeName = this.strings[nameRef];
433
- /* const attrStart = */ this.readU16();
434
- /* const attrSize = */ this.readU16();
435
- const attrCount = this.readU16();
436
- /* const idIndex = */ this.readU16();
437
- /* const classIndex = */ this.readU16();
438
- /* const styleIndex = */ this.readU16();
439
- for (let i = 0; i < attrCount; ++i) {
440
- node.attributes.push(this.readXmlAttribute());
441
- }
442
- if (this.document) {
443
- this.parent.childNodes.push(node);
444
- this.parent = node;
445
- }
446
- else {
447
- this.document = this.parent = node;
448
- }
449
- this.stack.push(node);
450
- this.debug && console.groupEnd();
451
- return node;
452
- }
453
- readXmlAttribute() {
454
- this.debug && console.group('readXmlAttribute');
455
- const attr = {
456
- namespaceURI: null,
457
- nodeType: NodeType.ATTRIBUTE_NODE,
458
- nodeName: null,
459
- name: null,
460
- value: null,
461
- typedValue: null,
462
- };
463
- const nsRef = this.readS32();
464
- const nameRef = this.readS32();
465
- const valueRef = this.readS32();
466
- if (nsRef > 0) {
467
- attr.namespaceURI = this.strings[nsRef];
468
- }
469
- attr.nodeName = attr.name = this.strings[nameRef];
470
- if (valueRef > 0) {
471
- if (attr.name === 'versionName') {
110
+ u16(); // attribute start
111
+ u16(); // attribute size
112
+ const attributeCount = u16();
113
+ cursor += 6; // id, class and style indexes
114
+ for (let i = 0; i < attributeCount; i++) {
115
+ s32(); // namespace
116
+ const name = strings[s32()];
117
+ s32(); // raw value
118
+ let value = readTypedValue();
119
+ if (name === 'versionName' && typeof value === 'string') {
472
120
  // strip UTF-16 null padding only; keep spaces and non-ASCII chars
473
121
  // biome-ignore lint/suspicious/noControlCharactersInRegex: removing NUL padding is the point
474
- this.strings[valueRef] = this.strings[valueRef].replace(/\u0000/g, '');
122
+ value = value.replace(/\u0000/g, '');
475
123
  }
476
- attr.value = this.strings[valueRef];
477
- }
478
- attr.typedValue = this.readTypedValue();
479
- this.debug && console.groupEnd();
480
- return attr;
481
- }
482
- readXmlElementEnd() {
483
- this.debug && console.group('readXmlCData');
484
- /* const line = */ this.readU32();
485
- /* const commentRef = */ this.readU32();
486
- /* const nsRef = */ this.readS32();
487
- /* const nameRef = */ this.readS32();
488
- this.stack.pop();
489
- this.parent = this.stack[this.stack.length - 1];
490
- this.debug && console.groupEnd();
491
- return null;
492
- }
493
- readXmlCData() {
494
- this.debug && console.group('readXmlCData');
495
- const cdata = {
496
- namespaceURI: null,
497
- nodeType: NodeType.CDATA_SECTION_NODE,
498
- nodeName: '#cdata',
499
- data: null,
500
- typedValue: null,
501
- };
502
- /* const line = */ this.readU32();
503
- /* const commentRef = */ this.readU32();
504
- const dataRef = this.readS32();
505
- if (dataRef > 0) {
506
- cdata.data = this.strings[dataRef];
507
- }
508
- cdata.typedValue = this.readTypedValue();
509
- this.parent.childNodes.push(cdata);
510
- this.debug && console.groupEnd();
511
- return cdata;
512
- }
513
- readNull(header) {
514
- this.debug && console.group('readNull');
515
- this.cursor += header.chunkSize - header.headerSize;
516
- this.debug && console.groupEnd();
517
- return null;
518
- }
519
- parse() {
520
- this.debug && console.group('BinaryXmlParser.parse');
521
- const xmlHeader = this.readChunkHeader();
522
- if (xmlHeader.chunkType !== ChunkType.XML) {
523
- throw new Error('Invalid XML header');
124
+ element.attributes[name] = value;
125
+ }
126
+ return element;
127
+ };
128
+ if (u16() !== CHUNK_XML) {
129
+ throw new Error('Invalid XML header');
130
+ }
131
+ cursor = 8; // past the XML chunk header
132
+ let root;
133
+ const stack = [];
134
+ while (cursor < buffer.length) {
135
+ const start = cursor;
136
+ const type = u16();
137
+ u16(); // header size
138
+ const size = u32();
139
+ if (size < 8) {
140
+ throw new Error(`Invalid chunk size ${size} at offset ${start}`);
524
141
  }
525
- while (this.cursor < this.buffer.length) {
526
- this.debug && console.group('chunk');
527
- const start = this.cursor;
528
- const header = this.readChunkHeader();
529
- switch (header.chunkType) {
530
- case ChunkType.STRING_POOL:
531
- this.readStringPool(header);
532
- break;
533
- case ChunkType.XML_RESOURCE_MAP:
534
- this.readResourceMap(header);
535
- break;
536
- case ChunkType.XML_START_NAMESPACE:
537
- this.readXmlNamespaceStart();
538
- break;
539
- case ChunkType.XML_END_NAMESPACE:
540
- this.readXmlNamespaceEnd();
541
- break;
542
- case ChunkType.XML_START_ELEMENT:
543
- this.readXmlElementStart();
544
- break;
545
- case ChunkType.XML_END_ELEMENT:
546
- this.readXmlElementEnd();
547
- break;
548
- case ChunkType.XML_CDATA:
549
- this.readXmlCData();
550
- break;
551
- case ChunkType.NULL:
552
- this.readNull(header);
553
- break;
554
- default:
555
- throw new Error(`Unsupported chunk type '${header.chunkType}'`);
556
- }
557
- const end = start + header.chunkSize;
558
- if (this.cursor !== end) {
559
- const diff = end - this.cursor;
560
- const type = header.chunkType.toString(16);
561
- console.debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed \
562
- end of chunk of type 0x${type}. The chunk started at offset ${start} and is \
563
- supposed to end at offset ${end}. Ignoring the rest of the chunk.`);
564
- this.cursor = end;
142
+ switch (type) {
143
+ case CHUNK_STRING_POOL:
144
+ readStringPool(start);
145
+ break;
146
+ case CHUNK_XML_START_ELEMENT: {
147
+ const element = readElement();
148
+ if (stack.length) {
149
+ stack[stack.length - 1].children.push(element);
150
+ }
151
+ else {
152
+ root ??= element;
153
+ }
154
+ stack.push(element);
155
+ break;
565
156
  }
566
- this.debug && console.groupEnd();
157
+ case CHUNK_XML_END_ELEMENT:
158
+ stack.pop();
159
+ break;
160
+ // namespaces, resource map, CDATA: not needed
567
161
  }
568
- this.debug && console.groupEnd();
569
- return this.document;
162
+ cursor = start + size;
163
+ }
164
+ if (!root) {
165
+ throw new Error('No root element');
570
166
  }
167
+ return root;
571
168
  }
572
- exports.BinaryXmlParser = BinaryXmlParser;