1//===- BinaryStreamReader.h - Reads objects from a binary stream *- C++ -*-===//
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
7//===----------------------------------------------------------------------===//
9#ifndef LLVM_SUPPORT_BINARYSTREAMREADER_H
10#define LLVM_SUPPORT_BINARYSTREAMREADER_H
25/// Provides read only access to a subclass of `BinaryStream`. Provides
26/// bounds checking and helpers for writing certain common data types such as
27/// null-terminated strings, integers in various flavors of endianness, etc.
28/// Can be subclassed to provide reading of custom datatypes, although no
45 /// Read as much as possible from the underlying string at the current offset
46 /// without invoking a copy, and set \p Buffer to the resulting data slice.
47 /// Updates the stream's offset to point after the newly read data.
49 /// \returns a success error code if the data was successfully read, otherwise
50 /// returns an appropriate error code.
53 /// Read \p Size bytes from the underlying stream at the current offset and
54 /// and set \p Buffer to the resulting data slice. Whether a copy occurs
55 /// depends on the implementation of the underlying stream. Updates the
56 /// stream's offset to point after the newly read data.
58 /// \returns a success error code if the data was successfully read, otherwise
59 /// returns an appropriate error code.
62 /// Read an integer of the specified endianness into \p Dest and update the
63 /// stream's offset. The data is always copied from the stream's underlying
64 /// buffer into \p Dest. Updates the stream's offset to point after the newly
67 /// \returns a success error code if the data was successfully read, otherwise
68 /// returns an appropriate error code.
70 static_assert(std::is_integral_v<T>,
71 "Cannot call readInteger with non-integral value!");
81 /// Similar to readInteger.
83 static_assert(std::is_enum<T>::value,
84 "Cannot call readEnum with non-enum value!");
85 std::underlying_type_t<T>
N;
88 Dest =
static_cast<T >(
N);
92 /// Read an unsigned LEB128 encoded value.
94 /// \returns a success error code if the data was successfully read, otherwise
95 /// returns an appropriate error code.
98 /// Read a signed LEB128 encoded value.
100 /// \returns a success error code if the data was successfully read, otherwise
101 /// returns an appropriate error code.
104 /// Read a null terminated string from \p Dest. Whether a copy occurs depends
105 /// on the implementation of the underlying stream. Updates the stream's
106 /// offset to point after the newly read data.
108 /// \returns a success error code if the data was successfully read, otherwise
109 /// returns an appropriate error code.
112 /// Similar to readCString, however read a null-terminated UTF16 string
115 /// \returns a success error code if the data was successfully read, otherwise
116 /// returns an appropriate error code.
119 /// Read a \p Length byte string into \p Dest. Whether a copy occurs depends
120 /// on the implementation of the underlying stream. Updates the stream's
121 /// offset to point after the newly read data.
123 /// \returns a success error code if the data was successfully read, otherwise
124 /// returns an appropriate error code.
127 /// Read the entire remainder of the underlying stream into \p Ref. This is
128 /// equivalent to calling getUnderlyingStream().slice(Offset). Updates the
129 /// stream's offset to point to the end of the stream. Never causes a copy.
131 /// \returns a success error code if the data was successfully read, otherwise
132 /// returns an appropriate error code.
135 /// Read \p Length bytes from the underlying stream into \p Ref. This is
136 /// equivalent to calling getUnderlyingStream().slice(Offset, Length).
137 /// Updates the stream's offset to point after the newly read object. Never
140 /// \returns a success error code if the data was successfully read, otherwise
141 /// returns an appropriate error code.
144 /// Read \p Length bytes from the underlying stream into \p Ref. This is
145 /// equivalent to calling getUnderlyingStream().slice(Offset, Length).
146 /// Updates the stream's offset to point after the newly read object. Never
149 /// \returns a success error code if the data was successfully read, otherwise
150 /// returns an appropriate error code.
153 /// Get a pointer to an object of type T from the underlying stream, as if by
154 /// memcpy, and store the result into \p Dest. It is up to the caller to
155 /// ensure that objects of type T can be safely treated in this manner.
156 /// Updates the stream's offset to point after the newly read object. Whether
157 /// a copy occurs depends upon the implementation of the underlying
160 /// \returns a success error code if the data was successfully read, otherwise
161 /// returns an appropriate error code.
166 Dest =
reinterpret_cast<const T *
>(Buffer.
data());
170 /// Get a reference to a \p NumElements element array of objects of type T
171 /// from the underlying stream as if by memcpy, and store the resulting array
172 /// slice into \p array. It is up to the caller to ensure that objects of
173 /// type T can be safely treated in this manner. Updates the stream's offset
174 /// to point after the newly read object. Whether a copy occurs depends upon
175 /// the implementation of the underlying stream.
177 /// \returns a success error code if the data was successfully read, otherwise
178 /// returns an appropriate error code.
179 template <
typename T>
182 if (NumElements == 0) {
187 if (NumElements > UINT32_MAX /
sizeof(
T))
191 if (
auto EC =
readBytes(Bytes, NumElements *
sizeof(
T)))
195 "Reading at invalid alignment!");
197 Array =
ArrayRef<T>(
reinterpret_cast<const T *
>(Bytes.
data()), NumElements);
201 /// Read a VarStreamArray of size \p Size bytes and store the result into
202 /// \p Array. Updates the stream's offset to point after the newly read
203 /// array. Never causes a copy (although iterating the elements of the
204 /// VarStreamArray may, depending upon the implementation of the underlying
207 /// \returns a success error code if the data was successfully read, otherwise
208 /// returns an appropriate error code.
209 template <
typename T,
typename U>
215 Array.setUnderlyingStream(S, Skew);
219 /// Read a FixedStreamArray of \p NumItems elements and store the result into
220 /// \p Array. Updates the stream's offset to point after the newly read
221 /// array. Never causes a copy (although iterating the elements of the
222 /// FixedStreamArray may, depending upon the implementation of the underlying
225 /// \returns a success error code if the data was successfully read, otherwise
226 /// returns an appropriate error code.
227 template <
typename T>
234 if (NumItems > UINT32_MAX /
sizeof(
T))
252 /// Advance the stream's offset by \p Amount bytes.
254 /// \returns a success error code if at least \p Amount bytes remain in the
255 /// stream, otherwise returns an appropriate error code.
258 /// Examine the next byte of the underlying stream without advancing the
259 /// stream's offset. If the stream is empty the behavior is undefined.
261 /// \returns the next byte in the stream.
266 LLVM_ABI std::pair<BinaryStreamReader, BinaryStreamReader>
275#endif // LLVM_SUPPORT_BINARYSTREAMREADER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Lightweight arrays that are backed by an arbitrary BinaryStream.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM_ABI Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
BinaryStreamReader & operator=(const BinaryStreamReader &Other)=default
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...
LLVM_ABI Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Error readArray(FixedStreamArray< T > &Array, uint32_t NumItems)
Read a FixedStreamArray of NumItems elements and store the result into Array.
LLVM_ABI 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 ...
LLVM_ABI uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream's offset.
LLVM_ABI Error readWideString(ArrayRef< UTF16 > &Dest)
Similar to readCString, however read a null-terminated UTF16 string instead.
uint64_t getLength() const
LLVM_ABI Error readSubstream(BinarySubstreamRef &Ref, uint32_t Length)
Read Length bytes from the underlying stream into Ref.
Error readEnum(T &Dest)
Similar to readInteger.
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
BinaryStreamReader()=default
uint64_t bytesRemaining() const
uint64_t getOffset() const
LLVM_ABI Error readSLEB128(int64_t &Dest)
Read a signed LEB128 encoded value.
LLVM_ABI Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy,...
LLVM_ABI Error padToAlignment(uint32_t Align)
LLVM_ABI Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
LLVM_ABI 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 ...
LLVM_ABI Error readULEB128(uint64_t &Dest)
Read an unsigned LEB128 encoded value.
LLVM_ABI Error skip(uint64_t Amount)
Advance the stream's offset by Amount bytes.
BinaryStreamReader(const BinaryStreamReader &Other)=default
Error readArray(VarStreamArray< T, U > &Array, uint32_t Size, uint32_t Skew=0)
Read a VarStreamArray of size Size bytes and store the result into Array.
virtual ~BinaryStreamReader()=default
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
An interface for accessing data in a stream-like format, but which discourages copying.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
FixedStreamArray is similar to VarStreamArray, except with each record having a fixed-length.
StringRef - Represent a constant reference to a string, i.e.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
This is an optimization pass for GlobalISel generic memory operations.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
@ Ref
The access may reference the value stored in memory.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
This struct is a compact representation of a valid (non-zero power of two) alignment.
static constexpr Align Of()
Allow constructions of constexpr Align from types.