1 //===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/BinaryStreamReader.h"
12 #include "llvm/Support/BinaryStreamError.h"
13 #include "llvm/Support/BinaryStreamRef.h"
16 using endianness
= llvm::support::endianness
;
18 BinaryStreamReader::BinaryStreamReader(BinaryStreamRef Ref
) : Stream(Ref
) {}
20 BinaryStreamReader::BinaryStreamReader(BinaryStream
&Stream
) : Stream(Stream
) {}
22 BinaryStreamReader::BinaryStreamReader(ArrayRef
<uint8_t> Data
,
24 : Stream(Data
, Endian
) {}
26 BinaryStreamReader::BinaryStreamReader(StringRef Data
, endianness Endian
)
27 : Stream(Data
, Endian
) {}
29 Error
BinaryStreamReader::readLongestContiguousChunk(
30 ArrayRef
<uint8_t> &Buffer
) {
31 if (auto EC
= Stream
.readLongestContiguousChunk(Offset
, Buffer
))
33 Offset
+= Buffer
.size();
34 return Error::success();
37 Error
BinaryStreamReader::readBytes(ArrayRef
<uint8_t> &Buffer
, uint32_t Size
) {
38 if (auto EC
= Stream
.readBytes(Offset
, Size
, Buffer
))
41 return Error::success();
44 Error
BinaryStreamReader::readCString(StringRef
&Dest
) {
45 uint32_t OriginalOffset
= getOffset();
46 uint32_t FoundOffset
= 0;
48 uint32_t ThisOffset
= getOffset();
49 ArrayRef
<uint8_t> Buffer
;
50 if (auto EC
= readLongestContiguousChunk(Buffer
))
52 StringRef
S(reinterpret_cast<const char *>(Buffer
.begin()), Buffer
.size());
53 size_t Pos
= S
.find_first_of('\0');
54 if (LLVM_LIKELY(Pos
!= StringRef::npos
)) {
55 FoundOffset
= Pos
+ ThisOffset
;
59 assert(FoundOffset
>= OriginalOffset
);
61 setOffset(OriginalOffset
);
62 size_t Length
= FoundOffset
- OriginalOffset
;
64 if (auto EC
= readFixedString(Dest
, Length
))
67 // Now set the offset back to after the null terminator.
68 setOffset(FoundOffset
+ 1);
69 return Error::success();
72 Error
BinaryStreamReader::readWideString(ArrayRef
<UTF16
> &Dest
) {
74 uint32_t OriginalOffset
= getOffset();
77 if (auto EC
= readObject(C
))
83 uint32_t NewOffset
= getOffset();
84 setOffset(OriginalOffset
);
86 if (auto EC
= readArray(Dest
, Length
))
89 return Error::success();
92 Error
BinaryStreamReader::readFixedString(StringRef
&Dest
, uint32_t Length
) {
93 ArrayRef
<uint8_t> Bytes
;
94 if (auto EC
= readBytes(Bytes
, Length
))
96 Dest
= StringRef(reinterpret_cast<const char *>(Bytes
.begin()), Bytes
.size());
97 return Error::success();
100 Error
BinaryStreamReader::readStreamRef(BinaryStreamRef
&Ref
) {
101 return readStreamRef(Ref
, bytesRemaining());
104 Error
BinaryStreamReader::readStreamRef(BinaryStreamRef
&Ref
, uint32_t Length
) {
105 if (bytesRemaining() < Length
)
106 return make_error
<BinaryStreamError
>(stream_error_code::stream_too_short
);
107 Ref
= Stream
.slice(Offset
, Length
);
109 return Error::success();
112 Error
BinaryStreamReader::readSubstream(BinarySubstreamRef
&Stream
,
114 Stream
.Offset
= getOffset();
115 return readStreamRef(Stream
.StreamData
, Size
);
118 Error
BinaryStreamReader::skip(uint32_t Amount
) {
119 if (Amount
> bytesRemaining())
120 return make_error
<BinaryStreamError
>(stream_error_code::stream_too_short
);
122 return Error::success();
125 Error
BinaryStreamReader::padToAlignment(uint32_t Align
) {
126 uint32_t NewOffset
= alignTo(Offset
, Align
);
127 return skip(NewOffset
- Offset
);
130 uint8_t BinaryStreamReader::peek() const {
131 ArrayRef
<uint8_t> Buffer
;
132 auto EC
= Stream
.readBytes(Offset
, 1, Buffer
);
133 assert(!EC
&& "Cannot peek an empty buffer!");
134 llvm::consumeError(std::move(EC
));
138 std::pair
<BinaryStreamReader
, BinaryStreamReader
>
139 BinaryStreamReader::split(uint32_t Off
) const {
140 assert(getLength() >= Off
);
142 BinaryStreamRef First
= Stream
.drop_front(Offset
);
144 BinaryStreamRef Second
= First
.drop_front(Off
);
145 First
= First
.keep_front(Off
);
146 BinaryStreamReader W1
{First
};
147 BinaryStreamReader W2
{Second
};
148 return std::make_pair(W1
, W2
);