Skip to content

Commit

Permalink
Turns out some string pools can refer to the same string multiple tim…
Browse files Browse the repository at this point in the history
…es. Previously we attempted to load as many individual strings as the pool had items. Now we're using the provided offsets. Fixes #17.
  • Loading branch information
sorccu committed Sep 26, 2018
1 parent 978279d commit 46eea0f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/apkreader/parser/binaryxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Heavily inspired by https://github.com/xiaxiaocao/apk-parser

const assert = require('assert')
const debug = require('debug')('adb:apkreader:parser:binaryxml')

const NodeType = {
Expand Down Expand Up @@ -306,13 +307,13 @@ and is supposed to end at offset ${end}. Ignoring the rest of the value.`)
var stringLength = this.readLength8(encoding)
var byteLength = this.readLength8(encoding)
var value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
this.readU8() // Trailing zero
assert.equal(this.readU8(), 0, 'String must end with trailing zero')
return value
case 'ucs2':
stringLength = this.readLength16(encoding)
byteLength = stringLength * 2
value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
this.readU16() // Trailing zero
assert.equal(this.readU16(), 0, 'String must end with trailing zero')
return value
default:
throw new Error(`Unsupported encoding '${encoding}'`)
Expand All @@ -321,6 +322,7 @@ and is supposed to end at offset ${end}. Ignoring the rest of the value.`)

readChunkHeader() {
return {
startOffset: this.cursor,
chunkType: this.readU16(),
headerSize: this.readU16(),
chunkSize: this.readU32()
Expand All @@ -338,8 +340,6 @@ and is supposed to end at offset ${end}. Ignoring the rest of the value.`)
throw new Error('Invalid string pool header')
}

const anchor = this.cursor

const offsets = []
for (let i = 0, l = header.stringCount; i < l; ++i) {
offsets.push(this.readU32())
Expand All @@ -350,13 +350,15 @@ and is supposed to end at offset ${end}. Ignoring the rest of the value.`)
? 'utf-8'
: 'ucs2'

this.cursor = (anchor + header.stringsStart) - header.headerSize
const stringsStart = header.startOffset + header.stringsStart
this.cursor = stringsStart
for (let i = 0, l = header.stringCount; i < l; ++i) {
this.cursor = stringsStart + offsets[i]
this.strings.push(this.readString(encoding))
}

// Skip styles
this.cursor = (anchor + header.chunkSize) - header.headerSize
this.cursor = header.startOffset + header.chunkSize

return null
}
Expand Down

0 comments on commit 46eea0f

Please sign in to comment.