[wpiutil] Remove LEB128 (#7628)

This was only used for NT3.
This commit is contained in:
Peter Johnson
2025-01-03 07:13:49 -08:00
committed by GitHub
parent 1240ee1bf4
commit 12a1475ee4
3 changed files with 0 additions and 342 deletions

View File

@@ -1,118 +0,0 @@
// 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.
#include "wpi/leb128.h"
#include "wpi/SpanExtras.h"
#include "wpi/raw_istream.h"
#include "wpi/raw_ostream.h"
namespace wpi {
uint64_t SizeUleb128(uint64_t val) {
size_t count = 0;
do {
val >>= 7;
++count;
} while (val != 0);
return count;
}
uint64_t WriteUleb128(SmallVectorImpl<char>& dest, uint64_t val) {
size_t count = 0;
do {
uint8_t byte = val & 0x7f;
val >>= 7;
if (val != 0) {
byte |= 0x80; // mark this byte to show that more bytes will follow
}
dest.push_back(byte);
count++;
} while (val != 0);
return count;
}
void WriteUleb128(raw_ostream& os, uint64_t val) {
do {
uint8_t byte = val & 0x7f;
val >>= 7;
if (val != 0) {
byte |= 0x80; // mark this byte to show that more bytes will follow
}
os << byte;
} while (val != 0);
}
uint64_t ReadUleb128(const char* addr, uint64_t* ret) {
uint64_t result = 0;
int shift = 0;
size_t count = 0;
while (1) {
unsigned char byte = *reinterpret_cast<const unsigned char*>(addr);
addr++;
count++;
result |= (byte & 0x7fULL) << shift;
shift += 7;
if (!(byte & 0x80)) {
break;
}
}
*ret = result;
return count;
}
bool ReadUleb128(raw_istream& is, uint64_t* ret) {
uint64_t result = 0;
int shift = 0;
while (1) {
unsigned char byte;
is.read(reinterpret_cast<char*>(&byte), 1);
if (is.has_error()) {
return false;
}
result |= (byte & 0x7fULL) << shift;
shift += 7;
if (!(byte & 0x80)) {
break;
}
}
*ret = result;
return true;
}
std::optional<uint64_t> Uleb128Reader::ReadOne(std::span<const uint8_t>* in) {
while (!in->empty()) {
uint8_t byte = in->front();
*in = wpi::drop_front(*in);
m_result |= (byte & 0x7fULL) << m_shift;
m_shift += 7;
if (!(byte & 0x80)) {
uint64_t result = m_result;
m_result = 0;
m_shift = 0;
return result;
}
}
return std::nullopt;
}
} // namespace wpi

View File

@@ -1,111 +0,0 @@
// 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.
#ifndef WPIUTIL_WPI_LEB128_H_
#define WPIUTIL_WPI_LEB128_H_
#include <stdint.h>
#include <optional>
#include <span>
namespace wpi {
template <typename T>
class SmallVectorImpl;
class raw_istream;
class raw_ostream;
/**
* Get size of unsigned LEB128 data.
*
* Determine the number of bytes required to encode an unsigned LEB128 datum.
* The algorithm is taken from Appendix C of the DWARF 3 spec. For information
* on the encodings refer to section "7.6 - Variable Length Data". Return
* the number of bytes required.
*
* @param val LEB128 data.
*/
uint64_t SizeUleb128(uint64_t val);
/**
* Write unsigned LEB128 data.
*
* Encode an unsigned LEB128 encoded datum. The algorithm is taken
* from Appendix C of the DWARF 3 spec. For information on the
* encodings refer to section "7.6 - Variable Length Data". Return
* the number of bytes written.
*
* @param dest The address where the ULEB128 data is to be stored.
* @param val Value to be stored.
*/
uint64_t WriteUleb128(SmallVectorImpl<char>& dest, uint64_t val);
/**
* Write unsigned LEB128 data.
*
* Encode an unsigned LEB128 encoded datum. The algorithm is taken
* from Appendix C of the DWARF 3 spec. For information on the
* encodings refer to section "7.6 - Variable Length Data".
*
* @param os Output stream.
* @param val Value to be stored.
*/
void WriteUleb128(raw_ostream& os, uint64_t val);
/**
* Read unsigned LEB128 data.
*
* Decode an unsigned LEB128 encoded datum. The algorithm is taken
* from Appendix C of the DWARF 3 spec. For information on the
* encodings refer to section "7.6 - Variable Length Data". Return
* the number of bytes read.
*
* @param addr The address where the ULEB128 data is stored.
* @param ret Address to store the result.
*/
uint64_t ReadUleb128(const char* addr, uint64_t* ret);
/**
* Read unsigned LEB128 data from a stream.
*
* Decode an unsigned LEB128 encoded datum. The algorithm is taken
* from Appendix C of the DWARF 3 spec. For information on the
* encodings refer to section "7.6 - Variable Length Data". Return
* false on stream error, true on success.
*
* @param is The input stream where the ULEB128 data is to be read from.
* @param ret Address to store the result.
*/
bool ReadUleb128(raw_istream& is, uint64_t* ret);
/**
* Unsigned LEB128 streaming reader.
*
* Decode an unsigned LEB128 encoded datum. The algorithm is taken
* from Appendix C of the DWARF 3 spec. For information on the
* encodings refer to section "7.6 - Variable Length Data".
*/
class Uleb128Reader {
public:
/**
* Decode a single ULEB128 value. Returns after a single ULEB128 value has
* been read or insufficient input (call in a loop to get multiple values).
* If a value is returned, internal state is reset so it's safe to immediately
* call this function again to decode another value.
*
* @param in Input data; modified as data is consumed (any unconsumed data
* is left when function returns).
* @return value (in std::optional)
*/
std::optional<uint64_t> ReadOne(std::span<const uint8_t>* in);
private:
uint64_t m_result = 0;
int m_shift = 0;
};
} // namespace wpi
#endif // WPIUTIL_WPI_LEB128_H_

View File

@@ -1,113 +0,0 @@
// 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.
//===- llvm/unittest/Support/LEB128Test.cpp - LEB128 function tests -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <stdint.h>
#include <string>
#include <string_view>
#include <gtest/gtest.h>
#include "wpi/SmallString.h"
#include "wpi/leb128.h"
#include "wpi/raw_istream.h"
namespace wpi {
TEST(LEB128Test, WriteUleb128) {
#define EXPECT_ULEB128_EQ(EXPECTED, VALUE, PAD) \
do { \
std::string_view expected(EXPECTED, sizeof(EXPECTED) - 1); \
SmallString<32> buf; \
size_t size = WriteUleb128(buf, VALUE); \
EXPECT_EQ(size, buf.size()); \
EXPECT_EQ(expected, buf.str()); \
} while (0)
// Write ULEB128
EXPECT_ULEB128_EQ("\x00", 0, 0);
EXPECT_ULEB128_EQ("\x01", 1, 0);
EXPECT_ULEB128_EQ("\x3f", 63, 0);
EXPECT_ULEB128_EQ("\x40", 64, 0);
EXPECT_ULEB128_EQ("\x7f", 0x7f, 0);
EXPECT_ULEB128_EQ("\x80\x01", 0x80, 0);
EXPECT_ULEB128_EQ("\x81\x01", 0x81, 0);
EXPECT_ULEB128_EQ("\x90\x01", 0x90, 0);
EXPECT_ULEB128_EQ("\xff\x01", 0xff, 0);
EXPECT_ULEB128_EQ("\x80\x02", 0x100, 0);
EXPECT_ULEB128_EQ("\x81\x02", 0x101, 0);
EXPECT_ULEB128_EQ("\x80\x41", 0x2080, 0);
EXPECT_ULEB128_EQ("\x80\xc1\x80\x80\x10", 0x100002080, 0);
#undef EXPECT_ULEB128_EQ
}
TEST(LEB128Test, ReadUleb128) {
#define EXPECT_READ_ULEB128_EQ(EXPECTED, VALUE) \
do { \
uint64_t val = 0; \
size_t size = ReadUleb128(VALUE, &val); \
EXPECT_EQ(sizeof(VALUE) - 1, size); \
EXPECT_EQ(EXPECTED, val); \
} while (0)
// Read ULEB128
EXPECT_READ_ULEB128_EQ(0u, "\x00");
EXPECT_READ_ULEB128_EQ(1u, "\x01");
EXPECT_READ_ULEB128_EQ(63u, "\x3f");
EXPECT_READ_ULEB128_EQ(64u, "\x40");
EXPECT_READ_ULEB128_EQ(0x7fu, "\x7f");
EXPECT_READ_ULEB128_EQ(0x80u, "\x80\x01");
EXPECT_READ_ULEB128_EQ(0x81u, "\x81\x01");
EXPECT_READ_ULEB128_EQ(0x90u, "\x90\x01");
EXPECT_READ_ULEB128_EQ(0xffu, "\xff\x01");
EXPECT_READ_ULEB128_EQ(0x100u, "\x80\x02");
EXPECT_READ_ULEB128_EQ(0x101u, "\x81\x02");
EXPECT_READ_ULEB128_EQ(0x2080u, "\x80\x41");
EXPECT_READ_ULEB128_EQ(0x100002080u, "\x80\xc1\x80\x80\x10");
#undef EXPECT_READ_ULEB128_EQ
}
TEST(LEB128Test, SizeUleb128) {
// Testing Plan:
// (1) 128 ^ n ............ need (n+1) bytes
// (2) 128 ^ n * 64 ....... need (n+1) bytes
// (3) 128 ^ (n+1) - 1 .... need (n+1) bytes
EXPECT_EQ(1u, SizeUleb128(0)); // special case
EXPECT_EQ(1u, SizeUleb128(0x1UL));
EXPECT_EQ(1u, SizeUleb128(0x40UL));
EXPECT_EQ(1u, SizeUleb128(0x7fUL));
EXPECT_EQ(2u, SizeUleb128(0x80UL));
EXPECT_EQ(2u, SizeUleb128(0x2000UL));
EXPECT_EQ(2u, SizeUleb128(0x3fffUL));
EXPECT_EQ(3u, SizeUleb128(0x4000UL));
EXPECT_EQ(3u, SizeUleb128(0x100000UL));
EXPECT_EQ(3u, SizeUleb128(0x1fffffUL));
EXPECT_EQ(4u, SizeUleb128(0x200000UL));
EXPECT_EQ(4u, SizeUleb128(0x8000000UL));
EXPECT_EQ(4u, SizeUleb128(0xfffffffUL));
EXPECT_EQ(5u, SizeUleb128(0x10000000UL));
EXPECT_EQ(5u, SizeUleb128(0x40000000UL));
EXPECT_EQ(5u, SizeUleb128(0x7fffffffUL));
EXPECT_EQ(5u, SizeUleb128(UINT32_MAX));
}
} // namespace wpi