diff options
Diffstat (limited to '')
-rw-r--r-- | src/base64.cpp | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/src/base64.cpp b/src/base64.cpp index 18a18e5..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 | ||
16 | Bu::Base64::Base64( Bu::Stream &rNext ) : | 16 | Bu::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 | ||
63 | void Bu::Base64::start() | 65 | void Bu::Base64::start() |
64 | { | 66 | { |
67 | iCurChunk = 0; | ||
65 | } | 68 | } |
66 | 69 | ||
67 | Bu::size Bu::Base64::stop() | 70 | Bu::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 | ||
92 | Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) | 104 | Bu::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; |
@@ -118,6 +134,8 @@ Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) | |||
118 | { | 134 | { |
119 | if( rNext.isEos() ) | 135 | if( rNext.isEos() ) |
120 | { | 136 | { |
137 | if( iRPos == 0 ) | ||
138 | iRPos = iChars; | ||
121 | bEosIn = true; | 139 | bEosIn = true; |
122 | if( j != 0 ) | 140 | if( j != 0 ) |
123 | { | 141 | { |
@@ -155,6 +173,10 @@ Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) | |||
155 | 173 | ||
156 | Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) | 174 | Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) |
157 | { | 175 | { |
176 | if( eMode == Decode ) | ||
177 | throw Bu::Base64Exception("Cannot write to an input stream."); | ||
178 | eMode = Encode; | ||
179 | |||
158 | Bu::size sOut = 0; | 180 | Bu::size sOut = 0; |
159 | char outBuf[4]; | 181 | char outBuf[4]; |
160 | for( Bu::size j = 0; j < nBytes; j++ ) | 182 | for( Bu::size j = 0; j < nBytes; j++ ) |
@@ -166,7 +188,16 @@ Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) | |||
166 | { | 188 | { |
167 | outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; | 189 | outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; |
168 | } | 190 | } |
169 | 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 ); | ||
170 | iBPos = iBuf = 0; | 201 | iBPos = iBuf = 0; |
171 | } | 202 | } |
172 | } | 203 | } |