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.dx.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 = 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 * Returns the API level corresponding to the given magic number, 60 * or {@code -1} if the given array is not a well-formed dex file 61 * magic number. 62 */ magicToApi(byte[] magic)63 public static int magicToApi(byte[] magic) { 64 if (magic.length != 8) { 65 return -1; 66 } 67 68 if ((magic[0] != 'd') || (magic[1] != 'e') || (magic[2] != 'x') || (magic[3] != '\n') || 69 (magic[7] != '\0')) { 70 return -1; 71 } 72 73 String version = "" + ((char) magic[4]) + ((char) magic[5]) +((char) magic[6]); 74 75 if (version.equals(VERSION_CURRENT)) { 76 return API_CURRENT; 77 } else if (version.equals(VERSION_FOR_API_13)) { 78 return 13; 79 } 80 81 return -1; 82 } 83 84 /** 85 * Returns the magic number corresponding to the given target API level. 86 */ apiToMagic(int targetApiLevel)87 public static String apiToMagic(int targetApiLevel) { 88 String version; 89 90 if (targetApiLevel >= API_CURRENT) { 91 version = VERSION_CURRENT; 92 } else { 93 version = VERSION_FOR_API_13; 94 } 95 96 return MAGIC_PREFIX + version + MAGIC_SUFFIX; 97 } 98 } 99