3
* (C) 1999-2007 Jack Lloyd
6
* Botan is released under the Simplified BSD License (see license.txt)
9
#ifndef BOTAN_DATA_SRC_H_
10
#define BOTAN_DATA_SRC_H_
12
#include <botan/secmem.h>
19
* This class represents an abstract data source object.
21
class BOTAN_PUBLIC_API(2,0) DataSource
25
* Read from the source. Moves the internal offset so that every
26
* call to read will return a new portion of the source.
28
* @param out the byte array to write the result to
29
* @param length the length of the byte array out
30
* @return length in bytes that was actually read and put
33
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT = 0;
35
virtual bool check_available(size_t n) = 0;
38
* Read from the source but do not modify the internal
39
* offset. Consecutive calls to peek() will return portions of
40
* the source starting at the same position.
42
* @param out the byte array to write the output to
43
* @param length the length of the byte array out
44
* @param peek_offset the offset into the stream to read at
45
* @return length in bytes that was actually read and put
48
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT = 0;
51
* Test whether the source still has data that can be read.
52
* @return true if there is still data to read, false otherwise
54
virtual bool end_of_data() const = 0;
56
* return the id of this data source
57
* @return std::string representing the id of this data source
59
virtual std::string id() const { return ""; }
63
* @param out the byte to read to
64
* @return length in bytes that was actually read and put
67
size_t read_byte(uint8_t& out);
71
* @param out an output byte
72
* @return length in bytes that was actually read and put
75
size_t peek_byte(uint8_t& out) const;
78
* Discard the next N bytes of the data
79
* @param N the number of bytes to discard
80
* @return number of bytes actually discarded
82
size_t discard_next(size_t N);
85
* @return number of bytes read so far.
87
virtual size_t get_bytes_read() const = 0;
89
DataSource() = default;
90
virtual ~DataSource() = default;
91
DataSource& operator=(const DataSource&) = delete;
92
DataSource(const DataSource&) = delete;
96
* This class represents a Memory-Based DataSource
98
class BOTAN_PUBLIC_API(2,0) DataSource_Memory final : public DataSource
101
size_t read(uint8_t[], size_t) override;
102
size_t peek(uint8_t[], size_t, size_t) const override;
103
bool check_available(size_t n) override;
104
bool end_of_data() const override;
107
* Construct a memory source that reads from a string
108
* @param in the string to read from
110
explicit DataSource_Memory(const std::string& in);
113
* Construct a memory source that reads from a byte array
114
* @param in the byte array to read from
115
* @param length the length of the byte array
117
DataSource_Memory(const uint8_t in[], size_t length) :
118
m_source(in, in + length), m_offset(0) {}
121
* Construct a memory source that reads from a secure_vector
122
* @param in the MemoryRegion to read from
124
explicit DataSource_Memory(const secure_vector<uint8_t>& in) :
125
m_source(in), m_offset(0) {}
128
* Construct a memory source that reads from a std::vector
129
* @param in the MemoryRegion to read from
131
explicit DataSource_Memory(const std::vector<uint8_t>& in) :
132
m_source(in.begin(), in.end()), m_offset(0) {}
134
size_t get_bytes_read() const override { return m_offset; }
136
secure_vector<uint8_t> m_source;
141
* This class represents a Stream-Based DataSource.
143
class BOTAN_PUBLIC_API(2,0) DataSource_Stream final : public DataSource
146
size_t read(uint8_t[], size_t) override;
147
size_t peek(uint8_t[], size_t, size_t) const override;
148
bool check_available(size_t n) override;
149
bool end_of_data() const override;
150
std::string id() const override;
152
DataSource_Stream(std::istream&,
153
const std::string& id = "<std::istream>");
155
#if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
157
* Construct a Stream-Based DataSource from filesystem path
158
* @param file the path to the file
159
* @param use_binary whether to treat the file as binary or not
161
DataSource_Stream(const std::string& file, bool use_binary = false);
164
DataSource_Stream(const DataSource_Stream&) = delete;
166
DataSource_Stream& operator=(const DataSource_Stream&) = delete;
168
~DataSource_Stream();
170
size_t get_bytes_read() const override { return m_total_read; }
172
const std::string m_identifier;
174
std::unique_ptr<std::istream> m_source_memory;
175
std::istream& m_source;