• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 package com.android.dex;
18 
19 /**
20  * Constants that show up in and are otherwise related to {@code .dex}
21  * files, and helper methods for same.
22  */
23 public final class DexFormat {
DexFormat()24     private DexFormat() {}
25 
26     /**
27      * API level to target in order to produce the most modern file
28      * format
29      */
30     public static final int API_CURRENT = 24;
31 
32     /** API level to target in order to suppress extended opcode usage */
33     public static final int API_NO_EXTENDED_OPCODES = 13;
34 
35     /**
36      * file name of the primary {@code .dex} file inside an
37      * application or library {@code .jar} file
38      */
39     public static final String DEX_IN_JAR_NAME = "classes.dex";
40 
41     /** common prefix for all dex file "magic numbers" */
42     public static final String MAGIC_PREFIX = "dex\n";
43 
44     /** common suffix for all dex file "magic numbers" */
45     public static final String MAGIC_SUFFIX = "\0";
46 
47     /**
48      * Dex file version number for dalvik.
49      * <p>
50      * Note: Dex version 36 was loadable in some versions of Dalvik but was never fully supported or
51      * completed and is not considered a valid dex file format.
52      * </p>
53      */
54     public static final String VERSION_CURRENT = "037";
55 
56     /** dex file version number for API level 13 and earlier */
57     public static final String VERSION_FOR_API_13 = "035";
58 
59     /**
60      * value used to indicate endianness of file contents
61      */
62     public static final int ENDIAN_TAG = 0x12345678;
63 
64     /**
65      * Maximum addressable field or method index.
66      * The largest addressable member is 0xffff, in the "instruction formats" spec as field@CCCC or
67      * meth@CCCC.
68      */
69     public static final int MAX_MEMBER_IDX = 0xFFFF;
70 
71     /**
72      * Maximum addressable type index.
73      * The largest addressable type is 0xffff, in the "instruction formats" spec as type@CCCC.
74      */
75     public static final int MAX_TYPE_IDX = 0xFFFF;
76 
77     /**
78      * Returns the API level corresponding to the given magic number,
79      * or {@code -1} if the given array is not a well-formed dex file
80      * magic number.
81      */
magicToApi(byte[] magic)82     public static int magicToApi(byte[] magic) {
83         if (magic.length != 8) {
84             return -1;
85         }
86 
87         if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') ||
88                 (magic[7] != '\0')) {
89             return -1;
90         }
91 
92         String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]);
93 
94         if (version.equals(VERSION_CURRENT)) {
95             return API_CURRENT;
96         } else if (version.equals(VERSION_FOR_API_13)) {
97             return API_NO_EXTENDED_OPCODES;
98         }
99 
100         return -1;
101     }
102 
103     /**
104      * Returns the magic number corresponding to the given target API level.
105      */
apiToMagic(int targetApiLevel)106     public static String apiToMagic(int targetApiLevel) {
107         String version;
108 
109         if (targetApiLevel >= API_CURRENT) {
110             version = VERSION_CURRENT;
111         } else {
112             version = VERSION_FOR_API_13;
113         }
114 
115         return MAGIC_PREFIX + version + MAGIC_SUFFIX;
116     }
117 
isSupportedDexMagic(byte[] magic)118     public static boolean isSupportedDexMagic(byte[] magic) {
119         int api = magicToApi(magic);
120         return api == API_NO_EXTENDED_OPCODES || api == API_CURRENT;
121     }
122 }
123