Botan  1.10.9
data_src.cpp
Go to the documentation of this file.
1 /*
2 * DataSource
3 * (C) 1999-2007 Jack Lloyd
4 * 2005 Matthew Gregan
5 *
6 * Distributed under the terms of the Botan license
7 */
8 
9 #include <botan/data_src.h>
10 #include <botan/exceptn.h>
11 #include <fstream>
12 #include <algorithm>
13 
14 namespace Botan {
15 
16 /*
17 * Read a single byte from the DataSource
18 */
20  {
21  return read(&out, 1);
22  }
23 
24 /*
25 * Peek a single byte from the DataSource
26 */
27 size_t DataSource::peek_byte(byte& out) const
28  {
29  return peek(&out, 1, 0);
30  }
31 
32 /*
33 * Discard the next N bytes of the data
34 */
36  {
37  size_t discarded = 0;
38  byte dummy;
39  for(size_t j = 0; j != n; ++j)
40  discarded += read_byte(dummy);
41  return discarded;
42  }
43 
44 /*
45 * Read from a memory buffer
46 */
47 size_t DataSource_Memory::read(byte out[], size_t length)
48  {
49  size_t got = std::min<size_t>(source.size() - offset, length);
50  copy_mem(out, &source[offset], got);
51  offset += got;
52  return got;
53  }
54 
55 /*
56 * Peek into a memory buffer
57 */
58 size_t DataSource_Memory::peek(byte out[], size_t length,
59  size_t peek_offset) const
60  {
61  const size_t bytes_left = source.size() - offset;
62  if(peek_offset >= bytes_left) return 0;
63 
64  size_t got = std::min(bytes_left - peek_offset, length);
65  copy_mem(out, &source[offset + peek_offset], got);
66  return got;
67  }
68 
69 /*
70 * Check if the memory buffer is empty
71 */
73  {
74  return (offset == source.size());
75  }
76 
77 /*
78 * DataSource_Memory Constructor
79 */
80 DataSource_Memory::DataSource_Memory(const byte in[], size_t length) :
81  source(in, length)
82  {
83  offset = 0;
84  }
85 
86 /*
87 * DataSource_Memory Constructor
88 */
90  source(in)
91  {
92  offset = 0;
93  }
94 
95 /*
96 * DataSource_Memory Constructor
97 */
98 DataSource_Memory::DataSource_Memory(const std::string& in) :
99  source(reinterpret_cast<const byte*>(in.data()), in.length())
100  {
101  offset = 0;
102  }
103 
104 /*
105 * Read from a stream
106 */
107 size_t DataSource_Stream::read(byte out[], size_t length)
108  {
109  source.read(reinterpret_cast<char*>(out), length);
110  if(source.bad())
111  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
112 
113  size_t got = source.gcount();
114  total_read += got;
115  return got;
116  }
117 
118 /*
119 * Peek into a stream
120 */
121 size_t DataSource_Stream::peek(byte out[], size_t length, size_t offset) const
122  {
123  if(end_of_data())
124  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
125 
126  size_t got = 0;
127 
128  if(offset)
129  {
130  SecureVector<byte> buf(offset);
131  source.read(reinterpret_cast<char*>(&buf[0]), buf.size());
132  if(source.bad())
133  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
134  got = source.gcount();
135  }
136 
137  if(got == offset)
138  {
139  source.read(reinterpret_cast<char*>(out), length);
140  if(source.bad())
141  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
142  got = source.gcount();
143  }
144 
145  if(source.eof())
146  source.clear();
147  source.seekg(total_read, std::ios::beg);
148 
149  return got;
150  }
151 
152 /*
153 * Check if the stream is empty or in error
154 */
156  {
157  return (!source.good());
158  }
159 
160 /*
161 * Return a human-readable ID for this stream
162 */
163 std::string DataSource_Stream::id() const
164  {
165  return identifier;
166  }
167 
168 /*
169 * DataSource_Stream Constructor
170 */
171 DataSource_Stream::DataSource_Stream(const std::string& path,
172  bool use_binary) :
173  identifier(path),
174  source_p(new std::ifstream(
175  path.c_str(),
176  use_binary ? std::ios::binary : std::ios::in)),
177  source(*source_p),
178  total_read(0)
179  {
180  if(!source.good())
181  {
182  delete source_p;
183  throw Stream_IO_Error("DataSource: Failure opening file " + path);
184  }
185  }
186 
187 /*
188 * DataSource_Stream Constructor
189 */
191  const std::string& name) :
192  identifier(name),
193  source_p(0),
194  source(in),
195  total_read(0)
196  {
197  }
198 
199 /*
200 * DataSource_Stream Destructor
201 */
203  {
204  delete source_p;
205  }
206 
207 }
size_t peek(byte[], size_t, size_t) const
Definition: data_src.cpp:58
std::string id() const
Definition: data_src.cpp:163
virtual size_t read(byte out[], size_t length)=0
BigInt n
Definition: numthry.cpp:26
size_t peek_byte(byte &out) const
Definition: data_src.cpp:27
size_t discard_next(size_t N)
Definition: data_src.cpp:35
Definition: secmem.h:422
unsigned char byte
Definition: types.h:22
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
DataSource_Stream(std::istream &, const std::string &id="<std::istream>")
Definition: data_src.cpp:190
size_t peek(byte[], size_t, size_t) const
Definition: data_src.cpp:121
size_t read_byte(byte &out)
Definition: data_src.cpp:19
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
size_t read(byte[], size_t)
Definition: data_src.cpp:47
bool end_of_data() const
Definition: data_src.cpp:155
DataSource_Memory(const std::string &in)
Definition: data_src.cpp:98
bool end_of_data() const
Definition: data_src.cpp:72
size_t read(byte[], size_t)
Definition: data_src.cpp:107