org.ow2.asmdex.lowLevelUtils
Class DexFileReader

java.lang.Object
  extended by org.ow2.asmdex.lowLevelUtils.BasicDexFileReader
      extended by org.ow2.asmdex.lowLevelUtils.DexFileReader
All Implemented Interfaces:
IDalvikValueReader

public class DexFileReader
extends BasicDexFileReader
implements IDalvikValueReader

Reads a DEX file, builds a representation of this file, and implements primitive Dalvik methods in order to conveniently parse the file structure. FIXME In order to optimize, the low level code is duplicated from DalvikValueReader to avoid indirection.

Author:
Julien Névo, based on the work by Pierre Crégut.

Field Summary
protected  int pos
          position
 
Fields inherited from class org.ow2.asmdex.lowLevelUtils.BasicDexFileReader
adler32Checksum, CLASS_DEF_ITEM_SIZE, classDefinitionsOffset, classDefinitionsSize, constantPoolStrings, constantPoolTypes, FIELD_ID_ITEM_SIZE, fieldIdsOffset, fieldIdsSize, FILE_SIZE_OFFSET, formatVersionNumber, HEADER_NOMINAL_SIZE, isStandardEndian, linkTableSize, METHOD_ID_ITEM_SIZE, methodIdsOffset, methodIdsSize, NO_INDEX, PROTO_ID_ITEM_SIZE, protoIdsOffset, protoIdsSize, reader, REVERSE_ENDIAN_VALUE, signature, STANDARD_ENDIAN_VALUE, STRING_ID_ITEM_SIZE, stringIdsOffset, stringIdsSize, TYPE_ID_ITEM_SIZE, typeIdsOffset, typeIdsSize
 
Constructor Summary
DexFileReader()
           
 
Method Summary
 void bytes(byte[] b)
          Reads a given number of bytes.
 long completeSignSizedLong(long l, int sz)
          Extends a long read with SizedLong of length sz according to its sign.
 java.util.HashMap<java.lang.Integer,java.lang.Integer> fillOffsetHashMap(int nbFields)
          Fill the returned HashMap with the index of the fields found in one of the structures which offset is given.
 int[] getAnnotationItemOffsetsFromAnnotationSetItem()
          Returns all the annotation item offsets of an annotation_set_item structure.
 ClassDefinitionItem getClassDefinitionItem(int classIndex)
          Returns a structure containing all the information of a class_def_item structure, which Class index is given.
 byte[] getContents()
          Returns the byte array included to this reader.
 java.lang.String getDescriptorFromPrototypeIndex(int prototypeIndex)
          Returns the descriptor of a prototype which id is given.
 FieldIdItem getFieldIdItem(int index)
          Gets a FieldIdItem structure containing the class_idx, type_idx and name_idx of the Field which index is given.
 MethodIdItem getMethodIdItem(int methodId)
          Returns a MethodIdItem structure containing the information related to the method_id_item whose index is given.
 java.lang.String getNameFromFieldIndex(int index)
          Gets the Name, third field of the field_it_item table, according to the field index given.
 java.lang.String getNameFromMethodIndex(int index)
          Gets the Name, third field of the method_id_item table, according to the method index given.
 int getPos()
          Get the current position of the pointer in the stream.
 java.lang.String getShortyStringFromProtoIndex(int index)
          Gets the String related to the shorty_idx item in the proto_id_item table, according to the proto index given.
 java.lang.String getStringItemFromStringIndex(int index)
          Gets the String related to string_id_item table, according to the string index given.
 java.lang.String getStringItemFromTypeIndex(int index)
          Gets the String related to the type_id_item table, linked to the string_id_item table, according to the type index given.
 int[] getTypeIdIndexesFromTypeList()
          Returns all the type_ids indexes of a type_list structure.
 java.lang.String getTypeNameFromFieldIndex(int index)
          Gets the Type Name, second field of the field_it_item table, linked to the type_id list, and according to the field index given.
 boolean hasMore()
          Check if there are still data to read in the stream.
 void parse(byte[] dexBytes)
          Takes an input stream corresponding to a Dex file and populate the structure.
 void relativeSeek(int offset)
          Adds an offset to the current position of the pointer in the stream.
 byte sbyte()
          Reads next signed byte value.
 void seek(int pos)
          Set the position of the pointer in the stream.
 int sint()
          Reads next signed integer value.
static int sint(java.io.InputStream stream)
          Reads an integer directly from an input stream.
 long sizedLong(int sz)
          Reads a long of a given size.
 void skipByte()
          Skips a Byte (signed or not).
 void skipInt()
          Skips an Int (signed or not).
 void skipShort()
          Skips a Short (signed or not).
 int sleb128()
          Reads next signed integer value coded in leb128 format
 short sshort()
          Reads next signed short value.
 short ubyte()
          Reads next unsigned byte value.
 int uint()
          Reads next unsigned integer value.
 long uleb128_16()
          Reads next unsigned integer value coded in leb128 format but with 16 bits chars.
 int uleb128_p1()
          Reads next unsigned integer value coded in leb128_p1 format
 int uleb128()
          Reads next unsigned integer value coded in leb128 format
 java.lang.String unicodeString(int strSize)
          Parse a string coded as 16 bit character
 int ushort()
          Reads next unsigned short value.
 java.lang.String utf8String()
          Reads a null terminated UTF8 string as handled by Dalvik (limited to unicode)
 
Methods inherited from class org.ow2.asmdex.lowLevelUtils.BasicDexFileReader
getClassDefinitionOffset, getClassDefinitionsOffset, getClassDefinitionsSize, getFieldIdsOffset, getFieldIdsSize, getFormatVersionNumber, getMethodIdsOffset, getMethodIdsSize, getOffsetFieldIdItem, getOffsetMethodIdItem, getOffsetProtoIdItem, getOffsetStringIdItem, getOffsetTypeIdItem, getProtoIdsOffset, getProtoIdsSize, getStringIdsOffset, getStringIdsSize, getTypeIdsOffset, getTypeIdsSize, isStandardEndian, readHeader, readStrings, readTypes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pos

protected int pos
position

Constructor Detail

DexFileReader

public DexFileReader()
Method Detail

parse

public void parse(byte[] dexBytes)
           throws java.io.IOException
Takes an input stream corresponding to a Dex file and populate the structure.

Overrides:
parse in class BasicDexFileReader
Parameters:
dexBytes - bytes of the Dex file to read.
Throws:
java.io.IOException

fillOffsetHashMap

public java.util.HashMap<java.lang.Integer,java.lang.Integer> fillOffsetHashMap(int nbFields)
Fill the returned HashMap with the index of the fields found in one of the structures which offset is given. These structures can be a field_annotation, a method_annotation, or a parameter_annotation. This method works because they all have the exact same structure. The dex file reader must point on the beginning of the list of annotations (field_annotation, method_annotation, or a parameter_annotation).

Parameters:
nbFields - count of fields.
Returns:
a map filled with the index linked to its offset.

getStringItemFromStringIndex

public java.lang.String getStringItemFromStringIndex(int index)
Gets the String related to string_id_item table, according to the string index given.

Parameters:
index - index of the string to find.
Returns:
the string linked to the string_id_item table, at the index given.

getStringItemFromTypeIndex

public java.lang.String getStringItemFromTypeIndex(int index)
Gets the String related to the type_id_item table, linked to the string_id_item table, according to the type index given. The dex file reader previous position is saved.

Parameters:
index - index of the string to find.
Returns:
the string linked to the type_id_item table, at the index given.

getNameFromMethodIndex

public java.lang.String getNameFromMethodIndex(int index)
Gets the Name, third field of the method_id_item table, according to the method index given. The dex file reader previous position is saved.

Parameters:
index - index of the name to find.
Returns:
the name of the index element of the method_id_item table.

getShortyStringFromProtoIndex

