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
32package org.jf.dexlib2.dexbacked.raw;
33
34import org.jf.dexlib2.dexbacked.BaseDexBuffer;
35
36public class OdexHeaderItem {
37    public static final int ITEM_SIZE = 40;
38
39    public static final byte[][] MAGIC_VALUES= new byte[][] {
40            new byte[] {0x64, 0x65, 0x79, 0x0A, 0x30, 0x33, 0x35, 0x00}, // "dey\n035\0"
41            new byte[] {0x64, 0x65, 0x79, 0x0A, 0x30, 0x33, 0x36, 0x00}  // "dey\n036\0"
42    };
43
44    public static final int MAGIC_OFFSET = 0;
45    public static final int MAGIC_LENGTH = 8;
46    public static final int DEX_OFFSET = 8;
47    public static final int DEX_LENGTH_OFFSET = 12;
48    public static final int DEPENDENCIES_OFFSET = 16;
49    public static final int DEPENDENCIES_LENGTH_OFFSET = 20;
50    public static final int AUX_OFFSET = 24;
51    public static final int AUX_LENGTH_OFFSET = 28;
52    public static final int FLAGS_OFFSET = 32;
53
54    public static int getVersion(byte[] magic) {
55        if (magic.length < 8) {
56            return 0;
57        }
58
59        boolean matches = true;
60        for (int i=0; i<MAGIC_VALUES.length; i++) {
61            byte[] expected = MAGIC_VALUES[i];
62            matches = true;
63            for (int j=0; j<8; j++) {
64                if (magic[j] != expected[j]) {
65                    matches = false;
66                    break;
67                }
68            }
69            if (matches) {
70                return i==0?35:36;
71            }
72        }
73        return 0;
74    }
75
76    public static boolean verifyMagic(byte[] buf) {
77        // verifies the magic value
78        return getVersion(buf) != 0;
79    }
80
81    public static int getDexOffset(byte[] buf) {
82        BaseDexBuffer bdb = new BaseDexBuffer(buf);
83        return bdb.readSmallUint(DEX_OFFSET);
84    }
85
86    public static int getDependenciesOffset(byte[] buf) {
87        BaseDexBuffer bdb = new BaseDexBuffer(buf);
88        return bdb.readSmallUint(DEPENDENCIES_OFFSET);
89    }
90}
91