Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // LEB128 encoder and decoder for packed relative relocations. |
| 6 | // |
Simon Baldwin | 569a752 | 2015-03-26 17:13:19 +0000 | [diff] [blame] | 7 | // Packed relocations consist of a large number of relatively small |
| 8 | // integer values. Encoding these as LEB128 saves space. |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 9 | // |
| 10 | // For more on LEB128 see http://en.wikipedia.org/wiki/LEB128. |
| 11 | |
| 12 | #ifndef TOOLS_RELOCATION_PACKER_SRC_LEB128_H_ |
| 13 | #define TOOLS_RELOCATION_PACKER_SRC_LEB128_H_ |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "elf_traits.h" |
| 19 | |
| 20 | namespace relocation_packer { |
| 21 | |
| 22 | // Encode packed words as a LEB128 byte stream. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 23 | template <typename uint_t> |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 24 | class Leb128Encoder { |
| 25 | public: |
| 26 | // Explicit (but empty) constructor and destructor, for chromium-style. |
| 27 | Leb128Encoder(); |
| 28 | ~Leb128Encoder(); |
| 29 | |
| 30 | // Add a value to the encoding stream. |
| 31 | // |value| is the unsigned int to add. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 32 | void Enqueue(uint_t value); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 33 | |
| 34 | // Add a vector of values to the encoding stream. |
| 35 | // |values| is the vector of unsigned ints to add. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 36 | void EnqueueAll(const std::vector<uint_t>& values); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 37 | |
| 38 | // Retrieve the encoded representation of the values. |
| 39 | // |encoding| is the returned vector of encoded data. |
| 40 | void GetEncoding(std::vector<uint8_t>* encoding) { *encoding = encoding_; } |
| 41 | |
| 42 | private: |
| 43 | // Growable vector holding the encoded LEB128 stream. |
| 44 | std::vector<uint8_t> encoding_; |
| 45 | }; |
| 46 | |
| 47 | // Decode a LEB128 byte stream to produce packed words. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 48 | template <typename uint_t> |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 49 | class Leb128Decoder { |
| 50 | public: |
| 51 | // Create a new decoder for the given encoded stream. |
| 52 | // |encoding| is the vector of encoded data. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 53 | explicit Leb128Decoder(const std::vector<uint8_t>& encoding, size_t start_with); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 54 | |
| 55 | // Explicit (but empty) destructor, for chromium-style. |
| 56 | ~Leb128Decoder(); |
| 57 | |
| 58 | // Retrieve the next value from the encoded stream. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 59 | uint_t Dequeue(); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 60 | |
| 61 | // Retrieve all remaining values from the encoded stream. |
| 62 | // |values| is the vector of decoded data. |
Dmitriy Ivanov | f8ff6b1 | 2015-01-27 19:32:56 -0800 | [diff] [blame] | 63 | void DequeueAll(std::vector<uint_t>* values); |
Dmitriy Ivanov | 87a0617 | 2015-02-06 10:56:28 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | // Encoded LEB128 stream. |
| 67 | std::vector<uint8_t> encoding_; |
| 68 | |
| 69 | // Cursor indicating the current stream retrieval point. |
| 70 | size_t cursor_; |
| 71 | }; |
| 72 | |
| 73 | } // namespace relocation_packer |
| 74 | |
| 75 | #endif // TOOLS_RELOCATION_PACKER_SRC_LEB128_H_ |