aboutsummaryrefslogtreecommitdiff
path: root/src/experimental
diff options
context:
space:
mode:
Diffstat (limited to 'src/experimental')
-rw-r--r--src/experimental/cipher.h87
1 files changed, 55 insertions, 32 deletions
diff --git a/src/experimental/cipher.h b/src/experimental/cipher.h
index 9e6f87d..6e1fa69 100644
--- a/src/experimental/cipher.h
+++ b/src/experimental/cipher.h
@@ -2,6 +2,7 @@
2#define BU_CIPHER_H 2#define BU_CIPHER_H
3 3
4#include "bu/filter.h" 4#include "bu/filter.h"
5#include "bu/util.h"
5 6
6namespace Bu 7namespace Bu
7{ 8{
@@ -10,7 +11,10 @@ namespace Bu
10 { 11 {
11 public: 12 public:
12 Cipher( Bu::Stream &rNext ) : 13 Cipher( Bu::Stream &rNext ) :
13 Bu::Filter( rNext ) 14 Bu::Filter( rNext ),
15 iReadBufFill( 0 ),
16 iReadBufPos( 0 ),
17 iWriteBufFill( 0 )
14 { 18 {
15 } 19 }
16 20
@@ -29,47 +33,59 @@ namespace Bu
29 33
30 virtual Bu::size read( void *pBuf, Bu::size iBytes ) 34 virtual Bu::size read( void *pBuf, Bu::size iBytes )
31 { 35 {
32 uint32_t i; 36 Bu::size iRead = 0;
33 37 while( iRead < iBytes )
34 if (iBytes%iBlockSize)
35 {
36 return 0;
37 }
38
39 iBytes /= iBlockSize;
40
41 for (i=0;i<iBytes;i++)
42 { 38 {
43 void *pSeg = ((char *)pBuf)+(i*iBlockSize); 39 if( iReadBufFill < iBlockSize )
44 int iRead = rNext.read( pSeg, iBlockSize ); 40 {
45 decipher( pSeg ); 41 int iR = rNext.read(
42 aReadBuf+iReadBufFill,
43 iBlockSize-iReadBufFill
44 );
45 if( iR == 0 )
46 return iRead;
47
48 iReadBufFill += iR;
49
50 if( iReadBufFill == iBlockSize )
51 decipher( aReadBuf );
52 }
53
54 if( iReadBufFill == iBlockSize )
55 {
56 int iCpy = Bu::min( (int)(iBytes-iRead), iBlockSize-iReadBufPos );
57 memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy );
58 iRead += iCpy;
59 iReadBufPos += iCpy;
60 if( iReadBufPos == iBlockSize )
61 {
62 iReadBufPos = iReadBufFill = 0;
63 }
64 }
46 } 65 }
47 66
48 return iBytes*iBlockSize; 67 return iRead;
49 } 68 }
50 69
51 virtual Bu::size write( const void *pBuf, Bu::size iBytes ) 70 virtual Bu::size write( const void *pBuf, Bu::size iBytes )
52 { 71 {
53 uint32_t i; 72 Bu::size iPos = 0;
54 73
55 if (iBytes%iBlockSize) 74 while( iPos < iBytes )
56 { 75 {
57 return 0; 76 int iLeft = Bu::min((int)(iBytes-iPos),iBlockSize-iReadBufFill);
77 memcpy( aReadBuf+iReadBufFill, (char *)pBuf+iPos, iLeft );
78 iPos += iLeft;
79 iReadBufFill += iLeft;
80 if( iReadBufFill == iBlockSize )
81 {
82 encipher( aReadBuf );
83 rNext.write( aReadBuf, iBlockSize );
84 iReadBufFill = 0;
85 }
58 } 86 }
59 87
60 iBytes /= iBlockSize; 88 return iPos;
61
62 char buf[iBlockSize];
63
64 for (i=0;i<iBytes;i++)
65 {
66 memcpy( buf, ((const char *)pBuf)+(i*iBlockSize), 8 );
67 encipher( buf );
68 rNext.write( buf, iBlockSize );
69 }
70
71 memset( &buf, 0, iBlockSize );
72 return iBytes*iBlockSize;
73 } 89 }
74 90
75 using Bu::Stream::read; 91 using Bu::Stream::read;
@@ -78,6 +94,13 @@ namespace Bu
78 protected: 94 protected:
79 virtual void encipher( void *pData )=0; 95 virtual void encipher( void *pData )=0;
80 virtual void decipher( void *pData )=0; 96 virtual void decipher( void *pData )=0;
97
98 private:
99 char aReadBuf[iBlockSize];
100 char aWriteBuf[iBlockSize];
101 int iReadBufFill;
102 int iReadBufPos;
103 int iWriteBufFill;
81 }; 104 };
82}; 105};
83 106