Move the map item related methods from RawDexfile to DexBackedDexFile

This commit is contained in:
Ben Gruver 2018-01-30 16:53:18 -08:00
parent e3fd1e8115
commit 88ece20f9f
2 changed files with 29 additions and 34 deletions

View File

@ -39,6 +39,7 @@ import org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference;
import org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference; import org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference;
import org.jf.dexlib2.dexbacked.reference.DexBackedStringReference; import org.jf.dexlib2.dexbacked.reference.DexBackedStringReference;
import org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference; import org.jf.dexlib2.dexbacked.reference.DexBackedTypeReference;
import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.dexbacked.util.FixedSizeSet; import org.jf.dexlib2.dexbacked.util.FixedSizeSet;
import org.jf.dexlib2.iface.DexFile; import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.iface.reference.Reference; import org.jf.dexlib2.iface.reference.Reference;
@ -68,6 +69,7 @@ public class DexBackedDexFile extends BaseDexBuffer implements DexFile {
private final int methodStartOffset; private final int methodStartOffset;
private final int classCount; private final int classCount;
private final int classStartOffset; private final int classStartOffset;
private final int mapOffset;
protected DexBackedDexFile(@Nonnull Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) { protected DexBackedDexFile(@Nonnull Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) {
super(buf, offset); super(buf, offset);
@ -90,6 +92,7 @@ public class DexBackedDexFile extends BaseDexBuffer implements DexFile {
methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET); methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET);
classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET); classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET);
classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET); classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET);
mapOffset = readSmallUint(HeaderItem.MAP_OFFSET);
} }
public DexBackedDexFile(@Nonnull Opcodes opcodes, @Nonnull BaseDexBuffer buf) { public DexBackedDexFile(@Nonnull Opcodes opcodes, @Nonnull BaseDexBuffer buf) {
@ -323,6 +326,32 @@ public class DexBackedDexFile extends BaseDexBuffer implements DexFile {
return new DexReader(this, offset); return new DexReader(this, offset);
} }
public List<MapItem> getMapItems() {
final int mapSize = readSmallUint(mapOffset);
return new FixedSizeList<MapItem>() {
@Override
public MapItem readItem(int index) {
int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE;
return new MapItem(DexBackedDexFile.this, mapItemOffset);
}
@Override public int size() {
return mapSize;
}
};
}
@Nullable
public MapItem getMapItemForSection(int itemType) {
for (MapItem mapItem: getMapItems()) {
if (mapItem.getType() == itemType) {
return mapItem;
}
}
return null;
}
public static class NotADexFile extends RuntimeException { public static class NotADexFile extends RuntimeException {
public NotADexFile() { public NotADexFile() {
} }

View File

@ -34,15 +34,12 @@ package org.jf.dexlib2.dexbacked.raw;
import org.jf.dexlib2.Opcodes; import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.dexbacked.BaseDexBuffer; 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.util.AnnotatedBytes; import org.jf.dexlib2.util.AnnotatedBytes;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
public class RawDexFile extends DexBackedDexFile { public class RawDexFile extends DexBackedDexFile {
@Nonnull public final HeaderItem headerItem; @Nonnull public final HeaderItem headerItem;
@ -62,37 +59,6 @@ public class RawDexFile extends DexBackedDexFile {
return Arrays.copyOfRange(getBuf(), getBaseOffset() + start, getBaseOffset() + start + length); return Arrays.copyOfRange(getBuf(), getBaseOffset() + start, getBaseOffset() + start + length);
} }
public int getMapOffset() {
return headerItem.getMapOffset();
}
@Nullable
public MapItem getMapItemForSection(int itemType) {
for (MapItem mapItem: getMapItems()) {
if (mapItem.getType() == itemType) {
return mapItem;
}
}
return null;
}
public List<MapItem> getMapItems() {
final int mapOffset = getMapOffset();
final int mapSize = readSmallUint(mapOffset);
return new FixedSizeList<MapItem>() {
@Override
public MapItem readItem(int index) {
int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE;
return new MapItem(RawDexFile.this, mapItemOffset);
}
@Override public int size() {
return mapSize;
}
};
}
public void writeAnnotations(@Nonnull Writer out, @Nonnull AnnotatedBytes annotatedBytes) throws IOException { public void writeAnnotations(@Nonnull Writer out, @Nonnull AnnotatedBytes annotatedBytes) throws IOException {
// TODO: need to pass in the offset // TODO: need to pass in the offset
annotatedBytes.writeAnnotations(out, getBuf()); annotatedBytes.writeAnnotations(out, getBuf());