blob: ad551990bfef6ebc2b81d4201a5412ea15258c41 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_LEB128_H_
4#define ART_SRC_LEB128_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07007
8namespace art {
9
10// Reads an unsigned LEB128 value, updating the given pointer to point
11// just past the end of the read value. This function tolerates
12// non-zero high-order bits in the fifth encoded byte.
13static inline uint32_t DecodeUnsignedLeb128(const byte** data) {
14 const byte* ptr = *data;
15 int result = *(ptr++);
16 if (result > 0x7f) {
17 int cur = *(ptr++);
18 result = (result & 0x7f) | ((cur & 0x7f) << 7);
19 if (cur > 0x7f) {
20 cur = *(ptr++);
21 result |= (cur & 0x7f) << 14;
22 if (cur > 0x7f) {
23 cur = *(ptr++);
24 result |= (cur & 0x7f) << 21;
25 if (cur > 0x7f) {
26 // Note: We don't check to see if cur is out of range here,
27 // meaning we tolerate garbage in the four high-order bits.
28 cur = *(ptr++);
29 result |= cur << 28;
30 }
31 }
32 }
33 }
34 *data = ptr;
35 return (uint32_t)result;
36}
37
Shih-wei Liao195487c2011-08-20 13:29:04 -070038// Reads an unsigned LEB128 + 1 value. updating the given pointer to point
39// just past the end of the read value. This function tolerates
40// non-zero high-order bits in the fifth encoded byte.
41// It is possible for this function to return -1.
42static inline int32_t DecodeUnsignedLeb128P1(const byte** data) {
43 return DecodeUnsignedLeb128(data) - 1;
44}
45
Carl Shapiro1fb86202011-06-27 17:43:13 -070046// Reads a signed LEB128 value, updating the given pointer to point
47// just past the end of the read value. This function tolerates
48// non-zero high-order bits in the fifth encoded byte.
49static inline int32_t DecodeSignedLeb128(const byte** data) {
50 const byte* ptr = *data;
51 int32_t result = *(ptr++);
52 if (result <= 0x7f) {
53 result = (result << 25) >> 25;
54 } else {
55 int cur = *(ptr++);
56 result = (result & 0x7f) | ((cur & 0x7f) << 7);
57 if (cur <= 0x7f) {
58 result = (result << 18) >> 18;
59 } else {
60 cur = *(ptr++);
61 result |= (cur & 0x7f) << 14;
62 if (cur <= 0x7f) {
63 result = (result << 11) >> 11;
64 } else {
65 cur = *(ptr++);
66 result |= (cur & 0x7f) << 21;
67 if (cur <= 0x7f) {
68 result = (result << 4) >> 4;
69 } else {
70 // Note: We don't check to see if cur is out of range here,
71 // meaning we tolerate garbage in the four high-order bits.
72 cur = *(ptr++);
73 result |= cur << 28;
74 }
75 }
76 }
77 }
78 *data = ptr;
79 return result;
80}
81
jeffhaod1f0fde2011-09-08 17:25:33 -070082// Returns the number of bytes needed to encode the value in unsigned LEB128.
83static inline uint32_t UnsignedLeb128Size(uint32_t data) {
84 uint32_t count = 0;
85 do {
86 data >>= 7;
87 count++;
88 } while (data != 0);
89 return count;
90}
91
92// Writes a 32-bit value in unsigned ULEB128 format.
93// Returns the updated pointer.
94static inline uint8_t* WriteUnsignedLeb128(uint8_t* ptr, uint32_t data) {
95 while (true) {
96 uint8_t out = data & 0x7f;
97 if (out != data) {
98 *ptr++ = out | 0x80;
99 data >>= 7;
100 } else {
101 *ptr++ = out;
102 break;
103 }
104 }
105 return ptr;
106}
107
Carl Shapiro1fb86202011-06-27 17:43:13 -0700108} // namespace art
109
110#endif // ART_SRC_LEB128_H_