0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
644
|
4 * Copyright (C) Nginx, Inc.
|
0
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10
|
|
11
|
416
|
12 static u_char *ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64,
|
|
13 u_char zero, ngx_uint_t hexadecimal, ngx_uint_t width);
|
596
|
14 static ngx_int_t ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src,
|
|
15 const u_char *basis);
|
416
|
16
|
|
17
|
392
|
18 void
|
|
19 ngx_strlow(u_char *dst, u_char *src, size_t n)
|
|
20 {
|
540
|
21 while (n) {
|
392
|
22 *dst = ngx_tolower(*src);
|
|
23 dst++;
|
|
24 src++;
|
540
|
25 n--;
|
392
|
26 }
|
|
27 }
|
|
28
|
|
29
|
42
|
30 u_char *
|
|
31 ngx_cpystrn(u_char *dst, u_char *src, size_t n)
|
0
|
32 {
|
|
33 if (n == 0) {
|
|
34 return dst;
|
|
35 }
|
|
36
|
498
|
37 while (--n) {
|
0
|
38 *dst = *src;
|
|
39
|
|
40 if (*dst == '\0') {
|
|
41 return dst;
|
|
42 }
|
498
|
43
|
|
44 dst++;
|
|
45 src++;
|
0
|
46 }
|
|
47
|
|
48 *dst = '\0';
|
|
49
|
|
50 return dst;
|
|
51 }
|
|
52
|
|
53
|
42
|
54 u_char *
|
|
55 ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
|
34
|
56 {
|
|
57 u_char *dst;
|
|
58
|
382
|
59 dst = ngx_pnalloc(pool, src->len);
|
50
|
60 if (dst == NULL) {
|
34
|
61 return NULL;
|
|
62 }
|
|
63
|
|
64 ngx_memcpy(dst, src->data, src->len);
|
|
65
|
|
66 return dst;
|
|
67 }
|
|
68
|
|
69
|
4
|
70 /*
|
|
71 * supported formats:
|
22
|
72 * %[0][width][x][X]O off_t
|
10
|
73 * %[0][width]T time_t
|
|
74 * %[0][width][u][x|X]z ssize_t/size_t
|
|
75 * %[0][width][u][x|X]d int/u_int
|
|
76 * %[0][width][u][x|X]l long
|
|
77 * %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t
|
|
78 * %[0][width][u][x|X]D int32_t/uint32_t
|
|
79 * %[0][width][u][x|X]L int64_t/uint64_t
|
44
|
80 * %[0][width|m][u][x|X]A ngx_atomic_int_t/ngx_atomic_uint_t
|
570
|
81 * %[0][width][.width]f double, max valid number fits to %18.15f
|
10
|
82 * %P ngx_pid_t
|
106
|
83 * %M ngx_msec_t
|
10
|
84 * %r rlim_t
|
328
|
85 * %p void *
|
|
86 * %V ngx_str_t *
|
|
87 * %v ngx_variable_value_t *
|
10
|
88 * %s null-terminated string
|
358
|
89 * %*s length and string
|
10
|
90 * %Z '\0'
|
74
|
91 * %N '\n'
|
10
|
92 * %c char
|
|
93 * %% %
|
4
|
94 *
|
10
|
95 * reserved:
|
|
96 * %t ptrdiff_t
|
518
|
97 * %S null-terminated wchar string
|
10
|
98 * %C wchar
|
4
|
99 */
|
|
100
|
10
|
101
|
52
|
102 u_char * ngx_cdecl
|
42
|
103 ngx_sprintf(u_char *buf, const char *fmt, ...)
|
10
|
104 {
|
|
105 u_char *p;
|
|
106 va_list args;
|
|
107
|
|
108 va_start(args, fmt);
|
484
|
109 p = ngx_vslprintf(buf, (void *) -1, fmt, args);
|
10
|
110 va_end(args);
|
|
111
|
|
112 return p;
|
|
113 }
|
|
114
|
|
115
|
52
|
116 u_char * ngx_cdecl
|
42
|
117 ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
|
4
|
118 {
|
10
|
119 u_char *p;
|
|
120 va_list args;
|
|
121
|
|
122 va_start(args, fmt);
|
484
|
123 p = ngx_vslprintf(buf, buf + max, fmt, args);
|
|
124 va_end(args);
|
|
125
|
|
126 return p;
|
|
127 }
|
|
128
|
|
129
|
|
130 u_char * ngx_cdecl
|
|
131 ngx_slprintf(u_char *buf, u_char *last, const char *fmt, ...)
|
|
132 {
|
|
133 u_char *p;
|
|
134 va_list args;
|
|
135
|
|
136 va_start(args, fmt);
|
|
137 p = ngx_vslprintf(buf, last, fmt, args);
|
10
|
138 va_end(args);
|
|
139
|
|
140 return p;
|
|
141 }
|
|
142
|
4
|
143
|
42
|
144 u_char *
|
484
|
145 ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args)
|
10
|
146 {
|
484
|
147 u_char *p, zero;
|
320
|
148 int d;
|
570
|
149 double f, scale;
|
358
|
150 size_t len, slen;
|
320
|
151 int64_t i64;
|
|
152 uint64_t ui64;
|
|
153 ngx_msec_t ms;
|
540
|
154 ngx_uint_t width, sign, hex, max_width, frac_width, n;
|
328
|
155 ngx_str_t *v;
|
|
156 ngx_variable_value_t *vv;
|
4
|
157
|
10
|
158 while (*fmt && buf < last) {
|
|
159
|
|
160 /*
|
|
161 * "buf < last" means that we could copy at least one character:
|
|
162 * the plain character, "%%", "%c", and minus without the checking
|
|
163 */
|
|
164
|
4
|
165 if (*fmt == '%') {
|
|
166
|
10
|
167 i64 = 0;
|
|
168 ui64 = 0;
|
|
169
|
|
170 zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
|
4
|
171 width = 0;
|
|
172 sign = 1;
|
416
|
173 hex = 0;
|
42
|
174 max_width = 0;
|
416
|
175 frac_width = 0;
|
364
|
176 slen = (size_t) -1;
|
4
|
177
|
|
178 while (*fmt >= '0' && *fmt <= '9') {
|
|
179 width = width * 10 + *fmt++ - '0';
|
|
180 }
|
|
181
|
|
182
|
|
183 for ( ;; ) {
|
|
184 switch (*fmt) {
|
|
185
|
|
186 case 'u':
|
|
187 sign = 0;
|
|
188 fmt++;
|
|
189 continue;
|
|
190
|
16
|
191 case 'm':
|
42
|
192 max_width = 1;
|
16
|
193 fmt++;
|
|
194 continue;
|
|
195
|
10
|
196 case 'X':
|
416
|
197 hex = 2;
|
10
|
198 sign = 0;
|
|
199 fmt++;
|
|
200 continue;
|
|
201
|
4
|
202 case 'x':
|
416
|
203 hex = 1;
|
10
|
204 sign = 0;
|
4
|
205 fmt++;
|
|
206 continue;
|
|
207
|
416
|
208 case '.':
|
|
209 fmt++;
|
|
210
|
|
211 while (*fmt >= '0' && *fmt <= '9') {
|
|
212 frac_width = frac_width * 10 + *fmt++ - '0';
|
|
213 }
|
|
214
|
|
215 break;
|
|
216
|
358
|
217 case '*':
|
364
|
218 slen = va_arg(args, size_t);
|
358
|
219 fmt++;
|
|
220 continue;
|
|
221
|
4
|
222 default:
|
|
223 break;
|
|
224 }
|
|
225
|
|
226 break;
|
|
227 }
|
|
228
|
|
229
|
|
230 switch (*fmt) {
|
|
231
|
10
|
232 case 'V':
|
328
|
233 v = va_arg(args, ngx_str_t *);
|
4
|
234
|
570
|
235 len = ngx_min(((size_t) (last - buf)), v->len);
|
320
|
236 buf = ngx_cpymem(buf, v->data, len);
|
10
|
237 fmt++;
|
4
|
238
|
10
|
239 continue;
|
4
|
240
|
328
|
241 case 'v':
|
|
242 vv = va_arg(args, ngx_variable_value_t *);
|
|
243
|
570
|
244 len = ngx_min(((size_t) (last - buf)), vv->len);
|
328
|
245 buf = ngx_cpymem(buf, vv->data, len);
|
|
246 fmt++;
|
|
247
|
|
248 continue;
|
|
249
|
4
|
250 case 's':
|
10
|
251 p = va_arg(args, u_char *);
|
4
|
252
|
364
|
253 if (slen == (size_t) -1) {
|
358
|
254 while (*p && buf < last) {
|
|
255 *buf++ = *p++;
|
|
256 }
|
|
257
|
|
258 } else {
|
570
|
259 len = ngx_min(((size_t) (last - buf)), slen);
|
364
|
260 buf = ngx_cpymem(buf, p, len);
|
4
|
261 }
|
358
|
262
|
4
|
263 fmt++;
|
|
264
|
|
265 continue;
|
|
266
|
10
|
267 case 'O':
|
|
268 i64 = (int64_t) va_arg(args, off_t);
|
|
269 sign = 1;
|
|
270 break;
|
|
271
|
|
272 case 'P':
|
|
273 i64 = (int64_t) va_arg(args, ngx_pid_t);
|
|
274 sign = 1;
|
|
275 break;
|
|
276
|
|
277 case 'T':
|
|
278 i64 = (int64_t) va_arg(args, time_t);
|
|
279 sign = 1;
|
|
280 break;
|
|
281
|
106
|
282 case 'M':
|
110
|
283 ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
|
|
284 if ((ngx_msec_int_t) ms == -1) {
|
|
285 sign = 1;
|
|
286 i64 = -1;
|
|
287 } else {
|
|
288 sign = 0;
|
|
289 ui64 = (uint64_t) ms;
|
|
290 }
|
106
|
291 break;
|
|
292
|
10
|
293 case 'z':
|
|
294 if (sign) {
|
|
295 i64 = (int64_t) va_arg(args, ssize_t);
|
|
296 } else {
|
|
297 ui64 = (uint64_t) va_arg(args, size_t);
|
|
298 }
|
|
299 break;
|
|
300
|
|
301 case 'i':
|
|
302 if (sign) {
|
|
303 i64 = (int64_t) va_arg(args, ngx_int_t);
|
|
304 } else {
|
|
305 ui64 = (uint64_t) va_arg(args, ngx_uint_t);
|
|
306 }
|
42
|
307
|
|
308 if (max_width) {
|
|
309 width = NGX_INT_T_LEN;
|
|
310 }
|
|
311
|
10
|
312 break;
|
|
313
|
|
314 case 'd':
|
|
315 if (sign) {
|
|
316 i64 = (int64_t) va_arg(args, int);
|
|
317 } else {
|
|
318 ui64 = (uint64_t) va_arg(args, u_int);
|
|
319 }
|
|
320 break;
|
|
321
|
|
322 case 'l':
|
|
323 if (sign) {
|
|
324 i64 = (int64_t) va_arg(args, long);
|
|
325 } else {
|
|
326 ui64 = (uint64_t) va_arg(args, u_long);
|
|
327 }
|
|
328 break;
|
|
329
|
|
330 case 'D':
|
|
331 if (sign) {
|
|
332 i64 = (int64_t) va_arg(args, int32_t);
|
|
333 } else {
|
|
334 ui64 = (uint64_t) va_arg(args, uint32_t);
|
|
335 }
|
|
336 break;
|
|
337
|
|
338 case 'L':
|
|
339 if (sign) {
|
|
340 i64 = va_arg(args, int64_t);
|
|
341 } else {
|
|
342 ui64 = va_arg(args, uint64_t);
|
|
343 }
|
|
344 break;
|
|
345
|
42
|
346 case 'A':
|
|
347 if (sign) {
|
|
348 i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
|
|
349 } else {
|
44
|
350 ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
|
42
|
351 }
|
|
352
|
|
353 if (max_width) {
|
|
354 width = NGX_ATOMIC_T_LEN;
|
|
355 }
|
|
356
|
|
357 break;
|
|
358
|
416
|
359 case 'f':
|
570
|
360 f = va_arg(args, double);
|
416
|
361
|
|
362 if (f < 0) {
|
|
363 *buf++ = '-';
|
|
364 f = -f;
|
|
365 }
|
|
366
|
|
367 ui64 = (int64_t) f;
|
|
368
|
|
369 buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
|
|
370
|
|
371 if (frac_width) {
|
|
372
|
|
373 if (buf < last) {
|
|
374 *buf++ = '.';
|
|
375 }
|
|
376
|
|
377 scale = 1.0;
|
|
378
|
540
|
379 for (n = frac_width; n; n--) {
|
416
|
380 scale *= 10.0;
|
|
381 }
|
|
382
|
|
383 /*
|
|
384 * (int64_t) cast is required for msvc6:
|
638
|
385 * it cannot convert uint64_t to double
|
416
|
386 */
|
570
|
387 ui64 = (uint64_t) ((f - (int64_t) ui64) * scale + 0.5);
|
416
|
388
|
|
389 buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
|
|
390 }
|
|
391
|
|
392 fmt++;
|
|
393
|
|
394 continue;
|
|
395
|
10
|
396 #if !(NGX_WIN32)
|
|
397 case 'r':
|
|
398 i64 = (int64_t) va_arg(args, rlim_t);
|
|
399 sign = 1;
|
|
400 break;
|
|
401 #endif
|
|
402
|
|
403 case 'p':
|
|
404 ui64 = (uintptr_t) va_arg(args, void *);
|
416
|
405 hex = 2;
|
10
|
406 sign = 0;
|
|
407 zero = '0';
|
162
|
408 width = NGX_PTR_SIZE * 2;
|
10
|
409 break;
|
|
410
|
6
|
411 case 'c':
|
10
|
412 d = va_arg(args, int);
|
6
|
413 *buf++ = (u_char) (d & 0xff);
|
|
414 fmt++;
|
|
415
|
|
416 continue;
|
|
417
|
10
|
418 case 'Z':
|
|
419 *buf++ = '\0';
|
|
420 fmt++;
|
|
421
|
|
422 continue;
|
|
423
|
74
|
424 case 'N':
|
|
425 #if (NGX_WIN32)
|
|
426 *buf++ = CR;
|
|
427 #endif
|
|
428 *buf++ = LF;
|
|
429 fmt++;
|
|
430
|
|
431 continue;
|
|
432
|
4
|
433 case '%':
|
|
434 *buf++ = '%';
|
|
435 fmt++;
|
|
436
|
|
437 continue;
|
|
438
|
|
439 default:
|
|
440 *buf++ = *fmt++;
|
|
441
|
|
442 continue;
|
|
443 }
|
|
444
|
10
|
445 if (sign) {
|
|
446 if (i64 < 0) {
|
|
447 *buf++ = '-';
|
|
448 ui64 = (uint64_t) -i64;
|
|
449
|
|
450 } else {
|
|
451 ui64 = (uint64_t) i64;
|
|
452 }
|
|
453 }
|
|
454
|
416
|
455 buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);
|
4
|
456
|
|
457 fmt++;
|
|
458
|
|
459 } else {
|
|
460 *buf++ = *fmt++;
|
|
461 }
|
|
462 }
|
|
463
|
|
464 return buf;
|
|
465 }
|
|
466
|
|
467
|
416
|
468 static u_char *
|
|
469 ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64, u_char zero,
|
|
470 ngx_uint_t hexadecimal, ngx_uint_t width)
|
|
471 {
|
|
472 u_char *p, temp[NGX_INT64_LEN + 1];
|
|
473 /*
|
|
474 * we need temp[NGX_INT64_LEN] only,
|
|
475 * but icc issues the warning
|
|
476 */
|
|
477 size_t len;
|
|
478 uint32_t ui32;
|
|
479 static u_char hex[] = "0123456789abcdef";
|
|
480 static u_char HEX[] = "0123456789ABCDEF";
|
|
481
|
|
482 p = temp + NGX_INT64_LEN;
|
|
483
|
|
484 if (hexadecimal == 0) {
|
|
485
|
|
486 if (ui64 <= NGX_MAX_UINT32_VALUE) {
|
|
487
|
|
488 /*
|
|
489 * To divide 64-bit numbers and to find remainders
|
|
490 * on the x86 platform gcc and icc call the libc functions
|
|
491 * [u]divdi3() and [u]moddi3(), they call another function
|
|
492 * in its turn. On FreeBSD it is the qdivrem() function,
|
|
493 * its source code is about 170 lines of the code.
|
|
494 * The glibc counterpart is about 150 lines of the code.
|
|
495 *
|
|
496 * For 32-bit numbers and some divisors gcc and icc use
|
|
497 * a inlined multiplication and shifts. For example,
|
|
498 * unsigned "i32 / 10" is compiled to
|
|
499 *
|
|
500 * (i32 * 0xCCCCCCCD) >> 35
|
|
501 */
|
|
502
|
|
503 ui32 = (uint32_t) ui64;
|
|
504
|
|
505 do {
|
|
506 *--p = (u_char) (ui32 % 10 + '0');
|
|
507 } while (ui32 /= 10);
|
|
508
|
|
509 } else {
|
|
510 do {
|
|
511 *--p = (u_char) (ui64 % 10 + '0');
|
|
512 } while (ui64 /= 10);
|
|
513 }
|
|
514
|
|
515 } else if (hexadecimal == 1) {
|
|
516
|
|
517 do {
|
|
518
|
|
519 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
520 *--p = hex[(uint32_t) (ui64 & 0xf)];
|
|
521
|
|
522 } while (ui64 >>= 4);
|
|
523
|
|
524 } else { /* hexadecimal == 2 */
|
|
525
|
|
526 do {
|
|
527
|
|
528 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
529 *--p = HEX[(uint32_t) (ui64 & 0xf)];
|
|
530
|
|
531 } while (ui64 >>= 4);
|
|
532 }
|
|
533
|
|
534 /* zero or space padding */
|
|
535
|
|
536 len = (temp + NGX_INT64_LEN) - p;
|
|
537
|
|
538 while (len++ < width && buf < last) {
|
|
539 *buf++ = zero;
|
|
540 }
|
|
541
|
|
542 /* number safe copy */
|
|
543
|
|
544 len = (temp + NGX_INT64_LEN) - p;
|
|
545
|
|
546 if (buf + len > last) {
|
|
547 len = last - buf;
|
|
548 }
|
|
549
|
|
550 return ngx_cpymem(buf, p, len);
|
|
551 }
|
|
552
|
|
553
|
286
|
554 /*
|
340
|
555 * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII strings only,
|
286
|
556 * and implement our own ngx_strcasecmp()/ngx_strncasecmp()
|
|
557 * to avoid libc locale overhead. Besides, we use the ngx_uint_t's
|
|
558 * instead of the u_char's, because they are slightly faster.
|
|
559 */
|
|
560
|
|
561 ngx_int_t
|
|
562 ngx_strcasecmp(u_char *s1, u_char *s2)
|
|
563 {
|
|
564 ngx_uint_t c1, c2;
|
|
565
|
|
566 for ( ;; ) {
|
|
567 c1 = (ngx_uint_t) *s1++;
|
|
568 c2 = (ngx_uint_t) *s2++;
|
|
569
|
526
|
570 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
571 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
286
|
572
|
|
573 if (c1 == c2) {
|
|
574
|
|
575 if (c1) {
|
|
576 continue;
|
|
577 }
|
|
578
|
|
579 return 0;
|
|
580 }
|
|
581
|
|
582 return c1 - c2;
|
|
583 }
|
|
584 }
|
|
585
|
|
586
|
|
587 ngx_int_t
|
|
588 ngx_strncasecmp(u_char *s1, u_char *s2, size_t n)
|
|
589 {
|
|
590 ngx_uint_t c1, c2;
|
|
591
|
|
592 while (n) {
|
|
593 c1 = (ngx_uint_t) *s1++;
|
|
594 c2 = (ngx_uint_t) *s2++;
|
|
595
|
526
|
596 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
597 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
286
|
598
|
|
599 if (c1 == c2) {
|
|
600
|
|
601 if (c1) {
|
|
602 n--;
|
|
603 continue;
|
|
604 }
|
|
605
|
|
606 return 0;
|
|
607 }
|
|
608
|
|
609 return c1 - c2;
|
|
610 }
|
|
611
|
|
612 return 0;
|
|
613 }
|
|
614
|
|
615
|
340
|
616 u_char *
|
|
617 ngx_strnstr(u_char *s1, char *s2, size_t len)
|
|
618 {
|
|
619 u_char c1, c2;
|
|
620 size_t n;
|
|
621
|
|
622 c2 = *(u_char *) s2++;
|
|
623
|
|
624 n = ngx_strlen(s2);
|
|
625
|
|
626 do {
|
|
627 do {
|
|
628 if (len-- == 0) {
|
|
629 return NULL;
|
|
630 }
|
|
631
|
|
632 c1 = *s1++;
|
|
633
|
|
634 if (c1 == 0) {
|
|
635 return NULL;
|
|
636 }
|
|
637
|
|
638 } while (c1 != c2);
|
|
639
|
|
640 if (n > len) {
|
|
641 return NULL;
|
|
642 }
|
|
643
|
|
644 } while (ngx_strncmp(s1, (u_char *) s2, n) != 0);
|
|
645
|
|
646 return --s1;
|
|
647 }
|
|
648
|
|
649
|
|
650 /*
|
|
651 * ngx_strstrn() and ngx_strcasestrn() are intended to search for static
|
|
652 * substring with known length in null-terminated string. The argument n
|
|
653 * must be length of the second substring - 1.
|
|
654 */
|
|
655
|
|
656 u_char *
|
|
657 ngx_strstrn(u_char *s1, char *s2, size_t n)
|
|
658 {
|
|
659 u_char c1, c2;
|
|
660
|
|
661 c2 = *(u_char *) s2++;
|
|
662
|
|
663 do {
|
|
664 do {
|
|
665 c1 = *s1++;
|
|
666
|
|
667 if (c1 == 0) {
|
|
668 return NULL;
|
|
669 }
|
|
670
|
|
671 } while (c1 != c2);
|
|
672
|
|
673 } while (ngx_strncmp(s1, (u_char *) s2, n) != 0);
|
|
674
|
|
675 return --s1;
|
|
676 }
|
|
677
|
|
678
|
|
679 u_char *
|
|
680 ngx_strcasestrn(u_char *s1, char *s2, size_t n)
|
|
681 {
|
|
682 ngx_uint_t c1, c2;
|
|
683
|
|
684 c2 = (ngx_uint_t) *s2++;
|
526
|
685 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
340
|
686
|
|
687 do {
|
|
688 do {
|
|
689 c1 = (ngx_uint_t) *s1++;
|
|
690
|
|
691 if (c1 == 0) {
|
|
692 return NULL;
|
|
693 }
|
|
694
|
526
|
695 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
340
|
696
|
|
697 } while (c1 != c2);
|
|
698
|
|
699 } while (ngx_strncasecmp(s1, (u_char *) s2, n) != 0);
|
|
700
|
|
701 return --s1;
|
|
702 }
|
|
703
|
|
704
|
472
|
705 /*
|
|
706 * ngx_strlcasestrn() is intended to search for static substring
|
|
707 * with known length in string until the argument last. The argument n
|
|
708 * must be length of the second substring - 1.
|
|
709 */
|
|
710
|
|
711 u_char *
|
|
712 ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n)
|
|
713 {
|
|
714 ngx_uint_t c1, c2;
|
|
715
|
|
716 c2 = (ngx_uint_t) *s2++;
|
526
|
717 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
472
|
718 last -= n;
|
|
719
|
|
720 do {
|
|
721 do {
|
476
|
722 if (s1 >= last) {
|
472
|
723 return NULL;
|
|
724 }
|
|
725
|
|
726 c1 = (ngx_uint_t) *s1++;
|
|
727
|
526
|
728 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
472
|
729
|
|
730 } while (c1 != c2);
|
|
731
|
|
732 } while (ngx_strncasecmp(s1, s2, n) != 0);
|
|
733
|
|
734 return --s1;
|
|
735 }
|
|
736
|
|
737
|
42
|
738 ngx_int_t
|
|
739 ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
|
0
|
740 {
|
|
741 if (n == 0) {
|
|
742 return 0;
|
|
743 }
|
|
744
|
|
745 n--;
|
|
746
|
|
747 for ( ;; ) {
|
|
748 if (s1[n] != s2[n]) {
|
|
749 return s1[n] - s2[n];
|
|
750 }
|
|
751
|
|
752 if (n == 0) {
|
|
753 return 0;
|
|
754 }
|
|
755
|
|
756 n--;
|
|
757 }
|
|
758 }
|
|
759
|
|
760
|
42
|
761 ngx_int_t
|
|
762 ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
|
4
|
763 {
|
|
764 u_char c1, c2;
|
|
765
|
|
766 if (n == 0) {
|
|
767 return 0;
|
|
768 }
|
|
769
|
|
770 n--;
|
|
771
|
|
772 for ( ;; ) {
|
|
773 c1 = s1[n];
|
|
774 if (c1 >= 'a' && c1 <= 'z') {
|
|
775 c1 -= 'a' - 'A';
|
|
776 }
|
|
777
|
|
778 c2 = s2[n];
|
|
779 if (c2 >= 'a' && c2 <= 'z') {
|
|
780 c2 -= 'a' - 'A';
|
|
781 }
|
|
782
|
|
783 if (c1 != c2) {
|
|
784 return c1 - c2;
|
|
785 }
|
|
786
|
|
787 if (n == 0) {
|
|
788 return 0;
|
|
789 }
|
|
790
|
|
791 n--;
|
|
792 }
|
|
793 }
|
|
794
|
|
795
|
42
|
796 ngx_int_t
|
274
|
797 ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2)
|
|
798 {
|
|
799 size_t n;
|
|
800 ngx_int_t m, z;
|
|
801
|
|
802 if (n1 <= n2) {
|
|
803 n = n1;
|
|
804 z = -1;
|
|
805
|
|
806 } else {
|
|
807 n = n2;
|
|
808 z = 1;
|
|
809 }
|
|
810
|
|
811 m = ngx_memcmp(s1, s2, n);
|
|
812
|
|
813 if (m || n1 == n2) {
|
|
814 return m;
|
|
815 }
|
|
816
|
|
817 return z;
|
|
818 }
|
|
819
|
|
820
|
|
821 ngx_int_t
|
526
|
822 ngx_dns_strcmp(u_char *s1, u_char *s2)
|
|
823 {
|
|
824 ngx_uint_t c1, c2;
|
|
825
|
|
826 for ( ;; ) {
|
|
827 c1 = (ngx_uint_t) *s1++;
|
|
828 c2 = (ngx_uint_t) *s2++;
|
|
829
|
|
830 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
831 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
|
832
|
|
833 if (c1 == c2) {
|
|
834
|
|
835 if (c1) {
|
|
836 continue;
|
|
837 }
|
|
838
|
|
839 return 0;
|
|
840 }
|
|
841
|
|
842 /* in ASCII '.' > '-', but we need '.' to be the lowest character */
|
|
843
|
|
844 c1 = (c1 == '.') ? ' ' : c1;
|
|
845 c2 = (c2 == '.') ? ' ' : c2;
|
|
846
|
|
847 return c1 - c2;
|
|
848 }
|
|
849 }
|
|
850
|
|
851
|
|
852 ngx_int_t
|
42
|
853 ngx_atoi(u_char *line, size_t n)
|
0
|
854 {
|
|
855 ngx_int_t value;
|
|
856
|
|
857 if (n == 0) {
|
|
858 return NGX_ERROR;
|
|
859 }
|
|
860
|
|
861 for (value = 0; n--; line++) {
|
|
862 if (*line < '0' || *line > '9') {
|
|
863 return NGX_ERROR;
|
|
864 }
|
|
865
|
|
866 value = value * 10 + (*line - '0');
|
|
867 }
|
|
868
|
|
869 if (value < 0) {
|
|
870 return NGX_ERROR;
|
|
871
|
|
872 } else {
|
|
873 return value;
|
|
874 }
|
|
875 }
|
|
876
|
|
877
|
570
|
878 /* parse a fixed point number, e.g., ngx_atofp("10.5", 4, 2) returns 1050 */
|
|
879
|
|
880 ngx_int_t
|
|
881 ngx_atofp(u_char *line, size_t n, size_t point)
|
|
882 {
|
|
883 ngx_int_t value;
|
|
884 ngx_uint_t dot;
|
|
885
|
|
886 if (n == 0) {
|
|
887 return NGX_ERROR;
|
|
888 }
|
|
889
|
|
890 dot = 0;
|
|
891
|
|
892 for (value = 0; n--; line++) {
|
|
893
|
|
894 if (point == 0) {
|
|
895 return NGX_ERROR;
|
|
896 }
|
|
897
|
|
898 if (*line == '.') {
|
|
899 if (dot) {
|
|
900 return NGX_ERROR;
|
|
901 }
|
|
902
|
|
903 dot = 1;
|
|
904 continue;
|
|
905 }
|
|
906
|
|
907 if (*line < '0' || *line > '9') {
|
|
908 return NGX_ERROR;
|
|
909 }
|
|
910
|
|
911 value = value * 10 + (*line - '0');
|
|
912 point -= dot;
|
|
913 }
|
|
914
|
|
915 while (point--) {
|
|
916 value = value * 10;
|
|
917 }
|
|
918
|
|
919 if (value < 0) {
|
|
920 return NGX_ERROR;
|
|
921
|
|
922 } else {
|
|
923 return value;
|
|
924 }
|
|
925 }
|
|
926
|
|
927
|
50
|
928 ssize_t
|
|
929 ngx_atosz(u_char *line, size_t n)
|
|
930 {
|
|
931 ssize_t value;
|
|
932
|
|
933 if (n == 0) {
|
|
934 return NGX_ERROR;
|
|
935 }
|
|
936
|
|
937 for (value = 0; n--; line++) {
|
|
938 if (*line < '0' || *line > '9') {
|
|
939 return NGX_ERROR;
|
|
940 }
|
|
941
|
|
942 value = value * 10 + (*line - '0');
|
|
943 }
|
|
944
|
|
945 if (value < 0) {
|
|
946 return NGX_ERROR;
|
|
947
|
|
948 } else {
|
|
949 return value;
|
|
950 }
|
|
951 }
|
|
952
|
|
953
|
|
954 off_t
|
|
955 ngx_atoof(u_char *line, size_t n)
|
|
956 {
|
|
957 off_t value;
|
|
958
|
|
959 if (n == 0) {
|
|
960 return NGX_ERROR;
|
|
961 }
|
|
962
|
|
963 for (value = 0; n--; line++) {
|
|
964 if (*line < '0' || *line > '9') {
|
|
965 return NGX_ERROR;
|
|
966 }
|
|
967
|
|
968 value = value * 10 + (*line - '0');
|
|
969 }
|
|
970
|
|
971 if (value < 0) {
|
|
972 return NGX_ERROR;
|
|
973
|
|
974 } else {
|
|
975 return value;
|
|
976 }
|
|
977 }
|
|
978
|
|
979
|
|
980 time_t
|
|
981 ngx_atotm(u_char *line, size_t n)
|
|
982 {
|
|
983 time_t value;
|
|
984
|
|
985 if (n == 0) {
|
|
986 return NGX_ERROR;
|
|
987 }
|
|
988
|
|
989 for (value = 0; n--; line++) {
|
|
990 if (*line < '0' || *line > '9') {
|
|
991 return NGX_ERROR;
|
|
992 }
|
|
993
|
|
994 value = value * 10 + (*line - '0');
|
|
995 }
|
|
996
|
|
997 if (value < 0) {
|
|
998 return NGX_ERROR;
|
|
999
|
|
1000 } else {
|
|
1001 return value;
|
|
1002 }
|
|
1003 }
|
|
1004
|
|
1005
|
42
|
1006 ngx_int_t
|
|
1007 ngx_hextoi(u_char *line, size_t n)
|
0
|
1008 {
|
250
|
1009 u_char c, ch;
|
0
|
1010 ngx_int_t value;
|
|
1011
|
|
1012 if (n == 0) {
|
|
1013 return NGX_ERROR;
|
|
1014 }
|
|
1015
|
|
1016 for (value = 0; n--; line++) {
|
|
1017 ch = *line;
|
|
1018
|
|
1019 if (ch >= '0' && ch <= '9') {
|
|
1020 value = value * 16 + (ch - '0');
|
|
1021 continue;
|
|
1022 }
|
|
1023
|
250
|
1024 c = (u_char) (ch | 0x20);
|
0
|
1025
|
250
|
1026 if (c >= 'a' && c <= 'f') {
|
|
1027 value = value * 16 + (c - 'a' + 10);
|
0
|
1028 continue;
|
|
1029 }
|
|
1030
|
|
1031 return NGX_ERROR;
|
|
1032 }
|
|
1033
|
|
1034 if (value < 0) {
|
|
1035 return NGX_ERROR;
|
|
1036
|
|
1037 } else {
|
|
1038 return value;
|
|
1039 }
|
|
1040 }
|
|
1041
|
|
1042
|
356
|
1043 u_char *
|
|
1044 ngx_hex_dump(u_char *dst, u_char *src, size_t len)
|
0
|
1045 {
|
|
1046 static u_char hex[] = "0123456789abcdef";
|
|
1047
|
356
|
1048 while (len--) {
|
|
1049 *dst++ = hex[*src >> 4];
|
|
1050 *dst++ = hex[*src++ & 0xf];
|
0
|
1051 }
|
|
1052
|
356
|
1053 return dst;
|
0
|
1054 }
|
|
1055
|
|
1056
|
42
|
1057 void
|
|
1058 ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
1059 {
|
|
1060 u_char *d, *s;
|
|
1061 size_t len;
|
|
1062 static u_char basis64[] =
|
|
1063 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1064
|
|
1065 len = src->len;
|
|
1066 s = src->data;
|
|
1067 d = dst->data;
|
|
1068
|
|
1069 while (len > 2) {
|
|
1070 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
1071 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
1072 *d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
|
|
1073 *d++ = basis64[s[2] & 0x3f];
|
|
1074
|
|
1075 s += 3;
|
|
1076 len -= 3;
|
|
1077 }
|
|
1078
|
|
1079 if (len) {
|
|
1080 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
1081
|
|
1082 if (len == 1) {
|
|
1083 *d++ = basis64[(s[0] & 3) << 4];
|
|
1084 *d++ = '=';
|
|
1085
|
|
1086 } else {
|
|
1087 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
1088 *d++ = basis64[(s[1] & 0x0f) << 2];
|
|
1089 }
|
|
1090
|
|
1091 *d++ = '=';
|
|
1092 }
|
|
1093
|
|
1094 dst->len = d - dst->data;
|
|
1095 }
|
|
1096
|
|
1097
|
42
|
1098 ngx_int_t
|
|
1099 ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
1100 {
|
254
|
1101 static u_char basis64[] = {
|
|
1102 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1103 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1104 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
|
|
1105 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
1106 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
1107 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
|
|
1108 77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
1109 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
0
|
1110
|
254
|
1111 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1112 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1113 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1114 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1115 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1116 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1117 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1118 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
|
|
1119 };
|
0
|
1120
|
596
|
1121 return ngx_decode_base64_internal(dst, src, basis64);
|
|
1122 }
|
|
1123
|
|
1124
|
|
1125 ngx_int_t
|
|
1126 ngx_decode_base64url(ngx_str_t *dst, ngx_str_t *src)
|
|
1127 {
|
|
1128 static u_char basis64[] = {
|
|
1129 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1130 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1131 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77,
|
|
1132 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
1133 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
1134 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 63,
|
|
1135 77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
1136 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
|
1137
|
|
1138 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1139 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1140 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1141 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1142 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1143 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1144 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
1145 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
|
|
1146 };
|
|
1147
|
|
1148 return ngx_decode_base64_internal(dst, src, basis64);
|
|
1149 }
|
|
1150
|
|
1151
|
|
1152 static ngx_int_t
|
|
1153 ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis)
|
|
1154 {
|
|
1155 size_t len;
|
|
1156 u_char *d, *s;
|
|
1157
|
0
|
1158 for (len = 0; len < src->len; len++) {
|
|
1159 if (src->data[len] == '=') {
|
|
1160 break;
|
|
1161 }
|
|
1162
|
596
|
1163 if (basis[src->data[len]] == 77) {
|
0
|
1164 return NGX_ERROR;
|
|
1165 }
|
|
1166 }
|
|
1167
|
|
1168 if (len % 4 == 1) {
|
|
1169 return NGX_ERROR;
|
|
1170 }
|
|
1171
|
|
1172 s = src->data;
|
|
1173 d = dst->data;
|
|
1174
|
|
1175 while (len > 3) {
|
596
|
1176 *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
|
|
1177 *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
|
|
1178 *d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]);
|
0
|
1179
|
|
1180 s += 4;
|
|
1181 len -= 4;
|
|
1182 }
|
|
1183
|
|
1184 if (len > 1) {
|
596
|
1185 *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
|
0
|
1186 }
|
|
1187
|
|
1188 if (len > 2) {
|
596
|
1189 *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
|
0
|
1190 }
|
|
1191
|
|
1192 dst->len = d - dst->data;
|
|
1193
|
|
1194 return NGX_OK;
|
|
1195 }
|
|
1196
|
|
1197
|
206
|
1198 /*
|
390
|
1199 * ngx_utf8_decode() decodes two and more bytes UTF sequences only
|
206
|
1200 * the return values:
|
|
1201 * 0x80 - 0x10ffff valid character
|
390
|
1202 * 0x110000 - 0xfffffffd invalid sequence
|
206
|
1203 * 0xfffffffe incomplete sequence
|
|
1204 * 0xffffffff error
|
|
1205 */
|
|
1206
|
|
1207 uint32_t
|
390
|
1208 ngx_utf8_decode(u_char **p, size_t n)
|
206
|
1209 {
|
|
1210 size_t len;
|
|
1211 uint32_t u, i, valid;
|
|
1212
|
|
1213 u = **p;
|
|
1214
|
638
|
1215 if (u >= 0xf0) {
|
206
|
1216
|
|
1217 u &= 0x07;
|
|
1218 valid = 0xffff;
|
|
1219 len = 3;
|
|
1220
|
638
|
1221 } else if (u >= 0xe0) {
|
206
|
1222
|
|
1223 u &= 0x0f;
|
|
1224 valid = 0x7ff;
|
|
1225 len = 2;
|
|
1226
|
638
|
1227 } else if (u >= 0xc2) {
|
206
|
1228
|
|
1229 u &= 0x1f;
|
|
1230 valid = 0x7f;
|
|
1231 len = 1;
|
|
1232
|
|
1233 } else {
|
|
1234 (*p)++;
|
|
1235 return 0xffffffff;
|
|
1236 }
|
|
1237
|
|
1238 if (n - 1 < len) {
|
|
1239 return 0xfffffffe;
|
|
1240 }
|
|
1241
|
|
1242 (*p)++;
|
|
1243
|
|
1244 while (len) {
|
|
1245 i = *(*p)++;
|
|
1246
|
|
1247 if (i < 0x80) {
|
|
1248 return 0xffffffff;
|
|
1249 }
|
|
1250
|
|
1251 u = (u << 6) | (i & 0x3f);
|
|
1252
|
|
1253 len--;
|
|
1254 }
|
|
1255
|
|
1256 if (u > valid) {
|
|
1257 return u;
|
|
1258 }
|
|
1259
|
|
1260 return 0xffffffff;
|
|
1261 }
|
|
1262
|
|
1263
|
72
|
1264 size_t
|
390
|
1265 ngx_utf8_length(u_char *p, size_t n)
|
72
|
1266 {
|
390
|
1267 u_char c, *last;
|
|
1268 size_t len;
|
72
|
1269
|
390
|
1270 last = p + n;
|
72
|
1271
|
390
|
1272 for (len = 0; p < last; len++) {
|
|
1273
|
|
1274 c = *p;
|
72
|
1275
|
|
1276 if (c < 0x80) {
|
390
|
1277 p++;
|
72
|
1278 continue;
|
|
1279 }
|
|
1280
|
390
|
1281 if (ngx_utf8_decode(&p, n) > 0x10ffff) {
|
|
1282 /* invalid UTF-8 */
|
|
1283 return n;
|
72
|
1284 }
|
|
1285 }
|
|
1286
|
|
1287 return len;
|
|
1288 }
|
|
1289
|
|
1290
|
76
|
1291 u_char *
|
390
|
1292 ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len)
|
76
|
1293 {
|
390
|
1294 u_char c, *next;
|
76
|
1295
|
|
1296 if (n == 0) {
|
|
1297 return dst;
|
|
1298 }
|
|
1299
|
390
|
1300 while (--n) {
|
76
|
1301
|
|
1302 c = *src;
|
|
1303 *dst = c;
|
|
1304
|
|
1305 if (c < 0x80) {
|
390
|
1306
|
|
1307 if (c != '\0') {
|
|
1308 dst++;
|
|
1309 src++;
|
|
1310 len--;
|
|
1311
|
76
|
1312 continue;
|
|
1313 }
|
|
1314
|
|
1315 return dst;
|
|
1316 }
|
|
1317
|
390
|
1318 next = src;
|
76
|
1319
|
390
|
1320 if (ngx_utf8_decode(&next, len) > 0x10ffff) {
|
|
1321 /* invalid UTF-8 */
|
|
1322 break;
|
76
|
1323 }
|
|
1324
|
390
|
1325 while (src < next) {
|
560
|
1326 *dst++ = *src++;
|
390
|
1327 len--;
|
|
1328 }
|
76
|
1329 }
|
|
1330
|
|
1331 *dst = '\0';
|
|
1332
|
|
1333 return dst;
|
|
1334 }
|
|
1335
|
|
1336
|
42
|
1337 uintptr_t
|
|
1338 ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
|
0
|
1339 {
|
540
|
1340 ngx_uint_t n;
|
254
|
1341 uint32_t *escape;
|
|
1342 static u_char hex[] = "0123456789abcdef";
|
10
|
1343
|
254
|
1344 /* " ", "#", "%", "?", %00-%1F, %7F-%FF */
|
34
|
1345
|
254
|
1346 static uint32_t uri[] = {
|
|
1347 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
0
|
1348
|
254
|
1349 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
1350 0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
|
|
1351
|
|
1352 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1353 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
2
|
1354
|
254
|
1355 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1356 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
0
|
1357
|
254
|
1358 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1359 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1360 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1361 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1362 };
|
2
|
1363
|
566
|
1364 /* " ", "#", "%", "&", "+", "?", %00-%1F, %7F-%FF */
|
34
|
1365
|
254
|
1366 static uint32_t args[] = {
|
|
1367 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1368
|
|
1369 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
592
|
1370 0x88000869, /* 1000 1000 0000 0000 0000 1000 0110 1001 */
|
34
|
1371
|
254
|
1372 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1373 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
34
|
1374
|
254
|
1375 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1376 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
34
|
1377
|
254
|
1378 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1379 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1380 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1381 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1382 };
|
34
|
1383
|
638
|
1384 /* not ALPHA, DIGIT, "-", ".", "_", "~" */
|
|
1385
|
|
1386 static uint32_t uri_component[] = {
|
|
1387 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1388
|
|
1389 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
1390 0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */
|
|
1391
|
|
1392 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1393 0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */
|
|
1394
|
|
1395 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1396 0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
|
|
1397
|
|
1398 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1399 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1400 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1401 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1402 };
|
|
1403
|
328
|
1404 /* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */
|
34
|
1405
|
254
|
1406 static uint32_t html[] = {
|
|
1407 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
10
|
1408
|
254
|
1409 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
320
|
1410 0x000000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */
|
254
|
1411
|
|
1412 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1413 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
10
|
1414
|
254
|
1415 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1416 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
10
|
1417
|
254
|
1418 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1419 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1420 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1421 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1422 };
|
10
|
1423
|
328
|
1424 /* " ", """, "%", "'", %00-%1F, %7F-%FF */
|
10
|
1425
|
320
|
1426 static uint32_t refresh[] = {
|
|
1427 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1428
|
|
1429 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
346
|
1430 0x00000085, /* 0000 0000 0000 0000 0000 0000 1000 0101 */
|
320
|
1431
|
|
1432 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1433 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1434
|
|
1435 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1436 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1437
|
|
1438 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1439 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1440 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1441 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1442 };
|
|
1443
|
328
|
1444 /* " ", "%", %00-%1F */
|
322
|
1445
|
|
1446 static uint32_t memcached[] = {
|
|
1447 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1448
|
|
1449 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
328
|
1450 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
|
322
|
1451
|
|
1452 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1453 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1454
|
|
1455 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1456 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1457
|
|
1458 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1459 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1460 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1461 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1462 };
|
|
1463
|
328
|
1464 /* mail_auth is the same as memcached */
|
|
1465
|
|
1466 static uint32_t *map[] =
|
638
|
1467 { uri, args, uri_component, html, refresh, memcached, memcached };
|
320
|
1468
|
|
1469
|
|
1470 escape = map[type];
|
10
|
1471
|
2
|
1472 if (dst == NULL) {
|
|
1473
|
|
1474 /* find the number of the characters to be escaped */
|
0
|
1475
|
526
|
1476 n = 0;
|
2
|
1477
|
540
|
1478 while (size) {
|
2
|
1479 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
1480 n++;
|
|
1481 }
|
|
1482 src++;
|
540
|
1483 size--;
|
2
|
1484 }
|
|
1485
|
34
|
1486 return (uintptr_t) n;
|
0
|
1487 }
|
|
1488
|
540
|
1489 while (size) {
|
2
|
1490 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
1491 *dst++ = '%';
|
|
1492 *dst++ = hex[*src >> 4];
|
|
1493 *dst++ = hex[*src & 0xf];
|
|
1494 src++;
|
0
|
1495
|
2
|
1496 } else {
|
|
1497 *dst++ = *src++;
|
|
1498 }
|
540
|
1499 size--;
|
0
|
1500 }
|
|
1501
|
34
|
1502 return (uintptr_t) dst;
|
0
|
1503 }
|
126
|
1504
|
|
1505
|
|
1506 void
|
150
|
1507 ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type)
|
126
|
1508 {
|
|
1509 u_char *d, *s, ch, c, decoded;
|
|
1510 enum {
|
|
1511 sw_usual = 0,
|
|
1512 sw_quoted,
|
|
1513 sw_quoted_second
|
|
1514 } state;
|
|
1515
|
|
1516 d = *dst;
|
|
1517 s = *src;
|
|
1518
|
|
1519 state = 0;
|
|
1520 decoded = 0;
|
|
1521
|
|
1522 while (size--) {
|
|
1523
|
|
1524 ch = *s++;
|
|
1525
|
|
1526 switch (state) {
|
|
1527 case sw_usual:
|
342
|
1528 if (ch == '?'
|
|
1529 && (type & (NGX_UNESCAPE_URI|NGX_UNESCAPE_REDIRECT)))
|
|
1530 {
|
126
|
1531 *d++ = ch;
|
|
1532 goto done;
|
|
1533 }
|
|
1534
|
|
1535 if (ch == '%') {
|
|
1536 state = sw_quoted;
|
|
1537 break;
|
|
1538 }
|
|
1539
|
|
1540 *d++ = ch;
|
|
1541 break;
|
|
1542
|
|
1543 case sw_quoted:
|
|
1544
|
|
1545 if (ch >= '0' && ch <= '9') {
|
|
1546 decoded = (u_char) (ch - '0');
|
|
1547 state = sw_quoted_second;
|
|
1548 break;
|
|
1549 }
|
|
1550
|
|
1551 c = (u_char) (ch | 0x20);
|
|
1552 if (c >= 'a' && c <= 'f') {
|
|
1553 decoded = (u_char) (c - 'a' + 10);
|
|
1554 state = sw_quoted_second;
|
|
1555 break;
|
|
1556 }
|
|
1557
|
138
|
1558 /* the invalid quoted character */
|
126
|
1559
|
138
|
1560 state = sw_usual;
|
|
1561
|
|
1562 *d++ = ch;
|
126
|
1563
|
|
1564 break;
|
|
1565
|
|
1566 case sw_quoted_second:
|
|
1567
|
138
|
1568 state = sw_usual;
|
|
1569
|
126
|
1570 if (ch >= '0' && ch <= '9') {
|
|
1571 ch = (u_char) ((decoded << 4) + ch - '0');
|
|
1572
|
342
|
1573 if (type & NGX_UNESCAPE_REDIRECT) {
|
150
|
1574 if (ch > '%' && ch < 0x7f) {
|
|
1575 *d++ = ch;
|
|
1576 break;
|
|
1577 }
|
|
1578
|
|
1579 *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
|
1580
|
126
|
1581 break;
|
|
1582 }
|
|
1583
|
150
|
1584 *d++ = ch;
|
126
|
1585
|
|
1586 break;
|
|
1587 }
|
|
1588
|
|
1589 c = (u_char) (ch | 0x20);
|
|
1590 if (c >= 'a' && c <= 'f') {
|
|
1591 ch = (u_char) ((decoded << 4) + c - 'a' + 10);
|
|
1592
|
342
|
1593 if (type & NGX_UNESCAPE_URI) {
|
|
1594 if (ch == '?') {
|
|
1595 *d++ = ch;
|
|
1596 goto done;
|
|
1597 }
|
|
1598
|
|
1599 *d++ = ch;
|
|
1600 break;
|
|
1601 }
|
|
1602
|
|
1603 if (type & NGX_UNESCAPE_REDIRECT) {
|
150
|
1604 if (ch == '?') {
|
|
1605 *d++ = ch;
|
|
1606 goto done;
|
|
1607 }
|
126
|
1608
|
150
|
1609 if (ch > '%' && ch < 0x7f) {
|
|
1610 *d++ = ch;
|
|
1611 break;
|
|
1612 }
|
|
1613
|
|
1614 *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
126
|
1615 break;
|
|
1616 }
|
|
1617
|
150
|
1618 *d++ = ch;
|
126
|
1619
|
|
1620 break;
|
|
1621 }
|
|
1622
|
138
|
1623 /* the invalid quoted character */
|
126
|
1624
|
|
1625 break;
|
|
1626 }
|
|
1627 }
|
|
1628
|
|
1629 done:
|
|
1630
|
|
1631 *dst = d;
|
|
1632 *src = s;
|
|
1633 }
|
132
|
1634
|
|
1635
|
340
|
1636 uintptr_t
|
|
1637 ngx_escape_html(u_char *dst, u_char *src, size_t size)
|
|
1638 {
|
|
1639 u_char ch;
|
540
|
1640 ngx_uint_t len;
|
340
|
1641
|
|
1642 if (dst == NULL) {
|
|
1643
|
|
1644 len = 0;
|
|
1645
|
540
|
1646 while (size) {
|
340
|
1647 switch (*src++) {
|
|
1648
|
|
1649 case '<':
|
|
1650 len += sizeof("<") - 2;
|
|
1651 break;
|
|
1652
|
|
1653 case '>':
|
|
1654 len += sizeof(">") - 2;
|
|
1655 break;
|
|
1656
|
|
1657 case '&':
|
|
1658 len += sizeof("&") - 2;
|
|
1659 break;
|
|
1660
|
642
|
1661 case '"':
|
|
1662 len += sizeof(""") - 2;
|
|
1663 break;
|
|
1664
|
340
|
1665 default:
|
|
1666 break;
|
|
1667 }
|
540
|
1668 size--;
|
340
|
1669 }
|
|
1670
|
|
1671 return (uintptr_t) len;
|
|
1672 }
|
|
1673
|
540
|
1674 while (size) {
|
340
|
1675 ch = *src++;
|
|
1676
|
|
1677 switch (ch) {
|
|
1678
|
|
1679 case '<':
|
|
1680 *dst++ = '&'; *dst++ = 'l'; *dst++ = 't'; *dst++ = ';';
|
|
1681 break;
|
|
1682
|
|
1683 case '>':
|
|
1684 *dst++ = '&'; *dst++ = 'g'; *dst++ = 't'; *dst++ = ';';
|
|
1685 break;
|
|
1686
|
|
1687 case '&':
|
|
1688 *dst++ = '&'; *dst++ = 'a'; *dst++ = 'm'; *dst++ = 'p';
|
|
1689 *dst++ = ';';
|
|
1690 break;
|
|
1691
|
642
|
1692 case '"':
|
|
1693 *dst++ = '&'; *dst++ = 'q'; *dst++ = 'u'; *dst++ = 'o';
|
|
1694 *dst++ = 't'; *dst++ = ';';
|
|
1695 break;
|
|
1696
|
340
|
1697 default:
|
|
1698 *dst++ = ch;
|
|
1699 break;
|
|
1700 }
|
540
|
1701 size--;
|
340
|
1702 }
|
|
1703
|
|
1704 return (uintptr_t) dst;
|
|
1705 }
|
|
1706
|
|
1707
|
582
|
1708 void
|
|
1709 ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
1710 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
1711 {
|
|
1712 ngx_str_node_t *n, *t;
|
|
1713 ngx_rbtree_node_t **p;
|
|
1714
|
|
1715 for ( ;; ) {
|
|
1716
|
|
1717 n = (ngx_str_node_t *) node;
|
|
1718 t = (ngx_str_node_t *) temp;
|
|
1719
|
|
1720 if (node->key != temp->key) {
|
|
1721
|
|
1722 p = (node->key < temp->key) ? &temp->left : &temp->right;
|
|
1723
|
|
1724 } else if (n->str.len != t->str.len) {
|
|
1725
|
|
1726 p = (n->str.len < t->str.len) ? &temp->left : &temp->right;
|
|
1727
|
|
1728 } else {
|
|
1729 p = (ngx_memcmp(n->str.data, t->str.data, n->str.len) < 0)
|
|
1730 ? &temp->left : &temp->right;
|
|
1731 }
|
|
1732
|
|
1733 if (*p == sentinel) {
|
|
1734 break;
|
|
1735 }
|
|
1736
|
|
1737 temp = *p;
|
|
1738 }
|
|
1739
|
|
1740 *p = node;
|
|
1741 node->parent = temp;
|
|
1742 node->left = sentinel;
|
|
1743 node->right = sentinel;
|
|
1744 ngx_rbt_red(node);
|
|
1745 }
|
|
1746
|
|
1747
|
|
1748 ngx_str_node_t *
|
|
1749 ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *val, uint32_t hash)
|
|
1750 {
|
|
1751 ngx_int_t rc;
|
|
1752 ngx_str_node_t *n;
|
|
1753 ngx_rbtree_node_t *node, *sentinel;
|
|
1754
|
|
1755 node = rbtree->root;
|
|
1756 sentinel = rbtree->sentinel;
|
|
1757
|
|
1758 while (node != sentinel) {
|
|
1759
|
|
1760 n = (ngx_str_node_t *) node;
|
|
1761
|
|
1762 if (hash != node->key) {
|
|
1763 node = (hash < node->key) ? node->left : node->right;
|
|
1764 continue;
|
|
1765 }
|
|
1766
|
|
1767 if (val->len != n->str.len) {
|
|
1768 node = (val->len < n->str.len) ? node->left : node->right;
|
|
1769 continue;
|
|
1770 }
|
|
1771
|
|
1772 rc = ngx_memcmp(val->data, n->str.data, val->len);
|
|
1773
|
|
1774 if (rc < 0) {
|
|
1775 node = node->left;
|
|
1776 continue;
|
|
1777 }
|
|
1778
|
|
1779 if (rc > 0) {
|
|
1780 node = node->right;
|
|
1781 continue;
|
|
1782 }
|
|
1783
|
|
1784 return n;
|
|
1785 }
|
|
1786
|
|
1787 return NULL;
|
|
1788 }
|
|
1789
|
|
1790
|
302
|
1791 /* ngx_sort() is implemented as insertion sort because we need stable sort */
|
|
1792
|
|
1793 void
|
|
1794 ngx_sort(void *base, size_t n, size_t size,
|
372
|
1795 ngx_int_t (*cmp)(const void *, const void *))
|
302
|
1796 {
|
372
|
1797 u_char *p1, *p2, *p;
|
|
1798
|
|
1799 p = ngx_alloc(size, ngx_cycle->log);
|
|
1800 if (p == NULL) {
|
|
1801 return;
|
|
1802 }
|
302
|
1803
|
|
1804 for (p1 = (u_char *) base + size;
|
|
1805 p1 < (u_char *) base + n * size;
|
|
1806 p1 += size)
|
|
1807 {
|
372
|
1808 ngx_memcpy(p, p1, size);
|
302
|
1809
|
|
1810 for (p2 = p1;
|
372
|
1811 p2 > (u_char *) base && cmp(p2 - size, p) > 0;
|
302
|
1812 p2 -= size)
|
|
1813 {
|
|
1814 ngx_memcpy(p2, p2 - size, size);
|
|
1815 }
|
|
1816
|
372
|
1817 ngx_memcpy(p2, p, size);
|
302
|
1818 }
|
372
|
1819
|
|
1820 ngx_free(p);
|
302
|
1821 }
|
|
1822
|
|
1823
|
132
|
1824 #if (NGX_MEMCPY_LIMIT)
|
|
1825
|
|
1826 void *
|
|
1827 ngx_memcpy(void *dst, void *src, size_t n)
|
|
1828 {
|
|
1829 if (n > NGX_MEMCPY_LIMIT) {
|
|
1830 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "memcpy %uz bytes", n);
|
|
1831 ngx_debug_point();
|
|
1832 }
|
|
1833
|
|
1834 return memcpy(dst, src, n);
|
|
1835 }
|
|
1836
|
|
1837 #endif
|