📄 base.c
字号:
/****************************************************************
This is a sample routine of base64 algorithm.The goal is to
illustrate principles,so some details may be ignored.
Author email:zhangwu2003@163.com
*****************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
unsigned char EncodBuf[16];
unsigned char DecodBuf[12];
void encode(unsigned char *source,int srclen)
{
int n,buflen,i,j;
//unsigned char *buf;
//buf=src;
buflen=n=srclen;
/*if(n%3!=0)
{
buflen=n+3-n%3;
buf=malloc(buflen);
for(i=0;i<3-n%3;i++)
{
buf[n+i]='='; //末位补充
}
}*/
//EncodBuf=malloc(buflen*4/3);
for(i=0,j=0;i<buflen;i+=3,j+=4)
{
EncodBuf[j]=(source[i]>>2)&0x3f;
EncodBuf[j+1]=((source[i]<<4) + (source[i+1]>>4))&0x3f;
EncodBuf[j+2]=((source[i+1]<<2) + (source[i+2]>>6))&0x3f;
EncodBuf[j+3]=source[i+2]&0x3F;
}
}
void decode(unsigned char *source,int srclen)
{
int n,i,j;
n=srclen;
//DecodBuf=malloc(srclen*3/4);
for(i=0,j=0;i<n;i+=4,j+=3)
{
DecodBuf[j]=(source[i]<<2) + ((source[i+1]&0x30)>>4);
DecodBuf[j+1]=((source[i+1]&0x0F)<<4) + ((source[i+2]&0x3C)>>2);
DecodBuf[j+2]=((source[i+2]&0x03)<<6) + source[i+3];
}
}
void main()
{
unsigned char src[12]={1,2,3,4,5,6,7,8,9,10,11,0};
unsigned int i;
encode(src,12); /* the second parameter must accord with the first one */
decode(EncodBuf,16);
for(i=0;i<12;i++)
{
printf("%02x ",src[i]);
}
printf("\n");
for(i=0;i<16;i++)
{
printf("%02x ",EncodBuf[i]);
}
printf("\n");
for(i=0;i<12;i++)
{
printf("%02x ",DecodBuf[i]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -