DexFile.h

/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Access .dex (Dalvik Executable Format) files. The code here assumes that
* the DEX file has been rewritten (byte-swapped, word-aligned) and that
* the contents can be directly accessed as a collection of C arrays. Please
* see docs/dalvik/dex-format.html for a detailed description.
*
* The structure and field names were chosen to match those in the DEX spec.
*
* It's generally assumed that the DEX file will be stored in shared memory,
* obviating the need to copy code and constant pool entries into newly
* allocated storage. Maintaining local pointers to items in the shared area
* is valid and encouraged.
*
* All memory-mapped structures are 32-bit aligned unless otherwise noted.
*/

#ifndef LIBDEX_DEXFILE_H_
#define LIBDEX_DEXFILE_H_

#include "vm/Common.h" // basic type defs, e.g. u1/u2/u4/u8, and LOG
#include "libdex/SysUtil.h"

/*
* gcc-style inline management -- ensures we have a copy of all functions
* in the library, so code that links against us will work whether or not
* it was built with optimizations enabled.
*/
#ifndef _DEX_GEN_INLINES /* only defined by DexInlines.c */
# define DEX_INLINE extern __inline__
#else
# define DEX_INLINE
#endif

/* DEX file magic number */
#define DEX_MAGIC "dex\n"

/* current version, encoded in 4 bytes of ASCII */
#define DEX_MAGIC_VERS "036\0"

/*
* older but still-recognized version (corresponding to Android API
* levels 13 and earlier
*/
#define DEX_MAGIC_VERS_API_13 "035\0"

/* same, but for optimized DEX header */
#define DEX_OPT_MAGIC "dey\n"
#define DEX_OPT_MAGIC_VERS "036\0"

#define DEX_DEP_MAGIC "deps"

/*
* 160-bit SHA-1 digest.
*/
enum { kSHA1DigestLen = 20,
kSHA1DigestOutputLen = kSHA1DigestLen*2 +1 };

/* general constants */
enum {
kDexEndianConstant = 0x12345678, /* the endianness indicator */
kDexNoIndex = 0xffffffff, /* not a valid index value */
};

/*
* Enumeration of all the primitive types.
*/
enum PrimitiveType {
PRIM_NOT = 0, /* value is a reference type, not a primitive type */
PRIM_VOID = 1,
PRIM_BOOLEAN = 2,
PRIM_BYTE = 3,
PRIM_SHORT = 4,
PRIM_CHAR = 5,
PRIM_INT = 6,
PRIM_LONG = 7,
PRIM_FLOAT = 8,
PRIM_DOUBLE = 9,
};

