Botan  1.11.26
Public Member Functions | List of all members
Botan::DataSource_Stream Class Reference

#include <data_src.h>

Inheritance diagram for Botan::DataSource_Stream:
Botan::DataSource

Public Member Functions

bool check_available (size_t n) override
 
 DataSource_Stream (std::istream &, const std::string &id="<std::istream>")
 
 DataSource_Stream (const std::string &file, bool use_binary=false)
 
 DataSource_Stream (const DataSource_Stream &)=delete
 
size_t discard_next (size_t N)
 
bool end_of_data () const override
 
size_t get_bytes_read () const override
 
std::string id () const override
 
DataSource_Streamoperator= (const DataSource_Stream &)=delete
 
size_t peek (byte[], size_t, size_t) const override
 
size_t peek_byte (byte &out) const
 
size_t read (byte[], size_t) override
 
size_t read_byte (byte &out)
 
 ~DataSource_Stream ()
 

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 144 of file data_src.h.

Constructor & Destructor Documentation

Botan::DataSource_Stream::DataSource_Stream ( std::istream &  in,
const std::string &  id = "<std::istream>" 
)

Definition at line 196 of file data_src.cpp.

197  :
198  m_identifier(name),
199  m_source_p(nullptr),
200  m_source(in),
201  m_total_read(0)
202  {
203  }
Botan::DataSource_Stream::DataSource_Stream ( const std::string &  file,
bool  use_binary = false 
)

Construct a Stream-Based DataSource from file

Parameters
filethe name of the file
use_binarywhether to treat the file as binary or not

Definition at line 178 of file data_src.cpp.

179  :
180  m_identifier(path),
181  m_source_p(new std::ifstream(path,
182  use_binary ? std::ios::binary : std::ios::in)),
183  m_source(*m_source_p),
184  m_total_read(0)
185  {
186  if(!m_source.good())
187  {
188  delete m_source_p;
189  throw Stream_IO_Error("DataSource: Failure opening file " + path);
190  }
191  }
Botan::DataSource_Stream::DataSource_Stream ( const DataSource_Stream )
delete
Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 208 of file data_src.cpp.

209  {
210  delete m_source_p;
211  }

Member Function Documentation

bool Botan::DataSource_Stream::check_available ( size_t  n)
overridevirtual

Implements Botan::DataSource.

Definition at line 116 of file data_src.cpp.

117  {
118  const std::streampos orig_pos = m_source.tellg();
119  m_source.seekg(0, std::ios::end);
120  const size_t avail = m_source.tellg() - orig_pos;
121  m_source.seekg(orig_pos);
122  return (avail >= n);
123  }
size_t Botan::DataSource::discard_next ( size_t  N)
inherited

Discard the next N bytes of the data

Parameters
Nthe number of bytes to discard
Returns
number of bytes actually discarded

Definition at line 35 of file data_src.cpp.

References Botan::CT::min(), and Botan::DataSource::read().

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  }
virtual size_t read(byte out[], size_t length)=0
T min(T a, T b)
Definition: ct_utils.h:148
std::uint8_t byte
Definition: types.h:31
bool Botan::DataSource_Stream::end_of_data ( ) const
overridevirtual

Test whether the source still has data that can be read.

Returns
true if there is still data to read, false otherwise

Implements Botan::DataSource.

Definition at line 162 of file data_src.cpp.

163  {
164  return (!m_source.good());
165  }
size_t Botan::DataSource_Stream::get_bytes_read ( ) const
inlineoverridevirtual
Returns
number of bytes read so far.

Implements Botan::DataSource.

Definition at line 169 of file data_src.h.

169 { return m_total_read; }
std::string Botan::DataSource_Stream::id ( ) const
overridevirtual

return the id of this data source

Returns
std::string representing the id of this data source

Reimplemented from Botan::DataSource.

Definition at line 170 of file data_src.cpp.

171  {
172  return m_identifier;
173  }
DataSource_Stream& Botan::DataSource_Stream::operator= ( const DataSource_Stream )
delete
size_t Botan::DataSource_Stream::peek ( byte  out[],
size_t  length,
size_t  peek_offset 
) const
overridevirtual

Read from the source but do not modify the internal offset. Consecutive calls to peek() will return portions of the source starting at the same position.

Parameters
outthe byte array to write the output to
lengththe length of the byte array out
peek_offsetthe offset into the stream to read at
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 128 of file data_src.cpp.

References Botan::DataSource_Memory::end_of_data().

129  {
130  if(end_of_data())
131  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
132 
133  size_t got = 0;
134 
135  if(offset)
136  {
137  secure_vector<byte> buf(offset);
138  m_source.read(reinterpret_cast<char*>(buf.data()), buf.size());
139  if(m_source.bad())
140  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
141  got = m_source.gcount();
142  }
143 
144  if(got == offset)
145  {
146  m_source.read(reinterpret_cast<char*>(out), length);
147  if(m_source.bad())
148  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
149  got = m_source.gcount();
150  }
151 
152  if(m_source.eof())
153  m_source.clear();
154  m_source.seekg(m_total_read, std::ios::beg);
155 
156  return got;
157  }
bool end_of_data() const override
Definition: data_src.cpp:162
size_t Botan::DataSource::peek_byte ( byte out) const
inherited

Peek at one byte.

Parameters
outan output byte
Returns
length in bytes that was actually read and put into out

Definition at line 27 of file data_src.cpp.

References Botan::DataSource::peek().

Referenced by Botan::ASN1::maybe_BER().

28  {
29  return peek(&out, 1, 0);
30  }
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
size_t Botan::DataSource_Stream::read ( byte  out[],
size_t  length 
)
overridevirtual

Read from the source. Moves the internal offset so that every call to read will return a new portion of the source.

Parameters
outthe byte array to write the result to
lengththe length of the byte array out
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 105 of file data_src.cpp.

106  {
107  m_source.read(reinterpret_cast<char*>(out), length);
108  if(m_source.bad())
109  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
110 
111  size_t got = m_source.gcount();
112  m_total_read += got;
113  return got;
114  }
size_t Botan::DataSource::read_byte ( byte out)
inherited

Read one byte.

Parameters
outthe byte to read to
Returns
length in bytes that was actually read and put into out

Definition at line 19 of file data_src.cpp.

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), and Botan::PGP_decode().

20  {
21  return read(&out, 1);
22  }
virtual size_t read(byte out[], size_t length)=0

The documentation for this class was generated from the following files: