📄 base64.c
字号:
#include "base64.h"
#include "config.h"
//#define isalnum(c) ( (c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9') )
#define is_base64(c) (isalnum(c) || (c == '+') || (c == '/'))
const char *base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
//对bytes_to_encode进行base64编码,并输出到outBuf中。
//注意:要获得完整的base64编码,outBuf的大小必须大于或等于原始数据的(1+1/3)倍
// (如果原始数据大小不是3的倍数,outBuf应多增加4字节缓存)。
unsigned char *base64_encode(const unsigned char *bytes_to_encode, unsigned int in_len,
unsigned char *outBuf, unsigned int bufSize)
{
int i = 0, j = 0;
int outIdx = 0;
unsigned char char_array_3[3], char_array_4[4];
if ( bytes_to_encode == NULL || outBuf == NULL )
return NULL;
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
{
if ( outIdx >= bufSize )
return outBuf;
outBuf[outIdx++] = base64_chars[char_array_4[i]];
}
i = 0;
}
}
//处理不足3字节的数据(最后1字节或2字节)
if (i)
{
//用0补足3字节原始数据
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
{
if ( outIdx >= bufSize )
return outBuf;
outBuf[outIdx++] = base64_chars[char_array_4[j]];
}
//将输出的base64编码补足4的倍数。
while((i++ < 3))
{
if ( outIdx >= bufSize )
return outBuf;
outBuf[outIdx++] = '=';
}
}
return outBuf;
}
//对encoded_string中的base64编码进行解码,并输出到outBuf中。
//注意:要获得完整的解码数据,outBuf的大小必须大于base64编码数据的3/4。
unsigned char *base64_decode(const unsigned char *encoded_string, unsigned char *outBuf, unsigned int bufSize)
{
int in_len = strlen((const char *)encoded_string);
int i = 0, j = 0, in_ = 0;
int outIdx = 0;
unsigned char char_array_4[4], char_array_3[3];
if ( encoded_string == NULL || outBuf == NULL )
return NULL;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4)
{
for (i = 0; i <4; i++)
char_array_4[i] = strchr(base64_chars, char_array_4[i]) - base64_chars;
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
{
if ( outIdx >= bufSize )
return outBuf;
outBuf[outIdx++] = char_array_3[i];
}
i = 0;
}
}
if (i)
{
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = strchr(base64_chars, char_array_4[j]) - base64_chars;
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
{
if ( outIdx >= bufSize )
return outBuf;
outBuf[outIdx++] += char_array_3[j];
}
}
return outBuf;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -