comparison src/util.c @ 5426:8d8bf0d31a23

[gaim-migrate @ 5805] i'm cleaning out my tree 1. new tobase64 function that works better 2. new date/time stamp parsing stuff for jabber 3. other misc stuff, not deserving of specific mention committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Sun, 18 May 2003 05:45:18 +0000
parents ccec8893c1be
children a2f26666de42
comparison
equal deleted inserted replaced
5425:73bde657e212 5426:8d8bf0d31a23
348 348
349 static const char alphabet[] = 349 static const char alphabet[] =
350 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 350 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
351 "0123456789+/"; 351 "0123456789+/";
352 352
353 /* XXX Find bug */ 353 /* This was borrowed from the Kame source, and then tweaked to our needs */
354 char *tobase64(const char *text) 354 char *tobase64(const unsigned char *buf, size_t len)
355 { 355 {
356 char *out = NULL; 356 char *s, *rv;
357 const char *c; 357 unsigned tmp;
358 unsigned int tmp = 0; 358
359 int len = 0, n = 0; 359 if(len < 0)
360 360 len = strlen(buf);
361 c = text; 361
362 362 s = g_malloc((4 * (len + 1)) / 3 + 1);
363 while (*c) { 363
364 tmp = tmp << 8; 364 rv = s;
365 tmp += *c; 365 while (len >= 3) {
366 n++; 366 tmp = buf[0] << 16 | buf[1] << 8 | buf[2];
367 367 s[0] = alphabet[tmp >> 18];
368 if (n == 3) { 368 s[1] = alphabet[(tmp >> 12) & 077];
369 out = g_realloc(out, len + 4); 369 s[2] = alphabet[(tmp >> 6) & 077];
370 out[len] = alphabet[(tmp >> 18) & 0x3f]; 370 s[3] = alphabet[tmp & 077];
371 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 371 len -= 3;
372 out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; 372 buf += 3;
373 out[len + 3] = alphabet[tmp & 0x3f]; 373 s += 4;
374 len += 4; 374 }
375 tmp = 0; 375
376 n = 0; 376 /* RFC 1521 enumerates these three possibilities... */
377 } 377 switch(len) {
378 c++; 378 case 2:
379 } 379 tmp = buf[0] << 16 | buf[1] << 8;
380 switch (n) { 380 s[0] = alphabet[(tmp >> 18) & 077];
381 381 s[1] = alphabet[(tmp >> 12) & 077];
382 case 2: 382 s[2] = alphabet[(tmp >> 6) & 077];
383 tmp <<= 8; 383 s[3] = '=';
384 out = g_realloc(out, len + 5); 384 s[4] = '\0';
385 out[len] = alphabet[(tmp >> 18) & 0x3f]; 385 break;
386 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 386 case 1:
387 out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; 387 tmp = buf[0] << 16;
388 out[len + 3] = '='; 388 s[0] = alphabet[(tmp >> 18) & 077];
389 out[len + 4] = 0; 389 s[1] = alphabet[(tmp >> 12) & 077];
390 break; 390 s[2] = s[3] = '=';
391 case 1: 391 s[4] = '\0';
392 tmp <<= 16; 392 break;
393 out = g_realloc(out, len + 5); 393 case 0:
394 out[len] = alphabet[(tmp >> 18) & 0x3f]; 394 s[0] = '\0';
395 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 395 break;
396 out[len + 2] = '='; 396 }
397 out[len + 3] = '='; 397
398 out[len + 4] = 0; 398 return rv;
399 break;
400 case 0:
401 out = g_realloc(out, len + 1);
402 out[len] = 0;
403 break;
404 }
405 return out;
406 } 399 }
407 400
408 401
409 void frombase64(const char *text, char **data, int *size) 402 void frombase64(const char *text, char **data, int *size)
410 { 403 {