C语言版 Base64编码

本文介绍了一个Base64编码函数及一个用于从Base64字符串解码并直接写入文件的函数。编码函数能够处理不同长度的输入,并正确填充输出。解码函数则特别针对常见解码函数在处理末尾等号时可能产生的多余字节问题进行了优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



in_str是输入缓冲区的地址
in_len 要编码的数据的长度
out_str 输出的地址
需要在外面分配内存


unsigned char *out_str = (unsigned char *) malloc (in_len * 4 / 3 + 1);


然后再调用函数编码。


void Base64Encode(const unsigned char *in_str, int in_len, unsigned char *out_str)
{ 
static unsigned char base64[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int curr_out_len = 0;
int i = 0;
unsigned char a, b, c;
out_str[0] = '\0';
if (in_len > 0)
{
   while (i < in_len)
   {
    a = in_str[i];
    b = (i + 1 >= in_len) ? 0 : in_str[i + 1];
    c = (i + 2 >= in_len) ? 0 : in_str[i + 2];
    if (i + 2 < in_len)
    {
     out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
     out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
     out_str[curr_out_len++] = (base64[((b << 2) & 0x3c) + ((c >> 6) & 0x3)]);
     out_str[curr_out_len++] = (base64[c & 0x3F]);
    }
    else if (i + 1 < in_len)
    {
     out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
     out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
     out_str[curr_out_len++] = (base64[((b << 2) & 0x3c) + ((c >> 6) & 0x3)]);
     out_str[curr_out_len++] = '=';
    }
    else
    {
     out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
     out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
     out_str[curr_out_len++] = '=';
     out_str[curr_out_len++] = '=';
    }
    i += 3;
   }
   out_str[curr_out_len] = '\0';
}
return;
}



网上的解码函数,都是解码成字符串的,如果解码并保存到文件中,那么往往会多保存几个字节,主要是对base64字符串最后面的=号的判断不够,所以只要自行实现了一下:
void __stdcall Base64DecodeToFile(unsigned char *input, const unsigned char *dst_filename)
{
static int map[256] = {0};
static unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t inplen = strlen(input);
int words = (inplen+3)/4;
size_t i=0, j=0;
int word = 0;
char *p = input;
int padnum = 0;
FILE *fd = NULL;


for (i = 0; i < 64; ++i)
{
   map[(int)b64[i]]=i;
}
if(input[inplen - 1] == '=') padnum = 1;
if(input[inplen - 1] == '=' && input[inplen - 2] == '=') padnum = 2;


fd = fopen(dst_filename,"wb");
if (fd == NULL) return;
for(i=0; i<words; i++)
{
   word = 0;
   word |= map[(int)*p++];
   word <<= 6;
   word |= map[(int)*p++];
   word <<= 6;
   word |= map[(int)*p++];
   word <<= 6;
   word |= map[(int)*p++];
   fprintf(fd, "%c", word >> 16 & 0xFF);
  
   if (i + 1 == words && padnum == 2) 
    break;
   fprintf(fd, "%c", word >> 8 & 0xFF);
  
   if (i + 1 == words && padnum == 1) 
    break;
   fprintf(fd, "%c", word & 0xFF);
}
fclose(fd);
return;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值