DEX文件解析 - type_ids解析

在上一篇中介绍了string_ids的解析,那么接下来就要学习type_ids的解析。

1. type_ids结构

androidaosp源码中,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

posted @ 2022-03-19 12:23  asjhan  阅读(7)  评论(0编辑  收藏  举报  来源