aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/cipher.h
blob: 5d5cb073bcedf18785365a522265452dc57ab5cc (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_CIPHER_H
#define BU_CIPHER_H

#include "bu/filter.h"
#include "bu/util.h"

namespace Bu
{
	template<int iBlockSize>
	class Cipher : public Bu::Filter
	{
	public:
		Cipher( Bu::Stream &rNext ) :
			Bu::Filter( rNext ),
			iReadBufFill( 0 ),
			iReadBufPos( 0 ),
			iWriteBufFill( 0 )
		{
		}

		virtual ~Cipher()
		{
		}

		virtual void start()
		{
		}

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

		virtual Bu::size read( void *pBuf, Bu::size iBytes )
		{
			Bu::size iRead = 0;
			while( iRead < iBytes )
			{
				if( iReadBufFill < iBlockSize )
				{
					int iR = rNext.read(
						aReadBuf+iReadBufFill,
						iBlockSize-iReadBufFill
						);
					if( iR == 0 )
						return iRead;

					iReadBufFill += iR;

					if( iReadBufFill == iBlockSize )
						decipher( aReadBuf );
				}

				if( iReadBufFill == iBlockSize )
				{
					int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos );
					memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy );
					iRead += iCpy;
					iReadBufPos += iCpy;
					if( iReadBufPos == iBlockSize )
					{
						iReadBufPos = iReadBufFill = 0;
					}
				}
			}

			return iRead;
		}

		virtual Bu::size write( const void *pBuf, Bu::size iBytes )
		{
			Bu::size iPos = 0;
			
			while( iPos < iBytes )
			{
				int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill);
				memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft );
				iPos += iLeft;
				iWriteBufFill += iLeft;
				if( iWriteBufFill == iBlockSize )
				{
					encipher( aWriteBuf );
					rNext.write( aWriteBuf, iBlockSize );
					iWriteBufFill = 0;
				}
			}

			return iPos;
		}

		virtual void flush()
		{
			if( iWriteBufFill > 0 && iWriteBufFill < iBlockSize )
			{
				memset( aWriteBuf+iWriteBufFill, 0, iBlockSize-iWriteBufFill );
				encipher( aWriteBuf );
				rNext.write( aWriteBuf, iBlockSize );
				iWriteBufFill = 0;
			}
			rNext.flush();
		}

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

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

	private:
		char aReadBuf[iBlockSize];
		char aWriteBuf[iBlockSize];
		int iReadBufFill;
		int iReadBufPos;
		int iWriteBufFill;
	};
};

#endif