1 //===- BinaryStreamRef.h - A copyable reference to a stream -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_BINARYSTREAMREF_H
11 #define LLVM_SUPPORT_BINARYSTREAMREF_H
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/Support/BinaryStream.h"
16 #include "llvm/Support/BinaryStreamError.h"
17 #include "llvm/Support/Error.h"
24 /// Common stuff for mutable and immutable StreamRefs.
25 template <class RefType, class StreamType> class BinaryStreamRefBase {
27 BinaryStreamRefBase() = default;
28 explicit BinaryStreamRefBase(StreamType &BorrowedImpl)
29 : BorrowedImpl(&BorrowedImpl), ViewOffset(0) {
30 if (!(BorrowedImpl.getFlags() & BSF_Append))
31 Length = BorrowedImpl.getLength();
34 BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint32_t Offset,
35 Optional<uint32_t> Length)
36 : SharedImpl(SharedImpl), BorrowedImpl(SharedImpl.get()),
37 ViewOffset(Offset), Length(Length) {}
38 BinaryStreamRefBase(StreamType &BorrowedImpl, uint32_t Offset,
39 Optional<uint32_t> Length)
40 : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {}
41 BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default;
42 BinaryStreamRefBase &operator=(const BinaryStreamRefBase &Other) = default;
44 BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default;
45 BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default;
48 llvm::support::endianness getEndian() const {
49 return BorrowedImpl->getEndian();
52 uint32_t getLength() const {
53 if (Length.hasValue())
56 return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0;
59 /// Return a new BinaryStreamRef with the first \p N elements removed. If
60 /// this BinaryStreamRef is length-tracking, then the resulting one will be
62 RefType drop_front(uint32_t N) const {
66 N = std::min(N, getLength());
67 RefType Result(static_cast<const RefType &>(*this));
71 Result.ViewOffset += N;
72 if (Result.Length.hasValue())
77 /// Return a new BinaryStreamRef with the last \p N elements removed. If
78 /// this BinaryStreamRef is length-tracking and \p N is greater than 0, then
79 /// this BinaryStreamRef will no longer length-track.
80 RefType drop_back(uint32_t N) const {
84 RefType Result(static_cast<const RefType &>(*this));
85 N = std::min(N, getLength());
90 // Since we're dropping non-zero bytes from the end, stop length-tracking
91 // by setting the length of the resulting StreamRef to an explicit value.
92 if (!Result.Length.hasValue())
93 Result.Length = getLength();
99 /// Return a new BinaryStreamRef with only the first \p N elements remaining.
100 RefType keep_front(uint32_t N) const {
101 assert(N <= getLength());
102 return drop_back(getLength() - N);
105 /// Return a new BinaryStreamRef with only the last \p N elements remaining.
106 RefType keep_back(uint32_t N) const {
107 assert(N <= getLength());
108 return drop_front(getLength() - N);
111 /// Return a new BinaryStreamRef with the first and last \p N elements
113 RefType drop_symmetric(uint32_t N) const {
114 return drop_front(N).drop_back(N);
117 /// Return a new BinaryStreamRef with the first \p Offset elements removed,
118 /// and retaining exactly \p Len elements.
119 RefType slice(uint32_t Offset, uint32_t Len) const {
120 return drop_front(Offset).keep_front(Len);
123 bool valid() const { return BorrowedImpl != nullptr; }
125 bool operator==(const RefType &Other) const {
126 if (BorrowedImpl != Other.BorrowedImpl)
128 if (ViewOffset != Other.ViewOffset)
130 if (Length != Other.Length)
136 Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) const {
137 if (Offset > getLength())
138 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
139 if (getLength() < DataSize + Offset)
140 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
141 return Error::success();
144 std::shared_ptr<StreamType> SharedImpl;
145 StreamType *BorrowedImpl = nullptr;
146 uint32_t ViewOffset = 0;
147 Optional<uint32_t> Length;
150 /// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
151 /// provides copy-semantics and read only access to a "window" of the underlying
152 /// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to
153 /// say, it does not inherit and override the methods of BinaryStream. In
154 /// general, you should not pass around pointers or references to BinaryStreams
155 /// and use inheritance to achieve polymorphism. Instead, you should pass
156 /// around BinaryStreamRefs by value and achieve polymorphism that way.
157 class BinaryStreamRef
158 : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> {
159 friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>;
160 friend class WritableBinaryStreamRef;
161 BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint32_t ViewOffset,
162 Optional<uint32_t> Length)
163 : BinaryStreamRefBase(Impl, ViewOffset, Length) {}
166 BinaryStreamRef() = default;
167 BinaryStreamRef(BinaryStream &Stream);
168 BinaryStreamRef(BinaryStream &Stream, uint32_t Offset,
169 Optional<uint32_t> Length);
170 explicit BinaryStreamRef(ArrayRef<uint8_t> Data,
171 llvm::support::endianness Endian);
172 explicit BinaryStreamRef(StringRef Data, llvm::support::endianness Endian);
174 BinaryStreamRef(const BinaryStreamRef &Other) = default;
175 BinaryStreamRef &operator=(const BinaryStreamRef &Other) = default;
176 BinaryStreamRef(BinaryStreamRef &&Other) = default;
177 BinaryStreamRef &operator=(BinaryStreamRef &&Other) = default;
179 // Use BinaryStreamRef.slice() instead.
180 BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset,
181 uint32_t Length) = delete;
183 /// Given an Offset into this StreamRef and a Size, return a reference to a
184 /// buffer owned by the stream.
186 /// \returns a success error code if the entire range of data is within the
187 /// bounds of this BinaryStreamRef's view and the implementation could read
188 /// the data, and an appropriate error code otherwise.
189 Error readBytes(uint32_t Offset, uint32_t Size,
190 ArrayRef<uint8_t> &Buffer) const;
192 /// Given an Offset into this BinaryStreamRef, return a reference to the
193 /// largest buffer the stream could support without necessitating a copy.
195 /// \returns a success error code if implementation could read the data,
196 /// and an appropriate error code otherwise.
197 Error readLongestContiguousChunk(uint32_t Offset,
198 ArrayRef<uint8_t> &Buffer) const;
201 struct BinarySubstreamRef {
202 uint32_t Offset; // Offset in the parent stream
203 BinaryStreamRef StreamData; // Stream Data
205 BinarySubstreamRef slice(uint32_t Off, uint32_t Size) const {
206 BinaryStreamRef SubSub = StreamData.slice(Off, Size);
207 return {Off + Offset, SubSub};
209 BinarySubstreamRef drop_front(uint32_t N) const {
210 return slice(N, size() - N);
212 BinarySubstreamRef keep_front(uint32_t N) const { return slice(0, N); }
214 std::pair<BinarySubstreamRef, BinarySubstreamRef>
215 split(uint32_t Offset) const {
216 return std::make_pair(keep_front(Offset), drop_front(Offset));
219 uint32_t size() const { return StreamData.getLength(); }
220 bool empty() const { return size() == 0; }
223 class WritableBinaryStreamRef
224 : public BinaryStreamRefBase<WritableBinaryStreamRef,
225 WritableBinaryStream> {
226 friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>;
227 WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl,
228 uint32_t ViewOffset, Optional<uint32_t> Length)
229 : BinaryStreamRefBase(Impl, ViewOffset, Length) {}
231 Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) const {
232 if (!(BorrowedImpl->getFlags() & BSF_Append))
233 return checkOffsetForRead(Offset, DataSize);
235 if (Offset > getLength())
236 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
237 return Error::success();
241 WritableBinaryStreamRef() = default;
242 WritableBinaryStreamRef(WritableBinaryStream &Stream);
243 WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset,
244 Optional<uint32_t> Length);
245 explicit WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
246 llvm::support::endianness Endian);
247 WritableBinaryStreamRef(const WritableBinaryStreamRef &Other) = default;
248 WritableBinaryStreamRef &
249 operator=(const WritableBinaryStreamRef &Other) = default;
251 WritableBinaryStreamRef(WritableBinaryStreamRef &&Other) = default;
252 WritableBinaryStreamRef &operator=(WritableBinaryStreamRef &&Other) = default;
254 // Use WritableBinaryStreamRef.slice() instead.
255 WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset,
256 uint32_t Length) = delete;
258 /// Given an Offset into this WritableBinaryStreamRef and some input data,
259 /// writes the data to the underlying stream.
261 /// \returns a success error code if the data could fit within the underlying
262 /// stream at the specified location and the implementation could write the
263 /// data, and an appropriate error code otherwise.
264 Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const;
266 /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef.
267 operator BinaryStreamRef() const;
269 /// For buffered streams, commits changes to the backing store.
273 } // end namespace llvm
275 #endif // LLVM_SUPPORT_BINARYSTREAMREF_H