Make RawDexFile extends DexBackedDexFile.Impl

This commit is contained in:
Ben Gruver 2013-02-28 22:49:31 -08:00
parent c1d7c834e7
commit cc3be5df1e
17 changed files with 47 additions and 42 deletions

View File

@ -29,6 +29,7 @@
package org.jf.baksmali; package org.jf.baksmali;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.raw.RawDexFile;
import org.jf.util.ConsoleUtil; import org.jf.util.ConsoleUtil;
import java.io.FileWriter; import java.io.FileWriter;
@ -49,7 +50,7 @@ public class dump {
consoleWidth = 120; consoleWidth = 120;
} }
dexFile.asRaw().dumpTo(writer, consoleWidth); new RawDexFile(dexFile).dumpTo(writer, consoleWidth);
} catch (IOException ex) { } catch (IOException ex) {
System.err.println("There was an error while dumping the dex file to " + dumpFileName); System.err.println("There was an error while dumping the dex file to " + dumpFileName);
ex.printStackTrace(System.err); ex.printStackTrace(System.err);

View File

@ -111,4 +111,9 @@ public class BaseDexBuffer {
public BaseDexReader readerAt(int offset) { public BaseDexReader readerAt(int offset) {
return new BaseDexReader<BaseDexBuffer>(this, offset); return new BaseDexReader<BaseDexBuffer>(this, offset);
} }
@Nonnull
protected byte[] getBuf() {
return buf;
}
} }

View File

@ -58,10 +58,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
@Override @Nonnull public abstract DexReader readerAt(int offset); @Override @Nonnull public abstract DexReader readerAt(int offset);
@Nonnull public RawDexFile asRaw() {
return new RawDexFile(buf);
}
public static class Impl extends DexBackedDexFile { public static class Impl extends DexBackedDexFile {
private final int stringCount; private final int stringCount;
private final int stringStartOffset; private final int stringStartOffset;
@ -76,6 +72,10 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile
private final int classCount; private final int classCount;
private final int classStartOffset; private final int classStartOffset;
public Impl(@Nonnull BaseDexBuffer buf) {
this(buf.buf);
}
public Impl(@Nonnull byte[] buf) { public Impl(@Nonnull byte[] buf) {
super(buf); super(buf);

View File

@ -49,7 +49,7 @@ public class AnnotationItem {
} }
@Override @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()); int visibility = dexFile.readUbyte(out.getCursor());
out.annotate(1, "visibility = %d: %s", visibility, getAnnotationVisibility(visibility)); out.annotate(1, "visibility = %d: %s", visibility, getAnnotationVisibility(visibility));

View File

@ -31,7 +31,6 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -48,7 +47,7 @@ public class AnnotationSetItem {
} }
@Override @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()); int size = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "size = %d", size); out.annotate(4, "size = %d", size);

View File

@ -31,7 +31,6 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -48,7 +47,7 @@ public class AnnotationSetRefList {
} }
@Override @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()); int size = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "size = %d", size); out.annotate(4, "size = %d", size);

View File

@ -33,7 +33,6 @@ package org.jf.dexlib2.dexbacked.raw;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -58,7 +57,7 @@ public class ClassDefItem {
} }
@Override @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()); int classIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));

View File

@ -51,7 +51,7 @@ public class FieldIdItem {
} }
@Override @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()); int classIndex = dexFile.readUshort(out.getCursor());
out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));

View File

@ -146,7 +146,7 @@ public class HeaderItem {
} }
@Override @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 startOffset = out.getCursor();
int headerSize; int headerSize;

View File

@ -76,7 +76,7 @@ public class MapItem {
} }
@Override @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()); int itemType = dexFile.readUshort(out.getCursor());
out.annotate(2, "type = 0x%x: %s", itemType, ItemType.getItemTypeName(itemType)); out.annotate(2, "type = 0x%x: %s", itemType, ItemType.getItemTypeName(itemType));
@ -90,7 +90,7 @@ public class MapItem {
} }
@Override @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()); int mapItemCount = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "size = %d", mapItemCount); out.annotate(4, "size = %d", mapItemCount);

View File

@ -51,7 +51,7 @@ public class MethodIdItem {
} }
@Override @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()); int classIndex = dexFile.readUshort(out.getCursor());
out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); out.annotate(2, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex));

View File

@ -51,7 +51,7 @@ public class ProtoIdItem {
} }
@Override @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()); int shortyIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "shorty_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, shortyIndex)); out.annotate(4, "shorty_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, shortyIndex));

View File

