blob: 101c5577863bf94e8b8c745a60abff5ed5272a67 [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#include "leb128.h"
6
7#include <stdint.h>
8#include <vector>
9
10#include "elf_traits.h"
11
12namespace relocation_packer {
13
14// Empty constructor and destructor to silence chromium-style.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080015template <typename uint_t>
16Leb128Encoder<uint_t>::Leb128Encoder() { }
17
18template <typename uint_t>
19Leb128Encoder<uint_t>::~Leb128Encoder() { }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080020
21// Add a single value to the encoding. Values are encoded with variable
22// length. The least significant 7 bits of each byte hold 7 bits of data,
23// and the most significant bit is set on each byte except the last.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080024template <typename uint_t>
25void Leb128Encoder<uint_t>::Enqueue(uint_t value) {
26 uint_t uvalue = static_cast<uint_t>(value);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080027 do {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080028 const uint8_t byte = uvalue & 127;
29 uvalue >>= 7;
30 encoding_.push_back((uvalue ? 128 : 0) | byte);
31 } while (uvalue);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080032}
33
34// Add a vector of values to the encoding.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080035template <typename uint_t>
36void Leb128Encoder<uint_t>::EnqueueAll(const std::vector<uint_t>& values) {
37 for (size_t i = 0; i < values.size(); ++i) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080038 Enqueue(values[i]);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080039 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080040}
41
42// Create a new decoder for the given encoded stream.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080043template <typename uint_t>
44Leb128Decoder<uint_t>::Leb128Decoder(const std::vector<uint8_t>& encoding, size_t start_with) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080045 encoding_ = encoding;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080046 cursor_ = start_with;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080047}
48
49// Empty destructor to silence chromium-style.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080050template <typename uint_t>
51Leb128Decoder<uint_t>::~Leb128Decoder() { }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080052
53// Decode and retrieve a single value from the encoding. Read forwards until
54// a byte without its most significant bit is found, then read the 7 bit
55// fields of the bytes spanned to re-form the value.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080056template <typename uint_t>
57uint_t Leb128Decoder<uint_t>::Dequeue() {
58 uint_t value = 0;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080059
60 size_t shift = 0;
61 uint8_t byte;
62
63 // Loop until we reach a byte with its high order bit clear.
64 do {
65 byte = encoding_[cursor_++];
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080066 value |= static_cast<uint_t>(byte & 127) << shift;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080067 shift += 7;
68 } while (byte & 128);
69
70 return value;
71}
72
73// Decode and retrieve all remaining values from the encoding.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080074template <typename uint_t>
75void Leb128Decoder<uint_t>::DequeueAll(std::vector<uint_t>* values) {
76 while (cursor_ < encoding_.size()) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080077 values->push_back(Dequeue());
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080078 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080079}
80
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080081template class Leb128Encoder<uint32_t>;
82template class Leb128Encoder<uint64_t>;
83
84template class Leb128Decoder<uint32_t>;
85template class Leb128Decoder<uint64_t>;
86
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080087} // namespace relocation_packer