/*
* access flags and masks; the "standard" ones are all <= 0x4000
*
* Note: There are related declarations in vm/oo/Object.h in the ClassFlags
* enum.
*/
enum {
ACC_PUBLIC = 0x00000001, // class, field, method, ic
ACC_PRIVATE = 0x00000002, // field, method, ic
ACC_PROTECTED = 0x00000004, // field, method, ic
ACC_STATIC = 0x00000008, // field, method, ic
ACC_FINAL = 0x00000010, // class, field, method, ic
ACC_SYNCHRONIZED = 0x00000020, // method (only allowed on natives)
ACC_SUPER = 0x00000020, // class (not used in Dalvik)
ACC_VOLATILE = 0x00000040, // field
ACC_BRIDGE = 0x00000040, // method (1.5)
ACC_TRANSIENT = 0x00000080, // field
ACC_VARARGS = 0x00000080, // method (1.5)
ACC_NATIVE = 0x00000100, // method
ACC_INTERFACE = 0x00000200, // class, ic
ACC_ABSTRACT = 0x00000400, // class, method, ic
ACC_STRICT = 0x00000800, // method
ACC_SYNTHETIC = 0x00001000, // field, method, ic
ACC_ANNOTATION = 0x00002000, // class, ic (1.5)
ACC_ENUM = 0x00004000, // class, field, ic (1.5)
ACC_CONSTRUCTOR = 0x00010000, // method (Dalvik only)
ACC_DECLARED_SYNCHRONIZED =
0x00020000, // method (Dalvik only)
ACC_CLASS_MASK =
(ACC_PUBLIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT
| ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
ACC_INNER_CLASS_MASK =
(ACC_CLASS_MASK | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC),
ACC_FIELD_MASK =
(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
| ACC_VOLATILE | ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
ACC_METHOD_MASK =
(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_STATIC | ACC_FINAL
| ACC_SYNCHRONIZED | ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE
| ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC | ACC_CONSTRUCTOR
| ACC_DECLARED_SYNCHRONIZED),
};

/* annotation constants */
enum {
kDexVisibilityBuild = 0x00, /* annotation visibility */
kDexVisibilityRuntime = 0x01,
kDexVisibilitySystem = 0x02,

kDexAnnotationByte = 0x00,
kDexAnnotationShort = 0x02,
kDexAnnotationChar = 0x03,
kDexAnnotationInt = 0x04,
kDexAnnotationLong = 0x06,
kDexAnnotationFloat = 0x10,
kDexAnnotationDouble = 0x11,
kDexAnnotationString = 0x17,
kDexAnnotationType = 0x18,
kDexAnnotationField = 0x19,
kDexAnnotationMethod = 0x1a,
kDexAnnotationEnum = 0x1b,
kDexAnnotationArray = 0x1c,
kDexAnnotationAnnotation = 0x1d,
kDexAnnotationNull = 0x1e,
kDexAnnotationBoolean = 0x1f,

kDexAnnotationValueTypeMask = 0x1f, /* low 5 bits */
kDexAnnotationValueArgShift = 5,
};

/* map item type codes */
enum {
kDexTypeHeaderItem = 0x0000,
kDexTypeStringIdItem = 0x0001,
kDexTypeTypeIdItem = 0x0002,
kDexTypeProtoIdItem = 0x0003,
kDexTypeFieldIdItem = 0x0004,
kDexTypeMethodIdItem = 0x0005,
kDexTypeClassDefItem = 0x0006,
kDexTypeMapList = 0x1000,
kDexTypeTypeList = 0x1001,
kDexTypeAnnotationSetRefList = 0x1002,
kDexTypeAnnotationSetItem = 0x1003,
kDexTypeClassDataItem = 0x2000,
kDexTypeCodeItem = 0x2001,
kDexTypeStringDataItem = 0x2002,
kDexTypeDebugInfoItem = 0x2003,
kDexTypeAnnotationItem = 0x2004,
kDexTypeEncodedArrayItem = 0x2005,
kDexTypeAnnotationsDirectoryItem = 0x2006,
};

/* auxillary data section chunk codes */
enum {
kDexChunkClassLookup = 0x434c4b50, /* CLKP */
kDexChunkRegisterMaps = 0x524d4150, /* RMAP */

kDexChunkEnd = 0x41454e44, /* AEND */
};

/* debug info opcodes and constants */
enum {
DBG_END_SEQUENCE = 0x00,
DBG_ADVANCE_PC = 0x01,
DBG_ADVANCE_LINE = 0x02,
DBG_START_LOCAL = 0x03,
DBG_START_LOCAL_EXTENDED = 0x04,
DBG_END_LOCAL = 0x05,
DBG_RESTART_LOCAL = 0x06,
DBG_SET_PROLOGUE_END = 0x07,
DBG_SET_EPILOGUE_BEGIN = 0x08,
DBG_SET_FILE = 0x09,
DBG_FIRST_SPECIAL = 0x0a,
DBG_LINE_BASE = -4,
DBG_LINE_RANGE = 15,
};

/*
* Direct-mapped "header_item" struct.
*/
struct DexHeader {
u1 magic[8]; /* includes version number */
u4 checksum; /* adler32 checksum */
u1 signature[kSHA1DigestLen]; /* SHA-1 hash */
u4 fileSize; /* length of entire file */
u4 headerSize; /* offset to start of next section */
u4 endianTag;
u4 linkSize;
u4 linkOff;
u4 mapOff;
u4 stringIdsSize;
u4 stringIdsOff;
u4 typeIdsSize;
u4 typeIdsOff;
u4 protoIdsSize;
u4 protoIdsOff;
u4 fieldIdsSize;
u4 fieldIdsOff;
u4 methodIdsSize;
u4 methodIdsOff;
u4 classDefsSize;
u4 classDefsOff;
u4 dataSize;
u4 dataOff;
};

/*
* Direct-mapped "map_item".
*/
struct DexMapItem {
u2 type; /* type code (see kDexType* above) */
u2 unused;
u4 size; /* count of items of the indicated type */
u4 offset; /* file offset to the start of data */
};

/*
* Direct-mapped "map_list".
*/
struct DexMapList {
u4 size; /* #of entries in list */
DexMapItem list[1]; /* entries */
};

/*
* Direct-mapped "string_id_item".
*/
struct DexStringId {
u4 stringDataOff; /* file offset to string_data_item */
};

/*
* Direct-mapped "type_id_item".
*/
struct DexTypeId {
u4 descriptorIdx; /* index into stringIds list for type descriptor */
};

/*
* Direct-mapped "field_id_item".
*/
struct DexFieldId {
u2 classIdx; /* index into typeIds list for defining class */
u2 typeIdx; /* index into typeIds for field type */
u4 nameIdx; /* index into stringIds for field name */
};

/*
* Direct-mapped "method_id_item".
*/
struct DexMethodId {
u2 classIdx; /* index into typeIds list for defining class */
u2 protoIdx; /* index into protoIds for method prototype */
u4 nameIdx; /* index into stringIds for method name */
};

/*
* Direct-mapped "proto_id_item".
*/
struct DexProtoId {
u4 shortyIdx; /* index into stringIds for shorty descriptor */
u4 returnTypeIdx; /* index into typeIds list for return type */
u4 parametersOff; /* file offset to type_list for parameter types */
};

/*
* Direct-mapped "class_def_item".
*/
struct DexClassDef {
u4 classIdx; /* index into typeIds for this class */
u4 accessFlags;
u4 superclassIdx; /* index into typeIds for superclass */
u4 interfacesOff; /* file offset to DexTypeList */
u4 sourceFileIdx; /* index into stringIds for source file name */
u4 annotationsOff; /* file offset to annotations_directory_item */
u4 classDataOff; /* file offset to class_data_item */
u4 staticValuesOff; /* file offset to DexEncodedArray */
};

/*
* Direct-mapped "type_item".
*/
struct DexTypeItem {
u2 typeIdx; /* index into typeIds */
};

/*
* Direct-mapped "type_list".
*/
struct DexTypeList {
u4 size; /* #of entries in list */
DexTypeItem list[1]; /* entries */
};

/*
* Direct-mapped "code_item".
*
* The "catches" table is used when throwing an exception,
* "debugInfo" is used when displaying an exception stack trace or
* debugging. An offset of zero indicates that there are no entries.
*/
struct DexCode {
u2 registersSize;
u2 insSize;
u2 outsSize;
u2 triesSize;
u4 debugInfoOff; /* file offset to debug info stream */
u4 insnsSize; /* size of the insns array, in u2 units */
u2 insns[1];
/* followed by optional u2 padding */
/* followed by try_item[triesSize] */
/* followed by uleb128 handlersSize */
/* followed by catch_handler_item[handlersSize] */
};

/*
* Direct-mapped "try_item".
*/
struct DexTry {
u4 startAddr; /* start address, in 16-bit code units */
u2 insnCount; /* instruction count, in 16-bit code units */
u2 handlerOff; /* offset in encoded handler data to handlers */
};

/*
* Link table. Currently undefined.
*/
struct DexLink {
u1 bleargh;
};


/*
* Direct-mapped "annotations_directory_item".
*/
struct DexAnnotationsDirectoryItem {
u4 classAnnotationsOff; /* offset to DexAnnotationSetItem */
u4 fieldsSize; /* count of DexFieldAnnotationsItem */
u4 methodsSize; /* count of DexMethodAnnotationsItem */
u4 parametersSize; /* count of DexParameterAnnotationsItem */
/* followed by DexFieldAnnotationsItem[fieldsSize] */
/* followed by DexMethodAnnotationsItem[methodsSize] */
/* followed by DexParameterAnnotationsItem[parametersSize] */
};

/*
* Direct-mapped "field_annotations_item".
*/
struct DexFieldAnnotationsItem {
u4 fieldIdx;
u4 annotationsOff; /* offset to DexAnnotationSetItem */
};

/*
* Direct-mapped "method_annotations_item".
*/
struct DexMethodAnnotationsItem {
u4 methodIdx;
u4 annotationsOff; /* offset to DexAnnotationSetItem */
};

/*
* Direct-mapped "parameter_annotations_item".
*/
struct DexParameterAnnotationsItem {
u4 methodIdx;
u4 annotationsOff; /* offset to DexAnotationSetRefList */
};

/*
* Direct-mapped "annotation_set_ref_item".
*/
struct DexAnnotationSetRefItem {
u4 annotationsOff; /* offset to DexAnnotationSetItem */
};

/*
* Direct-mapped "annotation_set_ref_list".
*/
struct DexAnnotationSetRefList {
u4 size;
DexAnnotationSetRefItem list[1];
};

/*
* Direct-mapped "annotation_set_item".
*/
struct DexAnnotationSetItem {
u4 size;
u4 entries[1]; /* offset to DexAnnotationItem */
};

/*
* Direct-mapped "annotation_item".
*
* NOTE: this structure is byte-aligned.
*/
struct DexAnnotationItem {
u1 visibility;
u1 annotation[1]; /* data in encoded_annotation format */
};

/*
* Direct-mapped "encoded_array".
*
* NOTE: this structure is byte-aligned.
*/
struct DexEncodedArray {
u1 array[1]; /* data in encoded_array format */
};

/*
* Lookup table for classes. It provides a mapping from class name to
* class definition. Used by dexFindClass().
*
* We calculate this at DEX optimization time and embed it in the file so we
* don't need the same hash table in every VM. This is slightly slower than
* a hash table with direct pointers to the items, but because it's shared
* there's less of a penalty for using a fairly sparse table.
*/
struct DexClassLookup {
int size; // total size, including "size"
int numEntries; // size of table[]; always power of 2
struct {
u4 classDescriptorHash; // class descriptor hash code
int classDescriptorOffset; // in bytes, from start of DEX
int classDefOffset; // in bytes, from start of DEX
} table[1];
};

/*
* Header added by DEX optimization pass. Values are always written in
* local byte and structure padding. The first field (magic + version)
* is guaranteed to be present and directly readable for all expected
* compiler configurations; the rest is version-dependent.
*
* Try to keep this simple and fixed-size.
*/
struct DexOptHeader {
u1 magic[8]; /* includes version number */

u4 dexOffset; /* file offset of DEX header */
u4 dexLength;
u4 depsOffset; /* offset of optimized DEX dependency table */
u4 depsLength;
u4 optOffset; /* file offset of optimized data tables */
u4 optLength;

u4 flags; /* some info flags */
u4 checksum; /* adler32 checksum covering deps/opt */

/* pad for 64-bit alignment if necessary */
};

#define DEX_OPT_FLAG_BIG (1<<1) /* swapped to big-endian */

#define DEX_INTERFACE_CACHE_SIZE 128 /* must be power of 2 */

/*
* Structure representing a DEX file.
*
* Code should regard DexFile as opaque, using the API calls provided here
* to access specific structures.
*/
struct DexFile {
/* directly-mapped "opt" header */
const DexOptHeader* pOptHeader;

/* pointers to directly-mapped structs and arrays in base DEX */
const DexHeader* pHeader;
const DexStringId* pStringIds;
const DexTypeId* pTypeIds;
const DexFieldId* pFieldIds;
const DexMethodId* pMethodIds;
const DexProtoId* pProtoIds;
const DexClassDef* pClassDefs;
const DexLink* pLinkData;

/*
* These are mapped out of the "auxillary" section, and may not be
* included in the file.
*/
const DexClassLookup* pClassLookup;
const void* pRegisterMapPool; // RegisterMapClassPool

/* points to start of DEX file data */
const u1* baseAddr;

/* track memory overhead for auxillary structures */
int overhead;

/* additional app-specific data structures associated with the DEX */
//void* auxData;
};

/*
* Utility function -- rounds up to the nearest power of 2.
*/
u4 dexRoundUpPower2(u4 val);

/*
* Parse an optimized or unoptimized .dex file sitting in memory.
*
* On success, return a newly-allocated DexFile.
*/
DexFile* dexFileParse(const u1* data, size_t length, int flags);

/* bit values for "flags" argument to dexFileParse */
enum {
kDexParseDefault = 0,
kDexParseVerifyChecksum = 1,
kDexParseContinueOnError = (1 << 1),
};

/*
* Fix the byte ordering of all fields in the DEX file, and do
* structural verification. This is only required for code that opens
* "raw" DEX files, such as the DEX optimizer.
*
* Return 0 on success.
*/
int dexSwapAndVerify(u1* addr, int len);

/*
* Detect the file type of the given memory buffer via magic number.
* Call dexSwapAndVerify() on an unoptimized DEX file, do nothing
* but return successfully on an optimized DEX file, and report an
* error for all other cases.
*
* Return 0 on success.
*/
int dexSwapAndVerifyIfNecessary(u1* addr, int len);

/*
* Check to see if the file magic and format version in the given
* header are recognized as valid. Returns true if they are
* acceptable.
*/
bool dexHasValidMagic(const DexHeader* pHeader);

/*
* Compute DEX checksum.
*/
u4 dexComputeChecksum(const DexHeader* pHeader);

/*
* Free a DexFile structure, along with any associated structures.
*/
void dexFileFree(DexFile* pDexFile);

/*
* Create class lookup table.
*/
DexClassLookup* dexCreateClassLookup(DexFile* pDexFile);

/*
* Find a class definition by descriptor.
*/
const DexClassDef* dexFindClass(const DexFile* pFile, const char* descriptor);

/*
* Set up the basic raw data pointers of a DexFile. This function isn't
* meant for general use.
*/
void dexFileSetupBasicPointers(DexFile* pDexFile, const u1* data);

/* return the DexMapList of the file, if any */
DEX_INLINE const DexMapList* dexGetMap(const DexFile* pDexFile) {
u4 mapOff = pDexFile->pHeader->mapOff;

if (mapOff == 0) {
return NULL;
} else {
return (const DexMapList*) (pDexFile->baseAddr + mapOff);
}
}

/* return the const char* string data referred to by the given string_id */
DEX_INLINE const char* dexGetStringData(const DexFile* pDexFile,
const DexStringId* pStringId) {
const u1* ptr = pDexFile->baseAddr + pStringId->stringDataOff;

// Skip the uleb128 length.
while (*(ptr++) > 0x7f) /* empty */ ;

return (const char*) ptr;
}
/* return the StringId with the specified index */
DEX_INLINE const DexStringId* dexGetStringId(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->stringIdsSize);
return &pDexFile->pStringIds[idx];
}
/* return the UTF-8 encoded string with the specified string_id index */
DEX_INLINE const char* dexStringById(const DexFile* pDexFile, u4 idx) {
const DexStringId* pStringId = dexGetStringId(pDexFile, idx);
return dexGetStringData(pDexFile, pStringId);
}

/* Return the UTF-8 encoded string with the specified string_id index,
* also filling in the UTF-16 size (number of 16-bit code points).*/
const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
u4* utf16Size);

