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