Url编解码(C)
2009-05-13 11:41:55来源:未知 阅读 ()
static const char c2x_table[] = "0123456789ABCDEF";
/* 字符转16进制 */
static unsigned char *c2x(unsigned what,unsigned char *where)
{
*where++ = '%';
*where++ = c2x_table[what>>4];
*where++ = c2x_table[what&0xf];
return where;
}
/* 16进制转字符 */
static char x2c(const char *what)
{
register char digit;
digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
return (digit);
}
/*
%\\^[]`+$,@:;/!#?=&.
不安全字符及特殊字符
*/
static int unsafechar(unsigned char ch)
{
unsigned char *p=&ch;
if(*p==' '||*p==''||*p=='%'||*p=='\\'||*p=='^'||*p=='['||*p==']'||*p=='`'\
||*p=='+'||*p=='$'||*p==','||*p=='@'||*p==':'||*p==';'\
||*p=='/'||*p=='!'||*p=='#'||*p=='?'||*p=='='||*p=='&'||*p=='.'||*p>0x80){
return(1);
}
else{
return(0);
}
}
/*
URL解码
*/
size_t url_decode(const char *src,char *dest)
{
char *cp=dest;
while(*src!='\0')
{
if(*src=='+')
{
*dest++=' ';
}
else if(*src=='%')
{
int ch;
ch=x2c(src+1);
*dest++=ch;
src+=2;
}
else
{
*dest++=*src;
}
src++;
}
*dest='\0';
return(dest-cp);
}
/*
URL编码
*/
size_t url_encode(const char *src, char *dest)
{
char *cp=dest;
while(*src!='\0')
{
unsigned char *p=(unsigned char*)src;
if(*p==' '){
*dest++='+';
}
else if(unsafechar(*p))
{
unsigned char w[3]={'\0'};
c2x(*p,w);
*dest=w[0];
*(dest+1)=w[1];
*(dest+2)=w[2];
dest+=3;
}
else
{
*dest++=*p;
}
src++;
}
*dest='\0';
return(dest-cp);
}
int main()
{
char *s="中国人民@#$%^&*();/@";
char *d=NULL,*d1=NULL;
d=(char *)malloc(100);
d1=(char *)malloc(100);
memset(d,0,100);
memset(d1,0,100);
url_encode(s,d);
printf("%s\n",s);
printf("%s\n",d);
url_decode(d,d1);
printf("%s\n",d1);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:FREEBSD 命令大全
- freeBSD7 boot from zfs 2009-05-13
- 如何更新openbsd的src 2009-05-13
- warning: do not list domain in BOTH mydestination 2009-05-13
- apache mod_rewrite - URL Rewriting 2009-05-13
- we provide the characteristics of services 2009-05-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash