summaryrefslogtreecommitdiff
path: root/src/experimental/cipher.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/experimental/cipher.h')
-rw-r--r--src/experimental/cipher.h218
1 files changed, 109 insertions, 109 deletions
diff --git a/src/experimental/cipher.h b/src/experimental/cipher.h
index 5d5cb07..6e58613 100644
--- a/src/experimental/cipher.h
+++ b/src/experimental/cipher.h
@@ -13,115 +13,115 @@
13 13
14namespace Bu 14namespace Bu
15{ 15{
16 template<int iBlockSize> 16 template<int iBlockSize>
17 class Cipher : public Bu::Filter 17 class Cipher : public Bu::Filter
18 { 18 {
19 public: 19 public:
20 Cipher( Bu::Stream &rNext ) : 20 Cipher( Bu::Stream &rNext ) :
21 Bu::Filter( rNext ), 21 Bu::Filter( rNext ),
22 iReadBufFill( 0 ), 22 iReadBufFill( 0 ),
23 iReadBufPos( 0 ), 23 iReadBufPos( 0 ),
24 iWriteBufFill( 0 ) 24 iWriteBufFill( 0 )
25 { 25 {
26 } 26 }
27 27
28 virtual ~Cipher() 28 virtual ~Cipher()
29 { 29 {
30 } 30 }
31 31
32 virtual void start() 32 virtual void start()
33 { 33 {
34 } 34 }
35 35
36 virtual Bu::size stop() 36 virtual Bu::size stop()
37 { 37 {
38 flush(); 38 flush();
39 return 0; 39 return 0;
40 } 40 }
41 41
42 virtual Bu::size read( void *pBuf, Bu::size iBytes ) 42 virtual Bu::size read( void *pBuf, Bu::size iBytes )
43 { 43 {
44 Bu::size iRead = 0; 44 Bu::size iRead = 0;
45 while( iRead < iBytes ) 45 while( iRead < iBytes )
46 { 46 {
47 if( iReadBufFill < iBlockSize ) 47 if( iReadBufFill < iBlockSize )
48 { 48 {
49 int iR = rNext.read( 49 int iR = rNext.read(
50 aReadBuf+iReadBufFill, 50 aReadBuf+iReadBufFill,
51 iBlockSize-iReadBufFill 51 iBlockSize-iReadBufFill
52 ); 52 );
53 if( iR == 0 ) 53 if( iR == 0 )
54 return iRead; 54 return iRead;
55 55
56 iReadBufFill += iR; 56 iReadBufFill += iR;
57 57
58 if( iReadBufFill == iBlockSize ) 58 if( iReadBufFill == iBlockSize )
59 decipher( aReadBuf ); 59 decipher( aReadBuf );
60 } 60 }
61 61
62 if( iReadBufFill == iBlockSize ) 62 if( iReadBufFill == iBlockSize )
63 { 63 {
64 int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos ); 64 int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos );
65 memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy ); 65 memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy );
66 iRead += iCpy; 66 iRead += iCpy;
67 iReadBufPos += iCpy; 67 iReadBufPos += iCpy;
68 if( iReadBufPos == iBlockSize ) 68 if( iReadBufPos == iBlockSize )
69 { 69 {
70 iReadBufPos = iReadBufFill = 0; 70 iReadBufPos = iReadBufFill = 0;
71 } 71 }
72 } 72 }
73 } 73 }
74 74
75 return iRead; 75 return iRead;
76 } 76 }
77 77
78 virtual Bu::size write( const void *pBuf, Bu::size iBytes ) 78 virtual Bu::size write( const void *pBuf, Bu::size iBytes )
79 { 79 {
80 Bu::size iPos = 0; 80 Bu::size iPos = 0;
81 81
82 while( iPos < iBytes ) 82 while( iPos < iBytes )
83 { 83 {
84 int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill); 84 int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill);
85 memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft ); 85 memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft );
86 iPos += iLeft; 86 iPos += iLeft;
87 iWriteBufFill += iLeft; 87 iWriteBufFill += iLeft;
88 if( iWriteBufFill == iBlockSize ) 88 if( iWriteBufFill == iBlockSize )
89 { 89 {
90 encipher( aWriteBuf ); 90 encipher( aWriteBuf );
91 rNext.write( aWriteBuf, iBlockSize ); 91 rNext.write( aWriteBuf, iBlockSize );
92 iWriteBufFill = 0; 92 iWriteBufFill = 0;
93 } 93 }
94 } 94 }
95 95
96 return iPos; 96 return iPos;
97 } 97 }
98 98
99 virtual void flush() 99 virtual void flush()
100 { 100 {
101 if( iWriteBufFill > 0 && iWriteBufFill < iBlockSize ) 101 if( iWriteBufFill > 0 && iWriteBufFill < iBlockSize )
102 { 102 {
103 memset( aWriteBuf+iWriteBufFill, 0, iBlockSize-iWriteBufFill ); 103 memset( aWriteBuf+iWriteBufFill, 0, iBlockSize-iWriteBufFill );
104 encipher( aWriteBuf ); 104 encipher( aWriteBuf );
105 rNext.write( aWriteBuf, iBlockSize ); 105 rNext.write( aWriteBuf, iBlockSize );
106 iWriteBufFill = 0; 106 iWriteBufFill = 0;
107 } 107 }
108 rNext.flush(); 108 rNext.flush();
109 } 109 }
110 110
111 using Bu::Stream::read; 111 using Bu::Stream::read;
112 using Bu::Stream::write; 112 using Bu::Stream::write;
113 113
114 protected: 114 protected:
115 virtual void encipher( void *pData )=0; 115 virtual void encipher( void *pData )=0;
116 virtual void decipher( void *pData )=0; 116 virtual void decipher( void *pData )=0;
117 117
118 private: 118 private:
119 char aReadBuf[iBlockSize]; 119 char aReadBuf[iBlockSize];
120 char aWriteBuf[iBlockSize]; 120 char aWriteBuf[iBlockSize];
121 int iReadBufFill; 121 int iReadBufFill;
122 int iReadBufPos; 122 int iReadBufPos;
123 int iWriteBufFill; 123 int iWriteBufFill;
124 }; 124 };
125}; 125};
126 126
127#endif 127#endif