-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathurl.c
94 lines (77 loc) · 1.81 KB
/
url.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*******************************************************************
* url编码和解码,解决url中包含中文的问题.
* 参考博客:http://blog.csdn.net/langeldep/article/details/6264058
* 本人做了少量的修改.
*******************************************************************/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include "url.h"
static unsigned char hexchars[] = "0123456789ABCDEF";
static
int 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)htoi(data + 1);
data += 2;
len -= 2;
}
else {
*dest = *data;
}
data++;
dest++;
}
*dest = '\0';
return dest - str;
}