Changeset b38d399
- Timestamp:
- 2014-11-24T05:16:09Z (10 years ago)
- Branches:
- master
- Children:
- 9f8bb17
- Parents:
- e2472dd
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/base64.c
re2472dd rb38d399 27 27 #include "base64.h" 28 28 29 static const char real_b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";30 31 29 char *tobase64(const char *text) 32 30 { … … 36 34 char *base64_encode(const unsigned char *in, int len) 37 35 { 38 char *out; 39 40 out = g_malloc((len + 2) /* the == padding */ 41 / 3 /* every 3-byte block */ 42 * 4 /* becomes a 4-byte one */ 43 + 1); /* and of course, ASCIIZ! */ 44 45 base64_encode_real((unsigned char*) in, len, (unsigned char*) out, real_b64); 46 47 return out; 48 } 49 50 int base64_encode_real(const unsigned char *in, int inlen, unsigned char *out, const char *b64digits) 51 { 52 int outlen = 0; 53 54 for (; inlen >= 3; inlen -= 3) 55 { 56 out[outlen++] = b64digits[in[0] >> 2]; 57 out[outlen++] = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 58 out[outlen++] = b64digits[((in[1]<<2) & 0x3c) | (in[2]>>6)]; 59 out[outlen++] = b64digits[in[2] & 0x3f]; 60 in += 3; 61 } 62 if (inlen > 0) 63 { 64 out[outlen++] = b64digits[in[0] >> 2]; 65 if (inlen > 1) 66 { 67 out[outlen++] = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)]; 68 out[outlen++] = b64digits[((in[1]<<2) & 0x3c)]; 69 } 70 else 71 { 72 out[outlen++] = b64digits[((in[0]<<4) & 0x30)]; 73 out[outlen++] = b64digits[64]; 74 } 75 out[outlen++] = b64digits[64]; 76 } 77 out[outlen] = 0; 78 79 return outlen; 36 return g_base64_encode(in, len); 80 37 } 81 38 … … 85 42 { 86 43 unsigned char *out; 87 88 44 base64_decode(in, &out); 89 90 45 return (char*) out; 91 46 } 92 47 93 /* FIXME: Lookup table stuff is not threadsafe! (But for now BitlBee is not threaded.) */94 48 int base64_decode(const char *in, unsigned char **out) 95 49 { 96 static char b64rev[256] = { 0 }; 97 int len, i; 98 99 /* Create a reverse-lookup for the Base64 sequence. */ 100 if( b64rev[0] == 0 ) 101 { 102 memset( b64rev, 0xff, 256 ); 103 for( i = 0; i <= 64; i ++ ) 104 b64rev[(int)real_b64[i]] = i; 105 } 106 107 len = strlen( in ); 108 *out = g_malloc( ( len + 6 ) / 4 * 3 ); 109 len = base64_decode_real( (unsigned char*) in, *out, b64rev ); 110 *out = g_realloc( *out, len + 1 ); 111 out[0][len] = 0; /* Zero termination can't hurt. */ 112 50 gsize len; 51 *out = g_base64_decode(in, &len); 52 53 /* Some silly functions expect it to be zero terminated */ 54 *out = g_realloc(*out, len + 1); 55 out[0][len] = 0; 56 113 57 return len; 114 58 } 115 116 int base64_decode_real(const unsigned char *in, unsigned char *out, char *b64rev)117 {118 int i, outlen = 0;119 120 for( i = 0; in[i] && in[i+1] && in[i+2] && in[i+3]; i += 4 )121 {122 int sx;123 124 sx = b64rev[(int)in[i+0]];125 if( sx >= 64 )126 break;127 out[outlen] = ( sx << 2 ) & 0xfc;128 129 sx = b64rev[(int)in[i+1]];130 if( sx >= 64 )131 break;132 out[outlen] |= ( sx >> 4 ) & 0x03;133 outlen ++;134 out[outlen] = ( sx << 4 ) & 0xf0;135 136 sx = b64rev[(int)in[i+2]];137 if( sx >= 64 )138 break;139 out[outlen] |= ( sx >> 2 ) & 0x0f;140 outlen ++;141 out[outlen] = ( sx << 6 ) & 0xc0;142 143 sx = b64rev[(int)in[i+3]];144 if( sx >= 64 )145 break;146 out[outlen] |= sx;147 outlen ++;148 }149 150 /* If sx > 64 the base64 string was damaged. Should we ignore this? */151 152 return outlen;153 } -
lib/base64.h
re2472dd rb38d399 28 28 G_MODULE_EXPORT char *tobase64( const char *text ); 29 29 G_MODULE_EXPORT char *base64_encode( const unsigned char *in, int len ); 30 G_MODULE_EXPORT int base64_encode_real( const unsigned char *in, int inlen, unsigned char *out, const char *b64digits );31 30 G_MODULE_EXPORT char *frombase64( const char *in ); 32 31 G_MODULE_EXPORT int base64_decode( const char *in, unsigned char **out ); 33 G_MODULE_EXPORT int base64_decode_real( const unsigned char *in, unsigned char *out, char *b64reverse ); -
lib/oauth.c
re2472dd rb38d399 76 76 { 77 77 unsigned char bytes[21]; 78 char *ret = g_new0( char, sizeof( bytes) / 3 * 4 + 1 );79 80 78 random_bytes( bytes, sizeof( bytes ) ); 81 base64_encode_real( bytes, sizeof( bytes), (unsigned char*) ret, "0123456789" 82 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0A" ); 83 84 return ret; 79 return base64_encode( bytes, sizeof( bytes ) ); 85 80 } 86 81 -
protocols/yahoo/libyahoo2.c
re2472dd rb38d399 681 681 } 682 682 683 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated)*/683 /* yahoo's variant of base64 */ 684 684 static void to_y64(unsigned char *out, const unsigned char *in, int inlen) 685 685 { 686 base64_encode_real(in, inlen, out, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-"); 686 char *encoded = base64_encode(in, inlen); 687 int i = 0; 688 689 do { 690 if (encoded[i] == '+') { 691 out[i] = '.'; 692 } else if (encoded[i] == '/') { 693 out[i] = '_'; 694 } else if (encoded[i] == '=') { 695 out[i] = '-'; 696 } else { 697 out[i] = encoded[i]; 698 } 699 } while (encoded[i++]); 700 701 g_free(encoded); 687 702 } 688 703
Note: See TracChangeset
for help on using the changeset viewer.