Changeset View
Changeset View
Standalone View
Standalone View
llvm/include/llvm/Support/BinaryStreamRef.h
Show All 24 Lines | |||||
protected: | protected: | ||||
BinaryStreamRefBase() = default; | BinaryStreamRefBase() = default; | ||||
explicit BinaryStreamRefBase(StreamType &BorrowedImpl) | explicit BinaryStreamRefBase(StreamType &BorrowedImpl) | ||||
: BorrowedImpl(&BorrowedImpl), ViewOffset(0) { | : BorrowedImpl(&BorrowedImpl), ViewOffset(0) { | ||||
if (!(BorrowedImpl.getFlags() & BSF_Append)) | if (!(BorrowedImpl.getFlags() & BSF_Append)) | ||||
Length = BorrowedImpl.getLength(); | Length = BorrowedImpl.getLength(); | ||||
} | } | ||||
BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint32_t Offset, | BinaryStreamRefBase(std::shared_ptr<StreamType> SharedImpl, uint64_t Offset, | ||||
Optional<uint32_t> Length) | Optional<uint64_t> Length) | ||||
: SharedImpl(SharedImpl), BorrowedImpl(SharedImpl.get()), | : SharedImpl(SharedImpl), BorrowedImpl(SharedImpl.get()), | ||||
ViewOffset(Offset), Length(Length) {} | ViewOffset(Offset), Length(Length) {} | ||||
BinaryStreamRefBase(StreamType &BorrowedImpl, uint32_t Offset, | BinaryStreamRefBase(StreamType &BorrowedImpl, uint64_t Offset, | ||||
Optional<uint32_t> Length) | Optional<uint64_t> Length) | ||||
: BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {} | : BorrowedImpl(&BorrowedImpl), ViewOffset(Offset), Length(Length) {} | ||||
BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default; | BinaryStreamRefBase(const BinaryStreamRefBase &Other) = default; | ||||
BinaryStreamRefBase &operator=(const BinaryStreamRefBase &Other) = default; | BinaryStreamRefBase &operator=(const BinaryStreamRefBase &Other) = default; | ||||
BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default; | BinaryStreamRefBase &operator=(BinaryStreamRefBase &&Other) = default; | ||||
BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default; | BinaryStreamRefBase(BinaryStreamRefBase &&Other) = default; | ||||
public: | public: | ||||
llvm::support::endianness getEndian() const { | llvm::support::endianness getEndian() const { | ||||
return BorrowedImpl->getEndian(); | return BorrowedImpl->getEndian(); | ||||
} | } | ||||
uint32_t getLength() const { | uint64_t getLength() const { | ||||
if (Length.hasValue()) | if (Length.hasValue()) | ||||
return *Length; | return *Length; | ||||
return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0; | return BorrowedImpl ? (BorrowedImpl->getLength() - ViewOffset) : 0; | ||||
} | } | ||||
/// Return a new BinaryStreamRef with the first \p N elements removed. If | /// Return a new BinaryStreamRef with the first \p N elements removed. If | ||||
/// this BinaryStreamRef is length-tracking, then the resulting one will be | /// this BinaryStreamRef is length-tracking, then the resulting one will be | ||||
/// too. | /// too. | ||||
RefType drop_front(uint32_t N) const { | RefType drop_front(uint64_t N) const { | ||||
if (!BorrowedImpl) | if (!BorrowedImpl) | ||||
return RefType(); | return RefType(); | ||||
N = std::min(N, getLength()); | N = std::min(N, getLength()); | ||||
RefType Result(static_cast<const RefType &>(*this)); | RefType Result(static_cast<const RefType &>(*this)); | ||||
if (N == 0) | if (N == 0) | ||||
return Result; | return Result; | ||||
Result.ViewOffset += N; | Result.ViewOffset += N; | ||||
if (Result.Length.hasValue()) | if (Result.Length.hasValue()) | ||||
*Result.Length -= N; | *Result.Length -= N; | ||||
return Result; | return Result; | ||||
} | } | ||||
/// Return a new BinaryStreamRef with the last \p N elements removed. If | /// Return a new BinaryStreamRef with the last \p N elements removed. If | ||||
/// this BinaryStreamRef is length-tracking and \p N is greater than 0, then | /// this BinaryStreamRef is length-tracking and \p N is greater than 0, then | ||||
/// this BinaryStreamRef will no longer length-track. | /// this BinaryStreamRef will no longer length-track. | ||||
RefType drop_back(uint32_t N) const { | RefType drop_back(uint64_t N) const { | ||||
if (!BorrowedImpl) | if (!BorrowedImpl) | ||||
return RefType(); | return RefType(); | ||||
RefType Result(static_cast<const RefType &>(*this)); | RefType Result(static_cast<const RefType &>(*this)); | ||||
N = std::min(N, getLength()); | N = std::min(N, getLength()); | ||||
if (N == 0) | if (N == 0) | ||||
return Result; | return Result; | ||||
// Since we're dropping non-zero bytes from the end, stop length-tracking | // Since we're dropping non-zero bytes from the end, stop length-tracking | ||||
// by setting the length of the resulting StreamRef to an explicit value. | // by setting the length of the resulting StreamRef to an explicit value. | ||||
if (!Result.Length.hasValue()) | if (!Result.Length.hasValue()) | ||||
Result.Length = getLength(); | Result.Length = getLength(); | ||||
*Result.Length -= N; | *Result.Length -= N; | ||||
return Result; | return Result; | ||||
} | } | ||||
/// Return a new BinaryStreamRef with only the first \p N elements remaining. | /// Return a new BinaryStreamRef with only the first \p N elements remaining. | ||||
RefType keep_front(uint32_t N) const { | RefType keep_front(uint64_t N) const { | ||||
assert(N <= getLength()); | assert(N <= getLength()); | ||||
return drop_back(getLength() - N); | return drop_back(getLength() - N); | ||||
} | } | ||||
/// Return a new BinaryStreamRef with only the last \p N elements remaining. | /// Return a new BinaryStreamRef with only the last \p N elements remaining. | ||||
RefType keep_back(uint32_t N) const { | RefType keep_back(uint64_t N) const { | ||||
assert(N <= getLength()); | assert(N <= getLength()); | ||||
return drop_front(getLength() - N); | return drop_front(getLength() - N); | ||||
} | } | ||||
/// Return a new BinaryStreamRef with the first and last \p N elements | /// Return a new BinaryStreamRef with the first and last \p N elements | ||||
/// removed. | /// removed. | ||||
RefType drop_symmetric(uint32_t N) const { | RefType drop_symmetric(uint64_t N) const { | ||||
return drop_front(N).drop_back(N); | return drop_front(N).drop_back(N); | ||||
} | } | ||||
/// Return a new BinaryStreamRef with the first \p Offset elements removed, | /// Return a new BinaryStreamRef with the first \p Offset elements removed, | ||||
/// and retaining exactly \p Len elements. | /// and retaining exactly \p Len elements. | ||||
RefType slice(uint32_t Offset, uint32_t Len) const { | RefType slice(uint64_t Offset, uint64_t Len) const { | ||||
return drop_front(Offset).keep_front(Len); | return drop_front(Offset).keep_front(Len); | ||||
} | } | ||||
bool valid() const { return BorrowedImpl != nullptr; } | bool valid() const { return BorrowedImpl != nullptr; } | ||||
friend bool operator==(const RefType &LHS, const RefType &RHS) { | friend bool operator==(const RefType &LHS, const RefType &RHS) { | ||||
if (LHS.BorrowedImpl != RHS.BorrowedImpl) | if (LHS.BorrowedImpl != RHS.BorrowedImpl) | ||||
return false; | return false; | ||||
if (LHS.ViewOffset != RHS.ViewOffset) | if (LHS.ViewOffset != RHS.ViewOffset) | ||||
return false; | return false; | ||||
if (LHS.Length != RHS.Length) | if (LHS.Length != RHS.Length) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
protected: | protected: | ||||
Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) const { | Error checkOffsetForRead(uint64_t Offset, uint64_t DataSize) const { | ||||
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(); | ||||
} | } | ||||
std::shared_ptr<StreamType> SharedImpl; | std::shared_ptr<StreamType> SharedImpl; | ||||
StreamType *BorrowedImpl = nullptr; | StreamType *BorrowedImpl = nullptr; | ||||
uint32_t ViewOffset = 0; | uint64_t ViewOffset = 0; | ||||
Optional<uint32_t> Length; | Optional<uint64_t> Length; | ||||
}; | }; | ||||
/// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It | /// BinaryStreamRef is to BinaryStream what ArrayRef is to an Array. It | ||||
/// provides copy-semantics and read only access to a "window" of the underlying | /// provides copy-semantics and read only access to a "window" of the underlying | ||||
/// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to | /// BinaryStream. Note that BinaryStreamRef is *not* a BinaryStream. That is to | ||||
/// say, it does not inherit and override the methods of BinaryStream. In | /// say, it does not inherit and override the methods of BinaryStream. In | ||||
/// general, you should not pass around pointers or references to BinaryStreams | /// general, you should not pass around pointers or references to BinaryStreams | ||||
/// and use inheritance to achieve polymorphism. Instead, you should pass | /// and use inheritance to achieve polymorphism. Instead, you should pass | ||||
/// around BinaryStreamRefs by value and achieve polymorphism that way. | /// around BinaryStreamRefs by value and achieve polymorphism that way. | ||||
class BinaryStreamRef | class BinaryStreamRef | ||||
: public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> { | : public BinaryStreamRefBase<BinaryStreamRef, BinaryStream> { | ||||
friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>; | friend BinaryStreamRefBase<BinaryStreamRef, BinaryStream>; | ||||
friend class WritableBinaryStreamRef; | friend class WritableBinaryStreamRef; | ||||
BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint32_t ViewOffset, | BinaryStreamRef(std::shared_ptr<BinaryStream> Impl, uint64_t ViewOffset, | ||||
Optional<uint32_t> Length) | Optional<uint64_t> Length) | ||||
: BinaryStreamRefBase(Impl, ViewOffset, Length) {} | : BinaryStreamRefBase(Impl, ViewOffset, Length) {} | ||||
public: | public: | ||||
BinaryStreamRef() = default; | BinaryStreamRef() = default; | ||||
BinaryStreamRef(BinaryStream &Stream); | BinaryStreamRef(BinaryStream &Stream); | ||||
BinaryStreamRef(BinaryStream &Stream, uint32_t Offset, | BinaryStreamRef(BinaryStream &Stream, uint64_t Offset, | ||||
Optional<uint32_t> Length); | Optional<uint64_t> Length); | ||||
explicit BinaryStreamRef(ArrayRef<uint8_t> Data, | explicit BinaryStreamRef(ArrayRef<uint8_t> Data, | ||||
llvm::support::endianness Endian); | llvm::support::endianness Endian); | ||||
explicit BinaryStreamRef(StringRef Data, llvm::support::endianness Endian); | explicit BinaryStreamRef(StringRef Data, llvm::support::endianness Endian); | ||||
BinaryStreamRef(const BinaryStreamRef &Other) = default; | BinaryStreamRef(const BinaryStreamRef &Other) = default; | ||||
BinaryStreamRef &operator=(const BinaryStreamRef &Other) = default; | BinaryStreamRef &operator=(const BinaryStreamRef &Other) = default; | ||||
BinaryStreamRef(BinaryStreamRef &&Other) = default; | BinaryStreamRef(BinaryStreamRef &&Other) = default; | ||||
BinaryStreamRef &operator=(BinaryStreamRef &&Other) = default; | BinaryStreamRef &operator=(BinaryStreamRef &&Other) = default; | ||||
// Use BinaryStreamRef.slice() instead. | // Use BinaryStreamRef.slice() instead. | ||||
BinaryStreamRef(BinaryStreamRef &S, uint32_t Offset, | BinaryStreamRef(BinaryStreamRef &S, uint64_t Offset, | ||||
uint32_t Length) = delete; | uint64_t Length) = delete; | ||||
/// Given an Offset into this StreamRef and a Size, return a reference to a | /// Given an Offset into this StreamRef and a Size, return a reference to a | ||||
/// buffer owned by the stream. | /// buffer owned by the stream. | ||||
/// | /// | ||||
/// \returns a success error code if the entire range of data is within the | /// \returns a success error code if the entire range of data is within the | ||||
/// bounds of this BinaryStreamRef's view and the implementation could read | /// bounds of this BinaryStreamRef's view and the implementation could read | ||||
/// the data, and an appropriate error code otherwise. | /// the data, and an appropriate error code otherwise. | ||||
Error readBytes(uint32_t Offset, uint32_t Size, | Error readBytes(uint64_t Offset, uint64_t Size, | ||||
ArrayRef<uint8_t> &Buffer) const; | ArrayRef<uint8_t> &Buffer) const; | ||||
/// Given an Offset into this BinaryStreamRef, return a reference to the | /// Given an Offset into this BinaryStreamRef, return a reference to the | ||||
/// largest buffer the stream could support without necessitating a copy. | /// largest buffer the stream could support without necessitating a copy. | ||||
/// | /// | ||||
/// \returns a success error code if implementation could read the data, | /// \returns a success error code if implementation could read the data, | ||||
/// and an appropriate error code otherwise. | /// and an appropriate error code otherwise. | ||||
Error readLongestContiguousChunk(uint32_t Offset, | Error readLongestContiguousChunk(uint64_t Offset, | ||||
ArrayRef<uint8_t> &Buffer) const; | ArrayRef<uint8_t> &Buffer) const; | ||||
}; | }; | ||||
struct BinarySubstreamRef { | struct BinarySubstreamRef { | ||||
uint32_t Offset = 0; // Offset in the parent stream | uint64_t Offset = 0; // Offset in the parent stream | ||||
BinaryStreamRef StreamData; // Stream Data | BinaryStreamRef StreamData; // Stream Data | ||||
BinarySubstreamRef slice(uint32_t Off, uint32_t Size) const { | BinarySubstreamRef slice(uint64_t Off, uint64_t Size) const { | ||||
BinaryStreamRef SubSub = StreamData.slice(Off, Size); | BinaryStreamRef SubSub = StreamData.slice(Off, Size); | ||||
return {Off + Offset, SubSub}; | return {Off + Offset, SubSub}; | ||||
} | } | ||||
BinarySubstreamRef drop_front(uint32_t N) const { | BinarySubstreamRef drop_front(uint64_t N) const { | ||||
return slice(N, size() - N); | return slice(N, size() - N); | ||||
} | } | ||||
BinarySubstreamRef keep_front(uint32_t N) const { return slice(0, N); } | BinarySubstreamRef keep_front(uint64_t N) const { return slice(0, N); } | ||||
std::pair<BinarySubstreamRef, BinarySubstreamRef> | std::pair<BinarySubstreamRef, BinarySubstreamRef> split(uint64_t Off) const { | ||||
split(uint32_t Off) const { | |||||
return std::make_pair(keep_front(Off), drop_front(Off)); | return std::make_pair(keep_front(Off), drop_front(Off)); | ||||
} | } | ||||
uint32_t size() const { return StreamData.getLength(); } | uint64_t size() const { return StreamData.getLength(); } | ||||
bool empty() const { return size() == 0; } | bool empty() const { return size() == 0; } | ||||
}; | }; | ||||
class WritableBinaryStreamRef | class WritableBinaryStreamRef | ||||
: public BinaryStreamRefBase<WritableBinaryStreamRef, | : public BinaryStreamRefBase<WritableBinaryStreamRef, | ||||
WritableBinaryStream> { | WritableBinaryStream> { | ||||
friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>; | friend BinaryStreamRefBase<WritableBinaryStreamRef, WritableBinaryStream>; | ||||
WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl, | WritableBinaryStreamRef(std::shared_ptr<WritableBinaryStream> Impl, | ||||
uint32_t ViewOffset, Optional<uint32_t> Length) | uint64_t ViewOffset, Optional<uint64_t> Length) | ||||
: BinaryStreamRefBase(Impl, ViewOffset, Length) {} | : BinaryStreamRefBase(Impl, ViewOffset, Length) {} | ||||
Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) const { | Error checkOffsetForWrite(uint64_t Offset, uint64_t DataSize) const { | ||||
if (!(BorrowedImpl->getFlags() & BSF_Append)) | if (!(BorrowedImpl->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(); | ||||
} | } | ||||
public: | public: | ||||
WritableBinaryStreamRef() = default; | WritableBinaryStreamRef() = default; | ||||
WritableBinaryStreamRef(WritableBinaryStream &Stream); | WritableBinaryStreamRef(WritableBinaryStream &Stream); | ||||
WritableBinaryStreamRef(WritableBinaryStream &Stream, uint32_t Offset, | WritableBinaryStreamRef(WritableBinaryStream &Stream, uint64_t Offset, | ||||
Optional<uint32_t> Length); | Optional<uint64_t> Length); | ||||
explicit WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data, | explicit WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data, | ||||
llvm::support::endianness Endian); | llvm::support::endianness Endian); | ||||
WritableBinaryStreamRef(const WritableBinaryStreamRef &Other) = default; | WritableBinaryStreamRef(const WritableBinaryStreamRef &Other) = default; | ||||
WritableBinaryStreamRef & | WritableBinaryStreamRef & | ||||
operator=(const WritableBinaryStreamRef &Other) = default; | operator=(const WritableBinaryStreamRef &Other) = default; | ||||
WritableBinaryStreamRef(WritableBinaryStreamRef &&Other) = default; | WritableBinaryStreamRef(WritableBinaryStreamRef &&Other) = default; | ||||
WritableBinaryStreamRef &operator=(WritableBinaryStreamRef &&Other) = default; | WritableBinaryStreamRef &operator=(WritableBinaryStreamRef &&Other) = default; | ||||
// Use WritableBinaryStreamRef.slice() instead. | // Use WritableBinaryStreamRef.slice() instead. | ||||
WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint32_t Offset, | WritableBinaryStreamRef(WritableBinaryStreamRef &S, uint64_t Offset, | ||||
uint32_t Length) = delete; | uint64_t Length) = delete; | ||||
/// Given an Offset into this WritableBinaryStreamRef and some input data, | /// Given an Offset into this WritableBinaryStreamRef and some input data, | ||||
/// writes the data to the underlying stream. | /// writes the data to the underlying stream. | ||||
/// | /// | ||||
/// \returns a success error code if the data could fit within the underlying | /// \returns a success error code if the data could fit within the underlying | ||||
/// stream at the specified location and the implementation could write the | /// stream at the specified location and the implementation could write the | ||||
/// data, and an appropriate error code otherwise. | /// data, and an appropriate error code otherwise. | ||||
Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const; | Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Data) const; | ||||
/// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef. | /// Conver this WritableBinaryStreamRef to a read-only BinaryStreamRef. | ||||
operator BinaryStreamRef() const; | operator BinaryStreamRef() const; | ||||
/// For buffered streams, commits changes to the backing store. | /// For buffered streams, commits changes to the backing store. | ||||
Error commit(); | Error commit(); | ||||
}; | }; | ||||
} // end namespace llvm | } // end namespace llvm | ||||
#endif // LLVM_SUPPORT_BINARYSTREAMREF_H | #endif // LLVM_SUPPORT_BINARYSTREAMREF_H |