1 /* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 package org.jf.dexlib; 30 31 import com.google.common.base.Preconditions; 32 import org.jf.dexlib.Util.AnnotatedOutput; 33 import org.jf.dexlib.Util.Input; 34 import org.jf.dexlib.Util.Utf8Utils; 35 36 public class HeaderItem extends Item<HeaderItem> { 37 /** 38 * the file format magic number, represented as the 39 * low-order bytes of a string 40 */ 41 public static final byte[][] MAGIC_VALUES = new byte[][] { 42 new byte[] {0x64, 0x65, 0x78, 0x0A, 0x30, 0x33, 0x35, 0x00}, //"dex\n035" + '\0'; 43 new byte[] {0x64, 0x65, 0x78, 0x0A, 0x30, 0x33, 0x36, 0x00}}; //"dex\n036" + '\0'; 44 45 46 /** size of this section, in bytes */ 47 private static final int HEADER_SIZE = 0x70; 48 49 /** the endianness constants */ 50 private static final int LITTLE_ENDIAN = 0x12345678; 51 private static final int BIG_ENDIAN = 0x78563412; 52 53 /* Which magic value to use when writing out the header item */ 54 private int magic_index = 0; 55 56 private boolean checksumSignatureSet = false; 57 private int checksum; 58 private byte[] signature; 59 60 /** 61 * Create a new uninitialized <code>HeaderItem</code> 62 * @param dexFile The <code>DexFile</code> containing this <code>HeaderItem</code> 63 */ HeaderItem(final DexFile dexFile)64 protected HeaderItem(final DexFile dexFile) { 65 super(dexFile); 66 } 67 68 /** {@inheritDoc} */ readItem(Input in, ReadContext readContext)69 protected void readItem(Input in, ReadContext readContext) { 70 byte[] readMagic = in.readBytes(8); 71 72 boolean success = false; 73 for (int i=0; i<MAGIC_VALUES.length; i++) { 74 byte[] magic_value = MAGIC_VALUES[i]; 75 boolean matched = true; 76 for (int j=0; j<8; j++) { 77 if (magic_value[j] != readMagic[j]) { 78 matched = false; 79 break; 80 } 81 } 82 if (matched) { 83 success = true; 84 magic_index = i; 85 break; 86 } 87 } 88 89 if (!success) { 90 throw new RuntimeException("Unrecognized dex magic value"); 91 } 92 93 checksum = in.readInt(); //checksum 94 signature = in.readBytes(20); //signature 95 checksumSignatureSet = true; 96 97 in.readInt(); //filesize 98 in.readInt(); //header size 99 100 int endianTag = in.readInt(); 101 if (endianTag == BIG_ENDIAN) { 102 throw new RuntimeException("This dex file is big endian. Only little endian is currently supported."); 103 } else if (endianTag != LITTLE_ENDIAN) { 104 throw new RuntimeException("The endian tag is not 0x12345678 or 0x78563412"); 105 } 106 107 //link_size + link_off 108 if ((in.readInt() | in.readInt()) != 0) { 109 System.err.println("This dex file has a link section, which is not supported. Ignoring."); 110 } 111 112 int sectionSize; 113 int sectionOffset; 114 115 //map_offset 116 sectionOffset = in.readInt(); 117 readContext.addSection(ItemType.TYPE_MAP_LIST, 1, sectionOffset); 118 119 //string_id_item 120 sectionSize = in.readInt(); 121 sectionOffset = in.readInt(); 122 readContext.addSection(ItemType.TYPE_STRING_ID_ITEM, sectionSize, sectionOffset); 123 124 //type_id_item 125 sectionSize = in.readInt(); 126 sectionOffset = in.readInt(); 127 readContext.addSection(ItemType.TYPE_TYPE_ID_ITEM, sectionSize, sectionOffset); 128 129 //proto_id_item 130 sectionSize = in.readInt(); 131 sectionOffset = in.readInt(); 132 readContext.addSection(ItemType.TYPE_PROTO_ID_ITEM, sectionSize, sectionOffset); 133 134 //field_id_item 135 sectionSize = in.readInt(); 136 sectionOffset = in.readInt(); 137 readContext.addSection(ItemType.TYPE_FIELD_ID_ITEM, sectionSize, sectionOffset); 138 139 //method_id_item 140 sectionSize = in.readInt(); 141 sectionOffset = in.readInt(); 142 readContext.addSection(ItemType.TYPE_METHOD_ID_ITEM, sectionSize, sectionOffset); 143 144 //class_data_item 145 sectionSize = in.readInt(); 146 sectionOffset = in.readInt(); 147 readContext.addSection(ItemType.TYPE_CLASS_DEF_ITEM, sectionSize, sectionOffset); 148 149 in.readInt(); //data_size 150 in.readInt(); //data_off 151 } 152 153 /** 154 * Sets the dex version number. 155 * 156 * 35 is the default. 157 * 36 is for dex files that use extended opcodes (only works with ICS+) 158 * 159 * @param version - must be either 35 or 36 160 */ setVersion(int version)161 public void setVersion(int version) { 162 if (version == 35) { 163 magic_index = 0; 164 return; 165 } 166 if (version == 36) { 167 magic_index = 1; 168 return; 169 } 170 throw new RuntimeException("Invalid dex version number passed to setVersion"); 171 } 172 173 /** {@inheritDoc} */ placeItem(int offset)174 protected int placeItem(int offset) { 175 return HEADER_SIZE; 176 } 177 178 /** {@inheritDoc} */ writeItem(AnnotatedOutput out)179 protected void writeItem(AnnotatedOutput out) { 180 StringBuilder magicBuilder = new StringBuilder(); 181 for (int i=0; i<8; i++) { 182 magicBuilder.append((char)MAGIC_VALUES[magic_index][i]); 183 } 184 185 out.annotate("magic: " + Utf8Utils.escapeString(magicBuilder.toString())); 186 out.write(MAGIC_VALUES[magic_index]); 187 188 out.annotate("checksum"); 189 out.writeInt(0); 190 191 out.annotate("signature"); 192 out.write(new byte[20]); 193 194 out.annotate("file_size: 0x" + Integer.toHexString(dexFile.getFileSize()) + " (" + dexFile.getFileSize() + 195 " bytes)"); 196 out.writeInt(dexFile.getFileSize()); 197 198 out.annotate("header_size: 0x" + Integer.toHexString(HEADER_SIZE)); 199 out.writeInt(HEADER_SIZE); 200 201 out.annotate("endian_tag: 0x" + Integer.toHexString(LITTLE_ENDIAN)); 202 out.writeInt(LITTLE_ENDIAN); 203 204 out.annotate("link_size: 0"); 205 out.writeInt(0); 206 207 out.annotate("link_off: 0"); 208 out.writeInt(0); 209 210 out.annotate("map_off: 0x" + Integer.toHexString(dexFile.MapItem.getOffset())); 211 out.writeInt(dexFile.MapItem.getOffset()); 212 213 out.annotate("string_ids_size: " + dexFile.StringIdsSection.getItems().size()); 214 out.writeInt(dexFile.StringIdsSection.getItems().size()); 215 216 out.annotate("string_ids_off: 0x" + Integer.toHexString(dexFile.StringIdsSection.getOffset())); 217 out.writeInt(dexFile.StringIdsSection.getOffset()); 218 219 out.annotate("type_ids_size: " + dexFile.TypeIdsSection.getItems().size()); 220 out.writeInt(dexFile.TypeIdsSection.getItems().size()); 221 222 out.annotate("type_ids_off: 0x" + Integer.toHexString(dexFile.TypeIdsSection.getOffset())); 223 out.writeInt(dexFile.TypeIdsSection.getOffset()); 224 225 out.annotate("proto_ids_size: " + dexFile.ProtoIdsSection.getItems().size()); 226 out.writeInt(dexFile.ProtoIdsSection.getItems().size()); 227 228 out.annotate("proto_ids_off: 0x" + Integer.toHexString(dexFile.ProtoIdsSection.getOffset())); 229 out.writeInt(dexFile.ProtoIdsSection.getOffset()); 230 231 out.annotate("field_ids_size: " + dexFile.FieldIdsSection.getItems().size()); 232 out.writeInt(dexFile.FieldIdsSection.getItems().size()); 233 234 out.annotate("field_ids_off: 0x" + Integer.toHexString(dexFile.FieldIdsSection.getOffset())); 235 out.writeInt(dexFile.FieldIdsSection.getOffset()); 236 237 out.annotate("method_ids_size: " + dexFile.MethodIdsSection.getItems().size()); 238 out.writeInt(dexFile.MethodIdsSection.getItems().size()); 239 240 out.annotate("method_ids_off: 0x" + Integer.toHexString(dexFile.MethodIdsSection.getOffset())); 241 out.writeInt(dexFile.MethodIdsSection.getOffset()); 242 243 out.annotate("class_defs_size: " + dexFile.ClassDefsSection.getItems().size()); 244 out.writeInt(dexFile.ClassDefsSection.getItems().size()); 245 246 out.annotate("class_defs_off: 0x" + Integer.toHexString(dexFile.ClassDefsSection.getOffset())); 247 out.writeInt(dexFile.ClassDefsSection.getOffset()); 248 249 out.annotate("data_size: 0x" + Integer.toHexString(dexFile.getDataSize()) + " (" + dexFile.getDataSize() + 250 " bytes)"); 251 out.writeInt(dexFile.getDataSize()); 252 253 out.annotate("data_off: 0x" + Integer.toHexString(dexFile.getDataOffset())); 254 out.writeInt(dexFile.getDataOffset()); 255 } 256 257 /** {@inheritDoc} */ getItemType()258 public ItemType getItemType() { 259 return ItemType.TYPE_HEADER_ITEM; 260 } 261 262 /** {@inheritDoc} */ getConciseIdentity()263 public String getConciseIdentity() { 264 return "header_item"; 265 } 266 267 /** {@inheritDoc} */ compareTo(HeaderItem o)268 public int compareTo(HeaderItem o) { 269 //there is only 1 header item 270 return 0; 271 } 272 273 /** 274 * Get the checksum that was originally stored as part of this header item 275 * 276 * Note that this should only be called if this HeaderItem is from a DexFile that was read from disk, as opposed 277 * to one that is created from scratch. 278 * 279 * @return The addler32 checksum (as an integer) of the dex file 280 */ getChecksum()281 public int getChecksum() { 282 Preconditions.checkState(checksumSignatureSet, 283 "This can only be called on a DexFile that was read from disk."); 284 return checksum; 285 } 286 287 /** 288 * Get the signature that was originally stored as part of this header item 289 * 290 * Note that this should only be called if this HeaderItem is from a DexFile that was read from disk, as opposed 291 * to one that is created from scratch. 292 * 293 * @return The sha1 checksum of the dex file, as a 20-element byte array 294 */ getSignature()295 public byte[] getSignature() { 296 Preconditions.checkState(checksumSignatureSet, 297 "This can only be called on a DexFile that was read from disk."); 298 return signature; 299 } 300 301 } 302