Botan 3.4.0
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 &, 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
 
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 () override
 

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 148 of file data_src.h.

Constructor & Destructor Documentation

◆ DataSource_Stream() [1/2]

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

Definition at line 189 of file data_src.cpp.

189 :
190 m_identifier(name), m_source(in), m_total_read(0) {}
std::string name

◆ DataSource_Stream() [2/2]

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

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

112 {
113 const std::streampos orig_pos = m_source.tellg();
114 m_source.seekg(0, std::ios::end);
115 const size_t avail = static_cast<size_t>(m_source.tellg() - orig_pos);
116 m_source.seekg(orig_pos);
117 return (avail >= n);
118}

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

40 {
41 uint8_t buf[64] = {0};
42 size_t discarded = 0;
43
44 while(n) {
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
54 return discarded;
55}
virtual size_t read(uint8_t out[], size_t length)=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 158 of file data_src.cpp.

158 {
159 return (!m_source.good());
160}

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

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

165 {
166 return m_identifier;
167}

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

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

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

33 {
34 return peek(&out, 1, 0);
35}
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const =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 101 of file data_src.cpp.

101 {
102 m_source.read(cast_uint8_ptr_to_char(out), length);
103 if(m_source.bad()) {
104 throw Stream_IO_Error("DataSource_Stream::read: Source failure");
105 }
106
107 const size_t got = static_cast<size_t>(m_source.gcount());
108 m_total_read += got;
109 return got;
110}

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

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

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: