Changeset 812a413
- Timestamp:
- 2006-06-23T18:15:28Z (18 years ago)
- Branches:
- master
- Children:
- df1694b
- Parents:
- 00ab350
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/yahoo/libyahoo2.c
r00ab350 r812a413 695 695 } 696 696 697 static char base64digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 698 "abcdefghijklmnopqrstuvwxyz" 699 "0123456789._"; 697 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */ 700 698 static void to_y64(unsigned char *out, const unsigned char *in, int inlen) 701 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */ 702 { 703 for (; inlen >= 3; inlen -= 3) 704 { 705 *out++ = base64digits[in[0] >> 2]; 706 *out++ = base64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 707 *out++ = base64digits[((in[1]<<2) & 0x3c) | (in[2]>>6)]; 708 *out++ = base64digits[in[2] & 0x3f]; 709 in += 3; 710 } 711 if (inlen > 0) 712 { 713 unsigned char fragment; 714 715 *out++ = base64digits[in[0] >> 2]; 716 fragment = (in[0] << 4) & 0x30; 717 if (inlen > 1) 718 fragment |= in[1] >> 4; 719 *out++ = base64digits[fragment]; 720 *out++ = (inlen < 2) ? '-' 721 : base64digits[(in[1] << 2) & 0x3c]; 722 *out++ = '-'; 723 } 724 *out = '\0'; 699 { 700 return base64_encode_real(in, inlen, out, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); 725 701 } 726 702 -
util.c
r00ab350 r812a413 84 84 } 85 85 86 static char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "0123456789+/";87 88 /* XXX Find bug */89 86 char *tobase64(const char *text) 90 87 { 91 char *out = NULL; 92 const char *c; 93 unsigned int tmp = 0; 94 int len = 0, n = 0; 95 96 c = text; 97 98 while (*c) { 99 tmp = tmp << 8; 100 tmp += *c; 101 n++; 102 103 if (n == 3) { 104 out = g_realloc(out, len + 4); 105 out[len] = alphabet[(tmp >> 18) & 0x3f]; 106 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 107 out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; 108 out[len + 3] = alphabet[tmp & 0x3f]; 109 len += 4; 110 tmp = 0; 111 n = 0; 112 } 113 c++; 114 } 115 switch (n) { 116 117 case 2: 118 tmp <<= 8; 119 out = g_realloc(out, len + 5); 120 out[len] = alphabet[(tmp >> 18) & 0x3f]; 121 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 122 out[len + 2] = alphabet[(tmp >> 6) & 0x3f]; 123 out[len + 3] = '='; 124 out[len + 4] = 0; 125 break; 126 case 1: 127 tmp <<= 16; 128 out = g_realloc(out, len + 5); 129 out[len] = alphabet[(tmp >> 18) & 0x3f]; 130 out[len + 1] = alphabet[(tmp >> 12) & 0x3f]; 131 out[len + 2] = '='; 132 out[len + 3] = '='; 133 out[len + 4] = 0; 134 break; 135 case 0: 136 out = g_realloc(out, len + 1); 137 out[len] = 0; 138 break; 139 } 88 char *out; 89 int len; 90 91 len = strlen(text); 92 out = g_malloc((len + 2) /* the == padding */ 93 / 3 /* every 3-byte block */ 94 * 4 /* becomes a 4-byte one */ 95 + 1); /* and of course, ASCIIZ! */ 96 97 base64_encode_real(text, len, out, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="); 98 140 99 return out; 100 } 101 102 void base64_encode_real(const unsigned char *in, int inlen, unsigned char *out, char *b64digits) 103 { 104 for (; inlen >= 3; inlen -= 3) 105 { 106 *out++ = b64digits[in[0] >> 2]; 107 *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 108 *out++ = b64digits[((in[1]<<2) & 0x3c) | (in[2]>>6)]; 109 *out++ = b64digits[in[2] & 0x3f]; 110 in += 3; 111 } 112 if (inlen > 0) 113 { 114 *out++ = b64digits[in[0] >> 2]; 115 if (inlen > 1) 116 { 117 *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 118 *out++ = b64digits[((in[1]<<2) & 0x3c)]; 119 } 120 else 121 { 122 *out++ = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 123 *out++ = b64digits[64]; 124 } 125 *out++ = b64digits[64]; 126 } 127 *out = '\0'; 141 128 } 142 129 -
util.h
r00ab350 r812a413 31 31 G_MODULE_EXPORT char *strip_newlines(char *source); 32 32 G_MODULE_EXPORT char *tobase64( const char *text ); 33 G_MODULE_EXPORT void base64_encode_real( const unsigned char *in, int inlen, unsigned char *out, char *b64digits ); 33 34 G_MODULE_EXPORT char *normalize( const char *s ); 34 35 G_MODULE_EXPORT void info_string_append( GString *str, char *newline, char *name, char *value );
Note: See TracChangeset
for help on using the changeset viewer.