1 //===- BinaryStreamRef.cpp - ----------------------------------------------===//
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 #include "llvm/Support/BinaryStreamRef.h"
11 #include "llvm/Support/BinaryByteStream.h"
14 using namespace llvm::support
;
18 class ArrayRefImpl
: public BinaryStream
{
20 ArrayRefImpl(ArrayRef
<uint8_t> Data
, endianness Endian
) : BBS(Data
, Endian
) {}
22 llvm::support::endianness
getEndian() const override
{
23 return BBS
.getEndian();
25 Error
readBytes(uint32_t Offset
, uint32_t Size
,
26 ArrayRef
<uint8_t> &Buffer
) override
{
27 return BBS
.readBytes(Offset
, Size
, Buffer
);
29 Error
readLongestContiguousChunk(uint32_t Offset
,
30 ArrayRef
<uint8_t> &Buffer
) override
{
31 return BBS
.readLongestContiguousChunk(Offset
, Buffer
);
33 uint32_t getLength() override
{ return BBS
.getLength(); }
39 class MutableArrayRefImpl
: public WritableBinaryStream
{
41 MutableArrayRefImpl(MutableArrayRef
<uint8_t> Data
, endianness Endian
)
42 : BBS(Data
, Endian
) {}
44 // Inherited via WritableBinaryStream
45 llvm::support::endianness
getEndian() const override
{
46 return BBS
.getEndian();
48 Error
readBytes(uint32_t Offset
, uint32_t Size
,
49 ArrayRef
<uint8_t> &Buffer
) override
{
50 return BBS
.readBytes(Offset
, Size
, Buffer
);
52 Error
readLongestContiguousChunk(uint32_t Offset
,
53 ArrayRef
<uint8_t> &Buffer
) override
{
54 return BBS
.readLongestContiguousChunk(Offset
, Buffer
);
56 uint32_t getLength() override
{ return BBS
.getLength(); }
58 Error
writeBytes(uint32_t Offset
, ArrayRef
<uint8_t> Data
) override
{
59 return BBS
.writeBytes(Offset
, Data
);
61 Error
commit() override
{ return BBS
.commit(); }
64 MutableBinaryByteStream BBS
;
68 BinaryStreamRef::BinaryStreamRef(BinaryStream
&Stream
)
69 : BinaryStreamRefBase(Stream
) {}
70 BinaryStreamRef::BinaryStreamRef(BinaryStream
&Stream
, uint32_t Offset
,
71 Optional
<uint32_t> Length
)
72 : BinaryStreamRefBase(Stream
, Offset
, Length
) {}
73 BinaryStreamRef::BinaryStreamRef(ArrayRef
<uint8_t> Data
, endianness Endian
)
74 : BinaryStreamRefBase(std::make_shared
<ArrayRefImpl
>(Data
, Endian
), 0,
76 BinaryStreamRef::BinaryStreamRef(StringRef Data
, endianness Endian
)
77 : BinaryStreamRef(makeArrayRef(Data
.bytes_begin(), Data
.bytes_end()),
80 Error
BinaryStreamRef::readBytes(uint32_t Offset
, uint32_t Size
,
81 ArrayRef
<uint8_t> &Buffer
) const {
82 if (auto EC
= checkOffsetForRead(Offset
, Size
))
84 return BorrowedImpl
->readBytes(ViewOffset
+ Offset
, Size
, Buffer
);
87 Error
BinaryStreamRef::readLongestContiguousChunk(
88 uint32_t Offset
, ArrayRef
<uint8_t> &Buffer
) const {
89 if (auto EC
= checkOffsetForRead(Offset
, 1))
93 BorrowedImpl
->readLongestContiguousChunk(ViewOffset
+ Offset
, Buffer
))
95 // This StreamRef might refer to a smaller window over a larger stream. In
96 // that case we will have read out more bytes than we should return, because
97 // we should not read past the end of the current view.
98 uint32_t MaxLength
= getLength() - Offset
;
99 if (Buffer
.size() > MaxLength
)
100 Buffer
= Buffer
.slice(0, MaxLength
);
101 return Error::success();
104 WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream
&Stream
)
105 : BinaryStreamRefBase(Stream
) {}
107 WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream
&Stream
,
109 Optional
<uint32_t> Length
)
110 : BinaryStreamRefBase(Stream
, Offset
, Length
) {}
112 WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef
<uint8_t> Data
,
114 : BinaryStreamRefBase(std::make_shared
<MutableArrayRefImpl
>(Data
, Endian
),
118 Error
WritableBinaryStreamRef::writeBytes(uint32_t Offset
,
119 ArrayRef
<uint8_t> Data
) const {
120 if (auto EC
= checkOffsetForWrite(Offset
, Data
.size()))
123 return BorrowedImpl
->writeBytes(ViewOffset
+ Offset
, Data
);
126 WritableBinaryStreamRef::operator BinaryStreamRef() const {
127 return BinaryStreamRef(*BorrowedImpl
, ViewOffset
, Length
);
130 /// For buffered streams, commits changes to the backing store.
131 Error
WritableBinaryStreamRef::commit() { return BorrowedImpl
->commit(); }