aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/base64.cpp31
-rw-r--r--src/base64.h3
-rw-r--r--src/tests/base64.cpp6
3 files changed, 30 insertions, 10 deletions
diff --git a/src/base64.cpp b/src/base64.cpp
index f6b8cce..c9a6b59 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -8,6 +8,9 @@ Bu::Base64::Base64( Bu::Stream &rNext ) :
8 Bu::Filter( rNext ), 8 Bu::Filter( rNext ),
9 iBPos( 0 ), 9 iBPos( 0 ),
10 iBuf( 0 ), 10 iBuf( 0 ),
11 iRPos( 0 ),
12 iChars( 0 ),
13 bEosIn( false ),
11 iTotalIn( 0 ), 14 iTotalIn( 0 ),
12 iTotalOut( 0 ), 15 iTotalOut( 0 ),
13 eMode( Nothing ) 16 eMode( Nothing )
@@ -79,10 +82,25 @@ size_t Bu::Base64::stop()
79 82
80size_t Bu::Base64::read( void *pBuf, size_t nBytes ) 83size_t Bu::Base64::read( void *pBuf, size_t nBytes )
81{ 84{
85 if( bEosIn == true && iRPos == iChars )
86 return 0;
82 size_t sIn = 0; 87 size_t sIn = 0;
83 char buf[4]; 88 char buf[4];
84 while( sIn < nBytes ) 89 while( sIn < nBytes )
85 { 90 {
91 for(; iRPos < iChars && sIn < nBytes; iRPos++, sIn++ )
92 {
93 ((unsigned char *)pBuf)[sIn] = (iBuf>>(8*(2-iRPos)))&0xFF;
94 }
95 if( iRPos == iChars )
96 {
97 if( bEosIn == true )
98 return sIn;
99 else
100 iRPos = 0;
101 }
102 else if( sIn == nBytes )
103 return sIn;
86 //if( rNext.read( buf, 4 ) == 0 ) 104 //if( rNext.read( buf, 4 ) == 0 )
87 // return sIn; 105 // return sIn;
88 for( int j = 0; j < 4; j++ ) 106 for( int j = 0; j < 4; j++ )
@@ -95,20 +113,19 @@ size_t Bu::Base64::read( void *pBuf, size_t nBytes )
95 j--; 113 j--;
96 } 114 }
97 } 115 }
98 int iChars = 3; 116 iChars = 3;
117 iBuf = 0;
99 for( int j = 0; j < 4; j++ ) 118 for( int j = 0; j < 4; j++ )
100 { 119 {
101 if( buf[j] == '=' ) 120 if( buf[j] == '=' )
121 {
102 iChars--; 122 iChars--;
123 bEosIn = true;
124 }
103 else 125 else
104 iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6); 126 iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6);
105 printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); 127 //printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) );
106 } 128 }
107 for( int j = 0; j < iChars; j++ )
108 {
109 ((unsigned char *)pBuf)[sIn++] = (iBuf>>(8*(2-j)))&0xFF;
110 }
111 iBuf = 0;
112 } 129 }
113 130
114 return sIn; 131 return sIn;
diff --git a/src/base64.h b/src/base64.h
index 41264e0..c7fb737 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -25,6 +25,9 @@ namespace Bu
25 private: 25 private:
26 int iBPos; 26 int iBPos;
27 int iBuf; 27 int iBuf;
28 int iRPos;
29 int iChars;
30 bool bEosIn;
28 size_t iTotalIn; 31 size_t iTotalIn;
29 size_t iTotalOut; 32 size_t iTotalOut;
30 static const char tblEnc[65]; 33 static const char tblEnc[65];
diff --git a/src/tests/base64.cpp b/src/tests/base64.cpp
index eaf6a16..3122d5c 100644
--- a/src/tests/base64.cpp
+++ b/src/tests/base64.cpp
@@ -31,12 +31,12 @@ int main( int argc, char *argv[] )
31 Bu::File fOut( argv[1], Bu::File::WriteNew ); 31 Bu::File fOut( argv[1], Bu::File::WriteNew );
32 Bu::Base64 bIn( fIn ); 32 Bu::Base64 bIn( fIn );
33 33
34 char buf[900]; 34 char buf[16];
35 for(;;) 35 for(;;)
36 { 36 {
37 int iRead = bIn.read( buf, 900 ); 37 int iRead = bIn.read( buf, 16 );
38 fOut.write( buf, iRead ); 38 fOut.write( buf, iRead );
39 if( iRead < 900 ) 39 if( iRead < 16 )
40 break; 40 break;
41 } 41 }
42 } 42 }