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