Index: tools/relocation_packer/src/leb128.cc |
diff --git a/tools/relocation_packer/src/leb128.cc b/tools/relocation_packer/src/leb128.cc |
index a322fb1c28b3c5902000366568f244a5487404dc..b48739c324d1683c7d047bc79dd26d51387c9205 100644 |
--- a/tools/relocation_packer/src/leb128.cc |
+++ b/tools/relocation_packer/src/leb128.cc |
@@ -2,13 +2,13 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-// TODO(simonb): Extend for 64-bit target libraries. |
- |
#include "leb128.h" |
#include <stdint.h> |
#include <vector> |
+#include "elf_traits.h" |
+ |
namespace relocation_packer { |
// Empty constructor and destructor to silence chromium-style. |
@@ -18,16 +18,16 @@ Leb128Encoder::~Leb128Encoder() { } |
// Add a single value to the encoding. Values are encoded with variable |
// length. The least significant 7 bits of each byte hold 7 bits of data, |
// and the most significant bit is set on each byte except the last. |
-void Leb128Encoder::Enqueue(uint32_t value) { |
- while (value > 127) { |
- encoding_.push_back((1 << 7) | (value & 127)); |
+void Leb128Encoder::Enqueue(ELF::Xword value) { |
+ do { |
+ const uint8_t byte = value & 127; |
value >>= 7; |
- } |
- encoding_.push_back(value); |
+ encoding_.push_back((value ? 128 : 0) | byte); |
+ } while (value); |
} |
// Add a vector of values to the encoding. |
-void Leb128Encoder::EnqueueAll(const std::vector<uint32_t>& values) { |
+void Leb128Encoder::EnqueueAll(const std::vector<ELF::Xword>& values) { |
for (size_t i = 0; i < values.size(); ++i) |
Enqueue(values[i]); |
} |
@@ -44,23 +44,24 @@ Leb128Decoder::~Leb128Decoder() { } |
// Decode and retrieve a single value from the encoding. Read forwards until |
// a byte without its most significant bit is found, then read the 7 bit |
// fields of the bytes spanned to re-form the value. |
-uint32_t Leb128Decoder::Dequeue() { |
- size_t extent = cursor_; |
- while (encoding_[extent] >> 7) |
- extent++; |
+ELF::Xword Leb128Decoder::Dequeue() { |
+ ELF::Xword value = 0; |
+ |
+ size_t shift = 0; |
+ uint8_t byte; |
- uint32_t value = 0; |
- for (size_t i = extent; i > cursor_; --i) { |
- value = (value << 7) | (encoding_[i] & 127); |
- } |
- value = (value << 7) | (encoding_[cursor_] & 127); |
+ // Loop until we reach a byte with its high order bit clear. |
+ do { |
+ byte = encoding_[cursor_++]; |
+ value |= static_cast<ELF::Xword>(byte & 127) << shift; |
+ shift += 7; |
+ } while (byte & 128); |
- cursor_ = extent + 1; |
return value; |
} |
// Decode and retrieve all remaining values from the encoding. |
-void Leb128Decoder::DequeueAll(std::vector<uint32_t>* values) { |
+void Leb128Decoder::DequeueAll(std::vector<ELF::Xword>* values) { |
while (cursor_ < encoding_.size()) |
values->push_back(Dequeue()); |
} |