summaryrefslogtreecommitdiffstats
path: root/dx/src/com/android/dex/EncodedValueReader.java
blob: a2634641069dff4a5e41c47326ac04aeff178476 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dex;

import com.android.dex.util.ByteInput;

/**
 * Pull parser for encoded values.
 */
public final class EncodedValueReader {
    public static final int ENCODED_BYTE = 0x00;
    public static final int ENCODED_SHORT = 0x02;
    public static final int ENCODED_CHAR = 0x03;
    public static final int ENCODED_INT = 0x04;
    public static final int ENCODED_LONG = 0x06;
    public static final int ENCODED_FLOAT = 0x10;
    public static final int ENCODED_DOUBLE = 0x11;
    public static final int ENCODED_STRING = 0x17;
    public static final int ENCODED_TYPE = 0x18;
    public static final int ENCODED_FIELD = 0x19;
    public static final int ENCODED_ENUM = 0x1b;
    public static final int ENCODED_METHOD = 0x1a;
    public static final int ENCODED_ARRAY = 0x1c;
    public static final int ENCODED_ANNOTATION = 0x1d;
    public static final int ENCODED_NULL = 0x1e;
    public static final int ENCODED_BOOLEAN = 0x1f;

    /** placeholder type if the type is not yet known */
    private static final int MUST_READ = -1;

    protected final ByteInput in;
    private int type = MUST_READ;
    private int annotationType;
    private int arg;

    public EncodedValueReader(ByteInput in) {
        this.in = in;
    }

    public EncodedValueReader(EncodedValue in) {
        this(in.asByteInput());
    }

    /**
     * Creates a new encoded value reader whose only value is the specified
     * known type. This is useful for encoded values without a type prefix,
     * such as class_def_item's encoded_array or annotation_item's
     * encoded_annotation.
     */
    public EncodedValueReader(ByteInput in, int knownType) {
        this.in = in;
        this.type = knownType;
    }

    public EncodedValueReader(EncodedValue in, int knownType) {
        this(in.asByteInput(), knownType);
    }

    /**
     * Returns the type of the next value to read.
     */
    public int peek() {
        if (type == MUST_READ) {
            int argAndType = in.readByte() & 0xff;
            type = argAndType & 0x1f;
            arg = (argAndType & 0xe0) >> 5;
        }
        return type;
    }

    /**
     * Begins reading the elements of an array, returning the array's size. The
     * caller must follow up by calling a read method for each element in the
     * array. For example, this reads a byte array: <pre>   {@code
     *   int arraySize = readArray();
     *   for (int i = 0, i < arraySize; i++) {
     *     readByte();
     *   }
     * }</pre>
     */
    public int readArray() {
        checkType(ENCODED_ARRAY);
        type = MUST_READ;
        return Leb128.readUnsignedLeb128(in);
    }

    /**
     * Begins reading the fields of an annotation, returning the number of
     * fields. The caller must follow up by making alternating calls to {@link
     * #readAnnotationName()} and another read method. For example, this reads
     * an annotation whose fields are all bytes: <pre>   {@code
     *   int fieldCount = readAnnotation();
     *   int annotationType = getAnnotationType();
     *   for (int i = 0; i < fieldCount; i++) {
     *       readAnnotationName();
     *       readByte();
     *   }
     * }</pre>
     */
    public int readAnnotation() {
        checkType(ENCODED_ANNOTATION);
        type = MUST_READ;
        annotationType = Leb128.readUnsignedLeb128(in);
        return Leb128.readUnsignedLeb128(in);
    }

    /**
     * Returns the type of the annotation just returned by {@link
     * #readAnnotation()}. This method's value is undefined unless the most
     * recent call was to {@link #readAnnotation()}.
     */
    public int getAnnotationType() {
        return annotationType;
    }

    public int readAnnotationName() {
        return Leb128.readUnsignedLeb128(in);
    }

    public byte readByte() {
        checkType(ENCODED_BYTE);
        type = MUST_READ;
        return (byte) EncodedValueCodec.readSignedInt(in, arg);
    }

    public short readShort() {
        checkType(ENCODED_SHORT);
        type = MUST_READ;
        return (short) EncodedValueCodec.readSignedInt(in, arg);
    }

    public char readChar() {
        checkType(ENCODED_CHAR);
        type = MUST_READ;
        return (char) EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public int readInt() {
        checkType(ENCODED_INT);
        type = MUST_READ;
        return EncodedValueCodec.readSignedInt(in, arg);
    }

    public long readLong() {
        checkType(ENCODED_LONG);
        type = MUST_READ;
        return EncodedValueCodec.readSignedLong(in, arg);
    }

    public float readFloat() {
        checkType(ENCODED_FLOAT);
        type = MUST_READ;
        return Float.intBitsToFloat(EncodedValueCodec.readUnsignedInt(in, arg, true));
    }

    public double readDouble() {
        checkType(ENCODED_DOUBLE);
        type = MUST_READ;
        return Double.longBitsToDouble(EncodedValueCodec.readUnsignedLong(in, arg, true));
    }

    public int readString() {
        checkType(ENCODED_STRING);
        type = MUST_READ;
        return EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public int readType() {
        checkType(ENCODED_TYPE);
        type = MUST_READ;
        return EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public int readField() {
        checkType(ENCODED_FIELD);
        type = MUST_READ;
        return EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public int readEnum() {
        checkType(ENCODED_ENUM);
        type = MUST_READ;
        return EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public int readMethod() {
        checkType(ENCODED_METHOD);
        type = MUST_READ;
        return EncodedValueCodec.readUnsignedInt(in, arg, false);
    }

    public void readNull() {
        checkType(ENCODED_NULL);
        type = MUST_READ;
    }

    public boolean readBoolean() {
        checkType(ENCODED_BOOLEAN);
        type = MUST_READ;
        return arg != 0;
    }

    /**
     * Skips a single value, including its nested values if it is an array or
     * annotation.
     */
    public void skipValue() {
        switch (peek()) {
        case ENCODED_BYTE:
            readByte();
            break;
        case ENCODED_SHORT:
            readShort();
            break;
        case ENCODED_CHAR:
            readChar();
            break;
        case ENCODED_INT:
            readInt();
            break;
        case ENCODED_LONG:
            readLong();
            break;
        case ENCODED_FLOAT:
            readFloat();
            break;
        case ENCODED_DOUBLE:
            readDouble();
            break;
        case ENCODED_STRING:
            readString();
            break;
        case ENCODED_TYPE:
            readType();
            break;
        case ENCODED_FIELD:
            readField();
            break;
        case ENCODED_ENUM:
            readEnum();
            break;
        case ENCODED_METHOD:
            readMethod();
            break;
        case ENCODED_ARRAY:
            for (int i = 0, size = readArray(); i < size; i++) {
                skipValue();
            }
            break;
        case ENCODED_ANNOTATION:
            for (int i = 0, size = readAnnotation(); i < size; i++) {
                readAnnotationName();
                skipValue();
            }
            break;
        case ENCODED_NULL:
            readNull();
            break;
        case ENCODED_BOOLEAN:
            readBoolean();
            break;
        default:
            throw new DexException("Unexpected type: " + Integer.toHexString(type));
        }
    }

    private void checkType(int expected) {
        if (peek() != expected) {
            throw new IllegalStateException("Expected array but was "
                    + Integer.toHexString(peek()));
        }
    }
}