diff options
Diffstat (limited to 'src/experimental/cipher.h')
-rw-r--r-- | src/experimental/cipher.h | 127 |
1 files changed, 0 insertions, 127 deletions
diff --git a/src/experimental/cipher.h b/src/experimental/cipher.h deleted file mode 100644 index 6e58613..0000000 --- a/src/experimental/cipher.h +++ /dev/null | |||
@@ -1,127 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2012 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_CIPHER_H | ||
9 | #define BU_CIPHER_H | ||
10 | |||
11 | #include "bu/filter.h" | ||
12 | #include "bu/util.h" | ||
13 | |||
14 | namespace Bu | ||
15 | { | ||
16 | template<int iBlockSize> | ||
17 | class Cipher : public Bu::Filter | ||
18 | { | ||
19 | public: | ||
20 | Cipher( Bu::Stream &rNext ) : | ||
21 | Bu::Filter( rNext ), | ||
22 | iReadBufFill( 0 ), | ||
23 | iReadBufPos( 0 ), | ||
24 | iWriteBufFill( 0 ) | ||
25 | { | ||
26 | } | ||
27 | |||
28 | virtual ~Cipher() | ||
29 | { | ||
30 | } | ||
31 | |||
32 | virtual void start() | ||
33 | { | ||
34 | } | ||
35 | |||
36 | virtual Bu::size stop() | ||
37 | { | ||
38 | flush(); | ||
39 | return 0; | ||
40 | } | ||
41 | |||
42 | virtual Bu::size read( void *pBuf, Bu::size iBytes ) | ||
43 | { | ||
44 | Bu::size iRead = 0; | ||
45 | while( iRead < iBytes ) | ||
46 | { | ||
47 | if( iReadBufFill < iBlockSize ) | ||
48 | { | ||
49 | int iR = rNext.read( | ||
50 | aReadBuf+iReadBufFill, | ||
51 | iBlockSize-iReadBufFill | ||
52 | ); | ||
53 | if( iR == 0 ) | ||
54 | return iRead; | ||
55 | |||
56 | iReadBufFill += iR; | ||
57 | |||
58 | if( iReadBufFill == iBlockSize ) | ||
59 | decipher( aReadBuf ); | ||
60 | } | ||
61 | |||
62 | if( iReadBufFill == iBlockSize ) | ||
63 | { | ||
64 | int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos ); | ||
65 | memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy ); | ||
66 | iRead += iCpy; | ||
67 | iReadBufPos += iCpy; | ||
68 | if( iReadBufPos == iBlockSize ) | ||
69 | { | ||
70 | iReadBufPos = iReadBufFill = 0; | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | return iRead; | ||
76 | } | ||
77 | |||
78 | virtual Bu::size write( const void *pBuf, Bu::size iBytes ) | ||
79 | { | ||
80 | Bu::size iPos = 0; | ||
81 | |||
82 | while( iPos < iBytes ) | ||
83 | { | ||
84 | int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill); | ||
85 | memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft ); | ||
86 | iPos += iLeft; | ||
87 | iWriteBufFill += iLeft; | ||
88 | if( iWriteBufFill == iBlockSize ) | ||
89 | { | ||
90 | encipher( aWriteBuf ); | ||
91 | rNext.write( aWriteBuf, iBlockSize ); | ||
92 | iWriteBufFill = 0; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | return iPos; | ||
97 | } | ||
98 | |||
99 | virtual void flush() | ||
100 | { | ||
101 | if( iWriteBufFill > 0 && iWriteBufFill < iBlockSize ) | ||
102 | { | ||
103 | memset( aWriteBuf+iWriteBufFill, 0, iBlockSize-iWriteBufFill ); | ||
104 | encipher( aWriteBuf ); | ||
105 | rNext.write( aWriteBuf, iBlockSize ); | ||
106 | iWriteBufFill = 0; | ||
107 | } | ||
108 | rNext.flush(); | ||
109 | } | ||
110 | |||
111 | using Bu::Stream::read; | ||
112 | using Bu::Stream::write; | ||
113 | |||
114 | protected: | ||
115 | virtual void encipher( void *pData )=0; | ||
116 | virtual void decipher( void *pData )=0; | ||
117 | |||
118 | private: | ||
119 | char aReadBuf[iBlockSize]; | ||
120 | char aWriteBuf[iBlockSize]; | ||
121 | int iReadBufFill; | ||
122 | int iReadBufPos; | ||
123 | int iWriteBufFill; | ||
124 | }; | ||
125 | }; | ||
126 | |||
127 | #endif | ||