Botan  2.1.0
Crypto and TLS for C++11
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 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 (uint8_t[], size_t, size_t) const override
 
size_t peek_byte (uint8_t &out) const
 
size_t read (uint8_t[], size_t) override
 
size_t read_byte (uint8_t &out)
 
 ~DataSource_Stream ()
 

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 143 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 200 of file data_src.cpp.

201  :
202  m_identifier(name),
203  m_source(in),
204  m_total_read(0)
205  {
206  }
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  // for ~unique_ptr
211  }

Member Function Documentation

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

Implements Botan::DataSource.

Definition at line 118 of file data_src.cpp.

119  {
120  const std::streampos orig_pos = m_source.tellg();
121  m_source.seekg(0, std::ios::end);
122  const size_t avail = m_source.tellg() - orig_pos;
123  m_source.seekg(orig_pos);
124  return (avail >= n);
125  }
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 38 of file data_src.cpp.

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

39  {
40  uint8_t buf[64] = { 0 };
41  size_t discarded = 0;
42 
43  while(n)
44  {
45  const size_t got = this->read(buf, std::min(n, sizeof(buf)));
46  discarded += got;
47  n -= got;
48 
49  if(got == 0)
50  break;
51  }
52 
53  return discarded;
54  }
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
T min(T a, T b)
Definition: ct_utils.h:180
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 164 of file data_src.cpp.

Referenced by peek().

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

Implements Botan::DataSource.

Definition at line 170 of file data_src.h.

170 { 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 172 of file data_src.cpp.

173  {
174  return m_identifier;
175  }
DataSource_Stream& Botan::DataSource_Stream::operator= ( const DataSource_Stream )
delete
size_t Botan::DataSource_Stream::peek ( uint8_t  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 130 of file data_src.cpp.

References end_of_data().

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

References Botan::DataSource::peek().

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

31  {
32  return peek(&out, 1, 0);
33  }
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0
size_t Botan::DataSource_Stream::read ( uint8_t  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 107 of file data_src.cpp.

108  {
109  m_source.read(reinterpret_cast<char*>(out), length);
110  if(m_source.bad())
111  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
112 
113  size_t got = m_source.gcount();
114  m_total_read += got;
115  return got;
116  }
size_t Botan::DataSource::read_byte ( uint8_t &  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 22 of file data_src.cpp.

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), Botan::BER_Decoder::discard_remaining(), Botan::ASN1::maybe_BER(), and Botan::BER_Decoder::raw_bytes().

23  {
24  return read(&out, 1);
25  }
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0

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