Botan  2.7.0
Crypto and TLS for C++11
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::string &in)
 
 DataSource_Memory (const uint8_t in[], size_t length)
 
 DataSource_Memory (const secure_vector< uint8_t > &in)
 
 DataSource_Memory (const std::vector< uint8_t > &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 98 of file data_src.h.

Constructor & Destructor Documentation

◆ DataSource_Memory() [1/4]

Botan::DataSource_Memory::DataSource_Memory ( const std::string &  in)
explicit

Construct a memory source that reads from a string

Parameters
inthe string to read from

Definition at line 98 of file data_src.cpp.

98  :
99  m_source(cast_char_ptr_to_uint8(in.data()),
100  cast_char_ptr_to_uint8(in.data()) + in.length()),
101  m_offset(0)
102  {
103  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131

◆ DataSource_Memory() [2/4]

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

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

◆ DataSource_Memory() [3/4]

Botan::DataSource_Memory::DataSource_Memory ( const 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  :
125  m_source(in), m_offset(0) {}

◆ DataSource_Memory() [4/4]

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

131  :
132  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 68 of file data_src.cpp.

69  {
70  return (n <= (m_source.size() - m_offset));
71  }

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

References Botan::DataSource::read().

40  {
41  uint8_t buf[64] = { 0 };
42  size_t discarded = 0;
43 
44  while(n)
45  {
46  const size_t got = this->read(buf, std::min(n, sizeof(buf)));
47  discarded += got;
48  n -= got;
49 
50  if(got == 0)
51  break;
52  }
53 
54  return discarded;
55  }
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0

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

91  {
92  return (m_offset == m_source.size());
93  }

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

134 { 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 59 of file data_src.h.

59 { 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 76 of file data_src.cpp.

References Botan::copy_mem().

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

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

References Botan::DataSource::peek().

Referenced by Botan::ASN1::maybe_BER().

32  {
33  return peek(&out, 1, 0);
34  }
virtual size_t peek(uint8_t out[], size_t length, size_t peek_offset) const BOTAN_WARN_UNUSED_RESULT=0

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

References Botan::copy_mem().

61  {
62  const size_t got = std::min<size_t>(m_source.size() - m_offset, length);
63  copy_mem(out, m_source.data() + m_offset, got);
64  m_offset += got;
65  return got;
66  }
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108

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

References Botan::DataSource::read().

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

24  {
25  return read(&out, 1);
26  }
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0

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