• 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     /** API level to target in order to allow spaces in SimpleName */
27     public static final int API_SPACES_IN_SIMPLE_NAME = 10000;
28 
29     /** API level to target in order to generate const-method-handle and const-method-type */
30     public static final int API_CONST_METHOD_HANDLE = 28;
31 
32     /** API level to target in order to generate invoke-polymorphic and invoke-custom */
33     public static final int API_METHOD_HANDLES = 26;
34 
35     /** API level to target in order to define default and static interface methods */
36     public static final int API_DEFINE_INTERFACE_METHODS = 24;
37 
38     /** API level to target in order to invoke default and static interface methods */
39     public static final int API_INVOKE_INTERFACE_METHODS = 24;
40 
41     /** API level at which the invocation of static interface methods is permitted by dx.
42      * This value has been determined experimentally by testing on different VM versions. */
43     public static final int API_INVOKE_STATIC_INTERFACE_METHODS = 21;
44 
45     /** API level to target in order to suppress extended opcode usage */
46     public static final int API_NO_EXTENDED_OPCODES = 13;
47 
48     /**
49      * API level to target in order to produce the most modern file
50      * format
51      */
52     public static final int API_CURRENT = API_CONST_METHOD_HANDLE;
53 
54     /** dex file version number for API level 10000 and earlier */
55     public static final String VERSION_FOR_API_10000 = "040";
56 
57     /** dex file version number for API level 28 and earlier */
58     public static final String VERSION_FOR_API_28 = "039";
59 
60     /** dex file version number for API level 26 and earlier */
61     public static final String VERSION_FOR_API_26 = "038";
62 
63     /** dex file version number for API level 24 and earlier */
64     public static final String VERSION_FOR_API_24 = "037";
65 
66     /** dex file version number for API level 13 and earlier */
67     public static final String VERSION_FOR_API_13 = "035";
68 
69     /**
70      * Dex file version number for dalvik.
71      * <p>
72      * Note: Dex version 36 was loadable in some versions of Dalvik but was never fully supported or
73      * completed and is not considered a valid dex file format.
74      * </p>
75      */
76     public static final String VERSION_CURRENT = VERSION_FOR_API_28;
77 
78     /**
79      * file name of the primary {@code .dex} file inside an
80      * application or library {@code .jar} file
81      */
82     public static final String DEX_IN_JAR_NAME = "classes.dex";
83 
84     /** common prefix for all dex file "magic numbers" */
85     public static final String MAGIC_PREFIX = "dex\n";
86 
87     /** common suffix for all dex file "magic numbers" */
88     public static final String MAGIC_SUFFIX = "\0";
89 
90     /**
91      * value used to indicate endianness of file contents
92      */
93     public static final int ENDIAN_TAG = 0x12345678;
94 
95     /**
96      * Maximum addressable field or method index.
97      * The largest addressable member is 0xffff, in the "instruction formats" spec as field@CCCC or
98      * meth@CCCC.
99      */
100     public static final int MAX_MEMBER_IDX = 0xFFFF;
101 
102     /**
103      * Maximum addressable type index.
104      * The largest addressable type is 0xffff, in the "instruction formats" spec as type@CCCC.
105      */
106     public static final int MAX_TYPE_IDX = 0xFFFF;
107 
108     /**
109      * Returns the API level corresponding to the given magic number,
110      * or {@code -1} if the given array is not a well-formed dex file
111      * magic number.
112      *
113      * @param magic array of bytes containing DEX file magic string
114      * @return API level corresponding to magic string if valid, -1 otherwise.
115      */
magicToApi(byte[] magic)116     public static int magicToApi(byte[] magic) {
117         if (magic.length != 8) {
118             return -1;
119         }
120 
121         if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') ||
122                 (magic[7] != '\0')) {
123             return -1;
124         }
125 
126         String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]);
127 
128         if (version.equals(VERSION_FOR_API_13)) {
129             return API_NO_EXTENDED_OPCODES;
130         } else if (version.equals(VERSION_FOR_API_24)) {
131             return API_DEFINE_INTERFACE_METHODS;
132         } else if (version.equals(VERSION_FOR_API_26)) {
133             return API_METHOD_HANDLES;
134         } else if (version.equals(VERSION_FOR_API_28)) {
135             return API_CONST_METHOD_HANDLE;
136         } else if (version.equals(VERSION_FOR_API_10000)) {
137             return API_SPACES_IN_SIMPLE_NAME;
138         } else if (version.equals(VERSION_CURRENT)) {
139             return API_CURRENT;
140         }
141 
142         return -1;
143     }
144 
145     /**
146      * Returns the magic number corresponding to the given target API level.
147      *
148      * @param targetApiLevel level of API (minimum supported value 13).
149      * @return Magic string corresponding to API level supplied.
150      */
apiToMagic(int targetApiLevel)151     public static String apiToMagic(int targetApiLevel) {
152         String version;
153 
154         if (targetApiLevel >= API_CURRENT) {
155             version = VERSION_CURRENT;
156         } else if (targetApiLevel >= API_SPACES_IN_SIMPLE_NAME) {
157             version = VERSION_FOR_API_10000;
158         } else if (targetApiLevel >= API_CONST_METHOD_HANDLE) {
159             version = VERSION_FOR_API_28;
160         } else if (targetApiLevel >= API_METHOD_HANDLES) {
161             version = VERSION_FOR_API_26;
162         } else if (targetApiLevel >= API_DEFINE_INTERFACE_METHODS) {
163             version = VERSION_FOR_API_24;
164         } else {
165             version = VERSION_FOR_API_13;
166         }
167 
168         return MAGIC_PREFIX + version + MAGIC_SUFFIX;
169     }
170 
171     /**
172      * Checks whether a DEX file magic string is supported.
173      * @param magic string from DEX file
174      * @return
175      */
isSupportedDexMagic(byte[] magic)176     public static boolean isSupportedDexMagic(byte[] magic) {
177         int api = magicToApi(magic);
178         return api > 0;
179     }
180 }
181