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.
10 #include "elf_traits.h"
12 namespace relocation_packer
{
14 // Empty constructor and destructor to silence chromium-style.
15 Leb128Encoder::Leb128Encoder() { }
16 Leb128Encoder::~Leb128Encoder() { }
18 // Add a single value to the encoding. Values are encoded with variable
19 // length. The least significant 7 bits of each byte hold 7 bits of data,
20 // and the most significant bit is set on each byte except the last.
21 void Leb128Encoder::Enqueue(ELF::Xword value
) {
23 const uint8_t byte
= value
& 127;
25 encoding_
.push_back((value
? 128 : 0) | byte
);
29 // Add a vector of values to the encoding.
30 void Leb128Encoder::EnqueueAll(const std::vector
<ELF::Xword
>& values
) {
31 for (size_t i
= 0; i
< values
.size(); ++i
)
35 // Create a new decoder for the given encoded stream.
36 Leb128Decoder::Leb128Decoder(const std::vector
<uint8_t>& encoding
) {
41 // Empty destructor to silence chromium-style.
42 Leb128Decoder::~Leb128Decoder() { }
44 // Decode and retrieve a single value from the encoding. Read forwards until
45 // a byte without its most significant bit is found, then read the 7 bit
46 // fields of the bytes spanned to re-form the value.
47 ELF::Xword
Leb128Decoder::Dequeue() {
53 // Loop until we reach a byte with its high order bit clear.
55 byte
= encoding_
[cursor_
++];
56 value
|= static_cast<ELF::Xword
>(byte
& 127) << shift
;
63 // Decode and retrieve all remaining values from the encoding.
64 void Leb128Decoder::DequeueAll(std::vector
<ELF::Xword
>* values
) {
65 while (cursor_
< encoding_
.size())
66 values
->push_back(Dequeue());
69 } // namespace relocation_packer