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
29package org.jf.dexlib;
30
31import org.jf.dexlib.Util.AnnotatedOutput;
32import org.jf.dexlib.Util.Input;
33import org.jf.dexlib.Util.Utf8Utils;
34
35public class HeaderItem extends Item<HeaderItem> {
36    /**
37     * the file format magic number, represented as the
38     * low-order bytes of a string
39     */
40    public static final byte[] MAGIC = new byte[] {0x64, 0x65, 0x78, 0x0A, 0x30, 0x33, 0x35, 0x00};//"dex\n035" + '\0';
41
42
43    /** size of this section, in bytes */
44    private static final int HEADER_SIZE = 0x70;
45
46    /** the endianness constants */
47    private static final int LITTLE_ENDIAN = 0x12345678;
48    private static final int BIG_ENDIAN = 0x78562312;
49
50    /**
51     * Create a new uninitialized <code>HeaderItem</code>
52     * @param dexFile The <code>DexFile</code> containing this <code>HeaderItem</code>
53     */
54    protected HeaderItem(final DexFile dexFile) {
55        super(dexFile);
56    }
57
58    /** {@inheritDoc} */
59    protected void readItem(Input in, ReadContext readContext) {
60        byte[] readMagic = in.readBytes(8);
61
62        for (int i=0; i<8; i++) {
63            if (MAGIC[i] != readMagic[i]) {
64                throw new RuntimeException("The magic value is not the expected value");
65            }
66        }
67
68        in.readBytes(20); //checksum
69        in.readInt(); //signature
70        in.readInt(); //filesize
71        if (in.readInt() != HEADER_SIZE) {
72            throw new RuntimeException("The header size is not the expected value (0x70)");
73        }
74
75        int endianTag = in.readInt();
76        if (endianTag == BIG_ENDIAN) {
77            throw new RuntimeException("This dex file is big endian. Only little endian is currently supported.");
78        } else if (endianTag != LITTLE_ENDIAN) {
79            throw new RuntimeException("The endian tag is not 0x12345678 or 0x78563412");
80        }
81
82        //link_size + link_off
83        if ((in.readInt() | in.readInt()) != 0) {
84            System.err.println("This dex file has a link section, which is not supported. Ignoring.");
85        }
86
87        int sectionSize;
88        int sectionOffset;
89
90        //map_offset
91        sectionOffset = in.readInt();
92        readContext.addSection(ItemType.TYPE_MAP_LIST, 1, sectionOffset);
93
94        //string_id_item
95        sectionSize = in.readInt();
96        sectionOffset = in.readInt();
97        readContext.addSection(ItemType.TYPE_STRING_ID_ITEM, sectionSize, sectionOffset);
98
99        //type_id_item
100        sectionSize = in.readInt();
101        sectionOffset = in.readInt();
102        readContext.addSection(ItemType.TYPE_TYPE_ID_ITEM, sectionSize, sectionOffset);
103
104        //proto_id_item
105        sectionSize = in.readInt();
106        sectionOffset = in.readInt();
107        readContext.addSection(ItemType.TYPE_PROTO_ID_ITEM, sectionSize, sectionOffset);
108
109        //field_id_item
110        sectionSize = in.readInt();
111        sectionOffset = in.readInt();
112        readContext.addSection(ItemType.TYPE_FIELD_ID_ITEM, sectionSize, sectionOffset);
113
114        //method_id_item
115        sectionSize = in.readInt();
116        sectionOffset = in.readInt();
117        readContext.addSection(ItemType.TYPE_METHOD_ID_ITEM, sectionSize, sectionOffset);
118
119        //class_data_item
120        sectionSize = in.readInt();
121        sectionOffset = in.readInt();
122        readContext.addSection(ItemType.TYPE_CLASS_DEF_ITEM, sectionSize, sectionOffset);
123
124        in.readInt(); //data_size
125        in.readInt(); //data_off
126    }
127
128    /** {@inheritDoc} */
129    protected int placeItem(int offset) {
130        return HEADER_SIZE;
131    }
132
133    /** {@inheritDoc} */
134    protected void writeItem(AnnotatedOutput out) {
135        StringBuilder magicBuilder = new StringBuilder();
136        for (int i=0; i<8; i++) {
137            magicBuilder.append((char)MAGIC[i]);
138        }
139
140        out.annotate("magic: " + Utf8Utils.escapeString(magicBuilder.toString()));
141        out.write(MAGIC);
142
143        out.annotate("checksum");
144        out.writeInt(0);
145
146        out.annotate("signature");
147        out.write(new byte[20]);
148
149        out.annotate("file_size: 0x" + Integer.toHexString(dexFile.getFileSize()) + " (" + dexFile.getFileSize() +
150                " bytes)");
151        out.writeInt(dexFile.getFileSize());
152
153        out.annotate("header_size: 0x" + Integer.toHexString(HEADER_SIZE));
154        out.writeInt(HEADER_SIZE);
155
156        out.annotate("endian_tag: 0x" + Integer.toHexString(LITTLE_ENDIAN));
157        out.writeInt(LITTLE_ENDIAN);
158
159        out.annotate("link_size: 0");
160        out.writeInt(0);
161
162        out.annotate("link_off: 0");
163        out.writeInt(0);
164
165        out.annotate("map_off: 0x" + Integer.toHexString(dexFile.MapItem.getOffset()));
166        out.writeInt(dexFile.MapItem.getOffset());
167
168        out.annotate("string_ids_size: " + dexFile.StringIdsSection.getItems().size());
169        out.writeInt(dexFile.StringIdsSection.getItems().size());
170
171        out.annotate("string_ids_off: 0x" + Integer.toHexString(dexFile.StringIdsSection.getOffset()));
172        out.writeInt(dexFile.StringIdsSection.getOffset());
173
174        out.annotate("type_ids_size: " + dexFile.TypeIdsSection.getItems().size());
175        out.writeInt(dexFile.TypeIdsSection.getItems().size());
176
177        out.annotate("type_ids_off: 0x" + Integer.toHexString(dexFile.TypeIdsSection.getOffset()));
178        out.writeInt(dexFile.TypeIdsSection.getOffset());
179
180        out.annotate("proto_ids_size: " + dexFile.ProtoIdsSection.getItems().size());
181        out.writeInt(dexFile.ProtoIdsSection.getItems().size());
182
183        out.annotate("proto_ids_off: 0x" + Integer.toHexString(dexFile.ProtoIdsSection.getOffset()));
184        out.writeInt(dexFile.ProtoIdsSection.getOffset());
185
186        out.annotate("field_ids_size: " + dexFile.FieldIdsSection.getItems().size());
187        out.writeInt(dexFile.FieldIdsSection.getItems().size());
188
189        out.annotate("field_ids_off: 0x" + Integer.toHexString(dexFile.FieldIdsSection.getOffset()));
190        out.writeInt(dexFile.FieldIdsSection.getOffset());
191
192        out.annotate("method_ids_size: " + dexFile.MethodIdsSection.getItems().size());
193        out.writeInt(dexFile.MethodIdsSection.getItems().size());
194
195        out.annotate("method_ids_off: 0x" + Integer.toHexString(dexFile.MethodIdsSection.getOffset()));
196        out.writeInt(dexFile.MethodIdsSection.getOffset());
197
198        out.annotate("class_defs_size: " + dexFile.ClassDefsSection.getItems().size());
199        out.writeInt(dexFile.ClassDefsSection.getItems().size());
200
201        out.annotate("class_defs_off: 0x" + Integer.toHexString(dexFile.ClassDefsSection.getOffset()));
202        out.writeInt(dexFile.ClassDefsSection.getOffset());
203
204        out.annotate("data_size: 0x" + Integer.toHexString(dexFile.getDataSize()) + " (" + dexFile.getDataSize() +
205                " bytes)");
206        out.writeInt(dexFile.getDataSize());
207
208        out.annotate("data_off: 0x" + Integer.toHexString(dexFile.getDataOffset()));
209        out.writeInt(dexFile.getDataOffset());
210    }
211
212    /** {@inheritDoc} */
213    public ItemType getItemType() {
214        return ItemType.TYPE_HEADER_ITEM;
215    }
216
217    /** {@inheritDoc} */
218    public String getConciseIdentity() {
219        return "header_item";
220    }
221
222    /** {@inheritDoc} */
223    public int compareTo(HeaderItem o) {
224        //there is only 1 header item
225        return 0;
226    }
227}
228