binaryxml.js
18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
'use strict'
// Heavily inspired by https://github.com/xiaxiaocao/apk-parser
/*eslint no-console: "off"*/
const assert = require('assert')
const debug = require('debug')('adb:apkreader:parser:binaryxml')
const NodeType = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
CDATA_SECTION_NODE: 4
}
const ChunkType = {
NULL: 0x0000,
STRING_POOL: 0x0001,
TABLE: 0x0002,
XML: 0x0003,
XML_FIRST_CHUNK: 0x0100,
XML_START_NAMESPACE: 0x0100,
XML_END_NAMESPACE: 0x0101,
XML_START_ELEMENT: 0x0102,
XML_END_ELEMENT: 0x0103,
XML_CDATA: 0x0104,
XML_LAST_CHUNK: 0x017f,
XML_RESOURCE_MAP: 0x0180,
TABLE_PACKAGE: 0x0200,
TABLE_TYPE: 0x0201,
TABLE_TYPE_SPEC: 0x0202
}
const StringFlags = {
SORTED: 1 << 0,
UTF8: 1 << 8
}
// Taken from android.util.TypedValue
const TypedValue = {
COMPLEX_MANTISSA_MASK: 0x00ffffff,
COMPLEX_MANTISSA_SHIFT: 0x00000008,
COMPLEX_RADIX_0p23: 0x00000003,
COMPLEX_RADIX_16p7: 0x00000001,
COMPLEX_RADIX_23p0: 0x00000000,
COMPLEX_RADIX_8p15: 0x00000002,
COMPLEX_RADIX_MASK: 0x00000003,
COMPLEX_RADIX_SHIFT: 0x00000004,
COMPLEX_UNIT_DIP: 0x00000001,
COMPLEX_UNIT_FRACTION: 0x00000000,
COMPLEX_UNIT_FRACTION_PARENT: 0x00000001,
COMPLEX_UNIT_IN: 0x00000004,
COMPLEX_UNIT_MASK: 0x0000000f,
COMPLEX_UNIT_MM: 0x00000005,
COMPLEX_UNIT_PT: 0x00000003,
COMPLEX_UNIT_PX: 0x00000000,
COMPLEX_UNIT_SHIFT: 0x00000000,
COMPLEX_UNIT_SP: 0x00000002,
DENSITY_DEFAULT: 0x00000000,
DENSITY_NONE: 0x0000ffff,
TYPE_ATTRIBUTE: 0x00000002,
TYPE_DIMENSION: 0x00000005,
TYPE_FIRST_COLOR_INT: 0x0000001c,
TYPE_FIRST_INT: 0x00000010,
TYPE_FLOAT: 0x00000004,
TYPE_FRACTION: 0x00000006,
TYPE_INT_BOOLEAN: 0x00000012,
TYPE_INT_COLOR_ARGB4: 0x0000001e,
TYPE_INT_COLOR_ARGB8: 0x0000001c,
TYPE_INT_COLOR_RGB4: 0x0000001f,
TYPE_INT_COLOR_RGB8: 0x0000001d,
TYPE_INT_DEC: 0x00000010,
TYPE_INT_HEX: 0x00000011,
TYPE_LAST_COLOR_INT: 0x0000001f,
TYPE_LAST_INT: 0x0000001f,
TYPE_NULL: 0x00000000,
TYPE_REFERENCE: 0x00000001,
TYPE_STRING: 0x00000003
}
class BinaryXmlParser {
constructor(buffer, options = {}) {
this.buffer = buffer
this.cursor = 0
this.strings = []
this.resources = []
this.document = null
this.parent = null
this.stack = []
this.debug = options.debug || false
}
readU8() {
this.debug && console.group('readU8')
this.debug && console.debug('cursor:', this.cursor)
const val = this.buffer[this.cursor]
this.debug && console.debug('value:', val)
this.cursor += 1
this.debug && console.groupEnd()
return val
}
readU16() {
this.debug && console.group('readU16')
this.debug && console.debug('cursor:', this.cursor)
const val = this.buffer.readUInt16LE(this.cursor)
this.debug && console.debug('value:', val)
this.cursor += 2
this.debug && console.groupEnd()
return val
}
readS32() {
this.debug && console.group('readS32')
this.debug && console.debug('cursor:', this.cursor)
const val = this.buffer.readInt32LE(this.cursor)
this.debug && console.debug('value:', val)
this.cursor += 4
this.debug && console.groupEnd()
return val
}
readU32() {
this.debug && console.group('readU32')
this.debug && console.debug('cursor:', this.cursor)
const val = this.buffer.readUInt32LE(this.cursor)
this.debug && console.debug('value:', val)
this.cursor += 4
this.debug && console.groupEnd()
return val
}
readLength8() {
this.debug && console.group('readLength8')
let len = this.readU8()
if (len & 0x80) {
len = (len & 0x7f) << 8
len += this.readU8()
}
this.debug && console.debug('length:', len)
this.debug && console.groupEnd()
return len
}
readLength16() {
this.debug && console.group('readLength16')
let len = this.readU16()
if (len & 0x8000) {
len = (len & 0x7fff) << 16
len += this.readU16()
}
this.debug && console.debug('length:', len)
this.debug && console.groupEnd()
return len
}
readDimension() {
this.debug && console.group('readDimension')
const dimension = {
value: null,
unit: null,
rawUnit: null
}
const value = this.readU32()
const unit = dimension.value & 0xff
dimension.value = value >> 8
dimension.rawUnit = unit
switch (unit) {
case TypedValue.COMPLEX_UNIT_MM:
dimension.unit = 'mm'
break
case TypedValue.COMPLEX_UNIT_PX:
dimension.unit = 'px'
break
case TypedValue.COMPLEX_UNIT_DIP:
dimension.unit = 'dp'
break
case TypedValue.COMPLEX_UNIT_SP:
dimension.unit = 'sp'
break
case TypedValue.COMPLEX_UNIT_PT:
dimension.unit = 'pt'
break
case TypedValue.COMPLEX_UNIT_IN:
dimension.unit = 'in'
break
}
this.debug && console.groupEnd()
return dimension
}
readFraction() {
this.debug && console.group('readFraction')
const fraction = {
value: null,
type: null,
rawType: null
}
const value = this.readU32()
const type = value & 0xf
fraction.value = this.convertIntToFloat(value >> 4)
fraction.rawType = type
switch (type) {
case TypedValue.COMPLEX_UNIT_FRACTION:
fraction.type = '%'
break
case TypedValue.COMPLEX_UNIT_FRACTION_PARENT:
fraction.type = '%p'
break
}
this.debug && console.groupEnd()
return fraction
}
readHex24() {
this.debug && console.group('readHex24')
var val = (this.readU32() & 0xffffff).toString(16)
this.debug && console.groupEnd()
return val
}
readHex32() {
this.debug && console.group('readHex32')
var val = this.readU32().toString(16)
this.debug && console.groupEnd()
return val
}
readTypedValue() {
this.debug && console.group('readTypedValue')
const typedValue = {
value: null,
type: null,
rawType: null
}
const start = this.cursor
let size = this.readU16()
/* const zero = */ this.readU8()
const dataType = this.readU8()
// Yes, there has been a real world APK where the size is malformed.
if (size === 0) {
size = 8
}
typedValue.rawType = dataType
switch (dataType) {
case TypedValue.TYPE_INT_DEC:
typedValue.value = this.readS32()
typedValue.type = 'int_dec'
break
case TypedValue.TYPE_INT_HEX:
typedValue.value = this.readS32()
typedValue.type = 'int_hex'
break
case TypedValue.TYPE_STRING:
var ref = this.readS32()
typedValue.value = ref > 0 ? this.strings[ref] : ''
typedValue.type = 'string'
break
case TypedValue.TYPE_REFERENCE:
var id = this.readU32()
typedValue.value = `resourceId:0x${id.toString(16)}`
typedValue.type = 'reference'
break
case TypedValue.TYPE_INT_BOOLEAN:
typedValue.value = this.readS32() !== 0
typedValue.type = 'boolean'
break
case TypedValue.TYPE_NULL:
this.readU32()
typedValue.value = null
typedValue.type = 'null'
break
case TypedValue.TYPE_INT_COLOR_RGB8:
typedValue.value = this.readHex24()
typedValue.type = 'rgb8'
break
case TypedValue.TYPE_INT_COLOR_RGB4:
typedValue.value = this.readHex24()
typedValue.type = 'rgb4'
break
case TypedValue.TYPE_INT_COLOR_ARGB8:
typedValue.value = this.readHex32()
typedValue.type = 'argb8'
break
case TypedValue.TYPE_INT_COLOR_ARGB4:
typedValue.value = this.readHex32()
typedValue.type = 'argb4'
break
case TypedValue.TYPE_DIMENSION:
typedValue.value = this.readDimension()
typedValue.type = 'dimension'
break
case TypedValue.TYPE_FRACTION:
typedValue.value = this.readFraction()
typedValue.type = 'fraction'
break
default: {
const type = dataType.toString(16)
debug(`Not sure what to do with typed value of type 0x${type}, falling \
back to reading an uint32.`)
typedValue.value = this.readU32()
typedValue.type = 'unknown'
}
}
// Ensure we consume the whole value
const end = start + size
if (this.cursor !== end) {
const type = dataType.toString(16)
const diff = end - this.cursor
debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed end \
of typed value of type 0x${type}. The typed value started at offset ${start} \
and is supposed to end at offset ${end}. Ignoring the rest of the value.`)
this.cursor = end
}
this.debug && console.groupEnd()
return typedValue
}
// https://twitter.com/kawasima/status/427730289201139712
convertIntToFloat(int) {
const buf = new ArrayBuffer(4)
;(new Int32Array(buf))[0] = int
return (new Float32Array(buf))[0]
}
readString(encoding) {
this.debug && console.group('readString', encoding)
switch (encoding) {
case 'utf-8':
var stringLength = this.readLength8(encoding)
this.debug && console.debug('stringLength:', stringLength)
var byteLength = this.readLength8(encoding)
this.debug && console.debug('byteLength:', byteLength)
var value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
this.debug && console.debug('value:', value)
assert.equal(this.readU8(), 0, 'String must end with trailing zero')
this.debug && console.groupEnd()
return value
case 'ucs2':
stringLength = this.readLength16(encoding)
this.debug && console.debug('stringLength:', stringLength)
byteLength = stringLength * 2
this.debug && console.debug('byteLength:', byteLength)
value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
this.debug && console.debug('value:', value)
assert.equal(this.readU16(), 0, 'String must end with trailing zero')
this.debug && console.groupEnd()
return value
default:
throw new Error(`Unsupported encoding '${encoding}'`)
}
}
readChunkHeader() {
this.debug && console.group('readChunkHeader')
var header = {
startOffset: this.cursor,
chunkType: this.readU16(),
headerSize: this.readU16(),
chunkSize: this.readU32()
}
this.debug && console.debug('startOffset:', header.startOffset)
this.debug && console.debug('chunkType:', header.chunkType)
this.debug && console.debug('headerSize:', header.headerSize)
this.debug && console.debug('chunkSize:', header.chunkSize)
this.debug && console.groupEnd()
return header
}
readStringPool(header) {
this.debug && console.group('readStringPool')
header.stringCount = this.readU32()
this.debug && console.debug('stringCount:', header.stringCount)
header.styleCount = this.readU32()
this.debug && console.debug('styleCount:', header.styleCount)
header.flags = this.readU32()
this.debug && console.debug('flags:', header.flags)
header.stringsStart = this.readU32()
this.debug && console.debug('stringsStart:', header.stringsStart)
header.stylesStart = this.readU32()
this.debug && console.debug('stylesStart:', header.stylesStart)
if (header.chunkType !== ChunkType.STRING_POOL) {
throw new Error('Invalid string pool header')
}
const offsets = []
for (let i = 0, l = header.stringCount; i < l; ++i) {
this.debug && console.debug('offset:', i)
offsets.push(this.readU32())
}
const sorted = (header.flags & StringFlags.SORTED) === StringFlags.SORTED
this.debug && console.debug('sorted:', sorted)
const encoding = (header.flags & StringFlags.UTF8) === StringFlags.UTF8
? 'utf-8'
: 'ucs2'
this.debug && console.debug('encoding:', encoding)
const stringsStart = header.startOffset + header.stringsStart
this.cursor = stringsStart
for (let i = 0, l = header.stringCount; i < l; ++i) {
this.debug && console.debug('string:', i)
this.debug && console.debug('offset:', offsets[i])
this.cursor = stringsStart + offsets[i]
this.strings.push(this.readString(encoding))
}
// Skip styles
this.cursor = header.startOffset + header.chunkSize
this.debug && console.groupEnd()
return null
}
readResourceMap(header) {
this.debug && console.group('readResourceMap')
const count = Math.floor((header.chunkSize - header.headerSize) / 4)
for (let i = 0; i < count; ++i) {
this.resources.push(this.readU32())
}
this.debug && console.groupEnd()
return null
}
readXmlNamespaceStart(/* header */) {
this.debug && console.group('readXmlNamespaceStart')
/* const line = */ this.readU32()
/* const commentRef = */ this.readU32()
/* const prefixRef = */ this.readS32()
/* const uriRef = */ this.readS32()
// We don't currently care about the values, but they could
// be accessed like so:
//
// namespaceURI.prefix = this.strings[prefixRef] // if prefixRef > 0
// namespaceURI.uri = this.strings[uriRef] // if uriRef > 0
this.debug && console.groupEnd()
return null
}
readXmlNamespaceEnd(/* header */) {
this.debug && console.group('readXmlNamespaceEnd')
/* const line = */ this.readU32()
/* const commentRef = */ this.readU32()
/* const prefixRef = */ this.readS32()
/* const uriRef = */ this.readS32()
// We don't currently care about the values, but they could
// be accessed like so:
//
// namespaceURI.prefix = this.strings[prefixRef] // if prefixRef > 0
// namespaceURI.uri = this.strings[uriRef] // if uriRef > 0
this.debug && console.groupEnd()
return null
}
readXmlElementStart(/* header */) {
this.debug && console.group('readXmlElementStart')
const node = {
namespaceURI: null,
nodeType: NodeType.ELEMENT_NODE,
nodeName: null,
attributes: [],
childNodes: []
}
/* const line = */ this.readU32()
/* const commentRef = */ this.readU32()
const nsRef = this.readS32()
const nameRef = this.readS32()
if (nsRef > 0) {
node.namespaceURI = this.strings[nsRef]
}
node.nodeName = this.strings[nameRef]
/* const attrStart = */ this.readU16()
/* const attrSize = */ this.readU16()
const attrCount = this.readU16()
/* const idIndex = */ this.readU16()
/* const classIndex = */ this.readU16()
/* const styleIndex = */ this.readU16()
for (let i = 0; i < attrCount; ++i) {
node.attributes.push(this.readXmlAttribute())
}
if (this.document) {
this.parent.childNodes.push(node)
this.parent = node
} else {
this.document = (this.parent = node)
}
this.stack.push(node)
this.debug && console.groupEnd()
return node
}
readXmlAttribute() {
this.debug && console.group('readXmlAttribute')
const attr = {
namespaceURI: null,
nodeType: NodeType.ATTRIBUTE_NODE,
nodeName: null,
name: null,
value: null,
typedValue: null
}
const nsRef = this.readS32()
const nameRef = this.readS32()
const valueRef = this.readS32()
if (nsRef > 0) {
attr.namespaceURI = this.strings[nsRef]
}
attr.nodeName = attr.name = this.strings[nameRef]
if (valueRef > 0) {
attr.value = this.strings[valueRef]
}
attr.typedValue = this.readTypedValue()
this.debug && console.groupEnd()
return attr
}
readXmlElementEnd(/* header */) {
this.debug && console.group('readXmlCData')
/* const line = */ this.readU32()
/* const commentRef = */ this.readU32()
/* const nsRef = */ this.readS32()
/* const nameRef = */ this.readS32()
this.stack.pop()
this.parent = this.stack[this.stack.length - 1]
this.debug && console.groupEnd()
return null
}
readXmlCData(/* header */) {
this.debug && console.group('readXmlCData')
const cdata = {
namespaceURI: null,
nodeType: NodeType.CDATA_SECTION_NODE,
nodeName: '#cdata',
data: null,
typedValue: null
}
/* const line = */ this.readU32()
/* const commentRef = */ this.readU32()
const dataRef = this.readS32()
if (dataRef > 0) {
cdata.data = this.strings[dataRef]
}
cdata.typedValue = this.readTypedValue()
this.parent.childNodes.push(cdata)
this.debug && console.groupEnd()
return cdata
}
readNull(header) {
this.debug && console.group('readNull')
this.cursor += header.chunkSize - header.headerSize
this.debug && console.groupEnd()
return null
}
parse() {
this.debug && console.group('BinaryXmlParser.parse')
const xmlHeader = this.readChunkHeader()
if (xmlHeader.chunkType !== ChunkType.XML) {
throw new Error('Invalid XML header')
}
while (this.cursor < this.buffer.length) {
this.debug && console.group('chunk')
const start = this.cursor
const header = this.readChunkHeader()
switch (header.chunkType) {
case ChunkType.STRING_POOL:
this.readStringPool(header)
break
case ChunkType.XML_RESOURCE_MAP:
this.readResourceMap(header)
break
case ChunkType.XML_START_NAMESPACE:
this.readXmlNamespaceStart(header)
break
case ChunkType.XML_END_NAMESPACE:
this.readXmlNamespaceEnd(header)
break
case ChunkType.XML_START_ELEMENT:
this.readXmlElementStart(header)
break
case ChunkType.XML_END_ELEMENT:
this.readXmlElementEnd(header)
break
case ChunkType.XML_CDATA:
this.readXmlCData(header)
break
case ChunkType.NULL:
this.readNull(header)
break
default:
throw new Error(`Unsupported chunk type '${header.chunkType}'`)
}
// Ensure we consume the whole chunk
const end = start + header.chunkSize
if (this.cursor !== end) {
const diff = end - this.cursor
const type = header.chunkType.toString(16)
debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed \
end of chunk of type 0x${type}. The chunk started at offset ${start} and is \
supposed to end at offset ${end}. Ignoring the rest of the chunk.`)
this.cursor = end
}
this.debug && console.groupEnd()
}
this.debug && console.groupEnd()
return this.document
}
}
module.exports = BinaryXmlParser