OSDN Git Service

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