Changeset View
Changeset View
Standalone View
Standalone View
llvm/include/llvm/Support/BinaryStream.h
Show All 35 Lines | |||||
public: | public: | ||||
virtual ~BinaryStream() = default; | virtual ~BinaryStream() = default; | ||||
virtual llvm::support::endianness getEndian() const = 0; | virtual llvm::support::endianness getEndian() const = 0; | ||||
/// Given an offset into the stream and a number of bytes, attempt to | /// Given an offset into the stream and a number of bytes, attempt to | ||||
/// read the bytes and set the output ArrayRef to point to data owned by the | /// read the bytes and set the output ArrayRef to point to data owned by the | ||||
/// stream. | /// stream. | ||||
virtual Error readBytes(uint32_t Offset, uint32_t Size, | virtual Error readBytes(uint64_t Offset, uint64_t Size, | ||||
ArrayRef<uint8_t> &Buffer) = 0; | ArrayRef<uint8_t> &Buffer) = 0; | ||||
/// Given an offset into the stream, read as much as possible without | /// Given an offset into the stream, read as much as possible without | ||||
/// copying any data. | /// copying any data. | ||||
virtual Error readLongestContiguousChunk(uint32_t Offset, | virtual Error readLongestContiguousChunk(uint64_t Offset, | ||||
ArrayRef<uint8_t> &Buffer) = 0; | ArrayRef<uint8_t> &Buffer) = 0; | ||||
/// Return the number of bytes of data in this stream. | /// Return the number of bytes of data in this stream. | ||||
virtual uint32_t getLength() = 0; | virtual uint64_t getLength() = 0; | ||||
/// Return the properties of this stream. | /// Return the properties of this stream. | ||||
virtual BinaryStreamFlags getFlags() const { return BSF_None; } | virtual BinaryStreamFlags getFlags() const { return BSF_None; } | ||||
protected: | protected: | ||||
Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) { | Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) { | ||||
if (Offset > getLength()) | if (Offset > getLength()) | ||||
return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | ||||
if (getLength() < DataSize + Offset) | if (getLength() < DataSize + Offset) | ||||
return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
}; | }; | ||||
/// A BinaryStream which can be read from as well as written to. Note | /// A BinaryStream which can be read from as well as written to. Note | ||||
/// that writing to a BinaryStream always necessitates copying from the input | /// that writing to a BinaryStream always necessitates copying from the input | ||||
/// buffer to the stream's backing store. Streams are assumed to be buffered | /// buffer to the stream's backing store. Streams are assumed to be buffered | ||||
/// so that to be portable it is necessary to call commit() on the stream when | /// so that to be portable it is necessary to call commit() on the stream when | ||||
/// all data has been written. | /// all data has been written. | ||||
class WritableBinaryStream : public BinaryStream { | class WritableBinaryStream : public BinaryStream { | ||||
public: | public: | ||||
~WritableBinaryStream() override = default; | ~WritableBinaryStream() override = default; | ||||
/// Attempt to write the given bytes into the stream at the desired | /// Attempt to write the given bytes into the stream at the desired | ||||
/// offset. This will always necessitate a copy. Cannot shrink or grow the | /// offset. This will always necessitate a copy. Cannot shrink or grow the | ||||
/// stream, only writes into existing allocated space. | /// stream, only writes into existing allocated space. | ||||
virtual Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) = 0; | virtual Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Data) = 0; | ||||
/// For buffered streams, commits changes to the backing store. | /// For buffered streams, commits changes to the backing store. | ||||
virtual Error commit() = 0; | virtual Error commit() = 0; | ||||
/// Return the properties of this stream. | /// Return the properties of this stream. | ||||
BinaryStreamFlags getFlags() const override { return BSF_Write; } | BinaryStreamFlags getFlags() const override { return BSF_Write; } | ||||
protected: | protected: | ||||
Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) { | Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize) { | ||||
if (!(getFlags() & BSF_Append)) | if (!(getFlags() & BSF_Append)) | ||||
return checkOffsetForRead(Offset, DataSize); | return checkOffsetForRead(Offset, DataSize); | ||||
if (Offset > getLength()) | if (Offset > getLength()) | ||||
return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | return make_error<BinaryStreamError>(stream_error_code::invalid_offset); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
}; | }; | ||||
} // end namespace llvm | } // end namespace llvm | ||||
#endif // LLVM_SUPPORT_BINARYSTREAM_H | #endif // LLVM_SUPPORT_BINARYSTREAM_H |