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.EncodedValue;
30
31import org.jf.dexlib.DexFile;
32import org.jf.dexlib.Util.AnnotatedOutput;
33import org.jf.dexlib.Util.Input;
34import org.jf.dexlib.Util.Leb128Utils;
35
36/**
37 * An <code>ArrayEncodedSubValue</code> is identical to an <code>ArrayEncodedValue</code>, except that it
38 * doesn't have the initial valueType/valueArg byte. This is used in the <code>EncodedArrayItem</code> object
39 */
40public class ArrayEncodedSubValue extends EncodedValue {
41 private int hashCode = 0;
42
43    public final EncodedValue[] values;
44
45    /**
46     * Constructs a new <code>ArrayEncodedSubValue</code> by reading the value from the given <code>Input</code> object.
47     * The <code>Input</code>'s cursor should be set to the 2nd byte of the encoded value
48     * @param dexFile The <code>DexFile</code> that is being read in
49     * @param in The <code>Input</code> object to read from
50     */
51    public ArrayEncodedSubValue(DexFile dexFile, Input in) {
52        values = new EncodedValue[in.readUnsignedLeb128()];
53
54        for (int i=0; i<values.length; i++) {
55            values[i] = EncodedValue.readEncodedValue(dexFile, in);
56        }
57    }
58
59    /**
60     * Constructs a new <code>ArrayEncodedSubValue</code> with the given values
61     * @param values The array values
62     */
63    public ArrayEncodedSubValue(EncodedValue[] values) {
64        this.values = values;
65    }
66
67    /** {@inheritDoc} */
68    public void writeValue(AnnotatedOutput out) {
69        if (out.annotates())
70        {
71            out.annotate("array_size: 0x" + Integer.toHexString(values.length) + " (" + values.length + ")");
72            out.writeUnsignedLeb128(values.length);
73            int index = 0;
74            for (EncodedValue encodedValue: values) {
75                out.annotate(0, "[" + index++ + "] array_element");
76                out.indent();
77                encodedValue.writeValue(out);
78                out.deindent();
79            }
80        } else {
81            out.writeUnsignedLeb128(values.length);
82            for (EncodedValue encodedValue: values) {
83                encodedValue.writeValue(out);
84            }
85        }
86    }
87
88    /** {@inheritDoc} */
89    public int placeValue(int offset) {
90        offset = offset + Leb128Utils.unsignedLeb128Size(values.length);
91        for (EncodedValue encodedValue: values) {
92            offset = encodedValue.placeValue(offset);
93        }
94
95        return offset;
96    }
97
98    /** {@inheritDoc} */
99    protected int compareValue(EncodedValue o) {
100        ArrayEncodedSubValue other = (ArrayEncodedSubValue)o;
101
102        int comp = values.length - other.values.length;
103        if (comp != 0) {
104            return comp;
105        }
106
107        for (int i=0; i<values.length; i++) {
108            comp = values[i].compareTo(other.values[i]);
109            if (comp != 0) {
110                return comp;
111            }
112        }
113
114        return comp;
115    }
116
117    /** {@inheritDoc} */
118    public ValueType getValueType() {
119        return ValueType.VALUE_ARRAY;
120    }
121
122    /**
123     * calculate and cache the hashcode
124     */
125    private void calcHashCode() {
126        hashCode = 0;
127
128        for (EncodedValue encodedValue: values) {
129            hashCode = 31 * hashCode + encodedValue.hashCode();
130        }
131    }
132
133    @Override
134    public int hashCode() {
135        //there's a small possibility that the actual hash code will be 0. If so, we'll
136        //just end up recalculating it each time
137        if (hashCode == 0)
138            calcHashCode();
139        return hashCode;
140    }
141}
142