/* return the TypeId with the specified index */
DEX_INLINE const DexTypeId* dexGetTypeId(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->typeIdsSize);
return &pDexFile->pTypeIds[idx];
}

/*
* Get the descriptor string associated with a given type index.
* The caller should not free() the returned string.
*/
DEX_INLINE const char* dexStringByTypeIdx(const DexFile* pDexFile, u4 idx) {
const DexTypeId* typeId = dexGetTypeId(pDexFile, idx);
return dexStringById(pDexFile, typeId->descriptorIdx);
}

/* return the MethodId with the specified index */
DEX_INLINE const DexMethodId* dexGetMethodId(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->methodIdsSize);
return &pDexFile->pMethodIds[idx];
}

/* return the FieldId with the specified index */
DEX_INLINE const DexFieldId* dexGetFieldId(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->fieldIdsSize);
return &pDexFile->pFieldIds[idx];
}

/* return the ProtoId with the specified index */
DEX_INLINE const DexProtoId* dexGetProtoId(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->protoIdsSize);
return &pDexFile->pProtoIds[idx];
}

/*
* Get the parameter list from a ProtoId. The returns NULL if the ProtoId
* does not have a parameter list.
*/
DEX_INLINE const DexTypeList* dexGetProtoParameters(
const DexFile *pDexFile, const DexProtoId* pProtoId) {
if (pProtoId->parametersOff == 0) {
return NULL;
}
return (const DexTypeList*)
(pDexFile->baseAddr + pProtoId->parametersOff);
}

/* return the ClassDef with the specified index */
DEX_INLINE const DexClassDef* dexGetClassDef(const DexFile* pDexFile, u4 idx) {
assert(idx < pDexFile->pHeader->classDefsSize);
return &pDexFile->pClassDefs[idx];
}

/* given a ClassDef pointer, recover its index */
DEX_INLINE u4 dexGetIndexForClassDef(const DexFile* pDexFile,
const DexClassDef* pClassDef)
{
assert(pClassDef >= pDexFile->pClassDefs &&
pClassDef < pDexFile->pClassDefs + pDexFile->pHeader->classDefsSize);
return pClassDef - pDexFile->pClassDefs;
}

/* get the interface list for a DexClass */
DEX_INLINE const DexTypeList* dexGetInterfacesList(const DexFile* pDexFile,
const DexClassDef* pClassDef)
{
if (pClassDef->interfacesOff == 0)
return NULL;
return (const DexTypeList*)
(pDexFile->baseAddr + pClassDef->interfacesOff);
}
/* return the Nth entry in a DexTypeList. */
DEX_INLINE const DexTypeItem* dexGetTypeItem(const DexTypeList* pList,
u4 idx)
{
assert(idx < pList->size);
return &pList->list[idx];
}
/* return the type_idx for the Nth entry in a TypeList */
DEX_INLINE u4 dexTypeListGetIdx(const DexTypeList* pList, u4 idx) {
const DexTypeItem* pItem = dexGetTypeItem(pList, idx);
return pItem->typeIdx;
}

/* get the static values list for a DexClass */
DEX_INLINE const DexEncodedArray* dexGetStaticValuesList(
const DexFile* pDexFile, const DexClassDef* pClassDef)
{
if (pClassDef->staticValuesOff == 0)
return NULL;
return (const DexEncodedArray*)
(pDexFile->baseAddr + pClassDef->staticValuesOff);
}

/* get the annotations directory item for a DexClass */
DEX_INLINE const DexAnnotationsDirectoryItem* dexGetAnnotationsDirectoryItem(
const DexFile* pDexFile, const DexClassDef* pClassDef)
{
if (pClassDef->annotationsOff == 0)
return NULL;
return (const DexAnnotationsDirectoryItem*)
(pDexFile->baseAddr + pClassDef->annotationsOff);
}

/* get the source file string */
DEX_INLINE const char* dexGetSourceFile(
const DexFile* pDexFile, const DexClassDef* pClassDef)
{
if (pClassDef->sourceFileIdx == 0xffffffff)
return NULL;
return dexStringById(pDexFile, pClassDef->sourceFileIdx);
}

/* get the size, in bytes, of a DexCode */
size_t dexGetDexCodeSize(const DexCode* pCode);

/* Get the list of "tries" for the given DexCode. */
DEX_INLINE const DexTry* dexGetTries(const DexCode* pCode) {
const u2* insnsEnd = &pCode->insns[pCode->insnsSize];

// Round to four bytes.
if ((((uintptr_t) insnsEnd) & 3) != 0) {
insnsEnd++;
}

return (const DexTry*) insnsEnd;
}