@ -32,6 +32,7 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.jf.dexlib2.dexbacked.BaseDexBuffer;
import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
@ -42,15 +43,17 @@ import java.io.Writer;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class RawDexFile { public class RawDexFile extends DexBackedDexFile.Impl {
@Nonnull private final byte[] buf;
@Nonnull public final DexBackedDexFile dexFile;
@Nonnull public final HeaderItem headerItem; @Nonnull public final HeaderItem headerItem;
public RawDexFile(BaseDexBuffer buf) {
super(buf);
this.headerItem = new HeaderItem(this);
}
public RawDexFile(byte[] buf) { public RawDexFile(byte[] buf) {
this.buf = buf; super(buf);
this.dexFile = new DexBackedDexFile.Impl(buf); this.headerItem = new HeaderItem(this);
this.headerItem = new HeaderItem(dexFile);
} }
public int getMapOffset() { public int getMapOffset() {
@ -59,13 +62,13 @@ public class RawDexFile {
public List<MapItem> getMapItems() { public List<MapItem> getMapItems() {
final int mapOffset = getMapOffset(); final int mapOffset = getMapOffset();
final int mapSize = dexFile.readSmallUint(mapOffset); final int mapSize = readSmallUint(mapOffset);
return new FixedSizeList<MapItem>() { return new FixedSizeList<MapItem>() {
@Override @Override
public MapItem readItem(int index) { public MapItem readItem(int index) {
int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE; int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE;
return new MapItem(dexFile, mapItemOffset); return new MapItem(RawDexFile.this, mapItemOffset);
} }
@Override public int size() { @Override public int size() {
@ -86,58 +89,58 @@ public class RawDexFile {
public void dumpTo(Writer out, int width) throws IOException { public void dumpTo(Writer out, int width) throws IOException {
AnnotatedBytes annotatedBytes = new AnnotatedBytes(width); AnnotatedBytes annotatedBytes = new AnnotatedBytes(width);
HeaderItem.getAnnotator().annotateSection(annotatedBytes, dexFile, 1); HeaderItem.getAnnotator().annotateSection(annotatedBytes, this, 1);
int stringCount = headerItem.getStringCount(); int stringCount = headerItem.getStringCount();
if (stringCount > 0) { if (stringCount > 0) {
annotatedBytes.skipTo(headerItem.getStringOffset()); annotatedBytes.skipTo(headerItem.getStringOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
StringIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, stringCount); StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount);
} }
int typeCount = headerItem.getTypeCount(); int typeCount = headerItem.getTypeCount();
if (typeCount > 0) { if (typeCount > 0) {
annotatedBytes.skipTo(headerItem.getTypeOffset()); annotatedBytes.skipTo(headerItem.getTypeOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
TypeIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, typeCount); TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount);
} }
int protoCount = headerItem.getProtoCount(); int protoCount = headerItem.getProtoCount();
if (protoCount > 0) { if (protoCount > 0) {
annotatedBytes.skipTo(headerItem.getProtoOffset()); annotatedBytes.skipTo(headerItem.getProtoOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, protoCount); ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount);
} }
int fieldCount = headerItem.getFieldCount(); int fieldCount = headerItem.getFieldCount();
if (fieldCount > 0) { if (fieldCount > 0) {
annotatedBytes.skipTo(headerItem.getFieldOffset()); annotatedBytes.skipTo(headerItem.getFieldOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, fieldCount); FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount);
} }
int methodCount = headerItem.getMethodCount(); int methodCount = headerItem.getMethodCount();
if (methodCount > 0) { if (methodCount > 0) {
annotatedBytes.skipTo(headerItem.getMethodOffset()); annotatedBytes.skipTo(headerItem.getMethodOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
MethodIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, methodCount); MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount);
} }
int classCount = headerItem.getClassCount(); int classCount = headerItem.getClassCount();
if (classCount > 0) { if (classCount > 0) {
annotatedBytes.skipTo(headerItem.getClassOffset()); annotatedBytes.skipTo(headerItem.getClassOffset());
annotatedBytes.annotate(0, " "); annotatedBytes.annotate(0, " ");
ClassDefItem.getAnnotator().annotateSection(annotatedBytes, dexFile, classCount); ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount);
} }
for (MapItem mapItem: getMapItems()) { for (MapItem mapItem: getMapItems()) {
SectionAnnotator annotator = annotators.get(mapItem.getType()); SectionAnnotator annotator = annotators.get(mapItem.getType());
if (annotator != null) { if (annotator != null) {
annotatedBytes.skipTo(mapItem.getOffset()); annotatedBytes.skipTo(mapItem.getOffset());
annotator.annotateSection(annotatedBytes, dexFile, mapItem.getItemCount()); annotator.annotateSection(annotatedBytes, this, mapItem.getItemCount());
} }
} }
annotatedBytes.writeAnnotations(out, buf); annotatedBytes.writeAnnotations(out, getBuf());
} }
} }

View File

@ -31,7 +31,6 @@
package org.jf.dexlib2.dexbacked.raw; package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import org.jf.util.AlignmentUtils; import org.jf.util.AlignmentUtils;
@ -40,7 +39,7 @@ import javax.annotation.Nullable;
public abstract class SectionAnnotator { public abstract class SectionAnnotator {
@Nonnull public abstract String getItemName(); @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 * 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 dexFile The DexBackedDexFile representing the dex file being annotated
* @param itemCount The number of items in the section (from the header/map) * @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(); String itemName = getItemName();
int itemAlignment = getItemAlignment(); int itemAlignment = getItemAlignment();
if (itemCount > 0) { 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; return null;
} }

View File

@ -47,7 +47,7 @@ public class StringIdItem {
return "string_id_item"; 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 itemIndex) {
int stringDataOffset = dexFile.readSmallUint(out.getCursor()); int stringDataOffset = dexFile.readSmallUint(out.getCursor());
try { try {

View File

@ -47,7 +47,7 @@ public class TypeIdItem {
} }
@Override @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()); int stringIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, StringIdItem.getReferenceAnnotation(dexFile, stringIndex)); out.annotate(4, StringIdItem.getReferenceAnnotation(dexFile, stringIndex));
} }

View File

@ -48,7 +48,7 @@ public class TypeListItem {
} }
@Override @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()); int size = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "size: %d", size); out.annotate(4, "size: %d", size);