monotone

monotone Mtn Source Tree

Root/botan/buf_filt.cpp

1/*************************************************
2* Buffering Filter Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/buf_filt.h>
7#include <algorithm>
8
9namespace Botan {
10
11/*************************************************
12* Buffering_Filter Constructor *
13*************************************************/
14Buffering_Filter::Buffering_Filter(u32bit b, u32bit i) : INITIAL_BLOCK_SIZE(i),
15 BLOCK_SIZE(b)
16 {
17 initial_block_pos = block_pos = 0;
18 initial.create(INITIAL_BLOCK_SIZE);
19 block.create(BLOCK_SIZE);
20 }
21
22/*************************************************
23* Reset the Buffering Filter *
24*************************************************/
25void Buffering_Filter::end_msg()
26 {
27 if(initial_block_pos != INITIAL_BLOCK_SIZE)
28 throw Exception("Buffering_Filter: Not enough data for first block");
29 final_block(block, block_pos);
30 initial_block_pos = block_pos = 0;
31 initial.clear();
32 block.clear();
33 }
34
35/*************************************************
36* Buffer input into blocks *
37*************************************************/
38void Buffering_Filter::write(const byte input[], u32bit length)
39 {
40 if(initial_block_pos != INITIAL_BLOCK_SIZE)
41 {
42 u32bit copied = std::min(INITIAL_BLOCK_SIZE - initial_block_pos, length);
43 initial.copy(initial_block_pos, input, copied);
44 input += copied;
45 length -= copied;
46 initial_block_pos += copied;
47 if(initial_block_pos == INITIAL_BLOCK_SIZE)
48 initial_block(initial);
49 }
50 block.copy(block_pos, input, length);
51 if(block_pos + length >= BLOCK_SIZE)
52 {
53 main_block(block);
54 input += (BLOCK_SIZE - block_pos);
55 length -= (BLOCK_SIZE - block_pos);
56 while(length >= BLOCK_SIZE)
57 {
58 main_block(input);
59 input += BLOCK_SIZE;
60 length -= BLOCK_SIZE;
61 }
62 block.copy(input, length);
63 block_pos = 0;
64 }
65 block_pos += length;
66 }
67
68}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status