2017-04-16 16:01:22 +00:00
|
|
|
//===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/BinaryStreamReader.h"
|
|
|
|
|
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
|
|
|
#include "llvm/Support/BinaryStreamRef.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2017-05-22 19:43:28 +00:00
|
|
|
using endianness = llvm::support::endianness;
|
2017-04-16 16:01:22 +00:00
|
|
|
|
2017-05-22 19:43:28 +00:00
|
|
|
BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref) : Stream(Ref) {}
|
|
|
|
|
|
|
|
BinaryStreamReader::BinaryStreamReader(BinaryStream &Stream) : Stream(Stream) {}
|
|
|
|
|
|
|
|
BinaryStreamReader::BinaryStreamReader(ArrayRef<uint8_t> Data,
|
|
|
|
endianness Endian)
|
|
|
|
: Stream(Data, Endian) {}
|
|
|
|
|
|
|
|
BinaryStreamReader::BinaryStreamReader(StringRef Data, endianness Endian)
|
|
|
|
: Stream(Data, Endian) {}
|
2017-04-16 16:01:22 +00:00
|
|
|
|
|
|
|
Error BinaryStreamReader::readLongestContiguousChunk(
|
|
|
|
ArrayRef<uint8_t> &Buffer) {
|
|
|
|
if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
|
|
|
|
return EC;
|
|
|
|
Offset += Buffer.size();
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
|
|
|
|
if (auto EC = Stream.readBytes(Offset, Size, Buffer))
|
|
|
|
return EC;
|
|
|
|
Offset += Size;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::readCString(StringRef &Dest) {
|
|
|
|
uint32_t OriginalOffset = getOffset();
|
2017-05-29 16:25:25 +00:00
|
|
|
uint32_t FoundOffset = 0;
|
2017-04-16 16:01:22 +00:00
|
|
|
while (true) {
|
2017-05-29 16:25:25 +00:00
|
|
|
uint32_t ThisOffset = getOffset();
|
|
|
|
ArrayRef<uint8_t> Buffer;
|
|
|
|
if (auto EC = readLongestContiguousChunk(Buffer))
|
2017-04-16 16:01:22 +00:00
|
|
|
return EC;
|
2017-05-29 16:25:25 +00:00
|
|
|
StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
|
|
|
|
size_t Pos = S.find_first_of('\0');
|
|
|
|
if (LLVM_LIKELY(Pos != StringRef::npos)) {
|
|
|
|
FoundOffset = Pos + ThisOffset;
|
2017-04-16 16:01:22 +00:00
|
|
|
break;
|
2017-05-29 16:25:25 +00:00
|
|
|
}
|
2017-04-16 16:01:22 +00:00
|
|
|
}
|
2017-05-29 16:25:25 +00:00
|
|
|
assert(FoundOffset >= OriginalOffset);
|
|
|
|
|
2017-04-16 16:01:22 +00:00
|
|
|
setOffset(OriginalOffset);
|
2017-05-29 16:25:25 +00:00
|
|
|
size_t Length = FoundOffset - OriginalOffset;
|
2017-04-16 16:01:22 +00:00
|
|
|
|
|
|
|
if (auto EC = readFixedString(Dest, Length))
|
|
|
|
return EC;
|
|
|
|
|
2017-05-29 16:25:25 +00:00
|
|
|
// Now set the offset back to after the null terminator.
|
|
|
|
setOffset(FoundOffset + 1);
|
2017-04-16 16:01:22 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
|
|
|
|
ArrayRef<uint8_t> Bytes;
|
|
|
|
if (auto EC = readBytes(Bytes, Length))
|
|
|
|
return EC;
|
|
|
|
Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
|
|
|
|
return readStreamRef(Ref, bytesRemaining());
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
|
|
|
|
if (bytesRemaining() < Length)
|
|
|
|
return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
|
|
|
|
Ref = Stream.slice(Offset, Length);
|
|
|
|
Offset += Length;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error BinaryStreamReader::skip(uint32_t Amount) {
|
|
|
|
if (Amount > bytesRemaining())
|
|
|
|
return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
|
|
|
|
Offset += Amount;
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-05-22 19:43:28 +00:00
|
|
|
Error BinaryStreamReader::padToAlignment(uint32_t Align) {
|
|
|
|
uint32_t NewOffset = alignTo(Offset, Align);
|
|
|
|
return skip(NewOffset - Offset);
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:01:22 +00:00
|
|
|
uint8_t BinaryStreamReader::peek() const {
|
|
|
|
ArrayRef<uint8_t> Buffer;
|
|
|
|
auto EC = Stream.readBytes(Offset, 1, Buffer);
|
|
|
|
assert(!EC && "Cannot peek an empty buffer!");
|
|
|
|
llvm::consumeError(std::move(EC));
|
|
|
|
return Buffer[0];
|
|
|
|
}
|
2017-05-03 20:26:11 +00:00
|
|
|
|
|
|
|
std::pair<BinaryStreamReader, BinaryStreamReader>
|
|
|
|
BinaryStreamReader::split(uint32_t Off) const {
|
|
|
|
assert(getLength() >= Off);
|
|
|
|
|
|
|
|
BinaryStreamRef First = Stream.drop_front(Offset);
|
|
|
|
|
|
|
|
BinaryStreamRef Second = First.drop_front(Off);
|
|
|
|
First = First.keep_front(Off);
|
|
|
|
BinaryStreamReader W1{First};
|
|
|
|
BinaryStreamReader W2{Second};
|
|
|
|
return std::make_pair(W1, W2);
|
|
|
|
}
|