/src/botan/build/include/public/botan/data_src.h
Line | Count | Source |
1 | | /* |
2 | | * DataSource |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * 2012 Markus Wanner |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_DATA_SRC_H_ |
10 | | #define BOTAN_DATA_SRC_H_ |
11 | | |
12 | | #include <botan/secmem.h> |
13 | | #include <iosfwd> |
14 | | #include <memory> |
15 | | #include <optional> |
16 | | #include <span> |
17 | | #include <string> |
18 | | #include <string_view> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | /** |
23 | | * This class represents an abstract data source object. |
24 | | */ |
25 | | class BOTAN_PUBLIC_API(2, 0) DataSource { |
26 | | public: |
27 | | /** |
28 | | * Read from the source. Moves the internal offset so that every |
29 | | * call to read will return a new portion of the source. |
30 | | * |
31 | | * @param out the byte array to write the result to |
32 | | * @param length the length of the byte array out |
33 | | * @return length in bytes that was actually read and put |
34 | | * into out |
35 | | */ |
36 | | [[nodiscard]] virtual size_t read(uint8_t out[], size_t length) = 0; |
37 | | |
38 | | virtual bool check_available(size_t n) = 0; |
39 | | |
40 | | /** |
41 | | * Read from the source but do not modify the internal |
42 | | * offset. Consecutive calls to peek() will return portions of |
43 | | * the source starting at the same position. |
44 | | * |
45 | | * @param out the byte array to write the output to |
46 | | * @param length the length of the byte array out |
47 | | * @param peek_offset the offset into the stream to read at |
48 | | * @return length in bytes that was actually read and put |
49 | | * into out |
50 | | */ |
51 | | [[nodiscard]] virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const = 0; |
52 | | |
53 | | /** |
54 | | * Test whether the source still has data that can be read. |
55 | | * @return true if there is no more data to read, false otherwise |
56 | | */ |
57 | | virtual bool end_of_data() const = 0; |
58 | | |
59 | | /** |
60 | | * return the id of this data source |
61 | | * @return std::string representing the id of this data source |
62 | | */ |
63 | 0 | virtual std::string id() const { return ""; } |
64 | | |
65 | | /** |
66 | | * Read one byte. |
67 | | * @param out the byte to read to |
68 | | * @return length in bytes that was actually read and put |
69 | | * into out |
70 | | */ |
71 | | size_t read_byte(uint8_t& out); |
72 | | |
73 | | /** |
74 | | * Read one byte. |
75 | | * |
76 | | * Returns nullopt if no further bytes are available |
77 | | */ |
78 | | std::optional<uint8_t> read_byte(); |
79 | | |
80 | | /** |
81 | | * Peek at one byte. |
82 | | * @param out an output byte |
83 | | * @return length in bytes that was actually read and put |
84 | | * into out |
85 | | */ |
86 | | size_t peek_byte(uint8_t& out) const; |
87 | | |
88 | | /** |
89 | | * Discard the next N bytes of the data |
90 | | * @param N the number of bytes to discard |
91 | | * @return number of bytes actually discarded |
92 | | */ |
93 | | size_t discard_next(size_t N); |
94 | | |
95 | | /** |
96 | | * @return number of bytes read so far. |
97 | | */ |
98 | | virtual size_t get_bytes_read() const = 0; |
99 | | |
100 | 968 | DataSource() = default; |
101 | 968 | virtual ~DataSource() = default; |
102 | | DataSource(const DataSource&) = delete; |
103 | | DataSource(DataSource&&) = default; |
104 | | DataSource& operator=(const DataSource&) = delete; |
105 | | DataSource& operator=(DataSource&&) = default; |
106 | | }; |
107 | | |
108 | | /** |
109 | | * This class represents a Memory-Based DataSource |
110 | | */ |
111 | | class BOTAN_PUBLIC_API(2, 0) DataSource_Memory final : public DataSource { |
112 | | public: |
113 | | size_t read(uint8_t buf[], size_t length) override; |
114 | | size_t peek(uint8_t buf[], size_t length, size_t offset) const override; |
115 | | bool check_available(size_t n) override; |
116 | | bool end_of_data() const override; |
117 | | |
118 | | /** |
119 | | * Construct a memory source that reads from a string |
120 | | * @param in the string to read from |
121 | | */ |
122 | | explicit DataSource_Memory(std::string_view in); |
123 | | |
124 | | /** |
125 | | * Construct a memory source that reads from a byte array |
126 | | * @param in the byte array to read from |
127 | | * @param length the length of the byte array |
128 | | */ |
129 | 484 | DataSource_Memory(const uint8_t in[], size_t length) : m_source(in, in + length), m_offset(0) {} |
130 | | |
131 | | /** |
132 | | * Construct a memory source that reads from a secure_vector |
133 | | * @param in the MemoryRegion to read from |
134 | | */ |
135 | 0 | explicit DataSource_Memory(secure_vector<uint8_t> in) : m_source(std::move(in)), m_offset(0) {} |
136 | | |
137 | | /** |
138 | | * Construct a memory source that reads from an arbitrary byte buffer |
139 | | * @param in the MemoryRegion to read from |
140 | | */ |
141 | 0 | explicit DataSource_Memory(std::span<const uint8_t> in) : m_source(in.begin(), in.end()), m_offset(0) {} |
142 | | |
143 | | /** |
144 | | * Construct a memory source that reads from a std::vector |
145 | | * @param in the MemoryRegion to read from |
146 | | */ |
147 | 0 | explicit DataSource_Memory(const std::vector<uint8_t>& in) : m_source(in.begin(), in.end()), m_offset(0) {} |
148 | | |
149 | 0 | size_t get_bytes_read() const override { return m_offset; } |
150 | | |
151 | | private: |
152 | | secure_vector<uint8_t> m_source; |
153 | | size_t m_offset; |
154 | | }; |
155 | | |
156 | | /** |
157 | | * This class represents a Stream-Based DataSource. |
158 | | */ |
159 | | class BOTAN_PUBLIC_API(2, 0) DataSource_Stream final : public DataSource { |
160 | | public: |
161 | | size_t read(uint8_t buf[], size_t length) override; |
162 | | size_t peek(uint8_t buf[], size_t length, size_t offset) const override; |
163 | | bool check_available(size_t n) override; |
164 | | bool end_of_data() const override; |
165 | | std::string id() const override; |
166 | | |
167 | | BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::istream& in, std::string_view id = "<std::istream>"); |
168 | | |
169 | | #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) |
170 | | /** |
171 | | * Construct a Stream-Based DataSource from filesystem path |
172 | | * @param filename the path to the file |
173 | | * @param use_binary whether to treat the file as binary or not |
174 | | */ |
175 | | BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::string_view filename, bool use_binary = false); |
176 | | #endif |
177 | | |
178 | | DataSource_Stream(const DataSource_Stream&) = delete; |
179 | | DataSource_Stream(DataSource_Stream&&) = delete; |
180 | | DataSource_Stream& operator=(const DataSource_Stream&) = delete; |
181 | | DataSource_Stream& operator=(DataSource_Stream&&) = delete; |
182 | | |
183 | | ~DataSource_Stream() override; |
184 | | |
185 | 0 | size_t get_bytes_read() const override { return m_total_read; } |
186 | | |
187 | | private: |
188 | | const std::string m_identifier; |
189 | | |
190 | | std::unique_ptr<std::istream> m_source_memory; |
191 | | std::istream& m_source; |
192 | | size_t m_total_read; |
193 | | }; |
194 | | |
195 | | } // namespace Botan |
196 | | |
197 | | #endif |