From 32bf5c9a0172de12c05ccce63e346d6fe6a4fe1f Mon Sep 17 00:00:00 2001 From: alexhu Date: Mon, 30 May 2016 17:14:19 +0800 Subject: [PATCH 1/4] #3 Output NameSpace format --- .../android/content/res/AXMLResource.java | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/src/main/java/android/content/res/AXMLResource.java b/src/main/java/android/content/res/AXMLResource.java index 9d0a126..8b0c5fa 100644 --- a/src/main/java/android/content/res/AXMLResource.java +++ b/src/main/java/android/content/res/AXMLResource.java @@ -29,8 +29,10 @@ import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashSet; +import java.util.List; /** * Main AXMLResource object @@ -162,19 +164,89 @@ public void print() { log("%s", header.toXML(stringSection, resourceSection, 0)); Iterator iterator = chunks.iterator(); int indents = 0; + List namespaceXmlList = new ArrayList(); + + while (iterator.hasNext()) { + Chunk chunk = iterator.next(); + if (chunk.getChunkType() == ChunkType.END_TAG) { + indents--; + } + + if (chunk.getChunkType() == ChunkType.START_NAMESPACE) { + namespaceXmlList.add(chunk.toXML(stringSection, resourceSection, indents)); + } else if (chunk.getChunkType() == ChunkType.END_NAMESPACE) { + // ignore + } else { + if (namespaceXmlList.isEmpty()) { + log("%s", chunk.toXML(stringSection, resourceSection, indents)); + } else { + log("%s", appendNameSpace(chunk.toXML(stringSection, resourceSection, indents), namespaceXmlList)); + namespaceXmlList.clear(); + } + } + + if (chunk.getChunkType() == ChunkType.START_TAG) { + indents++; + } + } + + } + + public String toXML() { + StringBuilder xmlStrbui = new StringBuilder(); + xmlStrbui.append(header.toXML(stringSection, resourceSection, 0)).append('\n'); + Iterator iterator = chunks.iterator(); + int indents = 0; + List namespaceXmlList = new ArrayList(); + while (iterator.hasNext()) { Chunk chunk = iterator.next(); if (chunk.getChunkType() == ChunkType.END_TAG) { indents--; } - // if(chunk.getChunkType() == ChunkType.START_NAMESPACE) - log("%s", chunk.toXML(stringSection, resourceSection, indents)); + if (chunk.getChunkType() == ChunkType.START_NAMESPACE) { + namespaceXmlList.add(chunk.toXML(stringSection, resourceSection, indents)); + } else if (chunk.getChunkType() == ChunkType.END_NAMESPACE) { + // ignore + } else { + if (namespaceXmlList.isEmpty()) { + xmlStrbui.append(chunk.toXML(stringSection, resourceSection, indents)).append('\n'); + } else { + xmlStrbui.append(appendNameSpace(chunk.toXML(stringSection, resourceSection, indents), namespaceXmlList)).append('\n'); + namespaceXmlList.clear(); + } + } if (chunk.getChunkType() == ChunkType.START_TAG) { indents++; } } + return xmlStrbui.toString(); + } + + private String appendNameSpace(String preChunkXml, List namespaceXmlList) { + StringBuilder strbui = new StringBuilder(preChunkXml); + int index = strbui.indexOf("\n"); + if (index == -1) { + index = strbui.indexOf(" "); + if (index == -1) { + index = strbui.lastIndexOf("/>"); + if (index == -1) { + index = strbui.lastIndexOf(">"); + } + } + } + if (index == -1) { + throw new RuntimeException("Append name space fail. chunk xml: " + preChunkXml); + } + StringBuilder namespaceStrbui = new StringBuilder(); + for (String namespaceXml : namespaceXmlList) { + namespaceStrbui.append("\n\t"); + namespaceStrbui.append(namespaceXml); + } + strbui.insert(index, namespaceStrbui.toString()); + return strbui.toString(); } private static void log(String format, Object... arguments) { From 892bf6390750e9ab2198440cfca69238d73b293e Mon Sep 17 00:00:00 2001 From: alexhu Date: Mon, 30 May 2016 17:14:19 +0800 Subject: [PATCH 2/4] #3 Output NameSpace format --- src/test/java/android/content/res/TestAXMLResource.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/android/content/res/TestAXMLResource.java b/src/test/java/android/content/res/TestAXMLResource.java index 0c2eef9..1fd16f9 100644 --- a/src/test/java/android/content/res/TestAXMLResource.java +++ b/src/test/java/android/content/res/TestAXMLResource.java @@ -52,6 +52,15 @@ public void testPrinting() throws IOException { underTest.print(); } + @Test + public void testToXml() throws IOException { + InputStream testStream = this.getClass().getClassLoader().getResourceAsStream(largeFromMalware); + + underTest = new AXMLResource(testStream); + + System.out.println(underTest.toXML());; + } + @Test public void testInsertApplicationAttribute() throws IOException { InputStream testStream = this.getClass().getClassLoader().getResourceAsStream(largeFromMalware); From b7ba2cc0e5334400405a88bc833e4d23fd7848d0 Mon Sep 17 00:00:00 2001 From: alexhu Date: Mon, 30 May 2016 17:14:19 +0800 Subject: [PATCH 3/4] #3 Output NameSpace format --- .../android/content/res/TestAXMLResource.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/test/java/android/content/res/TestAXMLResource.java b/src/test/java/android/content/res/TestAXMLResource.java index 1fd16f9..28fc963 100644 --- a/src/test/java/android/content/res/TestAXMLResource.java +++ b/src/test/java/android/content/res/TestAXMLResource.java @@ -7,7 +7,13 @@ import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.xml.sax.SAXException; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import java.io.*; import static org.junit.Assert.assertEquals; @@ -53,12 +59,24 @@ public void testPrinting() throws IOException { } @Test - public void testToXml() throws IOException { + public void testToXml() throws IOException, ParserConfigurationException { InputStream testStream = this.getClass().getClassLoader().getResourceAsStream(largeFromMalware); underTest = new AXMLResource(testStream); - - System.out.println(underTest.toXML());; + String xml = underTest.toXML(); + + try { + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes())); + Node manifestNode = document.getFirstChild(); + NamedNodeMap manifestNodeAttributes = manifestNode.getAttributes(); + assertEquals("http://schemas.android.com/apk/res/android", manifestNodeAttributes.getNamedItem("xmlns:android").getNodeValue()); + assertEquals("3133", manifestNodeAttributes.getNamedItem("android:versionCode").getNodeValue()); + assertEquals("1.9.3", manifestNodeAttributes.getNamedItem("android:versionName").getNodeValue()); + assertEquals("com.faithcomesbyhearing.android.pt.bibleis", manifestNodeAttributes.getNamedItem("package").getNodeValue()); + } catch (SAXException e) { + // Is not xml + assertTrue(false); + } } @Test From dd51b1ba0d012625813dd9e1e0840b390ea54d3b Mon Sep 17 00:00:00 2001 From: alexhu Date: Mon, 30 May 2016 17:14:19 +0800 Subject: [PATCH 4/4] #3 Output NameSpace format --- .../java/android/content/res/AXMLResource.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/java/android/content/res/AXMLResource.java b/src/main/java/android/content/res/AXMLResource.java index 8b0c5fa..a241ff1 100644 --- a/src/main/java/android/content/res/AXMLResource.java +++ b/src/main/java/android/content/res/AXMLResource.java @@ -227,17 +227,9 @@ public String toXML() { private String appendNameSpace(String preChunkXml, List namespaceXmlList) { StringBuilder strbui = new StringBuilder(preChunkXml); - int index = strbui.indexOf("\n"); - if (index == -1) { - index = strbui.indexOf(" "); - if (index == -1) { - index = strbui.lastIndexOf("/>"); - if (index == -1) { - index = strbui.lastIndexOf(">"); - } - } - } - if (index == -1) { + int index; + if ((index = strbui.indexOf("\n")) == -1 && (index = strbui.indexOf(" ")) == -1 + && (index = strbui.indexOf("/>")) == -1 && (index = strbui.indexOf(">")) == -1) { throw new RuntimeException("Append name space fail. chunk xml: " + preChunkXml); } StringBuilder namespaceStrbui = new StringBuilder();