-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuri_template_string.c
186 lines (161 loc) · 4.85 KB
/
uri_template_string.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
+----------------------------------------------------------------------+
| See LICENSE file for further copyright information |
+----------------------------------------------------------------------+
| Authors: Ioseb Dzmanashvili <ioseb.dzmanashvili@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_uri_template.h"
#define MAX_ONE_BYTE_CHAR 0x7f
#define MAX_TWO_BYTE_CHAR 0x7ff
#define MAX_THREE_BYTE_CHAR 0xffff
#define MAX_FOUR_BYTE_CHAR 0x1fffff
static unsigned char hexchars[] = "0123456789ABCDEF";
static unsigned char badchar[] = {239, 191, 189};
static unsigned char urlchars[3][128] = {
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, '-', '.', 0,
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 0, 0, 0, 0, 0, 0,
0, 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 0, 0, 0, 0, '_',
0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 0, 0, 0, '~', 0
}, {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, '!', 0, '#', '$', 0, '&', 0,
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', 0, '=', 0, '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', 0, ']', 0, '_',
0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 0, 0, 0, '~', 0
}, {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, '!', 0, '#', '$', 0, '&','\'',
'(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', 0, '=', 0, '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', 0, ']', 0, '_',
0, 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 0, 0, 0, '~', 0
}
};
inline static void append_encoded(smart_str *dest, const char *source, size_t num)
{
unsigned char c;
int i;
for (i = 0; i < num; i++) {
c = *source++;
smart_str_appendc(dest, '%');
smart_str_appendc(dest, hexchars[c >> 4]);
smart_str_appendc(dest, hexchars[c & 15]);
}
}
inline static void utf8_append_badchar(smart_str *dest)
{
int i;
for (i = 0; i < 3; i++) {
smart_str_appendc(dest, '%');
smart_str_appendc(dest, hexchars[badchar[i] >> 4]);
smart_str_appendc(dest, hexchars[badchar[i] & 15]);
}
}
static char *utf8_copy_char(smart_str *dest, char *source)
{
unsigned char first = *source;
unsigned char second = *(source + 1) ^ 0x80;
unsigned char third;
unsigned char fourth;
if (second & 0xC0) {
utf8_append_badchar(dest);
return ++source;
}
if (first < 0xE0) {
if (first < 0xC0) {
utf8_append_badchar(dest);
return ++source;
}
if ((((first << 6) | second)
& MAX_TWO_BYTE_CHAR) <= MAX_ONE_BYTE_CHAR) {
utf8_append_badchar(dest);
return ++source;
}
append_encoded(dest, source, 2);
return source += 2;
}
third = *(source + 2) ^ 0x80;
if (third & 0xC0) {
utf8_append_badchar(dest);
return ++source;
}
if (first < 0xF0) {
if ((((((first << 6) | second) << 6) | third)
& MAX_THREE_BYTE_CHAR) <= MAX_TWO_BYTE_CHAR) {
utf8_append_badchar(dest);
return ++source;
}
append_encoded(dest, source, 3);
return source += 3;
}
fourth = *(source + 3) ^ 0x80;
if (fourth & 0xC0) {
utf8_append_badchar(dest);
return ++source;
}
if (first < 0xF8) {
if (((((((first << 6 | second) << 6) | third) << 6) | fourth)
& MAX_FOUR_BYTE_CHAR) <= MAX_THREE_BYTE_CHAR) {
utf8_append_badchar(dest);
return ++source;
}
append_encoded(dest, source, 4);
return source += 4;
}
return source;
}
void uri_template_substr_copy(smart_str *dest, char *source, size_t num, int allowed_chars)
{
unsigned char c;
if (num <= 0) {
return;
}
while (*source && num-- > 0) {
c = *source;
if (c > 127) {
source = utf8_copy_char(dest, source);
} else {
if (urlchars[allowed_chars][c]) {
smart_str_appendc(dest, *source);
} else {
append_encoded(dest, source, 1);
}
source++;
}
}
}