1
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
219
220
221
222
223
235
236
237
238
239
240
241
242
243
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
266
267
271
272
273
274
275
276
277
282
283
284
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
315
316
317
318
319
320
321
322
323
324
325
334
335
336
340
341
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
377
378
379
380
381
384
385
386
387
388
/* ... */
/* ... */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "protocol_examples_utils.h"5 includes
#define NGX_ESCAPE_URI (0)
#define NGX_ESCAPE_ARGS (1)
#define NGX_ESCAPE_URI_COMPONENT (2)
#define NGX_ESCAPE_HTML (3)
#define NGX_ESCAPE_REFRESH (4)
#define NGX_ESCAPE_MEMCACHED (5)
#define NGX_ESCAPE_MAIL_AUTH (6)
#define NGX_UNESCAPE_URI (1)
#define NGX_UNESCAPE_REDIRECT (2)9 defines
uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, unsigned int type)
{
unsigned int n;
uint32_t *escape;
static u_char hex[] = "0123456789ABCDEF";
/* ... */
static uint32_t uri[] = {
0xffffffff,
0xd000002d,
0x50000000,
0xb8000001,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}{...};
static uint32_t args[] = {
0xffffffff,
0xd800086d,
0x50000000,
0xb8000001,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}{...};
static uint32_t uri_component[] = {
0xffffffff,
0xfc009fff,
0x78000001,
0xb8000001,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}{...};
static uint32_t html[] = {
0xffffffff,
0x500000ad,
0x50000000,
0xb8000001,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}{...};
static uint32_t refresh[] = {
0xffffffff,
0x50000085,
0x50000000,
0xd8000001,
0xffffffff,
0xffffffff,
0xffffffff,
0xffffffff
}{...};
static uint32_t memcached[] = {
0xffffffff,
0x00000021,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
0x00000000,
}{...};
static uint32_t *map[] =
{ uri, args, uri_component, html, refresh, memcached, memcached };
escape = map[type];
if (dst == NULL) {
n = 0;
while (size) {
if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
n++;
}{...}
src++;
size--;
}{...}
return (uintptr_t) n;
}{...}
while (size) {
if (escape[*src >> 5] & (1U << (*src & 0x1f))) {
*dst++ = '%';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0xf];
src++;
}{...} else {
*dst++ = *src++;
}{...}
size--;
}{...}
return (uintptr_t) dst;
}{ ... }
void ngx_unescape_uri(u_char **dst, u_char **src, size_t size, unsigned int type)
{
u_char *d, *s, ch, c, decoded;
enum {
sw_usual = 0,
sw_quoted,
sw_quoted_second
}{ ... } state;
d = *dst;
s = *src;
state = 0;
decoded = 0;
while (size--) {
ch = *s++;
switch (state) {
case sw_usual:
if (ch == '?'
&& (type & (NGX_UNESCAPE_URI | NGX_UNESCAPE_REDIRECT))) {
*d++ = ch;
goto done;
}{...}
if (ch == '%') {
state = sw_quoted;
break;
}{...}
*d++ = ch;
break;
...
case sw_quoted:
if (ch >= '0' && ch <= '9') {
decoded = (u_char) (ch - '0');
state = sw_quoted_second;
break;
}{...}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
decoded = (u_char) (c - 'a' + 10);
state = sw_quoted_second;
break;
}{...}
state = sw_usual;
*d++ = ch;
break;
...
case sw_quoted_second:
state = sw_usual;
if (ch >= '0' && ch <= '9') {
ch = (u_char) ((decoded << 4) + (ch - '0'));
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}{...}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}{...}
*d++ = ch;
break;
}{...}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
ch = (u_char) ((decoded << 4) + (c - 'a') + 10);
if (type & NGX_UNESCAPE_URI) {
if (ch == '?') {
*d++ = ch;
goto done;
}{...}
*d++ = ch;
break;
}{...}
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch == '?') {
*d++ = ch;
goto done;
}{...}
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}{...}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}{...}
*d++ = ch;
break;
}{...}
break;...
}{...}
}{...}
done:
*dst = d;
*src = s;
}{ ... }
uint32_t example_uri_encode(char *dest, const char *src, size_t len)
{
if (!src || !dest) {
return 0;
}{...}
uintptr_t ret = ngx_escape_uri((unsigned char *)dest, (unsigned char *)src, len, NGX_ESCAPE_URI_COMPONENT);
return (uint32_t)(ret - (uintptr_t)dest);
}{ ... }
void example_uri_decode(char *dest, const char *src, size_t len)
{
if (!src || !dest) {
return;
}{...}
unsigned char *src_ptr = (unsigned char *)src;
unsigned char *dst_ptr = (unsigned char *)dest;
ngx_unescape_uri(&dst_ptr, &src_ptr, len, NGX_UNESCAPE_URI);
}{ ... }