Botan 3.9.0
Crypto and TLS for C&
Botan::DataSource_Memory Class Referencefinal

#include <data_src.h>

Inheritance diagram for Botan::DataSource_Memory:
Botan::DataSource

Public Member Functions

bool check_available (size_t n) override
 DataSource_Memory (const std::vector< uint8_t > &in)
 DataSource_Memory (const uint8_t in[], size_t length)
 DataSource_Memory (secure_vector< uint8_t > in)
 DataSource_Memory (std::span< const uint8_t > in)
 DataSource_Memory (std::string_view in)
size_t discard_next (size_t N)
bool end_of_data () const override
size_t get_bytes_read () const override
virtual std::string id () const
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)

Detailed Description

This class represents a Memory-Based DataSource

Definition at line 111 of file data_src.h.

Constructor & Destructor Documentation

◆ DataSource_Memory() [1/5]

Botan::DataSource_Memory::DataSource_Memory ( std::string_view in)
explicit

Construct a memory source that reads from a string

Parameters
inthe string to read from

Definition at line 108 of file data_src.cpp.

DataSource_Memory(std::string_view in)
Definition data_src.cpp:108
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28

References Botan::as_span_of_bytes(), and DataSource_Memory().

Referenced by DataSource_Memory().

◆ DataSource_Memory() [2/5]

Botan::DataSource_Memory::DataSource_Memory ( const uint8_t in[],
size_t length )
inline

Construct a memory source that reads from a byte array

Parameters
inthe byte array to read from
lengththe length of the byte array

Definition at line 129 of file data_src.h.

129: m_source(in, in + length), m_offset(0) {}

◆ DataSource_Memory() [3/5]

Botan::DataSource_Memory::DataSource_Memory ( secure_vector< uint8_t > in)
inlineexplicit

Construct a memory source that reads from a secure_vector

Parameters
inthe MemoryRegion to read from

Definition at line 135 of file data_src.h.

135: m_source(std::move(in)), m_offset(0) {}

◆ DataSource_Memory() [4/5]

Botan::DataSource_Memory::DataSource_Memory ( std::span< const uint8_t > in)
inlineexplicit

Construct a memory source that reads from an arbitrary byte buffer

Parameters
inthe MemoryRegion to read from

Definition at line 141 of file data_src.h.

141: m_source(in.begin(), in.end()), m_offset(0) {}

◆ DataSource_Memory() [5/5]

Botan::DataSource_Memory::DataSource_Memory ( const std::vector< uint8_t > & in)
inlineexplicit

Construct a memory source that reads from a std::vector

Parameters
inthe MemoryRegion to read from

Definition at line 147 of file data_src.h.

147: m_source(in.begin(), in.end()), m_offset(0) {}

Member Function Documentation

◆ check_available()

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

Implements Botan::DataSource.

Definition at line 80 of file data_src.cpp.

80 {
81 return (n <= (m_source.size() - m_offset));
82}

◆ 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_Memory::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 101 of file data_src.cpp.

101 {
102 return (m_offset == m_source.size());
103}

◆ get_bytes_read()

size_t Botan::DataSource_Memory::get_bytes_read ( ) const
inlineoverridevirtual
Returns
number of bytes read so far.

Implements Botan::DataSource.

Definition at line 149 of file data_src.h.

149{ return m_offset; }

◆ id()

virtual std::string Botan::DataSource::id ( ) const
inlinevirtualinherited

return the id of this data source

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

Reimplemented in Botan::DataSource_Stream.

Definition at line 63 of file data_src.h.

63{ return ""; }

◆ peek()

size_t Botan::DataSource_Memory::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 87 of file data_src.cpp.

87 {
88 const size_t bytes_left = m_source.size() - m_offset;
89 if(peek_offset >= bytes_left) {
90 return 0;
91 }
92
93 const size_t got = std::min(bytes_left - peek_offset, length);
94 copy_mem(out, &m_source[m_offset + peek_offset], got);
95 return got;
96}
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145

References Botan::copy_mem().

◆ 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_Memory::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 73 of file data_src.cpp.

73 {
74 const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
75 copy_mem(out, m_source.data() + m_offset, got);
76 m_offset += got;
77 return got;
78}

References Botan::copy_mem().

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