Botan  1.11.10
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 */
35 size_t DataSource::discard_next(size_t n)
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 std::string& in) :
81  source(reinterpret_cast<const byte*>(in.data()),
82  reinterpret_cast<const byte*>(in.data()) + in.length()),
83  offset(0)
84  {
85  offset = 0;
86  }
87 
88 /*
89 * Read from a stream
90 */
91 size_t DataSource_Stream::read(byte out[], size_t length)
92  {
93  source.read(reinterpret_cast<char*>(out), length);
94  if(source.bad())
95  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
96 
97  size_t got = source.gcount();
98  total_read += got;
99  return got;
100  }
101 
102 /*
103 * Peek into a stream
104 */
105 size_t DataSource_Stream::peek(byte out[], size_t length, size_t offset) const
106  {
107  if(end_of_data())
108  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
109 
110  size_t got = 0;
111 
112  if(offset)
113  {
114  secure_vector<byte> buf(offset);
115  source.read(reinterpret_cast<char*>(&buf[0]), buf.size());
116  if(source.bad())
117  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
118  got = source.gcount();
119  }
120 
121  if(got == offset)
122  {
123  source.read(reinterpret_cast<char*>(out), length);
124  if(source.bad())
125  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
126  got = source.gcount();
127  }
128 
129  if(source.eof())
130  source.clear();
131  source.seekg(total_read, std::ios::beg);
132 
133  return got;
134  }
135 
136 /*
137 * Check if the stream is empty or in error
138 */
140  {
141  return (!source.good());
142  }
143 
144 /*
145 * Return a human-readable ID for this stream
146 */
147 std::string DataSource_Stream::id() const
148  {
149  return identifier;
150  }
151 
152 /*
153 * DataSource_Stream Constructor
154 */
155 DataSource_Stream::DataSource_Stream(const std::string& path,
156  bool use_binary) :
157  identifier(path),
158  source_p(new std::ifstream(
159  path.c_str(),
160  use_binary ? std::ios::binary : std::ios::in)),
161  source(*source_p),
162  total_read(0)
163  {
164  if(!source.good())
165  {
166  delete source_p;
167  throw Stream_IO_Error("DataSource: Failure opening file " + path);
168  }
169  }
170 
171 /*
172 * DataSource_Stream Constructor
173 */
175  const std::string& name) :
176  identifier(name),
177  source_p(nullptr),
178  source(in),
179  total_read(0)
180  {
181  }
182 
183 /*
184 * DataSource_Stream Destructor
185 */
187  {
188  delete source_p;
189  }
190 
191 }
size_t peek(byte[], size_t, size_t) const
Definition: data_src.cpp:58
std::string id() const
Definition: data_src.cpp:147
virtual size_t read(byte out[], size_t length)=0
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: bigint.h:560
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:92
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:174
size_t peek(byte[], size_t, size_t) const
Definition: data_src.cpp:105
uint8_t byte
Definition: types.h:30
size_t read_byte(byte &out)
Definition: data_src.cpp:19
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:39
Definition: buf_comp.h:15
size_t read(byte[], size_t)
Definition: data_src.cpp:47
bool end_of_data() const
Definition: data_src.cpp:139
DataSource_Memory(const std::string &in)
Definition: data_src.cpp:80
bool end_of_data() const
Definition: data_src.cpp:72
size_t read(byte[], size_t)
Definition: data_src.cpp:91