Botan  1.11.30
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 * Botan is released under the Simplified BSD License (see license.txt)
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  byte buf[64] = { 0 };
38  size_t discarded = 0;
39 
40  while(n)
41  {
42  const size_t got = this->read(buf, std::min(n, sizeof(buf)));
43  discarded += got;
44  n -= got;
45 
46  if(got == 0)
47  break;
48  }
49 
50  return discarded;
51  }
52 
53 /*
54 * Read from a memory buffer
55 */
56 size_t DataSource_Memory::read(byte out[], size_t length)
57  {
58  size_t got = std::min<size_t>(m_source.size() - m_offset, length);
59  copy_mem(out, m_source.data() + m_offset, got);
60  m_offset += got;
61  return got;
62  }
63 
65  {
66  return (n <= (m_source.size() - m_offset));
67  }
68 
69 /*
70 * Peek into a memory buffer
71 */
72 size_t DataSource_Memory::peek(byte out[], size_t length,
73  size_t peek_offset) const
74  {
75  const size_t bytes_left = m_source.size() - m_offset;
76  if(peek_offset >= bytes_left) return 0;
77 
78  size_t got = std::min(bytes_left - peek_offset, length);
79  copy_mem(out, &m_source[m_offset + peek_offset], got);
80  return got;
81  }
82 
83 /*
84 * Check if the memory buffer is empty
85 */
87  {
88  return (m_offset == m_source.size());
89  }
90 
91 /*
92 * DataSource_Memory Constructor
93 */
94 DataSource_Memory::DataSource_Memory(const std::string& in) :
95  m_source(reinterpret_cast<const byte*>(in.data()),
96  reinterpret_cast<const byte*>(in.data()) + in.length()),
97  m_offset(0)
98  {
99  }
100 
101 /*
102 * Read from a stream
103 */
104 size_t DataSource_Stream::read(byte out[], size_t length)
105  {
106  m_source.read(reinterpret_cast<char*>(out), length);
107  if(m_source.bad())
108  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
109 
110  size_t got = m_source.gcount();
111  m_total_read += got;
112  return got;
113  }
114 
116  {
117  const std::streampos orig_pos = m_source.tellg();
118  m_source.seekg(0, std::ios::end);
119  const size_t avail = m_source.tellg() - orig_pos;
120  m_source.seekg(orig_pos);
121  return (avail >= n);
122  }
123 
124 /*
125 * Peek into a stream
126 */
127 size_t DataSource_Stream::peek(byte out[], size_t length, size_t offset) const
128  {
129  if(end_of_data())
130  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
131 
132  size_t got = 0;
133 
134  if(offset)
135  {
136  secure_vector<byte> buf(offset);
137  m_source.read(reinterpret_cast<char*>(buf.data()), buf.size());
138  if(m_source.bad())
139  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
140  got = m_source.gcount();
141  }
142 
143  if(got == offset)
144  {
145  m_source.read(reinterpret_cast<char*>(out), length);
146  if(m_source.bad())
147  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
148  got = m_source.gcount();
149  }
150 
151  if(m_source.eof())
152  m_source.clear();
153  m_source.seekg(m_total_read, std::ios::beg);
154 
155  return got;
156  }
157 
158 /*
159 * Check if the stream is empty or in error
160 */
162  {
163  return (!m_source.good());
164  }
165 
166 /*
167 * Return a human-readable ID for this stream
168 */
169 std::string DataSource_Stream::id() const
170  {
171  return m_identifier;
172  }
173 
174 /*
175 * DataSource_Stream Constructor
176 */
177 DataSource_Stream::DataSource_Stream(const std::string& path,
178  bool use_binary) :
179  m_identifier(path),
180  m_source_p(new std::ifstream(path,
181  use_binary ? std::ios::binary : std::ios::in)),
182  m_source(*m_source_p),
183  m_total_read(0)
184  {
185  if(!m_source.good())
186  {
187  delete m_source_p;
188  throw Stream_IO_Error("DataSource: Failure opening file " + path);
189  }
190  }
191 
192 /*
193 * DataSource_Stream Constructor
194 */
196  const std::string& name) :
197  m_identifier(name),
198  m_source_p(nullptr),
199  m_source(in),
200  m_total_read(0)
201  {
202  }
203 
204 /*
205 * DataSource_Stream Destructor
206 */
208  {
209  delete m_source_p;
210  }
211 
212 }
size_t peek(byte[], size_t, size_t) const override
Definition: data_src.cpp:72
virtual size_t read(byte out[], size_t length)=0
bool check_available(size_t n) override
Definition: data_src.cpp:115
size_t peek(byte[], size_t, size_t) const override
Definition: data_src.cpp:127
size_t peek_byte(byte &out) const
Definition: data_src.cpp:27
size_t discard_next(size_t N)
Definition: data_src.cpp:35
size_t read(byte[], size_t) override
Definition: data_src.cpp:56
Definition: bigint.h:610
std::string id() const override
Definition: data_src.cpp:169
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:96
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:195
bool end_of_data() const override
Definition: data_src.cpp:86
size_t read(byte[], size_t) override
Definition: data_src.cpp:104
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:43
Definition: alg_id.cpp:13
bool end_of_data() const override
Definition: data_src.cpp:161
T min(T a, T b)
Definition: ct_utils.h:180
DataSource_Memory(const std::string &in)
Definition: data_src.cpp:94
bool check_available(size_t n) override
Definition: data_src.cpp:64
std::uint8_t byte
Definition: types.h:31