Skip to content

Commit 959b6de

Browse files
authored
fix: support COMPACT/OFFSET16 packed resources. (#3372)
* fix: support COMPACT/OFFSET16 * fix: properly read specNamesId from compact resources * fix: properly read OFFSET16 in entries * test: add assertions for compact/offset16 sample * refactor: extract flags out of private functions
1 parent 616539f commit 959b6de

File tree

4 files changed

+115
-18
lines changed

4 files changed

+115
-18
lines changed

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ARSCDecoder.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,21 @@ private ResType readTableType() throws IOException, AndrolibException {
280280

281281
mHeader.checkForUnreadHeader(mIn);
282282

283+
boolean isOffset16 = (typeFlags & TABLE_TYPE_FLAG_OFFSET16) != 0;
284+
boolean isSparse = (typeFlags & TABLE_TYPE_FLAG_SPARSE) != 0;
285+
283286
// Be sure we don't poison mResTable by marking the application as sparse
284287
// Only flag the ResTable as sparse if the main package is not loaded.
285-
if ((typeFlags & TABLE_TYPE_FLAG_SPARSE) != 0 && !mResTable.isMainPkgLoaded()) {
288+
if (isSparse && !mResTable.isMainPkgLoaded()) {
286289
mResTable.setSparseResources(true);
287290
}
288291

289-
if ((typeFlags & TABLE_TYPE_FLAG_OFFSET16) != 0) {
290-
LOGGER.warning("Please report this application to Apktool for a fix: https://github.com/iBotPeaches/Apktool/issues/3367");
291-
throw new AndrolibException("Unexpected TYPE_FLAG_OFFSET16");
292-
}
293-
294292
HashMap<Integer, Integer> entryOffsetMap = new LinkedHashMap<>();
295293
for (int i = 0; i < entryCount; i++) {
296-
if ((typeFlags & TABLE_TYPE_FLAG_SPARSE) != 0) {
294+
if (isSparse) {
297295
entryOffsetMap.put(mIn.readUnsignedShort(), mIn.readUnsignedShort());
296+
} else if (isOffset16) {
297+
entryOffsetMap.put(i, mIn.readUnsignedShort());
298298
} else {
299299
entryOffsetMap.put(i, mIn.readInt());
300300
}
@@ -310,11 +310,12 @@ private ResType readTableType() throws IOException, AndrolibException {
310310
}
311311

312312
mType = flags.isInvalid && !mKeepBroken ? null : mPkg.getOrCreateConfig(flags);
313+
int noEntry = isOffset16 ? NO_ENTRY_OFFSET16 : NO_ENTRY;
313314

314315
for (int i : entryOffsetMap.keySet()) {
315316
mResId = (mResId & 0xffff0000) | i;
316317
int offset = entryOffsetMap.get(i);
317-
if (offset == NO_ENTRY) {
318+
if (offset == noEntry) {
318319
mMissingResSpecMap.put(mResId, typeId);
319320
continue;
320321
}
@@ -347,25 +348,35 @@ private ResType readTableType() throws IOException, AndrolibException {
347348

348349
private EntryData readEntryData() throws IOException, AndrolibException {
349350
short size = mIn.readShort();
350-
if (size < 0) {
351-
throw new AndrolibException("Entry size is under 0 bytes.");
351+
short flags = mIn.readShort();
352+
353+
boolean isComplex = (flags & ENTRY_FLAG_COMPLEX) != 0;
354+
boolean isCompact = (flags & ENTRY_FLAG_COMPACT) != 0;
355+
356+
if (size < 0 && !isCompact) {
357+
throw new AndrolibException("Entry size is under 0 bytes and not compactly packed.");
352358
}
353359

354-
short flags = mIn.readShort();
355360
int specNamesId = mIn.readInt();
356-
if (specNamesId == NO_ENTRY) {
361+
if (specNamesId == NO_ENTRY && !isCompact) {
357362
return null;
358363
}
359364

360-
boolean isComplex = (flags & ENTRY_FLAG_COMPLEX) != 0;
361-
boolean isCompact = (flags & ENTRY_FLAG_COMPACT) != 0;
362-
365+
// #3366 - In a compactly packed entry, the key index is the size & type is higher 8 bits on flags.
366+
// We assume a size of 8 bytes for compact entries and the specNamesId is the data itself encoded.
367+
ResValue value;
363368
if (isCompact) {
364-
LOGGER.warning("Please report this application to Apktool for a fix: https://github.com/iBotPeaches/Apktool/issues/3366");
365-
throw new AndrolibException("Unexpected entry type: compact");
369+
byte type = (byte) ((flags >> 8) & 0xFF);
370+
value = readCompactValue(type, specNamesId);
371+
372+
// To keep code below happy - we know if compact that the size has the key index encoded.
373+
specNamesId = size;
374+
} else if (isComplex) {
375+
value = readComplexEntry();
376+
} else {
377+
value = readValue();
366378
}
367379

368-
ResValue value = isComplex ? readComplexEntry() : readValue();
369380
// #2824 - In some applications the res entries are duplicated with the 2nd being malformed.
370381
// AOSP skips this, so we will do the same.
371382
if (value == null) {
@@ -443,6 +454,12 @@ private ResBagValue readComplexEntry() throws IOException, AndrolibException {
443454
return factory.bagFactory(parent, items, mTypeSpec);
444455
}
445456

457+
private ResIntBasedValue readCompactValue(byte type, int data) throws AndrolibException {
458+
return type == TypedValue.TYPE_STRING
459+
? mPkg.getValueFactory().factory(mTableStrings.getHTML(data), data)
460+
: mPkg.getValueFactory().factory(type, data, null);
461+
}
462+
446463
private ResIntBasedValue readValue() throws IOException, AndrolibException {
447464
int size = mIn.readShort();
448465
if (size < 8) {
@@ -686,6 +703,7 @@ private void checkChunkType(int expectedType) throws AndrolibException {
686703
private static final int KNOWN_CONFIG_BYTES = 64;
687704

688705
private static final int NO_ENTRY = 0xFFFFFFFF;
706+
private static final int NO_ENTRY_OFFSET16 = 0xFFFF;
689707

690708
private static final Logger LOGGER = Logger.getLogger(ARSCDecoder.class.getName());
691709
}

brut.apktool/apktool-lib/src/test/java/brut/androlib/BaseTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import brut.directory.FileDirectory;
2323
import org.custommonkey.xmlunit.*;
2424
import org.w3c.dom.Document;
25+
import org.w3c.dom.Element;
2526
import org.xml.sax.SAXException;
2627

2728
import javax.xml.parsers.DocumentBuilder;
@@ -155,6 +156,17 @@ protected static Document loadDocument(File file) throws IOException, SAXExcepti
155156
}
156157
}
157158

159+
protected static int getStringEntryCount(Document doc, String key) {
160+
int count = 0;
161+
Element resources = doc.getDocumentElement();
162+
for (int i = 0; i < resources.getChildNodes().getLength(); i++) {
163+
if (resources.getChildNodes().item(i).getNodeName().equals(key)) {
164+
count++;
165+
}
166+
}
167+
return count;
168+
}
169+
158170
protected static ExtFile sTmpDir;
159171
protected static ExtFile sTestOrigDir;
160172
protected static ExtFile sTestNewDir;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3+
* Copyright (C) 2010 Connor Tumbleson <[email protected]>
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package brut.androlib.decode;
18+
19+
import brut.androlib.*;
20+
import brut.directory.ExtFile;
21+
import brut.common.BrutException;
22+
import brut.util.OS;
23+
import java.io.File;
24+
import java.io.IOException;
25+
26+
import org.junit.*;
27+
import org.w3c.dom.Document;
28+
import org.xml.sax.SAXException;
29+
30+
import javax.xml.parsers.ParserConfigurationException;
31+
32+
import static org.junit.Assert.*;
33+
34+
public class CompactResourceTest extends BaseTest {
35+
36+
@BeforeClass
37+
public static void beforeClass() throws Exception {
38+
TestUtils.cleanFrameworkFile();
39+
sTmpDir = new ExtFile(OS.createTempDirectory());
40+
TestUtils.copyResourceDir(CompactResourceTest.class, "decode/issue3366/", sTmpDir);
41+
}
42+
43+
@AfterClass
44+
public static void afterClass() throws BrutException {
45+
OS.rmdir(sTmpDir);
46+
}
47+
48+
@Test
49+
public void checkIfDecodeSucceeds() throws BrutException, IOException, ParserConfigurationException, SAXException {
50+
String apk = "issue3366.apk";
51+
File testApk = new File(sTmpDir, apk);
52+
53+
// decode issue3366.apk
54+
ApkDecoder apkDecoder = new ApkDecoder(testApk);
55+
sTestOrigDir = new ExtFile(sTmpDir + File.separator + apk + ".out");
56+
57+
File outDir = new File(sTmpDir + File.separator + apk + ".out");
58+
apkDecoder.decode(outDir);
59+
60+
Document doc = loadDocument(new File(sTestOrigDir + "/res/values/strings.xml"));
61+
assertEquals(1002, getStringEntryCount(doc, "string"));
62+
63+
Config config = Config.getDefaultConfig();
64+
LOGGER.info("Building duplicatedex.apk...");
65+
new ApkBuilder(config, sTestOrigDir).build(testApk);
66+
}
67+
}
Binary file not shown.

0 commit comments

Comments
 (0)