17330f729Sjoerg //===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg
97330f729Sjoerg #include "llvm/Support/BinaryStreamReader.h"
107330f729Sjoerg
117330f729Sjoerg #include "llvm/Support/BinaryStreamError.h"
127330f729Sjoerg #include "llvm/Support/BinaryStreamRef.h"
137330f729Sjoerg #include "llvm/Support/LEB128.h"
147330f729Sjoerg
157330f729Sjoerg using namespace llvm;
167330f729Sjoerg using endianness = llvm::support::endianness;
177330f729Sjoerg
BinaryStreamReader(BinaryStreamRef Ref)187330f729Sjoerg BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref) : Stream(Ref) {}
197330f729Sjoerg
BinaryStreamReader(BinaryStream & Stream)207330f729Sjoerg BinaryStreamReader::BinaryStreamReader(BinaryStream &Stream) : Stream(Stream) {}
217330f729Sjoerg
BinaryStreamReader(ArrayRef<uint8_t> Data,endianness Endian)227330f729Sjoerg BinaryStreamReader::BinaryStreamReader(ArrayRef<uint8_t> Data,
237330f729Sjoerg endianness Endian)
247330f729Sjoerg : Stream(Data, Endian) {}
257330f729Sjoerg
BinaryStreamReader(StringRef Data,endianness Endian)267330f729Sjoerg BinaryStreamReader::BinaryStreamReader(StringRef Data, endianness Endian)
277330f729Sjoerg : Stream(Data, Endian) {}
287330f729Sjoerg
readLongestContiguousChunk(ArrayRef<uint8_t> & Buffer)297330f729Sjoerg Error BinaryStreamReader::readLongestContiguousChunk(
307330f729Sjoerg ArrayRef<uint8_t> &Buffer) {
317330f729Sjoerg if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
327330f729Sjoerg return EC;
337330f729Sjoerg Offset += Buffer.size();
347330f729Sjoerg return Error::success();
357330f729Sjoerg }
367330f729Sjoerg
readBytes(ArrayRef<uint8_t> & Buffer,uint32_t Size)377330f729Sjoerg Error BinaryStreamReader::readBytes(ArrayRef<uint8_t> &Buffer, uint32_t Size) {
387330f729Sjoerg if (auto EC = Stream.readBytes(Offset, Size, Buffer))
397330f729Sjoerg return EC;
407330f729Sjoerg Offset += Size;
417330f729Sjoerg return Error::success();
427330f729Sjoerg }
437330f729Sjoerg
readULEB128(uint64_t & Dest)447330f729Sjoerg Error BinaryStreamReader::readULEB128(uint64_t &Dest) {
457330f729Sjoerg SmallVector<uint8_t, 10> EncodedBytes;
467330f729Sjoerg ArrayRef<uint8_t> NextByte;
477330f729Sjoerg
487330f729Sjoerg // Copy the encoded ULEB into the buffer.
497330f729Sjoerg do {
507330f729Sjoerg if (auto Err = readBytes(NextByte, 1))
517330f729Sjoerg return Err;
527330f729Sjoerg EncodedBytes.push_back(NextByte[0]);
537330f729Sjoerg } while (NextByte[0] & 0x80);
547330f729Sjoerg
557330f729Sjoerg Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
567330f729Sjoerg return Error::success();
577330f729Sjoerg }
587330f729Sjoerg
readSLEB128(int64_t & Dest)597330f729Sjoerg Error BinaryStreamReader::readSLEB128(int64_t &Dest) {
607330f729Sjoerg SmallVector<uint8_t, 10> EncodedBytes;
617330f729Sjoerg ArrayRef<uint8_t> NextByte;
627330f729Sjoerg
637330f729Sjoerg // Copy the encoded ULEB into the buffer.
647330f729Sjoerg do {
657330f729Sjoerg if (auto Err = readBytes(NextByte, 1))
667330f729Sjoerg return Err;
677330f729Sjoerg EncodedBytes.push_back(NextByte[0]);
687330f729Sjoerg } while (NextByte[0] & 0x80);
697330f729Sjoerg
707330f729Sjoerg Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
717330f729Sjoerg return Error::success();
727330f729Sjoerg }
737330f729Sjoerg
readCString(StringRef & Dest)747330f729Sjoerg Error BinaryStreamReader::readCString(StringRef &Dest) {
757330f729Sjoerg uint32_t OriginalOffset = getOffset();
767330f729Sjoerg uint32_t FoundOffset = 0;
777330f729Sjoerg while (true) {
787330f729Sjoerg uint32_t ThisOffset = getOffset();
797330f729Sjoerg ArrayRef<uint8_t> Buffer;
807330f729Sjoerg if (auto EC = readLongestContiguousChunk(Buffer))
817330f729Sjoerg return EC;
827330f729Sjoerg StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
837330f729Sjoerg size_t Pos = S.find_first_of('\0');
847330f729Sjoerg if (LLVM_LIKELY(Pos != StringRef::npos)) {
857330f729Sjoerg FoundOffset = Pos + ThisOffset;
867330f729Sjoerg break;
877330f729Sjoerg }
887330f729Sjoerg }
897330f729Sjoerg assert(FoundOffset >= OriginalOffset);
907330f729Sjoerg
917330f729Sjoerg setOffset(OriginalOffset);
927330f729Sjoerg size_t Length = FoundOffset - OriginalOffset;
937330f729Sjoerg
947330f729Sjoerg if (auto EC = readFixedString(Dest, Length))
957330f729Sjoerg return EC;
967330f729Sjoerg
977330f729Sjoerg // Now set the offset back to after the null terminator.
987330f729Sjoerg setOffset(FoundOffset + 1);
997330f729Sjoerg return Error::success();
1007330f729Sjoerg }
1017330f729Sjoerg
readWideString(ArrayRef<UTF16> & Dest)1027330f729Sjoerg Error BinaryStreamReader::readWideString(ArrayRef<UTF16> &Dest) {
1037330f729Sjoerg uint32_t Length = 0;
1047330f729Sjoerg uint32_t OriginalOffset = getOffset();
1057330f729Sjoerg const UTF16 *C;
1067330f729Sjoerg while (true) {
1077330f729Sjoerg if (auto EC = readObject(C))
1087330f729Sjoerg return EC;
1097330f729Sjoerg if (*C == 0x0000)
1107330f729Sjoerg break;
1117330f729Sjoerg ++Length;
1127330f729Sjoerg }
1137330f729Sjoerg uint32_t NewOffset = getOffset();
1147330f729Sjoerg setOffset(OriginalOffset);
1157330f729Sjoerg
1167330f729Sjoerg if (auto EC = readArray(Dest, Length))
1177330f729Sjoerg return EC;
1187330f729Sjoerg setOffset(NewOffset);
1197330f729Sjoerg return Error::success();
1207330f729Sjoerg }
1217330f729Sjoerg
readFixedString(StringRef & Dest,uint32_t Length)1227330f729Sjoerg Error BinaryStreamReader::readFixedString(StringRef &Dest, uint32_t Length) {
1237330f729Sjoerg ArrayRef<uint8_t> Bytes;
1247330f729Sjoerg if (auto EC = readBytes(Bytes, Length))
1257330f729Sjoerg return EC;
1267330f729Sjoerg Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
1277330f729Sjoerg return Error::success();
1287330f729Sjoerg }
1297330f729Sjoerg
readStreamRef(BinaryStreamRef & Ref)1307330f729Sjoerg Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref) {
1317330f729Sjoerg return readStreamRef(Ref, bytesRemaining());
1327330f729Sjoerg }
1337330f729Sjoerg
readStreamRef(BinaryStreamRef & Ref,uint32_t Length)1347330f729Sjoerg Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
1357330f729Sjoerg if (bytesRemaining() < Length)
1367330f729Sjoerg return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
1377330f729Sjoerg Ref = Stream.slice(Offset, Length);
1387330f729Sjoerg Offset += Length;
1397330f729Sjoerg return Error::success();
1407330f729Sjoerg }
1417330f729Sjoerg
readSubstream(BinarySubstreamRef & Ref,uint32_t Length)142*82d56013Sjoerg Error BinaryStreamReader::readSubstream(BinarySubstreamRef &Ref,
143*82d56013Sjoerg uint32_t Length) {
144*82d56013Sjoerg Ref.Offset = getOffset();
145*82d56013Sjoerg return readStreamRef(Ref.StreamData, Length);
1467330f729Sjoerg }
1477330f729Sjoerg
skip(uint32_t Amount)1487330f729Sjoerg Error BinaryStreamReader::skip(uint32_t Amount) {
1497330f729Sjoerg if (Amount > bytesRemaining())
1507330f729Sjoerg return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
1517330f729Sjoerg Offset += Amount;
1527330f729Sjoerg return Error::success();
1537330f729Sjoerg }
1547330f729Sjoerg
padToAlignment(uint32_t Align)1557330f729Sjoerg Error BinaryStreamReader::padToAlignment(uint32_t Align) {
1567330f729Sjoerg uint32_t NewOffset = alignTo(Offset, Align);
1577330f729Sjoerg return skip(NewOffset - Offset);
1587330f729Sjoerg }
1597330f729Sjoerg
peek() const1607330f729Sjoerg uint8_t BinaryStreamReader::peek() const {
1617330f729Sjoerg ArrayRef<uint8_t> Buffer;
1627330f729Sjoerg auto EC = Stream.readBytes(Offset, 1, Buffer);
1637330f729Sjoerg assert(!EC && "Cannot peek an empty buffer!");
1647330f729Sjoerg llvm::consumeError(std::move(EC));
1657330f729Sjoerg return Buffer[0];
1667330f729Sjoerg }
1677330f729Sjoerg
1687330f729Sjoerg std::pair<BinaryStreamReader, BinaryStreamReader>
split(uint32_t Off) const1697330f729Sjoerg BinaryStreamReader::split(uint32_t Off) const {
1707330f729Sjoerg assert(getLength() >= Off);
1717330f729Sjoerg
1727330f729Sjoerg BinaryStreamRef First = Stream.drop_front(Offset);
1737330f729Sjoerg
1747330f729Sjoerg BinaryStreamRef Second = First.drop_front(Off);
1757330f729Sjoerg First = First.keep_front(Off);
1767330f729Sjoerg BinaryStreamReader W1{First};
1777330f729Sjoerg BinaryStreamReader W2{Second};
1787330f729Sjoerg return std::make_pair(W1, W2);
1797330f729Sjoerg }
180