base64编码/解码函数 c语言
作者:admin 日期:2010-07-15
上面的代码只能处理字符串,改进一下
- int base64(char *s,char *d,int sl)
- {
- 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(sl==0)return 0;
- //if(!s||!d) return 0;
-
- int dl=0;
- //for(;*s!=0;)
- for(int i=0;i<sl;i=i+3)
- {
- /* if(cnt+4>76)
- {
- *d++='\n';
- cnt=0;
- }
- */
- //if(strlen(s)>=3)
- if((sl-i)>=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)
- else if((sl-i)==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)
- else if((sl-i)==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;
- dl+=4;
- }
- *d='\0';
- return dl;
- }
评论: 0 | 查看次数: 11074