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