aboutsummaryrefslogtreecommitdiff
path: root/src/base64.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/base64.cpp45
1 files changed, 37 insertions, 8 deletions
diff --git a/src/base64.cpp b/src/base64.cpp
index 04ca009..4d659f0 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -13,7 +13,7 @@ const char Bu::Base64::tblEnc[65] = {
13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
14}; 14};
15 15
16Bu::Base64::Base64( Bu::Stream &rNext ) : 16Bu::Base64::Base64( Bu::Stream &rNext, int iChunkSize ) :
17 Bu::Filter( rNext ), 17 Bu::Filter( rNext ),
18 iBPos( 0 ), 18 iBPos( 0 ),
19 iBuf( 0 ), 19 iBuf( 0 ),
@@ -22,7 +22,9 @@ Bu::Base64::Base64( Bu::Stream &rNext ) :
22 bEosIn( false ), 22 bEosIn( false ),
23 iTotalIn( 0 ), 23 iTotalIn( 0 ),
24 iTotalOut( 0 ), 24 iTotalOut( 0 ),
25 eMode( Nothing ) 25 eMode( Nothing ),
26 iChunkSize( iChunkSize ),
27 iCurChunk( 0 )
26{ 28{
27 start(); 29 start();
28 30
@@ -62,11 +64,12 @@ Bu::Base64::~Base64()
62 64
63void Bu::Base64::start() 65void Bu::Base64::start()
64{ 66{
67 iCurChunk = 0;
65} 68}
66 69
67Bu::size Bu::Base64::stop() 70Bu::size Bu::Base64::stop()
68{ 71{
69// if( eMode |= Encode ) 72 if( eMode == Encode )
70 { 73 {
71 char outBuf[4]; 74 char outBuf[4];
72 int iBUsed = 4-(3-iBPos); 75 int iBUsed = 4-(3-iBPos);
@@ -80,17 +83,30 @@ Bu::size Bu::Base64::stop()
80 { 83 {
81 outBuf[k] = '='; 84 outBuf[k] = '=';
82 } 85 }
83 iTotalOut += rNext.write( outBuf, 4 ); 86 iCurChunk += 4;
87 if( iChunkSize && iCurChunk >= iChunkSize )
88 {
89 iCurChunk = iCurChunk-iChunkSize;
90 iTotalOut += rNext.write( outBuf, 4-iCurChunk );
91 iTotalOut += rNext.write("\r\n", 2 );
92 iTotalOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk );
93 }
94 else
95 iTotalOut += rNext.write( outBuf, 4 );
84 return iTotalOut; 96 return iTotalOut;
85 } 97 }
86// else 98 else
87// { 99 {
88 return iTotalIn; 100 return iTotalIn;
89// } 101 }
90} 102}
91 103
92Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) 104Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes )
93{ 105{
106 if( eMode == Encode )
107 throw Bu::Base64Exception("Cannot read from an output stream.");
108 eMode = Decode;
109
94 if( bEosIn == true && iRPos == iChars ) 110 if( bEosIn == true && iRPos == iChars )
95 return 0; 111 return 0;
96 Bu::size sIn = 0; 112 Bu::size sIn = 0;
@@ -157,6 +173,10 @@ Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes )
157 173
158Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) 174Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes )
159{ 175{
176 if( eMode == Decode )
177 throw Bu::Base64Exception("Cannot write to an input stream.");
178 eMode = Encode;
179
160 Bu::size sOut = 0; 180 Bu::size sOut = 0;
161 char outBuf[4]; 181 char outBuf[4];
162 for( Bu::size j = 0; j < nBytes; j++ ) 182 for( Bu::size j = 0; j < nBytes; j++ )
@@ -168,7 +188,16 @@ Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes )
168 { 188 {
169 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; 189 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f];
170 } 190 }
171 sOut += rNext.write( outBuf, 4 ); 191 iCurChunk += 4;
192 if( iChunkSize && iCurChunk >= iChunkSize )
193 {
194 iCurChunk = iCurChunk-iChunkSize;
195 sOut += rNext.write( outBuf, 4-iCurChunk );
196 sOut += rNext.write("\r\n", 2 );
197 sOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk );
198 }
199 else
200 sOut += rNext.write( outBuf, 4 );
172 iBPos = iBuf = 0; 201 iBPos = iBuf = 0;
173 } 202 }
174 } 203 }