LLVM  5.0.0
BinaryStreamRef.cpp
Go to the documentation of this file.
1 //===- BinaryStreamRef.cpp - ----------------------------------------------===//
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 
12 
13 using namespace llvm;
14 using namespace llvm::support;
15 
16 namespace {
17 
18 class ArrayRefImpl : public BinaryStream {
19 public:
20  ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
21 
22  llvm::support::endianness getEndian() const override {
23  return BBS.getEndian();
24  }
25  Error readBytes(uint32_t Offset, uint32_t Size,
26  ArrayRef<uint8_t> &Buffer) override {
27  return BBS.readBytes(Offset, Size, Buffer);
28  }
29  Error readLongestContiguousChunk(uint32_t Offset,
30  ArrayRef<uint8_t> &Buffer) override {
31  return BBS.readLongestContiguousChunk(Offset, Buffer);
32  }
33  uint32_t getLength() override { return BBS.getLength(); }
34 
35 private:
36  BinaryByteStream BBS;
37 };
38 
39 class MutableArrayRefImpl : public WritableBinaryStream {
40 public:
41  MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
42  : BBS(Data, Endian) {}
43 
44  // Inherited via WritableBinaryStream
45  llvm::support::endianness getEndian() const override {
46  return BBS.getEndian();
47  }
48  Error readBytes(uint32_t Offset, uint32_t Size,
49  ArrayRef<uint8_t> &Buffer) override {
50  return BBS.readBytes(Offset, Size, Buffer);
51  }
52  Error readLongestContiguousChunk(uint32_t Offset,
53  ArrayRef<uint8_t> &Buffer) override {
54  return BBS.readLongestContiguousChunk(Offset, Buffer);
55  }
56  uint32_t getLength() override { return BBS.getLength(); }
57 
58  Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
59  return BBS.writeBytes(Offset, Data);
60  }
61  Error commit() override { return BBS.commit(); }
62 
63 private:
65 };
66 }
67 
69  : BinaryStreamRef(Stream, 0, Stream.getLength()) {}
71  uint32_t Length)
72  : BinaryStreamRefBase(Stream, Offset, Length) {}
74  : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
75  Data.size()) {}
77  : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
78  Endian) {}
79 
81  : BinaryStreamRefBase(Other) {}
82 
84  ArrayRef<uint8_t> &Buffer) const {
85  if (auto EC = checkOffset(Offset, Size))
86  return EC;
87  return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
88 }
89 
91  uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
92  if (auto EC = checkOffset(Offset, 1))
93  return EC;
94 
95  if (auto EC =
97  return EC;
98  // This StreamRef might refer to a smaller window over a larger stream. In
99  // that case we will have read out more bytes than we should return, because
100  // we should not read past the end of the current view.
101  uint32_t MaxLength = Length - Offset;
102  if (Buffer.size() > MaxLength)
103  Buffer = Buffer.slice(0, MaxLength);
104  return Error::success();
105 }
106 
108  : WritableBinaryStreamRef(Stream, 0, Stream.getLength()) {}
109 
111  uint32_t Offset,
112  uint32_t Length)
113  : BinaryStreamRefBase(Stream, Offset, Length) {}
114 
116  endianness Endian)
117  : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
118  0, Data.size()) {}
119 
121  const WritableBinaryStreamRef &Other)
122  : BinaryStreamRefBase(Other) {}
123 
125  ArrayRef<uint8_t> Data) const {
126  if (auto EC = checkOffset(Offset, Data.size()))
127  return EC;
128 
129  return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
130 }
131 
132 WritableBinaryStreamRef::operator BinaryStreamRef() const {
133  return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
134 }
135 
136 /// \brief For buffered streams, commits changes to the backing store.
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
An implementation of BinaryStream which holds its entire data set in a single contiguous buffer...
virtual Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer)=0
Given an offset into the stream and a number of bytes, attempt to read the bytes and set the output A...
Common stuff for mutable and immutable StreamRefs.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:179
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
virtual Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data)=0
Attempt to write the given bytes into the stream at the desired offset.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
virtual Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer)=0
Given an offset into the stream, read as much as possible without copying any data.
An interface for accessing data in a stream-like format, but which discourages copying.
Definition: BinaryStream.h:28
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...
Error commit()
For buffered streams, commits changes to the backing store.
static ErrorSuccess success()
Create a success value.
Definition: Error.h:311
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
virtual Error commit()=0
For buffered streams, commits changes to the backing store.
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 ...
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...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Error checkOffset(uint32_t Offset, uint32_t DataSize) const
A BinaryStream which can be read from as well as written to.
Definition: BinaryStream.h:63