blob: 6bcc504ffad5fa8763de554930462e63e7193e9c [file] [log] [blame]
Zachary Turnerd9a62632017-05-17 20:23:31 +00001//===- BinaryStreamRef.cpp - ----------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnerd9a62632017-05-17 20:23:31 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/BinaryStreamRef.h"
10#include "llvm/Support/BinaryByteStream.h"
11
12using namespace llvm;
13using namespace llvm::support;
14
15namespace {
16
17class ArrayRefImpl : public BinaryStream {
18public:
19 ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
20
21 llvm::support::endianness getEndian() const override {
22 return BBS.getEndian();
23 }
24 Error readBytes(uint32_t Offset, uint32_t Size,
25 ArrayRef<uint8_t> &Buffer) override {
26 return BBS.readBytes(Offset, Size, Buffer);
27 }
28 Error readLongestContiguousChunk(uint32_t Offset,
29 ArrayRef<uint8_t> &Buffer) override {
30 return BBS.readLongestContiguousChunk(Offset, Buffer);
31 }
32 uint32_t getLength() override { return BBS.getLength(); }
33
34private:
35 BinaryByteStream BBS;
36};
37
38class MutableArrayRefImpl : public WritableBinaryStream {
39public:
40 MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
41 : BBS(Data, Endian) {}
42
43 // Inherited via WritableBinaryStream
44 llvm::support::endianness getEndian() const override {
45 return BBS.getEndian();
46 }
47 Error readBytes(uint32_t Offset, uint32_t Size,
48 ArrayRef<uint8_t> &Buffer) override {
49 return BBS.readBytes(Offset, Size, Buffer);
50 }
51 Error readLongestContiguousChunk(uint32_t Offset,
52 ArrayRef<uint8_t> &Buffer) override {
53 return BBS.readLongestContiguousChunk(Offset, Buffer);
54 }
55 uint32_t getLength() override { return BBS.getLength(); }
56
57 Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
58 return BBS.writeBytes(Offset, Data);
59 }
60 Error commit() override { return BBS.commit(); }
61
62private:
63 MutableBinaryByteStream BBS;
64};
65}
66
67BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
Zachary Turner96c69852017-11-27 18:48:37 +000068 : BinaryStreamRefBase(Stream) {}
Zachary Turnerd9a62632017-05-17 20:23:31 +000069BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream, uint32_t Offset,
Zachary Turner96c69852017-11-27 18:48:37 +000070 Optional<uint32_t> Length)
Zachary Turnerd9a62632017-05-17 20:23:31 +000071 : BinaryStreamRefBase(Stream, Offset, Length) {}
72BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t> Data, endianness Endian)
73 : BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
74 Data.size()) {}
75BinaryStreamRef::BinaryStreamRef(StringRef Data, endianness Endian)
76 : BinaryStreamRef(makeArrayRef(Data.bytes_begin(), Data.bytes_end()),
77 Endian) {}
78
Zachary Turnerd9a62632017-05-17 20:23:31 +000079Error BinaryStreamRef::readBytes(uint32_t Offset, uint32_t Size,
80 ArrayRef<uint8_t> &Buffer) const {
Zachary Turner96c69852017-11-27 18:48:37 +000081 if (auto EC = checkOffsetForRead(Offset, Size))
Zachary Turnerd9a62632017-05-17 20:23:31 +000082 return EC;
83 return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
84}
85
86Error BinaryStreamRef::readLongestContiguousChunk(
87 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
Zachary Turner96c69852017-11-27 18:48:37 +000088 if (auto EC = checkOffsetForRead(Offset, 1))
Zachary Turnerd9a62632017-05-17 20:23:31 +000089 return EC;
90
91 if (auto EC =
92 BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
93 return EC;
94 // This StreamRef might refer to a smaller window over a larger stream. In
95 // that case we will have read out more bytes than we should return, because
96 // we should not read past the end of the current view.
Zachary Turner96c69852017-11-27 18:48:37 +000097 uint32_t MaxLength = getLength() - Offset;
Zachary Turnerd9a62632017-05-17 20:23:31 +000098 if (Buffer.size() > MaxLength)
99 Buffer = Buffer.slice(0, MaxLength);
100 return Error::success();
101}
102
103WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
Zachary Turner96c69852017-11-27 18:48:37 +0000104 : BinaryStreamRefBase(Stream) {}
Zachary Turnerd9a62632017-05-17 20:23:31 +0000105
106WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
107 uint32_t Offset,
Zachary Turner96c69852017-11-27 18:48:37 +0000108 Optional<uint32_t> Length)
Zachary Turnerd9a62632017-05-17 20:23:31 +0000109 : BinaryStreamRefBase(Stream, Offset, Length) {}
110
111WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
112 endianness Endian)
113 : BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
114 0, Data.size()) {}
115
Zachary Turnerd9a62632017-05-17 20:23:31 +0000116
117Error WritableBinaryStreamRef::writeBytes(uint32_t Offset,
118 ArrayRef<uint8_t> Data) const {
Zachary Turner96c69852017-11-27 18:48:37 +0000119 if (auto EC = checkOffsetForWrite(Offset, Data.size()))
Zachary Turnerd9a62632017-05-17 20:23:31 +0000120 return EC;
121
122 return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
123}
124
125WritableBinaryStreamRef::operator BinaryStreamRef() const {
126 return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
127}
128
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000129/// For buffered streams, commits changes to the backing store.
Zachary Turnerd9a62632017-05-17 20:23:31 +0000130Error WritableBinaryStreamRef::commit() { return BorrowedImpl->commit(); }