From 745a9ec043fdbe91faf26a3cfec4ecd12355f94a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 30 Jul 2009 20:08:24 +0000 Subject: I think I fixed a bug in Bu::Base64, we'll see what happens. --- src/base64.cpp | 31 ++++++++++++++++++++++++------- src/base64.h | 3 +++ src/tests/base64.cpp | 6 +++--- 3 files changed, 30 insertions(+), 10 deletions(-) (limited to 'src') 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 ) : Bu::Filter( rNext ), iBPos( 0 ), iBuf( 0 ), + iRPos( 0 ), + iChars( 0 ), + bEosIn( false ), iTotalIn( 0 ), iTotalOut( 0 ), eMode( Nothing ) @@ -79,10 +82,25 @@ size_t Bu::Base64::stop() size_t Bu::Base64::read( void *pBuf, size_t nBytes ) { + if( bEosIn == true && iRPos == iChars ) + return 0; size_t sIn = 0; char buf[4]; while( sIn < nBytes ) { + for(; iRPos < iChars && sIn < nBytes; iRPos++, sIn++ ) + { + ((unsigned char *)pBuf)[sIn] = (iBuf>>(8*(2-iRPos)))&0xFF; + } + if( iRPos == iChars ) + { + if( bEosIn == true ) + return sIn; + else + iRPos = 0; + } + else if( sIn == nBytes ) + return sIn; //if( rNext.read( buf, 4 ) == 0 ) // return sIn; for( int j = 0; j < 4; j++ ) @@ -95,20 +113,19 @@ size_t Bu::Base64::read( void *pBuf, size_t nBytes ) j--; } } - int iChars = 3; + iChars = 3; + iBuf = 0; for( int j = 0; j < 4; j++ ) { if( buf[j] == '=' ) + { iChars--; + bEosIn = true; + } else iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6); - printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); + //printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); } - for( int j = 0; j < iChars; j++ ) - { - ((unsigned char *)pBuf)[sIn++] = (iBuf>>(8*(2-j)))&0xFF; - } - iBuf = 0; } 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 private: int iBPos; int iBuf; + int iRPos; + int iChars; + bool bEosIn; size_t iTotalIn; size_t iTotalOut; 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[] ) Bu::File fOut( argv[1], Bu::File::WriteNew ); Bu::Base64 bIn( fIn ); - char buf[900]; + char buf[16]; for(;;) { - int iRead = bIn.read( buf, 900 ); + int iRead = bIn.read( buf, 16 ); fOut.write( buf, iRead ); - if( iRead < 900 ) + if( iRead < 16 ) break; } } -- cgit v1.2.3