Botan  1.11.15
Public Member Functions | Protected Member Functions
Botan::Buffered_Filter Class Reference

#include <buf_filt.h>

Inheritance diagram for Botan::Buffered_Filter:
Botan::Transform_Filter Botan::AEAD_Filter

List of all members.

Public Member Functions

 Buffered_Filter (size_t block_size, size_t final_minimum)
void end_msg ()
void write (const byte in[], size_t length)
template<typename Alloc >
void write (const std::vector< byte, Alloc > &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

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.

                                                   :
   main_block_mod(b), final_minimum(f)
   {
   if(main_block_mod == 0)
      throw std::invalid_argument("main_block_mod == 0");

   if(final_minimum > main_block_mod)
      throw std::invalid_argument("final_minimum > main_block_mod");

   buffer.resize(2 * main_block_mod);
   buffer_pos = 0;
   }
virtual Botan::Buffered_Filter::~Buffered_Filter ( ) [inline, virtual]

Definition at line 52 of file buf_filt.h.

{}

Member Function Documentation

void Botan::Buffered_Filter::buffer_reset ( ) [inline, protected]

Reset the buffer position

Definition at line 83 of file buf_filt.h.

{ buffer_pos = 0; }
virtual void Botan::Buffered_Filter::buffered_block ( const byte  input[],
size_t  length 
) [protected, pure 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().

size_t Botan::Buffered_Filter::buffered_block_size ( ) const [inline, protected]
Returns:
block size of inputs

Definition at line 73 of file buf_filt.h.

{ return main_block_mod; }
virtual void Botan::Buffered_Filter::buffered_final ( const byte  input[],
size_t  length 
) [protected, pure 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().

size_t Botan::Buffered_Filter::current_position ( ) const [inline, protected]
Returns:
current position in the buffer

Definition at line 78 of file buf_filt.h.

{ return buffer_pos; }

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().

   {
   if(buffer_pos < final_minimum)
      throw std::runtime_error("Buffered filter end_msg without enough input");

   size_t spare_blocks = (buffer_pos - final_minimum) / main_block_mod;

   if(spare_blocks)
      {
      size_t spare_bytes = main_block_mod * spare_blocks;
      buffered_block(&buffer[0], spare_bytes);
      buffered_final(&buffer[spare_bytes], buffer_pos - spare_bytes);
      }
   else
      {
      buffered_final(&buffer[0], buffer_pos);
      }

   buffer_pos = 0;
   }
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:
inthe input bytes
lengthof in in bytes

Definition at line 34 of file buf_filt.cpp.

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

   {
   if(!input_size)
      return;

   if(buffer_pos + input_size >= main_block_mod + final_minimum)
      {
      size_t to_copy = std::min<size_t>(buffer.size() - buffer_pos, input_size);

      copy_mem(&buffer[buffer_pos], input, to_copy);
      buffer_pos += to_copy;

      input += to_copy;
      input_size -= to_copy;

      size_t total_to_consume =
         round_down(std::min(buffer_pos,
                             buffer_pos + input_size - final_minimum),
                    main_block_mod);

      buffered_block(&buffer[0], total_to_consume);

      buffer_pos -= total_to_consume;

      copy_mem(&buffer[0], &buffer[0] + total_to_consume, buffer_pos);
      }

   if(input_size >= final_minimum)
      {
      size_t full_blocks = (input_size - final_minimum) / main_block_mod;
      size_t to_copy = full_blocks * main_block_mod;

      if(to_copy)
         {
         buffered_block(input, to_copy);

         input += to_copy;
         input_size -= to_copy;
         }
      }

   copy_mem(&buffer[buffer_pos], input, input_size);
   buffer_pos += input_size;
   }
template<typename Alloc >
void Botan::Buffered_Filter::write ( const std::vector< byte, Alloc > &  in,
size_t  length 
) [inline]

Definition at line 31 of file buf_filt.h.

         {
         write(&in[0], length);
         }

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