0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
42
|
11 u_char *
|
|
12 ngx_cpystrn(u_char *dst, u_char *src, size_t n)
|
0
|
13 {
|
|
14 if (n == 0) {
|
|
15 return dst;
|
|
16 }
|
|
17
|
66
|
18 for ( /* void */ ; --n; dst++, src++) {
|
0
|
19 *dst = *src;
|
|
20
|
|
21 if (*dst == '\0') {
|
|
22 return dst;
|
|
23 }
|
|
24 }
|
|
25
|
|
26 *dst = '\0';
|
|
27
|
|
28 return dst;
|
|
29 }
|
|
30
|
|
31
|
42
|
32 u_char *
|
|
33 ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
|
34
|
34 {
|
|
35 u_char *dst;
|
|
36
|
50
|
37 dst = ngx_palloc(pool, src->len);
|
|
38 if (dst == NULL) {
|
34
|
39 return NULL;
|
|
40 }
|
|
41
|
|
42 ngx_memcpy(dst, src->data, src->len);
|
|
43
|
|
44 return dst;
|
|
45 }
|
|
46
|
|
47
|
4
|
48 /*
|
|
49 * supported formats:
|
22
|
50 * %[0][width][x][X]O off_t
|
10
|
51 * %[0][width]T time_t
|
|
52 * %[0][width][u][x|X]z ssize_t/size_t
|
|
53 * %[0][width][u][x|X]d int/u_int
|
|
54 * %[0][width][u][x|X]l long
|
|
55 * %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t
|
|
56 * %[0][width][u][x|X]D int32_t/uint32_t
|
|
57 * %[0][width][u][x|X]L int64_t/uint64_t
|
44
|
58 * %[0][width|m][u][x|X]A ngx_atomic_int_t/ngx_atomic_uint_t
|
10
|
59 * %P ngx_pid_t
|
106
|
60 * %M ngx_msec_t
|
10
|
61 * %r rlim_t
|
328
|
62 * %p void *
|
|
63 * %V ngx_str_t *
|
|
64 * %v ngx_variable_value_t *
|
10
|
65 * %s null-terminated string
|
|
66 * %Z '\0'
|
74
|
67 * %N '\n'
|
10
|
68 * %c char
|
|
69 * %% %
|
4
|
70 *
|
10
|
71 * reserved:
|
|
72 * %t ptrdiff_t
|
|
73 * %S null-teminated wchar string
|
|
74 * %C wchar
|
4
|
75 */
|
|
76
|
10
|
77
|
52
|
78 u_char * ngx_cdecl
|
42
|
79 ngx_sprintf(u_char *buf, const char *fmt, ...)
|
10
|
80 {
|
|
81 u_char *p;
|
|
82 va_list args;
|
|
83
|
|
84 va_start(args, fmt);
|
|
85 p = ngx_vsnprintf(buf, /* STUB */ 65536, fmt, args);
|
|
86 va_end(args);
|
|
87
|
|
88 return p;
|
|
89 }
|
|
90
|
|
91
|
52
|
92 u_char * ngx_cdecl
|
42
|
93 ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
|
4
|
94 {
|
10
|
95 u_char *p;
|
|
96 va_list args;
|
|
97
|
|
98 va_start(args, fmt);
|
|
99 p = ngx_vsnprintf(buf, max, fmt, args);
|
|
100 va_end(args);
|
|
101
|
|
102 return p;
|
|
103 }
|
|
104
|
4
|
105
|
42
|
106 u_char *
|
|
107 ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
|
10
|
108 {
|
320
|
109 u_char *p, zero, *last, temp[NGX_INT64_LEN + 1];
|
28
|
110 /*
|
|
111 * really we need temp[NGX_INT64_LEN] only,
|
42
|
112 * but icc issues the warning
|
28
|
113 */
|
320
|
114 int d;
|
|
115 size_t len;
|
|
116 uint32_t ui32;
|
|
117 int64_t i64;
|
|
118 uint64_t ui64;
|
|
119 ngx_msec_t ms;
|
|
120 ngx_uint_t width, sign, hexadecimal, max_width;
|
328
|
121 ngx_str_t *v;
|
|
122 ngx_variable_value_t *vv;
|
320
|
123 static u_char hex[] = "0123456789abcdef";
|
|
124 static u_char HEX[] = "0123456789ABCDEF";
|
4
|
125
|
10
|
126 if (max == 0) {
|
|
127 return buf;
|
|
128 }
|
|
129
|
|
130 last = buf + max;
|
|
131
|
|
132 while (*fmt && buf < last) {
|
|
133
|
|
134 /*
|
|
135 * "buf < last" means that we could copy at least one character:
|
|
136 * the plain character, "%%", "%c", and minus without the checking
|
|
137 */
|
|
138
|
4
|
139 if (*fmt == '%') {
|
|
140
|
10
|
141 i64 = 0;
|
|
142 ui64 = 0;
|
|
143
|
|
144 zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
|
4
|
145 width = 0;
|
|
146 sign = 1;
|
|
147 hexadecimal = 0;
|
42
|
148 max_width = 0;
|
4
|
149
|
26
|
150 p = temp + NGX_INT64_LEN;
|
4
|
151
|
|
152 while (*fmt >= '0' && *fmt <= '9') {
|
|
153 width = width * 10 + *fmt++ - '0';
|
|
154 }
|
|
155
|
|
156
|
|
157 for ( ;; ) {
|
|
158 switch (*fmt) {
|
|
159
|
|
160 case 'u':
|
|
161 sign = 0;
|
|
162 fmt++;
|
|
163 continue;
|
|
164
|
16
|
165 case 'm':
|
42
|
166 max_width = 1;
|
16
|
167 fmt++;
|
|
168 continue;
|
|
169
|
10
|
170 case 'X':
|
|
171 hexadecimal = 2;
|
|
172 sign = 0;
|
|
173 fmt++;
|
|
174 continue;
|
|
175
|
4
|
176 case 'x':
|
|
177 hexadecimal = 1;
|
10
|
178 sign = 0;
|
4
|
179 fmt++;
|
|
180 continue;
|
|
181
|
|
182 default:
|
|
183 break;
|
|
184 }
|
|
185
|
|
186 break;
|
|
187 }
|
|
188
|
|
189
|
|
190 switch (*fmt) {
|
|
191
|
10
|
192 case 'V':
|
328
|
193 v = va_arg(args, ngx_str_t *);
|
4
|
194
|
320
|
195 len = v->len;
|
122
|
196 len = (buf + len < last) ? len : (size_t) (last - buf);
|
|
197
|
320
|
198 buf = ngx_cpymem(buf, v->data, len);
|
10
|
199 fmt++;
|
4
|
200
|
10
|
201 continue;
|
4
|
202
|
328
|
203 case 'v':
|
|
204 vv = va_arg(args, ngx_variable_value_t *);
|
|
205
|
|
206 len = vv->len;
|
|
207 len = (buf + len < last) ? len : (size_t) (last - buf);
|
|
208
|
|
209 buf = ngx_cpymem(buf, vv->data, len);
|
|
210 fmt++;
|
|
211
|
|
212 continue;
|
|
213
|
4
|
214 case 's':
|
10
|
215 p = va_arg(args, u_char *);
|
4
|
216
|
10
|
217 while (*p && buf < last) {
|
4
|
218 *buf++ = *p++;
|
|
219 }
|
|
220 fmt++;
|
|
221
|
|
222 continue;
|
|
223
|
10
|
224 case 'O':
|
|
225 i64 = (int64_t) va_arg(args, off_t);
|
|
226 sign = 1;
|
|
227 break;
|
|
228
|
|
229 case 'P':
|
|
230 i64 = (int64_t) va_arg(args, ngx_pid_t);
|
|
231 sign = 1;
|
|
232 break;
|
|
233
|
|
234 case 'T':
|
|
235 i64 = (int64_t) va_arg(args, time_t);
|
|
236 sign = 1;
|
|
237 break;
|
|
238
|
106
|
239 case 'M':
|
110
|
240 ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
|
|
241 if ((ngx_msec_int_t) ms == -1) {
|
|
242 sign = 1;
|
|
243 i64 = -1;
|
|
244 } else {
|
|
245 sign = 0;
|
|
246 ui64 = (uint64_t) ms;
|
|
247 }
|
106
|
248 break;
|
|
249
|
10
|
250 case 'z':
|
|
251 if (sign) {
|
|
252 i64 = (int64_t) va_arg(args, ssize_t);
|
|
253 } else {
|
|
254 ui64 = (uint64_t) va_arg(args, size_t);
|
|
255 }
|
|
256 break;
|
|
257
|
|
258 case 'i':
|
|
259 if (sign) {
|
|
260 i64 = (int64_t) va_arg(args, ngx_int_t);
|
|
261 } else {
|
|
262 ui64 = (uint64_t) va_arg(args, ngx_uint_t);
|
|
263 }
|
42
|
264
|
|
265 if (max_width) {
|
|
266 width = NGX_INT_T_LEN;
|
|
267 }
|
|
268
|
10
|
269 break;
|
|
270
|
|
271 case 'd':
|
|
272 if (sign) {
|
|
273 i64 = (int64_t) va_arg(args, int);
|
|
274 } else {
|
|
275 ui64 = (uint64_t) va_arg(args, u_int);
|
|
276 }
|
|
277 break;
|
|
278
|
|
279 case 'l':
|
|
280 if (sign) {
|
|
281 i64 = (int64_t) va_arg(args, long);
|
|
282 } else {
|
|
283 ui64 = (uint64_t) va_arg(args, u_long);
|
|
284 }
|
|
285 break;
|
|
286
|
|
287 case 'D':
|
|
288 if (sign) {
|
|
289 i64 = (int64_t) va_arg(args, int32_t);
|
|
290 } else {
|
|
291 ui64 = (uint64_t) va_arg(args, uint32_t);
|
|
292 }
|
|
293 break;
|
|
294
|
|
295 case 'L':
|
|
296 if (sign) {
|
|
297 i64 = va_arg(args, int64_t);
|
|
298 } else {
|
|
299 ui64 = va_arg(args, uint64_t);
|
|
300 }
|
|
301 break;
|
|
302
|
42
|
303 case 'A':
|
|
304 if (sign) {
|
|
305 i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
|
|
306 } else {
|
44
|
307 ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
|
42
|
308 }
|
|
309
|
|
310 if (max_width) {
|
|
311 width = NGX_ATOMIC_T_LEN;
|
|
312 }
|
|
313
|
|
314 break;
|
|
315
|
10
|
316 #if !(NGX_WIN32)
|
|
317 case 'r':
|
|
318 i64 = (int64_t) va_arg(args, rlim_t);
|
|
319 sign = 1;
|
|
320 break;
|
|
321 #endif
|
|
322
|
|
323 case 'p':
|
|
324 ui64 = (uintptr_t) va_arg(args, void *);
|
|
325 hexadecimal = 2;
|
|
326 sign = 0;
|
|
327 zero = '0';
|
162
|
328 width = NGX_PTR_SIZE * 2;
|
10
|
329 break;
|
|
330
|
6
|
331 case 'c':
|
10
|
332 d = va_arg(args, int);
|
6
|
333 *buf++ = (u_char) (d & 0xff);
|
|
334 fmt++;
|
|
335
|
|
336 continue;
|
|
337
|
10
|
338 case 'Z':
|
|
339 *buf++ = '\0';
|
|
340 fmt++;
|
|
341
|
|
342 continue;
|
|
343
|
74
|
344 case 'N':
|
|
345 #if (NGX_WIN32)
|
|
346 *buf++ = CR;
|
|
347 #endif
|
|
348 *buf++ = LF;
|
|
349 fmt++;
|
|
350
|
|
351 continue;
|
|
352
|
4
|
353 case '%':
|
|
354 *buf++ = '%';
|
|
355 fmt++;
|
|
356
|
|
357 continue;
|
|
358
|
|
359 default:
|
|
360 *buf++ = *fmt++;
|
|
361
|
|
362 continue;
|
|
363 }
|
|
364
|
10
|
365 if (sign) {
|
|
366 if (i64 < 0) {
|
|
367 *buf++ = '-';
|
|
368 ui64 = (uint64_t) -i64;
|
|
369
|
|
370 } else {
|
|
371 ui64 = (uint64_t) i64;
|
|
372 }
|
|
373 }
|
|
374
|
|
375 if (hexadecimal == 1) {
|
|
376 do {
|
|
377
|
|
378 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
379 *--p = hex[(uint32_t) (ui64 & 0xf)];
|
|
380
|
|
381 } while (ui64 >>= 4);
|
|
382
|
|
383 } else if (hexadecimal == 2) {
|
|
384 do {
|
|
385
|
|
386 /* the "(uint32_t)" cast disables the BCC's warning */
|
|
387 *--p = HEX[(uint32_t) (ui64 & 0xf)];
|
|
388
|
|
389 } while (ui64 >>= 4);
|
|
390
|
|
391 } else if (ui64 <= NGX_MAX_UINT32_VALUE) {
|
|
392
|
|
393 /*
|
|
394 * To divide 64-bit number and to find the remainder
|
|
395 * on the x86 platform gcc and icc call the libc functions
|
|
396 * [u]divdi3() and [u]moddi3(), they call another function
|
42
|
397 * in its turn. On FreeBSD it is the qdivrem() function,
|
10
|
398 * its source code is about 170 lines of the code.
|
|
399 * The glibc counterpart is about 150 lines of the code.
|
|
400 *
|
42
|
401 * For 32-bit numbers and some divisors gcc and icc use
|
|
402 * the inlined multiplication and shifts. For example,
|
|
403 * unsigned "i32 / 10" is compiled to
|
|
404 *
|
|
405 * (i32 * 0xCCCCCCCD) >> 35
|
10
|
406 */
|
|
407
|
|
408 ui32 = (uint32_t) ui64;
|
|
409
|
|
410 do {
|
|
411 *--p = (u_char) (ui32 % 10 + '0');
|
|
412 } while (ui32 /= 10);
|
|
413
|
|
414 } else {
|
|
415 do {
|
|
416 *--p = (u_char) (ui64 % 10 + '0');
|
|
417 } while (ui64 /= 10);
|
|
418 }
|
|
419
|
26
|
420 len = (temp + NGX_INT64_LEN) - p;
|
4
|
421
|
10
|
422 while (len++ < width && buf < last) {
|
|
423 *buf++ = zero;
|
4
|
424 }
|
|
425
|
26
|
426 len = (temp + NGX_INT64_LEN) - p;
|
10
|
427 if (buf + len > last) {
|
|
428 len = last - buf;
|
|
429 }
|
|
430
|
|
431 buf = ngx_cpymem(buf, p, len);
|
4
|
432
|
|
433 fmt++;
|
|
434
|
|
435 } else {
|
|
436 *buf++ = *fmt++;
|
|
437 }
|
|
438 }
|
|
439
|
|
440 return buf;
|
|
441 }
|
|
442
|
|
443
|
286
|
444 /*
|
|
445 * We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII string only,
|
|
446 * and implement our own ngx_strcasecmp()/ngx_strncasecmp()
|
|
447 * to avoid libc locale overhead. Besides, we use the ngx_uint_t's
|
|
448 * instead of the u_char's, because they are slightly faster.
|
|
449 */
|
|
450
|
|
451 ngx_int_t
|
|
452 ngx_strcasecmp(u_char *s1, u_char *s2)
|
|
453 {
|
|
454 ngx_uint_t c1, c2;
|
|
455
|
|
456 for ( ;; ) {
|
|
457 c1 = (ngx_uint_t) *s1++;
|
|
458 c2 = (ngx_uint_t) *s2++;
|
|
459
|
|
460 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
461 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
|
462
|
|
463 if (c1 == c2) {
|
|
464
|
|
465 if (c1) {
|
|
466 continue;
|
|
467 }
|
|
468
|
|
469 return 0;
|
|
470 }
|
|
471
|
|
472 return c1 - c2;
|
|
473 }
|
|
474 }
|
|
475
|
|
476
|
|
477 ngx_int_t
|
|
478 ngx_strncasecmp(u_char *s1, u_char *s2, size_t n)
|
|
479 {
|
|
480 ngx_uint_t c1, c2;
|
|
481
|
|
482 while (n) {
|
|
483 c1 = (ngx_uint_t) *s1++;
|
|
484 c2 = (ngx_uint_t) *s2++;
|
|
485
|
|
486 c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
487 c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
|
488
|
|
489 if (c1 == c2) {
|
|
490
|
|
491 if (c1) {
|
|
492 n--;
|
|
493 continue;
|
|
494 }
|
|
495
|
|
496 return 0;
|
|
497 }
|
|
498
|
|
499 return c1 - c2;
|
|
500 }
|
|
501
|
|
502 return 0;
|
|
503 }
|
|
504
|
|
505
|
42
|
506 ngx_int_t
|
|
507 ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
|
0
|
508 {
|
|
509 if (n == 0) {
|
|
510 return 0;
|
|
511 }
|
|
512
|
|
513 n--;
|
|
514
|
|
515 for ( ;; ) {
|
|
516 if (s1[n] != s2[n]) {
|
|
517 return s1[n] - s2[n];
|
|
518 }
|
|
519
|
|
520 if (n == 0) {
|
|
521 return 0;
|
|
522 }
|
|
523
|
|
524 n--;
|
|
525 }
|
|
526 }
|
|
527
|
|
528
|
42
|
529 ngx_int_t
|
|
530 ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
|
4
|
531 {
|
|
532 u_char c1, c2;
|
|
533
|
|
534 if (n == 0) {
|
|
535 return 0;
|
|
536 }
|
|
537
|
|
538 n--;
|
|
539
|
|
540 for ( ;; ) {
|
|
541 c1 = s1[n];
|
|
542 if (c1 >= 'a' && c1 <= 'z') {
|
|
543 c1 -= 'a' - 'A';
|
|
544 }
|
|
545
|
|
546 c2 = s2[n];
|
|
547 if (c2 >= 'a' && c2 <= 'z') {
|
|
548 c2 -= 'a' - 'A';
|
|
549 }
|
|
550
|
|
551 if (c1 != c2) {
|
|
552 return c1 - c2;
|
|
553 }
|
|
554
|
|
555 if (n == 0) {
|
|
556 return 0;
|
|
557 }
|
|
558
|
|
559 n--;
|
|
560 }
|
|
561 }
|
|
562
|
|
563
|
42
|
564 ngx_int_t
|
274
|
565 ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2)
|
|
566 {
|
|
567 size_t n;
|
|
568 ngx_int_t m, z;
|
|
569
|
|
570 if (n1 <= n2) {
|
|
571 n = n1;
|
|
572 z = -1;
|
|
573
|
|
574 } else {
|
|
575 n = n2;
|
|
576 z = 1;
|
|
577 }
|
|
578
|
|
579 m = ngx_memcmp(s1, s2, n);
|
|
580
|
|
581 if (m || n1 == n2) {
|
|
582 return m;
|
|
583 }
|
|
584
|
|
585 return z;
|
|
586 }
|
|
587
|
|
588
|
|
589 ngx_int_t
|
42
|
590 ngx_atoi(u_char *line, size_t n)
|
0
|
591 {
|
|
592 ngx_int_t value;
|
|
593
|
|
594 if (n == 0) {
|
|
595 return NGX_ERROR;
|
|
596 }
|
|
597
|
|
598 for (value = 0; n--; line++) {
|
|
599 if (*line < '0' || *line > '9') {
|
|
600 return NGX_ERROR;
|
|
601 }
|
|
602
|
|
603 value = value * 10 + (*line - '0');
|
|
604 }
|
|
605
|
|
606 if (value < 0) {
|
|
607 return NGX_ERROR;
|
|
608
|
|
609 } else {
|
|
610 return value;
|
|
611 }
|
|
612 }
|
|
613
|
|
614
|
50
|
615 ssize_t
|
|
616 ngx_atosz(u_char *line, size_t n)
|
|
617 {
|
|
618 ssize_t value;
|
|
619
|
|
620 if (n == 0) {
|
|
621 return NGX_ERROR;
|
|
622 }
|
|
623
|
|
624 for (value = 0; n--; line++) {
|
|
625 if (*line < '0' || *line > '9') {
|
|
626 return NGX_ERROR;
|
|
627 }
|
|
628
|
|
629 value = value * 10 + (*line - '0');
|
|
630 }
|
|
631
|
|
632 if (value < 0) {
|
|
633 return NGX_ERROR;
|
|
634
|
|
635 } else {
|
|
636 return value;
|
|
637 }
|
|
638 }
|
|
639
|
|
640
|
|
641 off_t
|
|
642 ngx_atoof(u_char *line, size_t n)
|
|
643 {
|
|
644 off_t value;
|
|
645
|
|
646 if (n == 0) {
|
|
647 return NGX_ERROR;
|
|
648 }
|
|
649
|
|
650 for (value = 0; n--; line++) {
|
|
651 if (*line < '0' || *line > '9') {
|
|
652 return NGX_ERROR;
|
|
653 }
|
|
654
|
|
655 value = value * 10 + (*line - '0');
|
|
656 }
|
|
657
|
|
658 if (value < 0) {
|
|
659 return NGX_ERROR;
|
|
660
|
|
661 } else {
|
|
662 return value;
|
|
663 }
|
|
664 }
|
|
665
|
|
666
|
|
667 time_t
|
|
668 ngx_atotm(u_char *line, size_t n)
|
|
669 {
|
|
670 time_t value;
|
|
671
|
|
672 if (n == 0) {
|
|
673 return NGX_ERROR;
|
|
674 }
|
|
675
|
|
676 for (value = 0; n--; line++) {
|
|
677 if (*line < '0' || *line > '9') {
|
|
678 return NGX_ERROR;
|
|
679 }
|
|
680
|
|
681 value = value * 10 + (*line - '0');
|
|
682 }
|
|
683
|
|
684 if (value < 0) {
|
|
685 return NGX_ERROR;
|
|
686
|
|
687 } else {
|
|
688 return value;
|
|
689 }
|
|
690 }
|
|
691
|
|
692
|
42
|
693 ngx_int_t
|
|
694 ngx_hextoi(u_char *line, size_t n)
|
0
|
695 {
|
250
|
696 u_char c, ch;
|
0
|
697 ngx_int_t value;
|
|
698
|
|
699 if (n == 0) {
|
|
700 return NGX_ERROR;
|
|
701 }
|
|
702
|
|
703 for (value = 0; n--; line++) {
|
|
704 ch = *line;
|
|
705
|
|
706 if (ch >= '0' && ch <= '9') {
|
|
707 value = value * 16 + (ch - '0');
|
|
708 continue;
|
|
709 }
|
|
710
|
250
|
711 c = (u_char) (ch | 0x20);
|
0
|
712
|
250
|
713 if (c >= 'a' && c <= 'f') {
|
|
714 value = value * 16 + (c - 'a' + 10);
|
0
|
715 continue;
|
|
716 }
|
|
717
|
|
718 return NGX_ERROR;
|
|
719 }
|
|
720
|
|
721 if (value < 0) {
|
|
722 return NGX_ERROR;
|
|
723
|
|
724 } else {
|
|
725 return value;
|
|
726 }
|
|
727 }
|
|
728
|
|
729
|
42
|
730 void
|
|
731 ngx_md5_text(u_char *text, u_char *md5)
|
0
|
732 {
|
|
733 int i;
|
|
734 static u_char hex[] = "0123456789abcdef";
|
|
735
|
|
736 for (i = 0; i < 16; i++) {
|
|
737 *text++ = hex[md5[i] >> 4];
|
|
738 *text++ = hex[md5[i] & 0xf];
|
|
739 }
|
|
740
|
|
741 *text = '\0';
|
|
742 }
|
|
743
|
|
744
|
42
|
745 void
|
|
746 ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
747 {
|
|
748 u_char *d, *s;
|
|
749 size_t len;
|
|
750 static u_char basis64[] =
|
|
751 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
752
|
|
753 len = src->len;
|
|
754 s = src->data;
|
|
755 d = dst->data;
|
|
756
|
|
757 while (len > 2) {
|
|
758 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
759 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
760 *d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
|
|
761 *d++ = basis64[s[2] & 0x3f];
|
|
762
|
|
763 s += 3;
|
|
764 len -= 3;
|
|
765 }
|
|
766
|
|
767 if (len) {
|
|
768 *d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
769
|
|
770 if (len == 1) {
|
|
771 *d++ = basis64[(s[0] & 3) << 4];
|
|
772 *d++ = '=';
|
|
773
|
|
774 } else {
|
|
775 *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
776 *d++ = basis64[(s[1] & 0x0f) << 2];
|
|
777 }
|
|
778
|
|
779 *d++ = '=';
|
|
780 }
|
|
781
|
|
782 dst->len = d - dst->data;
|
|
783 }
|
|
784
|
|
785
|
42
|
786 ngx_int_t
|
|
787 ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
|
0
|
788 {
|
|
789 size_t len;
|
|
790 u_char *d, *s;
|
254
|
791 static u_char basis64[] = {
|
|
792 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
793 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
794 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
|
|
795 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
796 77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
797 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
|
|
798 77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
799 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
0
|
800
|
254
|
801 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
802 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
803 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
804 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
805 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
806 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
807 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
808 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
|
|
809 };
|
0
|
810
|
|
811 for (len = 0; len < src->len; len++) {
|
|
812 if (src->data[len] == '=') {
|
|
813 break;
|
|
814 }
|
|
815
|
|
816 if (basis64[src->data[len]] == 77) {
|
|
817 return NGX_ERROR;
|
|
818 }
|
|
819 }
|
|
820
|
|
821 if (len % 4 == 1) {
|
|
822 return NGX_ERROR;
|
|
823 }
|
|
824
|
|
825 s = src->data;
|
|
826 d = dst->data;
|
|
827
|
|
828 while (len > 3) {
|
|
829 *d++ = (u_char) (basis64[s[0]] << 2 | basis64[s[1]] >> 4);
|
|
830 *d++ = (u_char) (basis64[s[1]] << 4 | basis64[s[2]] >> 2);
|
|
831 *d++ = (u_char) (basis64[s[2]] << 6 | basis64[s[3]]);
|
|
832
|
|
833 s += 4;
|
|
834 len -= 4;
|
|
835 }
|
|
836
|
|
837 if (len > 1) {
|
|
838 *d++ = (u_char) (basis64[s[0]] << 2 | basis64[s[1]] >> 4);
|
|
839 }
|
|
840
|
|
841 if (len > 2) {
|
|
842 *d++ = (u_char) (basis64[s[1]] << 4 | basis64[s[2]] >> 2);
|
|
843 }
|
|
844
|
|
845 dst->len = d - dst->data;
|
|
846
|
|
847 return NGX_OK;
|
|
848 }
|
|
849
|
|
850
|
206
|
851 /*
|
|
852 * ngx_utf_decode() decodes two and more bytes UTF sequences only
|
|
853 * the return values:
|
|
854 * 0x80 - 0x10ffff valid character
|
|
855 * 0x10ffff - 0xfffffffd invalid sequence
|
|
856 * 0xfffffffe incomplete sequence
|
|
857 * 0xffffffff error
|
|
858 */
|
|
859
|
|
860 uint32_t
|
|
861 ngx_utf_decode(u_char **p, size_t n)
|
|
862 {
|
|
863 size_t len;
|
|
864 uint32_t u, i, valid;
|
|
865
|
|
866 u = **p;
|
|
867
|
|
868 if (u > 0xf0) {
|
|
869
|
|
870 u &= 0x07;
|
|
871 valid = 0xffff;
|
|
872 len = 3;
|
|
873
|
|
874 } else if (u > 0xe0) {
|
|
875
|
|
876 u &= 0x0f;
|
|
877 valid = 0x7ff;
|
|
878 len = 2;
|
|
879
|
|
880 } else if (u > 0xc0) {
|
|
881
|
|
882 u &= 0x1f;
|
|
883 valid = 0x7f;
|
|
884 len = 1;
|
|
885
|
|
886 } else {
|
|
887 (*p)++;
|
|
888 return 0xffffffff;
|
|
889 }
|
|
890
|
|
891 if (n - 1 < len) {
|
|
892 return 0xfffffffe;
|
|
893 }
|
|
894
|
|
895 (*p)++;
|
|
896
|
|
897 while (len) {
|
|
898 i = *(*p)++;
|
|
899
|
|
900 if (i < 0x80) {
|
|
901 return 0xffffffff;
|
|
902 }
|
|
903
|
|
904 u = (u << 6) | (i & 0x3f);
|
|
905
|
|
906 len--;
|
|
907 }
|
|
908
|
|
909 if (u > valid) {
|
|
910 return u;
|
|
911 }
|
|
912
|
|
913 return 0xffffffff;
|
|
914 }
|
|
915
|
|
916
|
72
|
917 size_t
|
206
|
918 ngx_utf_length(u_char *p, size_t n)
|
72
|
919 {
|
|
920 u_char c;
|
|
921 size_t len;
|
|
922 ngx_uint_t i;
|
|
923
|
206
|
924 for (len = 0, i = 0; i < n; len++, i++) {
|
72
|
925
|
206
|
926 c = p[i];
|
72
|
927
|
|
928 if (c < 0x80) {
|
|
929 continue;
|
|
930 }
|
|
931
|
76
|
932 if (c >= 0xc0) {
|
|
933 for (c <<= 1; c & 0x80; c <<= 1) {
|
|
934 i++;
|
|
935 }
|
|
936
|
|
937 continue;
|
72
|
938 }
|
|
939
|
76
|
940 /* invalid utf */
|
|
941
|
206
|
942 return n;
|
72
|
943 }
|
|
944
|
|
945 return len;
|
|
946 }
|
|
947
|
|
948
|
76
|
949 u_char *
|
|
950 ngx_utf_cpystrn(u_char *dst, u_char *src, size_t n)
|
|
951 {
|
|
952 u_char c;
|
|
953
|
|
954 if (n == 0) {
|
|
955 return dst;
|
|
956 }
|
|
957
|
|
958 for ( /* void */ ; --n; dst++, src++) {
|
|
959
|
|
960 c = *src;
|
|
961 *dst = c;
|
|
962
|
|
963 if (c < 0x80) {
|
|
964 if (*dst != '\0') {
|
|
965 continue;
|
|
966 }
|
|
967
|
|
968 return dst;
|
|
969 }
|
|
970
|
|
971 if (c >= 0xc0) {
|
|
972 for (c <<= 1; c & 0x80; c <<= 1) {
|
|
973 *++dst = *++src;
|
|
974 }
|
|
975
|
|
976 continue;
|
|
977 }
|
|
978
|
|
979 /* invalid utf */
|
|
980 }
|
|
981
|
|
982 *dst = '\0';
|
|
983
|
|
984 return dst;
|
|
985 }
|
|
986
|
|
987
|
42
|
988 uintptr_t
|
|
989 ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
|
0
|
990 {
|
254
|
991 ngx_uint_t i, n;
|
|
992 uint32_t *escape;
|
|
993 static u_char hex[] = "0123456789abcdef";
|
10
|
994
|
254
|
995 /* " ", "#", "%", "?", %00-%1F, %7F-%FF */
|
34
|
996
|
254
|
997 static uint32_t uri[] = {
|
|
998 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
0
|
999
|
254
|
1000 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
1001 0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
|
|
1002
|
|
1003 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1004 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
2
|
1005
|
254
|
1006 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1007 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
0
|
1008
|
254
|
1009 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1010 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1011 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1012 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1013 };
|
2
|
1014
|
254
|
1015 /* " ", "#", "%", "+", "?", %00-%1F, %7F-%FF */
|
34
|
1016
|
254
|
1017 static uint32_t args[] = {
|
|
1018 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1019
|
|
1020 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
1021 0x80000829, /* 1000 0000 0000 0000 0000 1000 0010 1001 */
|
34
|
1022
|
254
|
1023 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1024 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
34
|
1025
|
254
|
1026 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1027 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
34
|
1028
|
254
|
1029 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1030 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1031 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1032 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1033 };
|
34
|
1034
|
328
|
1035 /* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */
|
34
|
1036
|
254
|
1037 static uint32_t html[] = {
|
|
1038 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
10
|
1039
|
254
|
1040 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
320
|
1041 0x000000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */
|
254
|
1042
|
|
1043 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1044 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
10
|
1045
|
254
|
1046 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1047 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
10
|
1048
|
254
|
1049 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1050 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1051 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1052 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1053 };
|
10
|
1054
|
328
|
1055 /* " ", """, "%", "'", %00-%1F, %7F-%FF */
|
10
|
1056
|
320
|
1057 static uint32_t refresh[] = {
|
|
1058 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1059
|
|
1060 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
328
|
1061 0x000000a5, /* 0000 0000 0000 0000 0000 0000 1010 0101 */
|
320
|
1062
|
|
1063 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1064 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1065
|
|
1066 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1067 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1068
|
|
1069 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1070 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1071 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1072 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1073 };
|
|
1074
|
328
|
1075 /* " ", "%", %00-%1F */
|
322
|
1076
|
|
1077 static uint32_t memcached[] = {
|
|
1078 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
1079
|
|
1080 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
328
|
1081 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
|
322
|
1082
|
|
1083 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
1084 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1085
|
|
1086 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
1087 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1088
|
|
1089 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1090 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1091 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1092 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
1093 };
|
|
1094
|
328
|
1095 /* mail_auth is the same as memcached */
|
|
1096
|
|
1097 static uint32_t *map[] =
|
|
1098 { uri, args, html, refresh, memcached, memcached };
|
320
|
1099
|
|
1100
|
|
1101 escape = map[type];
|
10
|
1102
|
2
|
1103 if (dst == NULL) {
|
|
1104
|
|
1105 /* find the number of the characters to be escaped */
|
0
|
1106
|
2
|
1107 n = 0;
|
|
1108
|
|
1109 for (i = 0; i < size; i++) {
|
|
1110 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
1111 n++;
|
|
1112 }
|
|
1113 src++;
|
|
1114 }
|
|
1115
|
34
|
1116 return (uintptr_t) n;
|
0
|
1117 }
|
|
1118
|
2
|
1119 for (i = 0; i < size; i++) {
|
|
1120 if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
1121 *dst++ = '%';
|
|
1122 *dst++ = hex[*src >> 4];
|
|
1123 *dst++ = hex[*src & 0xf];
|
|
1124 src++;
|
0
|
1125
|
2
|
1126 } else {
|
|
1127 *dst++ = *src++;
|
|
1128 }
|
0
|
1129 }
|
|
1130
|
34
|
1131 return (uintptr_t) dst;
|
0
|
1132 }
|
126
|
1133
|
|
1134
|
|
1135 void
|
150
|
1136 ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type)
|
126
|
1137 {
|
|
1138 u_char *d, *s, ch, c, decoded;
|
|
1139 enum {
|
|
1140 sw_usual = 0,
|
|
1141 sw_quoted,
|
|
1142 sw_quoted_second
|
|
1143 } state;
|
|
1144
|
|
1145 d = *dst;
|
|
1146 s = *src;
|
|
1147
|
|
1148 state = 0;
|
|
1149 decoded = 0;
|
|
1150
|
|
1151 while (size--) {
|
|
1152
|
|
1153 ch = *s++;
|
|
1154
|
|
1155 switch (state) {
|
|
1156 case sw_usual:
|
150
|
1157 if (ch == '?' && type == NGX_UNESCAPE_URI) {
|
126
|
1158 *d++ = ch;
|
|
1159 goto done;
|
|
1160 }
|
|
1161
|
|
1162 if (ch == '%') {
|
|
1163 state = sw_quoted;
|
|
1164 break;
|
|
1165 }
|
|
1166
|
|
1167 *d++ = ch;
|
|
1168 break;
|
|
1169
|
|
1170 case sw_quoted:
|
|
1171
|
|
1172 if (ch >= '0' && ch <= '9') {
|
|
1173 decoded = (u_char) (ch - '0');
|
|
1174 state = sw_quoted_second;
|
|
1175 break;
|
|
1176 }
|
|
1177
|
|
1178 c = (u_char) (ch | 0x20);
|
|
1179 if (c >= 'a' && c <= 'f') {
|
|
1180 decoded = (u_char) (c - 'a' + 10);
|
|
1181 state = sw_quoted_second;
|
|
1182 break;
|
|
1183 }
|
|
1184
|
138
|
1185 /* the invalid quoted character */
|
126
|
1186
|
138
|
1187 state = sw_usual;
|
|
1188
|
|
1189 *d++ = ch;
|
126
|
1190
|
|
1191 break;
|
|
1192
|
|
1193 case sw_quoted_second:
|
|
1194
|
138
|
1195 state = sw_usual;
|
|
1196
|
126
|
1197 if (ch >= '0' && ch <= '9') {
|
|
1198 ch = (u_char) ((decoded << 4) + ch - '0');
|
|
1199
|
150
|
1200 if (type == NGX_UNESCAPE_URI) {
|
|
1201 if (ch > '%' && ch < 0x7f) {
|
|
1202 *d++ = ch;
|
|
1203 break;
|
|
1204 }
|
|
1205
|
|
1206 *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
|
1207
|
126
|
1208 break;
|
|
1209 }
|
|
1210
|
150
|
1211 *d++ = ch;
|
126
|
1212
|
|
1213 break;
|
|
1214 }
|
|
1215
|
|
1216 c = (u_char) (ch | 0x20);
|
|
1217 if (c >= 'a' && c <= 'f') {
|
|
1218 ch = (u_char) ((decoded << 4) + c - 'a' + 10);
|
|
1219
|
150
|
1220 if (type == NGX_UNESCAPE_URI) {
|
|
1221 if (ch == '?') {
|
|
1222 *d++ = ch;
|
|
1223 goto done;
|
|
1224 }
|
126
|
1225
|
150
|
1226 if (ch > '%' && ch < 0x7f) {
|
|
1227 *d++ = ch;
|
|
1228 break;
|
|
1229 }
|
|
1230
|
|
1231 *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
126
|
1232 break;
|
|
1233 }
|
|
1234
|
150
|
1235 *d++ = ch;
|
126
|
1236
|
|
1237 break;
|
|
1238 }
|
|
1239
|
138
|
1240 /* the invalid quoted character */
|
126
|
1241
|
|
1242 break;
|
|
1243 }
|
|
1244 }
|
|
1245
|
|
1246 done:
|
|
1247
|
|
1248 *dst = d;
|
|
1249 *src = s;
|
|
1250 }
|
132
|
1251
|
|
1252
|
302
|
1253 /* ngx_sort() is implemented as insertion sort because we need stable sort */
|
|
1254
|
|
1255 void
|
|
1256 ngx_sort(void *base, size_t n, size_t size,
|
|
1257 int (*cmp)(const void *, const void *))
|
|
1258 {
|
|
1259 u_char *p1, *p2;
|
|
1260 u_char buf[256];
|
|
1261
|
|
1262 for (p1 = (u_char *) base + size;
|
|
1263 p1 < (u_char *) base + n * size;
|
|
1264 p1 += size)
|
|
1265 {
|
|
1266 ngx_memcpy(buf, p1, size);
|
|
1267
|
|
1268 for (p2 = p1;
|
|
1269 p2 > (u_char *) base && cmp(p2 - size, buf) > 0;
|
|
1270 p2 -= size)
|
|
1271 {
|
|
1272 ngx_memcpy(p2, p2 - size, size);
|
|
1273 }
|
|
1274
|
|
1275 ngx_memcpy(p2, buf, size);
|
|
1276 }
|
|
1277 }
|
|
1278
|
|
1279
|
132
|
1280 #if (NGX_MEMCPY_LIMIT)
|
|
1281
|
|
1282 void *
|
|
1283 ngx_memcpy(void *dst, void *src, size_t n)
|
|
1284 {
|
|
1285 if (n > NGX_MEMCPY_LIMIT) {
|
|
1286 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "memcpy %uz bytes", n);
|
|
1287 ngx_debug_point();
|
|
1288 }
|
|
1289
|
|
1290 return memcpy(dst, src, n);
|
|
1291 }
|
|
1292
|
|
1293 #endif
|