DEX文件解析 - type_ids解析
在上一篇中介绍了string_ids的解析,那么接下来就要学习type_ids
的解析。
1. type_ids结构
在android
的aosp
源码中,type_ids
的结构如下:
aosp
源码位置:art/libdexfile/dex/dex_file.h
TypeId
// Raw type_id_item.
struct TypeId {
dex::StringIndex descriptor_idx_; // index into string_ids
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
/**
* 解析TypeIds
* @param raf
* @return
*/
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);
//创建TypeIds实体类
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
分类:
Android常见格式解析
标签:
格式解析
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!