#include <Base64.h>
Collaboration diagram for Cgi::Base64:
Public Member Functions | |
Base64 (char= '=') | |
~Base64 () | |
void | encode_file (const char *s, const char *outfn) |
void | encode64 (const char *in, size_t len, std::vector< std::string > &out) |
void | encode_from_file (const char *, std::vector< std::string > &) |
std::string | encode_buffer (const std::string &) |
void | decode_file (char *s, char *outfn) |
void | decode_to_file (std::vector< std::string > &body, char *outfn) |
void | decode_stdout (char *fn, FILE *out=stdout) |
void | decode_to_stdout (const std::string &, FILE *=stdout) |
void | decode_to_stdout (std::vector< std::string > &, FILE *=stdout) |
void | decode_to_buffer (const std::string &, char *, size_t max) |
size_t | decode_length (const std::string &) |
Private Member Functions | |
void | setupbase64 () |
char | setupdecode (char c) |
unsigned char | reverse (char c) |
void | encode (const char *in, char *out) |
int | decode (char c) |
void | decode_string (char *s, unsigned char *output, int &l) |
Private Attributes | |
char | m_eqchar |
Definition at line 33 of file Base64.h.
Cgi::Base64::Base64 | ( | char | = '=' |
) |
Definition at line 39 of file Base64.cpp.
References setupbase64().
00039 : m_eqchar(eq) 00040 { 00041 setupbase64(); 00042 }
Cgi::Base64::~Base64 | ( | ) |
void Cgi::Base64::encode_file | ( | const char * | s, | |
const char * | outfn | |||
) |
Definition at line 138 of file Base64.cpp.
References encode(), and m_eqchar.
00139 { 00140 FILE *fil; 00141 FILE *fil2; 00142 int iptr = 0; 00143 int ptr = 0; 00144 size_t dv = 0; 00145 unsigned char c; 00146 char tmp[4]; 00147 char slask[200]; 00148 00149 slask[76] = 0; 00150 00151 if ((fil = fopen(s, "rb")) != NULL) 00152 { 00153 fil2 = fopen(outfn, "wt"); 00154 fread(&c,1,1,fil); 00155 while (!feof(fil)) 00156 { 00157 tmp[iptr++] = c; 00158 if (iptr == 3) 00159 { 00160 encode(tmp, slask + ptr); 00161 ptr += 4; 00162 if (ptr == 76) 00163 { 00164 fprintf(fil2,"%s\n",slask); 00165 ptr = 0; 00166 } 00167 iptr = 0; 00168 dv++; 00169 } 00170 fread(&c,1,1,fil); 00171 } 00172 fclose(fil); 00173 00174 if (iptr) 00175 { 00176 int i = iptr; 00177 while (iptr < 3) 00178 tmp[iptr++] = 0; 00179 encode(tmp, slask + ptr); 00180 switch (i) 00181 { 00182 case 1: 00183 slask[ptr + 2] = m_eqchar; 00184 case 2: 00185 slask[ptr + 3] = m_eqchar; 00186 } 00187 ptr += 4; 00188 if (ptr == 76) 00189 { 00190 fprintf(fil2,"%s\n",slask); 00191 ptr = 0; 00192 } 00193 } 00194 if (ptr) 00195 { 00196 slask[ptr] = 0; 00197 fprintf(fil2,"%s\n",slask); 00198 ptr = 0; 00199 } 00200 fclose(fil2); 00201 } 00202 }
void Cgi::Base64::encode64 | ( | const char * | in, | |
size_t | len, | |||
std::vector< std::string > & | out | |||
) |
Definition at line 205 of file Base64.cpp.
References encode(), and m_eqchar.
00206 { 00207 size_t dv = len / 3; 00208 size_t ptr = 0; 00209 size_t i; 00210 char tmp[100]; 00211 00212 tmp[76] = 0; 00213 for (i = 0; i < dv; i++) 00214 { 00215 encode(in + i * 3,tmp + ptr); 00216 ptr += 4; 00217 tmp[ptr] = 0; 00218 if (ptr == 76) 00219 { 00220 out.push_back( tmp ); 00221 ptr = 0; 00222 } 00223 } 00224 if (len % 3) 00225 { 00226 char slask[3]; 00227 00228 memset(slask,0,3); 00229 memmove(slask,in + i * 3,len % 3); 00230 00231 encode(slask,tmp + ptr); 00232 switch (len % 3) 00233 { 00234 case 1: 00235 tmp[ptr + 2] = m_eqchar; 00236 case 2: 00237 tmp[ptr + 3] = m_eqchar; 00238 } 00239 ptr += 4; 00240 tmp[ptr] = 0; 00241 if (ptr == 76) 00242 { 00243 out.push_back( tmp ); 00244 ptr = 0; 00245 } 00246 } 00247 if (ptr) 00248 { 00249 tmp[ptr] = 0; 00250 out.push_back( tmp ); 00251 ptr = 0; 00252 } 00253 }
void Cgi::Base64::encode_from_file | ( | const char * | , | |
std::vector< std::string > & | ||||
) |
Definition at line 311 of file Base64.cpp.
References encode(), and m_eqchar.
00312 { 00313 FILE *fil; 00314 int iptr = 0; 00315 int ptr = 0; 00316 size_t dv = 0; 00317 unsigned char c; 00318 char tmp[4]; 00319 char slask[200]; 00320 00321 slask[76] = 0; 00322 00323 if ((fil = fopen(fn, "rb")) != NULL) 00324 { 00325 fread(&c,1,1,fil); 00326 while (!feof(fil)) 00327 { 00328 tmp[iptr++] = c; 00329 if (iptr == 3) 00330 { 00331 encode(tmp, slask + ptr); 00332 ptr += 4; 00333 if (ptr == 76) 00334 { 00335 out.push_back( slask ); 00336 ptr = 0; 00337 } 00338 iptr = 0; 00339 dv++; 00340 } 00341 fread(&c,1,1,fil); 00342 } 00343 fclose(fil); 00344 00345 if (iptr) 00346 { 00347 int i = iptr; 00348 while (iptr < 3) 00349 tmp[iptr++] = 0; 00350 encode(tmp, slask + ptr); 00351 switch (i) 00352 { 00353 case 1: 00354 slask[ptr + 2] = m_eqchar; 00355 case 2: 00356 slask[ptr + 3] = m_eqchar; 00357 } 00358 ptr += 4; 00359 if (ptr == 76) 00360 { 00361 out.push_back( slask ); 00362 ptr = 0; 00363 } 00364 } 00365 if (ptr) 00366 { 00367 slask[ptr] = 0; 00368 out.push_back( slask ); 00369 ptr = 0; 00370 } 00371 } 00372 }
std::string Cgi::Base64::encode_buffer | ( | const std::string & | ) |
Definition at line 256 of file Base64.cpp.
References encode(), and m_eqchar.
00257 { 00258 const char *in = buf.c_str(); 00259 size_t len = buf.size(); 00260 std::string out; 00261 size_t dv = len / 3; 00262 size_t ptr = 0; 00263 size_t i; 00264 char tmp[100]; 00265 00266 tmp[76] = 0; 00267 for (i = 0; i < dv; i++) 00268 { 00269 encode(in + i * 3,tmp + ptr); 00270 ptr += 4; 00271 tmp[ptr] = 0; 00272 if (ptr == 76) 00273 { 00274 out += tmp; 00275 ptr = 0; 00276 } 00277 } 00278 if (len % 3) 00279 { 00280 char slask[3]; 00281 00282 memset(slask,0,3); 00283 memmove(slask,in + i * 3,len % 3); 00284 00285 encode(slask,tmp + ptr); 00286 switch (len % 3) 00287 { 00288 case 1: 00289 tmp[ptr + 2] = m_eqchar; 00290 case 2: 00291 tmp[ptr + 3] = m_eqchar; 00292 } 00293 ptr += 4; 00294 tmp[ptr] = 0; 00295 if (ptr == 76) 00296 { 00297 out += tmp; 00298 ptr = 0; 00299 } 00300 } 00301 if (ptr) 00302 { 00303 tmp[ptr] = 0; 00304 out += tmp; 00305 ptr = 0; 00306 } 00307 return out; 00308 }
void Cgi::Base64::decode_file | ( | char * | s, | |
char * | outfn | |||
) |
Definition at line 375 of file Base64.cpp.
References decode_string().
00376 { 00377 FILE *fil; 00378 FILE *fil2; 00379 char slask[200]; 00380 unsigned char output[200]; 00381 00382 if ((fil = fopen(s, "rt")) != NULL) 00383 { 00384 fil2 = fopen(outfn, "wb"); 00385 fgets(slask, 200, fil); 00386 while (!feof(fil)) 00387 { 00388 while (strlen(slask) && (slask[strlen(slask) - 1] == 13 || slask[strlen(slask) - 1] == 10)) 00389 { 00390 slask[strlen(slask) - 1] = 0; 00391 } 00392 for (int i = 0; i < (int)strlen(slask); i += 4) 00393 { 00394 int l; 00395 decode_string(slask + i, output, l); 00396 fwrite(output, 1, l, fil2); 00397 } 00398 // 00399 fgets(slask, 200, fil); 00400 } 00401 fclose(fil); 00402 fclose(fil2); 00403 } 00404 }
void Cgi::Base64::decode_to_file | ( | std::vector< std::string > & | body, | |
char * | outfn | |||
) |
Definition at line 407 of file Base64.cpp.
References decode_string().
00408 { 00409 FILE *fil; 00410 unsigned char out[4]; 00411 00412 if ((fil = fopen(outfn,"wb")) != NULL) 00413 { 00414 for (std::vector<std::string>::iterator it = body.begin(); it != body.end(); it++) 00415 { 00416 for (size_t i = 0; i < (*it).size(); i += 4) 00417 { 00418 int l; 00419 decode_string( (char *)(*it).c_str() + i, out, l); 00420 fwrite(out, 1, l, fil); 00421 } 00422 } 00423 fclose(fil); 00424 } 00425 }
void Cgi::Base64::decode_stdout | ( | char * | fn, | |
FILE * | out = stdout | |||
) |
Definition at line 428 of file Base64.cpp.
References decode_string().
00429 { 00430 FILE *fil; 00431 char slask[200]; 00432 unsigned char output[200]; 00433 00434 if ((fil = fopen(s, "rt")) != NULL) 00435 { 00436 fgets(slask, 200, fil); 00437 while (!feof(fil)) 00438 { 00439 while (strlen(slask) && (slask[strlen(slask) - 1] == 13 || slask[strlen(slask) - 1] == 10)) 00440 { 00441 slask[strlen(slask) - 1] = 0; 00442 } 00443 for (int i = 0; i < (int)strlen(slask); i += 4) 00444 { 00445 int l; 00446 decode_string(slask + i, output, l); 00447 fwrite(output, 1, l, fil2); 00448 } 00449 // 00450 fgets(slask, 200, fil); 00451 } 00452 fclose(fil); 00453 } 00454 }
void Cgi::Base64::decode_to_stdout | ( | const std::string & | , | |
FILE * | = stdout | |||
) |
Definition at line 457 of file Base64.cpp.
References decode_string().
00458 { 00459 size_t p = 0; 00460 char slask[20000]; 00461 unsigned char output[200]; 00462 00463 while (p < str.size()) 00464 { 00465 size_t i = 0; 00466 while (str[p] != 13 && str[p] != 10 && p < str.size()) 00467 { 00468 slask[i++] = str[p++]; 00469 } 00470 slask[i] = 0; 00471 while (str[p] == 13 || str[p] == 10) 00472 p++; 00473 for (i = 0; i < strlen(slask); i += 4) 00474 { 00475 int l; 00476 decode_string(slask + i, output, l); 00477 fwrite(output, 1, l, fil2); 00478 } 00479 } 00480 }
void Cgi::Base64::decode_to_stdout | ( | std::vector< std::string > & | , | |
FILE * | = stdout | |||
) |
Definition at line 483 of file Base64.cpp.
References decode_string().
00484 { 00485 unsigned char out[4]; 00486 00487 for (std::vector<std::string>::iterator it = body.begin(); it != body.end(); it++) 00488 { 00489 for (size_t i = 0; i < (*it).size(); i += 4) 00490 { 00491 int l; 00492 decode_string( (char *)(*it).c_str() + i, out, l); 00493 fwrite(out, 1, l, fil); 00494 } 00495 } 00496 }
void Cgi::Base64::decode_to_buffer | ( | const std::string & | , | |
char * | , | |||
size_t | max | |||
) |
Definition at line 528 of file Base64.cpp.
References decode_string().
Referenced by Cgi::Web::ValidateAuth().
00529 { 00530 size_t p = 0; 00531 size_t o = 0; 00532 char slask[20000]; 00533 unsigned char output[200]; 00534 00535 while (p < str.size()) 00536 { 00537 size_t i = 0; 00538 while (str[p] != 13 && str[p] != 10 && p < str.size()) 00539 { 00540 slask[i++] = str[p++]; 00541 } 00542 slask[i] = 0; 00543 while (str[p] == 13 || str[p] == 10) 00544 p++; 00545 for (i = 0; i < strlen(slask); i += 4) 00546 { 00547 int l; 00548 decode_string(slask + i, output, l); 00549 if (o + l <= max) 00550 { 00551 memcpy(ut + o, output, l); 00552 if (o + l < max) 00553 memset(ut + o + l, 0, 1); 00554 } 00555 o += l; 00556 } 00557 } 00558 }
size_t Cgi::Base64::decode_length | ( | const std::string & | ) |
Definition at line 499 of file Base64.cpp.
References decode_string().
00500 { 00501 size_t p = 0; 00502 size_t len = 0; 00503 char slask[20000]; 00504 unsigned char output[200]; 00505 00506 while (p < str.size()) 00507 { 00508 size_t i = 0; 00509 while (str[p] != 13 && str[p] != 10 && p < str.size()) 00510 { 00511 slask[i++] = str[p++]; 00512 } 00513 slask[i] = 0; 00514 while (str[p] == 13 || str[p] == 10) 00515 p++; 00516 for (i = 0; i < strlen(slask); i += 4) 00517 { 00518 int l; 00519 decode_string(slask + i, output, l); 00520 // fwrite(output, 1, l, fil2); 00521 len += l; 00522 } 00523 } 00524 return len; 00525 }
void Cgi::Base64::setupbase64 | ( | ) | [private] |
Definition at line 50 of file Base64.cpp.
References base64vec, and setupdecode().
Referenced by Base64().
00051 { 00052 for (char c = 0; c <= 'z'; c++) 00053 { 00054 base64vec[ (int)c] = setupdecode(c); // 0..63 00055 } 00056 }
char Cgi::Base64::setupdecode | ( | char | c | ) | [private] |
Definition at line 59 of file Base64.cpp.
Referenced by setupbase64().
00060 { 00061 if (c >= 'a') 00062 return (char)((c + 26) - 'a'); // 26..51 00063 if (c >= 'A') 00064 return (char)(c - 'A'); // 0..25 00065 if (c >= '0') 00066 return (char)((c + 52) - '0'); // 52..61 00067 if (c == '+') 00068 return 62; // 62 00069 if (c == '/') 00070 return 63; // 63 00071 return 0; 00072 }
unsigned char Cgi::Base64::reverse | ( | char | c | ) | [private] |
Definition at line 75 of file Base64.cpp.
References g_achEncodeTable.
Referenced by encode().
00076 { 00077 return g_achEncodeTable[(int)c]; 00078 }
void Cgi::Base64::encode | ( | const char * | in, | |
char * | out | |||
) | [private] |
Definition at line 81 of file Base64.cpp.
References reverse().
Referenced by encode64(), encode_buffer(), encode_file(), and encode_from_file().
00082 { 00083 int i1 = (in[0] >> 2) & 63; 00084 int i2 = ( ((in[0] & 3) << 4) + ((in[1] >> 4) & 15) ) & 63; 00085 int i3 = ( ((in[1] & 15) << 2) + ((in[2] >> 6) & 3) ) & 63; 00086 int i4 = in[2] & 63; 00087 00088 out[0] = reverse( (char)i1); 00089 out[1] = reverse( (char)i2); 00090 out[2] = reverse( (char)i3); 00091 out[3] = reverse( (char)i4); 00092 }
int Cgi::Base64::decode | ( | char | c | ) | [private] |
Definition at line 95 of file Base64.cpp.
References base64vec.
Referenced by decode_string().
00096 { 00097 return base64vec[(int)c]; 00098 }
void Cgi::Base64::decode_string | ( | char * | s, | |
unsigned char * | output, | |||
int & | l | |||
) | [private] |
Definition at line 101 of file Base64.cpp.
References decode(), and m_eqchar.
Referenced by decode_file(), decode_length(), decode_stdout(), decode_to_buffer(), decode_to_file(), and decode_to_stdout().
00102 { 00103 union { 00104 unsigned long l; 00105 char s[4]; 00106 } u; 00107 00108 if (s[2] == m_eqchar) 00109 { 00110 l = 1; 00111 u.l = decode(s[0]) << 18 | decode(s[1]) << 12; 00112 00113 output[0] = u.s[2]; 00114 output[1] = 0; 00115 output[2] = 0; 00116 return; 00117 } 00118 else 00119 if (s[3] == m_eqchar) 00120 { 00121 l = 2; 00122 u.l = decode(s[0]) << 18 | decode(s[1]) << 12 | decode(s[2]) << 6; 00123 00124 output[0] = u.s[2]; 00125 output[1] = u.s[1]; 00126 output[2] = 0; 00127 return; 00128 } 00129 l = 3; 00130 u.l = decode(s[0]) << 18 | decode(s[1]) << 12 | decode(s[2]) << 6 | decode(s[3]); 00131 00132 output[0] = u.s[2]; 00133 output[1] = u.s[1]; 00134 output[2] = u.s[0]; 00135 }
char Cgi::Base64::m_eqchar [private] |
Definition at line 80 of file Base64.h.
Referenced by decode_string(), encode64(), encode_buffer(), encode_file(), and encode_from_file().