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