aboutsummaryrefslogtreecommitdiff
path: root/src/stable/base64.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
commitec05778d5718a7912e506764d443a78d6a6179e3 (patch)
tree78a9a01532180030c095acefc45763f07c14edb8 /src/stable/base64.cpp
parentb20414ac1fe80a71a90601f4cd1767fa7014a9ba (diff)
downloadlibbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.gz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.bz2
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.xz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.zip
Converted tabs to spaces with tabconv.
Diffstat (limited to '')
-rw-r--r--src/stable/base64.cpp346
1 files changed, 173 insertions, 173 deletions
diff --git a/src/stable/base64.cpp b/src/stable/base64.cpp
index 5ace999..2dc123f 100644
--- a/src/stable/base64.cpp
+++ b/src/stable/base64.cpp
@@ -10,210 +10,210 @@
10namespace Bu { subExceptionDef( Base64Exception ) } 10namespace Bu { subExceptionDef( Base64Exception ) }
11 11
12const char Bu::Base64::tblEnc[65] = { 12const char Bu::Base64::tblEnc[65] = {
13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
14}; 14};
15 15
16Bu::Base64::Base64( Bu::Stream &rNext, int iChunkSize ) : 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 ),
20 iRPos( 0 ), 20 iRPos( 0 ),
21 iChars( 0 ), 21 iChars( 0 ),
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 ), 26 iChunkSize( iChunkSize ),
27 iCurChunk( 0 ) 27 iCurChunk( 0 )
28{ 28{
29 start(); 29 start();
30 30
31 memset( tblDec, 0, 80 ); 31 memset( tblDec, 0, 80 );
32 for( int j = 0; j < 64; j++ ) 32 for( int j = 0; j < 64; j++ )
33 { 33 {
34 tblDec[tblEnc[j]-'+'] = j; 34 tblDec[tblEnc[j]-'+'] = j;
35 // printf("'%c' = %d\n", tblEnc[j], j ); 35 // printf("'%c' = %d\n", tblEnc[j], j );
36 } 36 }
37 /* 37 /*
38 for( int j = 0; j < 64; j++ ) 38 for( int j = 0; j < 64; j++ )
39 { 39 {
40 printf("'%c' = '%c' (%d = %d)\n", 40 printf("'%c' = '%c' (%d = %d)\n",
41 tblEnc[j], tblEnc[tblDec[tblEnc[j]-'+']], 41 tblEnc[j], tblEnc[tblDec[tblEnc[j]-'+']],
42 j, tblDec[tblEnc[j]-'+'] ); 42 j, tblDec[tblEnc[j]-'+'] );
43 }*/ 43 }*/
44 44
45 // The following is used to compute the table size for the decoding table. 45 // The following is used to compute the table size for the decoding table.
46 /* 46 /*
47 char low='A', high='A'; 47 char low='A', high='A';
48 for( int j = 0; j < 64; j++ ) 48 for( int j = 0; j < 64; j++ )
49 { 49 {
50 if( tblEnc[j] < low ) 50 if( tblEnc[j] < low )
51 low = tblEnc[j]; 51 low = tblEnc[j];
52 if( tblEnc[j] > high ) 52 if( tblEnc[j] > high )
53 high = tblEnc[j]; 53 high = tblEnc[j];
54 } 54 }
55 55
56 printf("'%c' - '%c' (%d - %d) (%d)\n", low, high, low, high, high-low ); 56 printf("'%c' - '%c' (%d - %d) (%d)\n", low, high, low, high, high-low );
57 */ 57 */
58} 58}
59 59
60Bu::Base64::~Base64() 60Bu::Base64::~Base64()
61{ 61{
62 stop(); 62 stop();
63} 63}
64 64
65void Bu::Base64::start() 65void Bu::Base64::start()
66{ 66{
67 iCurChunk = 0; 67 iCurChunk = 0;
68} 68}
69 69
70Bu::size Bu::Base64::stop() 70Bu::size Bu::Base64::stop()
71{ 71{
72 if( eMode == Encode ) 72 if( eMode == Encode )
73 { 73 {
74 char outBuf[4]; 74 char outBuf[4];
75 int iBUsed = 4-(3-iBPos); 75 int iBUsed = 4-(3-iBPos);
76 if( iBPos == 0 ) 76 if( iBPos == 0 )
77 return iTotalOut; 77 return iTotalOut;
78 for( int k = 0; k < 4; k++ ) 78 for( int k = 0; k < 4; k++ )
79 { 79 {
80 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; 80 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f];
81 } 81 }
82 for( int k = iBUsed; k < 4; k++ ) 82 for( int k = iBUsed; k < 4; k++ )
83 { 83 {
84 outBuf[k] = '='; 84 outBuf[k] = '=';
85 } 85 }
86 iCurChunk += 4; 86 iCurChunk += 4;
87 if( iChunkSize && iCurChunk >= iChunkSize ) 87 if( iChunkSize && iCurChunk >= iChunkSize )
88 { 88 {
89 iCurChunk = iCurChunk-iChunkSize; 89 iCurChunk = iCurChunk-iChunkSize;
90 iTotalOut += rNext.write( outBuf, 4-iCurChunk ); 90 iTotalOut += rNext.write( outBuf, 4-iCurChunk );
91 iTotalOut += rNext.write("\r\n", 2 ); 91 iTotalOut += rNext.write("\r\n", 2 );
92 iTotalOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk ); 92 iTotalOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk );
93 } 93 }
94 else 94 else
95 iTotalOut += rNext.write( outBuf, 4 ); 95 iTotalOut += rNext.write( outBuf, 4 );
96 return iTotalOut; 96 return iTotalOut;
97 } 97 }
98 else 98 else
99 { 99 {
100 return iTotalIn; 100 return iTotalIn;
101 } 101 }
102} 102}
103 103
104Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) 104Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes )
105{ 105{
106 if( eMode == Encode ) 106 if( eMode == Encode )
107 throw Bu::Base64Exception("Cannot read from an output stream."); 107 throw Bu::Base64Exception("Cannot read from an output stream.");
108 eMode = Decode; 108 eMode = Decode;
109 109
110 if( bEosIn == true && iRPos == iChars ) 110 if( bEosIn == true && iRPos == iChars )
111 return 0; 111 return 0;
112 Bu::size sIn = 0; 112 Bu::size sIn = 0;
113 char buf[4]; 113 char buf[4];
114 while( sIn < nBytes ) 114 while( sIn < nBytes )
115 { 115 {
116 for(; iRPos < iChars && sIn < nBytes; iRPos++, sIn++ ) 116 for(; iRPos < iChars && sIn < nBytes; iRPos++, sIn++ )
117 { 117 {
118 ((unsigned char *)pBuf)[sIn] = (iBuf>>(8*(2-iRPos)))&0xFF; 118 ((unsigned char *)pBuf)[sIn] = (iBuf>>(8*(2-iRPos)))&0xFF;
119 } 119 }
120 if( iRPos == iChars ) 120 if( iRPos == iChars )
121 { 121 {
122 if( bEosIn == true ) 122 if( bEosIn == true )
123 return sIn; 123 return sIn;
124 else 124 else
125 iRPos = 0; 125 iRPos = 0;
126 } 126 }
127 else if( sIn == nBytes ) 127 else if( sIn == nBytes )
128 return sIn; 128 return sIn;
129 //if( rNext.read( buf, 4 ) == 0 ) 129 //if( rNext.read( buf, 4 ) == 0 )
130 // return sIn; 130 // return sIn;
131 for( int j = 0; j < 4; j++ ) 131 for( int j = 0; j < 4; j++ )
132 { 132 {
133 if( rNext.read( &buf[j], 1 ) == 0 ) 133 if( rNext.read( &buf[j], 1 ) == 0 )
134 { 134 {
135 if( rNext.isEos() ) 135 if( rNext.isEos() )
136 { 136 {
137 if( iRPos == 0 ) 137 if( iRPos == 0 )
138 iRPos = iChars; 138 iRPos = iChars;
139 bEosIn = true; 139 bEosIn = true;
140 if( j != 0 ) 140 if( j != 0 )
141 { 141 {
142 throw Base64Exception( 142 throw Base64Exception(
143 "Premature end of stream detected while " 143 "Premature end of stream detected while "
144 "decoding Base64 data." 144 "decoding Base64 data."
145 ); 145 );
146 } 146 }
147 } 147 }
148 return sIn; 148 return sIn;
149 } 149 }
150 if( buf[j] == ' ' || buf[j] == '\t' || 150 if( buf[j] == ' ' || buf[j] == '\t' ||
151 buf[j] == '\n' || buf[j] == '\r' ) 151 buf[j] == '\n' || buf[j] == '\r' )
152 { 152 {
153 j--; 153 j--;
154 } 154 }
155 } 155 }
156 iChars = 3; 156 iChars = 3;
157 iBuf = 0; 157 iBuf = 0;
158 for( int j = 0; j < 4; j++ ) 158 for( int j = 0; j < 4; j++ )
159 { 159 {
160 if( buf[j] == '=' ) 160 if( buf[j] == '=' )
161 { 161 {
162 iChars--; 162 iChars--;
163 bEosIn = true; 163 bEosIn = true;
164 } 164 }
165 else 165 else
166 iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6); 166 iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6);
167 //printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); 167 //printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) );
168 } 168 }
169 } 169 }
170 170
171 return sIn; 171 return sIn;
172} 172}
173 173
174Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) 174Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes )
175{ 175{
176 if( eMode == Decode ) 176 if( eMode == Decode )
177 throw Bu::Base64Exception("Cannot write to an input stream."); 177 throw Bu::Base64Exception("Cannot write to an input stream.");
178 eMode = Encode; 178 eMode = Encode;
179 179
180 Bu::size sOut = 0; 180 Bu::size sOut = 0;
181 char outBuf[4]; 181 char outBuf[4];
182 for( Bu::size j = 0; j < nBytes; j++ ) 182 for( Bu::size j = 0; j < nBytes; j++ )
183 { 183 {
184 iBuf |= (((uint8_t *)pBuf)[j])<<((2-iBPos++)*8); 184 iBuf |= (((uint8_t *)pBuf)[j])<<((2-iBPos++)*8);
185 if( iBPos == 3 ) 185 if( iBPos == 3 )
186 { 186 {
187 for( int k = 0; k < 4; k++ ) 187 for( int k = 0; k < 4; k++ )
188 { 188 {
189 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; 189 outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f];
190 } 190 }
191 iCurChunk += 4; 191 iCurChunk += 4;
192 if( iChunkSize && iCurChunk >= iChunkSize ) 192 if( iChunkSize && iCurChunk >= iChunkSize )
193 { 193 {
194 iCurChunk = iCurChunk-iChunkSize; 194 iCurChunk = iCurChunk-iChunkSize;
195 sOut += rNext.write( outBuf, 4-iCurChunk ); 195 sOut += rNext.write( outBuf, 4-iCurChunk );
196 sOut += rNext.write("\r\n", 2 ); 196 sOut += rNext.write("\r\n", 2 );
197 sOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk ); 197 sOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk );
198 } 198 }
199 else 199 else
200 sOut += rNext.write( outBuf, 4 ); 200 sOut += rNext.write( outBuf, 4 );
201 iBPos = iBuf = 0; 201 iBPos = iBuf = 0;
202 } 202 }
203 } 203 }
204 iTotalOut += sOut; 204 iTotalOut += sOut;
205 return sOut; 205 return sOut;
206} 206}
207 207
208bool Bu::Base64::isOpen() 208bool Bu::Base64::isOpen()
209{ 209{
210 return true; 210 return true;
211} 211}
212 212
213bool Bu::Base64::isEos() 213bool Bu::Base64::isEos()
214{ 214{
215 if( bEosIn == true && iRPos == iChars ) 215 if( bEosIn == true && iRPos == iChars )
216 return true; 216 return true;
217 return false; 217 return false;
218} 218}
219 219