From 62db52fe7bb7de9422e727ae9821728e64bc7af1 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Sun, 5 Oct 2014 12:05:08 -0500 Subject: [PATCH] Handle APKs with NULL padding on StringPools - issue 664 --- .../java/brut/androlib/res/decoder/StringBlock.java | 13 ++++++++----- .../src/main/java/brut/util/ExtDataInput.java | 12 +++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java index 38686d16..8b7c3ec9 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/StringBlock.java @@ -41,10 +41,12 @@ public class StringBlock { * be at the chunk type. */ public static StringBlock read(ExtDataInput reader) throws IOException { - reader.skipCheckInt(CHUNK_TYPE); + reader.skipCheckChunkTypeInt(CHUNK_STRINGPOOL_TYPE, CHUNK_NULL_TYPE); int chunkSize = reader.readInt(); + + // ResStringPool_header int stringCount = reader.readInt(); - int styleOffsetCount = reader.readInt(); + int styleCount = reader.readInt(); int flags = reader.readInt(); int stringsOffset = reader.readInt(); int stylesOffset = reader.readInt(); @@ -55,8 +57,8 @@ public class StringBlock { block.m_stringOwns = new int[stringCount]; Arrays.fill(block.m_stringOwns, -1); - if (styleOffsetCount != 0) { - block.m_styleOffsets = reader.readIntArray(styleOffsetCount); + if (styleCount != 0) { + block.m_styleOffsets = reader.readIntArray(styleCount); } { int size = ((stylesOffset == 0) ? chunkSize : stylesOffset) @@ -343,6 +345,7 @@ public class StringBlock { private static final Logger LOGGER = Logger.getLogger(StringBlock.class.getName()); // ResChunk_header = header.type (0x0001) + header.headerSize (0x001C) - private static final int CHUNK_TYPE = 0x001C0001; + private static final int CHUNK_STRINGPOOL_TYPE = 0x001C0001; + private static final int CHUNK_NULL_TYPE = 0x00000000; private static final int UTF8_FLAG = 0x00000100; } diff --git a/brut.j.util/src/main/java/brut/util/ExtDataInput.java b/brut.j.util/src/main/java/brut/util/ExtDataInput.java index 4b4884ba..4971b8d9 100644 --- a/brut.j.util/src/main/java/brut/util/ExtDataInput.java +++ b/brut.j.util/src/main/java/brut/util/ExtDataInput.java @@ -62,7 +62,17 @@ public class ExtDataInput extends DataInputDelegate { byte got = readByte(); if (got != expected) { throw new IOException(String.format( - "Expected: 0x%08x, got: 0x%08x", expected, got)); + "Expected: 0x%08x, got: 0x%08x", expected, got)); + } + } + + public void skipCheckChunkTypeInt(int expected, int possible) throws IOException { + int got = readInt(); + + if (got == possible) { + skipCheckChunkTypeInt(expected, -1); + } else if (got != expected) { + throw new IOException(String.format("Expected: 0x%08x, got: 0x%08x", expected, got)); } }