Botan 2.19.1
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::DataSource_Stream Class Referencefinal

#include <data_src.h>

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

Public Member Functions

bool check_available (size_t n) override
 
 DataSource_Stream (const DataSource_Stream &)=delete
 
 DataSource_Stream (std::istream &, const std::string &id="<std::istream>")
 
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

◆ DataSource_Stream() [1/2]

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

Definition at line 201 of file data_src.cpp.

202 :
203 m_identifier(name),
204 m_source(in),
205 m_total_read(0)
206 {
207 }
std::string name

◆ DataSource_Stream() [2/2]

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

◆ ~DataSource_Stream()

Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 209 of file data_src.cpp.

210 {
211 // for ~unique_ptr
212 }

Member Function Documentation

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 119 of file data_src.cpp.

120 {
121 const std::streampos orig_pos = m_source.tellg();
122 m_source.seekg(0, std::ios::end);
123 const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
124 m_source.seekg(orig_pos);
125 return (avail >= n);
126 }

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

40 {
41 uint8_t buf[64] = { 0 };
42 size_t discarded = 0;
43
44 while(n)
45 {
46 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
47 discarded += got;
48 n -= got;
49
50 if(got == 0)
51 break;
52 }
53
54 return discarded;
55 }
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0

References Botan::DataSource::read().

◆ 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 no more data to read, false otherwise

Implements Botan::DataSource.

Definition at line 165 of file data_src.cpp.

166 {
167 return (!m_source.good());
168 }

Referenced by peek().

◆ 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 170 of file data_src.h.

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

174 {
175 return m_identifier;
176 }

◆ operator=()

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

◆ peek()

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

132 {
133 if(end_of_data())
134 throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
135
136 size_t got = 0;
137
138 if(offset)
139 {
140 secure_vector<uint8_t> buf(offset);
141 m_source.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
142 if(m_source.bad())
143 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
144 got = static_cast<size_t>(m_source.gcount());
145 }
146
147 if(got == offset)
148 {
149 m_source.read(cast_uint8_ptr_to_char(out), length);
150 if(m_source.bad())
151 throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
152 got = static_cast<size_t>(m_source.gcount());
153 }
154
155 if(m_source.eof())
156 m_source.clear();
157 m_source.seekg(m_total_read, std::ios::beg);
158
159 return got;
160 }
bool end_of_data() const override
Definition: data_src.cpp:165
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:195

References Botan::cast_uint8_ptr_to_char(), and end_of_data().

◆ peek_byte()

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

32 {
33 return peek(&out, 1, 0);
34 }
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0

References Botan::DataSource::peek().

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

◆ read()

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

109 {
110 m_source.read(cast_uint8_ptr_to_char(out), length);
111 if(m_source.bad())
112 throw Stream_IO_Error("DataSource_Stream::read: Source failure");
113
114 const size_t got = static_cast<size_t>(m_source.gcount());
115 m_total_read += got;
116 return got;
117 }

References Botan::cast_uint8_ptr_to_char().

◆ read_byte()

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

24 {
25 return read(&out, 1);
26 }

References Botan::DataSource::read().

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


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