public java.lang.String getShortyStringFromProtoIndex(int index)
Gets the String related to the shorty_idx item in the proto_id_item table, according to the proto index given. The dex file reader previous position is saved.

Parameters:
index - index of the Prototype to find.
Returns:
the string linked to the shorty_idx item in the proto_id_item table, at the index given.

getNameFromFieldIndex

public java.lang.String getNameFromFieldIndex(int index)
Gets the Name, third field of the field_it_item table, according to the field index given. The dex file reader previous position is saved.

Parameters:
index - index of the name to find.
Returns:
the name of the index element of the field_id_item table.

getTypeNameFromFieldIndex

public java.lang.String getTypeNameFromFieldIndex(int index)
Gets the Type Name, second field of the field_it_item table, linked to the type_id list, and according to the field index given. The dex file reader previous position is saved.

Parameters:
index - index of the name to find.
Returns:
the type name of the index element of the field_id_item table.

getTypeIdIndexesFromTypeList

public int[] getTypeIdIndexesFromTypeList()
Returns all the type_ids indexes of a type_list structure. The dex file reader must point at the beginning of a type_list structure. The reader current position will be changed.

Returns:
an array containing all the indexes to the type_id of the type_list structure.

getAnnotationItemOffsetsFromAnnotationSetItem

public int[] getAnnotationItemOffsetsFromAnnotationSetItem()
Returns all the annotation item offsets of an annotation_set_item structure. The dex file reader must point at the beginning of an annotation_set_item structure. The reader current position will be changed.

Returns:
all the annotation item offsets of an annotation_set_item structure.

getDescriptorFromPrototypeIndex

public java.lang.String getDescriptorFromPrototypeIndex(int prototypeIndex)
Returns the descriptor of a prototype which id is given. The descriptor is constructed from the return type and parameters of the structure. The dex file reader position is saved.

Parameters:
prototypeIndex - index of the prototype.
Returns:
a String containing the descriptor of the prototype.

getMethodIdItem

public MethodIdItem getMethodIdItem(int methodId)
Returns a MethodIdItem structure containing the information related to the method_id_item whose index is given. The dex file reader position is not saved.

Parameters:
methodId - index of the Method.
Returns:
a MethodIdItem structure containing the information related to the method_id_item whose index is given.

getFieldIdItem

public FieldIdItem getFieldIdItem(int index)
Gets a FieldIdItem structure containing the class_idx, type_idx and name_idx of the Field which index is given. The dex file reader previous position is not saved.

Parameters:
index - index of the Field to find.
Returns:
a FieldIdItem structure.

getClassDefinitionItem

public ClassDefinitionItem getClassDefinitionItem(int classIndex)
Returns a structure containing all the information of a class_def_item structure, which Class index is given. The dex file reader position is saved.

Parameters:
classIndex - index of the Class.
Returns:
a structure containing all the information of a class_def_item structure.

sint

public static final int sint(java.io.InputStream stream)
                      throws java.io.IOException
Reads an integer directly from an input stream. Usually used while builder the value reader.

Parameters:
stream -
Returns:
an integer signed
Throws:
java.io.IOException

sbyte

public final byte sbyte()
Description copied from interface: IDalvikValueReader
Reads next signed byte value.

Specified by:
sbyte in interface IDalvikValueReader
Returns:
value read from stream

ubyte

public final short ubyte()
Description copied from interface: IDalvikValueReader
Reads next unsigned byte value.

Specified by:
ubyte in interface IDalvikValueReader
Returns:
value read from stream

sshort

public final short sshort()
Description copied from interface: IDalvikValueReader
Reads next signed short value.

Specified by:
sshort in interface IDalvikValueReader
Returns:
value read from stream

ushort

public final int ushort()
Description copied from interface: IDalvikValueReader
Reads next unsigned short value.

Specified by:
ushort in interface IDalvikValueReader
Returns:
value read from stream

sint

public final int sint()
Description copied from interface: IDalvikValueReader
Reads next signed integer value.

