Botan 3.5.0
Crypto and TLS for C&
Botan::Buffered_Filter Class Referenceabstract

#include <filters.h>

Inheritance diagram for Botan::Buffered_Filter:
Botan::Cipher_Mode_Filter

Public Member Functions

 Buffered_Filter (size_t block_size, size_t final_minimum)
 
void end_msg ()
 
template<typename Alloc >
void write (const std::vector< uint8_t, Alloc > &in, size_t length)
 
void write (const uint8_t in[], size_t length)
 
virtual ~Buffered_Filter ()=default
 

Protected Member Functions

void buffer_reset ()
 
virtual void buffered_block (const uint8_t input[], size_t length)=0
 
size_t buffered_block_size () const
 
virtual void buffered_final (const uint8_t input[], size_t length)=0
 
size_t current_position () const
 

Detailed Description

Filter mixin that breaks input into blocks, useful for cipher modes

Definition at line 40 of file filters.h.

Constructor & Destructor Documentation

◆ Buffered_Filter()

Botan::Buffered_Filter::Buffered_Filter ( size_t block_size,
size_t final_minimum )

Initialize a Buffered_Filter

Parameters
block_sizethe function buffered_block will be called with inputs which are a multiple of this size
final_minimumthe function buffered_final will be called with at least this many bytes.

Definition at line 18 of file buf_filt.cpp.

18 : m_main_block_mod(b), m_final_minimum(f) {
19 if(m_main_block_mod == 0) {
20 throw Invalid_Argument("m_main_block_mod == 0");
21 }
22
23 if(m_final_minimum > m_main_block_mod) {
24 throw Invalid_Argument("m_final_minimum > m_main_block_mod");
25 }
26
27 m_buffer.resize(2 * m_main_block_mod);
28 m_buffer_pos = 0;
29}

◆ ~Buffered_Filter()

virtual Botan::Buffered_Filter::~Buffered_Filter ( )
virtualdefault

Member Function Documentation

◆ buffer_reset()

void Botan::Buffered_Filter::buffer_reset ( )
inlineprotected

Reset the buffer position

Definition at line 103 of file filters.h.

103{ m_buffer_pos = 0; }

◆ buffered_block()

virtual void Botan::Buffered_Filter::buffered_block ( const uint8_t input[],
size_t length )
protectedpure virtual

The block processor, implemented by subclasses

Parameters
inputsome input bytes
lengththe size of input, guaranteed to be a multiple of block_size

Referenced by end_msg(), and write().

◆ buffered_block_size()

size_t Botan::Buffered_Filter::buffered_block_size ( ) const
inlineprotected
Returns
block size of inputs

Definition at line 93 of file filters.h.

93{ return m_main_block_mod; }

◆ buffered_final()

virtual void Botan::Buffered_Filter::buffered_final ( const uint8_t input[],
size_t length )
protectedpure virtual

The final block, implemented by subclasses

Parameters
inputsome input bytes
lengththe size of input, guaranteed to be at least final_minimum bytes

Referenced by end_msg().

◆ current_position()

size_t Botan::Buffered_Filter::current_position ( ) const
inlineprotected
Returns
current position in the buffer

Definition at line 98 of file filters.h.

98{ return m_buffer_pos; }

◆ end_msg()

void Botan::Buffered_Filter::end_msg ( )

Finish a message, emitting to buffered_block and buffered_final Will throw an exception if less than final_minimum bytes were written into the filter.

Definition at line 79 of file buf_filt.cpp.

79 {
80 if(m_buffer_pos < m_final_minimum) {
81 throw Invalid_State("Buffered filter end_msg without enough input");
82 }
83
84 size_t spare_blocks = (m_buffer_pos - m_final_minimum) / m_main_block_mod;
85
86 if(spare_blocks) {
87 size_t spare_bytes = m_main_block_mod * spare_blocks;
88 buffered_block(m_buffer.data(), spare_bytes);
89 buffered_final(&m_buffer[spare_bytes], m_buffer_pos - spare_bytes);
90 } else {
91 buffered_final(m_buffer.data(), m_buffer_pos);
92 }
93
94 m_buffer_pos = 0;
95}
virtual void buffered_block(const uint8_t input[], size_t length)=0
virtual void buffered_final(const uint8_t input[], size_t length)=0

References buffered_block(), and buffered_final().

◆ write() [1/2]

template<typename Alloc >
void Botan::Buffered_Filter::write ( const std::vector< uint8_t, Alloc > & in,
size_t length )
inline

Definition at line 51 of file filters.h.

51 {
52 write(in.data(), length);
53 }
void write(const uint8_t in[], size_t length)
Definition buf_filt.cpp:34

◆ write() [2/2]

void Botan::Buffered_Filter::write ( const uint8_t in[],
size_t length )

Write bytes into the buffered filter, which will them emit them in calls to buffered_block in the subclass

Parameters
inthe input bytes
lengthof in in bytes

Definition at line 34 of file buf_filt.cpp.

34 {
35 if(!input_size) {
36 return;
37 }
38
39 if(m_buffer_pos + input_size >= m_main_block_mod + m_final_minimum) {
40 size_t to_copy = std::min<size_t>(m_buffer.size() - m_buffer_pos, input_size);
41
42 copy_mem(&m_buffer[m_buffer_pos], input, to_copy);
43 m_buffer_pos += to_copy;
44
45 input += to_copy;
46 input_size -= to_copy;
47
48 const size_t available = std::min(m_buffer_pos, m_buffer_pos + input_size - m_final_minimum);
49
50 // Size down to available block size
51 const size_t total_to_consume = available - (available % m_main_block_mod);
52
53 buffered_block(m_buffer.data(), total_to_consume);
54
55 m_buffer_pos -= total_to_consume;
56
57 copy_mem(m_buffer.data(), m_buffer.data() + total_to_consume, m_buffer_pos);
58 }
59
60 if(input_size >= m_final_minimum) {
61 size_t full_blocks = (input_size - m_final_minimum) / m_main_block_mod;
62 size_t to_copy = full_blocks * m_main_block_mod;
63
64 if(to_copy) {
65 buffered_block(input, to_copy);
66
67 input += to_copy;
68 input_size -= to_copy;
69 }
70 }
71
72 copy_mem(&m_buffer[m_buffer_pos], input, input_size);
73 m_buffer_pos += input_size;
74}
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References buffered_block(), and Botan::copy_mem().


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