Botan  1.11.32
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

§ DataSource_Stream() [1/3]

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

Definition at line 195 of file data_src.cpp.

196  :
197  m_identifier(name),
198  m_source_p(nullptr),
199  m_source(in),
200  m_total_read(0)
201  {
202  }

§ DataSource_Stream() [2/3]

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 177 of file data_src.cpp.

178  :
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  }

§ DataSource_Stream() [3/3]

Botan::DataSource_Stream::DataSource_Stream ( const DataSource_Stream )
delete

§ ~DataSource_Stream()

Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 207 of file data_src.cpp.

208  {
209  delete m_source_p;
210  }

Member Function Documentation

§ check_available()

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

Implements Botan::DataSource.

Definition at line 115 of file data_src.cpp.

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  }

§ discard_next()

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:180
std::uint8_t byte
Definition: types.h:31

§ end_of_data()

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 161 of file data_src.cpp.

162  {
163  return (!m_source.good());
164  }

§ get_bytes_read()

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; }

§ id()

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 169 of file data_src.cpp.

170  {
171  return m_identifier;
172  }

§ operator=()

DataSource_Stream& Botan::DataSource_Stream::operator= ( const DataSource_Stream )
delete

§ peek()

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 127 of file data_src.cpp.

References Botan::DataSource_Memory::end_of_data().

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  }
bool end_of_data() const override
Definition: data_src.cpp:161

§ peek_byte()

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

§ read()

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 104 of file data_src.cpp.

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  }

§ read_byte()

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: