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 "leb128.h" | 5 #include "leb128.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "elf_traits.h" | 10 #include "elf_traits.h" |
11 | 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 template <typename uint_t> |
16 Leb128Encoder::~Leb128Encoder() { } | 16 Leb128Encoder<uint_t>::Leb128Encoder() { } |
| 17 |
| 18 template <typename uint_t> |
| 19 Leb128Encoder<uint_t>::~Leb128Encoder() { } |
17 | 20 |
18 // Add a single value to the encoding. Values are encoded with variable | 21 // 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, | 22 // 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. | 23 // and the most significant bit is set on each byte except the last. |
21 void Leb128Encoder::Enqueue(ELF::Xword value) { | 24 template <typename uint_t> |
| 25 void Leb128Encoder<uint_t>::Enqueue(uint_t value) { |
| 26 uint_t uvalue = static_cast<uint_t>(value); |
22 do { | 27 do { |
23 const uint8_t byte = value & 127; | 28 const uint8_t byte = uvalue & 127; |
24 value >>= 7; | 29 uvalue >>= 7; |
25 encoding_.push_back((value ? 128 : 0) | byte); | 30 encoding_.push_back((uvalue ? 128 : 0) | byte); |
26 } while (value); | 31 } while (uvalue); |
27 } | 32 } |
28 | 33 |
29 // Add a vector of values to the encoding. | 34 // Add a vector of values to the encoding. |
30 void Leb128Encoder::EnqueueAll(const std::vector<ELF::Xword>& values) { | 35 template <typename uint_t> |
31 for (size_t i = 0; i < values.size(); ++i) | 36 void Leb128Encoder<uint_t>::EnqueueAll(const std::vector<uint_t>& values) { |
| 37 for (size_t i = 0; i < values.size(); ++i) { |
32 Enqueue(values[i]); | 38 Enqueue(values[i]); |
| 39 } |
33 } | 40 } |
34 | 41 |
35 // Create a new decoder for the given encoded stream. | 42 // Create a new decoder for the given encoded stream. |
36 Leb128Decoder::Leb128Decoder(const std::vector<uint8_t>& encoding) { | 43 template <typename uint_t> |
| 44 Leb128Decoder<uint_t>::Leb128Decoder(const std::vector<uint8_t>& encoding, size_
t start_with) { |
37 encoding_ = encoding; | 45 encoding_ = encoding; |
38 cursor_ = 0; | 46 cursor_ = start_with; |
39 } | 47 } |
40 | 48 |
41 // Empty destructor to silence chromium-style. | 49 // Empty destructor to silence chromium-style. |
42 Leb128Decoder::~Leb128Decoder() { } | 50 template <typename uint_t> |
| 51 Leb128Decoder<uint_t>::~Leb128Decoder() { } |
43 | 52 |
44 // Decode and retrieve a single value from the encoding. Read forwards until | 53 // 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 | 54 // 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. | 55 // fields of the bytes spanned to re-form the value. |
47 ELF::Xword Leb128Decoder::Dequeue() { | 56 template <typename uint_t> |
48 ELF::Xword value = 0; | 57 uint_t Leb128Decoder<uint_t>::Dequeue() { |
| 58 uint_t value = 0; |
49 | 59 |
50 size_t shift = 0; | 60 size_t shift = 0; |
51 uint8_t byte; | 61 uint8_t byte; |
52 | 62 |
53 // Loop until we reach a byte with its high order bit clear. | 63 // Loop until we reach a byte with its high order bit clear. |
54 do { | 64 do { |
55 byte = encoding_[cursor_++]; | 65 byte = encoding_[cursor_++]; |
56 value |= static_cast<ELF::Xword>(byte & 127) << shift; | 66 value |= static_cast<uint_t>(byte & 127) << shift; |
57 shift += 7; | 67 shift += 7; |
58 } while (byte & 128); | 68 } while (byte & 128); |
59 | 69 |
60 return value; | 70 return value; |
61 } | 71 } |
62 | 72 |
63 // Decode and retrieve all remaining values from the encoding. | 73 // Decode and retrieve all remaining values from the encoding. |
64 void Leb128Decoder::DequeueAll(std::vector<ELF::Xword>* values) { | 74 template <typename uint_t> |
65 while (cursor_ < encoding_.size()) | 75 void Leb128Decoder<uint_t>::DequeueAll(std::vector<uint_t>* values) { |
| 76 while (cursor_ < encoding_.size()) { |
66 values->push_back(Dequeue()); | 77 values->push_back(Dequeue()); |
| 78 } |
67 } | 79 } |
68 | 80 |
| 81 template class Leb128Encoder<uint32_t>; |
| 82 template class Leb128Encoder<uint64_t>; |
| 83 |
| 84 template class Leb128Decoder<uint32_t>; |
| 85 template class Leb128Decoder<uint64_t>; |
| 86 |
69 } // namespace relocation_packer | 87 } // namespace relocation_packer |
OLD | NEW |