URL encode 与 URL decode 的C语言实现

 本文代码为从PHP代码中修改而来,只保留了2个函数。

 

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

 

URL编码做了如下操作:

字符"a"-"z""A"-"Z""0"-"9"".""-""*",和"_" 都不被编码,维持原值;

空格" "被转换为加号"+"

其他每个字节都被表示成"%xy"格式的由3个字符组成的字符串,编码为UTF-8

 

头文件如下

 

#ifndef URL_H #define URL_H #ifdef __cplusplus extern "C" { #endif int php_url_decode(char *str, int len); char *php_url_encode(char const *s, int len, int *new_length); #ifdef __cplusplus } #endif #endif /* URL_H */

 

 

C文件如下:

 

#include #include #include #include #include "url.h" static unsigned char hexchars[] = "0123456789ABCDEF"; static int php_htoi(char *s) { int value; int c; c = ((unsigned char *)s)[0]; if (isupper(c)) c = tolower(c); value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16; c = ((unsigned char *)s)[1]; if (isupper(c)) c = tolower(c); value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10; return (value); } char *php_url_encode(char const *s, int len, int *new_length) { register unsigned char c; unsigned char *to, *start; unsigned char const *from, *end; from = (unsigned char *)s; end = (unsigned char *)s + len; start = to = (unsigned char *) 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 & 15]; to += 3; } else { *to++ = c; } } *to = 0; if (new_length) { *new_length = to - start; } return (char *) start; } int php_url_decode(char *str, int len) { 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 = '/0'; return dest - str; }

 

你可能感兴趣的:(URL encode 与 URL decode 的C语言实现)