7#include "wvcountermode.h"
11 const void *_counter,
size_t _countersize) :
12 keycrypt(_keycrypt), counter(NULL)
18WvCounterModeEncoder::~WvCounterModeEncoder()
28 counter =
new unsigned char[_countersize];
29 countersize = _countersize;
30 memcpy(counter, _counter, countersize);
36 memcpy(_counter, counter, countersize);
42 for (
size_t i = 0; i < countersize && ! ++counter[i]; ++i);
50 size_t avail = inbuf.used();
51 size_t offset = outbuf.used();
55 for (len = avail; len >= countersize; len -= countersize)
57 counterbuf.reset(counter, countersize);
58 success = keycrypt->
encode(counterbuf, outbuf,
true);
62 if (
flush && len != 0 && success)
64 counterbuf.reset(counter, countersize);
65 success = keycrypt->
encode(counterbuf, outbuf,
true);
68 outbuf.unalloc(countersize - len);
73 outbuf.unalloc(outbuf.used() - offset - avail);
80 len = outbuf.optpeekable(offset);
81 unsigned char *dataout = outbuf.mutablepeek(offset, len);
82 size_t lenopt = inbuf.optgettable();
85 const unsigned char *datain = inbuf.get(len);
98 *(dataout++) ^= *(datain++);
WvCounterModeEncoder(WvEncoder *_keycrypt, const void *_counter, size_t _countersize)
Create a new counter mode encoder / decoder.
virtual void incrcounter()
Increments the counter.
void getcounter(void *counter) const
Stores the current counter in the supplied buffer.
void setcounter(const void *counter, size_t countersize)
Sets the Counter mode auto-incrementing counter.
virtual bool _encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
Template method implementation of encode().
bool encode(WvBuf &inbuf, WvBuf &outbuf, bool flush=false, bool finish=false)
Reads data from the input buffer, encodes it, and writes the result to the output buffer.
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.