comparison src/core/ngx_string.c @ 42:41ccba1aba45 NGINX_0_1_21

nginx 0.1.21 *) Bugfix: the ngx_http_stub_status_module showed incorrect statistics if "rtsig" method was used or if several worker process ran on SMP. *) Bugfix: nginx could not be built by the icc compiler on Linux or if the zlib-1.2.x library was building from sources. *) Bugfix: nginx could not be built on NetBSD 2.0.
author Igor Sysoev <http://sysoev.ru>
date Tue, 22 Feb 2005 00:00:00 +0300
parents aab2ea7c0458
children 4989c3d25945
comparison
equal deleted inserted replaced
41:4d8e7a81b3a0 42:41ccba1aba45
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 9
10 10
11 u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n) 11 u_char *
12 ngx_cpystrn(u_char *dst, u_char *src, size_t n)
12 { 13 {
13 if (n == 0) { 14 if (n == 0) {
14 return dst; 15 return dst;
15 } 16 }
16 17
26 27
27 return dst; 28 return dst;
28 } 29 }
29 30
30 31
31 u_char *ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src) 32 u_char *
33 ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
32 { 34 {
33 u_char *dst; 35 u_char *dst;
34 36
35 if (!(dst = ngx_palloc(pool, src->len))) { 37 if (!(dst = ngx_palloc(pool, src->len))) {
36 return NULL; 38 return NULL;
50 * %[0][width][u][x|X]d int/u_int 52 * %[0][width][u][x|X]d int/u_int
51 * %[0][width][u][x|X]l long 53 * %[0][width][u][x|X]l long
52 * %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t 54 * %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t
53 * %[0][width][u][x|X]D int32_t/uint32_t 55 * %[0][width][u][x|X]D int32_t/uint32_t
54 * %[0][width][u][x|X]L int64_t/uint64_t 56 * %[0][width][u][x|X]L int64_t/uint64_t
57 * %[0][width|m][u][x|X]A ngx_atomic_int_t
55 * %P ngx_pid_t 58 * %P ngx_pid_t
56 * %r rlim_t 59 * %r rlim_t
57 * %p pointer 60 * %p pointer
58 * %V pointer to ngx_str_t 61 * %V pointer to ngx_str_t
59 * %s null-terminated string 62 * %s null-terminated string
61 * %c char 64 * %c char
62 * %% % 65 * %% %
63 * 66 *
64 * TODO: 67 * TODO:
65 * %M ngx_msec_t 68 * %M ngx_msec_t
66 * %A ngx_atomic_t
67 * 69 *
68 * reserved: 70 * reserved:
69 * %t ptrdiff_t 71 * %t ptrdiff_t
70 * %S null-teminated wchar string 72 * %S null-teminated wchar string
71 * %C wchar 73 * %C wchar
72 */ 74 */
73 75
74 76
75 u_char *ngx_sprintf(u_char *buf, const char *fmt, ...) 77 u_char *
78 ngx_sprintf(u_char *buf, const char *fmt, ...)
76 { 79 {
77 u_char *p; 80 u_char *p;
78 va_list args; 81 va_list args;
79 82
80 va_start(args, fmt); 83 va_start(args, fmt);
83 86
84 return p; 87 return p;
85 } 88 }
86 89
87 90
88 u_char *ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...) 91 u_char *
92 ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
89 { 93 {
90 u_char *p; 94 u_char *p;
91 va_list args; 95 va_list args;
92 96
93 va_start(args, fmt); 97 va_start(args, fmt);
96 100
97 return p; 101 return p;
98 } 102 }
99 103
100 104
101 u_char *ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args) 105 u_char *
106 ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
102 { 107 {
103 u_char *p, zero, *last, temp[NGX_INT64_LEN + 1]; 108 u_char *p, zero, *last, temp[NGX_INT64_LEN + 1];
104 /* 109 /*
105 * really we need temp[NGX_INT64_LEN] only, 110 * really we need temp[NGX_INT64_LEN] only,
106 * but icc shows the warning 111 * but icc issues the warning
107 */ 112 */
108 int d; 113 int d;
109 size_t len; 114 size_t len;
110 uint32_t ui32; 115 uint32_t ui32;
111 int64_t i64; 116 int64_t i64;
112 uint64_t ui64; 117 uint64_t ui64;
113 ngx_str_t *s; 118 ngx_str_t *s;
114 ngx_uint_t width, sign, hexadecimal; 119 ngx_uint_t width, sign, hexadecimal, max_width;
115 static u_char hex[] = "0123456789abcdef"; 120 static u_char hex[] = "0123456789abcdef";
116 static u_char HEX[] = "0123456789ABCDEF"; 121 static u_char HEX[] = "0123456789ABCDEF";
117 122
118 if (max == 0) { 123 if (max == 0) {
119 return buf; 124 return buf;
135 140
136 zero = (u_char) ((*++fmt == '0') ? '0' : ' '); 141 zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
137 width = 0; 142 width = 0;
138 sign = 1; 143 sign = 1;
139 hexadecimal = 0; 144 hexadecimal = 0;
145 max_width = 0;
140 146
141 p = temp + NGX_INT64_LEN; 147 p = temp + NGX_INT64_LEN;
142 148
143 while (*fmt >= '0' && *fmt <= '9') { 149 while (*fmt >= '0' && *fmt <= '9') {
144 width = width * 10 + *fmt++ - '0'; 150 width = width * 10 + *fmt++ - '0';
152 sign = 0; 158 sign = 0;
153 fmt++; 159 fmt++;
154 continue; 160 continue;
155 161
156 case 'm': 162 case 'm':
157 width = NGX_INT_T_LEN; 163 max_width = 1;
158 fmt++; 164 fmt++;
159 continue; 165 continue;
160 166
161 case 'X': 167 case 'X':
162 hexadecimal = 2; 168 hexadecimal = 2;
226 if (sign) { 232 if (sign) {
227 i64 = (int64_t) va_arg(args, ngx_int_t); 233 i64 = (int64_t) va_arg(args, ngx_int_t);
228 } else { 234 } else {
229 ui64 = (uint64_t) va_arg(args, ngx_uint_t); 235 ui64 = (uint64_t) va_arg(args, ngx_uint_t);
230 } 236 }
237
238 if (max_width) {
239 width = NGX_INT_T_LEN;
240 }
241
231 break; 242 break;
232 243
233 case 'd': 244 case 'd':
234 if (sign) { 245 if (sign) {
235 i64 = (int64_t) va_arg(args, int); 246 i64 = (int64_t) va_arg(args, int);
258 if (sign) { 269 if (sign) {
259 i64 = va_arg(args, int64_t); 270 i64 = va_arg(args, int64_t);
260 } else { 271 } else {
261 ui64 = va_arg(args, uint64_t); 272 ui64 = va_arg(args, uint64_t);
262 } 273 }
274 break;
275
276 case 'A':
277 if (sign) {
278 i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
279 } else {
280 ui64 = (uint64_t) va_arg(args, ngx_atomic_int_t);
281 }
282
283 if (max_width) {
284 width = NGX_ATOMIC_T_LEN;
285 }
286
263 break; 287 break;
264 288
265 #if !(NGX_WIN32) 289 #if !(NGX_WIN32)
266 case 'r': 290 case 'r':
267 i64 = (int64_t) va_arg(args, rlim_t); 291 i64 = (int64_t) va_arg(args, rlim_t);
332 356
333 /* 357 /*
334 * To divide 64-bit number and to find the remainder 358 * To divide 64-bit number and to find the remainder
335 * on the x86 platform gcc and icc call the libc functions 359 * on the x86 platform gcc and icc call the libc functions
336 * [u]divdi3() and [u]moddi3(), they call another function 360 * [u]divdi3() and [u]moddi3(), they call another function
337 * in return. On FreeBSD it is the qdivrem() function, 361 * in its turn. On FreeBSD it is the qdivrem() function,
338 * its source code is about 170 lines of the code. 362 * its source code is about 170 lines of the code.
339 * The glibc counterpart is about 150 lines of the code. 363 * The glibc counterpart is about 150 lines of the code.
340 * 364 *
341 * For 32-bit numbers gcc and icc use the inlined 365 * For 32-bit numbers and some divisors gcc and icc use
342 * multiplication and shifts. For example, unsigned 366 * the inlined multiplication and shifts. For example,
343 * "i32 / 10" is compiled to "(i32 * 0xCCCCCCCD) >> 35". 367 * unsigned "i32 / 10" is compiled to
368 *
369 * (i32 * 0xCCCCCCCD) >> 35
344 */ 370 */
345 371
346 ui32 = (uint32_t) ui64; 372 ui32 = (uint32_t) ui64;
347 373
348 do { 374 do {
377 403
378 return buf; 404 return buf;
379 } 405 }
380 406
381 407
382 ngx_int_t ngx_rstrncmp(u_char *s1, u_char *s2, size_t n) 408 ngx_int_t
409 ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
383 { 410 {
384 if (n == 0) { 411 if (n == 0) {
385 return 0; 412 return 0;
386 } 413 }
387 414
399 n--; 426 n--;
400 } 427 }
401 } 428 }
402 429
403 430
404 ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n) 431 ngx_int_t
432 ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
405 { 433 {
406 u_char c1, c2; 434 u_char c1, c2;
407 435
408 if (n == 0) { 436 if (n == 0) {
409 return 0; 437 return 0;
433 n--; 461 n--;
434 } 462 }
435 } 463 }
436 464
437 465
438 ngx_int_t ngx_atoi(u_char *line, size_t n) 466 ngx_int_t
467 ngx_atoi(u_char *line, size_t n)
439 { 468 {
440 ngx_int_t value; 469 ngx_int_t value;
441 470
442 if (n == 0) { 471 if (n == 0) {
443 return NGX_ERROR; 472 return NGX_ERROR;
458 return value; 487 return value;
459 } 488 }
460 } 489 }
461 490
462 491
463 ngx_int_t ngx_hextoi(u_char *line, size_t n) 492 ngx_int_t
493 ngx_hextoi(u_char *line, size_t n)
464 { 494 {
465 u_char ch; 495 u_char ch;
466 ngx_int_t value; 496 ngx_int_t value;
467 497
468 if (n == 0) { 498 if (n == 0) {
497 return value; 527 return value;
498 } 528 }
499 } 529 }
500 530
501 531
502 void ngx_md5_text(u_char *text, u_char *md5) 532 void
533 ngx_md5_text(u_char *text, u_char *md5)
503 { 534 {
504 int i; 535 int i;
505 static u_char hex[] = "0123456789abcdef"; 536 static u_char hex[] = "0123456789abcdef";
506 537
507 for (i = 0; i < 16; i++) { 538 for (i = 0; i < 16; i++) {
511 542
512 *text = '\0'; 543 *text = '\0';
513 } 544 }
514 545
515 546
516 void ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src) 547 void
548 ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
517 { 549 {
518 u_char *d, *s; 550 u_char *d, *s;
519 size_t len; 551 size_t len;
520 static u_char basis64[] = 552 static u_char basis64[] =
521 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 553 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
551 583
552 dst->len = d - dst->data; 584 dst->len = d - dst->data;
553 } 585 }
554 586
555 587
556 ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src) 588 ngx_int_t
589 ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
557 { 590 {
558 size_t len; 591 size_t len;
559 u_char *d, *s; 592 u_char *d, *s;
560 static u_char basis64[] = 593 static u_char basis64[] =
561 { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 594 { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
614 647
615 return NGX_OK; 648 return NGX_OK;
616 } 649 }
617 650
618 651
619 uintptr_t ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type) 652 uintptr_t
653 ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
620 { 654 {
621 ngx_uint_t i, n; 655 ngx_uint_t i, n;
622 uint32_t *escape; 656 uint32_t *escape;
623 static u_char hex[] = "0123456789abcdef"; 657 static u_char hex[] = "0123456789abcdef";
624 658
625 /* " ", "%", "?", %00-%1F, %7F-%FF */ 659 /* " ", "#", "%", "?", %00-%1F, %7F-%FF */
626 660
627 static uint32_t uri[] = 661 static uint32_t uri[] =
628 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 662 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
629 663
630 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 664 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
631 0x80000021, /* 1000 0000 0000 0000 0000 0000 0010 0001 */ 665 0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
632 666
633 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 667 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
634 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 668 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
635 669
636 /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 670 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
639 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 673 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
640 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 674 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
641 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 675 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
642 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; 676 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ };
643 677
644 /* " ", "%", "+", "?", %00-%1F, %7F-%FF */ 678 /* " ", "#", "%", "+", "?", %00-%1F, %7F-%FF */
645 679
646 static uint32_t args[] = 680 static uint32_t args[] =
647 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 681 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
648 682
649 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 683 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
650 0x80000821, /* 1000 0000 0000 0000 0000 1000 0010 0001 */ 684 0x80000829, /* 1000 0000 0000 0000 0000 1000 0010 1001 */
651 685
652 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 686 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
653 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 687 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
654 688
655 /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 689 /* ~}| {zyx wvut srqp onml kjih gfed cba` */
664 698
665 static uint32_t html[] = 699 static uint32_t html[] =
666 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 700 { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
667 701
668 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 702 /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
669 0x80000021, /* 0000 0000 0000 0000 0000 0000 1010 0101 */ 703 0x800000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */
670 704
671 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 705 /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
672 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 706 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
673 707
674 /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 708 /* ~}| {zyx wvut srqp onml kjih gfed cba` */