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