-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.js
executable file
·579 lines (480 loc) · 17 KB
/
resource.js
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
var Buffer = require('buffer').Buffer;
var SeekOrigin = {
'Begin': 0
};
var Debug = {
'WriteLine': function (msg) {
// console.log(msg);
}
};
var ToX4String = function (num) {
return ('00000000' + num.toString(16)).substr(-8).toUpperCase();
};
function ToString (obj,style) {
if ( style == "X4" ) {
//return this.toString();
return ToX4String(obj);
}
return obj.toString();
}
function ContainsKey (obj,key) {
return obj[key] !== undefined;
};
function Add (obj,key, value) {
obj[key] = value;
};
String.prototype.ToUpper = function () {
return this.toUpperCase();
};
var System = {
'Globalization': {
'NumberStyles': {
'HexNumber': 16
}
}
};
var int = {
'Parse': function (str, style) {
if ( style == System.Globalization.NumberStyles.HexNumber ) {
return parseInt('0x' + str);
}
return parseInt(str);
}
};
var Encoding = {
'UTF8': {
'GetString': function (buffer) {
return buffer.toString('utf-8');
}
}
};
Array.prototype.Add = function (item) {
return this.push(item);
};
var BufferReader = function (buffer) {
this._buffer = buffer;
this.BaseStream = {};
this.BaseStream.Length = buffer.length;
this.BaseStream.Position = 0;
this.BaseStream.Seek = function (offset, start) {
this.Position = start + offset;
};
};
BufferReader.prototype.ReadInt16 = function () {
var temp = this._buffer.readInt16LE(this.BaseStream.Position, true);
this.BaseStream.Position += 2;
return temp;
};
BufferReader.prototype.ReadInt32 = function () {
var temp = this._buffer.readInt32LE(this.BaseStream.Position, true);
this.BaseStream.Position += 4;
return temp;
};
BufferReader.prototype.ReadBytes = function (length) {
var temp = this._buffer.slice(this.BaseStream.Position, this.BaseStream.Position + length);
this.BaseStream.Position += length;
return temp;
};
BufferReader.prototype.ReadChar = function () {
var charCode = this.ReadByte(true);
if (charCode) {
var ch = String.fromCharCode(charCode);
return ch;
} else {
return 0;
}
};
BufferReader.prototype.ReadByte = function () {
var temp = this._buffer.readInt8(this.BaseStream.Position, true);
this.BaseStream.Position += 1;
return temp;
};
var HEADER_START = 0;
var RES_NULL_TYPE = 0x0000;
var RES_STRING_POOL_TYPE = 0x0001;
var RES_TABLE_TYPE = 0x0002;
var RES_TABLE_PACKAGE_TYPE = 0x0200;
var RES_TABLE_TYPE_TYPE = 0x0201;
var RES_TABLE_TYPE_SPEC_TYPE = 0x0202;
var valueStringPool = null;
var typeStringPool = null;
var keyStringPool = null;
var package_id = 0;
var TYPE_REFERENCE = 0x01;
// The 'data' holds an index into the containing resource table's
// global value string pool.
var TYPE_STRING = 0x03;
var responseMap = {};
var entryMap = {};
var ApkResourceFinder = {
getResourceTable: function(buffer) {
//byte[] data = System.IO.File.ReadAllBytes("resources.arsc");
responseMap = {};
entryMap = {};
this.resIdList = [];
package_id = 0;
valueStringPool = null;
typeStringPool = null;
keyStringPool = null;
return this.processResourceTable(buffer);
},
processResourceTable: function(buffer) {
var lastPosition = 0;
var br = new BufferReader(buffer);
var type = br.ReadInt16();
var headerSize = br.ReadInt16();
var size = br.ReadInt32();
var packageCount = br.ReadInt32();
if (type !== RES_TABLE_TYPE)
{
throw new Error("No RES_TABLE_TYPE found!");
}
if (size !== br.BaseStream.Length)
{
throw new Error(
"The buffer size not matches to the resource table size.");
}
var realStringPoolCount = 0;
var realPackageCount = 0;
while (true)
{
var pos = br.BaseStream.Position;
var t = br.ReadInt16();
var hs = br.ReadInt16();
var s = br.ReadInt32();
if (t == RES_STRING_POOL_TYPE)
{
if (realStringPoolCount == 0)
{
// Only the first string pool is processed.
//Debug.WriteLine("Processing the string pool ...");
Debug.WriteLine("Processing the string pool ...");
//byte[] buffer = new byte[s];
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(pos, SeekOrigin.Begin);
var buffer = br.ReadBytes(s);
//br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
valueStringPool = this.processStringPool(buffer);
}
realStringPoolCount++;
}
else if (t == RES_TABLE_PACKAGE_TYPE)
{
// Process the package
//Debug.WriteLine("Processing package {0} ...", realPackageCount);
Debug.WriteLine("Processing package " + realPackageCount + " ...");
//byte[] buffer = new byte[s];
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(pos, SeekOrigin.Begin);
var buffer = br.ReadBytes(s);
//br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
this.processPackage(buffer);
realPackageCount++;
}
else if (t == RES_NULL_TYPE)
{
continue;
}
else
{
throw new Error("Unsupported Type");
}
br.BaseStream.Seek(pos + s, SeekOrigin.Begin);
if (br.BaseStream.Position == br.BaseStream.Length)
break;
}
if (realStringPoolCount !== 1)
{
throw new Error("More than 1 string pool found!");
}
if (realPackageCount !== packageCount)
{
throw new Error(
"Real package count not equals the declared count.");
}
return responseMap;
},
processPackage: function (buffer) {
var lastPosition = 0;
var br = new BufferReader(buffer);
//HEADER
var type = br.ReadInt16();
var headerSize = br.ReadInt16();
var size = br.ReadInt32();
var id = br.ReadInt32();
package_id = id;
//PackageName
var name = [];
for (var i = 0; i < 256; ++i)
{
name[i] = br.ReadChar();
}
var typeStrings = br.ReadInt32();
var lastPublicType = br.ReadInt32();
var keyStrings = br.ReadInt32();
var lastPublicKey = br.ReadInt32();
if (typeStrings !== headerSize)
{
throw new Error("TypeStrings must immediately follow the package structure header.");
}
//Debug.WriteLine("Type strings:");
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(typeStrings, SeekOrigin.Begin);
var bbTypeStrings = br.ReadBytes(br.BaseStream.Length - br.BaseStream.Position);
br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
typeStringPool = this.processStringPool(bbTypeStrings);
Debug.WriteLine("Key strings:");
br.BaseStream.Seek(keyStrings, SeekOrigin.Begin);
var key_type = br.ReadInt16();
var key_headerSize = br.ReadInt16();
var key_size = br.ReadInt32();
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(keyStrings, SeekOrigin.Begin);
var bbKeyStrings = br.ReadBytes(br.BaseStream.Length - br.BaseStream.Position);
br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
keyStringPool = this.processStringPool(bbKeyStrings);
// Iterate through all chunks
//
var typeSpecCount = 0;
var typeCount = 0;
br.BaseStream.Seek((keyStrings + key_size), SeekOrigin.Begin);
while (true)
{
var pos = br.BaseStream.Position;
var t = br.ReadInt16();
var hs = br.ReadInt16();
var s = br.ReadInt32();
if (t == RES_TABLE_TYPE_SPEC_TYPE)
{
// Process the string pool
br.BaseStream.Seek(pos, SeekOrigin.Begin);
var buffer = br.ReadBytes(s);
this.processTypeSpec(buffer);
typeSpecCount++;
}
else if (t == RES_TABLE_TYPE_TYPE)
{
// Process the package
br.BaseStream.Seek(pos, SeekOrigin.Begin);
var buffer = br.ReadBytes(s);
this.processType(buffer);
typeCount++;
}
br.BaseStream.Seek(pos + s, SeekOrigin.Begin);
if (br.BaseStream.Position == br.BaseStream.Length)
break;
}
return;
},
putIntoMap: function (resId, value) {
var valueList = null;
if (ContainsKey(responseMap,resId.ToUpper()))
valueList = responseMap[resId.ToUpper()];
if (valueList == null)
{
valueList = [];
}
valueList.Add(value);
if (ContainsKey(responseMap,resId.ToUpper()))
responseMap[resId.ToUpper()] = valueList;
else
Add(responseMap,resId.ToUpper(), valueList);
return;
},
processType: function (typeData) {
var br = new BufferReader(typeData);
var type = br.ReadInt16();
var headerSize = br.ReadInt16();
var size = br.ReadInt32();
var id = br.ReadByte();
var res0 = br.ReadByte();
var res1 = br.ReadInt16();
var entryCount = br.ReadInt32();
var entriesStart = br.ReadInt32();
//Dictionary<String, int> refKeys = new Dictionary<String, int>();
var refKeys = {};
var config_size = br.ReadInt32();
// Skip the config data
br.BaseStream.Seek(headerSize, SeekOrigin.Begin);
if (headerSize + entryCount * 4 !== entriesStart)
{
throw new Error("HeaderSize, entryCount and entriesStart are not valid.");
}
// Start to get entry indices
var entryIndices = [];
for (var i = 0; i < entryCount; ++i)
{
entryIndices[i] = br.ReadInt32();
}
// Get entries
for (var i = 0; i < entryCount; ++i)
{
if (entryIndices[i] == -1)
continue;
var resource_id = (package_id << 24) | (id << 16) | i;
var pos = br.BaseStream.Position;
var entry_size = br.ReadInt16();
var entry_flag = br.ReadInt16();
var entry_key = br.ReadInt32();
// Get the value (simple) or map (complex)
var FLAG_COMPLEX = 0x0001;
if ((entry_flag & FLAG_COMPLEX) == 0)
{
// Simple case
var value_size = br.ReadInt16();
var value_res0 = br.ReadByte();
var value_dataType = br.ReadByte();
var value_data = br.ReadInt32();
//String idStr = resource_id.ToString("X4");
var idStr = ToString(resource_id,"X4");
var keyStr = keyStringPool[entry_key];
var data = null;
Debug.WriteLine("Entry 0x" + idStr + ", key: " + keyStr + ", simple value type: ");
var entryArr = null;
if (ContainsKey(entryMap,int.Parse(idStr, System.Globalization.NumberStyles.HexNumber)))
entryArr = entryMap[int.Parse(idStr, System.Globalization.NumberStyles.HexNumber)];
if (entryArr == null)
entryArr = [];
entryArr.Add(keyStr);
if (ContainsKey(entryMap,int.Parse(idStr, System.Globalization.NumberStyles.HexNumber)))
entryMap[int.Parse(idStr, System.Globalization.NumberStyles.HexNumber)] = entryArr;
else
Add(entryMap,int.Parse(idStr, System.Globalization.NumberStyles.HexNumber), entryArr);
if (value_dataType == TYPE_STRING)
{
data = valueStringPool[value_data];
Debug.WriteLine(", data: " + valueStringPool[value_data] + "");
}
else if (value_dataType == TYPE_REFERENCE)
{
var hexIndex = ToString(value_data,"X4");
Add(refKeys,idStr, value_data);
}
else
{
data = ToString(value_data);
Debug.WriteLine(", data: " + value_data + "");
}
this.putIntoMap("@" + idStr, data);
} else {
var entry_parent = br.ReadInt32();
var entry_count = br.ReadInt32();
for (var j = 0; j < entry_count; ++j)
{
var ref_name = br.ReadInt32();
var value_size = br.ReadInt16();
var value_res0 = br.ReadByte();
var value_dataType = br.ReadByte();
var value_data = br.ReadInt32();
}
Debug.WriteLine("Entry 0x"
+ ToString(resource_id,"X4") + ", key: "
+ keyStringPool[entry_key]
+ ", complex value, not printed.");
}
}
//HashSet<String> refKs = new HashSet<String>(refKeys.Keys);
var refKs = refKeys;// Object.keys(refKeys);
for(var refK in refKs) {
var values = null;
if (ContainsKey(responseMap,"@" + ToString(refKeys[refK],"X4").ToUpper()))
values = responseMap["@" + ToString(refKeys[refK],"X4").ToUpper()];
if (values !== null)
for (var value in values) {
this.putIntoMap("@" + refK, value);
}
}
return;
},
processStringPool: function (data) {
var lastPosition = 0;
var br = new BufferReader(data);
var type = br.ReadInt16();
var headerSize = br.ReadInt16();
var size = br.ReadInt32();
var stringCount = br.ReadInt32();
var styleCount = br.ReadInt32();
var flags = br.ReadInt32();
var stringsStart = br.ReadInt32();
var stylesStart = br.ReadInt32();
var isUTF_8 = (flags & 256) !== 0;
var offsets = [];
for (var i = 0; i < stringCount; ++i)
{
offsets[i] = br.ReadInt32();
}
var strings = [];
for (var i = 0; i < stringCount; i++)
{
var pos = stringsStart + offsets[i];
lastPosition = br.BaseStream.Position;
var len = br.BaseStream.Seek(pos, SeekOrigin.Begin);
br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
if (len < 0)
{
var extendShort = br.ReadInt16();
}
pos += 2;
strings[i] = "";
if (isUTF_8)
{
var start = pos;
var length = 0;
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(pos, SeekOrigin.Begin);
while (br.ReadByte() !== 0)
{
length++;
pos++;
}
br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
var oneData = new Buffer(length);
if (length > 0)
{
var byteArray = data;
for (var k = 0; k < length; k++)
{
oneData[k] = byteArray[start + k];
}
}
if (oneData.length > 0)
strings[i] = Encoding.UTF8.GetString(oneData);
else
strings[i] = "";
}
else
{
var c;
lastPosition = br.BaseStream.Position;
br.BaseStream.Seek(pos, SeekOrigin.Begin);
while ((c = br.ReadChar()) !== 0)
{
strings[i] += c;
br.ReadChar();
pos += 2;
}
br.BaseStream.Seek(lastPosition, SeekOrigin.Begin);
}
Debug.WriteLine("Parsed value: " + strings[i]);
}
return strings;
},
processTypeSpec: function (data) {
var br = new BufferReader(data);
var type = br.ReadInt16();
var headerSize = br.ReadInt16();
var size = br.ReadInt32();
var id = br.ReadByte();
var res0 = br.ReadByte();
var res1 = br.ReadInt16();
var entryCount = br.ReadInt32();
Debug.WriteLine("Processing type spec " + typeStringPool[id - 1]);
var flags = [];
for (var i = 0; i < entryCount; ++i);
{
flags[i] = br.ReadInt32();
}
return;
}
};
module.exports = ApkResourceFinder;