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