0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
|
11 u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n)
|
|
12 {
|
|
13 if (n == 0) {
|
|
14 return dst;
|
|
15 }
|
|
16
|
|
17 for (/* void */; --n; dst++, src++) {
|
|
18 *dst = *src;
|
|
19
|
|
20 if (*dst == '\0') {
|
|
21 return dst;
|
|
22 }
|
|
23 }
|
|
24
|
|
25 *dst = '\0';
|
|
26
|
|
27 return dst;
|
|
28 }
|
|
29
|
|
30
|
34
|
31 u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
|
|
32 {
|
|
33 u_char *dst;
|
|
34
|
|
35 if (!(dst = ngx_palloc(pool, src->len))) {
|
|
36 return NULL;
|
|
37 }
|
|
38
|
|
39 ngx_memcpy(dst, src->data, src->len);
|
|
40
|
|
41 return dst;
|
|
42 }
|
|
43
|
|
44
|
4
|
45 /*
|
|
46 * supported formats:
|
22
|
47 * %[0][width][x][X]O off_t
|
10
|
48 * %[0][width]T time_t
|
|
49 * %[0][width][u][x|X]z ssize_t/size_t
|
|
50 * %[0][width][u][x|X]d int/u_int
|
|
51 * %[0][width][u][x|X]l long
|
|
52 * %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t
|
|
53 * %[0][width][u][x|X]D int32_t/uint32_t
|
|
54 * %[0][width][u][x|X]L int64_t/uint64_t
|
|
55 * %P ngx_pid_t
|
|
56 * %r rlim_t
|
|
57 * %p pointer
|
|
58 * %V pointer to ngx_str_t
|
|
59 * %s null-terminated string
|
|
60 * %Z '\0'
|
|
61 * %c char
|
|
62 * %% %
|
4
|
63 *
|
10
|
64 * TODO:
|
|
65 * %M ngx_msec_t
|
|
66 * %A ngx_atomic_t
|
|
67 *
|
|
68 * reserved:
|
|
69 * %t ptrdiff_t
|
|
70 * %S null-teminated wchar string
|
|
71 * %C wchar
|
4
|
72 */
|
|
73
|
10
|
74
|
|
75 u_char *ngx_sprintf(u_char *buf, const char *fmt, ...)
|
|
76 {
|
|
77 u_char *p;
|
|
78 va_list args;
|
|
79
|
|
80 va_start(args, fmt);
|
|
81 p = ngx_vsnprintf(buf, /* STUB */ 65536, fmt, args);
|
|
82 va_end(args);
|
|
83
|
|
84 return p;
|
|
85 }
|
|
86
|
|
87
|
|
88 u_char *ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
|
4
|
89 {
|
10
|
90 u_char *p;
|
|
91 va_list args;
|
|
92
|
|
93 va_start(args, fmt);
|
|
94 p = ngx_vsnprintf(buf, max, fmt, args);
|
|
95 va_end(args);
|
|
96
|
|
97 return p;
|
|
98 }
|
|
99
|
4
|
100
|
10
|
101 u_char *ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
|
|
102 {
|
28
|
103 u_char *p, zero, *last, temp[NGX_INT64_LEN + 1];
|
|
104 /*
|
|
105 * really we need temp[NGX_INT64_LEN] only,
|
|
106 * but icc shows the warning
|
|
107 */
|
10
|
108 int d;
|
|
109 size_t len;
|
|
110 uint32_t ui32;
|
|
111 int64_t i64;
|
|
112 uint64_t ui64;
|
|
113 ngx_str_t *s;
|
|
114 ngx_uint_t width, sign, hexadecimal;
|
|
115 static u_char hex[] = "0123456789abcdef";
|
|
116 static u_char HEX[] = "0123456789ABCDEF";
|
4
|
117
|
10
|
118 if (max == 0) {
|
|
119 return buf;
|
|
120 }
|
|
121
|
|
122 last = buf + max;
|
|
123
|
|
124 while (*fmt && buf < last) {
|
|
125
|
|
126 /*
|
|
127 * "buf < last" means that we could copy at least one character:
|
|
128 * the plain character, "%%", "%c", and minus without the checking
|
|
129 */
|
|
130
|
4
|
131 if (*fmt == '%') {
|
|
132
|
10
|
133 i64 = 0;
|
|
134 ui64 = 0;
|
|
135
|
|
136 zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
|
4
|
137 width = 0;
|
|
138 sign = 1;
|
|
139 hexadecimal = 0;
|
|
140
|
26
|
141 p = temp + NGX_INT64_LEN;
|
4
|
142
|
|
143 while (*fmt >= '0' && *fmt <= '9') {
|
|
144 width = width * 10 + *fmt++ - '0';
|
|
145 }
|
|
146
|
|
147
|
|
148 for ( ;; ) {
|
|
149 switch (*fmt) {
|
|
150
|
|
151 case 'u':
|
|
152 sign = 0;
|
|
153 fmt++;
|
|
154 continue;
|
|
155
|
16
|
156 case 'm':
|
|
157 width = NGX_INT_T_LEN;
|
|
158 fmt++;
|
|
159 continue;
|
|
160
|
10
|
161 case 'X':
|
|
162 hexadecimal = 2;
|
|
163 sign = 0;
|
|
164 fmt++;
|
|
165 continue;
|
|
166
|
4
|
167 case 'x':
|
|
168 hexadecimal = 1;
|
10
|
169 sign = 0;
|
4
|
170 fmt++;
|
|
171 continue;
|
|
172
|
|
173 default:
|
|
174 break;
|
|
175 }
|
|
176
|
|
177 break;
|
|
178 }
|
|
179
|
|
180
|
|
181 switch (*fmt) {
|
|
182
|
10
|
183 case 'V':
|
|
184 s = va_arg(args, ngx_str_t *);
|
4
|
185
|
10
|
186 len = (buf + s->len < last) ? s->len : (size_t) (last - buf);
|
|
187 buf = ngx_cpymem(buf, s->data, len);
|
|
188 fmt++;
|
4
|
189
|
10
|
190 continue;
|
4
|
191
|
|
192 case 's':
|
10
|
193 p = va_arg(args, u_char *);
|
4
|
194
|
10
|
195 while (*p && buf < last) {
|
4
|
196 *buf++ = *p++;
|
|
197 }
|
|
198 fmt++;
|
|
199
|
|
200 continue;
|
|
201
|
10
|
202 case 'O':
|
|
203 i64 = (int64_t) va_arg(args, off_t);
|
|
204 sign = 1;
|
|
205 break;
|
|
206
|
|
207 case 'P':
|
|
208 i64 = (int64_t) va_arg(args, ngx_pid_t);
|
|
209 sign = 1;
|
|
210 break;
|
|
211
|
|
212 case 'T':
|
|
213 i64 = (int64_t) va_arg(args, time_t);
|
|
214 sign = 1;
|
|
215 break;
|
|
216
|
|
217 case 'z':
|
|
218 if (sign) {
|
|
219 i64 = (int64_t) va_arg(args, ssize_t);
|
|
220 } else {
|
|
221 ui64 = (uint64_t) va_arg(args, size_t);
|
|
222 }
|
|
223 break;
|
|
224
|
|
225 case 'i':
|
|
226 if (sign) {
|
|
227 i64 = (int64_t) va_arg(args, ngx_int_t);
|
|
228 } else {
|
|
229 ui64 = (uint64_t) va_arg(args, ngx_uint_t);
|
|
230 }
|
|
231 break;
|
|
232
|
|
233 case 'd':
|
|
234 if (sign) {
|
|
235 i64 = (int64_t) va_arg(args, int);
|
|
236 } else {
|
|
237 ui64 = (uint64_t) va_arg(args, u_int);
|
|
238 }
|
|
239 break;
|
|
240
|
|
241 case 'l':
|
|
242 if (sign) {
|
|
243 i64 = (int64_t) va_arg(args, long);
|
|
244 } else {
|
|
245 ui64 = (uint64_t) va_arg(args, u_long);
|
|
246 }
|
|
247 break;
|
|
248
|
|
249 case 'D':
|
|
250 if (sign) {
|
|
251 i64 = (int64_t) va_arg(args, int32_t);
|
|
252 } else {
|
|
253 ui64 = (uint64_t) va_arg(args, uint32_t);
|
|
254 }
|
|
255 break;
|
|
256
|
|
257 case 'L':
|
|
258 if (sign) {
|
|
259 i64 = va_arg(args, int64_t);
|
|
260 } else {
|
|
261 ui64 = va_arg(args, uint64_t);
|
|
262 }
|
|
263 break;
|
|
264
|
|
265 #if !(NGX_WIN32)
|
|
266 case 'r':
|
|
267 i64 = (int64_t) va_arg(args, rlim_t);
|
|
268 sign = 1;
|
|
269 break;
|
|
270 #endif
|
|
271
|
|
272 case 'p':
|
|
273 ui64 = (uintptr_t) va_arg(args, void *);
|
|
274 hexadecimal = 2;
|
|
275 sign = 0;
|
|
276 zero = '0';
|
|
277 width = 8;
|
|
278 break;
|
|
279
|
6
|
280 case 'c':
|
10
|
281 d = va_arg(args, int);
|
6
|
282 *buf++ = (u_char) (d & 0xff);
|
|
283 fmt++;
|
|
284
|
|
285 continue;
|
|
286
|
10
|
287 case 'Z':
|
|
288 *buf++ = '\0';
|
|
289 fmt++;
|
|
290
|
|
291 continue;
|
|
292
|
4
|
293 case '%':
|
|
294 *buf++ = '%';
|
|
295 fmt++;
|
|
296
|
|
297 continue;
|
|
298
|
|
299 default:
|
|
300 *buf++ = *fmt++;
|
|
301
|
|
302 continue;
|
|
303 }
|
|
304
|
10
|
305 if (sign) {
|
|
306 if (i64 < 0) {
|
|
307 *buf++ = '-';
|
|
308 ui64 = (uint64_t) -i64;
|
|
309
|
|
310 } else {
|
|
311 ui64 = (uint64_t) i64;
|
|
312 }
|
|
313 }
|
|
314
|
|
315 if (hexadecimal == 1) {
|
|
316 do {
|
|
317
|
|
318 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
319 *--p = hex[(uint32_t) (ui64 & 0xf)];
|
|
320
|
|
321 } while (ui64 >>= 4);
|
|
322
|
|
323 } else if (hexadecimal == 2) {
|
|
324 do {
|
|
325
|
|
326 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
327 *--p = HEX[(uint32_t) (ui64 & 0xf)];
|
|
328
|
|
329 } while (ui64 >>= 4);
|
|
330
|
|
331 } else if (ui64 <= NGX_MAX_UINT32_VALUE) {
|
|
332
|
|
333 /*
|
|
334 * To divide 64-bit number and to find the remainder
|
|
335 * on the x86 platform gcc and icc call the libc functions
|
|
336 * [u]divdi3() and [u]moddi3(), they call another function
|
|
337 * in return. On FreeBSD it is the qdivrem() function,
|
|
338 * its source code is about 170 lines of the code.
|
|
339 * The glibc counterpart is about 150 lines of the code.
|
|
340 *
|
|
341 * For 32-bit numbers gcc and icc use the inlined
|
|
342 * multiplication and shifts. For example, unsigned
|
|
343 * "i32 / 10" is compiled to "(i32 * 0xCCCCCCCD) >> 35".
|
|
344 */
|
|
345
|
|
346 ui32 = (uint32_t) ui64;
|
|
347
|
|
348 do {
|
|
349 *--p = (u_char) (ui32 % 10 + '0');
|
|
350 } while (ui32 /= 10);
|
|
351
|
|
352 } else {
|
|
353 do {
|
|
354 *--p = (u_char) (ui64 % 10 + '0');
|
|
355 } while (ui64 /= 10);
|
|
356 }
|
|
357
|
26
|
358 len = (temp + NGX_INT64_LEN) - p;
|
4
|
359
|
10
|
360 while (len++ < width && buf < last) {
|
|
361 *buf++ = zero;
|
4
|
362 }
|
|
363
|
26
|
364 len = (temp + NGX_INT64_LEN) - p;
|
10
|
365 if (buf + len > last) {
|
|
366 len = last - buf;
|
|
367 }
|
|
368
|
|
369 buf = ngx_cpymem(buf, p, len);
|
4
|
370
|
|
371 fmt++;
|
|
372
|
|
373 } else {
|
|
374 *buf++ = *fmt++;
|
|
375 }
|
|
376 }
|
|
377
|
|
378 return buf;
|
|
379 }
|
|
380
|
|
381
|
0
|
382 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
|
|
383 {
|
|
384 if (n == 0) {
|
|
385 return 0;
|
|
386 }
|
|
387
|
|
388 n--;
|
|
389
|
|
390 for ( ;; ) {
|
|
391 if (s1[n] != s2[n]) {
|
|
392 return s1[n] - s2[n];
|
|
393 }
|
|
394
|
|
395 if (n == 0) {
|
|
396 return 0;
|
|
397 }
|
|
398
|
|
399 n--;
|
|
400 }
|
|
401 }
|
|
402
|
|
403
|
4
|
404 ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
|
|
405 {
|
|
406 u_char c1, c2;
|
|
407
|
|
408 if (n == 0) {
|
|
409 return 0;
|
|
410 }
|
|
411
|
|
412 n--;
|
|
413
|
|
414 for ( ;; ) {
|
|
415 c1 = s1[n];
|
|
416 if (c1 >= 'a' && c1 <= 'z') {
|
|
417 c1 -= 'a' - 'A';
|
|
418 }
|
|
419
|
|
420 c2 = s2[n];
|
|
421 if (c2 >= 'a' && c2 <= 'z') {
|
|
422 c2 -= 'a' - 'A';
|
|
423 }
|
|
424
|
|
425 if (c1 != c2) {
|
|
426 return c1 - c2;
|
|
427 }
|
|
428
|
|
429 if (n == 0) {
|
|
430 return 0;
|
|
431 }
|
|
432
|
|
433 n--;
|
|
434 }
|
|
435 }
|
|
436
|
|
437
|
0
|
438 ngx_int_t ngx_atoi(u_char *line, size_t n)
|
|
439 {
|
|
440 ngx_int_t value;
|
|
441
|
|
442 if (n == 0) {
|
|
443 return NGX_ERROR;
|
|
444 }
|
|
445
|
|
446 for (value = 0; n--; line++) {
|
|
447 if (*line < '0' || *line > '9') {
|
|
448 return NGX_ERROR;
|
|
449 }
|
|
450
|
|
451 value = value * 10 + (*line - '0');
|
|
452 }
|
|
453
|
|
454 if (value < 0) {
|
|
455 return NGX_ERROR;
|
|
456
|
|
457 } else {
|
|
458 return value;
|
|
459 }
|
|
460 }
|
|
461
|
|
462
|
|
463 ngx_int_t ngx_hextoi(u_char *line, size_t n)
|
|
464 {
|
|
465 u_char ch;
|
|
466 ngx_int_t value;
|
|
467
|
|
468 if (n == 0) {
|
|
469 return NGX_ERROR;
|
|
470 }
|
|
471
|
|
472 for (value = 0; n--; line++) {
|
|
473 ch = *line;
|
|
474
|
|
475 if (ch >= '0' && ch <= '9') {
|
|
476 value = value * 16 + (ch - '0');
|
|
477 continue;
|
|
478 }
|
|
479
|
|
480 if (ch >= 'A' && ch <= 'F') {
|
|
481 value = value * 16 + (ch - 'A' + 10);
|
|
482 continue;
|
|
483 }
|
|
484
|
|
485 if (ch >= 'a' && ch <= 'f') {
|
|
486 value = value * 16 + (ch - 'a' + 10);
|
|
487 continue;
|
|
488 }
|
|
489
|
|
490 return NGX_ERROR;
|
|
491 }
|
|
492
|
|
493 if (value < 0) {
|
|
494 return NGX_ERROR;
|
|
495
|
|
496 } else {
|
|
497 return value;
|
|
498 }
|
|
499 }
|
|
500
|
|
501
|
|
502 void ngx_md5_text(u_char *text, u_char *md5)
|
|
503 {
|
|
504 int i;
|
|
505 static u_char hex[] = "0123456789abcdef";
|
|
506
|
|
507 for (i = 0; i < 16; i++) {
|
|
508 *text++ = hex[md5[i] >> 4];
|
|
509 *text++ = hex[md5[i] & 0xf];
|
|
510 }
|
|
511
|
|
512 *text = '\0';
|
|
513 }
|
|
514
|
|
515
|
2
|
516 void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
517 {
|
|
518 u_char *d, *s;
|
|
519 size_t len;
|
|
520 static u_char basis64[] =
|
|
521 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
522
|
|
523 len = src->len;
|
|
524 s = src->data;
|
|
525 d = dst->data;
|
|
526
|
|
527 while (len > 2) {
|
|
528 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
529 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
530 *d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
|
|
531 *d++ = basis64[s[2] & 0x3f];
|
|
532
|
|
533 s += 3;
|
|
534 len -= 3;
|
|
535 }
|
|
536
|
|
537 if (len) {
|
|
538 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
539
|
|
540 if (len == 1) {
|
|
541 *d++ = basis64[(s[0] & 3) << 4];
|
|
542 *d++ = '=';
|
|
543
|
|
544 } else {
|
|
545 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
546 *d++ = basis64[(s[1] & 0x0f) << 2];
|
|
547 }
|
|
548
|
|
549 *d++ = '=';
|
|
550 }
|
|
551
|
|
552 dst->len = d - dst->data;
|
|
553 }
|
|
554
|
|
555
|
2
|
556 ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
557 {
|
|
558 size_t len;
|
|
559 u_char *d, *s;
|
|
560 static u_char basis64[] =
|
|
561 { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
562 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
563 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
|
|
564 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
565 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
566 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
|
|
567 77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
568 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
|
569
|
|
570 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
571 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
572 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
573 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
574 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
575 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
576 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
577 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };
|
|
578
|
|
579 for (len = 0; len < src->len; len++) {
|
|
580 if (src->data[len] == '=') {
|
|
581 break;
|
|
582 }
|
|
583
|
|
584 if (basis64[src->data[len]] == 77) {
|
|
585 return NGX_ERROR;
|
|
586 }
|
|
587 }
|
|
588
|
|
589 if (len % 4 == 1) {
|
|
590 return NGX_ERROR;
|
|
591 }
|
|
592
|
|
593 s = src->data;
|
|
594 d = dst->data;
|
|
595
|
|
596 while (len > 3) {
|
|
597 *d++ = (u_char) (basis64[s[0]] << 2 | basis64[s[1]] >> 4);
|
|
598 *d++ = (u_char) (basis64[s[1]] << 4 | basis64[s[2]] >> 2);
|
|
599 *d++ = (u_char) (basis64[s[2]] << 6 | basis64[s[3]]);
|
|
600
|
|
601 s += 4;
|
|
602 len -= 4;
|
|
603 }
|
|
604
|
|
605 if (len > 1) {
|
|
606 *d++ = (u_char) (basis64[s[0]] << 2 | basis64[s[1]] >> 4);
|
|
607 }
|
|
608
|
|
609 if (len > 2) {
|
|
610 *d++ = (u_char) (basis64[s[1]] << 4 | basis64[s[2]] >> 2);
|
|
611 }
|
|
612
|
|
613 dst->len = d - dst->data;
|
|
614
|
|
615 return NGX_OK;
|
|
616 }
|
|
617
|
|
618
|
34
|
619 uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
|
0
|
620 {
|
10
|
621 ngx_uint_t i, n;
|
|
622 uint32_t *escape;
|
2
|
623 static u_char hex[] = "0123456789abcdef";
|
10
|
624
|
34
|
625 /* " ", "%", "?", %00-%1F, %7F-%FF */
|
|
626
|
10
|
627 static uint32_t uri[] =
|
2
|
628 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
0
|
629
|
2
|
630 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
631 0x80000021, /* 1000 0000 0000 0000 0000 0000 0010 0001 */
|
|
632
|
|
633 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
634 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
635
|
|
636 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
637 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
0
|
638
|
2
|
639 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
640 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
641 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
642 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ };
|
|
643
|
34
|
644 /* " ", "%", "+", "?", %00-%1F, %7F-%FF */
|
|
645
|
|
646 static uint32_t args[] =
|
|
647 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
648
|
|
649 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
650 0x80000821, /* 1000 0000 0000 0000 0000 1000 0010 0001 */
|
|
651
|
|
652 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
653 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
654
|
|
655 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
656 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
|
657
|
|
658 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
659 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
660 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
661 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ };
|
|
662
|
|
663 /* " ", """, "%", "'", %00-%1F, %7F-%FF */
|
|
664
|
10
|
665 static uint32_t html[] =
|
|
666 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
667
|
|
668 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
669 0x80000021, /* 0000 0000 0000 0000 0000 0000 1010 0101 */
|
|
670
|
|
671 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
672 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
673
|
|
674 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
675 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
|
676
|
|
677 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
678 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
679 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
680 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ };
|
|
681
|
|
682
|
34
|
683 switch (type) {
|
|
684 case NGX_ESCAPE_HTML:
|
10
|
685 escape = html;
|
34
|
686 break;
|
|
687 case NGX_ESCAPE_ARGS:
|
|
688 escape = args;
|
|
689 break;
|
|
690 default:
|
10
|
691 escape = uri;
|
34
|
692 break;
|
10
|
693 }
|
|
694
|
2
|
695 if (dst == NULL) {
|
|
696
|
|
697 /* find the number of the characters to be escaped */
|
0
|
698
|
2
|
699 n = 0;
|
|
700
|
|
701 for (i = 0; i < size; i++) {
|
|
702 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
703 n++;
|
|
704 }
|
|
705 src++;
|
|
706 }
|
|
707
|
34
|
708 return (uintptr_t) n;
|
0
|
709 }
|
|
710
|
2
|
711 for (i = 0; i < size; i++) {
|
|
712 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
713 *dst++ = '%';
|
|
714 *dst++ = hex[*src >> 4];
|
|
715 *dst++ = hex[*src & 0xf];
|
|
716 src++;
|
0
|
717
|
2
|
718 } else {
|
|
719 *dst++ = *src++;
|
|
720 }
|
0
|
721 }
|
|
722
|
34
|
723 return (uintptr_t) dst;
|
0
|
724 }
|