BinaryStream.h 3.9 KB

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