Base64編碼解碼和URLEnocde編碼解碼的C實現

來源:互聯網
上載者:User

作者: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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.