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