2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Functions for interpreting LEB128 (little endian base 128) values
21 #ifndef _LIBDEX_LEB128
22 #define _LIBDEX_LEB128
27 * Reads an unsigned LEB128 value, updating the given pointer to point
28 * just past the end of the read value. This function tolerates
29 * non-zero high-order bits in the fifth encoded byte.
31 DEX_INLINE int readUnsignedLeb128(const u1** pStream) {
32 const u1* ptr = *pStream;
33 int result = *(ptr++);
37 result = (result & 0x7f) | ((cur & 0x7f) << 7);
40 result |= (cur & 0x7f) << 14;
43 result |= (cur & 0x7f) << 21;
46 * Note: We don't check to see if cur is out of
47 * range here, meaning we tolerate garbage in the
48 * high four-order bits.
62 * Reads a signed LEB128 value, updating the given pointer to point
63 * just past the end of the read value. This function tolerates
64 * non-zero high-order bits in the fifth encoded byte.
66 DEX_INLINE int readSignedLeb128(const u1** pStream) {
67 const u1* ptr = *pStream;
68 int result = *(ptr++);
71 result = (result << 25) >> 25;
74 result = (result & 0x7f) | ((cur & 0x7f) << 7);
76 result = (result << 18) >> 18;
79 result |= (cur & 0x7f) << 14;
81 result = (result << 11) >> 11;
84 result |= (cur & 0x7f) << 21;
86 result = (result << 4) >> 4;
89 * Note: We don't check to see if cur is out of
90 * range here, meaning we tolerate garbage in the
91 * high four-order bits.
105 * Reads an unsigned LEB128 value, updating the given pointer to point
106 * just past the end of the read value and also indicating whether the
107 * value was syntactically valid. The only syntactically *invalid*
108 * values are ones that are five bytes long where the final byte has
109 * any but the low-order four bits set. Additionally, if the limit is
110 * passed as non-NULL and bytes would need to be read past the limit,
111 * then the read is considered invalid.
113 int readAndVerifyUnsignedLeb128(const u1** pStream, const u1* limit,
117 * Reads a signed LEB128 value, updating the given pointer to point
118 * just past the end of the read value and also indicating whether the
119 * value was syntactically valid. The only syntactically *invalid*
120 * values are ones that are five bytes long where the final byte has
121 * any but the low-order four bits set. Additionally, if the limit is
122 * passed as non-NULL and bytes would need to be read past the limit,
123 * then the read is considered invalid.
125 int readAndVerifySignedLeb128(const u1** pStream, const u1* limit, bool* okay);
129 * Writes a 32-bit value in unsigned ULEB128 format.
131 * Returns the updated pointer.
133 DEX_INLINE u1* writeUnsignedLeb128(u1* ptr, u4 data)
136 u1 out = data & 0x7f;
150 * Returns the number of bytes needed to encode "val" in ULEB128 form.
152 DEX_INLINE int unsignedLeb128Size(u4 data)