Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
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[], 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)
 

Detailed Description

This class represents a Memory-Based DataSource

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

95 :
96 m_source(cast_char_ptr_to_uint8(in.data()), cast_char_ptr_to_uint8(in.data()) + in.length()), m_offset(0) {}
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:275

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

118: 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 124 of file data_src.h.

124: 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 130 of file data_src.h.

130: 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 136 of file data_src.h.

136: 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 67 of file data_src.cpp.

67 {
68 return (n <= (m_source.size() - m_offset));
69}

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

88 {
89 return (m_offset == m_source.size());
90}

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

138{ 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 61 of file data_src.h.

61{ 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 74 of file data_src.cpp.

74 {
75 const size_t bytes_left = m_source.size() - m_offset;
76 if(peek_offset >= bytes_left) {
77 return 0;
78 }
79
80 const size_t got = std::min(bytes_left - peek_offset, length);
81 copy_mem(out, &m_source[m_offset + peek_offset], got);
82 return got;
83}
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

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

60 {
61 const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
62 copy_mem(out, m_source.data() + m_offset, got);
63 m_offset += got;
64 return got;
65}

References Botan::copy_mem().

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