aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/cipher.h
blob: 9e6f87d9e061533a8f953aef1559d6cb29b2719e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef BU_CIPHER_H
#define BU_CIPHER_H

#include "bu/filter.h"

namespace Bu
{
	template<int iBlockSize>
	class Cipher : public Bu::Filter
	{
	public:
		Cipher( Bu::Stream &rNext ) :
			Bu::Filter( rNext )
		{
		}

		virtual ~Cipher()
		{
		}

		virtual void start()
		{
		}

		virtual Bu::size stop()
		{
			return 0;
		}

		virtual Bu::size read( void *pBuf, Bu::size iBytes )
		{
			uint32_t i;

			if (iBytes%iBlockSize)
			{
				return 0;
			}

			iBytes /= iBlockSize;

			for (i=0;i<iBytes;i++)
			{
				void *pSeg = ((char *)pBuf)+(i*iBlockSize);
				int iRead = rNext.read( pSeg, iBlockSize );
				decipher( pSeg );
			}

			return iBytes*iBlockSize;
		}

		virtual Bu::size write( const void *pBuf, Bu::size iBytes )
		{
			uint32_t i;

			if (iBytes%iBlockSize)
			{
				return 0;
			}

			iBytes /= iBlockSize;

			char buf[iBlockSize];

			for (i=0;i<iBytes;i++)
			{
				memcpy( buf, ((const char *)pBuf)+(i*iBlockSize), 8 );
				encipher( buf );
				rNext.write( buf, iBlockSize );
			}

			memset( &buf, 0, iBlockSize );
			return iBytes*iBlockSize;
		}

		using Bu::Stream::read;
		using Bu::Stream::write;

	protected:
		virtual void encipher( void *pData )=0;
		virtual void decipher( void *pData )=0;
	};
};

#endif