blob: fa0a24610335fb206e003411e6964c1dfad74a97 [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//
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.
10//
11// For more on LEB128 see http://en.wikipedia.org/wiki/LEB128.
12
13#ifndef TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
14#define TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_
15
16#include <stdint.h>
17#include <unistd.h>
18#include <vector>
19
20#include "elf_traits.h"
21
22namespace relocation_packer {
23
24// Encode packed words as a signed LEB128 byte stream.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080025template<typename int_t>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080026class Sleb128Encoder {
27 public:
28 // Explicit (but empty) constructor and destructor, for chromium-style.
29 Sleb128Encoder();
30 ~Sleb128Encoder();
31
32 // Add a value to the encoding stream.
33 // |value| is the signed int to add.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080034 void Enqueue(int_t value);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080035
36 // Add a vector of values to the encoding stream.
37 // |values| is the vector of signed ints to add.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080038 void EnqueueAll(const std::vector<int_t>& values);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080039
40 // Retrieve the encoded representation of the values.
41 // |encoding| is the returned vector of encoded data.
42 void GetEncoding(std::vector<uint8_t>* encoding) { *encoding = encoding_; }
43
44 private:
45 // Growable vector holding the encoded LEB128 stream.
46 std::vector<uint8_t> encoding_;
47};
48
49// Decode a LEB128 byte stream to produce packed words.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080050template <typename int_t>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080051class Sleb128Decoder {
52 public:
53 // Create a new decoder for the given encoded stream.
54 // |encoding| is the vector of encoded data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080055 explicit Sleb128Decoder(const std::vector<uint8_t>& encoding, size_t start_with);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080056
57 // Explicit (but empty) destructor, for chromium-style.
58 ~Sleb128Decoder();
59
60 // Retrieve the next value from the encoded stream.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080061 int_t Dequeue();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080062
63 // Retrieve all remaining values from the encoded stream.
64 // |values| is the vector of decoded data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080065 void DequeueAll(std::vector<int_t>* values);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080066
67 private:
68 // Encoded LEB128 stream.
69 std::vector<uint8_t> encoding_;
70
71 // Cursor indicating the current stream retrieval point.
72 size_t cursor_;
73};
74
75} // namespace relocation_packer
76
77#endif // TOOLS_RELOCATION_PACKER_SRC_SLEB128_H_