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