12bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson/* 22bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Copyright (C) 2011 The Android Open Source Project 32bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * 42bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 52bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * you may not use this file except in compliance with the License. 62bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * You may obtain a copy of the License at 72bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * 82bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 92bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * 102bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Unless required by applicable law or agreed to in writing, software 112bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 122bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * See the License for the specific language governing permissions and 142bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * limitations under the License. 152bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 162bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 172bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilsonpackage com.android.dex; 182bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 192bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson/** 202bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Constants that show up in and are otherwise related to {@code .dex} 212bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * files, and helper methods for same. 222bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 232bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilsonpublic final class DexFormat { 242bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson private DexFormat() {} 252bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 262bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** 272bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * API level to target in order to produce the most modern file 282bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * format 292bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 302bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final int API_CURRENT = 14; 312bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 322bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** API level to target in order to suppress extended opcode usage */ 332bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final int API_NO_EXTENDED_OPCODES = 13; 342bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 352bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** 362bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * file name of the primary {@code .dex} file inside an 372bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * application or library {@code .jar} file 382bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 392bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final String DEX_IN_JAR_NAME = "classes.dex"; 402bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 412bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** common prefix for all dex file "magic numbers" */ 422bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final String MAGIC_PREFIX = "dex\n"; 432bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 442bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** common suffix for all dex file "magic numbers" */ 452bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final String MAGIC_SUFFIX = "\0"; 462bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 472bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** dex file version number for the current format variant */ 482bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final String VERSION_CURRENT = "036"; 492bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 502bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** dex file version number for API level 13 and earlier */ 512bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final String VERSION_FOR_API_13 = "035"; 522bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 532bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** 542bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * value used to indicate endianness of file contents 552bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 562bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static final int ENDIAN_TAG = 0x12345678; 572bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 582bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** 59661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel * Maximum addressable field or method index. 60661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel * The largest addressable member is 0xffff, in the "instruction formats" spec as field@CCCC or 61661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel * meth@CCCC. 62661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel */ 63661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel public static final int MAX_MEMBER_IDX = 0xFFFF; 64661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel 65661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel /** 66661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel * Maximum addressable type index. 67661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel * The largest addressable type is 0xffff, in the "instruction formats" spec as type@CCCC. 68661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel */ 69661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel public static final int MAX_TYPE_IDX = 0xFFFF; 70661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel 71661f3c9ae4049cfa493fabaf129516e94f2388deYohann Roussel /** 722bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Returns the API level corresponding to the given magic number, 732bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * or {@code -1} if the given array is not a well-formed dex file 742bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * magic number. 752bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 762bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static int magicToApi(byte[] magic) { 772bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson if (magic.length != 8) { 782bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return -1; 792bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 802bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 812bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') || 822bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson (magic[7] != '\0')) { 832bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return -1; 842bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 852bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 862bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]); 872bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 882bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson if (version.equals(VERSION_CURRENT)) { 892bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return API_CURRENT; 902bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } else if (version.equals(VERSION_FOR_API_13)) { 912bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return 13; 922bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 932bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 942bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return -1; 952bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 962bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 972bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson /** 982bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson * Returns the magic number corresponding to the given target API level. 992bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson */ 1002bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson public static String apiToMagic(int targetApiLevel) { 1012bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson String version; 1022bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 1032bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson if (targetApiLevel >= API_CURRENT) { 1042bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson version = VERSION_CURRENT; 1052bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } else { 1062bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson version = VERSION_FOR_API_13; 1072bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 1082bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson 1092bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson return MAGIC_PREFIX + version + MAGIC_SUFFIX; 1102bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson } 1112bea5ee615b0f4add658d5660bd81c5145a0d05eJesse Wilson} 112