DexFile.h revision e377ef62a40267ab16c2dd20cc5f4c63af6397cc
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 */
64enum { kSHA1DigestLen = 20,
65       kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };
66
67/* general constants */
68enum {
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 */
79enum {
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 */
117enum {
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 */
144enum {
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 */
166enum {
167    kDexChunkClassLookup            = 0x434c4b50,   /* CLKP */
168    kDexChunkRegisterMaps           = 0x524d4150,   /* RMAP */
169
170    kDexChunkReducingIndexMap       = 0x5249584d,   /* RIXM */
171    kDexChunkExpandingIndexMap      = 0x4549584d,   /* EIXM */
172
173    kDexChunkEnd                    = 0x41454e44,   /* AEND */
174};
175
176/* debug info opcodes and constants */
177enum {
178    DBG_END_SEQUENCE         = 0x00,
179    DBG_ADVANCE_PC           = 0x01,
180    DBG_ADVANCE_LINE         = 0x02,
181    DBG_START_LOCAL          = 0x03,
182    DBG_START_LOCAL_EXTENDED = 0x04,
183    DBG_END_LOCAL            = 0x05,
184    DBG_RESTART_LOCAL        = 0x06,
185    DBG_SET_PROLOGUE_END     = 0x07,
186    DBG_SET_EPILOGUE_BEGIN   = 0x08,
187    DBG_SET_FILE             = 0x09,
188    DBG_FIRST_SPECIAL        = 0x0a,
189    DBG_LINE_BASE            = -4,
190    DBG_LINE_RANGE           = 15,
191};
192
193/*
194 * Direct-mapped "header_item" struct.
195 */
196typedef struct DexHeader {
197    u1  magic[8];           /* includes version number */
198    u4  checksum;           /* adler32 checksum */
199    u1  signature[kSHA1DigestLen]; /* SHA-1 hash */
200    u4  fileSize;           /* length of entire file */
201    u4  headerSize;         /* offset to start of next section */
202    u4  endianTag;
203    u4  linkSize;
204    u4  linkOff;
205    u4  mapOff;
206    u4  stringIdsSize;
207    u4  stringIdsOff;
208    u4  typeIdsSize;
209    u4  typeIdsOff;
210    u4  protoIdsSize;
211    u4  protoIdsOff;
212    u4  fieldIdsSize;
213    u4  fieldIdsOff;
214    u4  methodIdsSize;
215    u4  methodIdsOff;
216    u4  classDefsSize;
217    u4  classDefsOff;
218    u4  dataSize;
219    u4  dataOff;
220} DexHeader;
221
222/*
223 * Direct-mapped "map_item".
224 */
225typedef struct DexMapItem {
226    u2  type;              /* type code (see kDexType* above) */
227    u2  unused;
228    u4  size;              /* count of items of the indicated type */
229    u4  offset;            /* file offset to the start of data */
230} DexMapItem;
231
232/*
233 * Direct-mapped "map_list".
234 */
235typedef struct DexMapList {
236    u4  size;               /* #of entries in list */
237    DexMapItem list[1];     /* entries */
238} DexMapList;
239
240/*
241 * Direct-mapped "string_id_item".
242 */
243typedef struct DexStringId {
244    u4  stringDataOff;      /* file offset to string_data_item */
245} DexStringId;
246
247/*
248 * Direct-mapped "type_id_item".
249 */
250typedef struct DexTypeId {
251    u4  descriptorIdx;      /* index into stringIds list for type descriptor */
252} DexTypeId;
253
254/*
255 * Direct-mapped "field_id_item".
256 */
257typedef struct DexFieldId {
258    u2  classIdx;           /* index into typeIds list for defining class */
259    u2  typeIdx;            /* index into typeIds for field type */
260    u4  nameIdx;            /* index into stringIds for field name */
261} DexFieldId;
262
263/*
264 * Direct-mapped "method_id_item".
265 */
266typedef struct DexMethodId {
267    u2  classIdx;           /* index into typeIds list for defining class */
268    u2  protoIdx;           /* index into protoIds for method prototype */
269    u4  nameIdx;            /* index into stringIds for method name */
270} DexMethodId;
271
272/*
273 * Direct-mapped "proto_id_item".
274 */
275typedef struct DexProtoId {
276    u4  shortyIdx;          /* index into stringIds for shorty descriptor */
277    u4  returnTypeIdx;      /* index into typeIds list for return type */
278    u4  parametersOff;      /* file offset to type_list for parameter types */
279} DexProtoId;
280
281/*
282 * Direct-mapped "class_def_item".
283 */
284typedef struct DexClassDef {
285    u4  classIdx;           /* index into typeIds for this class */
286    u4  accessFlags;
287    u4  superclassIdx;      /* index into typeIds for superclass */
288    u4  interfacesOff;      /* file offset to DexTypeList */
289    u4  sourceFileIdx;      /* index into stringIds for source file name */
290    u4  annotationsOff;     /* file offset to annotations_directory_item */
291    u4  classDataOff;       /* file offset to class_data_item */
292    u4  staticValuesOff;    /* file offset to DexEncodedArray */
293} DexClassDef;
294
295/*
296 * Direct-mapped "type_item".
297 */
298typedef struct DexTypeItem {
299    u2  typeIdx;            /* index into typeIds */
300} DexTypeItem;
301
302/*
303 * Direct-mapped "type_list".
304 */
305typedef struct DexTypeList {
306    u4  size;               /* #of entries in list */
307    DexTypeItem list[1];    /* entries */
308} DexTypeList;
309
310/*
311 * Direct-mapped "code_item".
312 *
313 * The "catches" table is used when throwing an exception,
314 * "debugInfo" is used when displaying an exception stack trace or
315 * debugging. An offset of zero indicates that there are no entries.
316 */
317typedef struct DexCode {
318    u2  registersSize;
319    u2  insSize;
320    u2  outsSize;
321    u2  triesSize;
322    u4  debugInfoOff;       /* file offset to debug info stream */
323    u4  insnsSize;          /* size of the insns array, in u2 units */
324    u2  insns[1];
325    /* followed by optional u2 padding */
326    /* followed by try_item[triesSize] */
327    /* followed by uleb128 handlersSize */
328    /* followed by catch_handler_item[handlersSize] */
329} DexCode;
330
331/*
332 * Direct-mapped "try_item".
333 */
334typedef struct DexTry {
335    u4  startAddr;          /* start address, in 16-bit code units */
336    u2  insnCount;          /* instruction count, in 16-bit code units */
337    u2  handlerOff;         /* offset in encoded handler data to handlers */
338} DexTry;
339
340/*
341 * Link table.  Currently undefined.
342 */
343typedef struct DexLink {
344    u1  bleargh;
345} DexLink;
346
347
348/*
349 * Direct-mapped "annotations_directory_item".
350 */
351typedef struct DexAnnotationsDirectoryItem {
352    u4  classAnnotationsOff;  /* offset to DexAnnotationSetItem */
353    u4  fieldsSize;           /* count of DexFieldAnnotationsItem */
354    u4  methodsSize;          /* count of DexMethodAnnotationsItem */
355    u4  parametersSize;       /* count of DexParameterAnnotationsItem */
356    /* followed by DexFieldAnnotationsItem[fieldsSize] */
357    /* followed by DexMethodAnnotationsItem[methodsSize] */
358    /* followed by DexParameterAnnotationsItem[parametersSize] */
359} DexAnnotationsDirectoryItem;
360
361/*
362 * Direct-mapped "field_annotations_item".
363 */
364typedef struct DexFieldAnnotationsItem {
365    u4  fieldIdx;
366    u4  annotationsOff;             /* offset to DexAnnotationSetItem */
367} DexFieldAnnotationsItem;
368
369/*
370 * Direct-mapped "method_annotations_item".
371 */
372typedef struct DexMethodAnnotationsItem {
373    u4  methodIdx;
374    u4  annotationsOff;             /* offset to DexAnnotationSetItem */
375} DexMethodAnnotationsItem;
376
377/*
378 * Direct-mapped "parameter_annotations_item".
379 */
380typedef struct DexParameterAnnotationsItem {
381    u4  methodIdx;
382    u4  annotationsOff;             /* offset to DexAnotationSetRefList */
383} DexParameterAnnotationsItem;
384
385/*
386 * Direct-mapped "annotation_set_ref_item".
387 */
388typedef struct DexAnnotationSetRefItem {
389    u4  annotationsOff;             /* offset to DexAnnotationSetItem */
390} DexAnnotationSetRefItem;
391
392/*
393 * Direct-mapped "annotation_set_ref_list".
394 */
395typedef struct DexAnnotationSetRefList {
396    u4  size;
397    DexAnnotationSetRefItem list[1];
398} DexAnnotationSetRefList;
399
400/*
401 * Direct-mapped "anotation_set_item".
402 */
403typedef struct DexAnnotationSetItem {
404    u4  size;
405    u4  entries[1];                 /* offset to DexAnnotationItem */
406} DexAnnotationSetItem;
407
408/*
409 * Direct-mapped "annotation_item".
410 *
411 * NOTE: this structure is byte-aligned.
412 */
413typedef struct DexAnnotationItem {
414    u1  visibility;
415    u1  annotation[1];              /* data in encoded_annotation format */
416} DexAnnotationItem;
417
418/*
419 * Direct-mapped "encoded_array".
420 *
421 * NOTE: this structure is byte-aligned.
422 */
423typedef struct DexEncodedArray {
424    u1  array[1];                   /* data in encoded_array format */
425} DexEncodedArray;
426
427/*
428 * Lookup table for classes.  It provides a mapping from class name to
429 * class definition.  Used by dexFindClass().
430 *
431 * We calculate this at DEX optimization time and embed it in the file so we
432 * don't need the same hash table in every VM.  This is slightly slower than
433 * a hash table with direct pointers to the items, but because it's shared
434 * there's less of a penalty for using a fairly sparse table.
435 */
436typedef struct DexClassLookup {
437    int     size;                       // total size, including "size"
438    int     numEntries;                 // size of table[]; always power of 2
439    struct {
440        u4      classDescriptorHash;    // class descriptor hash code
441        int     classDescriptorOffset;  // in bytes, from start of DEX
442        int     classDefOffset;         // in bytes, from start of DEX
443    } table[1];
444} DexClassLookup;
445
446/*
447 * Map constant pool indices from one form to another.  Some or all of these
448 * may be NULL.
449 *
450 * The map values are 16-bit unsigned values.  If the values we map to
451 * require a larger range, we omit the mapping for that category (which
452 * requires that the lookup code recognize that the data will not be
453 * there for all DEX files in all categories.)
454 */
455typedef struct DexIndexMap {
456    const u2* classMap;         /* map, either expanding or reducing */
457    u4  classFullCount;         /* same as typeIdsSize */
458    u4  classReducedCount;      /* post-reduction count */
459    const u2* methodMap;
460    u4  methodFullCount;
461    u4  methodReducedCount;
462    const u2* fieldMap;
463    u4  fieldFullCount;
464    u4  fieldReducedCount;
465    const u2* stringMap;
466    u4  stringFullCount;
467    u4  stringReducedCount;
468} DexIndexMap;
469
470/*
471 * Header added by DEX optimization pass.  Values are always written in
472 * local byte and structure padding.  The first field (magic + version)
473 * is guaranteed to be present and directly readable for all expected
474 * compiler configurations; the rest is version-dependent.
475 *
476 * Try to keep this simple and fixed-size.
477 */
478typedef struct DexOptHeader {
479    u1  magic[8];           /* includes version number */
480
481    u4  dexOffset;          /* file offset of DEX header */
482    u4  dexLength;
483    u4  depsOffset;         /* offset of optimized DEX dependency table */
484    u4  depsLength;
485    u4  optOffset;          /* file offset of optimized data tables */
486    u4  optLength;
487
488    u4  flags;              /* some info flags */
489    u4  checksum;           /* adler32 checksum covering deps/opt */
490
491    /* pad for 64-bit alignment if necessary */
492} DexOptHeader;
493
494#define DEX_FLAG_VERIFIED           (1)     /* tried to verify all classes */
495#define DEX_OPT_FLAG_BIG            (1<<1)  /* swapped to big-endian */
496#define DEX_OPT_FLAG_FIELDS         (1<<2)  /* field access optimized */
497#define DEX_OPT_FLAG_INVOCATIONS    (1<<3)  /* method calls optimized */
498
499#define DEX_INTERFACE_CACHE_SIZE    128     /* must be power of 2 */
500
501/*
502 * Structure representing a DEX file.
503 *
504 * Code should regard DexFile as opaque, using the API calls provided here
505 * to access specific structures.
506 */
507typedef struct DexFile {
508    /* directly-mapped "opt" header */
509    const DexOptHeader* pOptHeader;
510
511    /* pointers to directly-mapped structs and arrays in base DEX */
512    const DexHeader*    pHeader;
513    const DexStringId*  pStringIds;
514    const DexTypeId*    pTypeIds;
515    const DexFieldId*   pFieldIds;
516    const DexMethodId*  pMethodIds;
517    const DexProtoId*   pProtoIds;
518    const DexClassDef*  pClassDefs;
519    const DexLink*      pLinkData;
520
521    /*
522     * These are mapped out of the "auxillary" section, and may not be
523     * included in the file.
524     */
525    const DexClassLookup* pClassLookup;
526    DexIndexMap         indexMap;
527    const void*         pRegisterMapPool;       // RegisterMapClassPool
528
529    /* points to start of DEX file data */
530    const u1*           baseAddr;
531
532    /* track memory overhead for auxillary structures */
533    int                 overhead;
534
535    /* additional app-specific data structures associated with the DEX */
536    //void*               auxData;
537} DexFile;
538
539/*
540 * Utility function -- rounds up to the nearest power of 2.
541 */
542u4 dexRoundUpPower2(u4 val);
543
544/*
545 * Parse an optimized or unoptimized .dex file sitting in memory.
546 *
547 * On success, return a newly-allocated DexFile.
548 */
549DexFile* dexFileParse(const u1* data, size_t length, int flags);
550
551/* bit values for "flags" argument to dexFileParse */
552enum {
553    kDexParseDefault            = 0,
554    kDexParseVerifyChecksum     = 1,
555    kDexParseContinueOnError    = (1 << 1),
556};
557
558/*
559 * Fix the byte ordering of all fields in the DEX file, and do
560 * structural verification. This is only required for code that opens
561 * "raw" DEX files, such as the DEX optimizer.
562 *
563 * Return 0 on success.
564 */
565int dexSwapAndVerify(u1* addr, int len);
566
567/*
568 * Detect the file type of the given memory buffer via magic number.
569 * Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
570 * but return successfully on an optimized DEX file, and report an
571 * error for all other cases.
572 *
573 * Return 0 on success.
574 */
575int dexSwapAndVerifyIfNecessary(u1* addr, int len);
576
577/*
578 * Compute DEX checksum.
579 */
580u4 dexComputeChecksum(const DexHeader* pHeader);
581
582/*
583 * Free a DexFile structure, along with any associated structures.
584 */
585void dexFileFree(DexFile* pDexFile);
586
587/*
588 * Create class lookup table.
589 */
590DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);
591
592/*
593 * Find a class definition by descriptor.
594 */
595const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);
596
597/*
598 * Set up the basic raw data pointers of a DexFile. This function isn't
599 * meant for general use.
600 */
601void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);
602
603/* return the DexMapList of the file, if any */
604DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
605    u4 mapOff = pDexFile->pHeader->mapOff;
606
607    if (mapOff == 0) {
608        return NULL;
609    } else {
610        return (const DexMapList*) (pDexFile->baseAddr + mapOff);
611    }
612}
613
614/* return the const char* string data referred to by the given string_id */
615DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
616        const DexStringId* pStringId) {
617    const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;
618
619    // Skip the uleb128 length.
620    while (*(ptr++) > 0x7f) /* empty */ ;
621
622    return (const char*) ptr;
623}
624/* return the StringId with the specified index */
625DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
626    assert(idx < pDexFile->pHeader->stringIdsSize);
627    return &pDexFile->pStringIds[idx];
628}
629/* return the UTF-8 encoded string with the specified string_id index */
630DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
631    const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
632    return dexGetStringData(pDexFile, pStringId);
633}
634
635/* Return the UTF-8 encoded string with the specified string_id index,
636 * also filling in the UTF-16 size (number of 16-bit code points).*/
637const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
638        u4* utf16Size);
639
640/* return the TypeId with the specified index */
641DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
642    assert(idx < pDexFile->pHeader->typeIdsSize);
643    return &pDexFile->pTypeIds[idx];
644}
645
646/*
647 * Get the descriptor string associated with a given type index.
648 * The caller should not free() the returned string.
649 */
650DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
651    const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
652    return dexStringById(pDexFile, typeId->descriptorIdx);
653}
654
655/* return the MethodId with the specified index */
656DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
657    assert(idx < pDexFile->pHeader->methodIdsSize);
658    return &pDexFile->pMethodIds[idx];
659}
660
661/* return the FieldId with the specified index */
662DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
663    assert(idx < pDexFile->pHeader->fieldIdsSize);
664    return &pDexFile->pFieldIds[idx];
665}
666
667/* return the ProtoId with the specified index */
668DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
669    assert(idx < pDexFile->pHeader->protoIdsSize);
670    return &pDexFile->pProtoIds[idx];
671}
672
673/*
674 * Get the parameter list from a ProtoId. The returns NULL if the ProtoId
675 * does not have a parameter list.
676 */
677DEX_INLINE const DexTypeList* dexGetProtoParameters(
678    const DexFile *pDexFile, const DexProtoId* pProtoId) {
679    if (pProtoId->parametersOff == 0) {
680        return NULL;
681    }
682    return (const DexTypeList*)
683        (pDexFile->baseAddr + pProtoId->parametersOff);
684}
685
686/* return the ClassDef with the specified index */
687DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
688    assert(idx < pDexFile->pHeader->classDefsSize);
689    return &pDexFile->pClassDefs[idx];
690}
691
692/* given a ClassDef pointer, recover its index */
693DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
694    const DexClassDef* pClassDef)
695{
696    assert(pClassDef >= pDexFile->pClassDefs &&
697           pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
698    return pClassDef - pDexFile->pClassDefs;
699}
700
701/* get the interface list for a DexClass */
702DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
703    const DexClassDef* pClassDef)
704{
705    if (pClassDef->interfacesOff == 0)
706        return NULL;
707    return (const DexTypeList*)
708        (pDexFile->baseAddr + pClassDef->interfacesOff);
709}
710/* return the Nth entry in a DexTypeList. */
711DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
712    u4 idx)
713{
714    assert(idx < pList->size);
715    return &pList->list[idx];
716}
717/* return the type_idx for the Nth entry in a TypeList */
718DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
719    const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
720    return pItem->typeIdx;
721}
722
723/* get the static values list for a DexClass */
724DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
725    const DexFile* pDexFile, const DexClassDef* pClassDef)
726{
727    if (pClassDef->staticValuesOff == 0)
728        return NULL;
729    return (const DexEncodedArray*)
730        (pDexFile->baseAddr + pClassDef->staticValuesOff);
731}
732
733/* get the annotations directory item for a DexClass */
734DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
735    const DexFile* pDexFile, const DexClassDef* pClassDef)
736{
737    if (pClassDef->annotationsOff == 0)
738        return NULL;
739    return (const DexAnnotationsDirectoryItem*)
740        (pDexFile->baseAddr + pClassDef->annotationsOff);
741}
742
743/* get the source file string */
744DEX_INLINE const char* dexGetSourceFile(
745    const DexFile* pDexFile, const DexClassDef* pClassDef)
746{
747    if (pClassDef->sourceFileIdx == 0xffffffff)
748        return NULL;
749    return dexStringById(pDexFile, pClassDef->sourceFileIdx);
750}
751
752/* get the size, in bytes, of a DexCode */
753size_t dexGetDexCodeSize(const DexCode* pCode);
754
755/* Get the list of "tries" for the given DexCode. */
756DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
757    const u2* insnsEnd = &pCode->insns[pCode->insnsSize];
758
759    // Round to four bytes.
760    if ((((u4) insnsEnd) & 3) != 0) {
761        insnsEnd++;
762    }
763
764    return (const DexTry*) insnsEnd;
765}
766
767/* Get the base of the encoded data for the given DexCode. */
768DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
769    const DexTry* pTries = dexGetTries(pCode);
770    return (const u1*) &pTries[pCode->triesSize];
771}
772
773/* get a pointer to the start of the debugging data */
774DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
775    const DexCode* pCode)
776{
777    if (pCode->debugInfoOff == 0) {
778        return NULL;
779    } else {
780        return pDexFile->baseAddr + pCode->debugInfoOff;
781    }
782}
783
784/*
785 * Callback for "new position table entry".
786 * Returning non-0 causes the decoder to stop early.
787 */
788typedef int (*DexDebugNewPositionCb)(void *cnxt, u4 address, u4 lineNum);
789
790/*
791 * Callback for "new locals table entry". "signature" is an empty string
792 * if no signature is available for an entry.
793 */
794typedef void (*DexDebugNewLocalCb)(void *cnxt, u2 reg, u4 startAddress,
795        u4 endAddress, const char *name, const char *descriptor,
796        const char *signature);
797
798/*
799 * Decode debug info for method.
800 *
801 * posCb is called in ascending address order.
802 * localCb is called in order of ascending end address.
803 */
804void dexDecodeDebugInfo(
805            const DexFile* pDexFile,
806            const DexCode* pDexCode,
807            const char* classDescriptor,
808            u4 protoIdx,
809            u4 accessFlags,
810            DexDebugNewPositionCb posCb, DexDebugNewLocalCb localCb,
811            void* cnxt);
812
813/* DexClassDef convenience - get class descriptor */
814DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
815    const DexClassDef* pClassDef)
816{
817    return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
818}
819
820/* DexClassDef convenience - get superclass descriptor */
821DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
822    const DexClassDef* pClassDef)
823{
824    if (pClassDef->superclassIdx == 0)
825        return NULL;
826    return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
827}
828
829/* DexClassDef convenience - get class_data_item pointer */
830DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
831    const DexClassDef* pClassDef)
832{
833    if (pClassDef->classDataOff == 0)
834        return NULL;
835    return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
836}
837
838/* Get an annotation set at a particular offset. */
839DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
840    const DexFile* pDexFile, u4 offset)
841{
842    return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
843}
844/* get the class' annotation set */
845DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
846    const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
847{
848    if (pAnnoDir->classAnnotationsOff == 0)
849        return NULL;
850    return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
851}
852
853/* get the class' field annotation list */
854DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
855    const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
856{
857    if (pAnnoDir->fieldsSize == 0)
858        return NULL;
859
860    // Skip past the header to the start of the field annotations.
861    return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
862}
863
864/* get field annotation list size */
865DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
866    const DexAnnotationsDirectoryItem* pAnnoDir)
867{
868    return pAnnoDir->fieldsSize;
869}
870
871/* return a pointer to the field's annotation set */
872DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
873    const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
874{
875    return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
876}
877
878/* get the class' method annotation list */
879DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
880    const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
881{
882    if (pAnnoDir->methodsSize == 0)
883        return NULL;
884
885    /*
886     * Skip past the header and field annotations to the start of the
887     * method annotations.
888     */
889    const u1* addr = (const u1*) &pAnnoDir[1];
890    addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
891    return (const DexMethodAnnotationsItem*) addr;
892}
893
894/* get method annotation list size */
895DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
896    const DexAnnotationsDirectoryItem* pAnnoDir)
897{
898    return pAnnoDir->methodsSize;
899}
900
901/* return a pointer to the method's annotation set */
902DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
903    const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
904{
905    return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
906}
907
908/* get the class' parameter annotation list */
909DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
910    const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
911{
912    if (pAnnoDir->parametersSize == 0)
913        return NULL;
914
915    /*
916     * Skip past the header, field annotations, and method annotations
917     * to the start of the parameter annotations.
918     */
919    const u1* addr = (const u1*) &pAnnoDir[1];
920    addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
921    addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
922    return (const DexParameterAnnotationsItem*) addr;
923}
924
925/* get method annotation list size */
926DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
927    const DexAnnotationsDirectoryItem* pAnnoDir)
928{
929    return pAnnoDir->parametersSize;
930}
931
932/* return the parameter annotation ref list */
933DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
934    const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
935{
936    return (const DexAnnotationSetRefList*)
937        (pDexFile->baseAddr + pItem->annotationsOff);
938}
939
940/* get method annotation list size */
941DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
942    const DexParameterAnnotationsItem* pItem)
943{
944    if (pItem->annotationsOff == 0)
945        return 0;
946    return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
947}
948
949/* return the Nth entry from an annotation set ref list */
950DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
951    const DexAnnotationSetRefList* pList, u4 idx)
952{
953    assert(idx < pList->size);
954    return &pList->list[idx];
955}
956
957/* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
958DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
959    const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
960{
961    return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
962}
963
964/* return the Nth annotation offset from a DexAnnotationSetItem */
965DEX_INLINE u4 dexGetAnnotationOff(
966    const DexAnnotationSetItem* pAnnoSet, u4 idx)
967{
968    assert(idx < pAnnoSet->size);
969    return pAnnoSet->entries[idx];
970}
971
972/* return the Nth annotation item from a DexAnnotationSetItem */
973DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
974    const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
975{
976    return (const DexAnnotationItem*)
977        (pDexFile->baseAddr + dexGetAnnotationOff(pAnnoSet, idx));
978}
979
980
981/*
982 * ===========================================================================
983 *      Utility Functions
984 * ===========================================================================
985 */
986
987/*
988 * Retrieve the next UTF-16 character from a UTF-8 string.
989 *
990 * Advances "*pUtf8Ptr" to the start of the next character.
991 *
992 * WARNING: If a string is corrupted by dropping a '\0' in the middle
993 * of a 3-byte sequence, you can end up overrunning the buffer with
994 * reads (and possibly with the writes if the length was computed and
995 * cached before the damage). For performance reasons, this function
996 * assumes that the string being parsed is known to be valid (e.g., by
997 * already being verified). Most strings we process here are coming
998 * out of dex files or other internal translations, so the only real
999 * risk comes from the JNI NewStringUTF call.
1000 */
1001DEX_INLINE u2 dexGetUtf16FromUtf8(const char** pUtf8Ptr)
1002{
1003    unsigned int one, two, three;
1004
1005    one = *(*pUtf8Ptr)++;
1006    if ((one & 0x80) != 0) {
1007        /* two- or three-byte encoding */
1008        two = *(*pUtf8Ptr)++;
1009        if ((one & 0x20) != 0) {
1010            /* three-byte encoding */
1011            three = *(*pUtf8Ptr)++;
1012            return ((one & 0x0f) << 12) |
1013                   ((two & 0x3f) << 6) |
1014                   (three & 0x3f);
1015        } else {
1016            /* two-byte encoding */
1017            return ((one & 0x1f) << 6) |
1018                   (two & 0x3f);
1019        }
1020    } else {
1021        /* one-byte encoding */
1022        return one;
1023    }
1024}
1025
1026/* Compare two '\0'-terminated modified UTF-8 strings, using Unicode
1027 * code point values for comparison. This treats different encodings
1028 * for the same code point as equivalent, except that only a real '\0'
1029 * byte is considered the string terminator. The return value is as
1030 * for strcmp(). */
1031int dexUtf8Cmp(const char* s1, const char* s2);
1032
1033
1034/* for dexIsValidMemberNameUtf8(), a bit vector indicating valid low ascii */
1035extern u4 DEX_MEMBER_VALID_LOW_ASCII[4];
1036
1037/* Helper for dexIsValidMemberUtf8(); do not call directly. */
1038bool dexIsValidMemberNameUtf8_0(const char** pUtf8Ptr);
1039
1040/* Return whether the pointed-at modified-UTF-8 encoded character is
1041 * valid as part of a member name, updating the pointer to point past
1042 * the consumed character. This will consume two encoded UTF-16 code
1043 * points if the character is encoded as a surrogate pair. Also, if
1044 * this function returns false, then the given pointer may only have
1045 * been partially advanced. */
1046DEX_INLINE bool dexIsValidMemberNameUtf8(const char** pUtf8Ptr) {
1047    u1 c = (u1) **pUtf8Ptr;
1048    if (c <= 0x7f) {
1049        // It's low-ascii, so check the table.
1050        u4 wordIdx = c >> 5;
1051        u4 bitIdx = c & 0x1f;
1052        (*pUtf8Ptr)++;
1053        return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
1054    }
1055
1056    /*
1057     * It's a multibyte encoded character. Call a non-inline function
1058     * for the heavy lifting.
1059     */
1060    return dexIsValidMemberNameUtf8_0(pUtf8Ptr);
1061}
1062
1063/* Return whether the given string is a valid field or method name. */
1064bool dexIsValidMemberName(const char* s);
1065
1066/* Return whether the given string is a valid type descriptor. */
1067bool dexIsValidTypeDescriptor(const char* s);
1068
1069/* Return whether the given string is a valid reference descriptor. This
1070 * is true if dexIsValidTypeDescriptor() returns true and the descriptor
1071 * is for a class or array and not a primitive type. */
1072bool dexIsReferenceDescriptor(const char* s);
1073
1074/* Return whether the given string is a valid class descriptor. This
1075 * is true if dexIsValidTypeDescriptor() returns true and the descriptor
1076 * is for a class and not an array or primitive type. */
1077bool dexIsClassDescriptor(const char* s);
1078
1079/* Return whether the given string is a valid field type descriptor. This
1080 * is true if dexIsValidTypeDescriptor() returns true and the descriptor
1081 * is for anything but "void". */
1082bool dexIsFieldDescriptor(const char* s);
1083
1084#endif /*_LIBDEX_DEXFILE*/
1085