4 Star 6 Fork 3

waynemz/xg_push

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
url_encoder.c 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
waynemz 提交于 2016-12-05 14:05 . Project first init.
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include "url_encode.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 *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 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;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/dcdcmin/xg_push.git
git@gitee.com:dcdcmin/xg_push.git
dcdcmin
xg_push
xg_push
master

搜索帮助