diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-10-25 16:04:43 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-10-25 16:04:43 +0000 |
commit | 052da60c2c5c4ce80ec0986ea07482348e7aa30a (patch) | |
tree | 32a951ea64de53b8c047d33b1d856fadbbe311fc /src | |
parent | 7c9cf28012f65ce6a67651030b817d7d45eda62b (diff) | |
download | libbu++-052da60c2c5c4ce80ec0986ea07482348e7aa30a.tar.gz libbu++-052da60c2c5c4ce80ec0986ea07482348e7aa30a.tar.bz2 libbu++-052da60c2c5c4ce80ec0986ea07482348e7aa30a.tar.xz libbu++-052da60c2c5c4ce80ec0986ea07482348e7aa30a.zip |
Base64 does line wrapping correctly on write, and also doesn't try to flush the
write buffer when reading is done. It's...strange, but yeah, it was doing that.
Deflate also defaults to zlib compression now, which means you can compress &
decompress without using any extra params. Turns out zlib auto-detect won't
decompress raw streams, so this is the safest overall option, and the easiest
to work with. zlib headers are small, and includes a crc at the end so you can
be sure your data is accurate, raw does not.
Diffstat (limited to 'src')
-rw-r--r-- | src/base64.cpp | 45 | ||||
-rw-r--r-- | src/base64.h | 4 | ||||
-rw-r--r-- | src/deflate.cpp | 3 | ||||
-rw-r--r-- | src/deflate.h | 2 |
4 files changed, 41 insertions, 13 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 | ||
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; |
@@ -157,6 +173,10 @@ Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) | |||
157 | 173 | ||
158 | Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) | 174 | Bu::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 | } |
diff --git a/src/base64.h b/src/base64.h index 53d7860..c081ac1 100644 --- a/src/base64.h +++ b/src/base64.h | |||
@@ -22,7 +22,7 @@ namespace Bu | |||
22 | class Base64 : public Bu::Filter | 22 | class Base64 : public Bu::Filter |
23 | { | 23 | { |
24 | public: | 24 | public: |
25 | Base64( Bu::Stream &rNext ); | 25 | Base64( Bu::Stream &rNext, int iChunkSize=0 ); |
26 | virtual ~Base64(); | 26 | virtual ~Base64(); |
27 | 27 | ||
28 | virtual void start(); | 28 | virtual void start(); |
@@ -51,6 +51,8 @@ namespace Bu | |||
51 | Decode = 0x02, | 51 | Decode = 0x02, |
52 | }; | 52 | }; |
53 | Mode eMode; | 53 | Mode eMode; |
54 | int iChunkSize; | ||
55 | int iCurChunk; | ||
54 | }; | 56 | }; |
55 | }; | 57 | }; |
56 | 58 | ||
diff --git a/src/deflate.cpp b/src/deflate.cpp index 10a9c5f..2d925a7 100644 --- a/src/deflate.cpp +++ b/src/deflate.cpp | |||
@@ -149,9 +149,6 @@ Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes ) | |||
149 | for(;;) | 149 | for(;;) |
150 | { | 150 | { |
151 | int ret = inflate( pState, Z_NO_FLUSH ); | 151 | int ret = inflate( pState, Z_NO_FLUSH ); |
152 | printf("inflate returned %d; avail in=%d, out=%d\n", ret, | ||
153 | pState->avail_in, pState->avail_out ); | ||
154 | |||
155 | nReadTotal += nRead-pState->avail_out; | 152 | nReadTotal += nRead-pState->avail_out; |
156 | 153 | ||
157 | if( ret == Z_STREAM_END ) | 154 | if( ret == Z_STREAM_END ) |
diff --git a/src/deflate.h b/src/deflate.h index 34e8657..f835cfc 100644 --- a/src/deflate.h +++ b/src/deflate.h | |||
@@ -37,7 +37,7 @@ namespace Bu | |||
37 | AutoGzip = 0x04|0x03 | 37 | AutoGzip = 0x04|0x03 |
38 | }; | 38 | }; |
39 | 39 | ||
40 | Deflate( Bu::Stream &rNext, int nCompression=-1, Format eFmt=AutoRaw ); | 40 | Deflate( Bu::Stream &rNext, int nCompression=-1, Format eFmt=AutoZlib ); |
41 | virtual ~Deflate(); | 41 | virtual ~Deflate(); |
42 | 42 | ||
43 | virtual void start(); | 43 | virtual void start(); |