WvStreams
wvxor.cc
1/*
2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * XOR cryptography abstractions.
6 * Could use this to implement short one time pads.
7 */
8#include "wvxor.h"
9
10/***** WvXOREncoder *****/
11
12WvXOREncoder::WvXOREncoder(const void *_key, size_t _keylen) :
13 keylen(_keylen), keyoff(0)
14{
15 key = new unsigned char[keylen];
16 memcpy(key, _key, keylen);
17}
18
19
20WvXOREncoder::~WvXOREncoder()
21{
22 deletev key;
23}
24
25
26bool WvXOREncoder::_encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
27{
28 size_t len;
29 while ((len = inbuf.optgettable()) != 0)
30 {
31 const unsigned char *data = inbuf.get(len);
32 unsigned char *out = outbuf.alloc(len);
33
34 // FIXME: this loop is SLOW! (believe it or not)
35 while (len-- > 0)
36 {
37 *out++ = (*data++) ^ key[keyoff++];
38 keyoff %= keylen;
39 }
40 }
41 return true;
42}
43
44/***** WvXORStream *****/
45
46WvXORStream::WvXORStream(WvStream *_cloned,
47 const void *_key, size_t _keysize) :
48 WvEncoderStream(_cloned)
49{
50 readchain.append(new WvXOREncoder(_key, _keysize), true);
51 writechain.append(new WvXOREncoder(_key, _keysize), true);
52}
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition: wvbufbase.h:154
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
Definition: wvbufbase.h:114
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition: wvbufbase.h:379
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:24
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:25
bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvxor.cc:26
WvXOREncoder(const void *_key, size_t _keylen)
Creates a new XOR encoder / decoder.
Definition: wvxor.cc:12
#define deletev
Remplacement for delete[].
Definition: delete.h:129