Botan 1.10.17
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)
 DataSource_Stream (const std::string &file, bool use_binary=false)
 DataSource_Stream (std::istream &, const std::string &id="<std::istream>")
size_t discard_next (size_t N)
bool end_of_data () const
std::string id () const
size_t peek (byte[], size_t, size_t) const
size_t peek_byte (byte &out) const
size_t read (byte[], size_t)
size_t read_byte (byte &out)
 ~DataSource_Stream ()

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 128 of file data_src.h.

Constructor & Destructor Documentation

◆ DataSource_Stream() [1/2]

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

Definition at line 204 of file data_src.cpp.

205 :
206 identifier(name),
207 source_p(0),
208 source(in),
209 total_read(0)
210 {
211 }

◆ DataSource_Stream() [2/2]

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

186 :
187 identifier(path),
188 source_p(new std::ifstream(
189 path.c_str(),
190 use_binary ? std::ios::binary : std::ios::in)),
191 source(*source_p),
192 total_read(0)
193 {
194 if(!source.good())
195 {
196 delete source_p;
197 throw Stream_IO_Error("DataSource: Failure opening file " + path);
198 }
199 }
Stream_IO_Error(const std::string &err)
Definition exceptn.h:167

◆ ~DataSource_Stream()

Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 216 of file data_src.cpp.

217 {
218 delete source_p;
219 }

Member Function Documentation

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 123 of file data_src.cpp.

124 {
125 const std::streampos orig_pos = source.tellg();
126 source.seekg(0, std::ios::end);
127 const size_t avail = source.tellg() - orig_pos;
128 source.seekg(orig_pos);
129 return (avail >= n);
130 }

◆ 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.

36 {
37 size_t discarded = 0;
38 byte dummy;
39 for(size_t j = 0; j != n; ++j)
40 discarded += read_byte(dummy);
41 return discarded;
42 }
size_t read_byte(byte &out)
Definition data_src.cpp:19

References read_byte().

Referenced by check_available().

◆ end_of_data()

bool Botan::DataSource_Stream::end_of_data ( ) const
virtual

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

170 {
171 return (!source.good());
172 }

Referenced by peek().

◆ id()

std::string Botan::DataSource_Stream::id ( ) const
virtual

return the id of this data source

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

Reimplemented from Botan::DataSource.

Definition at line 177 of file data_src.cpp.

178 {
179 return identifier;
180 }

◆ peek()

size_t Botan::DataSource_Stream::peek ( byte out[],
size_t length,
size_t peek_offset ) const
virtual

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

136 {
137 if(end_of_data())
138 throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
139
140 size_t got = 0;
141
142 if(offset)
143 {
144 SecureVector<byte> buf(offset);
145 source.read(reinterpret_cast<char*>(&buf[0]), buf.size());
146 if(source.bad())
147 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
148 got = source.gcount();
149 }
150
151 if(got == offset)
152 {
153 source.read(reinterpret_cast<char*>(out), length);
154 if(source.bad())
155 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
156 got = source.gcount();
157 }
158
159 if(source.eof())
160 source.clear();
161 source.seekg(total_read, std::ios::beg);
162
163 return got;
164 }
Invalid_State(const std::string &err)
Definition exceptn.h:27

References end_of_data(), Botan::Invalid_State::Invalid_State(), Botan::MemoryRegion< T >::size(), and Botan::Stream_IO_Error::Stream_IO_Error().

◆ 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.

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

References peek().

Referenced by check_available(), and Botan::ASN1::maybe_BER().

◆ read()

size_t Botan::DataSource_Stream::read ( byte out[],
size_t length )
virtual

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

113 {
114 source.read(reinterpret_cast<char*>(out), length);
115 if(source.bad())
116 throw Stream_IO_Error("DataSource_Stream::read: Source failure");
117
118 size_t got = source.gcount();
119 total_read += got;
120 return got;
121 }

References Botan::Stream_IO_Error::Stream_IO_Error().

◆ 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.

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

References read().

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


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