OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CRAZY_LINKER_LEB128_H | 5 #ifndef CRAZY_LINKER_LEB128_H |
6 #define CRAZY_LINKER_LEB128_H | 6 #define CRAZY_LINKER_LEB128_H |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 // Helper classes for decoding LEB128, used in packed relocation data. | 11 // Helper classes for decoding LEB128, used in packed relocation data. |
12 // http://en.wikipedia.org/wiki/LEB128 | 12 // http://en.wikipedia.org/wiki/LEB128 |
13 | 13 |
14 namespace crazy { | 14 namespace crazy { |
15 | 15 |
16 class Sleb128Decoder { | 16 class Sleb128Decoder { |
17 public: | 17 public: |
18 Sleb128Decoder(const uint8_t* buffer, size_t count) | 18 Sleb128Decoder(const uint8_t* buffer, size_t count) : current_(buffer) |
19 : current_(buffer), end_(buffer + count) { } | 19 #ifndef NDEBUG |
| 20 , end_(buffer + count) |
| 21 #endif |
| 22 {} |
20 | 23 |
21 size_t pop_front() { | 24 size_t pop_front() { |
22 size_t value = 0; | 25 size_t value = 0; |
23 static const size_t size = CHAR_BIT * sizeof(value); | 26 static const size_t size = CHAR_BIT * sizeof(value); |
24 | 27 |
25 size_t shift = 0; | 28 size_t shift = 0; |
26 uint8_t byte; | 29 uint8_t byte; |
27 | 30 |
28 do { | 31 do { |
29 assert(current_ < end_); | 32 assert(current_ < end_); |
30 | 33 |
31 byte = *current_++; | 34 byte = *current_++; |
32 value |= (static_cast<size_t>(byte & 127) << shift); | 35 value |= (static_cast<size_t>(byte & 127) << shift); |
33 shift += 7; | 36 shift += 7; |
34 } while (byte & 128); | 37 } while (byte & 128); |
35 | 38 |
36 if (shift < size && (byte & 64)) { | 39 if (shift < size && (byte & 64)) { |
37 value |= -(static_cast<size_t>(1) << shift); | 40 value |= -(static_cast<size_t>(1) << shift); |
38 } | 41 } |
39 | 42 |
40 return value; | 43 return value; |
41 } | 44 } |
42 | 45 |
43 private: | 46 private: |
44 const uint8_t* current_; | 47 const uint8_t* current_; |
| 48 #ifndef NDEBUG |
45 const uint8_t* const end_; | 49 const uint8_t* const end_; |
| 50 #endif |
46 }; | 51 }; |
47 | 52 |
48 } // namespace crazy | 53 } // namespace crazy |
49 | 54 |
50 #endif // CRAZY_LINKER_LEB128_H | 55 #endif // CRAZY_LINKER_LEB128_H |
OLD | NEW |