Botan 3.0.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 ()
 

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 152 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 202 of file data_src.cpp.

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

◆ DataSource_Stream() [2/2]

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

◆ ~DataSource_Stream()

Botan::DataSource_Stream::~DataSource_Stream ( )
default

Member Function Documentation

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 120 of file data_src.cpp.

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

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

41 {
42 uint8_t buf[64] = { 0 };
43 size_t discarded = 0;
44
45 while(n)
46 {
47 const size_t got = this->read(buf, std::min(n, sizeof(buf)));
48 discarded += got;
49 n -= got;
50
51 if(got == 0)
52 break;
53 }
54
55 return discarded;
56 }
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 166 of file data_src.cpp.

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

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

179{ 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 174 of file data_src.cpp.

175 {
176 return m_identifier;
177 }

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

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

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

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

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

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

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: