LLVM  14.0.0git
BinaryStreamRef.h
Go to the documentation of this file.
1 //===- BinaryStreamRef.h - A copyable reference to a 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_BINARYSTREAMREF_H
10 #define LLVM_SUPPORT_BINARYSTREAMREF_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/Optional.h"
16 #include "llvm/Support/Error.h"
17 #include <algorithm>
18 #include <cstdint>
19 #include <memory>
20 
21 namespace llvm {
22 
23 /// Common stuff for mutable and immutable StreamRefs.
24 template <class RefType, class StreamType> class BinaryStreamRefBase {
25 protected:
26  BinaryStreamRefBase() = default;
29  if (!(BorrowedImpl.getFlags() & BSF_Append))
30  Length = BorrowedImpl.getLength();
31  }
32 
33  BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint32_t Offset,
40  BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default;
41  BinaryStreamRefBase &operator=(const BinaryStreamRefBase &Other) = default;
42 
44  BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default;
45 
46 public:
48  return BorrowedImpl->getEndian();
49  }
50 
51  uint32_t getLength() const {
52  if (Length.hasValue())
53  return *Length;
54 
55  return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0;
56  }
57 
58  /// Return a new BinaryStreamRef with the first \p N elements removed. If
59  /// this BinaryStreamRef is length-tracking, then the resulting one will be
60  /// too.
61  RefType drop_front(uint32_t N) const {
62  if (!BorrowedImpl)
63  return RefType();
64 
65  N = std::min(N, getLength());
66  RefType Result(static_cast<const RefType &>(*this));
67  if (N == 0)
68  return Result;
69 
70  Result.ViewOffset += N;
71  if (Result.Length.hasValue())
72  *Result.Length -= N;
73  return Result;
74  }
75 
76  /// Return a new BinaryStreamRef with the last \p N elements removed. If
77  /// this BinaryStreamRef is length-tracking and \p N is greater than 0, then
78  /// this BinaryStreamRef will no longer length-track.
79  RefType drop_back(uint32_t N) const {
80  if (!BorrowedImpl)
81  return RefType();
82 
83  RefType Result(static_cast<const RefType &>(*this));
84  N = std::min(N, getLength());
85 
86  if (N == 0)
87  return Result;
88 
89  // Since we're dropping non-zero bytes from the end, stop length-tracking
90  // by setting the length of the resulting StreamRef to an explicit value.
91  if (!Result.Length.hasValue())
92  Result.Length = getLength();
93 
94  *Result.Length -= N;
95  return Result;
96  }
97 
98  /// Return a new BinaryStreamRef with only the first \p N elements remaining.
99  RefType keep_front(uint32_t N) const {
100  assert(N <= getLength());
101  return drop_back(getLength() - N);
102  }
103 
104  /// Return a new BinaryStreamRef with only the last \p N elements remaining.
105  RefType keep_back(uint32_t N) const {
106  assert(N <= getLength());
107  return drop_front(getLength() - N);
108  }
109 
110  /// Return a new BinaryStreamRef with the first and last \p N elements
111  /// removed.
112  RefType drop_symmetric(uint32_t N) const {
113  return drop_front(N).drop_back(N);
114  }
115 
116  /// Return a new BinaryStreamRef with the first \p Offset elements removed,
117  /// and retaining exactly \p Len elements.
118  RefType slice(uint32_t Offset, uint32_t Len) const {
119  return drop_front(Offset).keep_front(Len);
120  }
121 
122  bool valid() const { return BorrowedImpl != nullptr; }
123 
124  friend bool operator==(const RefType &LHS, const RefType &RHS) {
125  if (LHS.BorrowedImpl != RHS.BorrowedImpl)
126  return false;
127  if (LHS.ViewOffset != RHS.ViewOffset)
128  return false;
129  if (LHS.Length != RHS.Length)
130  return false;
131  return true;
132  }
133 
134 protected:
136  if (Offset > getLength())
137  return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
138  if (getLength() < DataSize + Offset)
139  return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
140  return Error::success();
141  }
142 
143  std::shared_ptr<StreamType> SharedImpl;
147 };
148 
149 /// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
150 /// provides copy-semantics and read only access to a "window" of the underlying
151 /// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to
152 /// say, it does not inherit and override the methods of BinaryStream. In
153 /// general, you should not pass around pointers or references to BinaryStreams
154 /// and use inheritance to achieve polymorphism. Instead, you should pass
155 /// around BinaryStreamRefs by value and achieve polymorphism that way.
157  : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
160  BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint32_t ViewOffset,
163 
164 public:
165  BinaryStreamRef() = default;
166  BinaryStreamRef(BinaryStream &Stream);
172 
173  BinaryStreamRef(const BinaryStreamRef &Other) = default;
174  BinaryStreamRef &operator=(const BinaryStreamRef &Other) = default;
177 
178  // Use BinaryStreamRef.slice() instead.
180  uint32_t Length) = delete;
181 
182  /// Given an Offset into this StreamRef and a Size, return a reference to a
183  /// buffer owned by the stream.
184  ///
185  /// \returns a success error code if the entire range of data is within the
186  /// bounds of this BinaryStreamRef's view and the implementation could read
187  /// the data, and an appropriate error code otherwise.
189  ArrayRef<uint8_t> &Buffer) const;
190 
191  /// Given an Offset into this BinaryStreamRef, return a reference to the
192  /// largest buffer the stream could support without necessitating a copy.
193  ///
194  /// \returns a success error code if implementation could read the data,
195  /// and an appropriate error code otherwise.
197  ArrayRef<uint8_t> &Buffer) const;
198 };
199 
201  uint32_t Offset = 0; // Offset in the parent stream
202  BinaryStreamRef StreamData; // Stream Data
203 
205  BinaryStreamRef SubSub = StreamData.slice(Off, Size);
206  return {Off + Offset, SubSub};
207  }
209  return slice(N, size() - N);
210  }
212 
213  std::pair<BinarySubstreamRef, BinarySubstreamRef>
214  split(uint32_t Off) const {
215  return std::make_pair(keep_front(Off), drop_front(Off));
216  }
217 
218  uint32_t size() const { return StreamData.getLength(); }
219  bool empty() const { return size() == 0; }
220 };
221 
223  : public BinaryStreamRefBase<WritableBinaryStreamRef,
224  WritableBinaryStream> {
226  WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl,
229 
230  Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) const {
231  if (!(BorrowedImpl->getFlags() & BSF_Append))
232  return checkOffsetForRead(Offset, DataSize);
233 
234  if (Offset > getLength())
235  return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
236  return Error::success();
237  }
238 
239 public:
240  WritableBinaryStreamRef() = default;
248  operator=(const WritableBinaryStreamRef &Other) = default;
249 
252 
253  // Use WritableBinaryStreamRef.slice() instead.
255  uint32_t Length) = delete;
256 
257  /// Given an Offset into this WritableBinaryStreamRef and some input data,
258  /// writes the data to the underlying stream.
259  ///
260  /// \returns a success error code if the data could fit within the underlying
261  /// stream at the specified location and the implementation could write the
262  /// data, and an appropriate error code otherwise.
264 
265  /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef.
266  operator BinaryStreamRef() const;
267 
268  /// For buffered streams, commits changes to the backing store.
269  Error commit();
270 };
271 
272 } // end namespace llvm
273 
274 #endif // LLVM_SUPPORT_BINARYSTREAMREF_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::BinaryStream
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:35
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::WritableBinaryStreamRef::operator=
WritableBinaryStreamRef & operator=(const WritableBinaryStreamRef &Other)=default
Optional.h
BinaryStream.h
llvm::BinaryStreamRef::BinaryStreamRef
BinaryStreamRef()=default
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::BinaryStreamRefBase::operator=
BinaryStreamRefBase & operator=(const BinaryStreamRefBase &Other)=default
Error.h
llvm::BinaryStreamRefBase::Length
Optional< uint32_t > Length
Definition: BinaryStreamRef.h:146
llvm::Optional< uint32_t >
llvm::BinaryStreamRefBase::keep_front
RefType keep_front(uint32_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
Definition: BinaryStreamRef.h:99
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::WritableBinaryStreamRef::commit
Error commit()
For buffered streams, commits changes to the backing store.
Definition: BinaryStreamRef.cpp:130
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::stream_error_code::invalid_offset
@ invalid_offset
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:288
llvm::MutableArrayRef< uint8_t >
llvm::BinaryStreamRef::readLongestContiguousChunk
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 ...
Definition: BinaryStreamRef.cpp:86
llvm::BinaryStreamRefBase::SharedImpl
std::shared_ptr< StreamType > SharedImpl
Definition: BinaryStreamRef.h:143
llvm::BinarySubstreamRef
Definition: BinaryStreamRef.h:200
BinaryStreamError.h
llvm::BinarySubstreamRef::StreamData
BinaryStreamRef StreamData
Definition: BinaryStreamRef.h:202
llvm::BinaryStreamRefBase::drop_back
RefType drop_back(uint32_t N) const
Return a new BinaryStreamRef with the last N elements removed.
Definition: BinaryStreamRef.h:79
llvm::WritableBinaryStreamRef::writeBytes
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Given an Offset into this WritableBinaryStreamRef and some input data, writes the data to the underly...
Definition: BinaryStreamRef.cpp:117
llvm::BinaryStreamRefBase::getLength
uint32_t getLength() const
Definition: BinaryStreamRef.h:51
llvm::BinaryStreamRefBase::BinaryStreamRefBase
BinaryStreamRefBase(std::shared_ptr< StreamType > SharedImpl, uint32_t Offset, Optional< uint32_t > Length)
Definition: BinaryStreamRef.h:33
llvm::minidump::StreamType
StreamType
The type of a minidump stream identifies its contents.
Definition: Minidump.h:50
llvm::BSF_Append
@ BSF_Append
Definition: BinaryStream.h:24
llvm::BinarySubstreamRef::size
uint32_t size() const
Definition: BinaryStreamRef.h:218
llvm::WritableBinaryStreamRef::WritableBinaryStreamRef
WritableBinaryStreamRef()=default
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BinaryStreamRefBase::BinaryStreamRefBase
BinaryStreamRefBase()=default
llvm::ArrayRef< uint8_t >
llvm::BinarySubstreamRef::keep_front
BinarySubstreamRef keep_front(uint32_t N) const
Definition: BinaryStreamRef.h:211
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::BinaryStreamRefBase
Common stuff for mutable and immutable StreamRefs.
Definition: BinaryStreamRef.h:24
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::BinaryStreamRefBase::drop_front
RefType drop_front(uint32_t N) const
Return a new BinaryStreamRef with the first N elements removed.
Definition: BinaryStreamRef.h:61
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::BinarySubstreamRef::split
std::pair< BinarySubstreamRef, BinarySubstreamRef > split(uint32_t Off) const
Definition: BinaryStreamRef.h:214
llvm::BinaryStreamRefBase::getEndian
llvm::support::endianness getEndian() const
Definition: BinaryStreamRef.h:47
llvm::BinaryStreamRef::readBytes
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.
Definition: BinaryStreamRef.cpp:79
llvm::BinaryStreamRefBase::operator==
friend bool operator==(const RefType &LHS, const RefType &RHS)
Definition: BinaryStreamRef.h:124
llvm::BinaryStreamRefBase::slice
RefType slice(uint32_t Offset, uint32_t Len) const
Return a new BinaryStreamRef with the first Offset elements removed, and retaining exactly Len elemen...
Definition: BinaryStreamRef.h:118
llvm::BinaryStreamRefBase::keep_back
RefType keep_back(uint32_t N) const
Return a new BinaryStreamRef with only the last N elements remaining.
Definition: BinaryStreamRef.h:105
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::BinaryStreamRefBase::valid
bool valid() const
Definition: BinaryStreamRef.h:122
llvm::BinarySubstreamRef::slice
BinarySubstreamRef slice(uint32_t Off, uint32_t Size) const
Definition: BinaryStreamRef.h:204
llvm::BinaryStreamRefBase::BinaryStreamRefBase
BinaryStreamRefBase(StreamType &BorrowedImpl)
Definition: BinaryStreamRef.h:27
llvm::BinaryStreamRefBase::BorrowedImpl
StreamType * BorrowedImpl
Definition: BinaryStreamRef.h:144
llvm::BinarySubstreamRef::Offset
uint32_t Offset
Definition: BinaryStreamRef.h:201
llvm::BinaryStreamRefBase::checkOffsetForRead
Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) const
Definition: BinaryStreamRef.h:135
N
#define N
llvm::BinaryStreamRef::operator=
BinaryStreamRef & operator=(const BinaryStreamRef &Other)=default
llvm::stream_error_code::stream_too_short
@ stream_too_short
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::BinaryStreamRefBase::drop_symmetric
RefType drop_symmetric(uint32_t N) const
Return a new BinaryStreamRef with the first and last N elements removed.
Definition: BinaryStreamRef.h:112
llvm::BinaryStreamRefBase::ViewOffset
uint32_t ViewOffset
Definition: BinaryStreamRef.h:145
llvm::BinarySubstreamRef::drop_front
BinarySubstreamRef drop_front(uint32_t N) const
Definition: BinaryStreamRef.h:208
llvm::BinarySubstreamRef::empty
bool empty() const
Definition: BinaryStreamRef.h:219
llvm::BinaryStreamRefBase::BinaryStreamRefBase
BinaryStreamRefBase(StreamType &BorrowedImpl, uint32_t Offset, Optional< uint32_t > Length)
Definition: BinaryStreamRef.h:37
llvm::WritableBinaryStream
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:73
llvm::WritableBinaryStreamRef
Definition: BinaryStreamRef.h:222
llvm::BinaryStreamRef
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Definition: BinaryStreamRef.h:156
llvm::WritableBinaryStream::getFlags
BinaryStreamFlags getFlags() const override
Return the properties of this stream.
Definition: BinaryStream.h:86
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172