Skip to content

Commit

Permalink
feat: add locate support for apk
Browse files Browse the repository at this point in the history
  • Loading branch information
lianghx-319 committed Jan 11, 2021
1 parent 79c91a6 commit c3da418
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 31,404 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ src/
package-lock.json
yarn.lock
yarn-error.log
dist/
31,121 changes: 0 additions & 31,121 deletions dist/app-info-parser.js

This file was deleted.

259 changes: 0 additions & 259 deletions dist/app-info-parser.js.map

This file was deleted.

2 changes: 0 additions & 2 deletions dist/app-info-parser.min.js

This file was deleted.

1 change: 0 additions & 1 deletion dist/app-info-parser.min.js.map

This file was deleted.

3 changes: 3 additions & 0 deletions example/basic-use.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<span style="width: 0; flex: 1; overflow-wrap: break-word; word-wrap: break-word;">${ result['versionName'] || result['CFBundleShortVersionString'] }</span>
</p>
</div>
<pre>
<code>${JSON.stringify(result, null, 2)}</code>
</pre>
`
document.getElementById('result-list').appendChild(div)
}).catch(e => {
Expand Down
71 changes: 54 additions & 17 deletions lib/resource-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function ResourceFinder() {
* @param len length
* @returns {Buffer}
*/
ResourceFinder.readBytes = function(bb, len) {
ResourceFinder.readBytes = function (bb, len) {
var uint8Array = new Uint8Array(len);
for (var i = 0; i < len; i++) {
uint8Array[i] = bb.readUint8();
Expand All @@ -54,7 +54,7 @@ ResourceFinder.readBytes = function(bb, len) {
* @param {ByteBuffer} bb
* @return {Map<String, Set<String>>}
*/
ResourceFinder.prototype.processResourceTable = function(resourceBuffer) {
ResourceFinder.prototype.processResourceTable = function (resourceBuffer) {
const bb = ByteBuffer.wrap(resourceBuffer, "binary", true);

// Resource table structure
Expand Down Expand Up @@ -139,7 +139,7 @@ ResourceFinder.prototype.processResourceTable = function(resourceBuffer) {
*
* @param {ByteBuffer} bb
*/
ResourceFinder.prototype.processPackage = function(bb) {
ResourceFinder.prototype.processPackage = function (bb) {
// Package structure
var type = bb.readShort(),
headerSize = bb.readShort(),
Expand Down Expand Up @@ -233,11 +233,42 @@ ResourceFinder.prototype.processPackage = function(bb) {
}
};

ResourceFinder.prototype.processConfig = function (bb) {
var config_mcc = bb.readShort(),
config_mnc = bb.readShort(),
config_language = [bb.readByte(), bb.readByte()],
config_region = [bb.readByte(), bb.readByte()]

var config = {
language: '',
region: '',
locate: 'default'
}

if (config_language.every(Boolean)) {
config.language = config_language.map(byte => String.fromCharCode(byte)).join('')
}

if (config_region.every(Boolean)) {
config.region = config_region.map(byte => String.fromCharCode(byte)).join('')
}

if (config.language) {
config.locate = config.language
}

if (config.region) {
config.locate += `-r${config.region}`
}

return config
}

/**
*
* @param {ByteBuffer} bb
*/
ResourceFinder.prototype.processType = function(bb) {
ResourceFinder.prototype.processType = function (bb) {
var type = bb.readShort(),
headerSize = bb.readShort(),
size = bb.readInt(),
Expand All @@ -250,6 +281,9 @@ ResourceFinder.prototype.processType = function(bb) {
var refKeys = {};

var config_size = bb.readInt();
var configBuffer = ResourceFinder.readBytes(bb, config_size)

var res_config = this.processConfig(configBuffer)

// Skip the config data
bb.offset = headerSize;
Expand Down Expand Up @@ -334,7 +368,7 @@ ResourceFinder.prototype.processType = function(bb) {
}
}

this.putIntoMap("@" + idStr, data);
this.putIntoMap("@" + idStr, data, res_config);
} else {
// Complex case
var entry_parent = bb.readInt();
Expand All @@ -351,10 +385,10 @@ ResourceFinder.prototype.processType = function(bb) {
if (DEBUG) {
console.log(
"Entry 0x" +
Number(resource_id).toString(16) +
", key: " +
this.keyStringPool[entry_key] +
", complex value, not printed."
Number(resource_id).toString(16) +
", key: " +
this.keyStringPool[entry_key] +
", complex value, not printed."
);
}
}
Expand All @@ -363,13 +397,13 @@ ResourceFinder.prototype.processType = function(bb) {
for (var refK in refKeys) {
var values = this.responseMap[
"@" +
Number(refKeys[refK])
.toString(16)
.toUpperCase()
Number(refKeys[refK])
.toString(16)
.toUpperCase()
];
if (values != null && Object.keys(values).length < 1000) {
for (var value in values) {
this.putIntoMap("@" + refK, value);
this.putIntoMap("@" + refK, value, res_config);
}
}
}
Expand All @@ -380,7 +414,7 @@ ResourceFinder.prototype.processType = function(bb) {
* @param {ByteBuffer} bb
* @return {Array}
*/
ResourceFinder.prototype.processStringPool = function(bb) {
ResourceFinder.prototype.processStringPool = function (bb) {
// String pool structure
//
var type = bb.readShort(),
Expand Down Expand Up @@ -467,7 +501,7 @@ ResourceFinder.prototype.processStringPool = function(bb) {
*
* @param {ByteBuffer} bb
*/
ResourceFinder.prototype.processTypeSpec = function(bb) {
ResourceFinder.prototype.processTypeSpec = function (bb) {
var type = bb.readShort(),
headerSize = bb.readShort(),
size = bb.readInt(),
Expand All @@ -487,11 +521,14 @@ ResourceFinder.prototype.processTypeSpec = function(bb) {
}
};

ResourceFinder.prototype.putIntoMap = function(resId, value) {
ResourceFinder.prototype.putIntoMap = function (resId, value, config) {
if (this.responseMap[resId.toUpperCase()] == null) {
this.responseMap[resId.toUpperCase()] = []
}
this.responseMap[resId.toUpperCase()].push(value)
this.responseMap[resId.toUpperCase()].push({
value,
locate: config.locate
})
};

module.exports = ResourceFinder;
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function findApkIconPath (info) {
const maxDpiIcon = { dpi: 120, icon: '' }

for (const i in rulesMap) {
info.application.icon.some((icon) => {
info.application.icon.some(({ value: icon }) => {
if (icon && icon.indexOf(i) !== -1) {
resultMap['application-icon-' + rulesMap[i]] = icon
return true
Expand All @@ -109,7 +109,7 @@ function findApkIconPath (info) {

if (Object.keys(resultMap).length === 0 || !maxDpiIcon.icon) {
maxDpiIcon.dpi = 120
maxDpiIcon.icon = info.application.icon[0] || ''
maxDpiIcon.icon = info.application.icon[0].value || ''
resultMap['applicataion-icon-120'] = maxDpiIcon.icon
}
return maxDpiIcon.icon
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "app-info-parser",
"name": "@bytrace/app-info-parser",
"version": "1.0.0",
"description": "Exact info from apk or ipa file.",
"main": "./src/index.js",
"main": "dist/app-info-parser.js",
"files": [
"dist"
],
"scripts": {
"test": "node ./example/node.js",
"build": "rimraf src/* && babel lib -d src",
Expand Down

0 comments on commit c3da418

Please sign in to comment.