annotate include/llvm/Support/BinaryStream.h @ 148:63bd29f05246

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