Botan 2.19.5 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:

## Public Member Functions
bool | check_available (size_t n) override
| DataSource_Memory (const secure_vector< uint8_t > &in)
| DataSource_Memory (const std::string &in)
| DataSource_Memory (const std::vector< uint8_t > &in)
| DataSource_Memory (const uint8_t in[], size_t length)
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

  in | the 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 }
Botan::cast_char_ptr_to_uint8
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:190

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

  in | the byte array to read from
  length | the 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 | ) |
| inline explicit

Construct a memory source that reads from a secure_vector

Parameters

  in | the 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 | ) |
| inline explicit

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

Parameters

  in | the 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 | ) |
| override virtual

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

  N | the number of bytes to discard

Returns
  number of bytes actually discarded

Definition at line 39 of file data_src.cpp.

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 }
Botan::DataSource::read
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0

References Botan::DataSource::read().

## ◆ end_of_data()

bool Botan::DataSource_Memory::end_of_data | ( |  | ) | const
| override virtual

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 }

Referenced by Botan::TLS::Session::Session().

## ◆ get_bytes_read()

size_t Botan::DataSource_Memory::get_bytes_read | ( |  | ) | const
| inline override virtual

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
| inline virtual inherited

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
| override virtual

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

  out | the byte array to write the output to
  length | the length of the byte array out
  peek_offset | the 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.

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 }
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133

References Botan::copy_mem().

## ◆ peek_byte()

size_t Botan::DataSource::peek_byte | ( | uint8_t & | out | ) | const
| inherited

Peek at one byte.

Parameters

  out | an output byte

Returns
  length in bytes that was actually read and put into out

Definition at line 31 of file data_src.cpp.

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

References Botan::DataSource::peek().

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

## ◆ read()

size_t Botan::DataSource_Memory::read | ( | uint8_t | out[],
|  | size_t | length
| ) |  |
| override virtual

Read from the source. Moves the internal offset so that every call to read will return a new portion of the source.

Parameters

  out | the byte array to write the result to
  length | the 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.

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 }

References Botan::copy_mem().

## ◆ read_byte()

size_t Botan::DataSource::read_byte | ( | uint8_t & | out | ) |
| inherited

Read one byte.

Parameters

  out | the 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.

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

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:

- src/lib/utils/data_src.h
- src/lib/utils/data_src.cpp