summaryrefslogtreecommitdiffstats
path: root/tools/relocation_packer/src/leb128.h
blob: 2c5b5d079c0b00da598e5580d9eea77b60d235e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// LEB128 encoder and decoder for packed relative relocations.
//
// Run-length encoded relative relocations consist of a large number
// of pairs of relatively small positive integer values.  Encoding these as
// LEB128 saves space.
//
// For more on LEB128 see http://en.wikipedia.org/wiki/LEB128.

#ifndef TOOLS_RELOCATION_PACKER_SRC_LEB128_H_
#define TOOLS_RELOCATION_PACKER_SRC_LEB128_H_

#include <stdint.h>
#include <vector>

#include "elf_traits.h"

namespace relocation_packer {

// Encode packed words as a LEB128 byte stream.
template <typename uint_t>
class Leb128Encoder {
 public:
  // Explicit (but empty) constructor and destructor, for chromium-style.
  Leb128Encoder();
  ~Leb128Encoder();

  // Add a value to the encoding stream.
  // |value| is the unsigned int to add.
  void Enqueue(uint_t value);

  // Add a vector of values to the encoding stream.
  // |values| is the vector of unsigned ints to add.
  void EnqueueAll(const std::vector<uint_t>& values);

  // Retrieve the encoded representation of the values.
  // |encoding| is the returned vector of encoded data.
  void GetEncoding(std::vector<uint8_t>* encoding) { *encoding = encoding_; }

 private:
  // Growable vector holding the encoded LEB128 stream.
  std::vector<uint8_t> encoding_;
};

// Decode a LEB128 byte stream to produce packed words.
template <typename uint_t>
class Leb128Decoder {
 public:
  // Create a new decoder for the given encoded stream.
  // |encoding| is the vector of encoded data.
  explicit Leb128Decoder(const std::vector<uint8_t>& encoding, size_t start_with);

  // Explicit (but empty) destructor, for chromium-style.
  ~Leb128Decoder();

  // Retrieve the next value from the encoded stream.
  uint_t Dequeue();

  // Retrieve all remaining values from the encoded stream.
  // |values| is the vector of decoded data.
  void DequeueAll(std::vector<uint_t>* values);

 private:
  // Encoded LEB128 stream.
  std::vector<uint8_t> encoding_;

  // Cursor indicating the current stream retrieval point.
  size_t cursor_;
};

}  // namespace relocation_packer

#endif  // TOOLS_RELOCATION_PACKER_SRC_LEB128_H_