Specified by:
sint in interface IDalvikValueReader
Returns:
value read from stream

uint

public final int uint()
Description copied from interface: IDalvikValueReader
Reads next unsigned integer value.

Specified by:
uint in interface IDalvikValueReader
Returns:
value read from stream

sleb128

public final int sleb128()
Description copied from interface: IDalvikValueReader
Reads next signed integer value coded in leb128 format

Specified by:
sleb128 in interface IDalvikValueReader
Returns:
value read from stream

uleb128

public final int uleb128()
Description copied from interface: IDalvikValueReader
Reads next unsigned integer value coded in leb128 format

Specified by:
uleb128 in interface IDalvikValueReader
Returns:
value read from stream

uleb128_p1

public final int uleb128_p1()
Description copied from interface: IDalvikValueReader
Reads next unsigned integer value coded in leb128_p1 format

Specified by:
uleb128_p1 in interface IDalvikValueReader
Returns:
value read from stream

uleb128_16

public final long uleb128_16()
Description copied from interface: IDalvikValueReader
Reads next unsigned integer value coded in leb128 format but with 16 bits chars.

Specified by:
uleb128_16 in interface IDalvikValueReader
Returns:
value read from stream

sizedLong

public final long sizedLong(int sz)
Description copied from interface: IDalvikValueReader
Reads a long of a given size. Considered as unsigned.

Specified by:
sizedLong in interface IDalvikValueReader
Returns:
a positive long

completeSignSizedLong

public final long completeSignSizedLong(long l,
                                        int sz)
Extends a long read with SizedLong of length sz according to its sign.

Specified by:
completeSignSizedLong in interface IDalvikValueReader
Parameters:
l -
sz - size-1 of the encoded number.
Returns:
a long

bytes

public void bytes(byte[] b)
Reads a given number of bytes.

Parameters:
b -

utf8String

public java.lang.String utf8String()
Description copied from interface: IDalvikValueReader
Reads a null terminated UTF8 string as handled by Dalvik (limited to unicode)

Specified by:
utf8String in interface IDalvikValueReader
Returns:
string

seek

public final void seek(int pos)
Description copied from interface: IDalvikValueReader
Set the position of the pointer in the stream.

Specified by:
seek in interface IDalvikValueReader
Parameters:
pos - positive absolute position

relativeSeek

public void relativeSeek(int offset)
Description copied from interface: IDalvikValueReader
Adds an offset to the current position of the pointer in the stream.

Specified by:
relativeSeek in interface IDalvikValueReader
Parameters:
offset - positive or negative offset to add.

getPos

public final int getPos()
Description copied from interface: IDalvikValueReader
Get the current position of the pointer in the stream. Usually to make a save restore.

Specified by:
getPos in interface IDalvikValueReader
Returns:
positive absolute position

unicodeString

public java.lang.String unicodeString(int strSize)
Description copied from interface: IDalvikValueReader
Parse a string coded as 16 bit character

Specified by:
unicodeString in interface IDalvikValueReader
Returns:
string

hasMore

public final boolean hasMore()
Description copied from interface: IDalvikValueReader
Check if there are still data to read in the stream.

Specified by:
hasMore in interface IDalvikValueReader
Returns:
true if more data

skipInt

public final void skipInt()
Description copied from interface: IDalvikValueReader
Skips an Int (signed or not).

Specified by:
skipInt in interface IDalvikValueReader

skipShort

public final void skipShort()
Description copied from interface: IDalvikValueReader
Skips a Short (signed or not).

Specified by:
skipShort in interface IDalvikValueReader

skipByte

public final void skipByte()
Description copied from interface: IDalvikValueReader
Skips a Byte (signed or not).

Specified by:
skipByte in interface IDalvikValueReader

getContents

public byte[] getContents()
Description copied from interface: IDalvikValueReader
Returns the byte array included to this reader.

Specified by:
getContents in interface IDalvikValueReader
Returns:
the byte array included to this reader.