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.Leb128Utils;
34import org.jf.dexlib.Util.Utf8Utils;
35
36public class StringDataItem extends Item<StringDataItem> {
37    private int hashCode = 0;
38
39    private String stringValue;
40
41    /**
42     * Creates a new uninitialized <code>StringDataItem</code>
43     * @param dexFile The <code>DexFile</code> that this item belongs to
44     */
45    protected StringDataItem(DexFile dexFile) {
46        super(dexFile);
47    }
48
49    /**
50     * Creates a new <code>StringDataItem</code> for the given string
51     * @param dexFile The <code>DexFile</code> that this item belongs to
52     * @param stringValue The string value that this item represents
53     */
54    private StringDataItem(DexFile dexFile, String stringValue) {
55        super(dexFile);
56
57        this.stringValue = stringValue;
58    }
59
60    /**
61     * Returns a <code>StringDataItem</code> for the given values, and that has been interned into
62     * the given <code>DexFile</code>
63     * @param dexFile The <code>DexFile</code> that this item belongs to
64     * @param value The string value that this item represents
65     * @return a <code>StringDataItem</code> for the given values, and that has been interned into
66     * the given <code>DexFile</code>
67     */
68    public static StringDataItem internStringDataItem(DexFile dexFile, String value) {
69        StringDataItem StringDataItem = new StringDataItem(dexFile, value);
70        return dexFile.StringDataSection.intern(StringDataItem);
71    }
72
73    /**
74     * Looks up the <code>StringDataItem</code> from the given <code>DexFile</code> for the given
75     * string value
76     * @param dexFile the <code>Dexfile</code> to find the string value in
77     * @param value The string value to look up
78     * @return a <code>StringDataItem</code> from the given <code>DexFile</code> for the given
79     * string value, or null if it doesn't exist
80     **/
81    public static StringDataItem lookupStringDataItem(DexFile dexFile, String value) {
82        StringDataItem StringDataItem = new StringDataItem(dexFile, value);
83        return dexFile.StringDataSection.getInternedItem(StringDataItem);
84    }
85
86    /** {@inheritDoc} */
87    protected void readItem(Input in, ReadContext readContext) {
88        in.readUnsignedLeb128(); //string length
89        stringValue = in.realNullTerminatedUtf8String();
90    }
91
92    /** {@inheritDoc} */
93    protected int placeItem(int offset) {
94        return offset + Leb128Utils.unsignedLeb128Size(stringValue.length()) +
95                Utf8Utils.stringToUtf8Bytes(stringValue).length + 1;
96    }
97
98    /** {@inheritDoc} */
99    protected void writeItem(AnnotatedOutput out) {
100        byte[] encodedValue = Utf8Utils.stringToUtf8Bytes(stringValue);
101        if (out.annotates()) {
102            out.annotate("string_size: 0x" + Integer.toHexString(stringValue.length()) + " (" + stringValue.length() +
103                    ")");
104            out.writeUnsignedLeb128(stringValue.length());
105
106            out.annotate(encodedValue.length + 1, "string_data: \"" + Utf8Utils.escapeString(stringValue) + "\"");
107        } else {
108            out.writeUnsignedLeb128(stringValue.length());
109        }
110        out.write(encodedValue);
111        out.writeByte(0);
112    }
113
114    /** {@inheritDoc} */
115    public ItemType getItemType() {
116        return ItemType.TYPE_STRING_DATA_ITEM;
117    }
118
119    /** {@inheritDoc} */
120    public String getConciseIdentity() {
121        return "string_data_item: \"" + Utf8Utils.escapeString(getStringValue()) + "\"";
122    }
123
124    /** {@inheritDoc} */
125    public int compareTo(StringDataItem o) {
126        return getStringValue().compareTo(o.getStringValue());
127    }
128
129    /**
130     * Get the string value of this item as a <code>String</code>
131     * @return the string value of this item as a String
132     */
133    public String getStringValue() {
134        return stringValue;
135    }
136
137    /**
138     * calculate and cache the hashcode
139     */
140    private void calcHashCode() {
141        hashCode = getStringValue().hashCode();
142    }
143
144    @Override
145    public int hashCode() {
146        //there's a small possibility that the actual hash code will be 0. If so, we'll
147        //just end up recalculating it each time
148        if (hashCode == 0)
149            calcHashCode();
150        return hashCode;
151    }
152
153    @Override
154    public boolean equals(Object o) {
155        if (this==o) {
156            return true;
157        }
158        if (o==null || !this.getClass().equals(o.getClass())) {
159            return false;
160        }
161
162        //This assumes that the referenced items have been interned in both objects.
163        //This is a valid assumption because all outside code must use the static
164        //"getInterned..." style methods to make new items, and any item created
165        //internally is guaranteed to be interned
166        StringDataItem other = (StringDataItem)o;
167        return getStringValue().equals(other.getStringValue());
168    }
169}
170