在上一篇中介绍了string_ids的解析,那么接下来就要学习type_ids 的解析。
1. type_ids结构
在android 的aosp 源码中,type_ids 的结构如下: aosp 源码位置:art/libdexfile/dex/dex_file.h
TypeId
struct TypeId {
dex::StringIndex descriptor_idx_;
private:
DISALLOW_COPY_AND_ASSIGN(TypeId);
};
StringIndex
class StringIndex {
public:
uint32_t index_;
....
....
};
TypeId 的结构,存储着一个无符号的4字节数据(StringIndex 对应的是无符号的int 类型),descriptor_idx_ 对应的是字符串索引列表 的索引/下标
2. 010Editor解析
如图,仔细观察
3. type_ids解析
java 语言解析type_ids
private static List<TypeId> toParseDexTypeIds(RandomAccessFile raf) {
try {
List<TypeId> typeIdList = new ArrayList<>();
int type_ids_off = mDexFile.getType_ids_off();
int type_ids_size = mDexFile.getType_ids_size();
raf.seek(type_ids_off);
for (int i=0;i<type_ids_size;i++) {
byte[] type_string_index_bytes = readData(raf, 4);
int type_string_index = NumConversion.byteToInt(type_string_index_bytes, false);
StringId stringId = mStringIds.get(type_string_index);
String TypeData = new String(stringId.getData());
System.out.println("索引:"+i+" 索引对应的数据:"+TypeData);
TypeId typeId = new TypeId();
typeId.setTypeDescriptorIdx(type_string_index);
typeIdList.add(typeId);
}
return typeIdList;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static byte[] readData(RandomAccessFile raf,int limit) {
byte[] buff = new byte[limit];
try {
raf.read(buff);
} catch (IOException e) {
e.printStackTrace();
}
return buff;
}
实体类TypeId
public class TypeId {
private int typeDescriptorIdx;
public int getTypeDescriptorIdx() {
return typeDescriptorIdx;
}
public void setTypeDescriptorIdx(int typeDescriptorIdx) {
this.typeDescriptorIdx = typeDescriptorIdx;
}
}
工具类NumConversion
public class NumConversion {
public static int byteToInt(byte[] bytes,boolean isBigEndian) {
if (bytes.length <=0 || bytes.length > 4) return -1;
int result = 0;
for (int i=0;i<bytes.length;i++) {
int b ;
if(isBigEndian){
b = (bytes[i] & 0xFF) << (8*(bytes.length-1-i));
}else {
b = (bytes[i] & 0xFF) << (8*i);
}
result = result | b;
}
return result;
}
}
asjhan for Android reverse
|