]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Support/BinaryStreamRef.h
Merge ^/head r317503 through r317807.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Support / BinaryStreamRef.h
1 //===- BinaryStreamRef.h - A copyable reference to a stream -----*- C++ -*-===//
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
10 #ifndef LLVM_SUPPORT_BINARYSTREAMREF_H
11 #define LLVM_SUPPORT_BINARYSTREAMREF_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/Support/BinaryStream.h"
15 #include "llvm/Support/BinaryStreamError.h"
16 #include "llvm/Support/Error.h"
17 #include <algorithm>
18 #include <cstdint>
19
20 namespace llvm {
21
22 /// Common stuff for mutable and immutable StreamRefs.
23 template <class StreamType, class RefType> class BinaryStreamRefBase {
24 public:
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) {}
28
29   llvm::support::endianness getEndian() const { return Stream->getEndian(); }
30
31   uint32_t getLength() const { return Length; }
32   const StreamType *getStream() const { return Stream; }
33
34   /// Return a new BinaryStreamRef with the first \p N elements removed.
35   RefType drop_front(uint32_t N) const {
36     if (!Stream)
37       return RefType();
38
39     N = std::min(N, Length);
40     return RefType(*Stream, ViewOffset + N, Length - N);
41   }
42
43   /// Return a new BinaryStreamRef with only the first \p N elements remaining.
44   RefType keep_front(uint32_t N) const {
45     if (!Stream)
46       return RefType();
47     N = std::min(N, Length);
48     return RefType(*Stream, ViewOffset, N);
49   }
50
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);
55   }
56
57   bool operator==(const RefType &Other) const {
58     if (Stream != Other.Stream)
59       return false;
60     if (ViewOffset != Other.ViewOffset)
61       return false;
62     if (Length != Other.Length)
63       return false;
64     return true;
65   }
66
67 protected:
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();
74   }
75
76   StreamType *Stream;
77   uint32_t ViewOffset;
78   uint32_t Length;
79 };
80
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.
88 class BinaryStreamRef
89     : public BinaryStreamRefBase<BinaryStream, BinaryStreamRef> {
90 public:
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) {}
96
97   // Use BinaryStreamRef.slice() instead.
98   BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset,
99                   uint32_t Length) = delete;
100
101   /// Check if a Stream is valid.
102   bool valid() const { return Stream != nullptr; }
103
104   /// Given an Offset into this StreamRef and a Size, return a reference to a
105   /// buffer owned by the stream.
106   ///
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))
113       return EC;
114
115     return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
116   }
117
118   /// Given an Offset into this BinaryStreamRef, return a reference to the
119   /// largest buffer the stream could support without necessitating a copy.
120   ///
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))
126       return EC;
127
128     if (auto EC =
129             Stream->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
130       return EC;
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();
138   }
139 };
140
141 class WritableBinaryStreamRef
142     : public BinaryStreamRefBase<WritableBinaryStream,
143                                  WritableBinaryStreamRef> {
144 public:
145   WritableBinaryStreamRef() = default;
146   WritableBinaryStreamRef(WritableBinaryStream &Stream)
147       : BinaryStreamRefBase(Stream, 0, Stream.getLength()) {}
148   WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset,
149                           uint32_t Length)
150       : BinaryStreamRefBase(Stream, Offset, Length) {}
151
152   // Use WritableBinaryStreamRef.slice() instead.
153   WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset,
154                           uint32_t Length) = delete;
155
156   /// Given an Offset into this WritableBinaryStreamRef and some input data,
157   /// writes the data to the underlying stream.
158   ///
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()))
164       return EC;
165
166     return Stream->writeBytes(ViewOffset + Offset, Data);
167   }
168
169   operator BinaryStreamRef() { return BinaryStreamRef(*Stream); }
170
171   /// \brief For buffered streams, commits changes to the backing store.
172   Error commit() { return Stream->commit(); }
173 };
174
175 } // end namespace llvm
176
177 #endif // LLVM_SUPPORT_BINARYSTREAMREF_H