Base64编码解码和URLEnocde编码解码的C实现

类别:编程语言 点击:0 评论:0 推荐:

作者:JsuFcz

/* base64编码函数 */

int base64(char *s,char *d)
{
char CharSet[64]={
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
unsigned char In[3];
unsigned char Out[4];
int cnt=0;

if(!s||!d) return 0;

for(;*s!=0;)
{
if(cnt+4>76)
{
*d++='\n';
cnt=0;
}
if(strlen(s)>=3)
{
In[0]=*s;
In[1]=*(s+1);
In[2]=*(s+2);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|(In[2]&0xc0)>>6;
Out[3]=In[2]&0x3f;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)=CharSet[Out[3]];
s+=3;
d+=4;
}
else if(strlen(s)==1)
{
In[0]=*s;
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)='=';
*(d+3)='=';
s+=1;
d+=4;
}
else if(strlen(s)==2)
{
In[0]=*s;
In[1]=*(s+1);
Out[0]=In[0]>>2;
Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
Out[2]=(In[1]&0x0f)<<2|0;
*d=CharSet[Out[0]];
*(d+1)=CharSet[Out[1]];
*(d+2)=CharSet[Out[2]];
*(d+3)='=';
s+=2;
d+=4;
}
cnt+=4;
}
*d='\0';
return 1;
}



/* base64解码函数 */

int unbase64char(char ch)
{
char CharSet[64]={
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
int i;
for(i=0;i<=63;i++) if(CharSet[i]==ch) break;
return i;
}

int unbase64(char *s,char *d)
{
unsigned char In[4];
unsigned char Out[3];

if(!s||!d) return 0;

for(;*s!=0;)
{
if(*s=='\n') s++;
In[0]=s[0];
In[1]=s[1];
In[2]=s[2];
In[3]=s[3];
if(In[2]!='='&&In[3]!='=')
{
In[0]=unbase64char(In[0]);
In[1]=unbase64char(In[1]);
In[2]=unbase64char(In[2]);
In[3]=unbase64char(In[3]);
Out[0]=In[0]<<2|(In[1]&0x30)>>4;
Out[1]=(In[1]&0x0f)<<4|(In[2]&0x3c)>>2;
Out[2]=(In[2]&0x03)<<6|In[3]&0x3f;
d[0]=Out[0];
d[1]=Out[1];
d[2]=Out[2];
s+=4;
d+=3;
}
else if(In[2]=='='&&In[3]=='=')
{
In[0]=unbase64char(In[0]);
In[1]=unbase64char(In[1]);
Out[0]=In[0]<<2|(In[1]&0x30)>>4;
d[0]=Out[0];
s+=4;
d+=1;
}
else if(In[2]!='='&&In[3]=='=')
{
In[0]=unbase64char(In[0]);
In[1]=unbase64char(In[1]);
In[2]=unbase64char(In[2]);
Out[0]=In[0]<<2|(In[1]&0x30)>>4;
Out[1]=(In[1]&0x0f)<<4|(In[2]&0x3c)>>2;
d[0]=Out[0];
d[1]=Out[1];
s+=4;
d+=2;
}
}
*d='\0';
return 1;
}





/* Unencode URL编码函数 */
/*
在这里要注意,编译器在处理中文字符时,会自动根据字符的位7来读入一个
或两个字符,这时可以强制采用unsigned char *来读入一个字符。可以看下
面isT()的用法
*/

int isT(char ch)
{
// unsigned char *p=(unsigned char*)&ch;
// if(*p==' '||*p=='='||*p=='%'||*p=='.'||*p=='/'||*p>126) return 1;
// else return 0;
if(ch==' '||ch=='='||ch=='%'||ch=='.'||ch=='/'||ch&0x80) return 1;
else return 0;
}

int encode(char *s,char *d)
{
if(!s||!d) return 0;
for(;*s!=0;s++)
{
unsigned char *p=(unsigned char*)s;
if(*p==' ')
{
*d='+';
d++;
}
else if(isT(*p))
{
char a[3];
*d='%';
sprintf(a,"%02x",*p);
*(d+1)=a[0];
*(d+2)=a[1];
d+=3;
}
else
{
*d=*p;
d++;
}
}
*d=0;
return 1; 
}


/* Unencode URL解码函数 */

int unencode(char *s,char *d)
{
if(!s||!d) return 0;
for(;*s!=0;s++)
{
if(*s=='+')
{
*d=' ';
d++;
}
else if(*s=='%')
{
int code;
if(sscanf(s+1,"%02x",&code)!=1) code='?';
*d=code;
s+=2;
d++;
}
else
{
*d=*s;
d++;
}
}
*d=0;
return 1;
}

本文地址:http://com.8s8s.com/it/it22592.htm