LLVM  4.0.0
StreamReader.cpp
Go to the documentation of this file.
1 //===- StreamReader.cpp - Reads bytes and objects from a stream -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 
14 
15 using namespace llvm;
16 using namespace llvm::msf;
17 
19 
21  if (auto EC = Stream.readLongestContiguousChunk(Offset, Buffer))
22  return EC;
23  Offset += Buffer.size();
24  return Error::success();
25 }
26 
28  if (auto EC = Stream.readBytes(Offset, Size, Buffer))
29  return EC;
30  Offset += Size;
31  return Error::success();
32 }
33 
35  const uint8_t *P;
36  if (auto EC = readObject(P))
37  return EC;
38  Dest = *P;
39  return Error::success();
40 }
41 
43  const support::ulittle16_t *P;
44  if (auto EC = readObject(P))
45  return EC;
46  Dest = *P;
47  return Error::success();
48 }
49 
51  const support::ulittle32_t *P;
52  if (auto EC = readObject(P))
53  return EC;
54  Dest = *P;
55  return Error::success();
56 }
57 
59  const support::ulittle64_t *P;
60  if (auto EC = readObject(P))
61  return EC;
62  Dest = *P;
63  return Error::success();
64 }
65 
67  const int8_t *P;
68  if (auto EC = readObject(P))
69  return EC;
70  Dest = *P;
71  return Error::success();
72 }
73 
75  const support::little16_t *P;
76  if (auto EC = readObject(P))
77  return EC;
78  Dest = *P;
79  return Error::success();
80 }
81 
83  const support::little32_t *P;
84  if (auto EC = readObject(P))
85  return EC;
86  Dest = *P;
87  return Error::success();
88 }
89 
91  const support::little64_t *P;
92  if (auto EC = readObject(P))
93  return EC;
94  Dest = *P;
95  return Error::success();
96 }
97 
99  uint32_t Length = 0;
100  // First compute the length of the string by reading 1 byte at a time.
101  uint32_t OriginalOffset = getOffset();
102  const char *C;
103  do {
104  if (auto EC = readObject(C))
105  return EC;
106  if (*C != '\0')
107  ++Length;
108  } while (*C != '\0');
109  // Now go back and request a reference for that many bytes.
110  uint32_t NewOffset = getOffset();
111  setOffset(OriginalOffset);
112 
113  ArrayRef<uint8_t> Data;
114  if (auto EC = readBytes(Data, Length))
115  return EC;
116  Dest = StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size());
117 
118  // Now set the offset back to where it was after we calculated the length.
119  setOffset(NewOffset);
120  return Error::success();
121 }
122 
124  ArrayRef<uint8_t> Bytes;
125  if (auto EC = readBytes(Bytes, Length))
126  return EC;
127  Dest = StringRef(reinterpret_cast<const char *>(Bytes.begin()), Bytes.size());
128  return Error::success();
129 }
130 
132  return readStreamRef(Ref, bytesRemaining());
133 }
134 
136  if (bytesRemaining() < Length)
137  return make_error<MSFError>(msf_error_code::insufficient_buffer);
138  Ref = Stream.slice(Offset, Length);
139  Offset += Length;
140  return Error::success();
141 }
142 
144  if (Amount > bytesRemaining())
145  return make_error<MSFError>(msf_error_code::insufficient_buffer);
146  Offset += Amount;
147  return Error::success();
148 }
149 
150 uint8_t StreamReader::peek() const {
151  ArrayRef<uint8_t> Buffer;
152  auto EC = Stream.readBytes(Offset, 1, Buffer);
153  assert(!EC && "Cannot peek an empty buffer!");
154  llvm::consumeError(std::move(EC));
155  return Buffer[0];
156 }
RefType slice(uint32_t Offset, uint32_t Len) const
Definition: StreamRef.h:47
Error readFixedString(StringRef &Dest, uint32_t Length)
void setOffset(uint32_t Off)
Definition: StreamReader.h:105
Error readZeroString(StringRef &Dest)
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const
Definition: StreamRef.h:81
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
#define P(N)
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Error readInteger(uint8_t &Dest)
Error skip(uint32_t Amount)
uint32_t Offset
iterator begin() const
Definition: ArrayRef.h:129
Error readLongestContiguousChunk(ArrayRef< uint8_t > &Buffer)
void consumeError(Error Err)
Consume a Error without doing anything.
uint32_t getOffset() const
Definition: StreamReader.h:106
static ErrorSuccess success()
Create a success value.
uint8_t peek() const
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
Error readStreamRef(ReadableStreamRef &Ref)
uint32_t bytesRemaining() const
Definition: StreamReader.h:108
StreamReader(ReadableStreamRef Stream)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const
Definition: StreamRef.h:92
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Error readObject(const T *&Dest)
Definition: StreamReader.h:53