react-native-update-cli 1.20.0 → 1.20.6

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,499 @@
1
+ /**
2
+ * Code translated from a C# project https://github.com/hylander0/Iteedee.ApkReader/blob/master/Iteedee.ApkReader/ApkResourceFinder.cs
3
+ *
4
+ * Decode binary file `resources.arsc` from a .apk file to a JavaScript Object.
5
+ */
6
+
7
+ var ByteBuffer = require("bytebuffer");
8
+
9
+ var DEBUG = false;
10
+
11
+ var RES_STRING_POOL_TYPE = 0x0001;
12
+ var RES_TABLE_TYPE = 0x0002;
13
+ var RES_TABLE_PACKAGE_TYPE = 0x0200;
14
+ var RES_TABLE_TYPE_TYPE = 0x0201;
15
+ var RES_TABLE_TYPE_SPEC_TYPE = 0x0202;
16
+
17
+ // The 'data' holds a ResTable_ref, a reference to another resource
18
+ // table entry.
19
+ var TYPE_REFERENCE = 0x01;
20
+ // The 'data' holds an index into the containing resource table's
21
+ // global value string pool.
22
+ var TYPE_STRING = 0x03;
23
+
24
+ function ResourceFinder() {
25
+ this.valueStringPool = null;
26
+ this.typeStringPool = null;
27
+ this.keyStringPool = null;
28
+
29
+ this.package_id = 0;
30
+
31
+ this.responseMap = {};
32
+ this.entryMap = {};
33
+ }
34
+
35
+ /**
36
+ * Same to C# BinaryReader.readBytes
37
+ *
38
+ * @param bb ByteBuffer
39
+ * @param len length
40
+ * @returns {Buffer}
41
+ */
42
+ ResourceFinder.readBytes = function(bb, len) {
43
+ var uint8Array = new Uint8Array(len);
44
+ for (var i = 0; i < len; i++) {
45
+ uint8Array[i] = bb.readUint8();
46
+ }
47
+
48
+ return ByteBuffer.wrap(uint8Array, "binary", true);
49
+ };
50
+
51
+ //
52
+ /**
53
+ *
54
+ * @param {ByteBuffer} bb
55
+ * @return {Map<String, Set<String>>}
56
+ */
57
+ ResourceFinder.prototype.processResourceTable = function(resourceBuffer) {
58
+ const bb = ByteBuffer.wrap(resourceBuffer, "binary", true);
59
+
60
+ // Resource table structure
61
+ var type = bb.readShort(),
62
+ headerSize = bb.readShort(),
63
+ size = bb.readInt(),
64
+ packageCount = bb.readInt(),
65
+ buffer,
66
+ bb2;
67
+ if (type != RES_TABLE_TYPE) {
68
+ throw new Error("No RES_TABLE_TYPE found!");
69
+ }
70
+ if (size != bb.limit) {
71
+ throw new Error("The buffer size not matches to the resource table size.");
72
+ }
73
+ bb.offset = headerSize;
74
+
75
+ var realStringPoolCount = 0,
76
+ realPackageCount = 0;
77
+
78
+ while (true) {
79
+ var pos, t, hs, s;
80
+ try {
81
+ pos = bb.offset;
82
+ t = bb.readShort();
83
+ hs = bb.readShort();
84
+ s = bb.readInt();
85
+ } catch (e) {
86
+ break;
87
+ }
88
+ if (t == RES_STRING_POOL_TYPE) {
89
+ // Process the string pool
90
+ if (realStringPoolCount == 0) {
91
+ // Only the first string pool is processed.
92
+ if (DEBUG) {
93
+ console.log("Processing the string pool ...");
94
+ }
95
+
96
+ buffer = new ByteBuffer(s);
97
+ bb.offset = pos;
98
+ bb.prependTo(buffer);
99
+
100
+ bb2 = ByteBuffer.wrap(buffer, "binary", true);
101
+
102
+ bb2.LE();
103
+ this.valueStringPool = this.processStringPool(bb2);
104
+ }
105
+ realStringPoolCount++;
106
+ } else if (t == RES_TABLE_PACKAGE_TYPE) {
107
+ // Process the package
108
+ if (DEBUG) {
109
+ console.log("Processing the package " + realPackageCount + " ...");
110
+ }
111
+
112
+ buffer = new ByteBuffer(s);
113
+ bb.offset = pos;
114
+ bb.prependTo(buffer);
115
+
116
+ bb2 = ByteBuffer.wrap(buffer, "binary", true);
117
+ bb2.LE();
118
+ this.processPackage(bb2);
119
+
120
+ realPackageCount++;
121
+ } else {
122
+ throw new Error("Unsupported type");
123
+ }
124
+ bb.offset = pos + s;
125
+ if (!bb.remaining()) break;
126
+ }
127
+
128
+ if (realStringPoolCount != 1) {
129
+ throw new Error("More than 1 string pool found!");
130
+ }
131
+ if (realPackageCount != packageCount) {
132
+ throw new Error("Real package count not equals the declared count.");
133
+ }
134
+
135
+ return this.responseMap;
136
+ };
137
+
138
+ /**
139
+ *
140
+ * @param {ByteBuffer} bb
141
+ */
142
+ ResourceFinder.prototype.processPackage = function(bb) {
143
+ // Package structure
144
+ var type = bb.readShort(),
145
+ headerSize = bb.readShort(),
146
+ size = bb.readInt(),
147
+ id = bb.readInt();
148
+
149
+ this.package_id = id;
150
+
151
+ for (var i = 0; i < 256; ++i) {
152
+ bb.readUint8();
153
+ }
154
+
155
+ var typeStrings = bb.readInt(),
156
+ lastPublicType = bb.readInt(),
157
+ keyStrings = bb.readInt(),
158
+ lastPublicKey = bb.readInt();
159
+
160
+ if (typeStrings != headerSize) {
161
+ throw new Error(
162
+ "TypeStrings must immediately following the package structure header."
163
+ );
164
+ }
165
+
166
+ if (DEBUG) {
167
+ console.log("Type strings:");
168
+ }
169
+
170
+ var lastPosition = bb.offset;
171
+ bb.offset = typeStrings;
172
+ var bbTypeStrings = ResourceFinder.readBytes(bb, bb.limit - bb.offset);
173
+ bb.offset = lastPosition;
174
+ this.typeStringPool = this.processStringPool(bbTypeStrings);
175
+
176
+ // Key strings
177
+ if (DEBUG) {
178
+ console.log("Key strings:");
179
+ }
180
+
181
+ bb.offset = keyStrings;
182
+ var key_type = bb.readShort(),
183
+ key_headerSize = bb.readShort(),
184
+ key_size = bb.readInt();
185
+
186
+ lastPosition = bb.offset;
187
+ bb.offset = keyStrings;
188
+ var bbKeyStrings = ResourceFinder.readBytes(bb, bb.limit - bb.offset);
189
+ bb.offset = lastPosition;
190
+ this.keyStringPool = this.processStringPool(bbKeyStrings);
191
+
192
+ // Iterate through all chunks
193
+ var typeSpecCount = 0;
194
+ var typeCount = 0;
195
+
196
+ bb.offset = keyStrings + key_size;
197
+
198
+ var bb2;
199
+
200
+ while (true) {
201
+ var pos = bb.offset;
202
+ try {
203
+ var t = bb.readShort();
204
+ var hs = bb.readShort();
205
+ var s = bb.readInt();
206
+ } catch (e) {
207
+ break;
208
+ }
209
+
210
+ if (t == RES_TABLE_TYPE_SPEC_TYPE) {
211
+ bb.offset = pos;
212
+ bb2 = ResourceFinder.readBytes(bb, s);
213
+ this.processTypeSpec(bb2);
214
+
215
+ typeSpecCount++;
216
+ } else if (t == RES_TABLE_TYPE_TYPE) {
217
+ bb.offset = pos;
218
+ bb2 = ResourceFinder.readBytes(bb, s);
219
+ this.processType(bb2);
220
+
221
+ typeCount++;
222
+ }
223
+
224
+ if (s == 0) {
225
+ break;
226
+ }
227
+
228
+ bb.offset = pos + s;
229
+
230
+ if (!bb.remaining()) {
231
+ break;
232
+ }
233
+ }
234
+ };
235
+
236
+ /**
237
+ *
238
+ * @param {ByteBuffer} bb
239
+ */
240
+ ResourceFinder.prototype.processType = function(bb) {
241
+ var type = bb.readShort(),
242
+ headerSize = bb.readShort(),
243
+ size = bb.readInt(),
244
+ id = bb.readByte(),
245
+ res0 = bb.readByte(),
246
+ res1 = bb.readShort(),
247
+ entryCount = bb.readInt(),
248
+ entriesStart = bb.readInt();
249
+
250
+ var refKeys = {};
251
+
252
+ var config_size = bb.readInt();
253
+
254
+ // Skip the config data
255
+ bb.offset = headerSize;
256
+
257
+ if (headerSize + entryCount * 4 != entriesStart) {
258
+ throw new Error("HeaderSize, entryCount and entriesStart are not valid.");
259
+ }
260
+
261
+ // Start to get entry indices
262
+ var entryIndices = new Array(entryCount);
263
+ for (var i = 0; i < entryCount; ++i) {
264
+ entryIndices[i] = bb.readInt();
265
+ }
266
+
267
+ // Get entries
268
+ for (var i = 0; i < entryCount; ++i) {
269
+ if (entryIndices[i] == -1) continue;
270
+
271
+ var resource_id = (this.package_id << 24) | (id << 16) | i;
272
+
273
+ var pos = bb.offset,
274
+ entry_size,
275
+ entry_flag,
276
+ entry_key,
277
+ value_size,
278
+ value_res0,
279
+ value_dataType,
280
+ value_data;
281
+ try {
282
+ entry_size = bb.readShort()
283
+ entry_flag = bb.readShort()
284
+ entry_key = bb.readInt()
285
+ } catch (e) {
286
+ break
287
+ }
288
+
289
+ // Get the value (simple) or map (complex)
290
+
291
+ var FLAG_COMPLEX = 0x0001;
292
+ if ((entry_flag & FLAG_COMPLEX) == 0) {
293
+ // Simple case
294
+ value_size = bb.readShort();
295
+ value_res0 = bb.readByte();
296
+ value_dataType = bb.readByte();
297
+ value_data = bb.readInt();
298
+
299
+ var idStr = Number(resource_id).toString(16);
300
+ var keyStr = this.keyStringPool[entry_key];
301
+
302
+ var data = null;
303
+
304
+ if (DEBUG) {
305
+ console.log(
306
+ "Entry 0x" + idStr + ", key: " + keyStr + ", simple value type: "
307
+ );
308
+ }
309
+
310
+ var key = parseInt(idStr, 16);
311
+
312
+ var entryArr = this.entryMap[key];
313
+ if (entryArr == null) {
314
+ entryArr = [];
315
+ }
316
+ entryArr.push(keyStr);
317
+
318
+ this.entryMap[key] = entryArr;
319
+
320
+ if (value_dataType == TYPE_STRING) {
321
+ data = this.valueStringPool[value_data];
322
+
323
+ if (DEBUG) {
324
+ console.log(", data: " + this.valueStringPool[value_data] + "");
325
+ }
326
+ } else if (value_dataType == TYPE_REFERENCE) {
327
+ var hexIndex = Number(value_data).toString(16);
328
+
329
+ refKeys[idStr] = value_data;
330
+ } else {
331
+ data = "" + value_data;
332
+ if (DEBUG) {
333
+ console.log(", data: " + value_data + "");
334
+ }
335
+ }
336
+
337
+ this.putIntoMap("@" + idStr, data);
338
+ } else {
339
+ // Complex case
340
+ var entry_parent = bb.readInt();
341
+ var entry_count = bb.readInt();
342
+
343
+ for (var j = 0; j < entry_count; ++j) {
344
+ var ref_name = bb.readInt();
345
+ value_size = bb.readShort();
346
+ value_res0 = bb.readByte();
347
+ value_dataType = bb.readByte();
348
+ value_data = bb.readInt();
349
+ }
350
+
351
+ if (DEBUG) {
352
+ console.log(
353
+ "Entry 0x" +
354
+ Number(resource_id).toString(16) +
355
+ ", key: " +
356
+ this.keyStringPool[entry_key] +
357
+ ", complex value, not printed."
358
+ );
359
+ }
360
+ }
361
+ }
362
+
363
+ for (var refK in refKeys) {
364
+ var values = this.responseMap[
365
+ "@" +
366
+ Number(refKeys[refK])
367
+ .toString(16)
368
+ .toUpperCase()
369
+ ];
370
+ if (values != null && Object.keys(values).length < 1000) {
371
+ for (var value in values) {
372
+ this.putIntoMap("@" + refK, values[value]);
373
+ }
374
+ }
375
+ }
376
+ };
377
+
378
+ /**
379
+ *
380
+ * @param {ByteBuffer} bb
381
+ * @return {Array}
382
+ */
383
+ ResourceFinder.prototype.processStringPool = function(bb) {
384
+ // String pool structure
385
+ //
386
+ var type = bb.readShort(),
387
+ headerSize = bb.readShort(),
388
+ size = bb.readInt(),
389
+ stringCount = bb.readInt(),
390
+ styleCount = bb.readInt(),
391
+ flags = bb.readInt(),
392
+ stringsStart = bb.readInt(),
393
+ stylesStart = bb.readInt(),
394
+ u16len,
395
+ buffer;
396
+
397
+ var isUTF_8 = (flags & 256) != 0;
398
+
399
+ var offsets = new Array(stringCount);
400
+ for (var i = 0; i < stringCount; ++i) {
401
+ offsets[i] = bb.readInt();
402
+ }
403
+
404
+ var strings = new Array(stringCount);
405
+
406
+ for (var i = 0; i < stringCount; ++i) {
407
+ var pos = stringsStart + offsets[i];
408
+ bb.offset = pos;
409
+
410
+ strings[i] = "";
411
+
412
+ if (isUTF_8) {
413
+ u16len = bb.readUint8();
414
+
415
+ if ((u16len & 0x80) != 0) {
416
+ u16len = ((u16len & 0x7f) << 8) + bb.readUint8();
417
+ }
418
+
419
+ var u8len = bb.readUint8();
420
+ if ((u8len & 0x80) != 0) {
421
+ u8len = ((u8len & 0x7f) << 8) + bb.readUint8();
422
+ }
423
+
424
+ if (u8len > 0) {
425
+ buffer = ResourceFinder.readBytes(bb, u8len);
426
+ try {
427
+ strings[i] = ByteBuffer.wrap(buffer, "utf8", true).toString("utf8");
428
+ } catch (e) {
429
+ if (DEBUG) {
430
+ console.error(e);
431
+ console.log("Error when turning buffer to utf-8 string.");
432
+ }
433
+ }
434
+ } else {
435
+ strings[i] = "";
436
+ }
437
+ } else {
438
+ u16len = bb.readUint16();
439
+ if ((u16len & 0x8000) != 0) {
440
+ // larger than 32768
441
+ u16len = ((u16len & 0x7fff) << 16) + bb.readUint16();
442
+ }
443
+
444
+ if (u16len > 0) {
445
+ var len = u16len * 2;
446
+ buffer = ResourceFinder.readBytes(bb, len);
447
+ try {
448
+ strings[i] = ByteBuffer.wrap(buffer, "utf8", true).toString("utf8");
449
+ } catch (e) {
450
+ if (DEBUG) {
451
+ console.error(e);
452
+ console.log("Error when turning buffer to utf-8 string.");
453
+ }
454
+ }
455
+ }
456
+ }
457
+
458
+ if (DEBUG) {
459
+ console.log("Parsed value: {0}", strings[i]);
460
+ }
461
+ }
462
+
463
+ return strings;
464
+ };
465
+
466
+ /**
467
+ *
468
+ * @param {ByteBuffer} bb
469
+ */
470
+ ResourceFinder.prototype.processTypeSpec = function(bb) {
471
+ var type = bb.readShort(),
472
+ headerSize = bb.readShort(),
473
+ size = bb.readInt(),
474
+ id = bb.readByte(),
475
+ res0 = bb.readByte(),
476
+ res1 = bb.readShort(),
477
+ entryCount = bb.readInt();
478
+
479
+ if (DEBUG) {
480
+ console.log("Processing type spec " + this.typeStringPool[id - 1] + "...");
481
+ }
482
+
483
+ var flags = new Array(entryCount);
484
+
485
+ for (var i = 0; i < entryCount; ++i) {
486
+ flags[i] = bb.readInt();
487
+ }
488
+ };
489
+
490
+ ResourceFinder.prototype.putIntoMap = function(resId, value) {
491
+ if (this.responseMap[resId.toUpperCase()] == null) {
492
+ this.responseMap[resId.toUpperCase()] = []
493
+ }
494
+ if(value){
495
+ this.responseMap[resId.toUpperCase()].push(value)
496
+ }
497
+ };
498
+
499
+ module.exports = ResourceFinder;
@@ -0,0 +1,167 @@
1
+ function objectType (o) {
2
+ return Object.prototype.toString.call(o).slice(8, -1).toLowerCase()
3
+ }
4
+
5
+ function isArray (o) {
6
+ return objectType(o) === 'array'
7
+ }
8
+
9
+ function isObject (o) {
10
+ return objectType(o) === 'object'
11
+ }
12
+
13
+ function isPrimitive (o) {
14
+ return o === null || ['boolean', 'number', 'string', 'undefined'].includes(objectType(o))
15
+ }
16
+
17
+ function isBrowser () {
18
+ return (
19
+ typeof process === 'undefined' ||
20
+ Object.prototype.toString.call(process) !== '[object process]'
21
+ )
22
+ }
23
+
24
+ /**
25
+ * map file place with resourceMap
26
+ * @param {Object} apkInfo // json info parsed from .apk file
27
+ * @param {Object} resourceMap // resourceMap
28
+ */
29
+ function mapInfoResource (apkInfo, resourceMap) {
30
+ iteratorObj(apkInfo)
31
+ return apkInfo
32
+ function iteratorObj (obj) {
33
+ for (var i in obj) {
34
+ if (isArray(obj[i])) {
35
+ iteratorArray(obj[i])
36
+ } else if (isObject(obj[i])) {
37
+ iteratorObj(obj[i])
38
+ } else if (isPrimitive(obj[i])) {
39
+ if (isResources(obj[i])) {
40
+ obj[i] = resourceMap[transKeyToMatchResourceMap(obj[i])]
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ function iteratorArray (array) {
47
+ const l = array.length
48
+ for (let i = 0; i < l; i++) {
49
+ if (isArray(array[i])) {
50
+ iteratorArray(array[i])
51
+ } else if (isObject(array[i])) {
52
+ iteratorObj(array[i])
53
+ } else if (isPrimitive(array[i])) {
54
+ if (isResources(array[i])) {
55
+ array[i] = resourceMap[transKeyToMatchResourceMap(array[i])]
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ function isResources (attrValue) {
62
+ if (!attrValue) return false
63
+ if (typeof attrValue !== 'string') {
64
+ attrValue = attrValue.toString()
65
+ }
66
+ return attrValue.indexOf('resourceId:') === 0
67
+ }
68
+
69
+ function transKeyToMatchResourceMap (resourceId) {
70
+ return '@' + resourceId.replace('resourceId:0x', '').toUpperCase()
71
+ }
72
+ }
73
+
74
+ /**
75
+ * find .apk file's icon path from json info
76
+ * @param info // json info parsed from .apk file
77
+ */
78
+ function findApkIconPath (info) {
79
+ if (!info.application.icon || !info.application.icon.splice) {
80
+ return ''
81
+ }
82
+ const rulesMap = {
83
+ mdpi: 48,
84
+ hdpi: 72,
85
+ xhdpi: 96,
86
+ xxdpi: 144,
87
+ xxxhdpi: 192
88
+ }
89
+ const resultMap = {}
90
+ const maxDpiIcon = { dpi: 120, icon: '' }
91
+
92
+ for (const i in rulesMap) {
93
+ info.application.icon.some((icon) => {
94
+ if (icon && icon.indexOf(i) !== -1) {
95
+ resultMap['application-icon-' + rulesMap[i]] = icon
96
+ return true
97
+ }
98
+ })
99
+
100
+ // get the maximal size icon
101
+ if (
102
+ resultMap['application-icon-' + rulesMap[i]] &&
103
+ rulesMap[i] >= maxDpiIcon.dpi
104
+ ) {
105
+ maxDpiIcon.dpi = rulesMap[i]
106
+ maxDpiIcon.icon = resultMap['application-icon-' + rulesMap[i]]
107
+ }
108
+ }
109
+
110
+ if (Object.keys(resultMap).length === 0 || !maxDpiIcon.icon) {
111
+ maxDpiIcon.dpi = 120
112
+ maxDpiIcon.icon = info.application.icon[0] || ''
113
+ resultMap['applicataion-icon-120'] = maxDpiIcon.icon
114
+ }
115
+ return maxDpiIcon.icon
116
+ }
117
+
118
+ /**
119
+ * find .ipa file's icon path from json info
120
+ * @param info // json info parsed from .ipa file
121
+ */
122
+ function findIpaIconPath (info) {
123
+ if (
124
+ info.CFBundleIcons &&
125
+ info.CFBundleIcons.CFBundlePrimaryIcon &&
126
+ info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles &&
127
+ info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles.length
128
+ ) {
129
+ return info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles[info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles.length - 1]
130
+ } else if (info.CFBundleIconFiles && info.CFBundleIconFiles.length) {
131
+ return info.CFBundleIconFiles[info.CFBundleIconFiles.length - 1]
132
+ } else {
133
+ return '.app/Icon.png'
134
+ }
135
+ }
136
+
137
+ /**
138
+ * transform buffer to base64
139
+ * @param {Buffer} buffer
140
+ */
141
+ function getBase64FromBuffer (buffer) {
142
+ return 'data:image/png;base64,' + buffer.toString('base64')
143
+ }
144
+
145
+ /**
146
+ * 去除unicode空字符
147
+ * @param {String} str
148
+ */
149
+ function decodeNullUnicode (str) {
150
+ if (typeof str === 'string') {
151
+ // eslint-disable-next-line
152
+ str = str.replace(/\u0000/g, '')
153
+ }
154
+ return str
155
+ }
156
+
157
+ module.exports = {
158
+ isArray,
159
+ isObject,
160
+ isPrimitive,
161
+ isBrowser,
162
+ mapInfoResource,
163
+ findApkIconPath,
164
+ findIpaIconPath,
165
+ getBase64FromBuffer,
166
+ decodeNullUnicode
167
+ }