1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dex.structure;
18 
19 /**
20  * {@code DexEncodedValueType} represents the type of an {code DexEncodedValue}.
21  */
22 public enum DexEncodedValueType {
23     /**
24      * <pre>
25      * VALUE_BYTE 0x00 (none; must be 0) ubyte[1]
26      * </pre>
27      *
28      * signed one-byte integer value
29      */
30     VALUE_BYTE((byte) 0x00),
31     /**
32      * <pre>
33      * VALUE_SHORT 0x02 size - 1 (0...1) ubyte[size]
34      * </pre>
35      *
36      * signed two-byte integer value, sign-extended
37      */
38     VALUE_SHORT((byte) 0x02),
39     /**
40      * <pre>
41      * VALUE_CHAR 0x03 size - 1 (0...1) ubyte[size]
42      * </pre>
43      *
44      * unsigned two-byte integer value, zero-extended
45      */
46     VALUE_CHAR((byte) 0x03),
47     /**
48      * <pre>
49      * VALUE_INT 0x04 size - 1 (0...3) ubyte[size]
50      * </pre>
51      *
52      * signed four-byte integer value, sign-extended
53      */
54     VALUE_INT((byte) 0x04),
55     /**
56      * <pre>
57      * VALUE_LONG 0x06 size - 1 (0...7) ubyte[size]
58      * </pre>
59      *
60      * signed eight-byte integer value, sign-extended
61      */
62     VALUE_LONG((byte) 0x06),
63     /**
64      * <pre>
65      * VALUE_FLOAT 0x10 size - 1 (0...3) ubyte[size]
66      * </pre>
67      *
68      * four-byte bit pattern, zero-extended to the right, and interpreted as an
69      * IEEE754 32-bit floating point value
70      */
71     VALUE_FLOAT((byte) 0x10),
72     /**
73      * <pre>
74      * VALUE_DOUBLE 0x11 size - 1 (0...7) ubyte[size]
75      * </pre>
76      *
77      * eight-byte bit pattern, zero-extended to the right, and interpreted as an
78      * IEEE754 64-bit floating point value
79      */
80     VALUE_DOUBLE((byte) 0x11),
81     /**
82      * <pre>
83      * VALUE_STRING    0x17     size - 1 (0...3)     ubyte[size]
84      * </pre>
85      *
86      * unsigned (zero-extended) four-byte integer value, interpreted as an index
87      * into the string_ids section and representing a string value
88      */
89     VALUE_STRING((byte) 0x17),
90     /**
91      * <pre>
92      * VALUE_TYPE 0x18 size - 1 (0...3) ubyte[size]
93      * </pre>
94      *
95      * unsigned (zero-extended) four-byte integer value, interpreted as an index
96      * into the type_ids section and representing a reflective type/class value
97      */
98     VALUE_TYPE((byte) 0x18),
99     /**
100      * <pre>
101      * VALUE_FIELD 0x19 size - 1 (0...3) ubyte[size]
102      * </pre>
103      *
104      * unsigned (zero-extended) four-byte integer value, interpreted as an index
105      * into the field_ids section and representing a reflective field value
106      */
107     VALUE_FIELD((byte) 0x19),
108     /**
109      * <pre>
110      * VALUE_METHOD 0x1a size - 1 (0...3) ubyte[size]
111      * </pre>
112      *
113      * unsigned (zero-extended) four-byte integer value, interpreted as an index
114      * into the method_ids section and representing a reflective method value
115      */
116     VALUE_METHOD((byte) 0x1a),
117     /**
118      * <pre>
119      * VALUE_ENUM 0x1b size - 1 (0...3) ubyte[size]
120      * </pre>
121      *
122      * unsigned (zero-extended) four-byte integer value, interpreted as an index
123      * into the field_ids section and representing the value of an enumerated
124      * type constant
125      */
126     VALUE_ENUM((byte) 0x1b),
127     /**
128      * <pre>
129      * VALUE_ARRAY 0x1c (none; must be 0) encoded_array
130      * </pre>
131      *
132      * an array of values, in the format specified by "encoded_array Format"
133      * below. The size of the value is implicit in the encoding.
134      */
135     VALUE_ARRAY((byte) 0x1c),
136     /**
137      * <pre>
138      * VALUE_ANNOTATION 0x1d (none; must be 0) encoded_annotation
139      * </pre>
140      *
141      * a sub-annotation, in the format specified by "encoded_annotation Format"
142      * below. The size of the value is implicit in the encoding.
143      */
144     VALUE_ANNOTATION((byte) 0x1d),
145     /**
146      * <pre>
147      * VALUE_NULL 0x1e (none; must be 0) (none)
148      * </pre>
149      *
150      * null reference value
151      */
152     VALUE_NULL((byte) 0x1e),
153     /**
154      * <pre>
155      * VALUE_BOOLEAN 0x1f boolean (0...1) (none)
156      * </pre>
157      *
158      * one-bit value; 0 for false and 1 for true. The bit is represented in the
159      * value_arg.
160      */
161     VALUE_BOOLEAN((byte) 0x1f);
162 
163     private byte value;
164 
165     /**
166      * Creates a new instance of {@code DexEncodedValueType} using the provided
167      * byte.
168      * <p>
169      * Format: value := (value_arg << 5) | value_type
170      *
171      * @param value
172      *            the {@code byte} containing the type and the value argument
173      */
DexEncodedValueType(byte value)174     private DexEncodedValueType(byte value) {
175         this.value = value;
176     }
177 
178     /**
179      * Returns the {@code DexEncodedValueType} for the given {@code byte}.
180      *
181      * @param value
182      *            the {@code byte} containing the type and the value argument
183      * @return the {@code DexEncodedValueType} for the given {@code byte}
184      */
get(byte value)185     public static DexEncodedValueType get(byte value) {
186         // FIXME don't loop -> switch to get performance boost
187         for (DexEncodedValueType type : values()) {
188             if (type.value == (value & 0x1F)) {
189                 return type;
190             }
191         }
192         throw new IllegalArgumentException("Type does not exist!");
193     }
194 
195     /**
196      * Returns the value argument of the given {@code byte}.
197      * <p>
198      * Format: value := (value_arg << 5) | value_type
199      *
200      * @param value
201      *            the {@code byte} containing the type and the value argument
202      * @return the value argument of the given {@code byte}
203      */
valueArg(byte value)204     public static byte valueArg(byte value) {
205         return (byte) (value >>> 5);
206     }
207 }
208