| 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 <stdint.h> | 9 #include <stdint.h> |
| 9 | 10 |
| 10 // Helper classes for decoding LEB128, used in packed relocation data. | 11 // Helper classes for decoding LEB128, used in packed relocation data. |
| 11 // http://en.wikipedia.org/wiki/LEB128 | 12 // http://en.wikipedia.org/wiki/LEB128 |
| 12 | 13 |
| 13 namespace crazy { | 14 namespace crazy { |
| 14 | 15 |
| 15 class Leb128Decoder { | |
| 16 public: | |
| 17 explicit Leb128Decoder(const uint8_t* encoding) | |
| 18 : encoding_(encoding), cursor_(0) { } | |
| 19 | |
| 20 size_t Dequeue() { | |
| 21 size_t value = 0; | |
| 22 | |
| 23 size_t shift = 0; | |
| 24 uint8_t byte; | |
| 25 | |
| 26 do { | |
| 27 byte = encoding_[cursor_++]; | |
| 28 value |= static_cast<size_t>(byte & 127) << shift; | |
| 29 shift += 7; | |
| 30 } while (byte & 128); | |
| 31 | |
| 32 return value; | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 const uint8_t* encoding_; | |
| 37 size_t cursor_; | |
| 38 }; | |
| 39 | |
| 40 class Sleb128Decoder { | 16 class Sleb128Decoder { |
| 41 public: | 17 public: |
| 42 explicit Sleb128Decoder(const uint8_t* encoding) | 18 Sleb128Decoder(const uint8_t* buffer, size_t count) |
| 43 : encoding_(encoding), cursor_(0) { } | 19 : current_(buffer), end_(buffer + count) { } |
| 44 | 20 |
| 45 ssize_t Dequeue() { | 21 size_t pop_front() { |
| 46 ssize_t value = 0; | 22 size_t value = 0; |
| 47 static const size_t size = CHAR_BIT * sizeof(value); | 23 static const size_t size = CHAR_BIT * sizeof(value); |
| 48 | 24 |
| 49 size_t shift = 0; | 25 size_t shift = 0; |
| 50 uint8_t byte; | 26 uint8_t byte; |
| 51 | 27 |
| 52 do { | 28 do { |
| 53 byte = encoding_[cursor_++]; | 29 assert(current_ < end_); |
| 54 value |= (static_cast<ssize_t>(byte & 127) << shift); | 30 |
| 31 byte = *current_++; |
| 32 value |= (static_cast<size_t>(byte & 127) << shift); |
| 55 shift += 7; | 33 shift += 7; |
| 56 } while (byte & 128); | 34 } while (byte & 128); |
| 57 | 35 |
| 58 if (shift < size && (byte & 64)) | 36 if (shift < size && (byte & 64)) { |
| 59 value |= -(static_cast<ssize_t>(1) << shift); | 37 value |= -(static_cast<size_t>(1) << shift); |
| 38 } |
| 60 | 39 |
| 61 return value; | 40 return value; |
| 62 } | 41 } |
| 63 | 42 |
| 64 private: | 43 private: |
| 65 const uint8_t* encoding_; | 44 const uint8_t* current_; |
| 66 size_t cursor_; | 45 const uint8_t* const end_; |
| 67 }; | 46 }; |
| 68 | 47 |
| 69 } // namespace crazy | 48 } // namespace crazy |
| 70 | 49 |
| 71 #endif // CRAZY_LINKER_LEB128_H | 50 #endif // CRAZY_LINKER_LEB128_H |
| OLD | NEW |