2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Functions to deal with class definition structures in DEX files
21 #ifndef _LIBDEX_DEXCLASS
22 #define _LIBDEX_DEXCLASS
27 /* expanded form of a class_data_item header */
28 typedef struct DexClassDataHeader {
30 u4 instanceFieldsSize;
32 u4 virtualMethodsSize;
35 /* expanded form of encoded_field */
36 typedef struct DexField {
37 u4 fieldIdx; /* index to a field_id_item */
41 /* expanded form of encoded_method */
42 typedef struct DexMethod {
43 u4 methodIdx; /* index to a method_id_item */
45 u4 codeOff; /* file offset to a code_item */
48 /* expanded form of class_data_item. Note: If a particular item is
49 * absent (e.g., no static fields), then the corresponding pointer
51 typedef struct DexClassData {
52 DexClassDataHeader header;
53 DexField* staticFields;
54 DexField* instanceFields;
55 DexMethod* directMethods;
56 DexMethod* virtualMethods;
59 /* Read and verify the header of a class_data_item. This updates the
60 * given data pointer to point past the end of the read data and
61 * returns an "okay" flag (that is, false == failure). */
62 bool dexReadAndVerifyClassDataHeader(const u1** pData, const u1* pLimit,
63 DexClassDataHeader *pHeader);
65 /* Read and verify an encoded_field. This updates the
66 * given data pointer to point past the end of the read data and
67 * returns an "okay" flag (that is, false == failure).
69 * The lastIndex value should be set to 0 before the first field in
70 * a list is read. It is updated as fields are read and used in the
73 * The verification done by this function is of the raw data format
74 * only; it does not verify that access flags or indices
76 bool dexReadAndVerifyClassDataField(const u1** pData, const u1* pLimit,
77 DexField* pField, u4* lastIndex);
79 /* Read and verify an encoded_method. This updates the
80 * given data pointer to point past the end of the read data and
81 * returns an "okay" flag (that is, false == failure).
83 * The lastIndex value should be set to 0 before the first method in
84 * a list is read. It is updated as fields are read and used in the
87 * The verification done by this function is of the raw data format
88 * only; it does not verify that access flags, indices, or offsets
90 bool dexReadAndVerifyClassDataMethod(const u1** pData, const u1* pLimit,
91 DexMethod* pMethod, u4* lastIndex);
93 /* Read, verify, and return an entire class_data_item. This updates
94 * the given data pointer to point past the end of the read data. This
95 * function allocates a single chunk of memory for the result, which
96 * must subsequently be free()d. This function returns NULL if there
97 * was trouble parsing the data. If this function is passed NULL, it
98 * returns an initialized empty DexClassData structure.
100 * The verification done by this function is of the raw data format
101 * only; it does not verify that access flags, indices, or offsets
103 DexClassData* dexReadAndVerifyClassData(const u1** pData, const u1* pLimit);
106 * Get the DexCode for a DexMethod. Returns NULL if the class is native
109 DEX_INLINE const DexCode* dexGetCode(const DexFile* pDexFile,
110 const DexMethod* pDexMethod)
112 if (pDexMethod->codeOff == 0)
114 return (const DexCode*) (pDexFile->baseAddr + pDexMethod->codeOff);
118 /* Read the header of a class_data_item without verification. This
119 * updates the given data pointer to point past the end of the read
121 DEX_INLINE void dexReadClassDataHeader(const u1** pData,
122 DexClassDataHeader *pHeader) {
123 pHeader->staticFieldsSize = readUnsignedLeb128(pData);
124 pHeader->instanceFieldsSize = readUnsignedLeb128(pData);
125 pHeader->directMethodsSize = readUnsignedLeb128(pData);
126 pHeader->virtualMethodsSize = readUnsignedLeb128(pData);
129 /* Read an encoded_field without verification. This updates the
130 * given data pointer to point past the end of the read data.
132 * The lastIndex value should be set to 0 before the first field in
133 * a list is read. It is updated as fields are read and used in the
136 DEX_INLINE void dexReadClassDataField(const u1** pData, DexField* pField,
138 u4 index = *lastIndex + readUnsignedLeb128(pData);
140 pField->accessFlags = readUnsignedLeb128(pData);
141 pField->fieldIdx = index;
145 /* Read an encoded_method without verification. This updates the
146 * given data pointer to point past the end of the read data.
148 * The lastIndex value should be set to 0 before the first method in
149 * a list is read. It is updated as fields are read and used in the
152 DEX_INLINE void dexReadClassDataMethod(const u1** pData, DexMethod* pMethod,
154 u4 index = *lastIndex + readUnsignedLeb128(pData);
156 pMethod->accessFlags = readUnsignedLeb128(pData);
157 pMethod->codeOff = readUnsignedLeb128(pData);
158 pMethod->methodIdx = index;