Botan  1.10.10
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 195 of file data_src.cpp.

196  :
197  identifier(name),
198  source_p(nullptr),
199  source(in),
200  total_read(0)
201  {
202  }
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  identifier(path),
180  source_p(new std::ifstream(path,
181  use_binary ? std::ios::binary : std::ios::in)),
182  source(*source_p),
183  total_read(0)
184  {
185  if(!source.good())
186  {
187  delete source_p;
188  throw Stream_IO_Error("DataSource: Failure opening file " + path);
189  }
190  }
Botan::DataSource_Stream::DataSource_Stream ( const DataSource_Stream )
delete
Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 207 of file data_src.cpp.

208  {
209  delete source_p;
210  }

Member Function Documentation

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 = source.tellg();
118  source.seekg(0, std::ios::end);
119  const size_t avail = source.tellg() - orig_pos;
120  source.seekg(orig_pos);
121  return (avail >= n);
122  }
const BigInt & n
Definition: rsa.cpp:107
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::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 
45  if(got == 0)
46  break;
47  }
48 
49  return discarded;
50  }
virtual size_t read(byte out[], size_t length)=0
const BigInt & n
Definition: rsa.cpp:107
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 161 of file data_src.cpp.

Referenced by peek().

162  {
163  return (!source.good());
164  }
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 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 169 of file data_src.cpp.

170  {
171  return identifier;
172  }
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 127 of file data_src.cpp.

References 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  source.read(reinterpret_cast<char*>(buf.data()), buf.size());
138  if(source.bad())
139  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
140  got = source.gcount();
141  }
142 
143  if(got == offset)
144  {
145  source.read(reinterpret_cast<char*>(out), length);
146  if(source.bad())
147  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
148  got = source.gcount();
149  }
150 
151  if(source.eof())
152  source.clear();
153  source.seekg(total_read, std::ios::beg);
154 
155  return got;
156  }
bool end_of_data() const override
Definition: data_src.cpp:161
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 104 of file data_src.cpp.

105  {
106  source.read(reinterpret_cast<char*>(out), length);
107  if(source.bad())
108  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
109 
110  size_t got = source.gcount();
111  total_read += got;
112  return got;
113  }
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(), Botan::BER_Decoder::discard_remaining(), Botan::PGP_decode(), and Botan::BER_Decoder::raw_bytes().

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: