Botan  1.10.9
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  /**
60  * Read one byte.
61  * @param out the byte to read to
62  * @return length in bytes that was actually read and put
63  * into out
64  */
65  size_t read_byte(byte& out);
66 
67  /**
68  * Peek at one byte.
69  * @param out an output byte
70  * @return length in bytes that was actually read and put
71  * into out
72  */
73  size_t peek_byte(byte& out) const;
74 
75  /**
76  * Discard the next N bytes of the data
77  * @param N the number of bytes to discard
78  * @return number of bytes actually discarded
79  */
80  size_t discard_next(size_t N);
81 
83  virtual ~DataSource() {}
84  private:
85  DataSource& operator=(const DataSource&) { return (*this); }
86  DataSource(const DataSource&);
87  };
88 
89 /**
90 * This class represents a Memory-Based DataSource
91 */
92 class BOTAN_DLL DataSource_Memory : public DataSource
93  {
94  public:
95  size_t read(byte[], size_t);
96  size_t peek(byte[], size_t, size_t) const;
97  bool end_of_data() const;
98 
99  /**
100  * Construct a memory source that reads from a string
101  * @param in the string to read from
102  */
103  DataSource_Memory(const std::string& in);
104 
105  /**
106  * Construct a memory source that reads from a byte array
107  * @param in the byte array to read from
108  * @param length the length of the byte array
109  */
110  DataSource_Memory(const byte in[], size_t length);
111 
112  /**
113  * Construct a memory source that reads from a MemoryRegion
114  * @param in the MemoryRegion to read from
115  */
117  private:
118  SecureVector<byte> source;
119  size_t offset;
120  };
121 
122 /**
123 * This class represents a Stream-Based DataSource.
124 */
125 class BOTAN_DLL DataSource_Stream : public DataSource
126  {
127  public:
128  size_t read(byte[], size_t);
129  size_t peek(byte[], size_t, size_t) const;
130  bool end_of_data() const;
131  std::string id() const;
132 
133  DataSource_Stream(std::istream&,
134  const std::string& id = "<std::istream>");
135 
136  /**
137  * Construct a Stream-Based DataSource from file
138  * @param file the name of the file
139  * @param use_binary whether to treat the file as binary or not
140  */
141  DataSource_Stream(const std::string& file, bool use_binary = false);
142 
144  private:
145  const std::string identifier;
146 
147  std::istream* source_p;
148  std::istream& source;
149  size_t total_read;
150  };
151 
152 }
153 
154 #endif
unsigned char byte
Definition: types.h:22
virtual std::string id() const
Definition: data_src.h:57
virtual ~DataSource()
Definition: data_src.h:83