Home | History | Annotate | Download | only in libdex
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  * Access .dex (Dalvik Executable Format) files.  The code here assumes that
     19  * the DEX file has been rewritten (byte-swapped, word-aligned) and that
     20  * the contents can be directly accessed as a collection of C arrays.  Please
     21  * see docs/dalvik/dex-format.html for a detailed description.
     22  *
     23  * The structure and field names were chosen to match those in the DEX spec.
     24  *
     25  * It's generally assumed that the DEX file will be stored in shared memory,
     26  * obviating the need to copy code and constant pool entries into newly
     27  * allocated storage.  Maintaining local pointers to items in the shared area
     28  * is valid and encouraged.
     29  *
     30  * All memory-mapped structures are 32-bit aligned unless otherwise noted.
     31  */
     32 
     33 #ifndef _LIBDEX_DEXFILE
     34 #define _LIBDEX_DEXFILE
     35 
     36 #include "vm/Common.h"      // basic type defs, e.g. u1/u2/u4/u8, and LOG
     37 #include "libdex/SysUtil.h"
     38 
     39 /*
     40  * gcc-style inline management -- ensures we have a copy of all functions
     41  * in the library, so code that links against us will work whether or not
     42  * it was built with optimizations enabled.
     43  */
     44 #ifndef _DEX_GEN_INLINES             /* only defined by DexInlines.c */
     45 # define DEX_INLINE extern __inline__
     46 #else
     47 # define DEX_INLINE
     48 #endif
     49 
     50 /* DEX file magic number */
     51 #define DEX_MAGIC       "dex\n"
     52 /* version, encoded in 4 bytes of ASCII */
     53 #define DEX_MAGIC_VERS  "035\0"
     54 
     55 /* same, but for optimized DEX header */
     56 #define DEX_OPT_MAGIC   "dey\n"
     57 #define DEX_OPT_MAGIC_VERS  "036\0"
     58 
     59 #define DEX_DEP_MAGIC   "deps"
     60 
     61 /*
     62  * 160-bit SHA-1 digest.
     63  */
     64 enum { kSHA1DigestLen = 20,
     65        kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };
     66 
     67 /* general constants */
     68 enum {
     69     kDexEndianConstant = 0x12345678,    /* the endianness indicator */
     70     kDexNoIndex = 0xffffffff,           /* not a valid index value */
     71 };
     72 
     73 /*
     74  * access flags and masks; the "standard" ones are all <= 0x4000
     75  *
     76  * Note: There are related declarations in vm/oo/Object.h in the ClassFlags
     77  * enum.
     78  */
     79 enum {
     80     ACC_PUBLIC       = 0x00000001,       // class, field, method, ic
     81     ACC_PRIVATE      = 0x00000002,       // field, method, ic
     82     ACC_PROTECTED    = 0x00000004,       // field, method, ic
     83     ACC_STATIC       = 0x00000008,       // field, method, ic
     84     ACC_FINAL        = 0x00000010,       // class, field, method, ic
     85     ACC_SYNCHRONIZED = 0x00000020,       // method (only allowed on natives)
     86     ACC_SUPER        = 0x00000020,       // class (not used in Dalvik)
     87     ACC_VOLATILE     = 0x00000040,       // field
     88     ACC_BRIDGE       = 0x00000040,       // method (1.5)
     89     ACC_TRANSIENT    = 0x00000080,       // field
     90     ACC_VARARGS      = 0x00000080,       // method (1.5)
     91     ACC_NATIVE       = 0x00000100,       // method
     92     ACC_INTERFACE    = 0x00000200,       // class, ic
     93     ACC_ABSTRACT     = 0x00000400,       // class, method, ic
     94     ACC_STRICT       = 0x00000800,       // method
     95     ACC_SYNTHETIC    = 0x00001000,       // field, method, ic
     96     ACC_ANNOTATION   = 0x00002000,       // class, ic (1.5)
     97     ACC_ENUM         = 0x00004000,       // class, field, ic (1.5)
     98     ACC_CONSTRUCTOR  = 0x00010000,       // method (Dalvik only)
     99     ACC_DECLARED_SYNCHRONIZED =
    100                        0x00020000,       // method (Dalvik only)
    101     ACC_CLASS_MASK =
    102         (ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT
    103                 | ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
    104     ACC_INNER_CLASS_MASK =
    105         (ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),
    106     ACC_FIELD_MASK =
    107         (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
    108                 | ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
    109     ACC_METHOD_MASK =
    110         (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
    111                 | ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE
    112                 | ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR
    113                 | ACC_DECLARED_SYNCHRONIZED),
    114 };
    115 
    116 /* annotation constants */
    117 enum {
    118     kDexVisibilityBuild         = 0x00,     /* annotation visibility */
    119     kDexVisibilityRuntime       = 0x01,
    120     kDexVisibilitySystem        = 0x02,
    121 
    122     kDexAnnotationByte          = 0x00,
    123     kDexAnnotationShort         = 0x02,
    124     kDexAnnotationChar          = 0x03,
    125     kDexAnnotationInt           = 0x04,
    126     kDexAnnotationLong          = 0x06,
    127     kDexAnnotationFloat         = 0x10,
    128     kDexAnnotationDouble        = 0x11,
    129     kDexAnnotationString        = 0x17,
    130     kDexAnnotationType          = 0x18,
    131     kDexAnnotationField         = 0x19,
    132     kDexAnnotationMethod        = 0x1a,
    133     kDexAnnotationEnum          = 0x1b,
    134     kDexAnnotationArray         = 0x1c,
    135     kDexAnnotationAnnotation    = 0x1d,
    136     kDexAnnotationNull          = 0x1e,
    137     kDexAnnotationBoolean       = 0x1f,
    138 
    139     kDexAnnotationValueTypeMask = 0x1f,     /* low 5 bits */
    140     kDexAnnotationValueArgShift = 5,
    141 };
    142 
    143 /* map item type codes */
    144 enum {
    145     kDexTypeHeaderItem               = 0x0000,
    146     kDexTypeStringIdItem             = 0x0001,
    147     kDexTypeTypeIdItem               = 0x0002,
    148     kDexTypeProtoIdItem              = 0x0003,
    149     kDexTypeFieldIdItem              = 0x0004,
    150     kDexTypeMethodIdItem             = 0x0005,
    151     kDexTypeClassDefItem             = 0x0006,
    152     kDexTypeMapList                  = 0x1000,
    153     kDexTypeTypeList                 = 0x1001,
    154     kDexTypeAnnotationSetRefList     = 0x1002,
    155     kDexTypeAnnotationSetItem        = 0x1003,
    156     kDexTypeClassDataItem            = 0x2000,
    157     kDexTypeCodeItem                 = 0x2001,
    158     kDexTypeStringDataItem           = 0x2002,
    159     kDexTypeDebugInfoItem            = 0x2003,
    160     kDexTypeAnnotationItem           = 0x2004,
    161     kDexTypeEncodedArrayItem         = 0x2005,
    162     kDexTypeAnnotationsDirectoryItem = 0x2006,
    163 };
    164 
    165 /* auxillary data section chunk codes */
    166 enum {
    167     kDexChunkClassLookup            = 0x434c4b50,   /* CLKP */
    168     kDexChunkRegisterMaps           = 0x524d4150,   /* RMAP */
    169 
    170     kDexChunkEnd                    = 0x41454e44,   /* AEND */
    171 };
    172 
    173 /* debug info opcodes and constants */
    174 enum {
    175     DBG_END_SEQUENCE         = 0x00,
    176     DBG_ADVANCE_PC           = 0x01,
    177     DBG_ADVANCE_LINE         = 0x02,
    178     DBG_START_LOCAL          = 0x03,
    179     DBG_START_LOCAL_EXTENDED = 0x04,
    180     DBG_END_LOCAL            = 0x05,
    181     DBG_RESTART_LOCAL        = 0x06,
    182     DBG_SET_PROLOGUE_END     = 0x07,
    183     DBG_SET_EPILOGUE_BEGIN   = 0x08,
    184     DBG_SET_FILE             = 0x09,
    185     DBG_FIRST_SPECIAL        = 0x0a,
    186     DBG_LINE_BASE            = -4,
    187     DBG_LINE_RANGE           = 15,
    188 };
    189 
    190 /*
    191  * Direct-mapped "header_item" struct.
    192  */
    193 typedef struct DexHeader {
    194     u1  magic[8];           /* includes version number */
    195     u4  checksum;           /* adler32 checksum */
    196     u1  signature[kSHA1DigestLen]; /* SHA-1 hash */
    197     u4  fileSize;           /* length of entire file */
    198     u4  headerSize;         /* offset to start of next section */
    199     u4  endianTag;
    200     u4  linkSize;
    201     u4  linkOff;
    202     u4  mapOff;
    203     u4  stringIdsSize;
    204     u4  stringIdsOff;
    205     u4  typeIdsSize;
    206     u4  typeIdsOff;
    207     u4  protoIdsSize;
    208     u4  protoIdsOff;
    209     u4  fieldIdsSize;
    210     u4  fieldIdsOff;
    211     u4  methodIdsSize;
    212     u4  methodIdsOff;
    213     u4  classDefsSize;
    214     u4  classDefsOff;
    215     u4  dataSize;
    216     u4  dataOff;
    217 } DexHeader;
    218 
    219 /*
    220  * Direct-mapped "map_item".
    221  */
    222 typedef struct DexMapItem {
    223     u2  type;              /* type code (see kDexType* above) */
    224     u2  unused;
    225     u4  size;              /* count of items of the indicated type */
    226     u4  offset;            /* file offset to the start of data */
    227 } DexMapItem;
    228 
    229 /*
    230  * Direct-mapped "map_list".
    231  */
    232 typedef struct DexMapList {
    233     u4  size;               /* #of entries in list */
    234     DexMapItem list[1];     /* entries */
    235 } DexMapList;
    236 
    237 /*
    238  * Direct-mapped "string_id_item".
    239  */
    240 typedef struct DexStringId {
    241     u4  stringDataOff;      /* file offset to string_data_item */
    242 } DexStringId;
    243 
    244 /*
    245  * Direct-mapped "type_id_item".
    246  */
    247 typedef struct DexTypeId {
    248     u4  descriptorIdx;      /* index into stringIds list for type descriptor */
    249 } DexTypeId;
    250 
    251 /*
    252  * Direct-mapped "field_id_item".
    253  */
    254 typedef struct DexFieldId {
    255     u2  classIdx;           /* index into typeIds list for defining class */
    256     u2  typeIdx;            /* index into typeIds for field type */
    257     u4  nameIdx;            /* index into stringIds for field name */
    258 } DexFieldId;
    259 
    260 /*
    261  * Direct-mapped "method_id_item".
    262  */
    263 typedef struct DexMethodId {
    264     u2  classIdx;           /* index into typeIds list for defining class */
    265     u2  protoIdx;           /* index into protoIds for method prototype */
    266     u4  nameIdx;            /* index into stringIds for method name */
    267 } DexMethodId;
    268 
    269 /*
    270  * Direct-mapped "proto_id_item".
    271  */
    272 typedef struct DexProtoId {
    273     u4  shortyIdx;          /* index into stringIds for shorty descriptor */
    274     u4  returnTypeIdx;      /* index into typeIds list for return type */
    275     u4  parametersOff;      /* file offset to type_list for parameter types */
    276 } DexProtoId;
    277 
    278 /*
    279  * Direct-mapped "class_def_item".
    280  */
    281 typedef struct DexClassDef {
    282     u4  classIdx;           /* index into typeIds for this class */
    283     u4  accessFlags;
    284     u4  superclassIdx;      /* index into typeIds for superclass */
    285     u4  interfacesOff;      /* file offset to DexTypeList */
    286     u4  sourceFileIdx;      /* index into stringIds for source file name */
    287     u4  annotationsOff;     /* file offset to annotations_directory_item */
    288     u4  classDataOff;       /* file offset to class_data_item */
    289     u4  staticValuesOff;    /* file offset to DexEncodedArray */
    290 } DexClassDef;
    291 
    292 /*
    293  * Direct-mapped "type_item".
    294  */
    295 typedef struct DexTypeItem {
    296     u2  typeIdx;            /* index into typeIds */
    297 } DexTypeItem;
    298 
    299 /*
    300  * Direct-mapped "type_list".
    301  */
    302 typedef struct DexTypeList {
    303     u4  size;               /* #of entries in list */
    304     DexTypeItem list[1];    /* entries */
    305 } DexTypeList;
    306 
    307 /*
    308  * Direct-mapped "code_item".
    309  *
    310  * The "catches" table is used when throwing an exception,
    311  * "debugInfo" is used when displaying an exception stack trace or
    312  * debugging. An offset of zero indicates that there are no entries.
    313  */
    314 typedef struct DexCode {
    315     u2  registersSize;
    316     u2  insSize;
    317     u2  outsSize;
    318     u2  triesSize;
    319     u4  debugInfoOff;       /* file offset to debug info stream */
    320     u4  insnsSize;          /* size of the insns array, in u2 units */
    321     u2  insns[1];
    322     /* followed by optional u2 padding */
    323     /* followed by try_item[triesSize] */
    324     /* followed by uleb128 handlersSize */
    325     /* followed by catch_handler_item[handlersSize] */
    326 } DexCode;
    327 
    328 /*
    329  * Direct-mapped "try_item".
    330  */
    331 typedef struct DexTry {
    332     u4  startAddr;          /* start address, in 16-bit code units */
    333     u2  insnCount;          /* instruction count, in 16-bit code units */
    334     u2  handlerOff;         /* offset in encoded handler data to handlers */
    335 } DexTry;
    336 
    337 /*
    338  * Link table.  Currently undefined.
    339  */
    340 typedef struct DexLink {
    341     u1  bleargh;
    342 } DexLink;
    343 
    344 
    345 /*
    346  * Direct-mapped "annotations_directory_item".
    347  */
    348 typedef struct DexAnnotationsDirectoryItem {
    349     u4  classAnnotationsOff;  /* offset to DexAnnotationSetItem */
    350     u4  fieldsSize;           /* count of DexFieldAnnotationsItem */
    351     u4  methodsSize;          /* count of DexMethodAnnotationsItem */
    352     u4  parametersSize;       /* count of DexParameterAnnotationsItem */
    353     /* followed by DexFieldAnnotationsItem[fieldsSize] */
    354     /* followed by DexMethodAnnotationsItem[methodsSize] */
    355     /* followed by DexParameterAnnotationsItem[parametersSize] */
    356 } DexAnnotationsDirectoryItem;
    357 
    358 /*
    359  * Direct-mapped "field_annotations_item".
    360  */
    361 typedef struct DexFieldAnnotationsItem {
    362     u4  fieldIdx;
    363     u4  annotationsOff;             /* offset to DexAnnotationSetItem */
    364 } DexFieldAnnotationsItem;
    365 
    366 /*
    367  * Direct-mapped "method_annotations_item".
    368  */
    369 typedef struct DexMethodAnnotationsItem {
    370     u4  methodIdx;
    371     u4  annotationsOff;             /* offset to DexAnnotationSetItem */
    372 } DexMethodAnnotationsItem;
    373 
    374 /*
    375  * Direct-mapped "parameter_annotations_item".
    376  */
    377 typedef struct DexParameterAnnotationsItem {
    378     u4  methodIdx;
    379     u4  annotationsOff;             /* offset to DexAnotationSetRefList */
    380 } DexParameterAnnotationsItem;
    381 
    382 /*
    383  * Direct-mapped "annotation_set_ref_item".
    384  */
    385 typedef struct DexAnnotationSetRefItem {
    386     u4  annotationsOff;             /* offset to DexAnnotationSetItem */
    387 } DexAnnotationSetRefItem;
    388 
    389 /*
    390  * Direct-mapped "annotation_set_ref_list".
    391  */
    392 typedef struct DexAnnotationSetRefList {
    393     u4  size;
    394     DexAnnotationSetRefItem list[1];
    395 } DexAnnotationSetRefList;
    396 
    397 /*
    398  * Direct-mapped "anotation_set_item".
    399  */
    400 typedef struct DexAnnotationSetItem {
    401     u4  size;
    402     u4  entries[1];                 /* offset to DexAnnotationItem */
    403 } DexAnnotationSetItem;
    404 
    405 /*
    406  * Direct-mapped "annotation_item".
    407  *
    408  * NOTE: this structure is byte-aligned.
    409  */
    410 typedef struct DexAnnotationItem {
    411     u1  visibility;
    412     u1  annotation[1];              /* data in encoded_annotation format */
    413 } DexAnnotationItem;
    414 
    415 /*
    416  * Direct-mapped "encoded_array".
    417  *
    418  * NOTE: this structure is byte-aligned.
    419  */
    420 typedef struct DexEncodedArray {
    421     u1  array[1];                   /* data in encoded_array format */
    422 } DexEncodedArray;
    423 
    424 /*
    425  * Lookup table for classes.  It provides a mapping from class name to
    426  * class definition.  Used by dexFindClass().
    427  *
    428  * We calculate this at DEX optimization time and embed it in the file so we
    429  * don't need the same hash table in every VM.  This is slightly slower than
    430  * a hash table with direct pointers to the items, but because it's shared
    431  * there's less of a penalty for using a fairly sparse table.
    432  */
    433 typedef struct DexClassLookup {
    434     int     size;                       // total size, including "size"
    435     int     numEntries;                 // size of table[]; always power of 2
    436     struct {
    437         u4      classDescriptorHash;    // class descriptor hash code
    438         int     classDescriptorOffset;  // in bytes, from start of DEX
    439         int     classDefOffset;         // in bytes, from start of DEX
    440     } table[1];
    441 } DexClassLookup;
    442 
    443 /*
    444  * Header added by DEX optimization pass.  Values are always written in
    445  * local byte and structure padding.  The first field (magic + version)
    446  * is guaranteed to be present and directly readable for all expected
    447  * compiler configurations; the rest is version-dependent.
    448  *
    449  * Try to keep this simple and fixed-size.
    450  */
    451 typedef struct DexOptHeader {
    452     u1  magic[8];           /* includes version number */
    453 
    454     u4  dexOffset;          /* file offset of DEX header */
    455     u4  dexLength;
    456     u4  depsOffset;         /* offset of optimized DEX dependency table */
    457     u4  depsLength;
    458     u4  optOffset;          /* file offset of optimized data tables */
    459     u4  optLength;
    460 
    461     u4  flags;              /* some info flags */
    462     u4  checksum;           /* adler32 checksum covering deps/opt */
    463 
    464     /* pad for 64-bit alignment if necessary */
    465 } DexOptHeader;
    466 
    467 #define DEX_FLAG_VERIFIED           (1)     /* tried to verify all classes */
    468 #define DEX_OPT_FLAG_BIG            (1<<1)  /* swapped to big-endian */
    469 #define DEX_OPT_FLAG_FIELDS         (1<<2)  /* field access optimized */
    470 #define DEX_OPT_FLAG_INVOCATIONS    (1<<3)  /* method calls optimized */
    471 
    472 #define DEX_INTERFACE_CACHE_SIZE    128     /* must be power of 2 */
    473 
    474 /*
    475  * Structure representing a DEX file.
    476  *
    477  * Code should regard DexFile as opaque, using the API calls provided here
    478  * to access specific structures.
    479  */
    480 typedef struct DexFile {
    481     /* directly-mapped "opt" header */
    482     const DexOptHeader* pOptHeader;
    483 
    484     /* pointers to directly-mapped structs and arrays in base DEX */
    485     const DexHeader*    pHeader;
    486     const DexStringId*  pStringIds;
    487     const DexTypeId*    pTypeIds;
    488     const DexFieldId*   pFieldIds;
    489     const DexMethodId*  pMethodIds;
    490     const DexProtoId*   pProtoIds;
    491     const DexClassDef*  pClassDefs;
    492     const DexLink*      pLinkData;
    493 
    494     /*
    495      * These are mapped out of the "auxillary" section, and may not be
    496      * included in the file.
    497      */
    498     const DexClassLookup* pClassLookup;
    499     const void*         pRegisterMapPool;       // RegisterMapClassPool
    500 
    501     /* points to start of DEX file data */
    502     const u1*           baseAddr;
    503 
    504     /* track memory overhead for auxillary structures */
    505     int                 overhead;
    506 
    507     /* additional app-specific data structures associated with the DEX */
    508     //void*               auxData;
    509 } DexFile;
    510 
    511 /*
    512  * Utility function -- rounds up to the nearest power of 2.
    513  */
    514 u4 dexRoundUpPower2(u4 val);
    515 
    516 /*
    517  * Parse an optimized or unoptimized .dex file sitting in memory.
    518  *
    519  * On success, return a newly-allocated DexFile.
    520  */
    521 DexFile* dexFileParse(const u1* data, size_t length, int flags);
    522 
    523 /* bit values for "flags" argument to dexFileParse */
    524 enum {
    525     kDexParseDefault            = 0,
    526     kDexParseVerifyChecksum     = 1,
    527     kDexParseContinueOnError    = (1 << 1),
    528 };
    529 
    530 /*
    531  * Fix the byte ordering of all fields in the DEX file, and do
    532  * structural verification. This is only required for code that opens
    533  * "raw" DEX files, such as the DEX optimizer.
    534  *
    535  * Return 0 on success.
    536  */
    537 int dexSwapAndVerify(u1* addr, int len);
    538 
    539 /*
    540  * Detect the file type of the given memory buffer via magic number.
    541  * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
    542  * but return successfully on an optimized DEX file, and report an
    543  * error for all other cases.
    544  *
    545  * Return 0 on success.
    546  */
    547 int dexSwapAndVerifyIfNecessary(u1* addr, int len);
    548 
    549 /*
    550  * Compute DEX checksum.
    551  */
    552 u4 dexComputeChecksum(const DexHeader* pHeader);
    553 
    554 /*
    555  * Free a DexFile structure, along with any associated structures.
    556  */
    557 void dexFileFree(DexFile* pDexFile);
    558 
    559 /*
    560  * Create class lookup table.
    561  */
    562 DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
    563 
    564 /*
    565  * Find a class definition by descriptor.
    566  */
    567 const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);
    568 
    569 /*
    570  * Set up the basic raw data pointers of a DexFile. This function isn't
    571  * meant for general use.
    572  */
    573 void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
    574 
    575 /* return the DexMapList of the file, if any */
    576 DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
    577     u4 mapOff = pDexFile->pHeader->mapOff;
    578 
    579     if (mapOff == 0) {
    580         return NULL;
    581     } else {
    582         return (const DexMapList*) (pDexFile->baseAddr + mapOff);
    583     }
    584 }
    585 
    586 /* return the const char* string data referred to by the given string_id */
    587 DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
    588         const DexStringId* pStringId) {
    589     const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;
    590 
    591     // Skip the uleb128 length.
    592     while (*(ptr++) > 0x7f) /* empty */ ;
    593 
    594     return (const char*) ptr;
    595 }
    596 /* return the StringId with the specified index */
    597 DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
    598     assert(idx < pDexFile->pHeader->stringIdsSize);
    599     return &pDexFile->pStringIds[idx];
    600 }
    601 /* return the UTF-8 encoded string with the specified string_id index */
    602 DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
    603     const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
    604     return dexGetStringData(pDexFile, pStringId);
    605 }
    606 
    607 /* Return the UTF-8 encoded string with the specified string_id index,
    608  * also filling in the UTF-16 size (number of 16-bit code points).*/
    609 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
    610         u4* utf16Size);
    611 
    612 /* return the TypeId with the specified index */
    613 DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
    614     assert(idx < pDexFile->pHeader->typeIdsSize);
    615     return &pDexFile->pTypeIds[idx];
    616 }
    617 
    618 /*
    619  * Get the descriptor string associated with a given type index.
    620  * The caller should not free() the returned string.
    621  */
    622 DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
    623     const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
    624     return dexStringById(pDexFile, typeId->descriptorIdx);
    625 }
    626 
    627 /* return the MethodId with the specified index */
    628 DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
    629     assert(idx < pDexFile->pHeader->methodIdsSize);
    630     return &pDexFile->pMethodIds[idx];
    631 }
    632 
    633 /* return the FieldId with the specified index */
    634 DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
    635     assert(idx < pDexFile->pHeader->fieldIdsSize);
    636     return &pDexFile->pFieldIds[idx];
    637 }
    638 
    639 /* return the ProtoId with the specified index */
    640 DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
    641     assert(idx < pDexFile->pHeader->protoIdsSize);
    642     return &pDexFile->pProtoIds[idx];
    643 }
    644 
    645 /*
    646  * Get the parameter list from a ProtoId. The returns NULL if the ProtoId
    647  * does not have a parameter list.
    648  */
    649 DEX_INLINE const DexTypeList* dexGetProtoParameters(
    650     const DexFile *pDexFile, const DexProtoId* pProtoId) {
    651     if (pProtoId->parametersOff == 0) {
    652         return NULL;
    653     }
    654     return (const DexTypeList*)
    655         (pDexFile->baseAddr + pProtoId->parametersOff);
    656 }
    657 
    658 /* return the ClassDef with the specified index */
    659 DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
    660     assert(idx < pDexFile->pHeader->classDefsSize);
    661     return &pDexFile->pClassDefs[idx];
    662 }
    663 
    664 /* given a ClassDef pointer, recover its index */
    665 DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
    666     const DexClassDef* pClassDef)
    667 {
    668     assert(pClassDef >= pDexFile->pClassDefs &&
    669            pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
    670     return pClassDef - pDexFile->pClassDefs;
    671 }
    672 
    673 /* get the interface list for a DexClass */
    674 DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
    675     const DexClassDef* pClassDef)
    676 {
    677     if (pClassDef->interfacesOff == 0)
    678         return NULL;
    679     return (const DexTypeList*)
    680         (pDexFile->baseAddr + pClassDef->interfacesOff);
    681 }
    682 /* return the Nth entry in a DexTypeList. */
    683 DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
    684     u4 idx)
    685 {
    686     assert(idx < pList->size);
    687     return &pList->list[idx];
    688 }
    689 /* return the type_idx for the Nth entry in a TypeList */
    690 DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
    691     const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
    692     return pItem->typeIdx;
    693 }
    694 
    695 /* get the static values list for a DexClass */
    696 DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
    697     const DexFile* pDexFile, const DexClassDef* pClassDef)
    698 {
    699     if (pClassDef->staticValuesOff == 0)
    700         return NULL;
    701     return (const DexEncodedArray*)
    702         (pDexFile->baseAddr + pClassDef->staticValuesOff);
    703 }
    704 
    705 /* get the annotations directory item for a DexClass */
    706 DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
    707     const DexFile* pDexFile, const DexClassDef* pClassDef)
    708 {
    709     if (pClassDef->annotationsOff == 0)
    710         return NULL;
    711     return (const DexAnnotationsDirectoryItem*)
    712         (pDexFile->baseAddr + pClassDef->annotationsOff);
    713 }
    714 
    715 /* get the source file string */
    716 DEX_INLINE const char* dexGetSourceFile(
    717     const DexFile* pDexFile, const DexClassDef* pClassDef)
    718 {
    719     if (pClassDef->sourceFileIdx == 0xffffffff)
    720         return NULL;
    721     return dexStringById(pDexFile, pClassDef->sourceFileIdx);
    722 }
    723 
    724 /* get the size, in bytes, of a DexCode */
    725 size_t dexGetDexCodeSize(const DexCode* pCode);
    726 
    727 /* Get the list of "tries" for the given DexCode. */
    728 DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
    729     const u2* insnsEnd = &pCode->insns[pCode->insnsSize];
    730 
    731     // Round to four bytes.
    732     if ((((u4) insnsEnd) & 3) != 0) {
    733         insnsEnd++;
    734     }
    735 
    736     return (const DexTry*) insnsEnd;
    737 }
    738 
    739 /* Get the base of the encoded data for the given DexCode. */
    740 DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
    741     const DexTry* pTries = dexGetTries(pCode);
    742     return (const u1*) &pTries[pCode->triesSize];
    743 }
    744 
    745 /* get a pointer to the start of the debugging data */
    746 DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
    747     const DexCode* pCode)
    748 {
    749     if (pCode->debugInfoOff == 0) {
    750         return NULL;
    751     } else {
    752         return pDexFile->baseAddr + pCode->debugInfoOff;
    753     }
    754 }
    755 
    756 /*
    757  * Callback for "new position table entry".
    758  * Returning non-0 causes the decoder to stop early.
    759  */
    760 typedef int (*DexDebugNewPositionCb)(void *cnxt, u4 address, u4 lineNum);
    761 
    762 /*
    763  * Callback for "new locals table entry". "signature" is an empty string
    764  * if no signature is available for an entry.
    765  */
    766 typedef void (*DexDebugNewLocalCb)(void *cnxt, u2 reg, u4 startAddress,
    767         u4 endAddress, const char *name, const char *descriptor,
    768         const char *signature);
    769 
    770 /*
    771  * Decode debug info for method.
    772  *
    773  * posCb is called in ascending address order.
    774  * localCb is called in order of ascending end address.
    775  */
    776 void dexDecodeDebugInfo(
    777             const DexFile* pDexFile,
    778             const DexCode* pDexCode,
    779             const char* classDescriptor,
    780             u4 protoIdx,
    781             u4 accessFlags,
    782             DexDebugNewPositionCb posCb, DexDebugNewLocalCb localCb,
    783             void* cnxt);
    784 
    785 /* DexClassDef convenience - get class descriptor */
    786 DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
    787     const DexClassDef* pClassDef)
    788 {
    789     return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
    790 }
    791 
    792 /* DexClassDef convenience - get superclass descriptor */
    793 DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
    794     const DexClassDef* pClassDef)
    795 {
    796     if (pClassDef->superclassIdx == 0)
    797         return NULL;
    798     return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
    799 }
    800 
    801 /* DexClassDef convenience - get class_data_item pointer */
    802 DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
    803     const DexClassDef* pClassDef)
    804 {
    805     if (pClassDef->classDataOff == 0)
    806         return NULL;
    807     return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
    808 }
    809 
    810 /* Get an annotation set at a particular offset. */
    811 DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
    812     const DexFile* pDexFile, u4 offset)
    813 {
    814     return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
    815 }
    816 /* get the class' annotation set */
    817 DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
    818     const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
    819 {
    820     if (pAnnoDir->classAnnotationsOff == 0)
    821         return NULL;
    822     return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
    823 }
    824 
    825 /* get the class' field annotation list */
    826 DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
    827     const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
    828 {
    829     if (pAnnoDir->fieldsSize == 0)
    830         return NULL;
    831 
    832     // Skip past the header to the start of the field annotations.
    833     return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
    834 }
    835 
    836 /* get field annotation list size */
    837 DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
    838     const DexAnnotationsDirectoryItem* pAnnoDir)
    839 {
    840     return pAnnoDir->fieldsSize;
    841 }
    842 
    843 /* return a pointer to the field's annotation set */
    844 DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
    845     const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
    846 {
    847     return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
    848 }
    849 
    850 /* get the class' method annotation list */
    851 DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
    852     const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
    853 {
    854     if (pAnnoDir->methodsSize == 0)
    855         return NULL;
    856 
    857     /*
    858      * Skip past the header and field annotations to the start of the
    859      * method annotations.
    860      */
    861     const u1* addr = (const u1*) &pAnnoDir[1];
    862     addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
    863     return (const DexMethodAnnotationsItem*) addr;
    864 }
    865 
    866 /* get method annotation list size */
    867 DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
    868     const DexAnnotationsDirectoryItem* pAnnoDir)
    869 {
    870     return pAnnoDir->methodsSize;
    871 }
    872 
    873 /* return a pointer to the method's annotation set */
    874 DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
    875     const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
    876 {
    877     return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
    878 }
    879 
    880 /* get the class' parameter annotation list */
    881 DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
    882     const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
    883 {
    884     if (pAnnoDir->parametersSize == 0)
    885         return NULL;
    886 
    887     /*
    888      * Skip past the header, field annotations, and method annotations
    889      * to the start of the parameter annotations.
    890      */
    891     const u1* addr = (const u1*) &pAnnoDir[1];
    892     addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
    893     addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
    894     return (const DexParameterAnnotationsItem*) addr;
    895 }
    896 
    897 /* get method annotation list size */
    898 DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
    899     const DexAnnotationsDirectoryItem* pAnnoDir)
    900 {
    901     return pAnnoDir->parametersSize;
    902 }
    903 
    904 /* return the parameter annotation ref list */
    905 DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
    906     const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
    907 {
    908     return (const DexAnnotationSetRefList*)
    909         (pDexFile->baseAddr + pItem->annotationsOff);
    910 }
    911 
    912 /* get method annotation list size */
    913 DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
    914     const DexParameterAnnotationsItem* pItem)
    915 {
    916     if (pItem->annotationsOff == 0)
    917         return 0;
    918     return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
    919 }
    920 
    921 /* return the Nth entry from an annotation set ref list */
    922 DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
    923     const DexAnnotationSetRefList* pList, u4 idx)
    924 {
    925     assert(idx < pList->size);
    926     return &pList->list[idx];
    927 }
    928 
    929 /* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
    930 DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
    931     const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
    932 {
    933     return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
    934 }
    935 
    936 /* return the Nth annotation offset from a DexAnnotationSetItem */
    937 DEX_INLINE u4 dexGetAnnotationOff(
    938     const DexAnnotationSetItem* pAnnoSet, u4 idx)
    939 {
    940     assert(idx < pAnnoSet->size);
    941     return pAnnoSet->entries[idx];
    942 }
    943 
    944 /* return the Nth annotation item from a DexAnnotationSetItem */
    945 DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
    946     const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
    947 {
    948     return (const DexAnnotationItem*)
    949         (pDexFile->baseAddr + dexGetAnnotationOff(pAnnoSet, idx));
    950 }
    951 
    952 
    953 /*
    954  * ===========================================================================
    955  *      Utility Functions
    956  * ===========================================================================
    957  */
    958 
    959 /*
    960  * Retrieve the next UTF-16 character from a UTF-8 string.
    961  *
    962  * Advances "*pUtf8Ptr" to the start of the next character.
    963  *
    964  * WARNING: If a string is corrupted by dropping a '\0' in the middle
    965  * of a 3-byte sequence, you can end up overrunning the buffer with
    966  * reads (and possibly with the writes if the length was computed and
    967  * cached before the damage). For performance reasons, this function
    968  * assumes that the string being parsed is known to be valid (e.g., by
    969  * already being verified). Most strings we process here are coming
    970  * out of dex files or other internal translations, so the only real
    971  * risk comes from the JNI NewStringUTF call.
    972  */
    973 DEX_INLINE u2 dexGetUtf16FromUtf8(const char** pUtf8Ptr)
    974 {
    975     unsigned int one, two, three;
    976 
    977     one = *(*pUtf8Ptr)++;
    978     if ((one & 0x80) != 0) {
    979         /* two- or three-byte encoding */
    980         two = *(*pUtf8Ptr)++;
    981         if ((one & 0x20) != 0) {
    982             /* three-byte encoding */
    983             three = *(*pUtf8Ptr)++;
    984             return ((one & 0x0f) << 12) |
    985                    ((two & 0x3f) << 6) |
    986                    (three & 0x3f);
    987         } else {
    988             /* two-byte encoding */
    989             return ((one & 0x1f) << 6) |
    990                    (two & 0x3f);
    991         }
    992     } else {
    993         /* one-byte encoding */
    994         return one;
    995     }
    996 }
    997 
    998 /* Compare two '\0'-terminated modified UTF-8 strings, using Unicode
    999  * code point values for comparison. This treats different encodings
   1000  * for the same code point as equivalent, except that only a real '\0'
   1001  * byte is considered the string terminator. The return value is as
   1002  * for strcmp(). */
   1003 int dexUtf8Cmp(const char* s1, const char* s2);
   1004 
   1005 
   1006 /* for dexIsValidMemberNameUtf8(), a bit vector indicating valid low ascii */
   1007 extern u4 DEX_MEMBER_VALID_LOW_ASCII[4];
   1008 
   1009 /* Helper for dexIsValidMemberUtf8(); do not call directly. */
   1010 bool dexIsValidMemberNameUtf8_0(const char** pUtf8Ptr);
   1011 
   1012 /* Return whether the pointed-at modified-UTF-8 encoded character is
   1013  * valid as part of a member name, updating the pointer to point past
   1014  * the consumed character. This will consume two encoded UTF-16 code
   1015  * points if the character is encoded as a surrogate pair. Also, if
   1016  * this function returns false, then the given pointer may only have
   1017  * been partially advanced. */
   1018 DEX_INLINE bool dexIsValidMemberNameUtf8(const char** pUtf8Ptr) {
   1019     u1 c = (u1) **pUtf8Ptr;
   1020     if (c <= 0x7f) {
   1021         // It's low-ascii, so check the table.
   1022         u4 wordIdx = c >> 5;
   1023         u4 bitIdx = c & 0x1f;
   1024         (*pUtf8Ptr)++;
   1025         return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
   1026     }
   1027 
   1028     /*
   1029      * It's a multibyte encoded character. Call a non-inline function
   1030      * for the heavy lifting.
   1031      */
   1032     return dexIsValidMemberNameUtf8_0(pUtf8Ptr);
   1033 }
   1034 
   1035 /* Return whether the given string is a valid field or method name. */
   1036 bool dexIsValidMemberName(const char* s);
   1037 
   1038 /* Return whether the given string is a valid type descriptor. */
   1039 bool dexIsValidTypeDescriptor(const char* s);
   1040 
   1041 /* Return whether the given string is a valid reference descriptor. This
   1042  * is true if dexIsValidTypeDescriptor() returns true and the descriptor
   1043  * is for a class or array and not a primitive type. */
   1044 bool dexIsReferenceDescriptor(const char* s);
   1045 
   1046 /* Return whether the given string is a valid class descriptor. This
   1047  * is true if dexIsValidTypeDescriptor() returns true and the descriptor
   1048  * is for a class and not an array or primitive type. */
   1049 bool dexIsClassDescriptor(const char* s);
   1050 
   1051 /* Return whether the given string is a valid field type descriptor. This
   1052  * is true if dexIsValidTypeDescriptor() returns true and the descriptor
   1053  * is for anything but "void". */
   1054 bool dexIsFieldDescriptor(const char* s);
   1055 
   1056 #endif /*_LIBDEX_DEXFILE*/
   1057