aboutsummaryrefslogtreecommitdiffstats
path: root/src/buf_filt.cpp
blob: 20c223be558e12621ade27268aecdb4b8352360c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*************************************************
* Buffering Filter Source File                   *
* (C) 1999-2007 The Botan Project                *
*************************************************/

#include <botan/buf_filt.h>
#include <algorithm>

namespace Botan {

/*************************************************
* Buffering_Filter Constructor                   *
*************************************************/
Buffering_Filter::Buffering_Filter(u32bit b, u32bit i) : INITIAL_BLOCK_SIZE(i),
                                                         BLOCK_SIZE(b)
   {
   initial_block_pos = block_pos = 0;
   initial.create(INITIAL_BLOCK_SIZE);
   block.create(BLOCK_SIZE);
   }

/*************************************************
* Reset the Buffering Filter                     *
*************************************************/
void Buffering_Filter::end_msg()
   {
   if(initial_block_pos != INITIAL_BLOCK_SIZE)
      throw Exception("Buffering_Filter: Not enough data for first block");
   final_block(block, block_pos);
   initial_block_pos = block_pos = 0;
   initial.clear();
   block.clear();
   }

/*************************************************
* Buffer input into blocks                       *
*************************************************/
void Buffering_Filter::write(const byte input[], u32bit length)
   {
   if(initial_block_pos != INITIAL_BLOCK_SIZE)
      {
      u32bit copied = std::min(INITIAL_BLOCK_SIZE - initial_block_pos, length);
      initial.copy(initial_block_pos, input, copied);
      input += copied;
      length -= copied;
      initial_block_pos += copied;
      if(initial_block_pos == INITIAL_BLOCK_SIZE)
         initial_block(initial);
      }
   block.copy(block_pos, input, length);
   if(block_pos + length >= BLOCK_SIZE)
      {
      main_block(block);
      input += (BLOCK_SIZE - block_pos);
      length -= (BLOCK_SIZE - block_pos);
      while(length >= BLOCK_SIZE)
         {
         main_block(input);
         input += BLOCK_SIZE;
         length -= BLOCK_SIZE;
         }
      block.copy(input, length);
      block_pos = 0;
      }
   block_pos += length;
   }

}