Botan 1.10.17

data_src.h
Go to the documentation of this file.
1 /*
2 * DataSource
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_DATA_SRC_H__
9 #define BOTAN_DATA_SRC_H__
10
11 #include <botan/secmem.h>
12 #include <string>
13 #include <iosfwd>
14
15 namespace Botan {
16
17 /**
18 * This class represents an abstract data source object.
19 */
20 class BOTAN_DLL DataSource
21 {
22 public:
23 /**
24 * Read from the source. Moves the internal offset so that every
25 * call to read will return a new portion of the source.
26 *
27 * @param out the byte array to write the result to
28 * @param length the length of the byte array out
29 * @return length in bytes that was actually read and put
30 * into out
31 */
32 virtual size_t read(byte out[], size_t length) = 0;
33
34 /**
35 * Read from the source but do not modify the internal
36 * offset. Consecutive calls to peek() will return portions of
37 * the source starting at the same position.
38 *
39 * @param out the byte array to write the output to
40 * @param length the length of the byte array out
41 * @param peek_offset the offset into the stream to read at
42 * @return length in bytes that was actually read and put
43 * into out
44 */
45 virtual size_t peek(byte out[], size_t length,
46 size_t peek_offset) const = 0;
47
48 /**
49 * Test whether the source still has data that can be read.
50 * @return true if there is still data to read, false otherwise
51 */
52 virtual bool end_of_data() const = 0;
53 /**
54 * return the id of this data source
55 * @return std::string representing the id of this data source
56 */
57 virtual std::string id() const { return ""; }
58
59 virtual bool check_available(size_t n) = 0;
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(byte& 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(byte& 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 DataSource() {}
85 virtual ~DataSource() {}
86 private:
87 DataSource& operator=(const DataSource&) { return (*this); }
88 DataSource(const DataSource&);
89 };
90
91 /**
92 * This class represents a Memory-Based DataSource
93 */
94 class BOTAN_DLL DataSource_Memory : public DataSource
95 {
96 public:
97 size_t read(byte[], size_t);
98 size_t peek(byte[], size_t, size_t) const;
99 bool check_available(size_t n);
100 bool end_of_data() const;
101
102 /**
103 * Construct a memory source that reads from a string
104 * @param in the string to read from
105 */
106 DataSource_Memory(const std::string& in);
107
108 /**
109 * Construct a memory source that reads from a byte array
110 * @param in the byte array to read from
111 * @param length the length of the byte array
112 */
113 DataSource_Memory(const byte in[], size_t length);
114
115 /**
116 * Construct a memory source that reads from a MemoryRegion
117 * @param in the MemoryRegion to read from
118 */
119 DataSource_Memory(const MemoryRegion<byte>& in);
120 private:
121 SecureVector<byte> source;
122 size_t offset;
123 };
124
125 /**
126 * This class represents a Stream-Based DataSource.
127 */
128 class BOTAN_DLL DataSource_Stream : public DataSource
129 {
130 public:
131 size_t read(byte[], size_t);
132 size_t peek(byte[], size_t, size_t) const;
133 bool check_available(size_t n);
134 bool end_of_data() const;
135 std::string id() const;
136
137 DataSource_Stream(std::istream&,
138 const std::string& id = "<std::istream>");
139
140 /**
141 * Construct a Stream-Based DataSource from file
142 * @param file the name of the file
143 * @param use_binary whether to treat the file as binary or not
144 */
145 DataSource_Stream(const std::string& file, bool use_binary = false);
146
147 ~DataSource_Stream();
148 private:
149 const std::string identifier;
150
151 std::istream* source_p;
152 std::istream& source;
153 size_t total_read;
154 };
155
156 }
157
158 #endif
Botan::DataSource_Stream
Definition: data_src.h:128
Botan::DataSource
Definition: data_src.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::DataSource::id
virtual std::string id() const
Definition: data_src.h:57
Botan::DataSource::~DataSource
virtual ~DataSource()
Definition: data_src.h:85
Botan::DataSource::DataSource
DataSource()
Definition: data_src.h:84
Botan::DataSource_Memory
Definition: data_src.h:94
Botan::SecureVector< byte >