LLVM  9.0.0svn
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 
14 using 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  uint32_t OriginalOffset = getOffset();
45  uint32_t FoundOffset = 0;
46  while (true) {
47  uint32_t ThisOffset = getOffset();
48  ArrayRef<uint8_t> Buffer;
49  if (auto EC = readLongestContiguousChunk(Buffer))
50  return EC;
51  StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
52  size_t Pos = S.find_first_of('\0');
53  if (LLVM_LIKELY(Pos != StringRef::npos)) {
54  FoundOffset = Pos + ThisOffset;
55  break;
56  }
57  }
58  assert(FoundOffset >= OriginalOffset);
59 
60  setOffset(OriginalOffset);
61  size_t Length = FoundOffset - OriginalOffset;
62 
63  if (auto EC = readFixedString(Dest, Length))
64  return EC;
65 
66  // Now set the offset back to after the null terminator.
67  setOffset(FoundOffset + 1);
68  return Error::success();
69 }
70 
72  uint32_t Length = 0;
73  uint32_t OriginalOffset = getOffset();
74  const UTF16 *C;
75  while (true) {
76  if (auto EC = readObject(C))
77  return EC;
78  if (*C == 0x0000)
79  break;
80  ++Length;
81  }
82  uint32_t NewOffset = getOffset();
83  setOffset(OriginalOffset);
84 
85  if (auto EC = readArray(Dest, Length))
86  return EC;
87  setOffset(NewOffset);
88  return Error::success();
89 }
90 
92  ArrayRef<uint8_t> Bytes;
93  if (auto EC = readBytes(Bytes, Length))
94  return EC;
95  Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
96  return Error::success();
97 }
98 
100  return readStreamRef(Ref, bytesRemaining());
101 }
102 
104  if (bytesRemaining() < Length)
105  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
106  Ref = Stream.slice(Offset, Length);
107  Offset += Length;
108  return Error::success();
109 }
110 
112  uint32_t Size) {
113  Stream.Offset = getOffset();
114  return readStreamRef(Stream.StreamData, Size);
115 }
116 
118  if (Amount > bytesRemaining())
119  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
120  Offset += Amount;
121  return Error::success();
122 }
123 
125  uint32_t NewOffset = alignTo(Offset, Align);
126  return skip(NewOffset - Offset);
127 }
128 
129 uint8_t BinaryStreamReader::peek() const {
130  ArrayRef<uint8_t> Buffer;
131  auto EC = Stream.readBytes(Offset, 1, Buffer);
132  assert(!EC && "Cannot peek an empty buffer!");
133  llvm::consumeError(std::move(EC));
134  return Buffer[0];
135 }
136 
137 std::pair<BinaryStreamReader, BinaryStreamReader>
139  assert(getLength() >= Off);
140 
141  BinaryStreamRef First = Stream.drop_front(Offset);
142 
143  BinaryStreamRef Second = First.drop_front(Off);
144  First = First.keep_front(Off);
145  BinaryStreamReader W1{First};
146  BinaryStreamReader W2{Second};
147  return std::make_pair(W1, W2);
148 }
uint64_t CallInst * C
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Error padToAlignment(uint32_t Align)
RefType slice(uint32_t Offset, uint32_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
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
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:190
iterator begin() const
Definition: ArrayRef.h:136
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.
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:684
unsigned short UTF16
Definition: ConvertUTF.h:110
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.
Error readBytes(uint32_t Offset, uint32_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:35
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
BinaryStreamRef StreamData
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.
static const size_t npos
Definition: StringRef.h:50
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_NODISCARD 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:380
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.
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const
Given an Offset into this BinaryStreamRef, return a reference to the largest buffer the stream could ...
uint32_t bytesRemaining() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
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 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 ...