blob: 3a63f6679956994347207b5d6787a6d2c4ed84c0 [file] [log] [blame]
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08001// 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// SLEB128 encoder and decoder for packed relative relocations.
6//
Simon Baldwin569a7522015-03-26 17:13:19 +00007// Packed relocations consist of a large number of relatively small
8// integer values. Encoding these as LEB128 saves space.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08009//
10// For more on LEB128 see http://en.wikipedia.org/wiki/LEB128.
11
12#ifndef TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
13#define TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
14
15#include <stdint.h>
16#include <unistd.h>
17#include <vector>
18
19#include "elf_traits.h"
20
21namespace relocation_packer {
22
23// Encode packed words as a signed LEB128 byte stream.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080024template<typename int_t>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080025class Sleb128Encoder {
26 public:
27 // Explicit (but empty) constructor and destructor, for chromium-style.
28 Sleb128Encoder();
29 ~Sleb128Encoder();
30
31 // Add a value to the encoding stream.
32 // |value| is the signed int to add.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080033 void Enqueue(int_t value);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080034
35 // Add a vector of values to the encoding stream.
36 // |values| is the vector of signed ints to add.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080037 void EnqueueAll(const std::vector<int_t>& values);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080038
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_; }
42
43 private:
44 // Growable vector holding the encoded LEB128 stream.
45 std::vector<uint8_t> encoding_;
46};
47
48// Decode a LEB128 byte stream to produce packed words.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080049template <typename int_t>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080050class Sleb128Decoder {
51 public:
52 // Create a new decoder for the given encoded stream.
53 // |encoding| is the vector of encoded data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080054 explicit Sleb128Decoder(const std::vector<uint8_t>& encoding, size_t start_with);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080055
56 // Explicit (but empty) destructor, for chromium-style.
57 ~Sleb128Decoder();
58
59 // Retrieve the next value from the encoded stream.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080060 int_t Dequeue();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080061
62 // Retrieve all remaining values from the encoded stream.
63 // |values| is the vector of decoded data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080064 void DequeueAll(std::vector<int_t>* values);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080065
66 private:
67 // Encoded LEB128 stream.
68 std::vector<uint8_t> encoding_;
69
70 // Cursor indicating the current stream retrieval point.
71 size_t cursor_;
72};
73
74} // namespace relocation_packer
75
76#endif // TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_