| 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 #include "sleb128.h" | 5 #include "sleb128.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "elf_traits.h" | 11 #include "elf_traits.h" |
| 12 | 12 |
| 13 namespace { |
| 14 |
| 15 template <typename T> |
| 16 class uint_traits {}; |
| 17 |
| 18 template <> |
| 19 class uint_traits<uint64_t> { |
| 20 public: |
| 21 typedef int64_t int_t; |
| 22 }; |
| 23 |
| 24 template <> |
| 25 class uint_traits<uint32_t> { |
| 26 public: |
| 27 typedef int32_t int_t; |
| 28 }; |
| 29 |
| 30 } |
| 31 |
| 13 namespace relocation_packer { | 32 namespace relocation_packer { |
| 14 | 33 |
| 15 // Empty constructor and destructor to silence chromium-style. | 34 // Empty constructor and destructor to silence chromium-style. |
| 16 Sleb128Encoder::Sleb128Encoder() { } | 35 template <typename uint_t> |
| 17 Sleb128Encoder::~Sleb128Encoder() { } | 36 Sleb128Encoder<uint_t>::Sleb128Encoder() { } |
| 37 |
| 38 template <typename uint_t> |
| 39 Sleb128Encoder<uint_t>::~Sleb128Encoder() { } |
| 18 | 40 |
| 19 // Add a single value to the encoding. Values are encoded with variable | 41 // Add a single value to the encoding. Values are encoded with variable |
| 20 // length. The least significant 7 bits of each byte hold 7 bits of data, | 42 // length. The least significant 7 bits of each byte hold 7 bits of data, |
| 21 // and the most significant bit is set on each byte except the last. The | 43 // and the most significant bit is set on each byte except the last. The |
| 22 // value is sign extended up to a multiple of 7 bits (ensuring that the | 44 // value is sign extended up to a multiple of 7 bits (ensuring that the |
| 23 // most significant bit is zero for a positive number and one for a | 45 // most significant bit is zero for a positive number and one for a |
| 24 // negative number). | 46 // negative number). |
| 25 void Sleb128Encoder::Enqueue(ELF::Sxword value) { | 47 template <typename uint_t> |
| 48 void Sleb128Encoder<uint_t>::Enqueue(uint_t value) { |
| 49 typedef typename uint_traits<uint_t>::int_t int_t; |
| 26 static const size_t size = CHAR_BIT * sizeof(value); | 50 static const size_t size = CHAR_BIT * sizeof(value); |
| 27 | 51 |
| 28 bool more = true; | 52 bool more = true; |
| 29 const bool negative = value < 0; | 53 const bool negative = static_cast<int_t>(value) < 0; |
| 30 | 54 |
| 31 while (more) { | 55 while (more) { |
| 32 uint8_t byte = value & 127; | 56 uint8_t byte = value & 127; |
| 33 value >>= 7; | 57 value >>= 7; |
| 34 | 58 |
| 35 // Sign extend if encoding a -ve value. | 59 // Sign extend if encoding a -ve value. |
| 36 if (negative) | 60 if (negative) |
| 37 value |= -(static_cast<ELF::Sxword>(1) << (size - 7)); | 61 value |= -(static_cast<uint_t>(1) << (size - 7)); |
| 38 | 62 |
| 39 // The sign bit of byte is second high order bit. | 63 // The sign bit of byte is second high order bit. |
| 40 const bool sign_bit = byte & 64; | 64 const bool sign_bit = byte & 64; |
| 41 if ((value == 0 && !sign_bit) || (value == -1 && sign_bit)) | 65 if ((value == 0 && !sign_bit) || (value == static_cast<uint_t>(-1) && sign_b
it)) |
| 42 more = false; | 66 more = false; |
| 43 else | 67 else |
| 44 byte |= 128; | 68 byte |= 128; |
| 45 encoding_.push_back(byte); | 69 encoding_.push_back(byte); |
| 46 } | 70 } |
| 47 } | 71 } |
| 48 | 72 |
| 49 // Add a vector of values to the encoding. | 73 // Add a vector of values to the encoding. |
| 50 void Sleb128Encoder::EnqueueAll(const std::vector<ELF::Sxword>& values) { | 74 template <typename uint_t> |
| 51 for (size_t i = 0; i < values.size(); ++i) | 75 void Sleb128Encoder<uint_t>::EnqueueAll(const std::vector<uint_t>& values) { |
| 76 for (size_t i = 0; i < values.size(); ++i) { |
| 52 Enqueue(values[i]); | 77 Enqueue(values[i]); |
| 78 } |
| 53 } | 79 } |
| 54 | 80 |
| 55 // Create a new decoder for the given encoded stream. | 81 // Create a new decoder for the given encoded stream. |
| 56 Sleb128Decoder::Sleb128Decoder(const std::vector<uint8_t>& encoding) { | 82 template <typename uint_t> |
| 83 Sleb128Decoder<uint_t>::Sleb128Decoder(const std::vector<uint8_t>& encoding, siz
e_t start_with) { |
| 57 encoding_ = encoding; | 84 encoding_ = encoding; |
| 58 cursor_ = 0; | 85 cursor_ = start_with; |
| 59 } | 86 } |
| 60 | 87 |
| 61 // Empty destructor to silence chromium-style. | 88 // Empty destructor to silence chromium-style. |
| 62 Sleb128Decoder::~Sleb128Decoder() { } | 89 template <typename uint_t> |
| 90 Sleb128Decoder<uint_t>::~Sleb128Decoder() { } |
| 63 | 91 |
| 64 // Decode and retrieve a single value from the encoding. Consume bytes | 92 // Decode and retrieve a single value from the encoding. Consume bytes |
| 65 // until one without its most significant bit is found, and re-form the | 93 // until one without its most significant bit is found, and re-form the |
| 66 // value from the 7 bit fields of the bytes consumed. | 94 // value from the 7 bit fields of the bytes consumed. |
| 67 ELF::Sxword Sleb128Decoder::Dequeue() { | 95 template <typename uint_t> |
| 68 ELF::Sxword value = 0; | 96 uint_t Sleb128Decoder<uint_t>::Dequeue() { |
| 97 uint_t value = 0; |
| 69 static const size_t size = CHAR_BIT * sizeof(value); | 98 static const size_t size = CHAR_BIT * sizeof(value); |
| 70 | 99 |
| 71 size_t shift = 0; | 100 size_t shift = 0; |
| 72 uint8_t byte; | 101 uint8_t byte; |
| 73 | 102 |
| 74 // Loop until we reach a byte with its high order bit clear. | 103 // Loop until we reach a byte with its high order bit clear. |
| 75 do { | 104 do { |
| 76 byte = encoding_[cursor_++]; | 105 byte = encoding_[cursor_++]; |
| 77 value |= (static_cast<ELF::Sxword>(byte & 127) << shift); | 106 value |= (static_cast<uint_t>(byte & 127) << shift); |
| 78 shift += 7; | 107 shift += 7; |
| 79 } while (byte & 128); | 108 } while (byte & 128); |
| 80 | 109 |
| 81 // The sign bit is second high order bit of the final byte decoded. | 110 // The sign bit is second high order bit of the final byte decoded. |
| 82 // Sign extend if value is -ve and we did not shift all of it. | 111 // Sign extend if value is -ve and we did not shift all of it. |
| 83 if (shift < size && (byte & 64)) | 112 if (shift < size && (byte & 64)) |
| 84 value |= -(static_cast<ELF::Sxword>(1) << shift); | 113 value |= -(static_cast<uint_t>(1) << shift); |
| 85 | 114 |
| 86 return value; | 115 return static_cast<uint_t>(value); |
| 87 } | 116 } |
| 88 | 117 |
| 89 // Decode and retrieve all remaining values from the encoding. | 118 // Decode and retrieve all remaining values from the encoding. |
| 90 void Sleb128Decoder::DequeueAll(std::vector<ELF::Sxword>* values) { | 119 template <typename uint_t> |
| 91 while (cursor_ < encoding_.size()) | 120 void Sleb128Decoder<uint_t>::DequeueAll(std::vector<uint_t>* values) { |
| 121 while (cursor_ < encoding_.size()) { |
| 92 values->push_back(Dequeue()); | 122 values->push_back(Dequeue()); |
| 123 } |
| 93 } | 124 } |
| 94 | 125 |
| 126 template class Sleb128Encoder<uint32_t>; |
| 127 template class Sleb128Encoder<uint64_t>; |
| 128 template class Sleb128Decoder<uint32_t>; |
| 129 template class Sleb128Decoder<uint64_t>; |
| 130 |
| 95 } // namespace relocation_packer | 131 } // namespace relocation_packer |
| OLD | NEW |