LLVM 20.0.0git
BinaryStreamReader.cpp
Go to the documentation of this file.
1//===- BinaryStreamReader.cpp - Reads objects from a binary stream --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
13#include "llvm/Support/LEB128.h"
14
15using namespace llvm;
16
18
20
22 endianness Endian)
23 : Stream(Data, Endian) {}
24
26 : Stream(Data, Endian) {}
27
29 ArrayRef<uint8_t> &Buffer) {
30 if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
31 return EC;
32 Offset += Buffer.size();
33 return Error::success();
34}
35
37 if (auto EC = Stream.readBytes(Offset, Size, Buffer))
38 return EC;
39 Offset += Size;
40 return Error::success();
41}
42
44 SmallVector<uint8_t, 10> EncodedBytes;
45 ArrayRef<uint8_t> NextByte;
46
47 // Copy the encoded ULEB into the buffer.
48 do {
49 if (auto Err = readBytes(NextByte, 1))
50 return Err;
51 EncodedBytes.push_back(NextByte[0]);
52 } while (NextByte[0] & 0x80);
53
54 Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
55 return Error::success();
56}
57
59 SmallVector<uint8_t, 10> EncodedBytes;
60 ArrayRef<uint8_t> NextByte;
61
62 // Copy the encoded ULEB into the buffer.
63 do {
64 if (auto Err = readBytes(NextByte, 1))
65 return Err;
66 EncodedBytes.push_back(NextByte[0]);
67 } while (NextByte[0] & 0x80);
68
69 Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
70 return Error::success();
71}
72
74 uint64_t OriginalOffset = getOffset();
75 uint64_t FoundOffset = 0;
76 while (true) {
77 uint64_t ThisOffset = getOffset();
78 ArrayRef<uint8_t> Buffer;
79 if (auto EC = readLongestContiguousChunk(Buffer))
80 return EC;
81 StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
82 size_t Pos = S.find_first_of('\0');
83 if (LLVM_LIKELY(Pos != StringRef::npos)) {
84 FoundOffset = Pos + ThisOffset;
85 break;
86 }
87 }
88 assert(FoundOffset >= OriginalOffset);
89
90 setOffset(OriginalOffset);
91 size_t Length = FoundOffset - OriginalOffset;
92
93 if (auto EC = readFixedString(Dest, Length))
94 return EC;
95
96 // Now set the offset back to after the null terminator.
97 setOffset(FoundOffset + 1);
98 return Error::success();
99}
100
102 uint64_t Length = 0;
103 uint64_t OriginalOffset = getOffset();
104 const UTF16 *C;
105 while (true) {
106 if (auto EC = readObject(C))
107 return EC;
108 if (*C == 0x0000)
109 break;
110 ++Length;
111 }
112 uint64_t NewOffset = getOffset();
113 setOffset(OriginalOffset);
114
115 if (auto EC = readArray(Dest, Length))
116 return EC;
117 setOffset(NewOffset);
118 return Error::success();
119}
120
122 ArrayRef<uint8_t> Bytes;
123 if (auto EC = readBytes(Bytes, Length))
124 return EC;
125 Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
126 return Error::success();
127}
128
131}
132
134 if (bytesRemaining() < Length)
135 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
136 Ref = Stream.slice(Offset, Length);
137 Offset += Length;
138 return Error::success();
139}
140
143 Ref.Offset = getOffset();
144 return readStreamRef(Ref.StreamData, Length);
145}
146
148 if (Amount > bytesRemaining())
149 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
150 Offset += Amount;
151 return Error::success();
152}
153
155 uint32_t NewOffset = alignTo(Offset, Align);
156 return skip(NewOffset - Offset);
157}
158
160 ArrayRef<uint8_t> Buffer;
161 auto EC = Stream.readBytes(Offset, 1, Buffer);
162 assert(!EC && "Cannot peek an empty buffer!");
163 llvm::consumeError(std::move(EC));
164 return Buffer[0];
165}
166
167std::pair<BinaryStreamReader, BinaryStreamReader>
169 assert(getLength() >= Off);
170
171 BinaryStreamRef First = Stream.drop_front(Offset);
172
173 BinaryStreamRef Second = First.drop_front(Off);
174 First = First.keep_front(Off);
176 BinaryStreamReader W2{Second};
177 return std::make_pair(W1, W2);
178}
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:319
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
Provides read only access to a subclass of BinaryStream.
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream's offset.
Error readWideString(ArrayRef< UTF16 > &Dest)
Similar to readCString, however read a null-terminated UTF16 string instead.
Error readSubstream(BinarySubstreamRef &Ref, uint32_t Length)
Read Length bytes from the underlying stream into Ref.
uint64_t bytesRemaining() const
Error readSLEB128(int64_t &Dest)
Read a signed LEB128 encoded value.
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy,...
Error padToAlignment(uint32_t Align)
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
std::pair< BinaryStreamReader, BinaryStreamReader > split(uint64_t Offset) const
void setOffset(uint64_t Off)
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
Error readULEB128(uint64_t &Dest)
Read an unsigned LEB128 encoded value.
Error skip(uint64_t Amount)
Advance the stream's offset by Amount bytes.
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
RefType slice(uint64_t Offset, uint64_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Error readLongestContiguousChunk(uint64_t Offset, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this BinaryStreamRef, return a reference to the largest buffer the stream could ...
Error readBytes(uint64_t Offset, uint64_t Size, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this StreamRef and a Size, return a reference to a buffer owned by the stream.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:34
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:377
static constexpr size_t npos
Definition: StringRef.h:53
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:480
unsigned short UTF16
Definition: ConvertUTF.h:129
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:131
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition: LEB128.h:165
@ Ref
The access may reference the value stored in memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
endianness
Definition: bit.h:70
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39