// 需要自己开辟空间 const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int hex2dec(char c) { if ('0' <= c && c <= '9') { return c - '0'; } else if ('a' <= c && c <= 'f') { return c - 'a' + 10; } else if ('A' <= c && c <= 'F') { return c - 'A' + 10; } else { return -1; } } char dec2hex(short int c) { if (0 <= c && c <= 9) { return c + '0'; } else if (10 <= c && c <= 15) { return c + 'A' - 10; } else { return -1; } } //urldecode编码 /* dest: 编码好的数据放在这里 src: 待编码的数据 dest <----- src */ void urlencode(char *dest, char *src) { int i = 0; int len = strlen(src); int res_len = 0; for (i = 0; i < len; ++i) { char c = src[i]; if ( ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '/' || c == '.') { dest[res_len++] = c; } else { int j = (short int)c; if (j < 0) j += 256; int i1, i0; i1 = j / 16; i0 = j - i1 * 16; dest[res_len++] = '%'; dest[res_len++] = dec2hex(i1); dest[res_len++] = dec2hex(i0); } } dest[res_len] = '\0'; } //urlencode解码 /* dest: 解码好的数据放在这里 src: 待解码的数据 dest <----- src */ void urldecode(char *dest, char *src) { int i = 0; int len = strlen(src); int res_len = 0; for (i = 0; i < len; ++i) { char c = src[i]; if (c != '%') { dest[res_len++] = c; } else { char c1 = src[++i]; char c0 = src[++i]; int num = 0; num = hex2dec(c1) * 16 + hex2dec(c0); dest[res_len++] = num; } } dest[res_len] = '\0'; } // base64编码 /* bindta: 原数据 base64: 转换成base64的数据 binlength: 原数据长度 */ void base64_encode(char * base64, const unsigned char * bindata, int binlength) { int i, j; unsigned char current; for ( i = 0, j = 0 ; i < binlength ; i += 3 ) { current = (bindata[i] >> 2) ; current &= (unsigned char)0x3F; base64[j++] = base64char[(int)current]; current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ; if ( i + 1 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F ); base64[j++] = base64char[(int)current]; current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ; if ( i + 2 >= binlength ) { base64[j++] = base64char[(int)current]; base64[j++] = '='; break; } current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 ); base64[j++] = base64char[(int)current]; current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ; base64[j++] = base64char[(int)current]; } base64[j] = '\0'; // printf("base_data: %s", base64); } // base64解码 /* base64: 原base64数据 bindta: 解码后的的数据 */ int base64_decode(unsigned char * bindata, const char * base64) { int i, j; unsigned char k; unsigned char temp[4]; for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 ) { memset( temp, 0xFF, sizeof(temp) ); for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i] ) temp[0]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+1] ) temp[1]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+2] ) temp[2]= k; } for ( k = 0 ; k < 64 ; k ++ ) { if ( base64char[k] == base64[i+3] ) temp[3]= k; } bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) | ((unsigned char)((unsigned char)(temp[1]>>4)&0x03)); if ( base64[i+2] == '=' ) break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) | ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F)); if ( base64[i+3] == '=' ) break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) | ((unsigned char)(temp[3]&0x3F)); } return j; }