LLVM  9.0.0svn
BinaryStreamReader.h
Go to the documentation of this file.
1 //===- BinaryStreamReader.h - Reads objects from a binary stream *- C++ -*-===//
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 
9 #ifndef LLVM_SUPPORT_BINARYSTREAMREADER_H
10 #define LLVM_SUPPORT_BINARYSTREAMREADER_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/Error.h"
20 
21 #include <string>
22 #include <type_traits>
23 
24 namespace llvm {
25 
26 /// Provides read only access to a subclass of `BinaryStream`. Provides
27 /// bounds checking and helpers for writing certain common data types such as
28 /// null-terminated strings, integers in various flavors of endianness, etc.
29 /// Can be subclassed to provide reading of custom datatypes, although no
30 /// are overridable.
32 public:
33  BinaryStreamReader() = default;
35  explicit BinaryStreamReader(BinaryStream &Stream);
39 
41  : Stream(Other.Stream), Offset(Other.Offset) {}
42 
44  Stream = Other.Stream;
45  Offset = Other.Offset;
46  return *this;
47  }
48 
49  virtual ~BinaryStreamReader() {}
50 
51  /// Read as much as possible from the underlying string at the current offset
52  /// without invoking a copy, and set \p Buffer to the resulting data slice.
53  /// Updates the stream's offset to point after the newly read data.
54  ///
55  /// \returns a success error code if the data was successfully read, otherwise
56  /// returns an appropriate error code.
58 
59  /// Read \p Size bytes from the underlying stream at the current offset and
60  /// and set \p Buffer to the resulting data slice. Whether a copy occurs
61  /// depends on the implementation of the underlying stream. Updates the
62  /// stream's offset to point after the newly read data.
63  ///
64  /// \returns a success error code if the data was successfully read, otherwise
65  /// returns an appropriate error code.
67 
68  /// Read an integer of the specified endianness into \p Dest and update the
69  /// stream's offset. The data is always copied from the stream's underlying
70  /// buffer into \p Dest. Updates the stream's offset to point after the newly
71  /// read data.
72  ///
73  /// \returns a success error code if the data was successfully read, otherwise
74  /// returns an appropriate error code.
75  template <typename T> Error readInteger(T &Dest) {
76  static_assert(std::is_integral<T>::value,
77  "Cannot call readInteger with non-integral value!");
78 
79  ArrayRef<uint8_t> Bytes;
80  if (auto EC = readBytes(Bytes, sizeof(T)))
81  return EC;
82 
83  Dest = llvm::support::endian::read<T, llvm::support::unaligned>(
84  Bytes.data(), Stream.getEndian());
85  return Error::success();
86  }
87 
88  /// Similar to readInteger.
89  template <typename T> Error readEnum(T &Dest) {
90  static_assert(std::is_enum<T>::value,
91  "Cannot call readEnum with non-enum value!");
92  typename std::underlying_type<T>::type N;
93  if (auto EC = readInteger(N))
94  return EC;
95  Dest = static_cast<T>(N);
96  return Error::success();
97  }
98 
99  /// Read a null terminated string from \p Dest. Whether a copy occurs depends
100  /// on the implementation of the underlying stream. Updates the stream's
101  /// offset to point after the newly read data.
102  ///
103  /// \returns a success error code if the data was successfully read, otherwise
104  /// returns an appropriate error code.
105  Error readCString(StringRef &Dest);
106 
107  /// Similar to readCString, however read a null-terminated UTF16 string
108  /// instead.
109  ///
110  /// \returns a success error code if the data was successfully read, otherwise
111  /// returns an appropriate error code.
113 
114  /// Read a \p Length byte string into \p Dest. Whether a copy occurs depends
115  /// on the implementation of the underlying stream. Updates the stream's
116  /// offset to point after the newly read data.
117  ///
118  /// \returns a success error code if the data was successfully read, otherwise
119  /// returns an appropriate error code.
120  Error readFixedString(StringRef &Dest, uint32_t Length);
121 
122  /// Read the entire remainder of the underlying stream into \p Ref. This is
123  /// equivalent to calling getUnderlyingStream().slice(Offset). Updates the
124  /// stream's offset to point to the end of the stream. Never causes a copy.
125  ///
126  /// \returns a success error code if the data was successfully read, otherwise
127  /// returns an appropriate error code.
129 
130  /// Read \p Length bytes from the underlying stream into \p Ref. This is
131  /// equivalent to calling getUnderlyingStream().slice(Offset, Length).
132  /// Updates the stream's offset to point after the newly read object. Never
133  /// causes a copy.
134  ///
135  /// \returns a success error code if the data was successfully read, otherwise
136  /// returns an appropriate error code.
138 
139  /// Read \p Length bytes from the underlying stream into \p Stream. This is
140  /// equivalent to calling getUnderlyingStream().slice(Offset, Length).
141  /// Updates the stream's offset to point after the newly read object. Never
142  /// causes a copy.
143  ///
144  /// \returns a success error code if the data was successfully read, otherwise
145  /// returns an appropriate error code.
147 
148  /// Get a pointer to an object of type T from the underlying stream, as if by
149  /// memcpy, and store the result into \p Dest. It is up to the caller to
150  /// ensure that objects of type T can be safely treated in this manner.
151  /// Updates the stream's offset to point after the newly read object. Whether
152  /// a copy occurs depends upon the implementation of the underlying
153  /// stream.
154  ///
155  /// \returns a success error code if the data was successfully read, otherwise
156  /// returns an appropriate error code.
157  template <typename T> Error readObject(const T *&Dest) {
158  ArrayRef<uint8_t> Buffer;
159  if (auto EC = readBytes(Buffer, sizeof(T)))
160  return EC;
161  Dest = reinterpret_cast<const T *>(Buffer.data());
162  return Error::success();
163  }
164 
165  /// Get a reference to a \p NumElements element array of objects of type T
166  /// from the underlying stream as if by memcpy, and store the resulting array
167  /// slice into \p array. It is up to the caller to ensure that objects of
168  /// type T can be safely treated in this manner. Updates the stream's offset
169  /// to point after the newly read object. Whether a copy occurs depends upon
170  /// the implementation of the underlying stream.
171  ///
172  /// \returns a success error code if the data was successfully read, otherwise
173  /// returns an appropriate error code.
174  template <typename T>
175  Error readArray(ArrayRef<T> &Array, uint32_t NumElements) {
176  ArrayRef<uint8_t> Bytes;
177  if (NumElements == 0) {
178  Array = ArrayRef<T>();
179  return Error::success();
180  }
181 
182  if (NumElements > UINT32_MAX / sizeof(T))
183  return make_error<BinaryStreamError>(
185 
186  if (auto EC = readBytes(Bytes, NumElements * sizeof(T)))
187  return EC;
188 
189  assert(alignmentAdjustment(Bytes.data(), alignof(T)) == 0 &&
190  "Reading at invalid alignment!");
191 
192  Array = ArrayRef<T>(reinterpret_cast<const T *>(Bytes.data()), NumElements);
193  return Error::success();
194  }
195 
196  /// Read a VarStreamArray of size \p Size bytes and store the result into
197  /// \p Array. Updates the stream's offset to point after the newly read
198  /// array. Never causes a copy (although iterating the elements of the
199  /// VarStreamArray may, depending upon the implementation of the underlying
200  /// stream).
201  ///
202  /// \returns a success error code if the data was successfully read, otherwise
203  /// returns an appropriate error code.
204  template <typename T, typename U>
206  uint32_t Skew = 0) {
207  BinaryStreamRef S;
208  if (auto EC = readStreamRef(S, Size))
209  return EC;
210  Array.setUnderlyingStream(S, Skew);
211  return Error::success();
212  }
213 
214  /// Read a FixedStreamArray of \p NumItems elements and store the result into
215  /// \p Array. Updates the stream's offset to point after the newly read
216  /// array. Never causes a copy (although iterating the elements of the
217  /// FixedStreamArray may, depending upon the implementation of the underlying
218  /// stream).
219  ///
220  /// \returns a success error code if the data was successfully read, otherwise
221  /// returns an appropriate error code.
222  template <typename T>
224  if (NumItems == 0) {
225  Array = FixedStreamArray<T>();
226  return Error::success();
227  }
228 
229  if (NumItems > UINT32_MAX / sizeof(T))
230  return make_error<BinaryStreamError>(
232 
233  BinaryStreamRef View;
234  if (auto EC = readStreamRef(View, NumItems * sizeof(T)))
235  return EC;
236 
237  Array = FixedStreamArray<T>(View);
238  return Error::success();
239  }
240 
241  bool empty() const { return bytesRemaining() == 0; }
242  void setOffset(uint32_t Off) { Offset = Off; }
243  uint32_t getOffset() const { return Offset; }
244  uint32_t getLength() const { return Stream.getLength(); }
245  uint32_t bytesRemaining() const { return getLength() - getOffset(); }
246 
247  /// Advance the stream's offset by \p Amount bytes.
248  ///
249  /// \returns a success error code if at least \p Amount bytes remain in the
250  /// stream, otherwise returns an appropriate error code.
251  Error skip(uint32_t Amount);
252 
253  /// Examine the next byte of the underlying stream without advancing the
254  /// stream's offset. If the stream is empty the behavior is undefined.
255  ///
256  /// \returns the next byte in the stream.
257  uint8_t peek() const;
258 
260 
261  std::pair<BinaryStreamReader, BinaryStreamReader>
262  split(uint32_t Offset) const;
263 
264 private:
265  BinaryStreamRef Stream;
266  uint32_t Offset = 0;
267 };
268 } // namespace llvm
269 
270 #endif // LLVM_SUPPORT_BINARYSTREAMREADER_H
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error padToAlignment(uint32_t Align)
Error readSubstream(BinarySubstreamRef &Stream, uint32_t Size)
Read Length bytes from the underlying stream into Stream.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream&#39;s offset.
Error readWideString(ArrayRef< UTF16 > &Dest)
Similar to readCString, however read a null-terminated UTF16 string instead.
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...
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream&#39;s offset.
FixedStreamArray is similar to VarStreamArray, except with each record having a fixed-length.
BinaryStreamReader & operator=(const BinaryStreamReader &Other)
void setUnderlyingStream(BinaryStreamRef S, uint32_t Skew=0)
The access may reference the value stored in memory.
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
std::pair< BinaryStreamReader, BinaryStreamReader > split(uint32_t Offset) const
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
#define T
virtual llvm::support::endianness getEndian() const =0
Error readArray(FixedStreamArray< T > &Array, uint32_t NumItems)
Read a FixedStreamArray of NumItems elements and store the result into Array.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
size_t alignmentAdjustment(const void *Ptr, size_t Alignment)
Returns the necessary adjustment for aligning Ptr to Alignment bytes, rounding up.
Definition: MathExtras.h:633
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.
BinaryStreamReader(const BinaryStreamReader &Other)
const T * data() const
Definition: ArrayRef.h:145
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
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 ...
#define N
uint32_t Size
Definition: Profile.cpp:46
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
Error skip(uint32_t Amount)
Advance the stream&#39;s offset by Amount bytes.
uint32_t bytesRemaining() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Error readEnum(T &Dest)
Similar to readInteger.
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy...
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 ...