1 // varint.cc -- variable length and unaligned integer encoding support.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> by refactoring scattered
5 // contents from other files in gold. Original code written by Ian
6 // Lance Taylor <iant@google.com> and Caleb Howe <cshowe@google.com>.
8 // This file is part of gold.
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 // MA 02110-1301, USA.
29 #include "int_encoding.h"
33 // Read an unsigned LEB128 number. Each byte contains 7 bits of
34 // information, plus one bit saying whether the number continues or
38 read_unsigned_LEB_128(const unsigned char* buffer
, size_t* len
)
42 unsigned int shift
= 0;
47 if (num_read
>= 64 / 7)
49 gold_warning(_("Unusually large LEB128 decoded, "
50 "debug information may be corrupted"));
55 result
|= (static_cast<uint64_t>(byte
& 0x7f)) << shift
;
65 // Read a signed LEB128 number. These are like regular LEB128
66 // numbers, except the last byte may have a sign bit set.
69 read_signed_LEB_128(const unsigned char* buffer
, size_t* len
)
78 if (num_read
>= 64 / 7)
80 gold_warning(_("Unusually large LEB128 decoded, "
81 "debug information may be corrupted"));
86 result
|= (static_cast<uint64_t>(byte
& 0x7f) << shift
);
91 if ((shift
< 8 * static_cast<int>(sizeof(result
))) && (byte
& 0x40))
92 result
|= -((static_cast<int64_t>(1)) << shift
);
98 write_unsigned_LEB_128(std::vector
<unsigned char>* buffer
, uint64_t value
)
102 unsigned char current_byte
= value
& 0x7f;
106 current_byte
|= 0x80;
108 buffer
->push_back(current_byte
);
114 get_length_as_unsigned_LEB_128(uint64_t value
)
119 unsigned char current_byte
= value
& 0x7f;
123 current_byte
|= 0x80;
131 } // End namespace gold.