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 
12 WvXOREncoder::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 
20 WvXOREncoder::~WvXOREncoder()
21 {
22  deletev key;
23 }
24 
25 
26 bool 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 
46 WvXORStream::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 }
WvBufBaseCommonImpl::get
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
WvEncoderStream
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
Definition: wvencoderstream.h:37
WvBufBaseCommonImpl::alloc
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
Definition: wvbufbase.h:379
WvXOREncoder::WvXOREncoder
WvXOREncoder(const void *_key, size_t _keylen)
Creates a new XOR encoder / decoder.
Definition: wvxor.cc:12
WvBufBaseCommonImpl::optgettable
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
Definition: wvbufbase.h:154
WvBufBase< unsigned char >
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
Definition: wvbuf.h:22
deletev
#define deletev
Remplacement for delete[].
Definition: delete.h:129
WvStream
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:24
WvXOREncoder::_encode
bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition: wvxor.cc:26