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/Support/BinaryStream.h"
15 #include "llvm/Support/BinaryStreamError.h"
16 #include "llvm/Support/Error.h"
22 /// Common stuff for mutable and immutable StreamRefs.
23 template <class StreamType, class RefType> class BinaryStreamRefBase {
25 BinaryStreamRefBase() : Stream(nullptr), ViewOffset(0), Length(0) {}
26 BinaryStreamRefBase(StreamType &Stream, uint32_t Offset, uint32_t Length)
27 : Stream(&Stream), ViewOffset(Offset), Length(Length) {}
29 llvm::support::endianness getEndian() const { return Stream->getEndian(); }
31 uint32_t getLength() const { return Length; }
32 const StreamType *getStream() const { return Stream; }
34 /// Return a new BinaryStreamRef with the first \p N elements removed.
35 RefType drop_front(uint32_t N) const {
39 N = std::min(N, Length);
40 return RefType(*Stream, ViewOffset + N, Length - N);
43 /// Return a new BinaryStreamRef with only the first \p N elements remaining.
44 RefType keep_front(uint32_t N) const {
47 N = std::min(N, Length);
48 return RefType(*Stream, ViewOffset, N);
51 /// Return a new BinaryStreamRef with the first \p Offset elements removed,
52 /// and retaining exactly \p Len elements.
53 RefType slice(uint32_t Offset, uint32_t Len) const {
54 return drop_front(Offset).keep_front(Len);
57 bool operator==(const RefType &Other) const {
58 if (Stream != Other.Stream)
60 if (ViewOffset != Other.ViewOffset)
62 if (Length != Other.Length)
68 Error checkOffset(uint32_t Offset, uint32_t DataSize) const {
69 if (Offset > getLength())
70 return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
71 if (getLength() < DataSize + Offset)
72 return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
73 return Error::success();
81 /// \brief BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It
82 /// provides copy-semantics and read only access to a "window" of the underlying
83 /// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to
84 /// say, it does not inherit and override the methods of BinaryStream. In
85 /// general, you should not pass around pointers or references to BinaryStreams
86 /// and use inheritance to achieve polymorphism. Instead, you should pass
87 /// around BinaryStreamRefs by value and achieve polymorphism that way.
89 : public BinaryStreamRefBase<BinaryStream, BinaryStreamRef> {
91 BinaryStreamRef() = default;
92 BinaryStreamRef(BinaryStream &Stream)
93 : BinaryStreamRefBase(Stream, 0, Stream.getLength()) {}
94 BinaryStreamRef(BinaryStream &Stream, uint32_t Offset, uint32_t Length)
95 : BinaryStreamRefBase(Stream, Offset, Length) {}
97 // Use BinaryStreamRef.slice() instead.
98 BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset,
99 uint32_t Length) = delete;
101 /// Check if a Stream is valid.
102 bool valid() const { return Stream != nullptr; }
104 /// Given an Offset into this StreamRef and a Size, return a reference to a
105 /// buffer owned by the stream.
107 /// \returns a success error code if the entire range of data is within the
108 /// bounds of this BinaryStreamRef's view and the implementation could read
109 /// the data, and an appropriate error code otherwise.
110 Error readBytes(uint32_t Offset, uint32_t Size,
111 ArrayRef<uint8_t> &Buffer) const {
112 if (auto EC = checkOffset(Offset, Size))
115 return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
118 /// Given an Offset into this BinaryStreamRef, return a reference to the
119 /// largest buffer the stream could support without necessitating a copy.
121 /// \returns a success error code if implementation could read the data,
122 /// and an appropriate error code otherwise.
123 Error readLongestContiguousChunk(uint32_t Offset,
124 ArrayRef<uint8_t> &Buffer) const {
125 if (auto EC = checkOffset(Offset, 1))
129 Stream->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
131 // This StreamRef might refer to a smaller window over a larger stream. In
132 // that case we will have read out more bytes than we should return, because
133 // we should not read past the end of the current view.
134 uint32_t MaxLength = Length - Offset;
135 if (Buffer.size() > MaxLength)
136 Buffer = Buffer.slice(0, MaxLength);
137 return Error::success();
141 class WritableBinaryStreamRef
142 : public BinaryStreamRefBase<WritableBinaryStream,
143 WritableBinaryStreamRef> {
145 WritableBinaryStreamRef() = default;
146 WritableBinaryStreamRef(WritableBinaryStream &Stream)
147 : BinaryStreamRefBase(Stream, 0, Stream.getLength()) {}
148 WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset,
150 : BinaryStreamRefBase(Stream, Offset, Length) {}
152 // Use WritableBinaryStreamRef.slice() instead.
153 WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset,
154 uint32_t Length) = delete;
156 /// Given an Offset into this WritableBinaryStreamRef and some input data,
157 /// writes the data to the underlying stream.
159 /// \returns a success error code if the data could fit within the underlying
160 /// stream at the specified location and the implementation could write the
161 /// data, and an appropriate error code otherwise.
162 Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const {
163 if (auto EC = checkOffset(Offset, Data.size()))
166 return Stream->writeBytes(ViewOffset + Offset, Data);
169 operator BinaryStreamRef() { return BinaryStreamRef(*Stream); }
171 /// \brief For buffered streams, commits changes to the backing store.
172 Error commit() { return Stream->commit(); }
175 } // end namespace llvm
177 #endif // LLVM_SUPPORT_BINARYSTREAMREF_H