2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/leb128.h"
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/raw_istream.h"
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
|
2017-10-21 20:31:20 -07:00
|
|
|
uint64_t SizeUleb128(uint64_t val) {
|
|
|
|
|
size_t count = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
do {
|
|
|
|
|
val >>= 7;
|
|
|
|
|
++count;
|
|
|
|
|
} while (val != 0);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
uint64_t WriteUleb128(SmallVectorImpl<char>& dest, uint64_t val) {
|
2017-10-21 20:31:20 -07:00
|
|
|
size_t count = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
unsigned char byte = val & 0x7f;
|
|
|
|
|
val >>= 7;
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (val != 0) {
|
2016-09-25 17:23:39 -07:00
|
|
|
byte |= 0x80; // mark this byte to show that more bytes will follow
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
dest.push_back(byte);
|
|
|
|
|
count++;
|
|
|
|
|
} while (val != 0);
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 20:31:20 -07:00
|
|
|
uint64_t ReadUleb128(const char* addr, uint64_t* ret) {
|
2019-12-24 14:12:42 -06:00
|
|
|
uint64_t result = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
int shift = 0;
|
2017-10-21 20:31:20 -07:00
|
|
|
size_t count = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
unsigned char byte = *reinterpret_cast<const unsigned char*>(addr);
|
|
|
|
|
addr++;
|
|
|
|
|
count++;
|
|
|
|
|
|
2021-09-08 16:24:28 -07:00
|
|
|
result |= (byte & 0x7fULL) << shift;
|
2016-09-25 17:23:39 -07:00
|
|
|
shift += 7;
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!(byte & 0x80)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*ret = result;
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 20:31:20 -07:00
|
|
|
bool ReadUleb128(raw_istream& is, uint64_t* ret) {
|
2019-12-24 14:12:42 -06:00
|
|
|
uint64_t result = 0;
|
2016-09-25 17:23:39 -07:00
|
|
|
int shift = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
unsigned char byte;
|
2017-10-21 20:31:20 -07:00
|
|
|
is.read(reinterpret_cast<char*>(&byte), 1);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (is.has_error()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2021-09-08 16:24:28 -07:00
|
|
|
result |= (byte & 0x7fULL) << shift;
|
2016-09-25 17:23:39 -07:00
|
|
|
shift += 7;
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!(byte & 0x80)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*ret = result;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace wpi
|