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 17package 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 */ 23public final class 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 = 14; 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 /** dex file version number for the current format variant */ 48 public static final String VERSION_CURRENT = "036"; 49 50 /** dex file version number for API level 13 and earlier */ 51 public static final String VERSION_FOR_API_13 = "035"; 52 53 /** 54 * value used to indicate endianness of file contents 55 */ 56 public static final int ENDIAN_TAG = 0x12345678; 57 58 /** 59 * Maximum addressable field or method index. 60 * The largest addressable member is 0xffff, in the "instruction formats" spec as field@CCCC or 61 * meth@CCCC. 62 */ 63 public static final int MAX_MEMBER_IDX = 0xFFFF; 64 65 /** 66 * Maximum addressable type index. 67 * The largest addressable type is 0xffff, in the "instruction formats" spec as type@CCCC. 68 */ 69 public static final int MAX_TYPE_IDX = 0xFFFF; 70 71 /** 72 * Returns the API level corresponding to the given magic number, 73 * or {@code -1} if the given array is not a well-formed dex file 74 * magic number. 75 */ 76 public static int magicToApi(byte[] magic) { 77 if (magic.length != 8) { 78 return -1; 79 } 80 81 if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') || 82 (magic[7] != '\0')) { 83 return -1; 84 } 85 86 String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]); 87 88 if (version.equals(VERSION_CURRENT)) { 89 return API_CURRENT; 90 } else if (version.equals(VERSION_FOR_API_13)) { 91 return 13; 92 } 93 94 return -1; 95 } 96 97 /** 98 * Returns the magic number corresponding to the given target API level. 99 */ 100 public static String apiToMagic(int targetApiLevel) { 101 String version; 102 103 if (targetApiLevel >= API_CURRENT) { 104 version = VERSION_CURRENT; 105 } else { 106 version = VERSION_FOR_API_13; 107 } 108 109 return MAGIC_PREFIX + version + MAGIC_SUFFIX; 110 } 111} 112