Commit 99768940 authored by Skylot's avatar Skylot

core: skip decoding for plain text xml (fix #82)

parent 76a0608a
...@@ -53,6 +53,7 @@ public final class ResourcesLoader { ...@@ -53,6 +53,7 @@ public final class ResourcesLoader {
} }
ZipFile zipFile = null; ZipFile zipFile = null;
InputStream inputStream = null; InputStream inputStream = null;
Object result = null;
try { try {
zipFile = new ZipFile(zipRef.getZipFile()); zipFile = new ZipFile(zipRef.getZipFile());
ZipEntry entry = zipFile.getEntry(zipRef.getEntryName()); ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
...@@ -60,7 +61,7 @@ public final class ResourcesLoader { ...@@ -60,7 +61,7 @@ public final class ResourcesLoader {
throw new IOException("Zip entry not found: " + zipRef); throw new IOException("Zip entry not found: " + zipRef);
} }
inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
return decoder.decode(entry.getSize(), inputStream); result = decoder.decode(entry.getSize(), inputStream);
} catch (Exception e) { } catch (Exception e) {
throw new JadxException("Error decode: " + zipRef.getEntryName(), e); throw new JadxException("Error decode: " + zipRef.getEntryName(), e);
} finally { } finally {
...@@ -75,6 +76,7 @@ public final class ResourcesLoader { ...@@ -75,6 +76,7 @@ public final class ResourcesLoader {
LOG.debug("Error close zip file: {}", zipRef, e); LOG.debug("Error close zip file: {}", zipRef, e);
} }
} }
return result;
} }
static CodeWriter loadContent(final JadxDecompiler jadxRef, final ResourceFile rf) { static CodeWriter loadContent(final JadxDecompiler jadxRef, final ResourceFile rf) {
...@@ -148,7 +150,7 @@ public final class ResourcesLoader { ...@@ -148,7 +150,7 @@ public final class ResourcesLoader {
// LOG.debug("Add resource entry: {}, size: {}", name, entry.getSize()); // LOG.debug("Add resource entry: {}, size: {}", name, entry.getSize());
} }
private static CodeWriter loadToCodeWriter(InputStream is) throws IOException { public static CodeWriter loadToCodeWriter(InputStream is) throws IOException {
CodeWriter cw = new CodeWriter(); CodeWriter cw = new CodeWriter();
ByteArrayOutputStream baos = new ByteArrayOutputStream(READ_BUFFER_SIZE); ByteArrayOutputStream baos = new ByteArrayOutputStream(READ_BUFFER_SIZE);
byte[] buffer = new byte[READ_BUFFER_SIZE]; byte[] buffer = new byte[READ_BUFFER_SIZE];
......
package jadx.core.xmlgen; package jadx.core.xmlgen;
import jadx.api.ResourcesLoader;
import jadx.core.codegen.CodeWriter; import jadx.core.codegen.CodeWriter;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.DexNode; import jadx.core.dex.nodes.DexNode;
...@@ -81,22 +82,30 @@ public class BinaryXMLParser extends CommonBinaryParser { ...@@ -81,22 +82,30 @@ public class BinaryXMLParser extends CommonBinaryParser {
} }
public synchronized CodeWriter parse(InputStream inputStream) throws IOException { public synchronized CodeWriter parse(InputStream inputStream) throws IOException {
is = new ParserStream(inputStream);
if (!isBinaryXml()) {
return ResourcesLoader.loadToCodeWriter(inputStream);
}
writer = new CodeWriter(); writer = new CodeWriter();
writer.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); writer.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
is = new ParserStream(inputStream);
firstElement = true; firstElement = true;
decode(); decode();
writer.finish(); writer.finish();
return writer; return writer;
} }
void decode() throws IOException { private boolean isBinaryXml() throws IOException {
if (is.readInt16() != 0x0003) { is.mark(4);
die("Version is not 3"); int v = is.readInt16(); // version
int h = is.readInt16(); // header size
if (v == 0x0003 && h == 0x0008) {
return true;
} }
if (is.readInt16() != 0x0008) { is.reset();
die("Size of header is not 8"); return false;
} }
void decode() throws IOException {
int size = is.readInt32(); int size = is.readInt32();
while (is.getPos() < size) { while (is.getPos() < size) {
int type = is.readInt16(); int type = is.readInt16();
......
...@@ -132,6 +132,17 @@ public class ParserStream { ...@@ -132,6 +132,17 @@ public class ParserStream {
checkPos(expectedOffset, error); checkPos(expectedOffset, error);
} }
public void mark(int len) throws IOException {
if (!input.markSupported()) {
throw new IOException("Mark not supported for input stream " + input.getClass());
}
input.mark(len);
}
public void reset() throws IOException {
input.reset();
}
@Override @Override
public String toString() { public String toString() {
return "pos: 0x" + Long.toHexString(readPos); return "pos: 0x" + Long.toHexString(readPos);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment