Botan: Botan::Buffered_Filter Class Reference

Botan 1.10.17

Public Member Functions | Protected Member Functions | List of all members
Botan::Buffered_Filter Class Referenceabstract

`#include <buf_filt.h>`

Inheritance diagram for Botan::Buffered_Filter:

## Public Member Functions
| Buffered_Filter (size_t block_size, size_t final_minimum)
void | end_msg ()
void | write (const byte in[], size_t length)
virtual | ~Buffered_Filter ()

## Protected Member Functions
void | buffer_reset ()
virtual void | buffered_block (const byte input[], size_t length)=0
size_t | buffered_block_size () const
virtual void | buffered_final (const byte 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 19 of file buf_filt.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_size | the function buffered_block will be called with inputs which are a multiple of this size
  final_minimum | the function buffered_final will be called with at least this many bytes.

Definition at line 18 of file buf_filt.cpp.

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

18 :
19 main_block_mod(b), final_minimum(f)
20 {
21 if(main_block_mod == 0)
22 throw std::invalid_argument("main_block_mod == 0");
23
24 if(final_minimum > main_block_mod)
25 throw std::invalid_argument("final_minimum > main_block_mod");
26
27 buffer.resize(2 * main_block_mod);
28 buffer_pos = 0;
29 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217

## ◆ ~Buffered_Filter()

virtual Botan::Buffered_Filter::~Buffered_Filter | ( |  | ) |
| inlinevirtual

Definition at line 46 of file buf_filt.h.

46 {}

## Member Function Documentation

## ◆ buffer_reset()

void Botan::Buffered_Filter::buffer_reset | ( |  | ) |
| inlineprotected

Reset the buffer position

Definition at line 77 of file buf_filt.h.

Referenced by Botan::CBC_Encryption::set_iv(), and Botan::CBC_Decryption::set_iv().

77 { buffer_pos = 0; }

## ◆ buffered_block()

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

The block processor, implemented by subclasses

Parameters

  input | some input bytes
  length | the 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 67 of file buf_filt.h.

Referenced by Botan::ECB_Decryption::ECB_Decryption(), and Botan::ECB_Encryption::ECB_Encryption().

67 { return main_block_mod; }

## ◆ buffered_final()

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

The final block, implemented by subclasses

Parameters

  input | some input bytes
  length | the 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 72 of file buf_filt.h.

72 { return 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 82 of file buf_filt.cpp.

References buffered_block(), and buffered_final().

83 {
84 if(buffer_pos < final_minimum)
85 throw std::runtime_error("Buffered filter end_msg without enough input");
86
87 size_t spare_blocks = (buffer_pos - final_minimum) / main_block_mod;
88
89 if(spare_blocks)
90 {
91 size_t spare_bytes = main_block_mod * spare_blocks;
92 buffered_block(&buffer[0], spare_bytes);
93 buffered_final(&buffer[spare_bytes], buffer_pos - spare_bytes);
94 }
95 else
96 {
97 buffered_final(&buffer[0], buffer_pos);
98 }
99
100 buffer_pos = 0;
101 }
Botan::Buffered_Filter::buffered_final
virtual void buffered_final(const byte input[], size_t length)=0
Botan::Buffered_Filter::buffered_block
virtual void buffered_block(const byte input[], size_t length)=0

## ◆ write()

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

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

Parameters

  in | the input bytes
  length | of in in bytes

Definition at line 34 of file buf_filt.cpp.

References buffered_block(), Botan::copy_mem(), Botan::CT::min(), Botan::round_down(), and Botan::MemoryRegion< T >::size().

35 {
36 if(!input_size)
37 return;
38
39 if(buffer_pos + input_size >= main_block_mod + final_minimum)
40 {
41 size_t to_copy = std::min<size_t>(buffer.size() - buffer_pos, input_size);
42
43 copy_mem(&buffer[buffer_pos], input, to_copy);
44 buffer_pos += to_copy;
45
46 input += to_copy;
47 input_size -= to_copy;
48
49 size_t total_to_consume =
50 round_down(std::min(buffer_pos,
51 buffer_pos + input_size - final_minimum),
52 main_block_mod);
53
54 buffered_block(&buffer[0], total_to_consume);
55
56 buffer_pos -= total_to_consume;
57
58 copy_mem(&buffer[0], &buffer[total_to_consume], buffer_pos);
59 }
60
61 if(input_size >= final_minimum)
62 {
63 size_t full_blocks = (input_size - final_minimum) / main_block_mod;
64 size_t to_copy = full_blocks * main_block_mod;
65
66 if(to_copy)
67 {
68 buffered_block(input, to_copy);
69
70 input += to_copy;
71 input_size -= to_copy;
72 }
73 }
74
75 copy_mem(&buffer[buffer_pos], input, input_size);
76 buffer_pos += input_size;
77 }
Botan::round_down
T round_down(T n, T align_to)
Definition: rounding.h:36
Botan::Buffered_Filter::buffered_block
virtual void buffered_block(const byte input[], size_t length)=0
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127

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

- src/filters/buf_filt.h
- src/filters/buf_filt.cpp