Botan 3.9.0
Crypto and TLS for C&
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 (DataSource_Stream &&)=delete
BOTAN_FUTURE_EXPLICIT DataSource_Stream (std::istream &in, std::string_view 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
DataSource_Streamoperator= (DataSource_Stream &&)=delete
size_t peek (uint8_t buf[], size_t length, size_t offset) const override
size_t peek_byte (uint8_t &out) const
size_t read (uint8_t buf[], size_t length) override
std::optional< uint8_t > read_byte ()
size_t read_byte (uint8_t &out)
 ~DataSource_Stream () override

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 159 of file data_src.h.

Constructor & Destructor Documentation

◆ DataSource_Stream() [1/3]

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

Definition at line 201 of file data_src.cpp.

201 :
202 m_identifier(name), m_source(in), m_total_read(0) {}

Referenced by DataSource_Stream(), DataSource_Stream(), operator=(), and operator=().

◆ DataSource_Stream() [2/3]

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

References DataSource_Stream().

◆ DataSource_Stream() [3/3]

Botan::DataSource_Stream::DataSource_Stream ( DataSource_Stream && )
delete

References DataSource_Stream().

◆ ~DataSource_Stream()

Botan::DataSource_Stream::~DataSource_Stream ( )
overridedefault

Member Function Documentation

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 124 of file data_src.cpp.

124 {
125 const std::streampos orig_pos = m_source.tellg();
126 m_source.seekg(0, std::ios::end);
127 const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
128 m_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 53 of file data_src.cpp.

53 {
54 uint8_t buf[64] = {0};
55 size_t discarded = 0;
56
57 while(n > 0) {
58 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
59 discarded += got;
60 n -= got;
61
62 if(got == 0) {
63 break;
64 }
65 }
66
67 return discarded;
68}
virtual size_t read(uint8_t out[], size_t length)=0

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

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

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

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

177 {
178 return m_identifier;
179}

◆ operator=() [1/2]

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

References DataSource_Stream().

◆ operator=() [2/2]

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

References DataSource_Stream().

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

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

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

46 {
47 return peek(&out, 1, 0);
48}
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =0

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

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

References Botan::cast_uint8_ptr_to_char().

◆ read_byte() [1/2]

std::optional< uint8_t > Botan::DataSource::read_byte ( )
inherited

Read one byte.

Returns nullopt if no further bytes are available

Definition at line 34 of file data_src.cpp.

34 {
35 uint8_t b = 0;
36 if(this->read(&b, 1) == 1) {
37 return b;
38 } else {
39 return {};
40 }
41}

References read().

◆ read_byte() [2/2]

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

27 {
28 return read(&out, 1);
29}

References read().

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


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