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 // SLEB128 encoder and decoder for packed relative relocations. | 5 // SLEB128 encoder and decoder for packed relative relocations. |
6 // | 6 // |
7 // Delta encoded relative relocations consist of a large number | 7 // Delta encoded relative relocations consist of a large number |
8 // of pairs signed integer values, many with small values. Encoding these | 8 // of pairs signed integer values, many with small values. Encoding these |
9 // as signed LEB128 saves space. | 9 // as signed LEB128 saves space. |
10 // | 10 // |
11 // For more on LEB128 see http://en.wikipedia.org/wiki/LEB128. | 11 // For more on LEB128 see http://en.wikipedia.org/wiki/LEB128. |
12 | 12 |
13 #ifndef TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ | 13 #ifndef TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ |
14 #define TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ | 14 #define TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ |
15 | 15 |
16 #include <stdint.h> | 16 #include <stdint.h> |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 #include <vector> | 18 #include <vector> |
19 | 19 |
20 #include "elf_traits.h" | 20 #include "elf_traits.h" |
21 | 21 |
22 namespace relocation_packer { | 22 namespace relocation_packer { |
23 | 23 |
24 // Encode packed words as a signed LEB128 byte stream. | 24 // Encode packed words as a signed LEB128 byte stream. |
| 25 template<typename int_t> |
25 class Sleb128Encoder { | 26 class Sleb128Encoder { |
26 public: | 27 public: |
27 // Explicit (but empty) constructor and destructor, for chromium-style. | 28 // Explicit (but empty) constructor and destructor, for chromium-style. |
28 Sleb128Encoder(); | 29 Sleb128Encoder(); |
29 ~Sleb128Encoder(); | 30 ~Sleb128Encoder(); |
30 | 31 |
31 // Add a value to the encoding stream. | 32 // Add a value to the encoding stream. |
32 // |value| is the signed int to add. | 33 // |value| is the signed int to add. |
33 void Enqueue(ELF::Sxword value); | 34 void Enqueue(int_t value); |
34 | 35 |
35 // Add a vector of values to the encoding stream. | 36 // Add a vector of values to the encoding stream. |
36 // |values| is the vector of signed ints to add. | 37 // |values| is the vector of signed ints to add. |
37 void EnqueueAll(const std::vector<ELF::Sxword>& values); | 38 void EnqueueAll(const std::vector<int_t>& values); |
38 | 39 |
39 // Retrieve the encoded representation of the values. | 40 // Retrieve the encoded representation of the values. |
40 // |encoding| is the returned vector of encoded data. | 41 // |encoding| is the returned vector of encoded data. |
41 void GetEncoding(std::vector<uint8_t>* encoding) { *encoding = encoding_; } | 42 void GetEncoding(std::vector<uint8_t>* encoding) { *encoding = encoding_; } |
42 | 43 |
43 private: | 44 private: |
44 // Growable vector holding the encoded LEB128 stream. | 45 // Growable vector holding the encoded LEB128 stream. |
45 std::vector<uint8_t> encoding_; | 46 std::vector<uint8_t> encoding_; |
46 }; | 47 }; |
47 | 48 |
48 // Decode a LEB128 byte stream to produce packed words. | 49 // Decode a LEB128 byte stream to produce packed words. |
| 50 template <typename int_t> |
49 class Sleb128Decoder { | 51 class Sleb128Decoder { |
50 public: | 52 public: |
51 // Create a new decoder for the given encoded stream. | 53 // Create a new decoder for the given encoded stream. |
52 // |encoding| is the vector of encoded data. | 54 // |encoding| is the vector of encoded data. |
53 explicit Sleb128Decoder(const std::vector<uint8_t>& encoding); | 55 explicit Sleb128Decoder(const std::vector<uint8_t>& encoding, size_t start_wit
h); |
54 | 56 |
55 // Explicit (but empty) destructor, for chromium-style. | 57 // Explicit (but empty) destructor, for chromium-style. |
56 ~Sleb128Decoder(); | 58 ~Sleb128Decoder(); |
57 | 59 |
58 // Retrieve the next value from the encoded stream. | 60 // Retrieve the next value from the encoded stream. |
59 ELF::Sxword Dequeue(); | 61 int_t Dequeue(); |
60 | 62 |
61 // Retrieve all remaining values from the encoded stream. | 63 // Retrieve all remaining values from the encoded stream. |
62 // |values| is the vector of decoded data. | 64 // |values| is the vector of decoded data. |
63 void DequeueAll(std::vector<ELF::Sxword>* values); | 65 void DequeueAll(std::vector<int_t>* values); |
64 | 66 |
65 private: | 67 private: |
66 // Encoded LEB128 stream. | 68 // Encoded LEB128 stream. |
67 std::vector<uint8_t> encoding_; | 69 std::vector<uint8_t> encoding_; |
68 | 70 |
69 // Cursor indicating the current stream retrieval point. | 71 // Cursor indicating the current stream retrieval point. |
70 size_t cursor_; | 72 size_t cursor_; |
71 }; | 73 }; |
72 | 74 |
73 } // namespace relocation_packer | 75 } // namespace relocation_packer |
74 | 76 |
75 #endif // TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ | 77 #endif // TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_ |
OLD | NEW |