/* Get the base of the encoded data for the given DexCode. */
DEX_INLINE const u1* dexGetCatchHandlerData(const DexCode* pCode) {
const DexTry* pTries = dexGetTries(pCode);
return (const u1*) &pTries[pCode->triesSize];
}

/* get a pointer to the start of the debugging data */
DEX_INLINE const u1* dexGetDebugInfoStream(const DexFile* pDexFile,
const DexCode* pCode)
{
if (pCode->debugInfoOff == 0) {
return NULL;
} else {
return pDexFile->baseAddr + pCode->debugInfoOff;
}
}

/* DexClassDef convenience - get class descriptor */
DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
const DexClassDef* pClassDef)
{
return dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
}

/* DexClassDef convenience - get superclass descriptor */
DEX_INLINE const char* dexGetSuperClassDescriptor(const DexFile* pDexFile,
const DexClassDef* pClassDef)
{
if (pClassDef->superclassIdx == 0)
return NULL;
return dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
}

/* DexClassDef convenience - get class_data_item pointer */
DEX_INLINE const u1* dexGetClassData(const DexFile* pDexFile,
const DexClassDef* pClassDef)
{
if (pClassDef->classDataOff == 0)
return NULL;
return (const u1*) (pDexFile->baseAddr + pClassDef->classDataOff);
}

