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 // TODO(simonb): Extend for 64-bit target libraries. | |
6 | |
7 #include "leb128.h" | 5 #include "leb128.h" |
8 | 6 |
9 #include <stdint.h> | 7 #include <stdint.h> |
10 #include <vector> | 8 #include <vector> |
11 | 9 |
| 10 #include "elf_traits.h" |
| 11 |
12 namespace relocation_packer { | 12 namespace relocation_packer { |
13 | 13 |
14 // Empty constructor and destructor to silence chromium-style. | 14 // Empty constructor and destructor to silence chromium-style. |
15 Leb128Encoder::Leb128Encoder() { } | 15 Leb128Encoder::Leb128Encoder() { } |
16 Leb128Encoder::~Leb128Encoder() { } | 16 Leb128Encoder::~Leb128Encoder() { } |
17 | 17 |
18 // Add a single value to the encoding. Values are encoded with variable | 18 // Add a single value to the encoding. Values are encoded with variable |
19 // length. The least significant 7 bits of each byte hold 7 bits of data, | 19 // length. The least significant 7 bits of each byte hold 7 bits of data, |
20 // and the most significant bit is set on each byte except the last. | 20 // and the most significant bit is set on each byte except the last. |
21 void Leb128Encoder::Enqueue(uint32_t value) { | 21 void Leb128Encoder::Enqueue(ELF::Xword value) { |
22 while (value > 127) { | 22 do { |
23 encoding_.push_back((1 << 7) | (value & 127)); | 23 const uint8_t byte = value & 127; |
24 value >>= 7; | 24 value >>= 7; |
25 } | 25 encoding_.push_back((value ? 128 : 0) | byte); |
26 encoding_.push_back(value); | 26 } while (value); |
27 } | 27 } |
28 | 28 |
29 // Add a vector of values to the encoding. | 29 // Add a vector of values to the encoding. |
30 void Leb128Encoder::EnqueueAll(const std::vector<uint32_t>& values) { | 30 void Leb128Encoder::EnqueueAll(const std::vector<ELF::Xword>& values) { |
31 for (size_t i = 0; i < values.size(); ++i) | 31 for (size_t i = 0; i < values.size(); ++i) |
32 Enqueue(values[i]); | 32 Enqueue(values[i]); |
33 } | 33 } |
34 | 34 |
35 // Create a new decoder for the given encoded stream. | 35 // Create a new decoder for the given encoded stream. |
36 Leb128Decoder::Leb128Decoder(const std::vector<uint8_t>& encoding) { | 36 Leb128Decoder::Leb128Decoder(const std::vector<uint8_t>& encoding) { |
37 encoding_ = encoding; | 37 encoding_ = encoding; |
38 cursor_ = 0; | 38 cursor_ = 0; |
39 } | 39 } |
40 | 40 |
41 // Empty destructor to silence chromium-style. | 41 // Empty destructor to silence chromium-style. |
42 Leb128Decoder::~Leb128Decoder() { } | 42 Leb128Decoder::~Leb128Decoder() { } |
43 | 43 |
44 // Decode and retrieve a single value from the encoding. Read forwards until | 44 // Decode and retrieve a single value from the encoding. Read forwards until |
45 // a byte without its most significant bit is found, then read the 7 bit | 45 // a byte without its most significant bit is found, then read the 7 bit |
46 // fields of the bytes spanned to re-form the value. | 46 // fields of the bytes spanned to re-form the value. |
47 uint32_t Leb128Decoder::Dequeue() { | 47 ELF::Xword Leb128Decoder::Dequeue() { |
48 size_t extent = cursor_; | 48 ELF::Xword value = 0; |
49 while (encoding_[extent] >> 7) | |
50 extent++; | |
51 | 49 |
52 uint32_t value = 0; | 50 size_t shift = 0; |
53 for (size_t i = extent; i > cursor_; --i) { | 51 uint8_t byte; |
54 value = (value << 7) | (encoding_[i] & 127); | |
55 } | |
56 value = (value << 7) | (encoding_[cursor_] & 127); | |
57 | 52 |
58 cursor_ = extent + 1; | 53 // Loop until we reach a byte with its high order bit clear. |
| 54 do { |
| 55 byte = encoding_[cursor_++]; |
| 56 value |= static_cast<ELF::Xword>(byte & 127) << shift; |
| 57 shift += 7; |
| 58 } while (byte & 128); |
| 59 |
59 return value; | 60 return value; |
60 } | 61 } |
61 | 62 |
62 // Decode and retrieve all remaining values from the encoding. | 63 // Decode and retrieve all remaining values from the encoding. |
63 void Leb128Decoder::DequeueAll(std::vector<uint32_t>* values) { | 64 void Leb128Decoder::DequeueAll(std::vector<ELF::Xword>* values) { |
64 while (cursor_ < encoding_.size()) | 65 while (cursor_ < encoding_.size()) |
65 values->push_back(Dequeue()); | 66 values->push_back(Dequeue()); |
66 } | 67 } |
67 | 68 |
68 } // namespace relocation_packer | 69 } // namespace relocation_packer |
OLD | NEW |