diff options
author | lloyd <[email protected]> | 2010-02-03 17:37:08 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-02-03 17:37:08 +0000 |
commit | c9ce8388a2fd3fb93d5afead65626eef3d2d938b (patch) | |
tree | c84c51ec4327823a124dcc48430388ecee399853 | |
parent | 25a7fa86f6f9f5b3f91114db357fa044b92db471 (diff) |
Fix some buffering and off-by-one errors in Buffering_Filter::write
Still not totally convinced this code is correct.
-rw-r--r-- | src/filters/buf_filt.cpp | 29 |
1 files changed, 10 insertions, 19 deletions
diff --git a/src/filters/buf_filt.cpp b/src/filters/buf_filt.cpp index 97dd1b890..9b4f4af88 100644 --- a/src/filters/buf_filt.cpp +++ b/src/filters/buf_filt.cpp @@ -12,14 +12,6 @@ namespace Botan { -namespace { - -const size_t BUFFER_MULTIPLE = 2; - -//static_assert(BUFFER_MULTIPLE >= 2, "BUFFER_MULTIPLE must be >= 2"); - -} - /* * Buffered_Filter Constructor */ @@ -32,7 +24,7 @@ Buffered_Filter::Buffered_Filter(u32bit b, u32bit f) : if(final_minimum > main_block_mod) throw std::invalid_argument("final_minimum > main_block_mod"); - buffer.resize(BUFFER_MULTIPLE * main_block_mod); + buffer.resize(2 * main_block_mod); buffer_pos = 0; } @@ -54,23 +46,22 @@ void Buffered_Filter::write(const byte input[], u32bit input_size) input += to_copy; input_size -= to_copy; - if(input_size >= final_minimum) - { - u32bit to_proc_blocks = buffer_pos / main_block_mod; - u32bit to_proc_bytes = to_proc_blocks * main_block_mod; + u32bit total_to_consume = + round_down(std::min(buffer_pos, + buffer_pos + input_size - final_minimum), + main_block_mod); - buffered_block(&buffer[0], to_proc_bytes); + buffered_block(&buffer[0], total_to_consume); - buffer_pos -= to_proc_bytes; + buffer_pos -= total_to_consume; - copy_mem(&buffer[0], &buffer[to_proc_bytes], buffer_pos); - } + copy_mem(&buffer[0], &buffer[total_to_consume], buffer_pos); } if(input_size >= final_minimum) { - u32bit full_blocks = (input_size - final_minimum) / buffer.size(); - u32bit to_copy = full_blocks * buffer.size(); + u32bit full_blocks = (input_size - final_minimum) / main_block_mod; + u32bit to_copy = full_blocks * main_block_mod; if(to_copy) { |