C language implementation of URL encode and URL decode

Source: Internet
Author: User
Tags url decode

Reprinted from: http://blog.csdn.net/langeldep/article/details/6264058

The code for this article has been modified from PHP code, leaving only 2 functions.

int Php_url_decode (char *str, int len);
Char *php_url_encode (char const *s, int len, int *new_length);

URL encoding does the following:

Character "a"-"Z" , "A"-"Z" , "0"-"9" "." , "-" , "*" "_"   are not coded to maintain the original value;

The space "" is converted to the plus sign "+".

Each of the other bytes is represented as a 3-character string in the "%xy" format , encoded as UTF-8.

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#include<sys/types.h>StaticUnsignedCharHexchars[] ="0123456789ABCDEF";Static intPhp_htoi (Char*1) {    intvalue; intC; C= ((unsignedChar*) s) [0]; if(Isupper (c)) C=ToLower (c); Value= (c >='0'&& C <='9'? C'0'C'a'+Ten) * -; C= ((unsignedChar*) s) [1]; if(Isupper (c)) C=ToLower (c); Value+ = C >='0'&& C <='9'? C'0'C'a'+Ten; return(value);}Char*php_url_encode (Char Const*s,intLenint*new_length) {Register unsignedCharC; unsignedChar*to, *start; unsignedChar Const* from, *end;  from= (unsignedChar*) s; End= (unsignedChar*) s +Len; Start= to = (unsignedChar*)calloc(1,3*len+1);  while( from<end) {C= * from++; if(c = =' ')         {            *to++ ='+'; }         Else if(C <'0'&& C! ='-'&& C! ='.') ||(c<'A'&& C >'9') ||(c>'Z'&& C <'a'&& C! ='_') ||(c>'Z') ) {to[0] ='%'; to[1] = Hexchars[c >>4]; to[2] = hexchars[c & the]; to+=3; }        Else         {            *to++ =C; }    }    *to =0; if(new_length) {*new_length = to-start; }    return(Char*) Start;intPhp_url_decode (Char*STR,intLen) {    Char*dest =str; Char*data =str;  while(len--)     {        if(*data = ='+')         {            *dest =' '; }        Else if(*data = ='%'&& Len >=2&& Isxdigit ((int) * (Data +1)) && Isxdigit ((int) * (Data +2)))         {            *dest = (Char) Php_htoi (Data +1); Data+=2; Len-=2; }         Else         {            *dest = *data; } Data++; Dest++; }    *dest =' /'; returnDest-str;}intMainintargcChar**argv) {    Char*str ="Hello, world! ",*New_str; intLen =0, New_len =0, Old_len =0; Len=strlen (str); New_str= Php_url_encode (str,len,&New_len); printf ("new string:%s,new length:%d\n", New_str,new_len); Old_len=Php_url_decode (New_str,new_len); printf ("Old string:%s,old length:%d\n", New_str,old_len); return 0;}

PS: Check a lot of information to understand the principle of urldecode, so simple, hehe

C language implementation of URL encode and URL decode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.