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
31 * Reads an unsigned LEB128 value, updating the given pointer to point
32 * just past the end of the read value. This function tolerates
33 * non-zero high-order bits in the fifth encoded byte.
35 DEX_INLINE int readUnsignedLeb128(const u1** pStream) {
36 const u1* ptr = *pStream;
37 int result = *(ptr++);
41 result = (result & 0x7f) | ((cur & 0x7f) << 7);
44 result |= (cur & 0x7f) << 14;
47 result |= (cur & 0x7f) << 21;
50 * Note: We don't check to see if cur is out of
51 * range here, meaning we tolerate garbage in the
52 * high four-order bits.
66 * Reads a signed LEB128 value, updating the given pointer to point
67 * just past the end of the read value. This function tolerates
68 * non-zero high-order bits in the fifth encoded byte.
70 DEX_INLINE int readSignedLeb128(const u1** pStream) {
71 const u1* ptr = *pStream;
72 int result = *(ptr++);
75 result = (result << 25) >> 25;
78 result = (result & 0x7f) | ((cur & 0x7f) << 7);
80 result = (result << 18) >> 18;
83 result |= (cur & 0x7f) << 14;
85 result = (result << 11) >> 11;
88 result |= (cur & 0x7f) << 21;
90 result = (result << 4) >> 4;
93 * Note: We don't check to see if cur is out of
94 * range here, meaning we tolerate garbage in the
95 * high four-order bits.
109 * Reads an unsigned LEB128 value, updating the given pointer to point
110 * just past the end of the read value and also indicating whether the
111 * value was syntactically valid. The only syntactically *invalid*
112 * values are ones that are five bytes long where the final byte has
113 * any but the low-order four bits set. Additionally, if the limit is
114 * passed as non-NULL and bytes would need to be read past the limit,
115 * then the read is considered invalid.
117 int readAndVerifyUnsignedLeb128(const u1** pStream, const u1* limit,
121 * Reads a signed LEB128 value, updating the given pointer to point
122 * just past the end of the read value and also indicating whether the
123 * value was syntactically valid. The only syntactically *invalid*
124 * values are ones that are five bytes long where the final byte has
125 * any but the low-order four bits set. Additionally, if the limit is
126 * passed as non-NULL and bytes would need to be read past the limit,
127 * then the read is considered invalid.
129 int readAndVerifySignedLeb128(const u1** pStream, const u1* limit, bool* okay);
133 * Writes a 32-bit value in unsigned ULEB128 format.
135 * Returns the updated pointer.
137 DEX_INLINE u1* writeUnsignedLeb128(u1* ptr, u4 data)
140 u1 out = data & 0x7f;
154 * Returns the number of bytes needed to encode "val" in ULEB128 form.
156 DEX_INLINE int unsignedLeb128Size(u4 data)