Botan  1.10.9
Public Member Functions | List of all members
Botan::DataSource_Memory Class Reference

#include <data_src.h>

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

Public Member Functions

 DataSource_Memory (const std::string &in)
 
 DataSource_Memory (const byte in[], size_t length)
 
 DataSource_Memory (const MemoryRegion< byte > &in)
 
size_t discard_next (size_t N)
 
bool end_of_data () const
 
virtual std::string id () const
 
size_t peek (byte[], size_t, size_t) const
 
size_t peek_byte (byte &out) const
 
size_t read (byte[], size_t)
 
size_t read_byte (byte &out)
 

Detailed Description

This class represents a Memory-Based DataSource

Definition at line 92 of file data_src.h.

Constructor & Destructor Documentation

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

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  source(reinterpret_cast<const byte*>(in.data()), in.length())
100  {
101  offset = 0;
102  }
Botan::DataSource_Memory::DataSource_Memory ( const byte  in[],
size_t  length 
)

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

80  :
81  source(in, length)
82  {
83  offset = 0;
84  }
Botan::DataSource_Memory::DataSource_Memory ( const MemoryRegion< byte > &  in)

Construct a memory source that reads from a MemoryRegion

Parameters
inthe MemoryRegion to read from

Definition at line 89 of file data_src.cpp.

89  :
90  source(in)
91  {
92  offset = 0;
93  }

Member Function Documentation

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

References n, and Botan::DataSource::read_byte().

36  {
37  size_t discarded = 0;
38  byte dummy;
39  for(size_t j = 0; j != n; ++j)
40  discarded += read_byte(dummy);
41  return discarded;
42  }
BigInt n
Definition: numthry.cpp:26
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19
bool Botan::DataSource_Memory::end_of_data ( ) const
virtual

Test whether the source still has data that can be read.

Returns
true if there is still data to read, false otherwise

Implements Botan::DataSource.

Definition at line 72 of file data_src.cpp.

References Botan::MemoryRegion< T >::size().

73  {
74  return (offset == source.size());
75  }
size_t size() const
Definition: secmem.h:29
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, and Botan::DataSource_Command.

Definition at line 57 of file data_src.h.

57 { return ""; }
size_t Botan::DataSource_Memory::peek ( byte  out[],
size_t  length,
size_t  peek_offset 
) const
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
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 58 of file data_src.cpp.

References Botan::copy_mem(), and Botan::MemoryRegion< T >::size().

60  {
61  const size_t bytes_left = source.size() - offset;
62  if(peek_offset >= bytes_left) return 0;
63 
64  size_t got = std::min(bytes_left - peek_offset, length);
65  copy_mem(out, &source[offset + peek_offset], got);
66  return got;
67  }
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
size_t Botan::DataSource::peek_byte ( byte 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 27 of file data_src.cpp.

References Botan::DataSource::peek().

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

28  {
29  return peek(&out, 1, 0);
30  }
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
size_t Botan::DataSource_Memory::read ( byte  out[],
size_t  length 
)
virtual

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

References Botan::copy_mem(), and Botan::MemoryRegion< T >::size().

48  {
49  size_t got = std::min<size_t>(source.size() - offset, length);
50  copy_mem(out, &source[offset], got);
51  offset += got;
52  return got;
53  }
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
size_t Botan::DataSource::read_byte ( byte 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 19 of file data_src.cpp.

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), Botan::DataSource::discard_next(), Botan::BER_Decoder::discard_remaining(), Botan::PGP_decode(), and Botan::BER_Decoder::raw_bytes().

20  {
21  return read(&out, 1);
22  }
virtual size_t read(byte out[], size_t length)=0

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