/* Get an annotation set at a particular offset. */
DEX_INLINE const DexAnnotationSetItem* dexGetAnnotationSetItem(
const DexFile* pDexFile, u4 offset)
{
if (offset == 0) {
return NULL;
}
return (const DexAnnotationSetItem*) (pDexFile->baseAddr + offset);
}
/* get the class' annotation set */
DEX_INLINE const DexAnnotationSetItem* dexGetClassAnnotationSet(
const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
{
return dexGetAnnotationSetItem(pDexFile, pAnnoDir->classAnnotationsOff);
}

/* get the class' field annotation list */
DEX_INLINE const DexFieldAnnotationsItem* dexGetFieldAnnotations(
const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
{
if (pAnnoDir->fieldsSize == 0)
return NULL;

// Skip past the header to the start of the field annotations.
return (const DexFieldAnnotationsItem*) &pAnnoDir[1];
}

/* get field annotation list size */
DEX_INLINE int dexGetFieldAnnotationsSize(const DexFile* pDexFile,
const DexAnnotationsDirectoryItem* pAnnoDir)
{
return pAnnoDir->fieldsSize;
}

/* return a pointer to the field's annotation set */
DEX_INLINE const DexAnnotationSetItem* dexGetFieldAnnotationSetItem(
const DexFile* pDexFile, const DexFieldAnnotationsItem* pItem)
{
return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
}

/* get the class' method annotation list */
DEX_INLINE const DexMethodAnnotationsItem* dexGetMethodAnnotations(
const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
{
if (pAnnoDir->methodsSize == 0)
return NULL;

/*
* Skip past the header and field annotations to the start of the
* method annotations.
*/
const u1* addr = (const u1*) &pAnnoDir[1];
addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
return (const DexMethodAnnotationsItem*) addr;
}

/* get method annotation list size */
DEX_INLINE int dexGetMethodAnnotationsSize(const DexFile* pDexFile,
const DexAnnotationsDirectoryItem* pAnnoDir)
{
return pAnnoDir->methodsSize;
}

/* return a pointer to the method's annotation set */
DEX_INLINE const DexAnnotationSetItem* dexGetMethodAnnotationSetItem(
const DexFile* pDexFile, const DexMethodAnnotationsItem* pItem)
{
return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
}

/* get the class' parameter annotation list */
DEX_INLINE const DexParameterAnnotationsItem* dexGetParameterAnnotations(
const DexFile* pDexFile, const DexAnnotationsDirectoryItem* pAnnoDir)
{
if (pAnnoDir->parametersSize == 0)
return NULL;

/*
* Skip past the header, field annotations, and method annotations
* to the start of the parameter annotations.
*/
const u1* addr = (const u1*) &pAnnoDir[1];
addr += pAnnoDir->fieldsSize * sizeof (DexFieldAnnotationsItem);
addr += pAnnoDir->methodsSize * sizeof (DexMethodAnnotationsItem);
return (const DexParameterAnnotationsItem*) addr;
}

/* get method annotation list size */
DEX_INLINE int dexGetParameterAnnotationsSize(const DexFile* pDexFile,
const DexAnnotationsDirectoryItem* pAnnoDir)
{
return pAnnoDir->parametersSize;
}

/* return the parameter annotation ref list */
DEX_INLINE const DexAnnotationSetRefList* dexGetParameterAnnotationSetRefList(
const DexFile* pDexFile, const DexParameterAnnotationsItem* pItem)
{
if (pItem->annotationsOff == 0) {
return NULL;
}
return (const DexAnnotationSetRefList*) (pDexFile->baseAddr + pItem->annotationsOff);
}

/* get method annotation list size */
DEX_INLINE int dexGetParameterAnnotationSetRefSize(const DexFile* pDexFile,
const DexParameterAnnotationsItem* pItem)
{
if (pItem->annotationsOff == 0) {
return 0;
}
return dexGetParameterAnnotationSetRefList(pDexFile, pItem)->size;
}

/* return the Nth entry from an annotation set ref list */
DEX_INLINE const DexAnnotationSetRefItem* dexGetParameterAnnotationSetRef(
const DexAnnotationSetRefList* pList, u4 idx)
{
assert(idx < pList->size);
return &pList->list[idx];
}

/* given a DexAnnotationSetRefItem, return the DexAnnotationSetItem */
DEX_INLINE const DexAnnotationSetItem* dexGetSetRefItemItem(
const DexFile* pDexFile, const DexAnnotationSetRefItem* pItem)
{
return dexGetAnnotationSetItem(pDexFile, pItem->annotationsOff);
}

/* return the Nth annotation offset from a DexAnnotationSetItem */
DEX_INLINE u4 dexGetAnnotationOff(
const DexAnnotationSetItem* pAnnoSet, u4 idx)
{
assert(idx < pAnnoSet->size);
return pAnnoSet->entries[idx];
}

/* return the Nth annotation item from a DexAnnotationSetItem */
DEX_INLINE const DexAnnotationItem* dexGetAnnotationItem(
const DexFile* pDexFile, const DexAnnotationSetItem* pAnnoSet, u4 idx)
{
u4 offset = dexGetAnnotationOff(pAnnoSet, idx);
if (offset == 0) {
return NULL;
}
return (const DexAnnotationItem*) (pDexFile->baseAddr + offset);
}

/*
* Get the type descriptor character associated with a given primitive
* type. This returns '\0' if the type is invalid.
*/
char dexGetPrimitiveTypeDescriptorChar(PrimitiveType type);

/*
* Get the type descriptor string associated with a given primitive
* type.
*/
const char* dexGetPrimitiveTypeDescriptor(PrimitiveType type);

/*
* Get the boxed type descriptor string associated with a given
* primitive type. This returns NULL for an invalid type, including
* particularly for type "void". In the latter case, even though there
* is a class Void, there's no such thing as a boxed instance of it.
*/
const char* dexGetBoxedTypeDescriptor(PrimitiveType type);

/*
* Get the primitive type constant from the given descriptor character.
* This returns PRIM_NOT (note: this is a 0) if the character is invalid
* as a primitive type descriptor.
*/
PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);

#endif // LIBDEX_DEXFILE_H_

DexType.java

package com.pediy.test;

//import org.jf.dexlib.Code.Instruction;
//import org.jf.dexlib.Code.InstructionIterator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DexType {

public DexHeader dexHeader;
public ArrayList<DexStringId> dexStringId; //�����ַ���ƫ�ƴ洢
public ArrayList<String > dexStringList;//�����ַ���
public ArrayList<DexTypeId> dexTypeId;//����dexTypeID
public ArrayList<String > dexTypeList;//����type
public ArrayList<DexProtoId> dexProtoId;//����dexProtoID
public ArrayList<String > dexProtoIdString;
public ArrayList<String > dexProtoIdType;
public ArrayList<DexTypeList> dexTypeLists;
public ArrayList<String > dexProtoList;
public ArrayList<DexFieldId> dexFieldId;
public ArrayList<String > dexFieldList;
public ArrayList<DexMethodId> dexMethodId;
public ArrayList<String > dexMethodList;
public ArrayList<DexClassDef> dexClassDefs;
public ArrayList<DexClassData> dexClassData;
public DexMapList dexMapLists;


//���캯��
DexType(){
dexHeader = new DexHeader();

dexStringId = new ArrayList<DexStringId>();
dexStringList = new ArrayList<String >();

dexTypeId = new ArrayList<DexTypeId >();
dexTypeList = new ArrayList<String >();
dexTypeLists = new ArrayList<DexTypeList>();

dexProtoId = new ArrayList<DexProtoId>();
dexProtoIdString = new ArrayList<String >();
dexProtoIdType = new ArrayList<String >();
dexProtoList = new ArrayList<String >();

dexFieldId = new ArrayList<DexFieldId>();
dexFieldList = new ArrayList<String >();

dexMethodId = new ArrayList<DexMethodId>();
dexMethodList = new ArrayList<String >();

dexClassDefs = new ArrayList<DexClassDef>();
dexClassData = new ArrayList<DexClassData>();

dexMapLists = new DexMapList();
}

/**
* typedef struct DexHeader{
* u1 magic[8]; //dex�汾��ʶ8��1�ֽ�
* u4 checksum; //adler32У��1��4�ֽ�
* u1 signature[kSHA1DigestLen]; //SHA-1��ϣֵ
* u4 fileSize; //�����ļ��Ĵ�С
* u4 headerSize; //DexHeader�Ľṹ��С
* u4 endianTag; //�ֽ�����
* u4 linkSize; //���Ӷδ�С
* u4 linkOff; //���Ӷ�ƫ��
* u4 mapOff; //DexMapList���ļ�ƫ��
* u4 stringIdsSize; //DexStringId�ĸ���
* u4 stringIdsOff; //DexStringId���ļ�ƫ��
* u4 typeIdsSize; //DexTupeID�ĸ���
* u4 typeIdsOff; //DexTypeId���ļ�ƫ��
* u4 protoIdsSize; //DexProtoId�ĸ���
* u4 protoIdsOff; //DexProtoId���ļ�ƫ��
* u4 fieldIdsSize; //DexFieldId�ĸ���
* u4 fieldIdsOff; //DexFieldId���ļ�ƫ��
* u4 methodIdsSize; //DexMethodId�ĸ���
* u4 methodIdsOff; //DexMethodID���ļ�ƫ��
* u4 classDefsSize; //DexClassDef�ĸ���
* u4 classDefsOff; //DexClassDef���ļ�ƫ��
* u4 dataSize; //���ݶεĴ�С
* u4 dataOff; //���ݶε��ļ�ƫ��
* }
*/
public class DexHeader{
public byte[] magic = new byte[8]; //dex�汾��ʶ
public byte[] checksum = new byte[4]; //adler32��
public byte[] signature = new byte[20]; //sha-1��ϣֵ
public byte[] fileSize = new byte[4]; //�����ļ��Ĵ�С
public byte[] headerSize = new byte[4]; //DexHeader�ṹ��С
public byte[] endianTag = new byte[4]; //����
public byte[] linkSize = new byte[4]; //���Ӷδ�С
public byte[] linkOff = new byte[4]; //���Ӷ�ƫ��
public byte[] mapOff = new byte[4]; //DexMapList���ļ�ƫ��
public byte[] stringIdsSize = new byte[4]; //DexStringId�ĸ���
public byte[] stringIdsOff = new byte[4]; //DexStringId���ļ�ƫ��
public byte[] typeIdsSize = new byte[4]; //
public byte[] typeIdsOff = new byte[4]; //
public byte[] protoIdsSize = new byte[4]; //
public byte[] protoIdsOff = new byte[4]; //
public byte[] fieldIdsSize = new byte[4]; //
public byte[] fieldIdsOff = new byte[4]; //
public byte[] methodIdsSize = new byte[4]; //
public byte[] methodIdsOff = new byte[4]; //
public byte[] classDefsSize = new byte[4]; //
public byte[] classDefsOff = new byte[4]; //
public byte[] dataSize = new byte[4]; //
public byte[] dataOff = new byte[4]; //

public List<byte[]> em = new ArrayList<>();

public DexHeader() {
this.em.add(magic);
this.em.add(checksum);
this.em.add(signature);
this.em.add(fileSize);
this.em.add(headerSize);
this.em.add(endianTag);
this.em.add(linkSize);
this.em.add(linkOff);
this.em.add(mapOff);
this.em.add(stringIdsSize);
this.em.add(stringIdsOff);
this.em.add(typeIdsSize);
this.em.add(typeIdsOff);
this.em.add(protoIdsSize);
this.em.add(protoIdsOff);
this.em.add(fieldIdsSize);
this.em.add(fieldIdsOff);
this.em.add(methodIdsSize);
this.em.add(methodIdsOff);
this.em.add(classDefsSize);
this.em.add(classDefsOff);
this.em.add(dataSize);
this.em.add(dataOff);
}
}

/**
* typedef struct DexStringId{
* u4 stringDataOff;
* }
*/
public class DexStringId{
public byte[] stringDataOff= new byte[4];
}

/**
* typedef struct DexTypeId{
* u4 descriptorIdx;
* }
*/
public class DexTypeId{
public byte[] descriptorIdx=new byte[4];
}

/**
* typedef struct DexProtoId{
* u4 shortyIdx;
* u4 returnTypeIdx;
* u4 parametersOff;
* }
*/
public class DexProtoId{
public byte[] shortyIdx=new byte[4];
public byte[] returnTypeIdx=new byte[4];
public byte[] parametersOff=new byte[4];
public List<DexTypeItem> dexTypeItemList = new ArrayList<>();
}

/**
* typedef struct DexTypeItem{
* u2 typeIdx;
* }
*/
public class DexTypeItem{
public byte[] typeIdx=new byte[2];
}

/**
* typedef struct DexTypeList{
* u4 size;
* DexTypeItem list[11;
* }
*/
public class DexTypeList{
public byte[] size=new byte[4];
public ArrayList<DexTypeItem> list=new ArrayList<DexTypeItem>();
}

/**
* typedef struct DexFieldId{
* u4 classIdx;
* u4 typeIdx;
* u4 nameIdx;
* }
*/
public class DexFieldId{
public byte[] classIdx=new byte[2];
public byte[] typeIdx=new byte[2];
public byte[] nameIdx=new byte[4];
}

/**
* typedef struct DexMethodId{
* u4 classIdx;
* u4 protoIdx;
* u4 nameIdx;
* }
*/
public class DexMethodId{
public byte[] classIdx=new byte[2];
public byte[] protoIdx=new byte[2];
public byte[] nameIdx=new byte[4];
}

/**
* typedef struct DexClassDef{
* u4 classIdx;
* u4 accessFlags;
* u4 superclassIdx;
* u4 interfacesOff;
* u4 sourceFileIdx;
* u4 annotationsOff;
* u4 classDataOff;
* u4 staticValuesOff;
* }
*/
public class DexClassDef{
public byte[] classIdx=new byte[4];
public byte[] accessFlags=new byte[4];
public byte[] superclassIdx=new byte[4];
public byte[] interfacesOff=new byte[4];
public byte[] sourceFileIdx=new byte[4];
public byte[] annotationsOff=new byte[4];
public byte[] classDataOff=new byte[4];
public byte[] staticValuesOff=new byte[4];

}


/**
* typedef struct DexClassDataItem{
* u4 staticFieldsSize;
* u4 instanceFieldsSize;
* u4 directMethodsSize;
* u4 virtualMethodsSize;
* }
*/
public class DexClassDataItem{
public byte[] staticFieldsSize = new byte[5];
public byte[] instanceFieldsSize = new byte[5];
public byte[] directMethodsSize = new byte[5];
public byte[] virtualMethodsSize = new byte[5];

}

public class DexCodeItem{
short registers_size ;
short ins_size ;
short outs_size ;
short tries_size ;
int debug_info_off ;
int insns_size;
List<byte[]> insns ;

public DexCodeItem(byte[] pBuff, int pOff) {
registers_size = Utils.byte2Short_2(pBuff, pOff + 0);
ins_size = Utils.byte2Short_2(pBuff, pOff + 2);
outs_size = Utils.byte2Short_2(pBuff, pOff + 4);
tries_size = Utils.byte2Short_2(pBuff, pOff + 6);
debug_info_off = Utils.byte2Int_4(pBuff, pOff + 8);
insns_size = Utils.byte2Int_4(pBuff, pOff + 12);
insns = new ArrayList<>();

System.out.printf("\tCodeItem:\t.registers:%d, ������������:%d, ���������������ʱ��Ҫ�IJ�������:%d, try_item����:%d, %d,%d\r\n", registers_size, ins_size, outs_size, tries_size, debug_info_off, insns_size);
byte[] ttmp = new byte[insns_size*2];
System.arraycopy(pBuff, pOff+16, ttmp, 0, ttmp.length);
ParseDex.getOpcodeByIns2(ttmp);

// for (int i=0; i<insns_size; i++){
// byte[] tmp = new byte[2];
// System.arraycopy(pBuff, pOff + 16+i*2, tmp, 0, 2);
// insns.add(tmp);
//// System.out.printf("\t\t"+ParseDex.getOpcodeByIns2(tmp));
//// System.out.printf(": " + Utils.bytes2HexString(tmp) + "\r\n");
// }
// System.out.println("");

}

public int getSize_CodeItem(){
return insns_size;
}
}

/**
* typedef struct DexField{
* u4 fieldIdx;
* u4 accessFlags;
* }
*/
public class DexField{
public byte[] fieldIdx = new byte[5];
public byte[] accessFlags = new byte[5];
}

/**
* typedef struct DexMethod{
* u4 methodIdx;
* u4 accessFlags;
* u4 codoOff;
* }
*/
public class DexMethod{
public byte[] methodIdx = new byte[5];
public byte[] accessFlags = new byte[5];
public byte[] codeOff = new byte[5];
}

/**
* typedef struct DexClassData{
* DexClassDataItem header;
* DexField* staticFields;
* DexField* instanceFields;
* DexMethod* directMethods;
* DexMethod* virtualMethods
* }
*/
public class DexClassData{
public ArrayList<DexClassDataItem> dexClassDataItems = new ArrayList<DexClassDataItem>();
public ArrayList<DexField> staticFields = new ArrayList<DexField>();
public ArrayList<DexField> instanceFields = new ArrayList<DexField>();
public ArrayList<DexMethod> directMethods = new ArrayList<DexMethod>();
public ArrayList<DexMethod> virtualMethods = new ArrayList<DexMethod>();
}

/**
* typedef struct DexMapItem{
* u2 type;
* u2 unused;
* u4 size;
* u4 offset;
* }
*/
public class DexMapItem{
public byte[] type=new byte[2];
public byte[] unused=new byte[2];
public byte[] size=new byte[4];
public byte[] offset=new byte[4];
}



/**
* typedef struct DexMapList{
* u4 size;
* DexMapItem list[11;
* }
*/
public class DexMapList{
public byte[] size=new byte[4];
public ArrayList<DexMapItem> list=new ArrayList<DexMapItem>();
public Map<Integer, String> type_code = new HashMap<>();

public DexMapList() {
type_code.put(0x0000, "TYPE_HEADER_ITEM");
type_code.put(0x0001, "TYPE_STRING_ID_ITEM");
type_code.put(0x0002, "TYPE_TYPE_ID_ITEM");
type_code.put(0x0003, "TYPE_PROTO_ID_ITEM");
type_code.put(0x0004, "TYPE_FIELD_ID_ITEM");
type_code.put(0x0005, "TYPE_METHOD_ID_ITEM");
type_code.put(0x0006, "TYPE_CLASS_DEF_ITEM");
type_code.put(0x1000, "TYPE_MAP_LIST");
type_code.put(0x1001, "TYPE_TYPE_LIST");
type_code.put(0x1002, "TYPE_ANNOTATION_SET_REF_LIST");
type_code.put(0x1003, "TYPE_ANNOTATION_SET_ITEM");
type_code.put(0x2000, "TYPE_CLASS_DATA_ITEM");
type_code.put(0x2001, "TYPE_CODE_ITEM");
type_code.put(0x2002, "TYPE_STRING_DATA_ITEM");
type_code.put(0x2003, "TYPE_DEBUG_INFO_ITEM");
type_code.put(0x2004, "TYPE_ANNOTATION_ITEM");
type_code.put(0x2005, "TYPE_ENCODED_ARRAY_ITEM");
type_code.put(0x2006, "TYPE_ANNOTATIONS_DIRECTORY_ITEM");
}
}
}

Main.java

package com.pediy.test;

public class Main {
public static void main(String[] args) {
String dexPath = "HelloWorld.dex";
byte[] fileByteArrays = Utils.readFile(dexPath);
if (fileByteArrays == null){
System.out.println("read file filed");
return;
}
ParseDex parseDex = new ParseDex(fileByteArrays);
parseDex.parse();
}
}

ParseDex.java

package com.pediy.test;

import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.Opcodes;

public class ParseDex {

public static byte[] byteData;
private DexType dexType;


public ParseDex(byte[] pArr) {
byteData = pArr;
dexType = new DexType();
}


public void parse(){
parseHeader(0);
printHeader();

int stringOff = Utils.byte2Int_4(dexType.dexHeader.stringIdsOff, 0);
int stringSize = Utils.byte2Int_4(dexType.dexHeader.stringIdsSize, 0);
readDexStringId(stringOff, stringSize);
printStrings();

int typeOff = Utils.byte2Int_4(dexType.dexHeader.typeIdsOff, 0);
int typeSize = Utils.byte2Int_4(dexType.dexHeader.typeIdsSize, 0);
readDexTypeId(typeOff, typeSize);
printTypes();

int protoOff = Utils.byte2Int_4(dexType.dexHeader.protoIdsOff, 0);
int protoSize = Utils.byte2Int_4(dexType.dexHeader.protoIdsSize, 0);
readDexProtoId(protoOff, protoSize);

int fieldOff = Utils.byte2Int_4(dexType.dexHeader.fieldIdsOff, 0);
int fieldSize = Utils.byte2Int_4(dexType.dexHeader.fieldIdsSize, 0);
readDexFieldId(fieldOff, fieldSize);

int methodOff = Utils.byte2Int_4(dexType.dexHeader.methodIdsOff, 0);
int methodSize = Utils.byte2Int_4(dexType.dexHeader.methodIdsSize, 0);
readDexMethodId(methodOff, methodSize);

int classOff = Utils.byte2Int_4(dexType.dexHeader.classDefsOff, 0);
int classSize = Utils.byte2Int_4(dexType.dexHeader.classDefsSize, 0);
readDexClassDefsId(classOff, classSize);

}

private String getStringById(int pId){
return dexType.dexStringList.get(pId);
}

private String getTypeStringById(int pId){
return dexType.dexTypeList.get(pId);
}

private String getProtoStringById(int pId){
return dexType.dexProtoList.get(pId);
}

private String getFieldStringById(int pId){
return dexType.dexFieldList.get(pId);
}

private String getMethodStringById(int pId){
return dexType.dexMethodList.get(pId);
}

private void parseHeader(int offset){
if (byteData == null){
System.out.println("byteData is null");
}

// System.arraycopy(byteData, 0, dexType.dexHeader.magic, 0, dexType.dexHeader.magic.length);
// System.arraycopy(byteData, 0+dexType.dexHeader.magic.length, dexType.dexHeader.checksum, 0, dexType.dexHeader.checksum.length);
// System.arraycopy(byteData, 0+dexType.dexHeader.checksum.length, dexType.dexHeader.signature, 0, dexType.dexHeader.signature.length);

int curPos = 0;
for (int i=0; i< dexType.dexHeader.em.size(); i++){
System.arraycopy(byteData, curPos, dexType.dexHeader.em.get(i), 0, dexType.dexHeader.em.get(i).length);
curPos += dexType.dexHeader.em.get(i).length;
}
}

private void printHeader(){
System.out.printf("%-12s: %s\r\n", "magic", Utils.bytes2HexString(dexType.dexHeader.magic));
System.out.printf("%-12s: %s\r\n", "checksum", Utils.bytes2HexString(dexType.dexHeader.checksum));
System.out.printf("%-12s: %s\r\n", "signature", Utils.bytes2HexString(dexType.dexHeader.signature));
}

private void readDexStringId(int pOff, int pCount){
for (int i=0; i<pCount; i++){
DexType.DexStringId t_dexStringId = dexType.new DexStringId();
System.arraycopy(byteData, pOff+i*4, t_dexStringId.stringDataOff, 0, t_dexStringId.stringDataOff.length);

int dexStringId = Utils.byte2Int_4(t_dexStringId.stringDataOff, 0);
Utils.RETULEB128 tmp = Utils.readULEB128(byteData, dexStringId);
byte[] strContent = Utils.copyNewBytes(byteData, dexStringId+tmp.readSize, tmp.retValue);
String realString = new String(strContent);

dexType.dexStringId.add(t_dexStringId);
dexType.dexStringList.add(realString);
}
}

private void printStrings(){
System.out.println("------------strings----------------");
for (int i=0; i<dexType.dexStringList.size(); i++){
System.out.println("\t" + dexType.dexStringList.get(i));
}
}

private void readDexTypeId(int pOff, int pCount){
for (int i=0; i<pCount; i++){
DexType.DexTypeId t_dexTypeId = dexType.new DexTypeId();
System.arraycopy(byteData, pOff+i*4, t_dexTypeId.descriptorIdx, 0, t_dexTypeId.descriptorIdx.length);

int dexTypeId = Utils.byte2Int_4(t_dexTypeId.descriptorIdx, 0);
dexType.dexTypeId.add(t_dexTypeId);
dexType.dexTypeList.add(getStringById(dexTypeId));
}
}

private void printTypes(){
System.out.println("------------types----------------");
for (String one:dexType.dexTypeList) {
System.out.println("\t" + one);
}
}

private void readDexProtoId(int pOff, int pCount){
for (int i=0; i<pCount; i++){
DexType.DexProtoId t_dexProtoId = dexType.new DexProtoId();
System.arraycopy(byteData, pOff+12*i+0, t_dexProtoId.shortyIdx, 0, t_dexProtoId.shortyIdx.length);
System.arraycopy(byteData, pOff+12*i+4, t_dexProtoId.returnTypeIdx, 0, t_dexProtoId.returnTypeIdx.length);
System.arraycopy(byteData, pOff+12*i+8, t_dexProtoId.parametersOff, 0, t_dexProtoId.parametersOff.length);

String pri = "";
String shorty_string = getStringById(Utils.byte2Int_4(t_dexProtoId.shortyIdx, 0));
String return_type_idx = getStringById(Utils.byte2Int_4(t_dexProtoId.returnTypeIdx, 0));
pri += (shorty_string + ", ");
pri += (return_type_idx + ", ");
int parameters_Off = Utils.byte2Int_4(t_dexProtoId.parametersOff, 0);
pri += "(";
if (parameters_Off == 0){
dexType.dexProtoId.add(t_dexProtoId);
dexType.dexProtoList.add(getTypeStringById(Utils.byte2Int_4(t_dexProtoId.returnTypeIdx, 0)));
pri += ")";
continue;
}

int structCount = Utils.byte2Int_4(byteData, parameters_Off);
for (int j=0; j<structCount; j++){
DexType.DexTypeItem t_dexTypeItem = dexType.new DexTypeItem();
System.arraycopy(byteData, parameters_Off+4+j*t_dexTypeItem.typeIdx.length, t_dexTypeItem.typeIdx, 0, t_dexTypeItem.typeIdx.length);

int tmp = Utils.byte2Short_2(t_dexTypeItem.typeIdx, 0);
String p = getTypeStringById(tmp);
pri += (p + ", ");
}

pri += ")";
System.out.println(pri);

dexType.dexProtoId.add(t_dexProtoId);
dexType.dexProtoList.add(getTypeStringById(Utils.byte2Int_4(t_dexProtoId.returnTypeIdx, 0)));
}
}

private void readDexFieldId(int pOff, int pCount){
System.out.println("------------fields----------------");
for (int i=0; i<pCount; i++){
DexType.DexFieldId t_dexFieldId = dexType.new DexFieldId();
System.arraycopy(byteData, pOff+8*i+0, t_dexFieldId.classIdx, 0, t_dexFieldId.classIdx.length);
System.arraycopy(byteData, pOff+8*i+2, t_dexFieldId.typeIdx, 0, t_dexFieldId.typeIdx.length);
System.arraycopy(byteData, pOff+8*i+4, t_dexFieldId.nameIdx, 0, t_dexFieldId.nameIdx.length);

int classIdx = Utils.byte2Short_2(t_dexFieldId.classIdx, 0);
int typeIdx = Utils.byte2Short_2(t_dexFieldId.typeIdx, 0);
int nameIdx = Utils.byte2Int_4(t_dexFieldId.nameIdx, 0);

String pri = "";
pri += ("class:"+getTypeStringById(classIdx) + ", type:" + getTypeStringById(typeIdx) + ", name:"+getStringById(nameIdx));
System.out.println(pri);

dexType.dexFieldId.add(t_dexFieldId);
dexType.dexFieldList.add(getStringById(nameIdx));
}
}

private void readDexMethodId(int pOff, int pCount){
System.out.println("------------methods----------------");
for (int i=0; i<pCount; i++){
DexType.DexMethodId t_dexMethodId = dexType.new DexMethodId();
System.arraycopy(byteData, pOff+8*i+0, t_dexMethodId.classIdx, 0, t_dexMethodId.classIdx.length);
System.arraycopy(byteData, pOff+8*i+2, t_dexMethodId.protoIdx, 0, t_dexMethodId.protoIdx.length);
System.arraycopy(byteData, pOff+8*i+4, t_dexMethodId.nameIdx, 0, t_dexMethodId.nameIdx.length);

int classIdx = Utils.byte2Short_2(t_dexMethodId.classIdx, 0);
int protoIdx = Utils.byte2Short_2(t_dexMethodId.protoIdx, 0);
int nameIdx = Utils.byte2Int_4(t_dexMethodId.nameIdx, 0);

String pri = "";
pri += ("class:"+getTypeStringById(classIdx) + ", proto:" + getProtoStringById(protoIdx) + ", name:"+getStringById(nameIdx));
System.out.println(pri);

dexType.dexMethodId.add(t_dexMethodId);
dexType.dexMethodList.add(getStringById(nameIdx));
}
}

private void readDexClassDefsId(int pOff, int pCount){
System.out.println("------------ClassDefs----------------");
for (int i=0; i<pCount; i++){
DexType.DexClassDef t_dexClassDef = dexType.new DexClassDef();
System.arraycopy(byteData, pOff+i*32+0, t_dexClassDef.classIdx, 0, t_dexClassDef.classIdx.length);
System.arraycopy(byteData, pOff+i*32+4, t_dexClassDef.accessFlags, 0, t_dexClassDef.accessFlags.length);
System.arraycopy(byteData, pOff+i*32+8, t_dexClassDef.superclassIdx, 0, t_dexClassDef.superclassIdx.length);
System.arraycopy(byteData, pOff+i*32+12, t_dexClassDef.interfacesOff, 0, t_dexClassDef.interfacesOff.length);
System.arraycopy(byteData, pOff+i*32+16, t_dexClassDef.sourceFileIdx, 0, t_dexClassDef.sourceFileIdx.length);
System.arraycopy(byteData, pOff+i*32+20, t_dexClassDef.annotationsOff, 0, t_dexClassDef.annotationsOff.length);
System.arraycopy(byteData, pOff+i*32+24, t_dexClassDef.classDataOff, 0, t_dexClassDef.classDataOff.length);
System.arraycopy(byteData, pOff+i*32+28, t_dexClassDef.staticValuesOff, 0, t_dexClassDef.staticValuesOff.length);

int classIdx = Utils.byte2Int_4(t_dexClassDef.classIdx, 0);
int accessFlags = Utils.byte2Int_4(t_dexClassDef.accessFlags, 0);
int superclassIdx = Utils.byte2Int_4(t_dexClassDef.superclassIdx, 0);
int interfacesOff = Utils.byte2Int_4(t_dexClassDef.interfacesOff, 0);
int sourceFileIdx = Utils.byte2Int_4(t_dexClassDef.sourceFileIdx, 0);
int annotationsOff = Utils.byte2Int_4(t_dexClassDef.annotationsOff, 0);
int classDataOff = Utils.byte2Int_4(t_dexClassDef.classDataOff, 0);
int staticValuesOff = Utils.byte2Int_4(t_dexClassDef.staticValuesOff, 0);

String pri = "classIdx: " + getTypeStringById(classIdx) + "\r\naccessFlags: " + accessFlags + "\r\nsuperclassIdx: "
+ getTypeStringById(superclassIdx) + "\r\ninterfacesOff: ";
pri += (interfacesOff + "\r\nsourceFileIdx: " + getStringById(sourceFileIdx) + "\r\nannotationsOff: " + annotationsOff + "\r\nclassDataOff: ");
pri += (classDataOff + "\r\nstaticValuesOff: " + staticValuesOff);
System.out.println(pri);

if (classDataOff == 0){
continue;
}

int off = 0;
Utils.RETULEB128 retuleb128 = null;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int static_fields_size = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int instance_fields_size = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int direct_methods_size = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int virtual_methods_size = retuleb128.retValue;
off += retuleb128.readSize;

System.out.println("static_fields_size:" + static_fields_size + ", ");
for (int j=0; j<static_fields_size; j++){
retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int field_idx_diff = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int access_flags = retuleb128.retValue;
off += retuleb128.readSize;

System.out.println("\t" + getFieldStringById(field_idx_diff) + ", " + access_flags);
}

System.out.println("instance_fields_size:" + instance_fields_size + ", ");
for (int j=0; j<static_fields_size; j++) {
retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int field_idx_diff = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int access_flags = retuleb128.retValue;
off += retuleb128.readSize;

System.out.println("\t" + getFieldStringById(field_idx_diff) + ", " + access_flags);
}

System.out.println("direct_methods_size:" + direct_methods_size + ", ");
for (int j=0; j<direct_methods_size; j++) {
retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int method_idx_diff = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int access_flags = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int code_off = retuleb128.retValue;
off += retuleb128.readSize;

if (code_off < 1)
continue;

System.out.println("\t" + getMethodStringById(method_idx_diff) + ", " + access_flags);
DexType.DexCodeItem t_dexCodeItem = dexType.new DexCodeItem(byteData, code_off);
}

System.out.println("virtual_methods_size:" + virtual_methods_size + ", ");
for (int j=0; j<virtual_methods_size; j++) {
retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int method_idx_diff = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int access_flags = retuleb128.retValue;
off += retuleb128.readSize;

retuleb128 = Utils.readULEB128(byteData, classDataOff + off);
int code_off = retuleb128.retValue;
off += retuleb128.readSize;

if (code_off < 1)
continue;

System.out.println("\t" + getMethodStringById(method_idx_diff) + ", " + access_flags);
DexType.DexCodeItem t_dexCodeItem = dexType.new DexCodeItem(byteData, code_off);
}
}
}

public static void getOpcodeByIns2(byte[] insns){
//�����org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction public static Instruction readFrom(DexBackedDexFile dexFile, DexReader reader) {
Opcode opcode = null;
for (int insnsPosition=0; insnsPosition<insns.length; insnsPosition += (opcode.format.size/2*2)){
try{
int opcodeValue = (insns[insnsPosition] & 255);
if (opcodeValue == 0) {
opcodeValue = (insns[insnsPosition] & 255) | ((insns[insnsPosition + 1] & 255) << 8);
}
opcode = Opcodes.forApi(35).getOpcodeByValue(opcodeValue);
byte[] tmp = new byte[opcode.format.size/2*2];
System.arraycopy(insns, insnsPosition, tmp, 0, tmp.length);

System.out.println("\t\t" + opcode.name + " : " + Utils.bytes2HexString(tmp));
if (opcode.format.size < 1)
break;
}catch (Exception ex){
break;
}
}
}
}

Utils.java

package com.pediy.test;

import java.io.*;

public final class Utils {

public static String bytes2HexString(byte[] bytes){
StringBuilder result = new StringBuilder();
for(int i=0; i<bytes.length;i++){
int t = bytes[i] & 0xff;
String hex = Integer.toHexString(t).toUpperCase();
if(hex.length() < 2){
result.append("0"+hex);
}else{
result.append(hex);
}
result.append(" ");
}
return result.toString();

}

public static String bytes2HexStringBig(byte[] bytes){
StringBuilder result = new StringBuilder();
for(int i=0; i<bytes.length;i++){
int t = bytes[i] & 0xff;
String hex = Integer.toHexString(t).toUpperCase();
if(hex.length() < 2){
result.append("0"+hex);
}else{
result.append(hex);
}
result.append(" ");
}
return result.toString();

}

public static byte[] readFile(String fileName){
File file = new File(fileName);
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try{
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
int size = 0;
while ((size = fis.read(temp)) != -1) {
bos.write(temp, 0, size);
}
return bos.toByteArray();
}catch(Exception e){
System.out.println("read file error:"+e.toString());
}finally{
if(fis != null){
try{
fis.close();
}catch(Exception e){
System.out.println("close file error:"+e.toString());
}
}
if(bos != null){
try{
bos.close();
}catch(Exception e){
System.out.println("close file error:"+e.toString());
}
}
}
return null;
}

public static byte[] copyNewBytes(byte[] addr,int start,int length){
byte[] destByte = new byte[length];
for(int i=0;i<length;i++){
destByte[i] = addr[start+i];
}
return destByte;
}

public static int byte2Int_4(byte[] res, int pOff) {
int targets = (res[pOff+0] & 0xff)
| ((res[pOff+1] << 8) & 0xff00)
| ((res[pOff+2] << 24) >>> 8)
| (res[pOff+3] << 24);
return targets;
}

public static short byte2Short_2(byte[] b, int pOff) {
short s = 0;
short s0 = (short) (b[pOff+0] & 0xff);// 最低位
short s1 = (short) (b[pOff+1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}


static class RETULEB128{
int retValue;
int readSize;
}

public static RETULEB128 readULEB128(byte[] data, int pOff) {
try{
RETULEB128 ret = new RETULEB128();
int result = 0;
int shift = 0;
int bytesRead = 0;
int i = -1;

while (true) {
i++;
if (pOff+i >= data.length)
throw new IOException("pOff+i >= data.length");
int currentByte = data[pOff + i];

bytesRead++;
result |= (currentByte & 0x7F) << shift;
shift += 7;

if ((currentByte & 0x80) == 0) {
break;
}
if (bytesRead > 5) {
throw new IOException("Invalid ULEB128 encoded data: more than 5 bytes");
}
}

ret.retValue = result;
ret.readSize = bytesRead;
return ret;
}catch (IOException ioe){
ioe.printStackTrace();
}
return null;
}


}

python脚本

import codecs

with codecs.open('a.txt', 'r', 'utf-8') as f:
data = f.read()

with codecs.open('file_gbk.txt', 'w', 'gbk') as f:
f.write(data)