diff --git a/baksmali/src/main/java/org/jf/baksmali/dump.java b/baksmali/src/main/java/org/jf/baksmali/dump.java index 99125429..6ea31ddd 100644 --- a/baksmali/src/main/java/org/jf/baksmali/dump.java +++ b/baksmali/src/main/java/org/jf/baksmali/dump.java @@ -29,6 +29,7 @@ package org.jf.baksmali; import org.jf.dexlib2.dexbacked.DexBackedDexFile; +import org.jf.dexlib2.dexbacked.raw.RawDexFile; import org.jf.util.ConsoleUtil; import java.io.FileWriter; @@ -49,7 +50,7 @@ public class dump { consoleWidth = 120; } - dexFile.asRaw().dumpTo(writer, consoleWidth); + new RawDexFile(dexFile).dumpTo(writer, consoleWidth); } catch (IOException ex) { System.err.println("There was an error while dumping the dex file to " + dumpFileName); ex.printStackTrace(System.err); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/BaseDexBuffer.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/BaseDexBuffer.java index 235bd07f..3d5d3231 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/BaseDexBuffer.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/BaseDexBuffer.java @@ -111,4 +111,9 @@ public class BaseDexBuffer { public BaseDexReader readerAt(int offset) { return new BaseDexReader(this, offset); } + + @Nonnull + protected byte[] getBuf() { + return buf; + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java index 78222e26..dd6cf24f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java @@ -58,10 +58,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile @Override @Nonnull public abstract DexReader readerAt(int offset); - @Nonnull public RawDexFile asRaw() { - return new RawDexFile(buf); - } - public static class Impl extends DexBackedDexFile { private final int stringCount; private final int stringStartOffset; @@ -76,6 +72,10 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile private final int classCount; private final int classStartOffset; + public Impl(@Nonnull BaseDexBuffer buf) { + this(buf.buf); + } + public Impl(@Nonnull byte[] buf) { super(buf); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationItem.java index b19eb688..3584be74 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationItem.java @@ -49,7 +49,7 @@ public class AnnotationItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int visibility = dexFile.readUbyte(out.getCursor()); out.annotate(1, "visibility = %d: %s", visibility, getAnnotationVisibility(visibility)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetItem.java index 4c83bf1f..5979cb1a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetItem.java @@ -31,7 +31,6 @@ package org.jf.dexlib2.dexbacked.raw; -import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; import javax.annotation.Nonnull; @@ -48,7 +47,7 @@ public class AnnotationSetItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int size = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "size = %d", size); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetRefList.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetRefList.java index 133cdce3..0cd7a957 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetRefList.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/AnnotationSetRefList.java @@ -31,7 +31,6 @@ package org.jf.dexlib2.dexbacked.raw; -import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; import javax.annotation.Nonnull; @@ -48,7 +47,7 @@ public class AnnotationSetRefList { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int size = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "size = %d", size); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java index 6bbcd42b..f93476d7 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java @@ -33,7 +33,6 @@ package org.jf.dexlib2.dexbacked.raw; import com.google.common.base.Joiner; import org.jf.dexlib2.AccessFlags; -import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; import javax.annotation.Nonnull; @@ -58,7 +57,7 @@ public class ClassDefItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int classIndex = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/FieldIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/FieldIdItem.java index aee06baf..f702f91b 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/FieldIdItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/FieldIdItem.java @@ -51,7 +51,7 @@ public class FieldIdItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int classIndex = dexFile.readUshort(out.getCursor()); out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java index 1ab799f4..a3638ee6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java @@ -146,7 +146,7 @@ public class HeaderItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int startOffset = out.getCursor(); int headerSize; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java index dc91c4ef..e71c9abe 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java @@ -76,7 +76,7 @@ public class MapItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int itemType = dexFile.readUshort(out.getCursor()); out.annotate(2, "type = 0x%x: %s", itemType, ItemType.getItemTypeName(itemType)); @@ -90,7 +90,7 @@ public class MapItem { } @Override - public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemCount) { + public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemCount) { int mapItemCount = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "size = %d", mapItemCount); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MethodIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MethodIdItem.java index ae095711..018904b8 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MethodIdItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MethodIdItem.java @@ -51,7 +51,7 @@ public class MethodIdItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int classIndex = dexFile.readUshort(out.getCursor()); out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java index 0b88025d..3d481f71 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java @@ -51,7 +51,7 @@ public class ProtoIdItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int shortyIndex = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "shorty_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, shortyIndex)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java index e3a101a2..10dc7644 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java @@ -32,6 +32,7 @@ package org.jf.dexlib2.dexbacked.raw; import com.google.common.collect.ImmutableMap; +import org.jf.dexlib2.dexbacked.BaseDexBuffer; import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.util.AnnotatedBytes; @@ -42,15 +43,17 @@ import java.io.Writer; import java.util.List; import java.util.Map; -public class RawDexFile { - @Nonnull private final byte[] buf; - @Nonnull public final DexBackedDexFile dexFile; +public class RawDexFile extends DexBackedDexFile.Impl { @Nonnull public final HeaderItem headerItem; + public RawDexFile(BaseDexBuffer buf) { + super(buf); + this.headerItem = new HeaderItem(this); + } + public RawDexFile(byte[] buf) { - this.buf = buf; - this.dexFile = new DexBackedDexFile.Impl(buf); - this.headerItem = new HeaderItem(dexFile); + super(buf); + this.headerItem = new HeaderItem(this); } public int getMapOffset() { @@ -59,13 +62,13 @@ public class RawDexFile { public List getMapItems() { final int mapOffset = getMapOffset(); - final int mapSize = dexFile.readSmallUint(mapOffset); + final int mapSize = readSmallUint(mapOffset); return new FixedSizeList() { @Override public MapItem readItem(int index) { int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE; - return new MapItem(dexFile, mapItemOffset); + return new MapItem(RawDexFile.this, mapItemOffset); } @Override public int size() { @@ -86,58 +89,58 @@ public class RawDexFile { public void dumpTo(Writer out, int width) throws IOException { AnnotatedBytes annotatedBytes = new AnnotatedBytes(width); - HeaderItem.getAnnotator().annotateSection(annotatedBytes, dexFile, 1); + HeaderItem.getAnnotator().annotateSection(annotatedBytes, this, 1); int stringCount = headerItem.getStringCount(); if (stringCount > 0) { annotatedBytes.skipTo(headerItem.getStringOffset()); annotatedBytes.annotate(0, " "); - StringIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, stringCount); + StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount); } int typeCount = headerItem.getTypeCount(); if (typeCount > 0) { annotatedBytes.skipTo(headerItem.getTypeOffset()); annotatedBytes.annotate(0, " "); - TypeIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, typeCount); + TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount); } int protoCount = headerItem.getProtoCount(); if (protoCount > 0) { annotatedBytes.skipTo(headerItem.getProtoOffset()); annotatedBytes.annotate(0, " "); - ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, protoCount); + ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount); } int fieldCount = headerItem.getFieldCount(); if (fieldCount > 0) { annotatedBytes.skipTo(headerItem.getFieldOffset()); annotatedBytes.annotate(0, " "); - FieldIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, fieldCount); + FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount); } int methodCount = headerItem.getMethodCount(); if (methodCount > 0) { annotatedBytes.skipTo(headerItem.getMethodOffset()); annotatedBytes.annotate(0, " "); - MethodIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, methodCount); + MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount); } int classCount = headerItem.getClassCount(); if (classCount > 0) { annotatedBytes.skipTo(headerItem.getClassOffset()); annotatedBytes.annotate(0, " "); - ClassDefItem.getAnnotator().annotateSection(annotatedBytes, dexFile, classCount); + ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount); } for (MapItem mapItem: getMapItems()) { SectionAnnotator annotator = annotators.get(mapItem.getType()); if (annotator != null) { annotatedBytes.skipTo(mapItem.getOffset()); - annotator.annotateSection(annotatedBytes, dexFile, mapItem.getItemCount()); + annotator.annotateSection(annotatedBytes, this, mapItem.getItemCount()); } } - annotatedBytes.writeAnnotations(out, buf); + annotatedBytes.writeAnnotations(out, getBuf()); } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java index 90f7b3ea..087524e9 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java @@ -31,7 +31,6 @@ package org.jf.dexlib2.dexbacked.raw; -import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.util.AlignmentUtils; @@ -40,7 +39,7 @@ import javax.annotation.Nullable; public abstract class SectionAnnotator { @Nonnull public abstract String getItemName(); - protected abstract void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex); + protected abstract void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex); /** * Write out annotations for this section @@ -49,7 +48,7 @@ public abstract class SectionAnnotator { * @param dexFile The DexBackedDexFile representing the dex file being annotated * @param itemCount The number of items in the section (from the header/map) */ - public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemCount) { + public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemCount) { String itemName = getItemName(); int itemAlignment = getItemAlignment(); if (itemCount > 0) { @@ -74,7 +73,7 @@ public abstract class SectionAnnotator { } } - @Nullable public String getItemIdentity(@Nonnull DexBackedDexFile dexFile, int itemIndex, int itemOffset) { + @Nullable public String getItemIdentity(@Nonnull RawDexFile dexFile, int itemIndex, int itemOffset) { return null; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/StringIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/StringIdItem.java index a4b9ce6d..52e45654 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/StringIdItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/StringIdItem.java @@ -47,7 +47,7 @@ public class StringIdItem { return "string_id_item"; } - @Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, + @Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int stringDataOffset = dexFile.readSmallUint(out.getCursor()); try { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeIdItem.java index d60f3893..e734d858 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeIdItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeIdItem.java @@ -47,7 +47,7 @@ public class TypeIdItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int stringIndex = dexFile.readSmallUint(out.getCursor()); out.annotate(4, StringIdItem.getReferenceAnnotation(dexFile, stringIndex)); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeListItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeListItem.java index c6cb42c7..0c6a9870 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeListItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/TypeListItem.java @@ -48,7 +48,7 @@ public class TypeListItem { } @Override - protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemIndex) { + protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) { int size = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "size: %d", size);