Botan 3.9.0
Crypto and TLS for C&
data_src.h
Go to the documentation of this file.
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
20namespace Botan {
21
22/**
23* This class represents an abstract data source object.
24*/
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 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 DataSource() = default;
101 virtual ~DataSource() = default;
102 DataSource(const DataSource&) = delete;
103 DataSource(DataSource&&) = default;
104 DataSource& operator=(const DataSource&) = delete;
106};
107
108/**
109* This class represents a Memory-Based DataSource
110*/
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 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 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 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 explicit DataSource_Memory(const std::vector<uint8_t>& in) : m_source(in.begin(), in.end()), m_offset(0) {}
148
149 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*/
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
182
184
185 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
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
DataSource_Memory(const std::vector< uint8_t > &in)
Definition data_src.h:147
DataSource_Memory(secure_vector< uint8_t > in)
Definition data_src.h:135
DataSource_Memory(std::string_view in)
Definition data_src.cpp:108
size_t read(uint8_t buf[], size_t length) override
Definition data_src.cpp:73
bool check_available(size_t n) override
Definition data_src.cpp:80
DataSource_Memory(std::span< const uint8_t > in)
Definition data_src.h:141
size_t peek(uint8_t buf[], size_t length, size_t offset) const override
Definition data_src.cpp:87
DataSource_Memory(const uint8_t in[], size_t length)
Definition data_src.h:129
size_t get_bytes_read() const override
Definition data_src.h:149
bool end_of_data() const override
Definition data_src.cpp:101
bool end_of_data() const override
Definition data_src.cpp:170
BOTAN_FUTURE_EXPLICIT DataSource_Stream(std::istream &in, std::string_view id="<std::istream>")
Definition data_src.cpp:201
size_t get_bytes_read() const override
Definition data_src.h:185
DataSource_Stream & operator=(DataSource_Stream &&)=delete
DataSource_Stream(const DataSource_Stream &)=delete
size_t read(uint8_t buf[], size_t length) override
Definition data_src.cpp:113
size_t peek(uint8_t buf[], size_t length, size_t offset) const override
Definition data_src.cpp:135
DataSource_Stream(DataSource_Stream &&)=delete
bool check_available(size_t n) override
Definition data_src.cpp:124
DataSource_Stream & operator=(const DataSource_Stream &)=delete
std::string id() const override
Definition data_src.cpp:177
virtual std::string id() const
Definition data_src.h:63
DataSource & operator=(DataSource &&)=default
DataSource(DataSource &&)=default
DataSource(const DataSource &)=delete
virtual size_t get_bytes_read() const =0
virtual ~DataSource()=default
DataSource()=default
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0
virtual size_t read(uint8_t out[], size_t length)=0
virtual bool check_available(size_t n)=0
virtual bool end_of_data() const =0
DataSource & operator=(const DataSource &)=delete
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69