Boost logo

Boost :

From: Pedro Lamarão (pedro.lamarao_at_[hidden])
Date: 2007-03-24 15:08:50


Sergey escreveu:
> Thanks for suggestion. My crypt_something class meant to be streamed
> but i failed to describe it. crypt_something is buffered 'processor'
> and user can call process() several time
> example:
> crypt_something cs;
> cs.init(encryption_key);
> ...
> while (!input_stream.eof())
> {
> in_buffer = input_stream.read(); //reading to buffer from input stream
> size_t outsize = cs.process_outsize(in_buffer.begin(), in_buffer.end()); //determining
> //amount of memory we will need to write output
> if (out_buffer.size() < outsize)
> out_buffer.resize(outsize);
> stream_buffer::iterator it = cs.process(in_buffer.begin(), in_buffer.end(), out_buffer.begin());
> out_stream.write(out_buffer.begin(), it);
> }
> //flushing buffer
> size_t outsize = cs.flush_outsize<stream_buffer::item_type>();
> if (out_buffer.size() < outsize)
> out_buffer.resize(outsize);
> stream_buffer::iterator it = cs.flush(out_buffer.begin());
> out_stream.write(out_buffer.begin(), it);
>
> It is written in some kind of pseudo-code but i hope it is clear
> enough now :)

I think it is inproper to begin with "streams".
"Streams" imply a source of data and that is irrelevant from a cipher's
point of view.

I'd recommend thinking in terms of algorithm as in <algorithm> --
operations on pairs of iterators or ranges.

Something basic along the lines of:

/**
  * pre-condition: distance(begin, end) == cipher's block size.
  */
template <typename InputIterator, typename OutputIterator>
OutputIterator
cipher_update (InputIterator begin, InputIterator end, OutputIterator out);

/**
  * no precondition as above: padding as necessary.
  */
template <typename InputIterator, typename OutputIterator>
OutputIterator
cipher_finalize (InputIterator begin, InputIterator end, OutputIterator
out);

These generic algorithms could be specialized through "tag dispatching"
as always to increase performance for special cases.

--
  Pedro Lamarão

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk