| 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 class Leb128Decoder { |
| 16 public: | 17 public: |
| 17 explicit Leb128Decoder(const uint8_t* encoding) | 18 Leb128Decoder(const uint8_t* buffer, size_t count) |
| 18 : encoding_(encoding), cursor_(0) { } | 19 : current_(buffer), end_(buffer + count) { } |
| 20 virtual size_t pop_front() = 0; |
| 21 virtual ~Leb128Decoder() { } |
| 19 | 22 |
| 20 size_t Dequeue() { | 23 protected: |
| 24 const uint8_t* current_; |
| 25 const uint8_t* const end_; |
| 26 }; |
| 27 |
| 28 class Uleb128Decoder : public Leb128Decoder { |
| 29 public: |
| 30 Uleb128Decoder(const uint8_t* buffer, size_t count) |
| 31 : Leb128Decoder(buffer, count) { } |
| 32 |
| 33 size_t pop_front() { |
| 21 size_t value = 0; | 34 size_t value = 0; |
| 22 | 35 |
| 23 size_t shift = 0; | 36 size_t shift = 0; |
| 24 uint8_t byte; | 37 uint8_t byte; |
| 25 | 38 |
| 26 do { | 39 do { |
| 27 byte = encoding_[cursor_++]; | 40 assert(current_ < end_); |
| 41 |
| 42 byte = *current_++; |
| 28 value |= static_cast<size_t>(byte & 127) << shift; | 43 value |= static_cast<size_t>(byte & 127) << shift; |
| 29 shift += 7; | 44 shift += 7; |
| 30 } while (byte & 128); | 45 } while (byte & 128); |
| 31 | 46 |
| 32 return value; | 47 return value; |
| 33 } | 48 } |
| 34 | |
| 35 private: | |
| 36 const uint8_t* encoding_; | |
| 37 size_t cursor_; | |
| 38 }; | 49 }; |
| 39 | 50 |
| 40 class Sleb128Decoder { | 51 class Sleb128Decoder : public Leb128Decoder { |
| 41 public: | 52 public: |
| 42 explicit Sleb128Decoder(const uint8_t* encoding) | 53 Sleb128Decoder(const uint8_t* buffer, size_t count) |
| 43 : encoding_(encoding), cursor_(0) { } | 54 : Leb128Decoder(buffer, count) { } |
| 44 | 55 |
| 45 ssize_t Dequeue() { | 56 size_t pop_front() { |
| 46 ssize_t value = 0; | 57 size_t value = 0; |
| 47 static const size_t size = CHAR_BIT * sizeof(value); | 58 static const size_t size = CHAR_BIT * sizeof(value); |
| 48 | 59 |
| 49 size_t shift = 0; | 60 size_t shift = 0; |
| 50 uint8_t byte; | 61 uint8_t byte; |
| 51 | 62 |
| 52 do { | 63 do { |
| 53 byte = encoding_[cursor_++]; | 64 assert(current_ < end_); |
| 54 value |= (static_cast<ssize_t>(byte & 127) << shift); | 65 |
| 66 byte = *current_++; |
| 67 value |= (static_cast<size_t>(byte & 127) << shift); |
| 55 shift += 7; | 68 shift += 7; |
| 56 } while (byte & 128); | 69 } while (byte & 128); |
| 57 | 70 |
| 58 if (shift < size && (byte & 64)) | 71 if (shift < size && (byte & 64)) { |
| 59 value |= -(static_cast<ssize_t>(1) << shift); | 72 value |= -(static_cast<size_t>(1) << shift); |
| 73 } |
| 60 | 74 |
| 61 return value; | 75 return value; |
| 62 } | 76 } |
| 63 | |
| 64 private: | |
| 65 const uint8_t* encoding_; | |
| 66 size_t cursor_; | |
| 67 }; | 77 }; |
| 68 | 78 |
| 69 } // namespace crazy | 79 } // namespace crazy |
| 70 | 80 |
| 71 #endif // CRAZY_LINKER_LEB128_H | 81 #endif // CRAZY_LINKER_LEB128_H |
| OLD | NEW |