diff options
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r-- | llvm/lib/Support/BinaryStreamReader.cpp | 33 | ||||
-rw-r--r-- | llvm/lib/Support/BinaryStreamWriter.cpp | 13 |
2 files changed, 45 insertions, 1 deletions
diff --git a/llvm/lib/Support/BinaryStreamReader.cpp b/llvm/lib/Support/BinaryStreamReader.cpp index 6f691bf4dcd..b17786593bd 100644 --- a/llvm/lib/Support/BinaryStreamReader.cpp +++ b/llvm/lib/Support/BinaryStreamReader.cpp @@ -10,6 +10,7 @@ #include "llvm/Support/BinaryStreamError.h" #include "llvm/Support/BinaryStreamRef.h" +#include "llvm/Support/LEB128.h" using namespace llvm; using endianness = llvm::support::endianness; @@ -40,6 +41,36 @@ Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) { return Error::success(); } +Error BinaryStreamReader::readULEB128(uint64_t &Dest) { + SmallVector<uint8_t, 10> EncodedBytes; + ArrayRef<uint8_t> NextByte; + + // Copy the encoded ULEB into the buffer. + do { + if (auto Err = readBytes(NextByte, 1)) + return Err; + EncodedBytes.push_back(NextByte[0]); + } while (NextByte[0] & 0x80); + + Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end()); + return Error::success(); +} + +Error BinaryStreamReader::readSLEB128(int64_t &Dest) { + SmallVector<uint8_t, 10> EncodedBytes; + ArrayRef<uint8_t> NextByte; + + // Copy the encoded ULEB into the buffer. + do { + if (auto Err = readBytes(NextByte, 1)) + return Err; + EncodedBytes.push_back(NextByte[0]); + } while (NextByte[0] & 0x80); + + Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end()); + return Error::success(); +} + Error BinaryStreamReader::readCString(StringRef &Dest) { uint32_t OriginalOffset = getOffset(); uint32_t FoundOffset = 0; @@ -145,4 +176,4 @@ BinaryStreamReader::split(uint32_t Off) const { BinaryStreamReader W1{First}; BinaryStreamReader W2{Second}; return std::make_pair(W1, W2); -}
\ No newline at end of file +} diff --git a/llvm/lib/Support/BinaryStreamWriter.cpp b/llvm/lib/Support/BinaryStreamWriter.cpp index 929968bf2e4..986e18da281 100644 --- a/llvm/lib/Support/BinaryStreamWriter.cpp +++ b/llvm/lib/Support/BinaryStreamWriter.cpp @@ -11,6 +11,7 @@ #include "llvm/Support/BinaryStreamError.h" #include "llvm/Support/BinaryStreamReader.h" #include "llvm/Support/BinaryStreamRef.h" +#include "llvm/Support/LEB128.h" using namespace llvm; @@ -31,6 +32,18 @@ Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) { return Error::success(); } +Error BinaryStreamWriter::writeULEB128(uint64_t Value) { + uint8_t EncodedBytes[10] = {0}; + unsigned Size = encodeULEB128(Value, &EncodedBytes[0]); + return writeBytes({EncodedBytes, Size}); +} + +Error BinaryStreamWriter::writeSLEB128(int64_t Value) { + uint8_t EncodedBytes[10] = {0}; + unsigned Size = encodeSLEB128(Value, &EncodedBytes[0]); + return writeBytes({EncodedBytes, Size}); +} + Error BinaryStreamWriter::writeCString(StringRef Str) { if (auto EC = writeFixedString(Str)) return EC; |