blob: 6041f8c31f4fe3795e01ad1f7a1ba51f531c804f [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_LEB128_H_
18#define ART_RUNTIME_LEB128_H_
Carl Shapiro1fb86202011-06-27 17:43:13 -070019
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.
Ian Rogers96faf5b2013-08-09 22:05:32 -070027static inline uint32_t DecodeUnsignedLeb128(const uint8_t** data) {
28 const uint8_t* ptr = *data;
Carl Shapiro1fb86202011-06-27 17:43:13 -070029 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;
buzbeecbd6d442012-11-17 14:11:25 -080049 return static_cast<uint32_t>(result);
Carl Shapiro1fb86202011-06-27 17:43:13 -070050}
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.
Ian Rogers96faf5b2013-08-09 22:05:32 -070056static inline int32_t DecodeUnsignedLeb128P1(const uint8_t** data) {
Shih-wei Liao195487c2011-08-20 13:29:04 -070057 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.
Ian Rogers96faf5b2013-08-09 22:05:32 -070063static inline int32_t DecodeSignedLeb128(const uint8_t** data) {
64 const uint8_t* ptr = *data;
Carl Shapiro1fb86202011-06-27 17:43:13 -070065 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
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106} // namespace art
107
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700108#endif // ART_RUNTIME_LEB128_H_