Botan  1.11.15
src/lib/filters/buf_filt.h
Go to the documentation of this file.
00001 /*
00002 * Buffered Filter
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_BUFFERED_FILTER_H__
00009 #define BOTAN_BUFFERED_FILTER_H__
00010 
00011 #include <botan/secmem.h>
00012 
00013 namespace Botan {
00014 
00015 /**
00016 * Filter mixin that breaks input into blocks, useful for
00017 * cipher modes
00018 */
00019 class BOTAN_DLL Buffered_Filter
00020    {
00021    public:
00022       /**
00023       * Write bytes into the buffered filter, which will them emit them
00024       * in calls to buffered_block in the subclass
00025       * @param in the input bytes
00026       * @param length of in in bytes
00027       */
00028       void write(const byte in[], size_t length);
00029 
00030       template<typename Alloc>
00031          void write(const std::vector<byte, Alloc>& in, size_t length)
00032          {
00033          write(&in[0], length);
00034          }
00035 
00036       /**
00037       * Finish a message, emitting to buffered_block and buffered_final
00038       * Will throw an exception if less than final_minimum bytes were
00039       * written into the filter.
00040       */
00041       void end_msg();
00042 
00043       /**
00044       * Initialize a Buffered_Filter
00045       * @param block_size the function buffered_block will be called
00046       *        with inputs which are a multiple of this size
00047       * @param final_minimum the function buffered_final will be called
00048       *        with at least this many bytes.
00049       */
00050       Buffered_Filter(size_t block_size, size_t final_minimum);
00051 
00052       virtual ~Buffered_Filter() {}
00053    protected:
00054       /**
00055       * The block processor, implemented by subclasses
00056       * @param input some input bytes
00057       * @param length the size of input, guaranteed to be a multiple
00058       *        of block_size
00059       */
00060       virtual void buffered_block(const byte input[], size_t length) = 0;
00061 
00062       /**
00063       * The final block, implemented by subclasses
00064       * @param input some input bytes
00065       * @param length the size of input, guaranteed to be at least
00066       *        final_minimum bytes
00067       */
00068       virtual void buffered_final(const byte input[], size_t length) = 0;
00069 
00070       /**
00071       * @return block size of inputs
00072       */
00073       size_t buffered_block_size() const { return main_block_mod; }
00074 
00075       /**
00076       * @return current position in the buffer
00077       */
00078       size_t current_position() const { return buffer_pos; }
00079 
00080       /**
00081       * Reset the buffer position
00082       */
00083       void buffer_reset() { buffer_pos = 0; }
00084    private:
00085       size_t main_block_mod, final_minimum;
00086 
00087       secure_vector<byte> buffer;
00088       size_t buffer_pos;
00089    };
00090 
00091 }
00092 
00093 #endif