Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 <string>
14 #include <iosfwd>
15
16 namespace Botan {
17
18 /**
19 * This class represents an abstract data source object.
20 */
21 class BOTAN_DLL DataSource
22 {
23 public:
24 /**
25 * Read from the source. Moves the internal offset so that every
26 * call to read will return a new portion of the source.
27 *
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
31 * into out
32 */
33 virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT = 0;
34
35 virtual bool check_available(size_t n) = 0;
36
37 /**
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.
41 *
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
46 * into out
47 */
48 virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT = 0;
49
50 /**
51 * Test whether the source still has data that can be read.
52 * @return true if there is still data to read, false otherwise
53 */
54 virtual bool end_of_data() const = 0;
55 /**
56 * return the id of this data source
57 * @return std::string representing the id of this data source
58 */
59 virtual std::string id() const { return ""; }
60
61 /**
62 * Read one byte.
63 * @param out the byte to read to
64 * @return length in bytes that was actually read and put
65 * into out
66 */
67 size_t read_byte(uint8_t& out);
68
69 /**
70 * Peek at one byte.
71 * @param out an output byte
72 * @return length in bytes that was actually read and put
73 * into out
74 */
75 size_t peek_byte(uint8_t& out) const;
76
77 /**
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
81 */
82 size_t discard_next(size_t N);
83
84 /**
85 * @return number of bytes read so far.
86 */
87 virtual size_t get_bytes_read() const = 0;
88
89 DataSource() = default;
90 virtual ~DataSource() = default;
91 DataSource& operator=(const DataSource&) = delete;
92 DataSource(const DataSource&) = delete;
93 };
94
95 /**
96 * This class represents a Memory-Based DataSource
97 */
98 class BOTAN_DLL DataSource_Memory : public DataSource
99 {
100 public:
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;
105
106 /**
107 * Construct a memory source that reads from a string
108 * @param in the string to read from
109 */
110 explicit DataSource_Memory(const std::string& in);
111
112 /**
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
116 */
117 DataSource_Memory(const uint8_t in[], size_t length) :
118 m_source(in, in + length), m_offset(0) {}
119
120 /**
121 * Construct a memory source that reads from a secure_vector
122 * @param in the MemoryRegion to read from
123 */
124 explicit DataSource_Memory(const secure_vector<uint8_t>& in) :
125 m_source(in), m_offset(0) {}
126
127 /**
128 * Construct a memory source that reads from a std::vector
129 * @param in the MemoryRegion to read from
130 */
131 explicit DataSource_Memory(const std::vector<uint8_t>& in) :
132 m_source(in.begin(), in.end()), m_offset(0) {}
133
134 size_t get_bytes_read() const override { return m_offset; }
135 private:
136 secure_vector<uint8_t> m_source;
137 size_t m_offset;
138 };
139
140 /**
141 * This class represents a Stream-Based DataSource.
142 */
143 class BOTAN_DLL DataSource_Stream : public DataSource
144 {
145 public:
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;
151
152 DataSource_Stream(std::istream&,
153 const std::string& id = "<std::istream>");
154
155 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
156 /**
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
160 */
161 DataSource_Stream(const std::string& file, bool use_binary = false);
162 #endif
163
164 DataSource_Stream(const DataSource_Stream&) = delete;
165
166 DataSource_Stream& operator=(const DataSource_Stream&) = delete;
167
168 ~DataSource_Stream();
169
170 size_t get_bytes_read() const override { return m_total_read; }
171 private:
172 const std::string m_identifier;
173
174 std::unique_ptr<std::istream> m_source_memory;
175 std::istream& m_source;
176 size_t m_total_read;
177 };
178
179 }
180
181 #endif
Botan::DataSource_Stream
Definition: data_src.h:143
Botan::DataSource_Memory::DataSource_Memory
DataSource_Memory(const uint8_t in[], size_t length)
Definition: data_src.h:117
Botan::DataSource_Memory::DataSource_Memory
DataSource_Memory(const secure_vector< uint8_t > &in)
Definition: data_src.h:124
BOTAN_WARN_UNUSED_RESULT
#define BOTAN_WARN_UNUSED_RESULT
Definition: compiler.h:57
Botan::DataSource
Definition: data_src.h:21
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::DataSource::id
virtual std::string id() const
Definition: data_src.h:59
Botan::DataSource_Memory::get_bytes_read
size_t get_bytes_read() const override
Definition: data_src.h:134
Botan::DataSource_Stream::get_bytes_read
size_t get_bytes_read() const override
Definition: data_src.h:170
Botan::DataSource_Memory::DataSource_Memory
DataSource_Memory(const std::vector< uint8_t > &in)
Definition: data_src.h:131
Botan
Definition: alg_id.cpp:13
Botan::DataSource_Memory
Definition: data_src.h:98