1 /* 2 * Copyright 2013, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.dexlib2.dexbacked.raw; 33 34 import org.jf.dexlib2.VersionMap; 35 import org.jf.dexlib2.dexbacked.BaseDexBuffer; 36 import org.jf.dexlib2.dexbacked.raw.util.DexAnnotator; 37 import org.jf.dexlib2.util.AnnotatedBytes; 38 import org.jf.util.StringUtils; 39 40 import javax.annotation.Nonnull; 41 import javax.annotation.Nullable; 42 43 public class HeaderItem { 44 public static final int ITEM_SIZE = 0x70; 45 46 private static final byte[] MAGIC_VALUE = new byte[] { 0x64, 0x65, 0x78, 0x0a, 0x00, 0x00, 0x00, 0x00 }; 47 48 public static final int LITTLE_ENDIAN_TAG = 0x12345678; 49 public static final int BIG_ENDIAN_TAG = 0x78563412; 50 51 public static final int CHECKSUM_OFFSET = 8; 52 53 // this is the start of the checksumed data 54 public static final int CHECKSUM_DATA_START_OFFSET = 12; 55 public static final int SIGNATURE_OFFSET = 12; 56 public static final int SIGNATURE_SIZE = 20; 57 58 // this is the start of the sha-1 hashed data 59 public static final int SIGNATURE_DATA_START_OFFSET = 32; 60 public static final int FILE_SIZE_OFFSET = 32; 61 62 public static final int HEADER_SIZE_OFFSET = 36; 63 64 public static final int ENDIAN_TAG_OFFSET = 40; 65 66 public static final int MAP_OFFSET = 52; 67 68 public static final int STRING_COUNT_OFFSET = 56; 69 public static final int STRING_START_OFFSET = 60; 70 71 public static final int TYPE_COUNT_OFFSET = 64; 72 public static final int TYPE_START_OFFSET = 68; 73 74 public static final int PROTO_COUNT_OFFSET = 72; 75 public static final int PROTO_START_OFFSET = 76; 76 77 public static final int FIELD_COUNT_OFFSET = 80; 78 public static final int FIELD_START_OFFSET = 84; 79 80 public static final int METHOD_COUNT_OFFSET = 88; 81 public static final int METHOD_START_OFFSET = 92; 82 83 public static final int CLASS_COUNT_OFFSET = 96; 84 public static final int CLASS_START_OFFSET = 100; 85 86 @Nonnull private RawDexFile dexFile; 87 HeaderItem(@onnull RawDexFile dexFile)88 public HeaderItem(@Nonnull RawDexFile dexFile) { 89 this.dexFile = dexFile; 90 } 91 getChecksum()92 public int getChecksum() { 93 return dexFile.readSmallUint(CHECKSUM_OFFSET); 94 } 95 getSignature()96 @Nonnull public byte[] getSignature() { 97 return dexFile.readByteRange(SIGNATURE_OFFSET, SIGNATURE_SIZE); 98 } 99 getMapOffset()100 public int getMapOffset() { 101 return dexFile.readSmallUint(MAP_OFFSET); 102 } 103 getHeaderSize()104 public int getHeaderSize() { 105 return dexFile.readSmallUint(HEADER_SIZE_OFFSET); 106 } 107 getStringCount()108 public int getStringCount() { 109 return dexFile.readSmallUint(STRING_COUNT_OFFSET); 110 } 111 getStringOffset()112 public int getStringOffset() { 113 return dexFile.readSmallUint(STRING_START_OFFSET); 114 } 115 getTypeCount()116 public int getTypeCount() { 117 return dexFile.readSmallUint(TYPE_COUNT_OFFSET); 118 } 119 getTypeOffset()120 public int getTypeOffset() { 121 return dexFile.readSmallUint(TYPE_START_OFFSET); 122 } 123 getProtoCount()124 public int getProtoCount() { 125 return dexFile.readSmallUint(PROTO_COUNT_OFFSET); 126 } 127 getProtoOffset()128 public int getProtoOffset() { 129 return dexFile.readSmallUint(PROTO_START_OFFSET); 130 } 131 getFieldCount()132 public int getFieldCount() { 133 return dexFile.readSmallUint(FIELD_COUNT_OFFSET); 134 } 135 getFieldOffset()136 public int getFieldOffset() { 137 return dexFile.readSmallUint(FIELD_START_OFFSET); 138 } 139 getMethodCount()140 public int getMethodCount() { 141 return dexFile.readSmallUint(METHOD_COUNT_OFFSET); 142 } 143 getMethodOffset()144 public int getMethodOffset() { 145 return dexFile.readSmallUint(METHOD_START_OFFSET); 146 } 147 getClassCount()148 public int getClassCount() { 149 return dexFile.readSmallUint(CLASS_COUNT_OFFSET); 150 } 151 getClassOffset()152 public int getClassOffset() { 153 return dexFile.readSmallUint(CLASS_START_OFFSET); 154 } 155 156 @Nonnull makeAnnotator(@onnull DexAnnotator annotator, @Nonnull MapItem mapItem)157 public static SectionAnnotator makeAnnotator(@Nonnull DexAnnotator annotator, @Nonnull MapItem mapItem) { 158 return new SectionAnnotator(annotator, mapItem) { 159 @Nonnull @Override public String getItemName() { 160 return "header_item"; 161 } 162 163 @Override 164 protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) { 165 int startOffset = out.getCursor(); 166 int headerSize; 167 168 StringBuilder magicBuilder = new StringBuilder(); 169 for (int i=0; i<8; i++) { 170 magicBuilder.append((char)dexFile.readUbyte(startOffset + i)); 171 } 172 173 out.annotate(8, "magic: %s", StringUtils.escapeString(magicBuilder.toString())); 174 out.annotate(4, "checksum"); 175 out.annotate(20, "signature"); 176 out.annotate(4, "file_size: %d", dexFile.readInt(out.getCursor())); 177 178 headerSize = dexFile.readInt(out.getCursor()); 179 out.annotate(4, "header_size: %d", headerSize); 180 181 int endianTag = dexFile.readInt(out.getCursor()); 182 out.annotate(4, "endian_tag: 0x%x (%s)", endianTag, getEndianText(endianTag)); 183 184 out.annotate(4, "link_size: %d", dexFile.readInt(out.getCursor())); 185 out.annotate(4, "link_offset: 0x%x", dexFile.readInt(out.getCursor())); 186 187 out.annotate(4, "map_off: 0x%x", dexFile.readInt(out.getCursor())); 188 189 out.annotate(4, "string_ids_size: %d", dexFile.readInt(out.getCursor())); 190 out.annotate(4, "string_ids_off: 0x%x", dexFile.readInt(out.getCursor())); 191 192 out.annotate(4, "type_ids_size: %d", dexFile.readInt(out.getCursor())); 193 out.annotate(4, "type_ids_off: 0x%x", dexFile.readInt(out.getCursor())); 194 195 out.annotate(4, "proto_ids_size: %d", dexFile.readInt(out.getCursor())); 196 out.annotate(4, "proto_ids_off: 0x%x", dexFile.readInt(out.getCursor())); 197 198 out.annotate(4, "field_ids_size: %d", dexFile.readInt(out.getCursor())); 199 out.annotate(4, "field_ids_off: 0x%x", dexFile.readInt(out.getCursor())); 200 201 out.annotate(4, "method_ids_size: %d", dexFile.readInt(out.getCursor())); 202 out.annotate(4, "method_ids_off: 0x%x", dexFile.readInt(out.getCursor())); 203 204 out.annotate(4, "class_defs_size: %d", dexFile.readInt(out.getCursor())); 205 out.annotate(4, "class_defs_off: 0x%x", dexFile.readInt(out.getCursor())); 206 207 out.annotate(4, "data_size: %d", dexFile.readInt(out.getCursor())); 208 out.annotate(4, "data_off: 0x%x", dexFile.readInt(out.getCursor())); 209 210 if (headerSize > ITEM_SIZE) { 211 out.annotateTo(headerSize, "header padding"); 212 } 213 } 214 }; 215 } 216 217 private static String getEndianText(int endianTag) { 218 if (endianTag == LITTLE_ENDIAN_TAG) { 219 return "Little Endian"; 220 } 221 if (endianTag == BIG_ENDIAN_TAG) { 222 return "Big Endian"; 223 } 224 return "Invalid"; 225 } 226 227 /** 228 * Get the highest magic number supported by Android for this api level. 229 * @return The dex file magic number 230 */ 231 public static byte[] getMagicForApi(int api) { 232 return getMagicForDexVersion(VersionMap.mapApiToDexVersion(api)); 233 } 234 235 public static byte[] getMagicForDexVersion(int dexVersion) { 236 byte[] magic = MAGIC_VALUE.clone(); 237 238 if (dexVersion < 0 || dexVersion > 999) { 239 throw new IllegalArgumentException("dexVersion must be within [0, 999]"); 240 } 241 242 for (int i=6; i>=4; i--) { 243 int digit = dexVersion % 10; 244 magic[i] = (byte)('0' + digit); 245 dexVersion /= 10; 246 } 247 248 return magic; 249 } 250 251 /** 252 * Verifies the magic value at the beginning of a dex file 253 * 254 * @param buf A byte array containing at least the first 8 bytes of a dex file 255 * @param offset The offset within the buffer to the beginning of the dex header 256 * @return True if the magic value is valid 257 */ 258 public static boolean verifyMagic(byte[] buf, int offset) { 259 if (buf.length - offset < 8) { 260 return false; 261 } 262 263 for (int i=0; i<4; i++) { 264 if (buf[offset + i] != MAGIC_VALUE[i]) { 265 return false; 266 } 267 } 268 for (int i=4; i<7; i++) { 269 if (buf[offset + i] < '0' || 270 buf[offset + i] > '9') { 271 return false; 272 } 273 } 274 if (buf[offset + 7] != MAGIC_VALUE[7]) { 275 return false; 276 } 277 278 return true; 279 } 280 281 /** 282 * Gets the dex version from a dex header 283 * 284 * @param buf A byte array containing at least the first 7 bytes of a dex file 285 * @param offset The offset within the buffer to the beginning of the dex header 286 * @return The dex version if the header is valid or -1 if the header is invalid 287 */ 288 public static int getVersion(byte[] buf, int offset) { 289 if (!verifyMagic(buf, offset)) { 290 return -1; 291 } 292 293 return getVersionUnchecked(buf, offset); 294 } 295 296 private static int getVersionUnchecked(byte[] buf, int offset) { 297 int version = (buf[offset + 4] - '0') * 100; 298 version += (buf[offset + 5] - '0') * 10; 299 version += buf[offset + 6] - '0'; 300 301 return version; 302 } 303 304 public static boolean isSupportedDexVersion(int version) { 305 return VersionMap.mapDexVersionToApi(version) != VersionMap.NO_VERSION; 306 } 307 308 public static int getEndian(byte[] buf, int offset) { 309 BaseDexBuffer bdb = new BaseDexBuffer(buf); 310 return bdb.readInt(offset + ENDIAN_TAG_OFFSET); 311 } 312 } 313