blob: 918d711c9e63ca2aea6c48033c350540044b2f75 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_LEB128_H_
18#define ART_SRC_LEB128_H_
19
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "globals.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070021
22namespace art {
23
24// Reads an unsigned LEB128 value, updating the given pointer to point
25// just past the end of the read value. This function tolerates
26// non-zero high-order bits in the fifth encoded byte.
27static inline uint32_t DecodeUnsignedLeb128(const byte** data) {
28 const byte* ptr = *data;
29 int result = *(ptr++);
30 if (result > 0x7f) {
31 int cur = *(ptr++);
32 result = (result & 0x7f) | ((cur & 0x7f) << 7);
33 if (cur > 0x7f) {
34 cur = *(ptr++);
35 result |= (cur & 0x7f) << 14;
36 if (cur > 0x7f) {
37 cur = *(ptr++);
38 result |= (cur & 0x7f) << 21;
39 if (cur > 0x7f) {
40 // Note: We don't check to see if cur is out of range here,
41 // meaning we tolerate garbage in the four high-order bits.
42 cur = *(ptr++);
43 result |= cur << 28;
44 }
45 }
46 }
47 }
48 *data = ptr;
49 return (uint32_t)result;
50}
51
Shih-wei Liao195487c2011-08-20 13:29:04 -070052// Reads an unsigned LEB128 + 1 value. updating the given pointer to point
53// just past the end of the read value. This function tolerates
54// non-zero high-order bits in the fifth encoded byte.
55// It is possible for this function to return -1.
56static inline int32_t DecodeUnsignedLeb128P1(const byte** data) {
57 return DecodeUnsignedLeb128(data) - 1;
58}
59
Carl Shapiro1fb86202011-06-27 17:43:13 -070060// Reads a signed LEB128 value, updating the given pointer to point
61// just past the end of the read value. This function tolerates
62// non-zero high-order bits in the fifth encoded byte.
63static inline int32_t DecodeSignedLeb128(const byte** data) {
64 const byte* ptr = *data;
65 int32_t result = *(ptr++);
66 if (result <= 0x7f) {
67 result = (result << 25) >> 25;
68 } else {
69 int cur = *(ptr++);
70 result = (result & 0x7f) | ((cur & 0x7f) << 7);
71 if (cur <= 0x7f) {
72 result = (result << 18) >> 18;
73 } else {
74 cur = *(ptr++);
75 result |= (cur & 0x7f) << 14;
76 if (cur <= 0x7f) {
77 result = (result << 11) >> 11;
78 } else {
79 cur = *(ptr++);
80 result |= (cur & 0x7f) << 21;
81 if (cur <= 0x7f) {
82 result = (result << 4) >> 4;
83 } else {
84 // Note: We don't check to see if cur is out of range here,
85 // meaning we tolerate garbage in the four high-order bits.
86 cur = *(ptr++);
87 result |= cur << 28;
88 }
89 }
90 }
91 }
92 *data = ptr;
93 return result;
94}
95
jeffhaod1f0fde2011-09-08 17:25:33 -070096// Returns the number of bytes needed to encode the value in unsigned LEB128.
97static inline uint32_t UnsignedLeb128Size(uint32_t data) {
98 uint32_t count = 0;
99 do {
100 data >>= 7;
101 count++;
102 } while (data != 0);
103 return count;
104}
105
106// Writes a 32-bit value in unsigned ULEB128 format.
107// Returns the updated pointer.
108static inline uint8_t* WriteUnsignedLeb128(uint8_t* ptr, uint32_t data) {
109 while (true) {
110 uint8_t out = data & 0x7f;
111 if (out != data) {
112 *ptr++ = out | 0x80;
113 data >>= 7;
114 } else {
115 *ptr++ = out;
116 break;
117 }
118 }
119 return ptr;
120}
121
Carl Shapiro1fb86202011-06-27 17:43:13 -0700122} // namespace art
123
124#endif // ART_SRC_LEB128_H_