Botan  2.7.0
Crypto and TLS for C++11
buf_filt.h
Go to the documentation of this file.
1 /*
2 * Buffered Filter
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_BUFFERED_FILTER_H_
9 #define BOTAN_BUFFERED_FILTER_H_
10 
11 #include <botan/secmem.h>
12 
13 namespace Botan {
14 
15 /**
16 * Filter mixin that breaks input into blocks, useful for
17 * cipher modes
18 */
20  {
21  public:
22  /**
23  * Write bytes into the buffered filter, which will them emit them
24  * in calls to buffered_block in the subclass
25  * @param in the input bytes
26  * @param length of in in bytes
27  */
28  void write(const uint8_t in[], size_t length);
29 
30  template<typename Alloc>
31  void write(const std::vector<uint8_t, Alloc>& in, size_t length)
32  {
33  write(in.data(), length);
34  }
35 
36  /**
37  * Finish a message, emitting to buffered_block and buffered_final
38  * Will throw an exception if less than final_minimum bytes were
39  * written into the filter.
40  */
41  void end_msg();
42 
43  /**
44  * Initialize a Buffered_Filter
45  * @param block_size the function buffered_block will be called
46  * with inputs which are a multiple of this size
47  * @param final_minimum the function buffered_final will be called
48  * with at least this many bytes.
49  */
50  Buffered_Filter(size_t block_size, size_t final_minimum);
51 
52  virtual ~Buffered_Filter() = default;
53  protected:
54  /**
55  * The block processor, implemented by subclasses
56  * @param input some input bytes
57  * @param length the size of input, guaranteed to be a multiple
58  * of block_size
59  */
60  virtual void buffered_block(const uint8_t input[], size_t length) = 0;
61 
62  /**
63  * The final block, implemented by subclasses
64  * @param input some input bytes
65  * @param length the size of input, guaranteed to be at least
66  * final_minimum bytes
67  */
68  virtual void buffered_final(const uint8_t input[], size_t length) = 0;
69 
70  /**
71  * @return block size of inputs
72  */
73  size_t buffered_block_size() const { return m_main_block_mod; }
74 
75  /**
76  * @return current position in the buffer
77  */
78  size_t current_position() const { return m_buffer_pos; }
79 
80  /**
81  * Reset the buffer position
82  */
83  void buffer_reset() { m_buffer_pos = 0; }
84  private:
85  size_t m_main_block_mod, m_final_minimum;
86 
87  secure_vector<uint8_t> m_buffer;
88  size_t m_buffer_pos;
89  };
90 
91 }
92 
93 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
size_t current_position() const
Definition: buf_filt.h:78
void write(const std::vector< uint8_t, Alloc > &in, size_t length)
Definition: buf_filt.h:31
Definition: alg_id.cpp:13
size_t buffered_block_size() const
Definition: buf_filt.h:73
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88