📄 base64.c
字号:
#include "base64.h"#define BASE64_ENCODE_OP1(ch) base64[(ch >> 2) & 0x3f]#define BASE64_ENCODE_OP2(ch1, ch2) base64[((ch1 << 4) & 0x30) | ((ch2 >> 4) & 0x0f)]#define BASE64_ENCODE_OP3(ch1, ch2) base64[((ch1 << 2) & 0x3c) | ((ch2 >> 6) & 0x03)]#define BASE64_ENCODE_OP4(ch) base64[ch & 0x3f]#define BASE64_DECODE_OP1(ch1, ch2) (getbase64pos(ch1) << 2 & 0xfc) | ((getbase64pos(ch2) >> 4) & 0x03) #define BASE64_DECODE_OP2(ch1, ch2) (getbase64pos(ch1) << 4 & 0xf0) | ((getbase64pos(ch2) >> 2) & 0x0f)#define BASE64_DECODE_OP3(ch1, ch2) (getbase64pos(ch1) << 6 & 0xc0) | (getbase64pos(ch2) & 0x3f)char base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int getbase64pos(char ch){ int i; for(i=0; i<strlen(base64); i++) { if (ch == base64[i]) return i; } return 0;}/* base64 encode, length change 3->4 *//* "abc"->"61 62 63"->"01100001 01100010 01100011 *//* 00011000 00010110 00001001 00100011 */int base64_encode(char *s, int sl, char *d){ int i; int rl=0; for(i=0; i<sl/3; i++) { d[i*4] = BASE64_ENCODE_OP1(s[i*3]); d[i*4+1] = BASE64_ENCODE_OP2(s[i*3], s[i*3+1]); d[i*4+2] = BASE64_ENCODE_OP3(s[i*3+1], s[i*3+2]); d[i*4+3] = BASE64_ENCODE_OP4(s[i*3+2]); } rl = (sl / 3) * 4; if (sl % 3 == 1) { d[i*4] = BASE64_ENCODE_OP1(s[i*3]); d[i*4+1] = BASE64_ENCODE_OP2(s[i*3], 0x00); d[i*4+2] = '='; d[i*4+3] = '='; rl += 4; } else if (sl % 3 == 2) { d[i*4] = BASE64_ENCODE_OP1(s[i*3]); d[i*4+1] = BASE64_ENCODE_OP2(s[i*3], s[i*3+1]); d[i*4+2] = BASE64_ENCODE_OP3(s[i*3+1], 0x00); d[i*4+3] = '='; rl += 4; } return rl; }/* base64 decode, length change 4->3 */int base64_decode(char *s, int sl, char *d){ int i; for(i=0; i<sl/4; i++) { d[3*i] = BASE64_DECODE_OP1(s[i*4], s[i*4+1]); d[3*i+1] = BASE64_DECODE_OP2(s[i*4+1], s[i*4+2]); d[3*i+2] = BASE64_DECODE_OP3(s[i*4+2], s[i*4+3]); } return sl * 3 / 4;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -