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.
5 // SLEB128 encoder and decoder for packed relative relocations.
7 // Delta encoded relative relocations consist of a large number
8 // of pairs signed integer values, many with small values. Encoding these
9 // as signed LEB128 saves space.
11 // For more on LEB128 see http://en.wikipedia.org/wiki/LEB128.
13 #ifndef TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
14 #define TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
20 #include "elf_traits.h"
22 namespace relocation_packer
{
24 // Encode packed words as a signed LEB128 byte stream.
25 class Sleb128Encoder
{
27 // Explicit (but empty) constructor and destructor, for chromium-style.
31 // Add a value to the encoding stream.
32 // |value| is the signed int to add.
33 void Enqueue(ELF::Sxword value
);
35 // Add a vector of values to the encoding stream.
36 // |values| is the vector of signed ints to add.
37 void EnqueueAll(const std::vector
<ELF::Sxword
>& values
);
39 // Retrieve the encoded representation of the values.
40 // |encoding| is the returned vector of encoded data.
41 void GetEncoding(std::vector
<uint8_t>* encoding
) { *encoding
= encoding_
; }
44 // Growable vector holding the encoded LEB128 stream.
45 std::vector
<uint8_t> encoding_
;
48 // Decode a LEB128 byte stream to produce packed words.
49 class Sleb128Decoder
{
51 // Create a new decoder for the given encoded stream.
52 // |encoding| is the vector of encoded data.
53 explicit Sleb128Decoder(const std::vector
<uint8_t>& encoding
);
55 // Explicit (but empty) destructor, for chromium-style.
58 // Retrieve the next value from the encoded stream.
59 ELF::Sxword
Dequeue();
61 // Retrieve all remaining values from the encoded stream.
62 // |values| is the vector of decoded data.
63 void DequeueAll(std::vector
<ELF::Sxword
>* values
);
66 // Encoded LEB128 stream.
67 std::vector
<uint8_t> encoding_
;
69 // Cursor indicating the current stream retrieval point.
73 } // namespace relocation_packer
75 #endif // TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_