Rename DexFileBuffer and DexFileReader to DexBuffer and DexReader

This commit is contained in:
Ben Gruver 2012-10-20 23:29:26 -07:00
parent 06eb83505b
commit 36e2ee2005
24 changed files with 491 additions and 491 deletions

View File

@ -39,17 +39,17 @@ import javax.annotation.Nonnull;
import java.util.List;
public class DexBackedAnnotation implements Annotation {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
public final int visibility;
@Nonnull public final String type;
private final int elementsOffset;
public DexBackedAnnotation(@Nonnull DexFileBuffer dexFile,
public DexBackedAnnotation(@Nonnull DexBuffer dexBuf,
int annotationOffset) {
this.dexFile = dexFile;
this.dexBuf = dexBuf;
DexFileReader reader = dexFile.readerAt(annotationOffset);
DexReader reader = dexBuf.readerAt(annotationOffset);
this.visibility = reader.readUbyte();
this.type = reader.getType(reader.readSmallUleb128());
this.elementsOffset = reader.getOffset();
@ -61,14 +61,14 @@ public class DexBackedAnnotation implements Annotation {
@Nonnull
@Override
public List<? extends AnnotationElement> getElements() {
DexFileReader reader = dexFile.readerAt(elementsOffset);
DexReader reader = dexBuf.readerAt(elementsOffset);
final int size = reader.readSmallUleb128();
return new VariableSizeList<AnnotationElement>(dexFile, reader.getOffset()) {
return new VariableSizeList<AnnotationElement>(dexBuf, reader.getOffset()) {
@Nonnull
@Override
protected AnnotationElement readItem(DexFileReader dexFileReader, int index) {
return new DexBackedAnnotationElement(dexFileReader);
protected AnnotationElement readItem(DexReader reader, int index) {
return new DexBackedAnnotationElement(reader);
}
@Override public int size() { return size;}

View File

@ -41,9 +41,9 @@ public class DexBackedAnnotationElement implements AnnotationElement {
@Nonnull public final String name;
@Nonnull public final EncodedValue value;
public DexBackedAnnotationElement(DexFileReader dexFileReader) {
this.name = dexFileReader.getString(dexFileReader.readSmallUleb128());
this.value = DexBackedEncodedValue.readFrom(dexFileReader);
public DexBackedAnnotationElement(DexReader reader) {
this.name = reader.getString(reader.readSmallUleb128());
this.value = DexBackedEncodedValue.readFrom(reader);
}
@Nonnull @Override public String getName() { return name; }

View File

@ -37,9 +37,9 @@ import org.jf.dexlib2.immutable.ImmutableExceptionHandler;
import javax.annotation.Nonnull;
public class DexBackedCatchAllExceptionHandler extends ImmutableExceptionHandler {
public DexBackedCatchAllExceptionHandler(@Nonnull DexFileReader dexFileReader,
public DexBackedCatchAllExceptionHandler(@Nonnull DexReader reader,
@Nonnull InstructionOffsetMap instructionOffsetMap) {
super(null,
instructionOffsetMap.getInstructionIndexAtOffsetExact(dexFileReader.readSmallUleb128()));
instructionOffsetMap.getInstructionIndexAtOffsetExact(reader.readSmallUleb128()));
}
}

View File

@ -41,7 +41,7 @@ import javax.annotation.Nullable;
import java.util.List;
public class DexBackedClassDef implements ClassDef {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
@Nonnull public final String name;
public final int accessFlags;
@ -63,21 +63,21 @@ public class DexBackedClassDef implements ClassDef {
private static final int CLASS_DATA_OFFSET = 24;
private static final int STATIC_INITIAL_VALUES_OFFSET = 28;
public DexBackedClassDef(@Nonnull DexFileBuffer dexFile,
public DexBackedClassDef(@Nonnull DexBuffer dexBuf,
int classDefOffset) {
this.dexFile = dexFile;
this.dexBuf = dexBuf;
this.name = dexFile.getType(dexFile.readSmallUint(classDefOffset));
this.accessFlags = dexFile.readSmallUint(classDefOffset + ACCESS_FLAGS_OFFSET);
this.superclass = dexFile.getOptionalString(dexFile.readSmallUint(classDefOffset + SUPERCLASS_OFFSET));
this.interfacesOffset = dexFile.readSmallUint(classDefOffset + INTERFACES_OFFSET);
this.sourceFile = dexFile.getOptionalString(dexFile.readSmallUint(classDefOffset + SOURCE_FILE_OFFSET));
this.name = dexBuf.getType(dexBuf.readSmallUint(classDefOffset));
this.accessFlags = dexBuf.readSmallUint(classDefOffset + ACCESS_FLAGS_OFFSET);
this.superclass = dexBuf.getOptionalString(dexBuf.readSmallUint(classDefOffset + SUPERCLASS_OFFSET));
this.interfacesOffset = dexBuf.readSmallUint(classDefOffset + INTERFACES_OFFSET);
this.sourceFile = dexBuf.getOptionalString(dexBuf.readSmallUint(classDefOffset + SOURCE_FILE_OFFSET));
int annotationsDirectoryOffset = dexFile.readSmallUint(classDefOffset + ANNOTATIONS_OFFSET);
this.annotationsDirectory = AnnotationsDirectory.newOrEmpty(dexFile, annotationsDirectoryOffset);
int annotationsDirectoryOffset = dexBuf.readSmallUint(classDefOffset + ANNOTATIONS_OFFSET);
this.annotationsDirectory = AnnotationsDirectory.newOrEmpty(dexBuf, annotationsDirectoryOffset);
this.classDataOffset = dexFile.readSmallUint(CLASS_DATA_OFFSET);
this.staticInitialValuesOffset = dexFile.readSmallUint(classDefOffset + STATIC_INITIAL_VALUES_OFFSET);
this.classDataOffset = dexBuf.readSmallUint(CLASS_DATA_OFFSET);
this.staticInitialValuesOffset = dexBuf.readSmallUint(classDefOffset + STATIC_INITIAL_VALUES_OFFSET);
}
@ -90,11 +90,11 @@ public class DexBackedClassDef implements ClassDef {
@Override
public List<String> getInterfaces() {
if (interfacesOffset > 0) {
final int size = dexFile.readSmallUint(interfacesOffset);
final int size = dexBuf.readSmallUint(interfacesOffset);
return new FixedSizeList<String>() {
@Override
public String readItem(int index) {
return dexFile.getString(dexFile.readSmallUint(interfacesOffset + 4 + (2*index)));
return dexBuf.getString(dexBuf.readSmallUint(interfacesOffset + 4 + (2*index)));
}
@Override public int size() { return size; }
@ -113,7 +113,7 @@ public class DexBackedClassDef implements ClassDef {
@Override
public List<? extends DexBackedField> getFields() {
if (classDataOffset != 0) {
DexFileReader reader = dexFile.readerAt(classDataOffset);
DexReader reader = dexBuf.readerAt(classDataOffset);
int staticFieldCount = reader.readSmallUleb128();
int instanceFieldCount = reader.readSmallUleb128();
final int fieldCount = staticFieldCount + instanceFieldCount;
@ -127,16 +127,16 @@ public class DexBackedClassDef implements ClassDef {
@Nonnull
@Override
public Iterator listIterator() {
return new Iterator(dexFile, fieldsStartOffset) {
return new Iterator(dexBuf, fieldsStartOffset) {
private int previousFieldIndex = 0;
@Nonnull private final AnnotationsDirectory.AnnotationIterator annotationIterator =
annotationsDirectory.getFieldAnnotationIterator();
@Nonnull private final StaticInitialValueIterator staticInitialValueIterator =
StaticInitialValueIterator.newOrEmpty(dexFile, staticInitialValuesOffset);
StaticInitialValueIterator.newOrEmpty(dexBuf, staticInitialValuesOffset);
@Nonnull
@Override
protected DexBackedField readItem(DexFileReader reader, int index) {
protected DexBackedField readItem(DexReader reader, int index) {
DexBackedField item = new DexBackedField(reader, previousFieldIndex,
staticInitialValueIterator, annotationIterator);
previousFieldIndex = item.fieldIndex;
@ -144,7 +144,7 @@ public class DexBackedClassDef implements ClassDef {
}
@Override
protected void skipItem(DexFileReader reader, int index) {
protected void skipItem(DexReader reader, int index) {
previousFieldIndex = DexBackedField.skipEncodedField(reader, previousFieldIndex);
staticInitialValueIterator.skipNext();
}
@ -162,7 +162,7 @@ public class DexBackedClassDef implements ClassDef {
@Override
public List<? extends Method> getMethods() {
if (classDataOffset > 0) {
DexFileReader reader = dexFile.readerAt(classDataOffset);
DexReader reader = dexBuf.readerAt(classDataOffset);
int staticFieldCount = reader.readSmallUleb128();
int instanceFieldCount = reader.readSmallUleb128();
int directMethodCount = reader.readSmallUleb128();
@ -177,18 +177,18 @@ public class DexBackedClassDef implements ClassDef {
@Nonnull
@Override
public Iterator listIterator() {
return new Iterator(dexFile, methodsStartOffset) {
return new Iterator(dexBuf, methodsStartOffset) {
private int previousMethodIndex = 0;
@Nonnull private final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator =
annotationsDirectory.getMethodAnnotationIterator();
@Nonnull private final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator =
annotationsDirectory.getParameterAnnotationIterator();
@Nonnull private final StaticInitialValueIterator staticInitialValueIterator =
StaticInitialValueIterator.newOrEmpty(dexFile, staticInitialValuesOffset);
StaticInitialValueIterator.newOrEmpty(dexBuf, staticInitialValuesOffset);
@Nonnull
@Override
protected DexBackedMethod readItem(DexFileReader reader, int index) {
protected DexBackedMethod readItem(DexReader reader, int index) {
DexBackedMethod item = new DexBackedMethod(reader, previousMethodIndex,
methodAnnotationIterator, parameterAnnotationIterator);
previousMethodIndex = item.methodIndex;
@ -196,7 +196,7 @@ public class DexBackedClassDef implements ClassDef {
}
@Override
protected void skipItem(DexFileReader reader, int index) {
protected void skipItem(DexReader reader, int index) {
previousMethodIndex = DexBackedMethod.skipEncodedMethod(reader, previousMethodIndex);
staticInitialValueIterator.skipNext();
}

View File

@ -39,24 +39,24 @@ import javax.annotation.Nonnull;
import java.util.List;
public class DexBackedDexFile implements DexFile {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
private static final int CLASS_DEF_SIZE = 32;
public DexBackedDexFile(DexFileBuffer dexFile) {
this.dexFile = dexFile;
public DexBackedDexFile(DexBuffer dexBuf) {
this.dexBuf = dexBuf;
}
@Nonnull
@Override
public List<? extends ClassDef> getClasses() {
final int classCount = dexFile.getClassCount();
final int classCount = dexBuf.getClassCount();
return new FixedSizeList<ClassDef>() {
@Override
public ClassDef readItem(int index) {
int classOffset = dexFile.getClassDefOffset(index);
return new DexBackedClassDef(dexFile, classOffset);
int classOffset = dexBuf.getClassDefOffset(index);
return new DexBackedClassDef(dexBuf, classOffset);
}
@Override

View File

@ -37,10 +37,10 @@ import org.jf.dexlib2.immutable.ImmutableExceptionHandler;
import javax.annotation.Nonnull;
public class DexBackedExceptionHandler extends ImmutableExceptionHandler {
public DexBackedExceptionHandler(@Nonnull DexFileReader dexFileReader,
public DexBackedExceptionHandler(@Nonnull DexReader reader,
@Nonnull InstructionOffsetMap instructionOffsetMap) {
// TODO: verify dalvik doesn't accept an exception handler that points in the middle of an instruction
super(dexFileReader.getType(dexFileReader.readSmallUleb128()),
instructionOffsetMap.getInstructionIndexAtOffsetExact(dexFileReader.readSmallUleb128()));
super(reader.getType(reader.readSmallUleb128()),
instructionOffsetMap.getInstructionIndexAtOffsetExact(reader.readSmallUleb128()));
}
}

View File

@ -41,7 +41,7 @@ import javax.annotation.Nullable;
import java.util.List;
public class DexBackedField implements Field {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
@Nonnull public final String name;
@Nonnull public final String type;
@ -55,22 +55,22 @@ public class DexBackedField implements Field {
private static final int TYPE_OFFSET = 2;
private static final int NAME_OFFSET = 4;
public DexBackedField(@Nonnull DexFileReader dexFileReader,
public DexBackedField(@Nonnull DexReader reader,
int previousFieldIndex,
@Nonnull StaticInitialValueIterator staticInitialValueIterator,
@Nonnull AnnotationsDirectory.AnnotationIterator annotationIterator) {
this.dexFile = dexFileReader.getDexFile();
this.dexBuf = reader.getDexBuffer();
int fieldIndexDiff = dexFileReader.readSmallUleb128();
int fieldIndexDiff = reader.readSmallUleb128();
this.fieldIndex = fieldIndexDiff + previousFieldIndex;
this.accessFlags = dexFileReader.readSmallUleb128();
this.accessFlags = reader.readSmallUleb128();
this.annotationSetOffset = annotationIterator.seekTo(fieldIndex);
this.initialValue = staticInitialValueIterator.getNextOrNull();
int fieldIdItemOffset = dexFileReader.getFieldIdItemOffset(fieldIndex);
this.type = dexFileReader.getType(dexFileReader.readUshort(fieldIdItemOffset + TYPE_OFFSET));
this.name = dexFileReader.getString(dexFileReader.readSmallUint(fieldIdItemOffset + NAME_OFFSET));
int fieldIdItemOffset = reader.getFieldIdItemOffset(fieldIndex);
this.type = reader.getType(reader.readUshort(fieldIdItemOffset + TYPE_OFFSET));
this.name = reader.getString(reader.readSmallUint(fieldIdItemOffset + NAME_OFFSET));
}
@ -82,31 +82,31 @@ public class DexBackedField implements Field {
@Nonnull
@Override
public List<? extends DexBackedAnnotation> getAnnotations() {
return AnnotationsDirectory.getAnnotations(dexFile, annotationSetOffset);
return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset);
}
/**
* Skips the reader over a single encoded_field structure.
* @param dexFileReader The {@code DexFileReader} to skip
* @param reader The {@code DexFileReader} to skip
* @param previousFieldIndex The field index of the previous field, or 0 if this is the first
* @return The field index of the field that was skipped
*/
public static int skipEncodedField(@Nonnull DexFileReader dexFileReader, int previousFieldIndex) {
int idxDiff = dexFileReader.readSmallUleb128();
dexFileReader.skipUleb128();
public static int skipEncodedField(@Nonnull DexReader reader, int previousFieldIndex) {
int idxDiff = reader.readSmallUleb128();
reader.skipUleb128();
return previousFieldIndex + idxDiff;
}
/**
* Skips the reader over the specified number of encoded_field structures
*
* @param dexFileReader The reader to skip
* @param reader The reader to skip
* @param count The number of encoded_field structures to skip over
*/
public static void skipAllFields(@Nonnull DexFileReader dexFileReader, int count) {
public static void skipAllFields(@Nonnull DexReader reader, int count) {
for (int i=0; i<count; i++) {
dexFileReader.skipUleb128();
dexFileReader.skipUleb128();
reader.skipUleb128();
reader.skipUleb128();
}
}
}

View File

@ -44,7 +44,7 @@ import javax.annotation.Nullable;
import java.util.List;
public class DexBackedMethod implements Method {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
@Nonnull public final String name;
public final int accessFlags;
@ -65,30 +65,30 @@ public class DexBackedMethod implements Method {
private static final int RETURN_TYPE_OFFSET = 4;
private static final int PARAMETERS_OFFSET = 8;
public DexBackedMethod(@Nonnull DexFileReader dexFileReader,
public DexBackedMethod(@Nonnull DexReader reader,
int previousMethodIndex,
@Nonnull AnnotationsDirectory.AnnotationIterator methodAnnotationIterator,
@Nonnull AnnotationsDirectory.AnnotationIterator paramaterAnnotationIterator) {
this.dexFile = dexFileReader.getDexFile();
this.dexBuf = reader.getDexBuffer();
int methodIndexDiff = dexFileReader.readSmallUleb128();
int methodIndexDiff = reader.readSmallUleb128();
this.methodIndex = methodIndexDiff + previousMethodIndex;
this.accessFlags = dexFileReader.readSmallUleb128();
this.codeOffset = dexFileReader.readSmallUleb128();
this.accessFlags = reader.readSmallUleb128();
this.codeOffset = reader.readSmallUleb128();
this.methodAnnotationSetOffset = methodAnnotationIterator.seekTo(methodIndex);
int parameterAnnotationSetListOffset = paramaterAnnotationIterator.seekTo(methodIndex);
this.parameterAnnotations =
AnnotationsDirectory.getParameterAnnotations(dexFile, parameterAnnotationSetListOffset);
AnnotationsDirectory.getParameterAnnotations(dexBuf, parameterAnnotationSetListOffset);
int methodIdItemOffset = dexFileReader.getMethodIdItemOffset(methodIndex);
int protoIndex = dexFileReader.readUshort(methodIdItemOffset + PROTO_OFFSET);
int protoIdItemOffset = dexFileReader.getProtoIdItemOffset(protoIndex);
int methodIdItemOffset = reader.getMethodIdItemOffset(methodIndex);
int protoIndex = reader.readUshort(methodIdItemOffset + PROTO_OFFSET);
int protoIdItemOffset = reader.getProtoIdItemOffset(protoIndex);
this.name = dexFileReader.getString(dexFileReader.readSmallUint(methodIdItemOffset + NAME_OFFSET));
this.name = reader.getString(reader.readSmallUint(methodIdItemOffset + NAME_OFFSET));
this.returnType = dexFileReader.getString(dexFileReader.readSmallUint(protoIdItemOffset + RETURN_TYPE_OFFSET));
this.parametersOffset = dexFileReader.readSmallUint(protoIdItemOffset + PARAMETERS_OFFSET);
this.returnType = reader.getString(reader.readSmallUint(protoIdItemOffset + RETURN_TYPE_OFFSET));
this.parametersOffset = reader.readSmallUint(protoIdItemOffset + PARAMETERS_OFFSET);
}
@ -100,7 +100,7 @@ public class DexBackedMethod implements Method {
@Override
public List<? extends MethodParameter> getParameters() {
if (parametersOffset > 0) {
final int size = dexFile.readSmallUint(parametersOffset);
final int size = dexBuf.readSmallUint(parametersOffset);
return new FixedSizeList<MethodParameter>() {
@Override
@ -109,8 +109,8 @@ public class DexBackedMethod implements Method {
@Nonnull
@Override
public String getType() {
int typeIndex = dexFile.readUshort(parametersOffset + 4 + (index * 2));
return dexFile.getType(typeIndex);
int typeIndex = dexBuf.readUshort(parametersOffset + 4 + (index * 2));
return dexBuf.getType(typeIndex);
}
@Nonnull
@ -134,28 +134,28 @@ public class DexBackedMethod implements Method {
@Nonnull
@Override
public List<? extends Annotation> getAnnotations() {
return AnnotationsDirectory.getAnnotations(dexFile, methodAnnotationSetOffset);
return AnnotationsDirectory.getAnnotations(dexBuf, methodAnnotationSetOffset);
}
@Nullable
@Override
public MethodImplementation getImplementation() {
if (codeOffset > 0) {
return new DexBackedMethodImplementation(dexFile, codeOffset);
return new DexBackedMethodImplementation(dexBuf, codeOffset);
}
return null;
}
/**
* Skips the reader over a single encoded_method structure.
* @param dexFileReader The {@code DexFileReader} to skip
* @param reader The {@code DexFileReader} to skip
* @param previousMethodIndex The method index of the previous field, or 0 if this is the first
* @return The method index of the field that was skipped
*/
public static int skipEncodedMethod(@Nonnull DexFileReader dexFileReader, int previousMethodIndex) {
int idxDiff = dexFileReader.readSmallUleb128();
dexFileReader.skipUleb128();
dexFileReader.skipUleb128();
public static int skipEncodedMethod(@Nonnull DexReader reader, int previousMethodIndex) {
int idxDiff = reader.readSmallUleb128();
reader.skipUleb128();
reader.skipUleb128();
return previousMethodIndex + idxDiff;
}
}

View File

@ -45,7 +45,7 @@ import java.util.ArrayList;
import java.util.List;
public class DexBackedMethodImplementation implements MethodImplementation {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
private final int codeOffset;
public final int registerCount;
@ -59,11 +59,11 @@ public class DexBackedMethodImplementation implements MethodImplementation {
private static final int TRY_ITEM_SIZE = 8;
public DexBackedMethodImplementation(@Nonnull DexFileBuffer dexFile,
public DexBackedMethodImplementation(@Nonnull DexBuffer dexBuf,
int codeOffset) {
this.dexFile = dexFile;
this.dexBuf = dexBuf;
this.codeOffset = codeOffset;
this.registerCount = dexFile.readUshort(codeOffset);
this.registerCount = dexBuf.readUshort(codeOffset);
instructions = buildInstructionList();
instructionOffsetMap = buildInstructionOffsetMap();
@ -75,9 +75,9 @@ public class DexBackedMethodImplementation implements MethodImplementation {
@Nonnull
@Override
public List<? extends TryBlock> getTryBlocks() {
final int triesSize = dexFile.readUshort(codeOffset + TRIES_SIZE_OFFSET);
final int triesSize = dexBuf.readUshort(codeOffset + TRIES_SIZE_OFFSET);
if (triesSize > 0) {
int instructionsSize = dexFile.readSmallUint(codeOffset + INSTRUCTIONS_SIZE_OFFSET);
int instructionsSize = dexBuf.readSmallUint(codeOffset + INSTRUCTIONS_SIZE_OFFSET);
final int triesStartOffset = AlignmentUtils.alignOffset(
codeOffset + INSTRUCTIONS_START_OFFSET + (instructionsSize*2), 4);
final int handlersStartOffset = triesStartOffset + triesSize*TRY_ITEM_SIZE;
@ -85,7 +85,7 @@ public class DexBackedMethodImplementation implements MethodImplementation {
return new FixedSizeList<TryBlock>() {
@Override
public TryBlock readItem(int index) {
return new DexBackedTryBlock(dexFile,
return new DexBackedTryBlock(dexBuf,
triesStartOffset + index*TRY_ITEM_SIZE,
handlersStartOffset,
instructionOffsetMap);
@ -102,12 +102,12 @@ public class DexBackedMethodImplementation implements MethodImplementation {
private ImmutableList<? extends Instruction> buildInstructionList() {
// instructionsSize is the number of 16-bit code units in the instruction list, not the number of instructions
int instructionsSize = dexFile.readSmallUint(codeOffset + INSTRUCTIONS_SIZE_OFFSET);
int instructionsSize = dexBuf.readSmallUint(codeOffset + INSTRUCTIONS_SIZE_OFFSET);
// we can use instructionsSize as an upper bound on the number of instructions there will be
ArrayList<Instruction> instructions = new ArrayList<Instruction>(instructionsSize);
int instructionsStartOffset = codeOffset + INSTRUCTIONS_START_OFFSET;
DexFileReader reader = dexFile.readerAt(instructionsStartOffset);
DexReader reader = dexBuf.readerAt(instructionsStartOffset);
int endOffset = instructionsStartOffset + (instructionsSize*2);
while (reader.getOffset() < endOffset) {

View File

@ -40,7 +40,7 @@ import javax.annotation.Nonnull;
import java.util.List;
public class DexBackedTryBlock implements TryBlock {
public final DexFileBuffer dexFile;
public final DexBuffer dexBuf;
private final InstructionOffsetMap instructionOffsetMap;
public final int startIndex;
@ -52,18 +52,18 @@ public class DexBackedTryBlock implements TryBlock {
private static final int CODE_UNIT_COUNT_OFFSET = 4;
private static final int HANDLER_OFFSET_OFFSET = 6;
public DexBackedTryBlock(DexFileBuffer dexFile,
public DexBackedTryBlock(DexBuffer dexBuf,
int tryItemOffset,
int handlersStartOffset,
InstructionOffsetMap instructionOffsetMap) {
this.dexFile = dexFile;
this.dexBuf = dexBuf;
this.instructionOffsetMap = instructionOffsetMap;
int startOffset = dexFile.readSmallUint(tryItemOffset + START_ADDRESS_OFFSET);
int startOffset = dexBuf.readSmallUint(tryItemOffset + START_ADDRESS_OFFSET);
// map the code unit offset to the instruction index
this.startIndex = instructionOffsetMap.getInstructionIndexAtOffsetExact(startOffset);
int codeUnitCount = dexFile.readUshort(tryItemOffset + CODE_UNIT_COUNT_OFFSET);
int codeUnitCount = dexBuf.readUshort(tryItemOffset + CODE_UNIT_COUNT_OFFSET);
// TODO: check if dalivk accepts insns_size = 0
if (codeUnitCount == 0) {
this.instructionCount = 0;
@ -72,7 +72,7 @@ public class DexBackedTryBlock implements TryBlock {
this.instructionCount = lastIndex - startIndex + 1;
}
this.exceptionHandlersOffset = handlersStartOffset + dexFile.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET);
this.exceptionHandlersOffset = handlersStartOffset + dexBuf.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET);
}
@Override public int getStartIndex() { return startIndex; }
@ -81,30 +81,30 @@ public class DexBackedTryBlock implements TryBlock {
@Nonnull
@Override
public List<? extends ExceptionHandler> getExceptionHandlers() {
DexFileReader reader = dexFile.readerAt(exceptionHandlersOffset);
DexReader reader = dexBuf.readerAt(exceptionHandlersOffset);
final int encodedSize = reader.readSleb128();
if (encodedSize > 0) {
//no catch-all
return new VariableSizeList<ExceptionHandler>(dexFile, reader.getOffset()) {
return new VariableSizeList<ExceptionHandler>(dexBuf, reader.getOffset()) {
@Nonnull
@Override
protected ExceptionHandler readItem(DexFileReader dexFileReader, int index) {
return new DexBackedExceptionHandler(dexFileReader, instructionOffsetMap);
protected ExceptionHandler readItem(DexReader reader, int index) {
return new DexBackedExceptionHandler(reader, instructionOffsetMap);
}
@Override public int size() { return encodedSize; }
};
} else {
//with catch-all
final int sizeWithCatchAll = (-1 * encodedSize) + 1;
return new VariableSizeList<ExceptionHandler>(dexFile, reader.getOffset()) {
return new VariableSizeList<ExceptionHandler>(dexBuf, reader.getOffset()) {
@Nonnull
@Override
protected ExceptionHandler readItem(DexFileReader dexFileReader, int index) {
protected ExceptionHandler readItem(DexReader dexReader, int index) {
if (index == sizeWithCatchAll-1) {
return new DexBackedCatchAllExceptionHandler(dexFileReader, instructionOffsetMap);
return new DexBackedCatchAllExceptionHandler(dexReader, instructionOffsetMap);
} else {
return new DexBackedExceptionHandler(dexFileReader, instructionOffsetMap);
return new DexBackedExceptionHandler(dexReader, instructionOffsetMap);
}
}
@Override public int size() { return sizeWithCatchAll; }

View File

@ -35,11 +35,11 @@ import org.jf.util.ExceptionWithContext;
import javax.annotation.Nullable;
public class DexFileBuffer {
public class DexBuffer {
// TODO: consider using a direct ByteBuffer instead
protected final byte[] buf;
public DexFileBuffer(byte[] buf) {
public DexBuffer(byte[] buf) {
this.buf = buf;
}
@ -141,7 +141,7 @@ public class DexFileBuffer {
return buf[offset];
}
public DexFileReader readerAt(int offset) {
return new DexFileReader(this, offset);
public DexReader readerAt(int offset) {
return new DexReader(this, offset);
}
}

View File

@ -35,33 +35,33 @@ import org.jf.util.ExceptionWithContext;
import javax.annotation.Nonnull;
public class DexFileReader {
@Nonnull private final DexFileBuffer dexFile;
public class DexReader {
@Nonnull private final DexBuffer dexBuf;
private int offset;
public DexFileReader(@Nonnull DexFileBuffer dexFile, int offset) {
this.dexFile = dexFile;
public DexReader(@Nonnull DexBuffer dexBuf, int offset) {
this.dexBuf = dexBuf;
this.offset = offset;
}
@Nonnull public DexFileBuffer getDexFile() { return dexFile; }
@Nonnull public DexBuffer getDexBuffer() { return dexBuf; }
public int getOffset() { return offset; }
public String getString(int stringIndex) { return dexFile.getString(stringIndex); }
public int getFieldIdItemOffset(int fieldIndex) { return dexFile.getFieldIdItemOffset(fieldIndex); }
public int getMethodIdItemOffset(int methodIndex) { return dexFile.getMethodIdItemOffset(methodIndex); }
public int getProtoIdItemOffset(int methodIndex) { return dexFile.getProtoIdItemOffset(methodIndex); }
public String getType(int typeIndex) { return dexFile.getType(typeIndex); }
public String getField(int fieldIndex) { return dexFile.getField(fieldIndex); }
public String getMethod(int methodIndex) { return dexFile.getMethod(methodIndex); }
public String getReference(int type, int index) { return dexFile.getReference(type, index); }
public String getString(int stringIndex) { return dexBuf.getString(stringIndex); }
public int getFieldIdItemOffset(int fieldIndex) { return dexBuf.getFieldIdItemOffset(fieldIndex); }
public int getMethodIdItemOffset(int methodIndex) { return dexBuf.getMethodIdItemOffset(methodIndex); }
public int getProtoIdItemOffset(int methodIndex) { return dexBuf.getProtoIdItemOffset(methodIndex); }
public String getType(int typeIndex) { return dexBuf.getType(typeIndex); }
public String getField(int fieldIndex) { return dexBuf.getField(fieldIndex); }
public String getMethod(int methodIndex) { return dexBuf.getMethod(methodIndex); }
public String getReference(int type, int index) { return dexBuf.getReference(type, index); }
/** {@inheritDoc} */
public int readSleb128() {
int end = offset;
int currentByteValue;
int result;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
result = buf[end++] & 0xff;
if (result <= 0x7f) {
@ -101,7 +101,7 @@ public class DexFileReader {
int end = offset;
int currentByteValue;
int result;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
result = buf[end++] & 0xff;
if (result > 0x7f) {
@ -139,7 +139,7 @@ public class DexFileReader {
public void skipUleb128() {
int end = offset;
byte currentByteValue;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
currentByteValue = buf[end++];
if (currentByteValue < 0) { // if the MSB is set
@ -169,49 +169,49 @@ public class DexFileReader {
public int readSmallUint() {
int o = offset;
int result = dexFile.readSmallUint(o);
int result = dexBuf.readSmallUint(o);
offset = o + 4;
return result;
}
public int readUshort() {
int o = offset;
int result = dexFile.readUshort(offset);
int result = dexBuf.readUshort(offset);
offset = o + 2;
return result;
}
public int readUbyte() {
int o = offset;
int result = dexFile.readUbyte(offset);
int result = dexBuf.readUbyte(offset);
offset = o + 1;
return result;
}
public long readLong() {
int o = offset;
long result = dexFile.readLong(offset);
long result = dexBuf.readLong(offset);
offset = o + 2;
return result;
}
public int readInt() {
int o = offset;
int result = dexFile.readInt(offset);
int result = dexBuf.readInt(offset);
offset = o + 4;
return result;
}
public int readShort() {
int o = offset;
int result = dexFile.readShort(offset);
int result = dexBuf.readShort(offset);
offset = o + 2;
return result;
}
public int readByte() {
int o = offset;
int result = dexFile.readByte(offset);
int result = dexBuf.readByte(offset);
offset = o + 1;
return result;
}
@ -219,18 +219,18 @@ public class DexFileReader {
public void skipByte() { offset++; }
public void skipBytes(int i) { offset += i; }
public int readSmallUint(int offset) { return dexFile.readSmallUint(offset); }
public int readUshort(int offset) { return dexFile.readUshort(offset); }
public int readUbyte(int offset) { return dexFile.readUbyte(offset); }
public long readLong(int offset) { return dexFile.readLong(offset); }
public int readInt(int offset) { return dexFile.readInt(offset); }
public int readShort(int offset) { return dexFile.readShort(offset); }
public int readByte(int offset) { return dexFile.readByte(offset); }
public int readSmallUint(int offset) { return dexBuf.readSmallUint(offset); }
public int readUshort(int offset) { return dexBuf.readUshort(offset); }
public int readUbyte(int offset) { return dexBuf.readUbyte(offset); }
public long readLong(int offset) { return dexBuf.readLong(offset); }
public int readInt(int offset) { return dexBuf.readInt(offset); }
public int readShort(int offset) { return dexBuf.readShort(offset); }
public int readByte(int offset) { return dexBuf.readByte(offset); }
public int readSizedInt(int bytes) {
int o = offset;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
int result;
switch (bytes) {
@ -261,7 +261,7 @@ public class DexFileReader {
public int readSizedSmallUint(int bytes) {
int o = offset;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
int result = 0;
switch (bytes) {
@ -291,7 +291,7 @@ public class DexFileReader {
public int readSizedRightExtendedInt(int bytes) {
int o = offset;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
int result = 0;
switch (bytes) {
@ -323,7 +323,7 @@ public class DexFileReader {
public long readSizedRightExtendedLong(int bytes) {
int o = offset;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
long result = 0;
switch (bytes) {
@ -389,7 +389,7 @@ public class DexFileReader {
public long readSizedLong(int bytes) {
int o = offset;
byte[] buf = dexFile.buf;
byte[] buf = dexBuf.buf;
long result = 0;
switch (bytes) {

View File

@ -31,7 +31,7 @@
package org.jf.dexlib2.dexbacked.instruction;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.iface.instruction.Instruction;
import org.jf.dexlib2.iface.instruction.formats.*;
@ -42,7 +42,7 @@ import org.jf.util.NibbleUtils;
import javax.annotation.Nonnull;
public abstract class DexBackedInstruction {
public static Instruction readFrom(DexFileReader reader) {
public static Instruction readFrom(DexReader reader) {
int opcodeValue = reader.readUbyte();
Opcode opcode = Opcode.getOpcodeByValue(opcodeValue);
@ -106,19 +106,19 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction10t instruction10t(Opcode opcode, DexFileReader reader) {
private static Instruction10t instruction10t(Opcode opcode, DexReader reader) {
int offset = reader.readByte();
return new ImmutableInstruction10t(opcode, offset);
}
@Nonnull
private static Instruction10x instruction10x(Opcode opcode, DexFileReader reader) {
private static Instruction10x instruction10x(Opcode opcode, DexReader reader) {
reader.skipByte();
return new ImmutableInstruction10x(opcode);
}
@Nonnull
private static Instruction11n instruction11n(Opcode opcode, DexFileReader reader) {
private static Instruction11n instruction11n(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerA = NibbleUtils.extractLowUnsignedNibble(b);
int literal = NibbleUtils.extractHighSignedNibble(b);
@ -126,13 +126,13 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction11x instruction11x(Opcode opcode, DexFileReader reader) {
private static Instruction11x instruction11x(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
return new ImmutableInstruction11x(opcode, registerA);
}
@Nonnull
private static Instruction12x instruction12x(Opcode opcode, DexFileReader reader) {
private static Instruction12x instruction12x(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerA = NibbleUtils.extractLowUnsignedNibble(b);
int registerB = NibbleUtils.extractHighUnsignedNibble(b);
@ -140,14 +140,14 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction20t instruction20t(Opcode opcode, DexFileReader reader) {
private static Instruction20t instruction20t(Opcode opcode, DexReader reader) {
reader.skipByte();
int offset = reader.readShort();
return new ImmutableInstruction20t(opcode, offset);
}
@Nonnull
private static Instruction21c instruction21c(Opcode opcode, DexFileReader reader) {
private static Instruction21c instruction21c(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int referenceIndex = reader.readUshort();
String reference = reader.getReference(opcode.referenceType, referenceIndex);
@ -155,35 +155,35 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction21ih instruction21ih(Opcode opcode, DexFileReader reader) {
private static Instruction21ih instruction21ih(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int literalHat = reader.readShort();
return new ImmutableInstruction21ih(opcode, registerA, literalHat << 16);
}
@Nonnull
private static Instruction21lh instruction21lh(Opcode opcode, DexFileReader reader) {
private static Instruction21lh instruction21lh(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int literalHat = reader.readShort();
return new ImmutableInstruction21lh(opcode, registerA, ((long)literalHat) << 48);
}
@Nonnull
private static Instruction21s instruction21s(Opcode opcode, DexFileReader reader) {
private static Instruction21s instruction21s(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int literal = reader.readShort();
return new ImmutableInstruction21s(opcode, registerA, literal);
}
@Nonnull
private static Instruction21t instruction21t(Opcode opcode, DexFileReader reader) {
private static Instruction21t instruction21t(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int offset = reader.readShort();
return new ImmutableInstruction21t(opcode, registerA, offset);
}
@Nonnull
private static Instruction22b instruction22b(Opcode opcode, DexFileReader reader) {
private static Instruction22b instruction22b(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int registerB = reader.readUbyte();
int literal = reader.readByte();
@ -191,7 +191,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction22c instruction22c(Opcode opcode, DexFileReader reader) {
private static Instruction22c instruction22c(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerA = NibbleUtils.extractLowUnsignedNibble(b);
int registerB = NibbleUtils.extractHighUnsignedNibble(b);
@ -202,7 +202,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction22s instruction22s(Opcode opcode, DexFileReader reader) {
private static Instruction22s instruction22s(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerA = NibbleUtils.extractLowUnsignedNibble(b);
int registerB = NibbleUtils.extractHighUnsignedNibble(b);
@ -211,7 +211,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction22t instruction22t(Opcode opcode, DexFileReader reader) {
private static Instruction22t instruction22t(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerA = NibbleUtils.extractLowUnsignedNibble(b);
int registerB = NibbleUtils.extractHighUnsignedNibble(b);
@ -220,14 +220,14 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction22x instruction22x(Opcode opcode, DexFileReader reader) {
private static Instruction22x instruction22x(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int registerB = reader.readUshort();
return new ImmutableInstruction22x(opcode, registerA, registerB);
}
@Nonnull
private static Instruction23x instruction23x(Opcode opcode, DexFileReader reader) {
private static Instruction23x instruction23x(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int registerB = reader.readUbyte();
int registerC = reader.readUbyte();
@ -235,14 +235,14 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction30t instruction30t(Opcode opcode, DexFileReader reader) {
private static Instruction30t instruction30t(Opcode opcode, DexReader reader) {
reader.skipByte();
int offset = reader.readInt();
return new ImmutableInstruction30t(opcode, offset);
}
@Nonnull
private static Instruction31c instruction31c(Opcode opcode, DexFileReader reader) {
private static Instruction31c instruction31c(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int referenceIndex = reader.readSmallUint();
String reference = reader.getReference(opcode.referenceType, referenceIndex);
@ -250,21 +250,21 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction31i instruction31i(Opcode opcode, DexFileReader reader) {
private static Instruction31i instruction31i(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int literal = reader.readInt();
return new ImmutableInstruction31i(opcode, registerA, literal);
}
@Nonnull
private static Instruction31t instruction31t(Opcode opcode, DexFileReader reader) {
private static Instruction31t instruction31t(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
int offset = reader.readInt();
return new ImmutableInstruction31t(opcode, registerA, offset);
}
@Nonnull
private static Instruction32x instruction32x(Opcode opcode, DexFileReader reader) {
private static Instruction32x instruction32x(Opcode opcode, DexReader reader) {
reader.skipByte();
int registerA = reader.readUshort();
int registerB = reader.readUshort();
@ -272,7 +272,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction35c instruction35c(Opcode opcode, DexFileReader reader) {
private static Instruction35c instruction35c(Opcode opcode, DexReader reader) {
int b = reader.readUbyte();
int registerCount = NibbleUtils.extractHighUnsignedNibble(b);
int registerG = NibbleUtils.extractLowUnsignedNibble(b);
@ -293,7 +293,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction3rc instruction3rc(Opcode opcode, DexFileReader reader) {
private static Instruction3rc instruction3rc(Opcode opcode, DexReader reader) {
int registerCount = reader.readUbyte();
int referenceIndex = reader.readUshort();
String reference = reader.getReference(opcode.referenceType, referenceIndex);
@ -302,7 +302,7 @@ public abstract class DexBackedInstruction {
}
@Nonnull
private static Instruction51l instruction51l(Opcode opcode, DexFileReader reader) {
private static Instruction51l instruction51l(Opcode opcode, DexReader reader) {
int registerA = reader.readUbyte();
long literal = reader.readLong();
return new ImmutableInstruction51l(opcode, registerA, literal);

View File

@ -32,7 +32,7 @@
package org.jf.dexlib2.dexbacked.util;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexBackedAnnotation;
import javax.annotation.Nonnull;
@ -53,12 +53,12 @@ public abstract class AnnotationsDirectory {
@Nonnull public abstract AnnotationIterator getMethodAnnotationIterator();
@Nonnull public abstract AnnotationIterator getParameterAnnotationIterator();
public static AnnotationsDirectory newOrEmpty(@Nonnull DexFileBuffer dexFile,
public static AnnotationsDirectory newOrEmpty(@Nonnull DexBuffer dexBuf,
int directoryAnnotationsOffset) {
if (directoryAnnotationsOffset == 0) {
return EMPTY;
}
return new AnnotationsDirectoryImpl(dexFile, directoryAnnotationsOffset);
return new AnnotationsDirectoryImpl(dexBuf, directoryAnnotationsOffset);
}
@ -71,15 +71,15 @@ public abstract class AnnotationsDirectory {
}
@Nonnull
public static List<? extends DexBackedAnnotation> getAnnotations(@Nonnull final DexFileBuffer dexFile,
public static List<? extends DexBackedAnnotation> getAnnotations(@Nonnull final DexBuffer dexBuf,
final int annotationSetOffset) {
if (annotationSetOffset != 0) {
final int size = dexFile.readSmallUint(annotationSetOffset);
final int size = dexBuf.readSmallUint(annotationSetOffset);
return new FixedSizeList<DexBackedAnnotation>() {
@Override
public DexBackedAnnotation readItem(int index) {
int annotationOffset = dexFile.readSmallUint(annotationSetOffset + 4 + (4*index));
return new DexBackedAnnotation(dexFile, annotationOffset);
int annotationOffset = dexBuf.readSmallUint(annotationSetOffset + 4 + (4*index));
return new DexBackedAnnotation(dexBuf, annotationOffset);
}
@Override public int size() { return size; }
@ -89,16 +89,16 @@ public abstract class AnnotationsDirectory {
return ImmutableList.of();
}
public static List<List<? extends DexBackedAnnotation>> getParameterAnnotations(@Nonnull final DexFileBuffer dexFile,
public static List<List<? extends DexBackedAnnotation>> getParameterAnnotations(@Nonnull final DexBuffer dexBuf,
final int annotationSetListOffset) {
if (annotationSetListOffset > 0) {
final int size = dexFile.readSmallUint(annotationSetListOffset);
final int size = dexBuf.readSmallUint(annotationSetListOffset);
return new FixedSizeList<List<? extends DexBackedAnnotation>>() {
@Override
public List<? extends DexBackedAnnotation> readItem(int index) {
int annotationSetOffset = dexFile.readSmallUint(annotationSetListOffset + 4 + index * 4);
return getAnnotations(dexFile, annotationSetOffset);
int annotationSetOffset = dexBuf.readSmallUint(annotationSetListOffset + 4 + index * 4);
return getAnnotations(dexBuf, annotationSetOffset);
}
@Override public int size() { return size; }
@ -108,7 +108,7 @@ public abstract class AnnotationsDirectory {
}
private static class AnnotationsDirectoryImpl extends AnnotationsDirectory {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
private final int directoryOffset;
private static final int FIELD_COUNT_OFFSET = 4;
@ -121,27 +121,27 @@ public abstract class AnnotationsDirectory {
/** The size of a method_annotation structure */
private static final int METHOD_ANNOTATION_SIZE = 8;
public AnnotationsDirectoryImpl(@Nonnull DexFileBuffer dexFile,
public AnnotationsDirectoryImpl(@Nonnull DexBuffer dexBuf,
int directoryOffset) {
this.dexFile = dexFile;
this.dexBuf = dexBuf;
this.directoryOffset = directoryOffset;
}
public int getFieldAnnotationCount() {
return dexFile.readSmallUint(directoryOffset + FIELD_COUNT_OFFSET);
return dexBuf.readSmallUint(directoryOffset + FIELD_COUNT_OFFSET);
}
public int getMethodAnnotationCount() {
return dexFile.readSmallUint(directoryOffset + METHOD_COUNT_OFFSET);
return dexBuf.readSmallUint(directoryOffset + METHOD_COUNT_OFFSET);
}
public int getParameterAnnotationCount() {
return dexFile.readSmallUint(directoryOffset + PARAMETER_COUNT_OFFSET);
return dexBuf.readSmallUint(directoryOffset + PARAMETER_COUNT_OFFSET);
}
@Nonnull
public List<? extends DexBackedAnnotation> getClassAnnotations() {
return getAnnotations(dexFile, dexFile.readSmallUint(directoryOffset));
return getAnnotations(dexBuf, dexBuf.readSmallUint(directoryOffset));
}
@Nonnull
@ -177,7 +177,7 @@ public abstract class AnnotationsDirectory {
this.startOffset = startOffset;
this.size = size;
if (size > 0) {
currentItemIndex = dexFile.readSmallUint(startOffset);
currentItemIndex = dexBuf.readSmallUint(startOffset);
this.currentIndex = 0;
} else {
currentItemIndex = -1;
@ -188,11 +188,11 @@ public abstract class AnnotationsDirectory {
public int seekTo(int itemIndex) {
while (currentItemIndex < itemIndex && (currentIndex+1) < size) {
currentIndex++;
currentItemIndex = dexFile.readSmallUint(startOffset + (currentIndex*8));
currentItemIndex = dexBuf.readSmallUint(startOffset + (currentIndex*8));
}
if (currentItemIndex == itemIndex) {
return dexFile.readSmallUint(startOffset + (currentIndex*8)+4);
return dexBuf.readSmallUint(startOffset + (currentIndex*8)+4);
}
return 0;
}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.dexbacked.util;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.dexbacked.value.DexBackedEncodedValue;
import org.jf.dexlib2.iface.value.EncodedValue;
@ -49,20 +49,20 @@ public abstract class StaticInitialValueIterator {
public abstract void skipNext();
@Nonnull
public static StaticInitialValueIterator newOrEmpty(@Nonnull DexFileBuffer dexFile, int offset) {
public static StaticInitialValueIterator newOrEmpty(@Nonnull DexBuffer dexBuf, int offset) {
if (offset == 0) {
return EMPTY;
}
return new StaticInitialValueIteratorImpl(dexFile, offset);
return new StaticInitialValueIteratorImpl(dexBuf, offset);
}
private static class StaticInitialValueIteratorImpl extends StaticInitialValueIterator {
@Nonnull private final DexFileReader reader;
@Nonnull private final DexReader reader;
private final int size;
private int index = 0;
public StaticInitialValueIteratorImpl(@Nonnull DexFileBuffer dexFile, int offset) {
this.reader = dexFile.readerAt(offset);
public StaticInitialValueIteratorImpl(@Nonnull DexBuffer dexBuf, int offset) {
this.reader = dexBuf.readerAt(offset);
this.size = reader.readSmallUleb128();
}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.dexbacked.util;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.util.AbstractListIterator;
import javax.annotation.Nonnull;
@ -44,19 +44,19 @@ import java.util.NoSuchElementException;
* @param <T> The type of the item that this list contains
*/
public abstract class VariableSizeList<T> extends AbstractSequentialList<T> {
@Nonnull private final DexFileBuffer dexFile;
@Nonnull private final DexBuffer dexBuf;
private final int offset;
public VariableSizeList(@Nonnull DexFileBuffer dexFile, int offset) {
this.dexFile = dexFile;
public VariableSizeList(@Nonnull DexBuffer dexBuf, int offset) {
this.dexBuf = dexBuf;
this.offset = offset;
}
@Nonnull
protected abstract T readItem(DexFileReader dexFileReader, int index);
protected abstract T readItem(DexReader reader, int index);
protected void skipItem(DexFileReader dexFileReader, int index) {
readItem(dexFileReader, index);
protected void skipItem(DexReader reader, int index) {
readItem(reader, index);
}
@Nonnull
@ -80,7 +80,7 @@ public abstract class VariableSizeList<T> extends AbstractSequentialList<T> {
public class Iterator extends AbstractListIterator<T> {
private int index = 0;
@Nonnull private final DexFileReader dexFileReader = dexFile.readerAt(offset);
@Nonnull private final DexReader reader = dexBuf.readerAt(offset);
@Override public boolean hasNext() { return index < size(); }
@Override public int nextIndex() { return index; }
@ -91,14 +91,14 @@ public abstract class VariableSizeList<T> extends AbstractSequentialList<T> {
if (index >= size()) {
throw new NoSuchElementException();
}
return readItem(dexFileReader, index++);
return readItem(reader, index++);
}
public void skip() {
if (index >= size()) {
throw new NoSuchElementException();
}
skipItem(dexFileReader, index++);
skipItem(reader, index++);
}
}
}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.dexbacked.util;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.util.AbstractListIterator;
import javax.annotation.Nonnull;
@ -66,10 +66,10 @@ public abstract class VariableSizeListWithContext<T> extends AbstractSequentialL
public abstract class Iterator extends AbstractListIterator<T> {
private int index = 0;
@Nonnull private final DexFileReader reader;
@Nonnull private final DexReader reader;
public Iterator(DexFileBuffer dexFile, int offset) {
this.reader = dexFile.readerAt(offset);
public Iterator(DexBuffer dexBuf, int offset) {
this.reader = dexBuf.readerAt(offset);
}
/**
@ -79,7 +79,7 @@ public abstract class VariableSizeListWithContext<T> extends AbstractSequentialL
*
* @return The next item that was read from {@code reader}
*/
@Nonnull protected abstract T readItem(DexFileReader reader, int index);
@Nonnull protected abstract T readItem(DexReader reader, int index);
/**
* Skip the next item in {@code reader}.
@ -88,7 +88,7 @@ public abstract class VariableSizeListWithContext<T> extends AbstractSequentialL
* can be overridden if skipping an item can be implemented more efficiently than reading
* the same item.
*/
protected void skipItem(DexFileReader reader, int index) {
protected void skipItem(DexReader reader, int index) {
readItem(reader, index);
}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.dexbacked.value;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.ValueType;
import org.jf.dexlib2.dexbacked.DexBackedAnnotationElement;
import org.jf.dexlib2.dexbacked.util.VariableSizeList;
@ -43,23 +43,23 @@ import javax.annotation.Nonnull;
import java.util.List;
public class DexBackedAnnotationEncodedValue implements AnnotationEncodedValue {
@Nonnull public final DexFileBuffer dexFile;
public final String type;
@Nonnull public final DexBuffer dexBuf;
@Nonnull public final String type;
private final int elementsOffset;
public DexBackedAnnotationEncodedValue(@Nonnull DexFileReader dexFileReader) {
this.dexFile = dexFileReader.getDexFile();
this.type = dexFileReader.getString(dexFileReader.readSmallUleb128());
this.elementsOffset = dexFileReader.getOffset();
skipElements(dexFileReader);
public DexBackedAnnotationEncodedValue(@Nonnull DexReader reader) {
this.dexBuf = reader.getDexBuffer();
this.type = reader.getString(reader.readSmallUleb128());
this.elementsOffset = reader.getOffset();
skipElements(reader);
}
public static void skipFrom(DexFileReader reader) {
public static void skipFrom(DexReader reader) {
reader.skipUleb128();
skipElements(reader);
}
private static void skipElements(DexFileReader reader) {
private static void skipElements(DexReader reader) {
int elementCount = reader.readSmallUleb128();
for (int i=0; i<elementCount; i++) {
reader.skipUleb128();
@ -73,14 +73,14 @@ public class DexBackedAnnotationEncodedValue implements AnnotationEncodedValue {
@Nonnull
@Override
public List<? extends AnnotationElement> getElements() {
DexFileReader reader = dexFile.readerAt(elementsOffset);
DexReader reader = dexBuf.readerAt(elementsOffset);
final int size = reader.readSmallUleb128();
return new VariableSizeList<AnnotationElement>(dexFile, reader.getOffset()) {
return new VariableSizeList<AnnotationElement>(dexBuf, reader.getOffset()) {
@Nonnull
@Override
protected AnnotationElement readItem(DexFileReader dexFileReader, int index) {
return new DexBackedAnnotationElement(dexFileReader);
protected AnnotationElement readItem(DexReader dexReader, int index) {
return new DexBackedAnnotationElement(dexReader);
}
@Override public int size() { return size;}

View File

@ -31,8 +31,8 @@
package org.jf.dexlib2.dexbacked.value;
import org.jf.dexlib2.dexbacked.DexFileBuffer;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexBuffer;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.ValueType;
import org.jf.dexlib2.dexbacked.util.VariableSizeList;
import org.jf.dexlib2.iface.value.ArrayEncodedValue;
@ -42,16 +42,16 @@ import javax.annotation.Nonnull;
import java.util.List;
public class DexBackedArrayEncodedValue implements ArrayEncodedValue {
@Nonnull public final DexFileBuffer dexFile;
@Nonnull public final DexBuffer dexBuf;
private final int encodedArrayOffset;
public DexBackedArrayEncodedValue(@Nonnull DexFileReader dexFileReader) {
this.dexFile = dexFileReader.getDexFile();
this.encodedArrayOffset = dexFileReader.getOffset();
skipFrom(dexFileReader);
public DexBackedArrayEncodedValue(@Nonnull DexReader reader) {
this.dexBuf = reader.getDexBuffer();
this.encodedArrayOffset = reader.getOffset();
skipFrom(reader);
}
public static void skipFrom(@Nonnull DexFileReader reader) {
public static void skipFrom(@Nonnull DexReader reader) {
int elementCount = reader.readSmallUleb128();
for (int i=0; i<elementCount; i++) {
DexBackedEncodedValue.skipFrom(reader);
@ -63,14 +63,14 @@ public class DexBackedArrayEncodedValue implements ArrayEncodedValue {
@Nonnull
@Override
public List<? extends EncodedValue> getValue() {
DexFileReader reader = dexFile.readerAt(encodedArrayOffset);
DexReader reader = dexBuf.readerAt(encodedArrayOffset);
final int size = reader.readSmallUleb128();
return new VariableSizeList<EncodedValue>(dexFile, reader.getOffset()) {
return new VariableSizeList<EncodedValue>(dexBuf, reader.getOffset()) {
@Nonnull
@Override
protected EncodedValue readItem(@Nonnull DexFileReader dexFileReader, int index) {
return DexBackedEncodedValue.readFrom(dexFileReader);
protected EncodedValue readItem(@Nonnull DexReader dexReader, int index) {
return DexBackedEncodedValue.readFrom(dexReader);
}
@Override public int size() { return size;}

View File

@ -31,7 +31,7 @@
package org.jf.dexlib2.dexbacked.value;
import org.jf.dexlib2.dexbacked.DexFileReader;
import org.jf.dexlib2.dexbacked.DexReader;
import org.jf.dexlib2.ValueType;
import org.jf.dexlib2.iface.value.EncodedValue;
import org.jf.dexlib2.immutable.value.*;
@ -39,7 +39,7 @@ import org.jf.dexlib2.util.Preconditions;
import org.jf.util.ExceptionWithContext;
public abstract class DexBackedEncodedValue {
public static EncodedValue readFrom(DexFileReader reader) {
public static EncodedValue readFrom(DexReader reader) {
int startOffset = reader.getOffset();
try {
@ -105,7 +105,7 @@ public abstract class DexBackedEncodedValue {
}
}
public static void skipFrom(DexFileReader reader) {
public static void skipFrom(DexReader reader) {
int startOffset = reader.getOffset();
try {

View File

@ -0,0 +1,230 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.dexbacked;
import junit.framework.Assert;
import org.jf.util.ExceptionWithContext;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
public class DexBufferTest {
@Test
public void testReadSmallUintSuccess() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
Assert.assertEquals(0x44332211, dexBuf.readSmallUint(0));
dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexBuf.readSmallUint(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(0x7fffffff, dexBuf.readSmallUint(0));
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge1() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
dexBuf.readSmallUint(0);
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge2() {
DexBuffer dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
dexBuf.readSmallUint(0);
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge3() {
DexBuffer dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
dexBuf.readSmallUint(0);
}
@Test
public void testReadUshort() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x11, 0x22});
Assert.assertEquals(dexBuf.readUshort(0), 0x2211);
dexBuf = new DexBuffer(new byte[] {0x00, 0x00});
Assert.assertEquals(dexBuf.readUshort(0), 0);
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff});
Assert.assertEquals(dexBuf.readUshort(0), 0xffff);
dexBuf = new DexBuffer(new byte[] {(byte)0x00, (byte)0x80});
Assert.assertEquals(dexBuf.readUshort(0), 0x8000);
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0x7f});
Assert.assertEquals(dexBuf.readUshort(0), 0x7fff);
}
@Test
public void testReadUbyte() {
byte[] buf = new byte[1];
DexBuffer dexBuf = new DexBuffer(buf);
for (int i=0; i<=0xff; i++) {
buf[0] = (byte)i;
Assert.assertEquals(i, dexBuf.readUbyte(0));
}
}
@Test
public void testReadLong() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77});
Assert.assertEquals(0x7766554433221100L, dexBuf.readLong(0));
dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexBuf.readLong(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(Long.MAX_VALUE, dexBuf.readLong(0));
dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80});
Assert.assertEquals(Long.MIN_VALUE, dexBuf.readLong(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
Assert.assertEquals(0x80ffffffffffffffL, dexBuf.readLong(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
Assert.assertEquals(-1, dexBuf.readLong(0));
}
@Test
public void testReadInt() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
Assert.assertEquals(0x44332211, dexBuf.readInt(0));
dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexBuf.readInt(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(Integer.MAX_VALUE, dexBuf.readInt(0));
dexBuf = new DexBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
Assert.assertEquals(Integer.MIN_VALUE, dexBuf.readInt(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
Assert.assertEquals(0x80ffffff, dexBuf.readInt(0));
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
Assert.assertEquals(-1, dexBuf.readInt(0));
}
@Test
public void testReadShort() {
DexBuffer dexBuf = new DexBuffer(new byte[] {0x11, 0x22});
Assert.assertEquals(dexBuf.readShort(0), 0x2211);
dexBuf = new DexBuffer(new byte[] {0x00, 0x00});
Assert.assertEquals(dexBuf.readShort(0), 0);
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0xff});
Assert.assertEquals(dexBuf.readShort(0), -1);
dexBuf = new DexBuffer(new byte[] {(byte)0x00, (byte)0x80});
Assert.assertEquals(dexBuf.readShort(0), Short.MIN_VALUE);
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0x7f});
Assert.assertEquals(dexBuf.readShort(0), 0x7fff);
dexBuf = new DexBuffer(new byte[] {(byte)0xff, (byte)0x80});
Assert.assertEquals(dexBuf.readShort(0), 0xffff80ff);
}
@Test
public void testReadByte() {
byte[] buf = new byte[1];
DexBuffer dexBuf = new DexBuffer(buf);
for (int i=0; i<=0xff; i++) {
buf[0] = (byte)i;
Assert.assertEquals((byte)i, dexBuf.readByte(0));
}
}
@Test
public void testReadRandom() {
Random r = new Random(1234567890);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(4).order(ByteOrder.LITTLE_ENDIAN);
byte[] buf = new byte[4];
DexBuffer dexBuf = new DexBuffer(buf);
for (int i=0; i<10000; i++) {
int val = r.nextInt();
byteBuf.putInt(0, val);
byteBuf.position(0);
byteBuf.get(buf);
boolean expectException = val < 0;
try {
int returnedVal = dexBuf.readSmallUint(0);
Assert.assertFalse(String.format("Didn't throw an exception for value: %x", val), expectException);
Assert.assertEquals(val, returnedVal);
} catch (Exception ex) {
Assert.assertTrue(String.format("Threw an exception for value: %x", val), expectException);
}
Assert.assertEquals(val, dexBuf.readInt(0));
Assert.assertEquals(val & 0xFFFF, dexBuf.readUshort(0));
Assert.assertEquals((val >> 8) & 0xFFFF, dexBuf.readUshort(1));
Assert.assertEquals((val >> 16) & 0xFFFF, dexBuf.readUshort(2));
Assert.assertEquals((short)val, dexBuf.readShort(0));
Assert.assertEquals((short)(val >> 8), dexBuf.readShort(1));
Assert.assertEquals((short)(val >> 16), dexBuf.readShort(2));
}
}
@Test
public void testReadLongRandom() {
Random r = new Random(1234567890);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(8).order(ByteOrder.LITTLE_ENDIAN);
byte[] buf = new byte[8];
DexBuffer dexBuf = new DexBuffer(buf);
for (int i=0; i<10000; i++) {
int val = r.nextInt();
byteBuf.putLong(0, val);
byteBuf.position(0);
byteBuf.get(buf);
Assert.assertEquals(val, dexBuf.readLong(0));
}
}
}

View File

@ -1,230 +0,0 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.dexbacked;
import junit.framework.Assert;
import org.jf.util.ExceptionWithContext;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;
public class DexFileBufferTest {
@Test
public void testReadSmallUintSuccess() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
Assert.assertEquals(0x44332211, dexFile.readSmallUint(0));
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexFile.readSmallUint(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(0x7fffffff, dexFile.readSmallUint(0));
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge1() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
dexFile.readSmallUint(0);
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge2() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
dexFile.readSmallUint(0);
}
@Test(expected=ExceptionWithContext.class)
public void testReadSmallUintTooLarge3() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
dexFile.readSmallUint(0);
}
@Test
public void testReadUshort() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x11, 0x22});
Assert.assertEquals(dexFile.readUshort(0), 0x2211);
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00});
Assert.assertEquals(dexFile.readUshort(0), 0);
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff});
Assert.assertEquals(dexFile.readUshort(0), 0xffff);
dexFile = new DexFileBuffer(new byte[] {(byte)0x00, (byte)0x80});
Assert.assertEquals(dexFile.readUshort(0), 0x8000);
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0x7f});
Assert.assertEquals(dexFile.readUshort(0), 0x7fff);
}
@Test
public void testReadUbyte() {
byte[] buf = new byte[1];
DexFileBuffer dexFile = new DexFileBuffer(buf);
for (int i=0; i<=0xff; i++) {
buf[0] = (byte)i;
Assert.assertEquals(i, dexFile.readUbyte(0));
}
}
@Test
public void testReadLong() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77});
Assert.assertEquals(0x7766554433221100L, dexFile.readLong(0));
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexFile.readLong(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(Long.MAX_VALUE, dexFile.readLong(0));
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80});
Assert.assertEquals(Long.MIN_VALUE, dexFile.readLong(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
Assert.assertEquals(0x80ffffffffffffffL, dexFile.readLong(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
Assert.assertEquals(-1, dexFile.readLong(0));
}
@Test
public void testReadInt() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
Assert.assertEquals(0x44332211, dexFile.readInt(0));
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
Assert.assertEquals(0, dexFile.readInt(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
Assert.assertEquals(Integer.MAX_VALUE, dexFile.readInt(0));
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
Assert.assertEquals(Integer.MIN_VALUE, dexFile.readInt(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
Assert.assertEquals(0x80ffffff, dexFile.readInt(0));
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
Assert.assertEquals(-1, dexFile.readInt(0));
}
@Test
public void testReadShort() {
DexFileBuffer dexFile = new DexFileBuffer(new byte[] {0x11, 0x22});
Assert.assertEquals(dexFile.readShort(0), 0x2211);
dexFile = new DexFileBuffer(new byte[] {0x00, 0x00});
Assert.assertEquals(dexFile.readShort(0), 0);
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0xff});
Assert.assertEquals(dexFile.readShort(0), -1);
dexFile = new DexFileBuffer(new byte[] {(byte)0x00, (byte)0x80});
Assert.assertEquals(dexFile.readShort(0), Short.MIN_VALUE);
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0x7f});
Assert.assertEquals(dexFile.readShort(0), 0x7fff);
dexFile = new DexFileBuffer(new byte[] {(byte)0xff, (byte)0x80});
Assert.assertEquals(dexFile.readShort(0), 0xffff80ff);
}
@Test
public void testReadByte() {
byte[] buf = new byte[1];
DexFileBuffer dexFile = new DexFileBuffer(buf);
for (int i=0; i<=0xff; i++) {
buf[0] = (byte)i;
Assert.assertEquals((byte)i, dexFile.readByte(0));
}
}
@Test
public void testReadRandom() {
Random r = new Random(1234567890);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(4).order(ByteOrder.LITTLE_ENDIAN);
byte[] buf = new byte[4];
DexFileBuffer dexFile = new DexFileBuffer(buf);
for (int i=0; i<10000; i++) {
int val = r.nextInt();
byteBuf.putInt(0, val);
byteBuf.position(0);
byteBuf.get(buf);
boolean expectException = val < 0;
try {
int returnedVal = dexFile.readSmallUint(0);
Assert.assertFalse(String.format("Didn't throw an exception for value: %x", val), expectException);
Assert.assertEquals(val, returnedVal);
} catch (Exception ex) {
Assert.assertTrue(String.format("Threw an exception for value: %x", val), expectException);
}
Assert.assertEquals(val, dexFile.readInt(0));
Assert.assertEquals(val & 0xFFFF, dexFile.readUshort(0));
Assert.assertEquals((val >> 8) & 0xFFFF, dexFile.readUshort(1));
Assert.assertEquals((val >> 16) & 0xFFFF, dexFile.readUshort(2));
Assert.assertEquals((short)val, dexFile.readShort(0));
Assert.assertEquals((short)(val >> 8), dexFile.readShort(1));
Assert.assertEquals((short)(val >> 16), dexFile.readShort(2));
}
}
@Test
public void testReadLongRandom() {
Random r = new Random(1234567890);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(8).order(ByteOrder.LITTLE_ENDIAN);
byte[] buf = new byte[8];
DexFileBuffer dexFile = new DexFileBuffer(buf);
for (int i=0; i<10000; i++) {
int val = r.nextInt();
byteBuf.putLong(0, val);
byteBuf.position(0);
byteBuf.get(buf);
Assert.assertEquals(val, dexFile.readLong(0));
}
}
}

View File

@ -246,19 +246,19 @@ public class DexFileReaderLeb128Test {
}
private void performTest(int expectedValue, byte[] buf, int expectedLength) {
DexFileBuffer dexFile = new DexFileBuffer(buf);
DexFileReader reader = dexFile.readerAt(0);
DexBuffer dexBuf = new DexBuffer(buf);
DexReader reader = dexBuf.readerAt(0);
Assert.assertEquals(expectedValue, reader.readSmallUleb128());
Assert.assertEquals(expectedLength, reader.getOffset());
reader = dexFile.readerAt(0);
reader = dexBuf.readerAt(0);
reader.skipUleb128();
Assert.assertEquals(expectedLength, reader.getOffset());
}
private void performFailureTest(byte[] buf) {
DexFileBuffer dexFile = new DexFileBuffer(buf);
DexFileReader reader = dexFile.readerAt(0);
DexBuffer dexBuf = new DexBuffer(buf);
DexReader reader = dexBuf.readerAt(0);
try {
reader.readSmallUleb128();
Assert.fail();

View File

@ -35,7 +35,7 @@ import junit.framework.Assert;
import org.jf.util.ExceptionWithContext;
import org.junit.Test;
public class DexFileReaderSleb128Test {
public class DexReaderSleb128Test {
@Test
public void testSleb128() {
performTest(0x0, new byte[]{0x0, 0x11}, 1);
@ -253,15 +253,15 @@ public class DexFileReaderSleb128Test {
}
private void performTest(int expectedValue, byte[] buf, int expectedLength) {
DexFileBuffer dexFile = new DexFileBuffer(buf);
DexFileReader reader = dexFile.readerAt(0);
DexBuffer dexBuf = new DexBuffer(buf);
DexReader reader = dexBuf.readerAt(0);
Assert.assertEquals(expectedValue, reader.readSleb128());
Assert.assertEquals(expectedLength, reader.getOffset());
}
private void performFailureTest(byte[] buf) {
DexFileBuffer dexFile = new DexFileBuffer(buf);
DexFileReader reader = dexFile.readerAt(0);
DexBuffer dexBuf = new DexBuffer(buf);
DexReader reader = dexBuf.readerAt(0);
try {
reader.readSleb128();
Assert.fail();