BinaryStream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- BinaryStream.h - Base interface for a stream of data -----*- C++ -*-===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_SUPPORT_BINARYSTREAM_H
  9. #define LLVM_SUPPORT_BINARYSTREAM_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/BitmaskEnum.h"
  12. #include "llvm/Support/BinaryStreamError.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/Error.h"
  15. #include <cstdint>
  16. namespace llvm {
  17. enum BinaryStreamFlags {
  18. BSF_None = 0,
  19. BSF_Write = 1, // Stream supports writing.
  20. BSF_Append = 2, // Writing can occur at offset == length.
  21. LLVM_MARK_AS_BITMASK_ENUM(/* LargestValue = */ BSF_Append)
  22. };
  23. /// An interface for accessing data in a stream-like format, but which
  24. /// discourages copying. Instead of specifying a buffer in which to copy
  25. /// data on a read, the API returns an ArrayRef to data owned by the stream's
  26. /// implementation. Since implementations may not necessarily store data in a
  27. /// single contiguous buffer (or even in memory at all), in such cases a it may
  28. /// be necessary for an implementation to cache such a buffer so that it can
  29. /// return it.
  30. class BinaryStream {
  31. public:
  32. virtual ~BinaryStream() = default;
  33. virtual llvm::support::endianness getEndian() const = 0;
  34. /// Given an offset into the stream and a number of bytes, attempt to
  35. /// read the bytes and set the output ArrayRef to point to data owned by the
  36. /// stream.
  37. virtual Error readBytes(uint32_t Offset, uint32_t Size,
  38. ArrayRef<uint8_t> &Buffer) = 0;
  39. /// Given an offset into the stream, read as much as possible without
  40. /// copying any data.
  41. virtual Error readLongestContiguousChunk(uint32_t Offset,
  42. ArrayRef<uint8_t> &Buffer) = 0;
  43. /// Return the number of bytes of data in this stream.
  44. virtual uint32_t getLength() = 0;
  45. /// Return the properties of this stream.
  46. virtual BinaryStreamFlags getFlags() const { return BSF_None; }
  47. protected:
  48. Error checkOffsetForRead(uint32_t Offset, uint32_t DataSize) {
  49. if (Offset > getLength())
  50. return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
  51. if (getLength() < DataSize + Offset)
  52. return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
  53. return Error::success();
  54. }
  55. };
  56. /// A BinaryStream which can be read from as well as written to. Note
  57. /// that writing to a BinaryStream always necessitates copying from the input
  58. /// buffer to the stream's backing store. Streams are assumed to be buffered
  59. /// so that to be portable it is necessary to call commit() on the stream when
  60. /// all data has been written.
  61. class WritableBinaryStream : public BinaryStream {
  62. public:
  63. ~WritableBinaryStream() override = default;
  64. /// Attempt to write the given bytes into the stream at the desired
  65. /// offset. This will always necessitate a copy. Cannot shrink or grow the
  66. /// stream, only writes into existing allocated space.
  67. virtual Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) = 0;
  68. /// For buffered streams, commits changes to the backing store.
  69. virtual Error commit() = 0;
  70. /// Return the properties of this stream.
  71. BinaryStreamFlags getFlags() const override { return BSF_Write; }
  72. protected:
  73. Error checkOffsetForWrite(uint32_t Offset, uint32_t DataSize) {
  74. if (!(getFlags() & BSF_Append))
  75. return checkOffsetForRead(Offset, DataSize);
  76. if (Offset > getLength())
  77. return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
  78. return Error::success();
  79. }
  80. };
  81. } // end namespace llvm
  82. #endif // LLVM_SUPPORT_BINARYSTREAM_H