LLVM  14.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 
15 using namespace llvm;
17 
19 
21 
23  endianness Endian)
24  : Stream(Data, Endian) {}
25 
27  : Stream(Data, Endian) {}
28 
30  ArrayRef<uint8_t> &Buffer) {
31  if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
32  return EC;
33  Offset += Buffer.size();
34  return Error::success();
35 }
36 
38  if (auto EC = Stream.readBytes(Offset, Size, Buffer))
39  return EC;
40  Offset += Size;
41  return Error::success();
42 }
43 
45  SmallVector<uint8_t, 10> EncodedBytes;
46  ArrayRef<uint8_t> NextByte;
47 
48  // Copy the encoded ULEB into the buffer.
49  do {
50  if (auto Err = readBytes(NextByte, 1))
51  return Err;
52  EncodedBytes.push_back(NextByte[0]);
53  } while (NextByte[0] & 0x80);
54 
55  Dest = decodeULEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
56  return Error::success();
57 }
58 
60  SmallVector<uint8_t, 10> EncodedBytes;
61  ArrayRef<uint8_t> NextByte;
62 
63  // Copy the encoded ULEB into the buffer.
64  do {
65  if (auto Err = readBytes(NextByte, 1))
66  return Err;
67  EncodedBytes.push_back(NextByte[0]);
68  } while (NextByte[0] & 0x80);
69 
70  Dest = decodeSLEB128(EncodedBytes.begin(), nullptr, EncodedBytes.end());
71  return Error::success();
72 }
73 
75  uint64_t OriginalOffset = getOffset();
76  uint64_t FoundOffset = 0;
77  while (true) {
78  uint64_t ThisOffset = getOffset();
79  ArrayRef<uint8_t> Buffer;
80  if (auto EC = readLongestContiguousChunk(Buffer))
81  return EC;
82  StringRef S(reinterpret_cast<const char *>(Buffer.begin()), Buffer.size());
83  size_t Pos = S.find_first_of('\0');
84  if (LLVM_LIKELY(Pos != StringRef::npos)) {
85  FoundOffset = Pos + ThisOffset;
86  break;
87  }
88  }
89  assert(FoundOffset >= OriginalOffset);
90 
91  setOffset(OriginalOffset);
92  size_t Length = FoundOffset - OriginalOffset;
93 
94  if (auto EC = readFixedString(Dest, Length))
95  return EC;
96 
97  // Now set the offset back to after the null terminator.
98  setOffset(FoundOffset + 1);
99  return Error::success();
100 }
101 
103  uint64_t Length = 0;
104  uint64_t OriginalOffset = getOffset();
105  const UTF16 *C;
106  while (true) {
107  if (auto EC = readObject(C))
108  return EC;
109  if (*C == 0x0000)
110  break;
111  ++Length;
112  }
113  uint64_t NewOffset = getOffset();
114  setOffset(OriginalOffset);
115 
116  if (auto EC = readArray(Dest, Length))
117  return EC;
118  setOffset(NewOffset);
119  return Error::success();
120 }
121 
123  ArrayRef<uint8_t> Bytes;
124  if (auto EC = readBytes(Bytes, Length))
125  return EC;
126  Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
127  return Error::success();
128 }
129 
131  return readStreamRef(Ref, bytesRemaining());
132 }
133 
135  if (bytesRemaining() < Length)
136  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
137  Ref = Stream.slice(Offset, Length);
138  Offset += Length;
139  return Error::success();
140 }
141 
143  uint32_t Length) {
144  Ref.Offset = getOffset();
145  return readStreamRef(Ref.StreamData, Length);
146 }
147 
149  if (Amount > bytesRemaining())
150  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
151  Offset += Amount;
152  return Error::success();
153 }
154 
156  uint32_t NewOffset = alignTo(Offset, Align);
157  return skip(NewOffset - Offset);
158 }
159 
160 uint8_t BinaryStreamReader::peek() const {
161  ArrayRef<uint8_t> Buffer;
162  auto EC = Stream.readBytes(Offset, 1, Buffer);
163  assert(!EC && "Cannot peek an empty buffer!");
165  return Buffer[0];
166 }
167 
168 std::pair<BinaryStreamReader, BinaryStreamReader>
170  assert(getLength() >= Off);
171 
173 
174  BinaryStreamRef Second = First.drop_front(Off);
175  First = First.keep_front(Off);
177  BinaryStreamReader W2{Second};
178  return std::make_pair(W1, W2);
179 }
llvm::BinaryStreamReader::split
std::pair< BinaryStreamReader, BinaryStreamReader > split(uint64_t Offset) const
Definition: BinaryStreamReader.cpp:169
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
BinaryStreamReader.h
llvm::BinaryStreamRef::readLongestContiguousChunk
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 ...
Definition: BinaryStreamRef.cpp:86
llvm::BinaryStreamReader::setOffset
void setOffset(uint64_t Off)
Definition: BinaryStreamReader.h:254
llvm::BinaryStreamReader::readSLEB128
Error readSLEB128(int64_t &Dest)
Read a signed LEB128 encoded value.
Definition: BinaryStreamReader.cpp:59
llvm::BinaryStream
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::BinaryStreamReader::readBytes
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 ...
Definition: BinaryStreamReader.cpp:37
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:59
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::BinaryStreamReader::skip
Error skip(uint64_t Amount)
Advance the stream's offset by Amount bytes.
Definition: BinaryStreamReader.cpp:148
llvm::BinaryStreamReader::readLongestContiguousChunk
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
Read as much as possible from the underlying string at the current offset without invoking a copy,...
Definition: BinaryStreamReader.cpp:29
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::BinaryStreamReader::bytesRemaining
uint64_t bytesRemaining() const
Definition: BinaryStreamReader.h:257
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::BinaryStreamReader::getLength
uint64_t getLength() const
Definition: BinaryStreamReader.h:256
llvm::BinaryStreamReader::BinaryStreamReader
BinaryStreamReader()=default
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1045
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::decodeULEB128
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:128
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::BinaryStreamReader::getOffset
uint64_t getOffset() const
Definition: BinaryStreamReader.h:255
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::BinarySubstreamRef
Definition: BinaryStreamRef.h:200
BinaryStreamError.h
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
llvm::BinaryStreamReader::readSubstream
Error readSubstream(BinarySubstreamRef &Ref, uint32_t Length)
Read Length bytes from the underlying stream into Ref.
Definition: BinaryStreamReader.cpp:142
llvm::BinaryStreamReader::readStreamRef
Error readStreamRef(BinaryStreamRef &Ref)
Read the entire remainder of the underlying stream into Ref.
Definition: BinaryStreamReader.cpp:130
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::BinaryStreamReader::padToAlignment
Error padToAlignment(uint32_t Align)
Definition: BinaryStreamReader.cpp:155
llvm::BinaryStreamReader::readULEB128
Error readULEB128(uint64_t &Dest)
Read an unsigned LEB128 encoded value.
Definition: BinaryStreamReader.cpp:44
llvm::BinaryStreamReader
Provides read only access to a subclass of BinaryStream.
Definition: BinaryStreamReader.h:31
uint64_t
llvm::BinaryStreamReader::readCString
Error readCString(StringRef &Dest)
Read a null terminated string from Dest.
Definition: BinaryStreamReader.cpp:74
LEB128.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BinaryStreamReader::readObject
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...
Definition: BinaryStreamReader.h:169
llvm::ArrayRef< uint8_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
BinaryStreamRef.h
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::BinaryStreamReader::readWideString
Error readWideString(ArrayRef< UTF16 > &Dest)
Similar to readCString, however read a null-terminated UTF16 string instead.
Definition: BinaryStreamReader.cpp:102
llvm::BinaryStreamRefBase::slice
RefType slice(uint64_t Offset, uint64_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
Definition: BinaryStreamRef.h:118
llvm::BinaryStreamReader::readFixedString
Error readFixedString(StringRef &Dest, uint32_t Length)
Read a Length byte string into Dest.
Definition: BinaryStreamReader.cpp:122
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::BinaryStreamRef::readBytes
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.
Definition: BinaryStreamRef.cpp:79
llvm::decodeSLEB128
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:161
llvm::BinaryStreamReader::peek
uint8_t peek() const
Examine the next byte of the underlying stream without advancing the stream's offset.
Definition: BinaryStreamReader.cpp:160
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
llvm::stream_error_code::stream_too_short
@ stream_too_short
LLVM_LIKELY
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:225
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::UTF16
unsigned short UTF16
Definition: ConvertUTF.h:110
llvm::BinaryStreamRefBase::drop_front
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
Definition: BinaryStreamRef.h:61
llvm::BinaryStreamReader::readArray
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 ...
Definition: BinaryStreamReader.h:187
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156