2002-08-07 00:39:45 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_config.h>
|
2003-03-21 00:09:44 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
static u_char *ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64,
|
|
|
|
u_char zero, ngx_uint_t hexadecimal, ngx_uint_t width);
|
2010-09-02 22:37:16 +08:00
|
|
|
static ngx_int_t ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src,
|
|
|
|
const u_char *basis);
|
2008-11-10 23:20:59 +08:00
|
|
|
|
|
|
|
|
2008-08-04 18:07:00 +08:00
|
|
|
void
|
|
|
|
ngx_strlow(u_char *dst, u_char *src, size_t n)
|
|
|
|
{
|
2009-11-03 01:12:09 +08:00
|
|
|
while (n) {
|
2008-08-04 18:07:00 +08:00
|
|
|
*dst = ngx_tolower(*src);
|
|
|
|
dst++;
|
|
|
|
src++;
|
2009-11-03 01:12:09 +08:00
|
|
|
n--;
|
2008-08-04 18:07:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
u_char *
|
|
|
|
ngx_cpystrn(u_char *dst, u_char *src, size_t n)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-05-07 01:03:16 +08:00
|
|
|
if (n == 0) {
|
2002-08-07 00:39:45 +08:00
|
|
|
return dst;
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2009-06-05 20:33:49 +08:00
|
|
|
while (--n) {
|
2002-08-07 00:39:45 +08:00
|
|
|
*dst = *src;
|
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
if (*dst == '\0') {
|
2002-08-07 00:39:45 +08:00
|
|
|
return dst;
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
2009-06-05 20:33:49 +08:00
|
|
|
|
|
|
|
dst++;
|
|
|
|
src++;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
u_char *
|
|
|
|
ngx_pstrdup(ngx_pool_t *pool, ngx_str_t *src)
|
2005-02-04 03:33:37 +08:00
|
|
|
{
|
|
|
|
u_char *dst;
|
|
|
|
|
2008-06-17 23:00:30 +08:00
|
|
|
dst = ngx_pnalloc(pool, src->len);
|
2005-03-19 20:38:37 +08:00
|
|
|
if (dst == NULL) {
|
2005-02-04 03:33:37 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(dst, src->data, src->len);
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
/*
|
|
|
|
* supported formats:
|
2004-12-03 02:40:46 +08:00
|
|
|
* %[0][width][x][X]O off_t
|
2004-11-11 22:07:14 +08:00
|
|
|
* %[0][width]T time_t
|
|
|
|
* %[0][width][u][x|X]z ssize_t/size_t
|
|
|
|
* %[0][width][u][x|X]d int/u_int
|
|
|
|
* %[0][width][u][x|X]l long
|
|
|
|
* %[0][width|m][u][x|X]i ngx_int_t/ngx_uint_t
|
|
|
|
* %[0][width][u][x|X]D int32_t/uint32_t
|
|
|
|
* %[0][width][u][x|X]L int64_t/uint64_t
|
2005-02-24 20:29:09 +08:00
|
|
|
* %[0][width|m][u][x|X]A ngx_atomic_int_t/ngx_atomic_uint_t
|
2010-05-12 21:12:31 +08:00
|
|
|
* %[0][width][.width]f double, max valid number fits to %18.15f
|
2004-11-11 22:07:14 +08:00
|
|
|
* %P ngx_pid_t
|
2005-10-07 21:30:52 +08:00
|
|
|
* %M ngx_msec_t
|
2004-11-11 22:07:14 +08:00
|
|
|
* %r rlim_t
|
2007-08-20 17:57:19 +08:00
|
|
|
* %p void *
|
|
|
|
* %V ngx_str_t *
|
|
|
|
* %v ngx_variable_value_t *
|
2004-11-11 22:07:14 +08:00
|
|
|
* %s null-terminated string
|
2007-12-25 01:05:16 +08:00
|
|
|
* %*s length and string
|
2004-11-11 22:07:14 +08:00
|
|
|
* %Z '\0'
|
2005-06-23 21:41:06 +08:00
|
|
|
* %N '\n'
|
2004-11-11 22:07:14 +08:00
|
|
|
* %c char
|
|
|
|
* %% %
|
2004-10-21 23:34:38 +08:00
|
|
|
*
|
2004-11-11 22:07:14 +08:00
|
|
|
* reserved:
|
|
|
|
* %t ptrdiff_t
|
2009-08-26 23:03:53 +08:00
|
|
|
* %S null-terminated wchar string
|
2004-11-11 22:07:14 +08:00
|
|
|
* %C wchar
|
2004-10-21 23:34:38 +08:00
|
|
|
*/
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2005-03-23 00:02:46 +08:00
|
|
|
u_char * ngx_cdecl
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_sprintf(u_char *buf, const char *fmt, ...)
|
2004-10-21 23:34:38 +08:00
|
|
|
{
|
2004-11-11 22:07:14 +08:00
|
|
|
u_char *p;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2009-04-27 20:51:33 +08:00
|
|
|
p = ngx_vslprintf(buf, (void *) -1, fmt, args);
|
2004-11-11 22:07:14 +08:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-23 00:02:46 +08:00
|
|
|
u_char * ngx_cdecl
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
|
2004-11-11 22:07:14 +08:00
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2009-04-27 20:51:33 +08:00
|
|
|
p = ngx_vslprintf(buf, buf + max, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u_char * ngx_cdecl
|
|
|
|
ngx_slprintf(u_char *buf, u_char *last, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
p = ngx_vslprintf(buf, last, fmt, args);
|
2004-11-11 22:07:14 +08:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
u_char *
|
2009-04-27 20:51:33 +08:00
|
|
|
ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args)
|
2004-11-11 22:07:14 +08:00
|
|
|
{
|
2009-04-27 20:51:33 +08:00
|
|
|
u_char *p, zero;
|
2007-07-17 17:23:23 +08:00
|
|
|
int d;
|
2010-05-12 21:12:31 +08:00
|
|
|
double f, scale;
|
2007-12-25 01:05:16 +08:00
|
|
|
size_t len, slen;
|
2007-07-17 17:23:23 +08:00
|
|
|
int64_t i64;
|
|
|
|
uint64_t ui64;
|
|
|
|
ngx_msec_t ms;
|
2009-11-03 01:12:09 +08:00
|
|
|
ngx_uint_t width, sign, hex, max_width, frac_width, n;
|
2007-08-20 17:57:19 +08:00
|
|
|
ngx_str_t *v;
|
|
|
|
ngx_variable_value_t *vv;
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
while (*fmt && buf < last) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "buf < last" means that we could copy at least one character:
|
|
|
|
* the plain character, "%%", "%c", and minus without the checking
|
|
|
|
*/
|
2004-10-21 23:34:38 +08:00
|
|
|
|
|
|
|
if (*fmt == '%') {
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
i64 = 0;
|
|
|
|
ui64 = 0;
|
|
|
|
|
|
|
|
zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
|
2004-10-21 23:34:38 +08:00
|
|
|
width = 0;
|
|
|
|
sign = 1;
|
2008-11-10 23:20:59 +08:00
|
|
|
hex = 0;
|
2005-02-22 22:40:13 +08:00
|
|
|
max_width = 0;
|
2008-11-10 23:20:59 +08:00
|
|
|
frac_width = 0;
|
2008-01-22 23:13:01 +08:00
|
|
|
slen = (size_t) -1;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
|
|
|
while (*fmt >= '0' && *fmt <= '9') {
|
|
|
|
width = width * 10 + *fmt++ - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
switch (*fmt) {
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
sign = 0;
|
|
|
|
fmt++;
|
|
|
|
continue;
|
2004-11-21 03:52:20 +08:00
|
|
|
|
|
|
|
case 'm':
|
2005-02-22 22:40:13 +08:00
|
|
|
max_width = 1;
|
2004-11-21 03:52:20 +08:00
|
|
|
fmt++;
|
|
|
|
continue;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'X':
|
2008-11-10 23:20:59 +08:00
|
|
|
hex = 2;
|
2004-11-11 22:07:14 +08:00
|
|
|
sign = 0;
|
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
case 'x':
|
2008-11-10 23:20:59 +08:00
|
|
|
hex = 1;
|
2004-11-11 22:07:14 +08:00
|
|
|
sign = 0;
|
2004-10-21 23:34:38 +08:00
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
case '.':
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
while (*fmt >= '0' && *fmt <= '9') {
|
|
|
|
frac_width = frac_width * 10 + *fmt++ - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2007-12-25 01:05:16 +08:00
|
|
|
case '*':
|
2008-01-24 23:18:17 +08:00
|
|
|
slen = va_arg(args, size_t);
|
2007-12-25 01:05:16 +08:00
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'V':
|
2007-08-20 17:57:19 +08:00
|
|
|
v = va_arg(args, ngx_str_t *);
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2010-05-14 17:55:33 +08:00
|
|
|
len = ngx_min(((size_t) (last - buf)), v->len);
|
2007-07-17 17:23:23 +08:00
|
|
|
buf = ngx_cpymem(buf, v->data, len);
|
2004-11-11 22:07:14 +08:00
|
|
|
fmt++;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
continue;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2007-08-20 17:57:19 +08:00
|
|
|
case 'v':
|
|
|
|
vv = va_arg(args, ngx_variable_value_t *);
|
|
|
|
|
2010-05-14 17:55:33 +08:00
|
|
|
len = ngx_min(((size_t) (last - buf)), vv->len);
|
2007-08-20 17:57:19 +08:00
|
|
|
buf = ngx_cpymem(buf, vv->data, len);
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 's':
|
|
|
|
p = va_arg(args, u_char *);
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-01-22 23:13:01 +08:00
|
|
|
if (slen == (size_t) -1) {
|
2007-12-25 01:05:16 +08:00
|
|
|
while (*p && buf < last) {
|
|
|
|
*buf++ = *p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2010-05-14 17:55:33 +08:00
|
|
|
len = ngx_min(((size_t) (last - buf)), slen);
|
2008-01-24 23:18:58 +08:00
|
|
|
buf = ngx_cpymem(buf, p, len);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
2007-12-25 01:05:16 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
fmt++;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
continue;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'O':
|
|
|
|
i64 = (int64_t) va_arg(args, off_t);
|
|
|
|
sign = 1;
|
2004-10-21 23:34:38 +08:00
|
|
|
break;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'P':
|
|
|
|
i64 = (int64_t) va_arg(args, ngx_pid_t);
|
|
|
|
sign = 1;
|
|
|
|
break;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'T':
|
|
|
|
i64 = (int64_t) va_arg(args, time_t);
|
|
|
|
sign = 1;
|
|
|
|
break;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2005-10-07 21:30:52 +08:00
|
|
|
case 'M':
|
2005-10-12 21:50:36 +08:00
|
|
|
ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
|
|
|
|
if ((ngx_msec_int_t) ms == -1) {
|
|
|
|
sign = 1;
|
|
|
|
i64 = -1;
|
|
|
|
} else {
|
|
|
|
sign = 0;
|
|
|
|
ui64 = (uint64_t) ms;
|
|
|
|
}
|
2005-10-07 21:30:52 +08:00
|
|
|
break;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'z':
|
|
|
|
if (sign) {
|
|
|
|
i64 = (int64_t) va_arg(args, ssize_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
} else {
|
2004-11-11 22:07:14 +08:00
|
|
|
ui64 = (uint64_t) va_arg(args, size_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
break;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'i':
|
|
|
|
if (sign) {
|
|
|
|
i64 = (int64_t) va_arg(args, ngx_int_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
} else {
|
2004-11-11 22:07:14 +08:00
|
|
|
ui64 = (uint64_t) va_arg(args, ngx_uint_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
2005-02-22 22:40:13 +08:00
|
|
|
|
|
|
|
if (max_width) {
|
|
|
|
width = NGX_INT_T_LEN;
|
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
break;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'd':
|
|
|
|
if (sign) {
|
|
|
|
i64 = (int64_t) va_arg(args, int);
|
|
|
|
} else {
|
|
|
|
ui64 = (uint64_t) va_arg(args, u_int);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'l':
|
|
|
|
if (sign) {
|
|
|
|
i64 = (int64_t) va_arg(args, long);
|
|
|
|
} else {
|
|
|
|
ui64 = (uint64_t) va_arg(args, u_long);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'D':
|
2004-10-21 23:34:38 +08:00
|
|
|
if (sign) {
|
2004-11-11 22:07:14 +08:00
|
|
|
i64 = (int64_t) va_arg(args, int32_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
} else {
|
2004-11-11 22:07:14 +08:00
|
|
|
ui64 = (uint64_t) va_arg(args, uint32_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
break;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'L':
|
|
|
|
if (sign) {
|
|
|
|
i64 = va_arg(args, int64_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
} else {
|
2004-11-11 22:07:14 +08:00
|
|
|
ui64 = va_arg(args, uint64_t);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
case 'A':
|
|
|
|
if (sign) {
|
|
|
|
i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
|
|
|
|
} else {
|
2005-02-24 20:29:09 +08:00
|
|
|
ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
|
2005-02-22 22:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (max_width) {
|
|
|
|
width = NGX_ATOMIC_T_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
case 'f':
|
2010-05-12 21:12:31 +08:00
|
|
|
f = va_arg(args, double);
|
2008-11-10 23:20:59 +08:00
|
|
|
|
|
|
|
if (f < 0) {
|
|
|
|
*buf++ = '-';
|
|
|
|
f = -f;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui64 = (int64_t) f;
|
|
|
|
|
|
|
|
buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
|
|
|
|
|
|
|
|
if (frac_width) {
|
|
|
|
|
|
|
|
if (buf < last) {
|
|
|
|
*buf++ = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
scale = 1.0;
|
|
|
|
|
2009-11-03 01:12:09 +08:00
|
|
|
for (n = frac_width; n; n--) {
|
2008-11-10 23:20:59 +08:00
|
|
|
scale *= 10.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (int64_t) cast is required for msvc6:
|
2011-09-19 22:48:29 +08:00
|
|
|
* it cannot convert uint64_t to double
|
2008-11-10 23:20:59 +08:00
|
|
|
*/
|
2010-05-12 21:13:11 +08:00
|
|
|
ui64 = (uint64_t) ((f - (int64_t) ui64) * scale + 0.5);
|
2008-11-10 23:20:59 +08:00
|
|
|
|
|
|
|
buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
#if !(NGX_WIN32)
|
|
|
|
case 'r':
|
|
|
|
i64 = (int64_t) va_arg(args, rlim_t);
|
|
|
|
sign = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
ui64 = (uintptr_t) va_arg(args, void *);
|
2008-11-10 23:20:59 +08:00
|
|
|
hex = 2;
|
2004-11-11 22:07:14 +08:00
|
|
|
sign = 0;
|
|
|
|
zero = '0';
|
2006-02-16 23:26:46 +08:00
|
|
|
width = NGX_PTR_SIZE * 2;
|
2004-11-11 22:07:14 +08:00
|
|
|
break;
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'c':
|
|
|
|
d = va_arg(args, int);
|
|
|
|
*buf++ = (u_char) (d & 0xff);
|
2004-10-21 23:34:38 +08:00
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'Z':
|
|
|
|
*buf++ = '\0';
|
2004-10-25 23:29:23 +08:00
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2005-06-23 21:41:06 +08:00
|
|
|
case 'N':
|
|
|
|
#if (NGX_WIN32)
|
|
|
|
*buf++ = CR;
|
|
|
|
#endif
|
|
|
|
*buf++ = LF;
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
case '%':
|
|
|
|
*buf++ = '%';
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*buf++ = *fmt++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (sign) {
|
|
|
|
if (i64 < 0) {
|
|
|
|
*buf++ = '-';
|
|
|
|
ui64 = (uint64_t) -i64;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ui64 = (uint64_t) i64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
fmt++;
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} else {
|
|
|
|
*buf++ = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
return buf;
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
static u_char *
|
|
|
|
ngx_sprintf_num(u_char *buf, u_char *last, uint64_t ui64, u_char zero,
|
|
|
|
ngx_uint_t hexadecimal, ngx_uint_t width)
|
|
|
|
{
|
|
|
|
u_char *p, temp[NGX_INT64_LEN + 1];
|
|
|
|
/*
|
|
|
|
* we need temp[NGX_INT64_LEN] only,
|
|
|
|
* but icc issues the warning
|
|
|
|
*/
|
|
|
|
size_t len;
|
|
|
|
uint32_t ui32;
|
|
|
|
static u_char hex[] = "0123456789abcdef";
|
|
|
|
static u_char HEX[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
p = temp + NGX_INT64_LEN;
|
|
|
|
|
|
|
|
if (hexadecimal == 0) {
|
|
|
|
|
|
|
|
if (ui64 <= NGX_MAX_UINT32_VALUE) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To divide 64-bit numbers and to find remainders
|
|
|
|
* on the x86 platform gcc and icc call the libc functions
|
|
|
|
* [u]divdi3() and [u]moddi3(), they call another function
|
|
|
|
* in its turn. On FreeBSD it is the qdivrem() function,
|
|
|
|
* its source code is about 170 lines of the code.
|
|
|
|
* The glibc counterpart is about 150 lines of the code.
|
|
|
|
*
|
|
|
|
* For 32-bit numbers and some divisors gcc and icc use
|
|
|
|
* a inlined multiplication and shifts. For example,
|
|
|
|
* unsigned "i32 / 10" is compiled to
|
|
|
|
*
|
|
|
|
* (i32 * 0xCCCCCCCD) >> 35
|
|
|
|
*/
|
|
|
|
|
|
|
|
ui32 = (uint32_t) ui64;
|
|
|
|
|
|
|
|
do {
|
|
|
|
*--p = (u_char) (ui32 % 10 + '0');
|
|
|
|
} while (ui32 /= 10);
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
*--p = (u_char) (ui64 % 10 + '0');
|
|
|
|
} while (ui64 /= 10);
|
|
|
|
}
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} else if (hexadecimal == 1) {
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
do {
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
/* the "(uint32_t)" cast disables the BCC's warning */
|
|
|
|
*--p = hex[(uint32_t) (ui64 & 0xf)];
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} while (ui64 >>= 4);
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} else { /* hexadecimal == 2 */
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
do {
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
/* the "(uint32_t)" cast disables the BCC's warning */
|
|
|
|
*--p = HEX[(uint32_t) (ui64 & 0xf)];
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
} while (ui64 >>= 4);
|
|
|
|
}
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
/* zero or space padding */
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
len = (temp + NGX_INT64_LEN) - p;
|
|
|
|
|
|
|
|
while (len++ < width && buf < last) {
|
|
|
|
*buf++ = zero;
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
|
2008-11-10 23:20:59 +08:00
|
|
|
/* number safe copy */
|
|
|
|
|
|
|
|
len = (temp + NGX_INT64_LEN) - p;
|
|
|
|
|
|
|
|
if (buf + len > last) {
|
|
|
|
len = last - buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ngx_cpymem(buf, p, len);
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-15 02:51:19 +08:00
|
|
|
/*
|
2007-09-26 20:23:34 +08:00
|
|
|
* We use ngx_strcasecmp()/ngx_strncasecmp() for 7-bit ASCII strings only,
|
2007-02-15 02:51:19 +08:00
|
|
|
* and implement our own ngx_strcasecmp()/ngx_strncasecmp()
|
|
|
|
* to avoid libc locale overhead. Besides, we use the ngx_uint_t's
|
|
|
|
* instead of the u_char's, because they are slightly faster.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_strcasecmp(u_char *s1, u_char *s2)
|
|
|
|
{
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
c1 = (ngx_uint_t) *s1++;
|
|
|
|
c2 = (ngx_uint_t) *s2++;
|
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
|
|
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
2007-02-15 02:51:19 +08:00
|
|
|
|
|
|
|
if (c1 == c2) {
|
|
|
|
|
|
|
|
if (c1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_strncasecmp(u_char *s1, u_char *s2, size_t n)
|
|
|
|
{
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
while (n) {
|
|
|
|
c1 = (ngx_uint_t) *s1++;
|
|
|
|
c2 = (ngx_uint_t) *s2++;
|
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
|
|
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
2007-02-15 02:51:19 +08:00
|
|
|
|
|
|
|
if (c1 == c2) {
|
|
|
|
|
|
|
|
if (c1) {
|
|
|
|
n--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-01 22:48:33 +08:00
|
|
|
u_char *
|
|
|
|
ngx_strnstr(u_char *s1, char *s2, size_t len)
|
|
|
|
{
|
|
|
|
u_char c1, c2;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
c2 = *(u_char *) s2++;
|
|
|
|
|
|
|
|
n = ngx_strlen(s2);
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
if (len-- == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 = *s1++;
|
|
|
|
|
|
|
|
if (c1 == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (c1 != c2);
|
|
|
|
|
|
|
|
if (n > len) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (ngx_strncmp(s1, (u_char *) s2, n) != 0);
|
|
|
|
|
|
|
|
return --s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-01 21:00:30 +08:00
|
|
|
/*
|
|
|
|
* ngx_strstrn() and ngx_strcasestrn() are intended to search for static
|
|
|
|
* substring with known length in null-terminated string. The argument n
|
|
|
|
* must be length of the second substring - 1.
|
|
|
|
*/
|
|
|
|
|
2007-09-27 03:25:52 +08:00
|
|
|
u_char *
|
|
|
|
ngx_strstrn(u_char *s1, char *s2, size_t n)
|
|
|
|
{
|
|
|
|
u_char c1, c2;
|
|
|
|
|
|
|
|
c2 = *(u_char *) s2++;
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
c1 = *s1++;
|
|
|
|
|
|
|
|
if (c1 == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (c1 != c2);
|
|
|
|
|
|
|
|
} while (ngx_strncmp(s1, (u_char *) s2, n) != 0);
|
|
|
|
|
|
|
|
return --s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
ngx_strcasestrn(u_char *s1, char *s2, size_t n)
|
|
|
|
{
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
c2 = (ngx_uint_t) *s2++;
|
2009-09-11 21:57:50 +08:00
|
|
|
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
2007-09-27 03:25:52 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
c1 = (ngx_uint_t) *s1++;
|
|
|
|
|
|
|
|
if (c1 == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
2007-09-27 03:25:52 +08:00
|
|
|
|
|
|
|
} while (c1 != c2);
|
|
|
|
|
|
|
|
} while (ngx_strncasecmp(s1, (u_char *) s2, n) != 0);
|
|
|
|
|
|
|
|
return --s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-05 01:31:54 +08:00
|
|
|
/*
|
|
|
|
* ngx_strlcasestrn() is intended to search for static substring
|
|
|
|
* with known length in string until the argument last. The argument n
|
|
|
|
* must be length of the second substring - 1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
u_char *
|
|
|
|
ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n)
|
|
|
|
{
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
c2 = (ngx_uint_t) *s2++;
|
2009-09-11 21:57:50 +08:00
|
|
|
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
2009-04-05 01:31:54 +08:00
|
|
|
last -= n;
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
2009-04-06 19:42:42 +08:00
|
|
|
if (s1 >= last) {
|
2009-04-05 01:31:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 = (ngx_uint_t) *s1++;
|
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
2009-04-05 01:31:54 +08:00
|
|
|
|
|
|
|
} while (c1 != c2);
|
|
|
|
|
|
|
|
} while (ngx_strncasecmp(s1, s2, n) != 0);
|
|
|
|
|
|
|
|
return --s1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_rstrncmp(u_char *s1, u_char *s2, size_t n)
|
2003-05-07 01:03:16 +08:00
|
|
|
{
|
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n--;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
if (s1[n] != s2[n]) {
|
2004-03-16 15:10:12 +08:00
|
|
|
return s1[n] - s2[n];
|
2003-05-07 01:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
|
2004-10-21 23:34:38 +08:00
|
|
|
{
|
|
|
|
u_char c1, c2;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n--;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
c1 = s1[n];
|
|
|
|
if (c1 >= 'a' && c1 <= 'z') {
|
|
|
|
c1 -= 'a' - 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 = s2[n];
|
|
|
|
if (c2 >= 'a' && c2 <= 'z') {
|
|
|
|
c2 -= 'a' - 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c1 != c2) {
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 04:15:59 +08:00
|
|
|
ngx_int_t
|
2007-01-13 05:58:02 +08:00
|
|
|
ngx_memn2cmp(u_char *s1, u_char *s2, size_t n1, size_t n2)
|
2007-01-13 04:15:59 +08:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
ngx_int_t m, z;
|
|
|
|
|
|
|
|
if (n1 <= n2) {
|
|
|
|
n = n1;
|
|
|
|
z = -1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
n = n2;
|
|
|
|
z = 1;
|
|
|
|
}
|
|
|
|
|
2007-01-13 05:58:02 +08:00
|
|
|
m = ngx_memcmp(s1, s2, n);
|
2007-01-13 04:15:59 +08:00
|
|
|
|
|
|
|
if (m || n1 == n2) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-12 17:28:37 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_dns_strcmp(u_char *s1, u_char *s2)
|
|
|
|
{
|
|
|
|
ngx_uint_t c1, c2;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
c1 = (ngx_uint_t) *s1++;
|
|
|
|
c2 = (ngx_uint_t) *s2++;
|
|
|
|
|
|
|
|
c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;
|
|
|
|
c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
|
|
|
|
|
|
|
|
if (c1 == c2) {
|
|
|
|
|
|
|
|
if (c1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in ASCII '.' > '-', but we need '.' to be the lowest character */
|
|
|
|
|
|
|
|
c1 = (c1 == '.') ? ' ' : c1;
|
|
|
|
c2 = (c2 == '.') ? ' ' : c2;
|
|
|
|
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_atoi(u_char *line, size_t n)
|
2003-03-21 00:09:44 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_int_t value;
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2003-05-07 01:03:16 +08:00
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-03-21 00:09:44 +08:00
|
|
|
for (value = 0; n--; line++) {
|
2003-04-28 23:06:39 +08:00
|
|
|
if (*line < '0' || *line > '9') {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2003-04-28 23:06:39 +08:00
|
|
|
value = value * 10 + (*line - '0');
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|
|
|
|
|
2003-04-28 23:06:39 +08:00
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
2004-05-21 01:33:52 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-14 17:01:30 +08:00
|
|
|
/* parse a fixed point number, e.g., ngx_atofp("10.5", 4, 2) returns 1050 */
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_atofp(u_char *line, size_t n, size_t point)
|
|
|
|
{
|
|
|
|
ngx_int_t value;
|
|
|
|
ngx_uint_t dot;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = 0;
|
|
|
|
|
|
|
|
for (value = 0; n--; line++) {
|
|
|
|
|
|
|
|
if (point == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*line == '.') {
|
|
|
|
if (dot) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*line < '0' || *line > '9') {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value * 10 + (*line - '0');
|
|
|
|
point -= dot;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (point--) {
|
|
|
|
value = value * 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
ssize_t
|
|
|
|
ngx_atosz(u_char *line, size_t n)
|
|
|
|
{
|
|
|
|
ssize_t value;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (value = 0; n--; line++) {
|
|
|
|
if (*line < '0' || *line > '9') {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value * 10 + (*line - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
off_t
|
|
|
|
ngx_atoof(u_char *line, size_t n)
|
|
|
|
{
|
|
|
|
off_t value;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (value = 0; n--; line++) {
|
|
|
|
if (*line < '0' || *line > '9') {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value * 10 + (*line - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t
|
|
|
|
ngx_atotm(u_char *line, size_t n)
|
|
|
|
{
|
|
|
|
time_t value;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (value = 0; n--; line++) {
|
|
|
|
if (*line < '0' || *line > '9') {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value * 10 + (*line - '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_hextoi(u_char *line, size_t n)
|
2004-05-21 01:33:52 +08:00
|
|
|
{
|
2006-10-16 20:21:17 +08:00
|
|
|
u_char c, ch;
|
2004-05-21 01:33:52 +08:00
|
|
|
ngx_int_t value;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (value = 0; n--; line++) {
|
|
|
|
ch = *line;
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
value = value * 16 + (ch - '0');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-16 20:21:17 +08:00
|
|
|
c = (u_char) (ch | 0x20);
|
2004-05-21 01:33:52 +08:00
|
|
|
|
2006-10-16 20:21:17 +08:00
|
|
|
if (c >= 'a' && c <= 'f') {
|
|
|
|
value = value * 16 + (c - 'a' + 10);
|
2004-05-21 01:33:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
2003-04-28 23:06:39 +08:00
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-18 05:06:17 +08:00
|
|
|
u_char *
|
|
|
|
ngx_hex_dump(u_char *dst, u_char *src, size_t len)
|
2003-11-05 06:12:39 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
static u_char hex[] = "0123456789abcdef";
|
2003-11-06 01:03:41 +08:00
|
|
|
|
2007-12-18 05:06:17 +08:00
|
|
|
while (len--) {
|
|
|
|
*dst++ = hex[*src >> 4];
|
|
|
|
*dst++ = hex[*src++ & 0xf];
|
2003-11-10 04:03:38 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 05:06:17 +08:00
|
|
|
return dst;
|
2003-11-05 06:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
void
|
|
|
|
ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
|
2004-07-31 01:05:14 +08:00
|
|
|
{
|
|
|
|
u_char *d, *s;
|
2004-08-27 23:40:59 +08:00
|
|
|
size_t len;
|
2004-07-31 01:05:14 +08:00
|
|
|
static u_char basis64[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
len = src->len;
|
2004-08-29 11:55:41 +08:00
|
|
|
s = src->data;
|
|
|
|
d = dst->data;
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
while (len > 2) {
|
|
|
|
*d++ = basis64[(s[0] >> 2) & 0x3f];
|
|
|
|
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
|
|
*d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
|
|
|
|
*d++ = basis64[s[2] & 0x3f];
|
|
|
|
|
|
|
|
s += 3;
|
|
|
|
len -= 3;
|
2004-07-31 01:05:14 +08:00
|
|
|
}
|
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
if (len) {
|
|
|
|
*d++ = basis64[(s[0] >> 2) & 0x3f];
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
if (len == 1) {
|
|
|
|
*d++ = basis64[(s[0] & 3) << 4];
|
2004-07-31 01:05:14 +08:00
|
|
|
*d++ = '=';
|
|
|
|
|
|
|
|
} else {
|
2004-08-27 23:40:59 +08:00
|
|
|
*d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
|
|
|
|
*d++ = basis64[(s[1] & 0x0f) << 2];
|
2004-07-31 01:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = '=';
|
|
|
|
}
|
|
|
|
|
|
|
|
dst->len = d - dst->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
|
2004-07-31 01:05:14 +08:00
|
|
|
{
|
2006-10-28 22:36:44 +08:00
|
|
|
static u_char basis64[] = {
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
|
|
|
|
77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
|
|
|
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
|
|
|
|
};
|
2004-08-27 23:40:59 +08:00
|
|
|
|
2010-09-02 22:37:16 +08:00
|
|
|
return ngx_decode_base64_internal(dst, src, basis64);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_decode_base64url(ngx_str_t *dst, ngx_str_t *src)
|
|
|
|
{
|
|
|
|
static u_char basis64[] = {
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77,
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 63,
|
|
|
|
77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
|
|
|
|
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
|
|
|
|
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
|
|
|
|
};
|
|
|
|
|
|
|
|
return ngx_decode_base64_internal(dst, src, basis64);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_decode_base64_internal(ngx_str_t *dst, ngx_str_t *src, const u_char *basis)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
u_char *d, *s;
|
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
for (len = 0; len < src->len; len++) {
|
|
|
|
if (src->data[len] == '=') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-02 22:37:16 +08:00
|
|
|
if (basis[src->data[len]] == 77) {
|
2004-08-27 23:40:59 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len % 4 == 1) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
s = src->data;
|
2004-08-29 11:55:41 +08:00
|
|
|
d = dst->data;
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
while (len > 3) {
|
2010-09-02 22:37:16 +08:00
|
|
|
*d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
|
|
|
|
*d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
|
|
|
|
*d++ = (u_char) (basis[s[2]] << 6 | basis[s[3]]);
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
s += 4;
|
|
|
|
len -= 4;
|
|
|
|
}
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
if (len > 1) {
|
2010-09-02 22:37:16 +08:00
|
|
|
*d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
|
2004-08-27 23:40:59 +08:00
|
|
|
}
|
2004-07-31 01:05:14 +08:00
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
if (len > 2) {
|
2010-09-02 22:37:16 +08:00
|
|
|
*d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
|
2004-07-31 01:05:14 +08:00
|
|
|
}
|
|
|
|
|
2004-08-27 23:40:59 +08:00
|
|
|
dst->len = d - dst->data;
|
|
|
|
|
2004-07-31 01:05:14 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-29 00:00:26 +08:00
|
|
|
/*
|
2008-07-29 22:41:34 +08:00
|
|
|
* ngx_utf8_decode() decodes two and more bytes UTF sequences only
|
2006-06-29 00:00:26 +08:00
|
|
|
* the return values:
|
|
|
|
* 0x80 - 0x10ffff valid character
|
2008-07-25 22:29:05 +08:00
|
|
|
* 0x110000 - 0xfffffffd invalid sequence
|
2006-06-29 00:00:26 +08:00
|
|
|
* 0xfffffffe incomplete sequence
|
|
|
|
* 0xffffffff error
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint32_t
|
2008-07-29 22:41:34 +08:00
|
|
|
ngx_utf8_decode(u_char **p, size_t n)
|
2006-06-29 00:00:26 +08:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
uint32_t u, i, valid;
|
|
|
|
|
|
|
|
u = **p;
|
|
|
|
|
2011-10-13 21:56:41 +08:00
|
|
|
if (u >= 0xf0) {
|
2006-06-29 00:00:26 +08:00
|
|
|
|
|
|
|
u &= 0x07;
|
|
|
|
valid = 0xffff;
|
|
|
|
len = 3;
|
|
|
|
|
2011-10-13 21:56:41 +08:00
|
|
|
} else if (u >= 0xe0) {
|
2006-06-29 00:00:26 +08:00
|
|
|
|
|
|
|
u &= 0x0f;
|
|
|
|
valid = 0x7ff;
|
|
|
|
len = 2;
|
|
|
|
|
2011-10-13 21:56:41 +08:00
|
|
|
} else if (u >= 0xc2) {
|
2006-06-29 00:00:26 +08:00
|
|
|
|
|
|
|
u &= 0x1f;
|
|
|
|
valid = 0x7f;
|
|
|
|
len = 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
(*p)++;
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n - 1 < len) {
|
|
|
|
return 0xfffffffe;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*p)++;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
i = *(*p)++;
|
|
|
|
|
|
|
|
if (i < 0x80) {
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
u = (u << 6) | (i & 0x3f);
|
|
|
|
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u > valid) {
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-16 02:33:41 +08:00
|
|
|
size_t
|
2008-07-29 22:41:34 +08:00
|
|
|
ngx_utf8_length(u_char *p, size_t n)
|
2005-06-16 02:33:41 +08:00
|
|
|
{
|
2008-07-25 22:29:05 +08:00
|
|
|
u_char c, *last;
|
|
|
|
size_t len;
|
2005-06-16 02:33:41 +08:00
|
|
|
|
2008-07-25 22:29:05 +08:00
|
|
|
last = p + n;
|
2005-06-16 02:33:41 +08:00
|
|
|
|
2008-07-25 22:29:05 +08:00
|
|
|
for (len = 0; p < last; len++) {
|
|
|
|
|
|
|
|
c = *p;
|
2005-06-16 02:33:41 +08:00
|
|
|
|
|
|
|
if (c < 0x80) {
|
2008-07-25 22:29:05 +08:00
|
|
|
p++;
|
2005-06-16 02:33:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-07-29 22:41:34 +08:00
|
|
|
if (ngx_utf8_decode(&p, n) > 0x10ffff) {
|
|
|
|
/* invalid UTF-8 */
|
2008-07-25 22:29:05 +08:00
|
|
|
return n;
|
2005-06-16 02:33:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
u_char *
|
2008-07-29 22:41:34 +08:00
|
|
|
ngx_utf8_cpystrn(u_char *dst, u_char *src, size_t n, size_t len)
|
2005-07-08 22:34:20 +08:00
|
|
|
{
|
2008-07-25 22:29:05 +08:00
|
|
|
u_char c, *next;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-07-25 22:29:05 +08:00
|
|
|
while (--n) {
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
c = *src;
|
|
|
|
*dst = c;
|
|
|
|
|
|
|
|
if (c < 0x80) {
|
2008-07-25 22:29:05 +08:00
|
|
|
|
|
|
|
if (c != '\0') {
|
|
|
|
dst++;
|
|
|
|
src++;
|
|
|
|
len--;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2008-07-25 22:29:05 +08:00
|
|
|
next = src;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2008-07-29 22:41:34 +08:00
|
|
|
if (ngx_utf8_decode(&next, len) > 0x10ffff) {
|
|
|
|
/* invalid UTF-8 */
|
2008-07-25 22:29:05 +08:00
|
|
|
break;
|
2005-07-08 22:34:20 +08:00
|
|
|
}
|
|
|
|
|
2008-07-25 22:29:05 +08:00
|
|
|
while (src < next) {
|
2010-01-11 21:39:24 +08:00
|
|
|
*dst++ = *src++;
|
2008-07-25 22:29:05 +08:00
|
|
|
len--;
|
|
|
|
}
|
2005-07-08 22:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
uintptr_t
|
|
|
|
ngx_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
|
2003-03-21 00:09:44 +08:00
|
|
|
{
|
2009-11-03 01:12:09 +08:00
|
|
|
ngx_uint_t n;
|
2006-10-28 22:36:44 +08:00
|
|
|
uint32_t *escape;
|
|
|
|
static u_char hex[] = "0123456789abcdef";
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* " ", "#", "%", "?", %00-%1F, %7F-%FF */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
static uint32_t uri[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
|
|
0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
};
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2010-04-01 20:45:59 +08:00
|
|
|
/* " ", "#", "%", "&", "+", "?", %00-%1F, %7F-%FF */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
static uint32_t args[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
2010-07-30 21:25:19 +08:00
|
|
|
0x88000869, /* 1000 1000 0000 0000 0000 1000 0110 1001 */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2011-10-12 01:56:51 +08:00
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* not ALPHA, DIGIT, "-", ".", "_", "~" */
|
|
|
|
|
|
|
|
static uint32_t uri_component[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
|
|
0xfc009fff, /* 1111 1100 0000 0000 1001 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */
|
|
|
|
|
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0xb8000001, /* 1011 1000 0000 0000 0000 0000 0000 0001 */
|
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
};
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2007-08-20 17:50:53 +08:00
|
|
|
/* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */
|
2005-02-04 03:33:37 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
static uint32_t html[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
2007-07-13 17:35:51 +08:00
|
|
|
0x000000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2006-10-28 22:36:44 +08:00
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
};
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2007-08-20 17:50:53 +08:00
|
|
|
/* " ", """, "%", "'", %00-%1F, %7F-%FF */
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2007-07-13 17:37:01 +08:00
|
|
|
static uint32_t refresh[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
2007-11-09 21:17:58 +08:00
|
|
|
0x00000085, /* 0000 0000 0000 0000 0000 0000 1000 0101 */
|
2007-07-13 17:37:01 +08:00
|
|
|
|
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
|
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
};
|
|
|
|
|
2007-08-20 17:50:53 +08:00
|
|
|
/* " ", "%", %00-%1F */
|
2007-07-23 03:18:59 +08:00
|
|
|
|
|
|
|
static uint32_t memcached[] = {
|
|
|
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
2007-08-20 17:50:53 +08:00
|
|
|
0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */
|
2007-07-23 03:18:59 +08:00
|
|
|
|
|
|
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
|
|
|
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
|
|
|
|
};
|
|
|
|
|
2007-08-20 17:50:53 +08:00
|
|
|
/* mail_auth is the same as memcached */
|
|
|
|
|
|
|
|
static uint32_t *map[] =
|
2011-10-12 01:56:51 +08:00
|
|
|
{ uri, args, uri_component, html, refresh, memcached, memcached };
|
2007-07-13 17:37:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
escape = map[type];
|
2004-11-11 22:07:14 +08:00
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
if (dst == NULL) {
|
|
|
|
|
|
|
|
/* find the number of the characters to be escaped */
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2009-09-11 21:57:50 +08:00
|
|
|
n = 0;
|
2004-10-11 23:07:03 +08:00
|
|
|
|
2009-11-03 01:12:09 +08:00
|
|
|
while (size) {
|
2004-10-11 23:07:03 +08:00
|
|
|
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
src++;
|
2009-11-03 01:12:09 +08:00
|
|
|
size--;
|
2004-10-11 23:07:03 +08:00
|
|
|
}
|
|
|
|
|
2005-02-04 03:33:37 +08:00
|
|
|
return (uintptr_t) n;
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|
|
|
|
|
2009-11-03 01:12:09 +08:00
|
|
|
while (size) {
|
2004-10-11 23:07:03 +08:00
|
|
|
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
|
|
*dst++ = '%';
|
|
|
|
*dst++ = hex[*src >> 4];
|
|
|
|
*dst++ = hex[*src & 0xf];
|
|
|
|
src++;
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
} else {
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
2009-11-03 01:12:09 +08:00
|
|
|
size--;
|
2004-10-11 23:07:03 +08:00
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2005-02-04 03:33:37 +08:00
|
|
|
return (uintptr_t) dst;
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2006-01-18 04:04:32 +08:00
|
|
|
ngx_unescape_uri(u_char **dst, u_char **src, size_t size, ngx_uint_t type)
|
2005-11-15 21:30:52 +08:00
|
|
|
{
|
|
|
|
u_char *d, *s, ch, c, decoded;
|
|
|
|
enum {
|
|
|
|
sw_usual = 0,
|
|
|
|
sw_quoted,
|
|
|
|
sw_quoted_second
|
|
|
|
} state;
|
|
|
|
|
|
|
|
d = *dst;
|
|
|
|
s = *src;
|
|
|
|
|
|
|
|
state = 0;
|
|
|
|
decoded = 0;
|
|
|
|
|
|
|
|
while (size--) {
|
|
|
|
|
|
|
|
ch = *s++;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case sw_usual:
|
2007-10-22 18:19:17 +08:00
|
|
|
if (ch == '?'
|
|
|
|
&& (type & (NGX_UNESCAPE_URI|NGX_UNESCAPE_REDIRECT)))
|
|
|
|
{
|
2005-11-15 21:30:52 +08:00
|
|
|
*d++ = ch;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '%') {
|
|
|
|
state = sw_quoted;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = ch;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sw_quoted:
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
decoded = (u_char) (ch - '0');
|
|
|
|
state = sw_quoted_second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = (u_char) (ch | 0x20);
|
|
|
|
if (c >= 'a' && c <= 'f') {
|
|
|
|
decoded = (u_char) (c - 'a' + 10);
|
|
|
|
state = sw_quoted_second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
/* the invalid quoted character */
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
state = sw_usual;
|
|
|
|
|
|
|
|
*d++ = ch;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sw_quoted_second:
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
state = sw_usual;
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
ch = (u_char) ((decoded << 4) + ch - '0');
|
|
|
|
|
2007-10-22 18:19:17 +08:00
|
|
|
if (type & NGX_UNESCAPE_REDIRECT) {
|
2006-01-18 04:04:32 +08:00
|
|
|
if (ch > '%' && ch < 0x7f) {
|
|
|
|
*d++ = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-18 04:04:32 +08:00
|
|
|
*d++ = ch;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = (u_char) (ch | 0x20);
|
|
|
|
if (c >= 'a' && c <= 'f') {
|
|
|
|
ch = (u_char) ((decoded << 4) + c - 'a' + 10);
|
|
|
|
|
2007-10-22 18:19:17 +08:00
|
|
|
if (type & NGX_UNESCAPE_URI) {
|
|
|
|
if (ch == '?') {
|
|
|
|
*d++ = ch;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d++ = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type & NGX_UNESCAPE_REDIRECT) {
|
2006-01-18 04:04:32 +08:00
|
|
|
if (ch == '?') {
|
|
|
|
*d++ = ch;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch > '%' && ch < 0x7f) {
|
|
|
|
*d++ = ch;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2006-01-18 04:04:32 +08:00
|
|
|
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
|
2005-11-15 21:30:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-18 04:04:32 +08:00
|
|
|
*d++ = ch;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-16 23:07:08 +08:00
|
|
|
/* the invalid quoted character */
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
*dst = d;
|
|
|
|
*src = s;
|
|
|
|
}
|
2005-12-05 21:18:09 +08:00
|
|
|
|
|
|
|
|
2007-09-27 17:36:50 +08:00
|
|
|
uintptr_t
|
|
|
|
ngx_escape_html(u_char *dst, u_char *src, size_t size)
|
|
|
|
{
|
|
|
|
u_char ch;
|
2009-11-03 01:12:09 +08:00
|
|
|
ngx_uint_t len;
|
2007-09-27 17:36:50 +08:00
|
|
|
|
|
|
|
if (dst == NULL) {
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
2009-11-03 01:12:09 +08:00
|
|
|
while (size) {
|
2007-09-27 17:36:50 +08:00
|
|
|
switch (*src++) {
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
len += sizeof("<") - 2;
|
2007-10-10 02:42:00 +08:00
|
|
|
break;
|
2007-09-27 17:36:50 +08:00
|
|
|
|
|
|
|
case '>':
|
|
|
|
len += sizeof(">") - 2;
|
2007-10-10 02:42:00 +08:00
|
|
|
break;
|
2007-09-27 17:36:50 +08:00
|
|
|
|
|
|
|
case '&':
|
|
|
|
len += sizeof("&") - 2;
|
2007-10-10 02:42:00 +08:00
|
|
|
break;
|
2007-09-27 17:36:50 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-11-03 01:12:09 +08:00
|
|
|
size--;
|
2007-09-27 17:36:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (uintptr_t) len;
|
|
|
|
}
|
|
|
|
|
2009-11-03 01:12:09 +08:00
|
|
|
while (size) {
|
2007-09-27 17:36:50 +08:00
|
|
|
ch = *src++;
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
*dst++ = '&'; *dst++ = 'l'; *dst++ = 't'; *dst++ = ';';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
*dst++ = '&'; *dst++ = 'g'; *dst++ = 't'; *dst++ = ';';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
*dst++ = '&'; *dst++ = 'a'; *dst++ = 'm'; *dst++ = 'p';
|
|
|
|
*dst++ = ';';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*dst++ = ch;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-03 01:12:09 +08:00
|
|
|
size--;
|
2007-09-27 17:36:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (uintptr_t) dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-23 23:31:33 +08:00
|
|
|
void
|
|
|
|
ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
|
|
|
ngx_str_node_t *n, *t;
|
|
|
|
ngx_rbtree_node_t **p;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
n = (ngx_str_node_t *) node;
|
|
|
|
t = (ngx_str_node_t *) temp;
|
|
|
|
|
|
|
|
if (node->key != temp->key) {
|
|
|
|
|
|
|
|
p = (node->key < temp->key) ? &temp->left : &temp->right;
|
|
|
|
|
|
|
|
} else if (n->str.len != t->str.len) {
|
|
|
|
|
|
|
|
p = (n->str.len < t->str.len) ? &temp->left : &temp->right;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
p = (ngx_memcmp(n->str.data, t->str.data, n->str.len) < 0)
|
|
|
|
? &temp->left : &temp->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = node;
|
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_str_node_t *
|
|
|
|
ngx_str_rbtree_lookup(ngx_rbtree_t *rbtree, ngx_str_t *val, uint32_t hash)
|
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_node_t *n;
|
|
|
|
ngx_rbtree_node_t *node, *sentinel;
|
|
|
|
|
|
|
|
node = rbtree->root;
|
|
|
|
sentinel = rbtree->sentinel;
|
|
|
|
|
|
|
|
while (node != sentinel) {
|
|
|
|
|
|
|
|
n = (ngx_str_node_t *) node;
|
|
|
|
|
|
|
|
if (hash != node->key) {
|
|
|
|
node = (hash < node->key) ? node->left : node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val->len != n->str.len) {
|
|
|
|
node = (val->len < n->str.len) ? node->left : node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_memcmp(val->data, n->str.data, val->len);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
node = node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc > 0) {
|
|
|
|
node = node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-21 22:05:23 +08:00
|
|
|
/* ngx_sort() is implemented as insertion sort because we need stable sort */
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_sort(void *base, size_t n, size_t size,
|
2008-03-24 21:04:02 +08:00
|
|
|
ngx_int_t (*cmp)(const void *, const void *))
|
2007-05-21 22:05:23 +08:00
|
|
|
{
|
2008-03-24 03:58:54 +08:00
|
|
|
u_char *p1, *p2, *p;
|
|
|
|
|
|
|
|
p = ngx_alloc(size, ngx_cycle->log);
|
|
|
|
if (p == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2007-05-21 22:05:23 +08:00
|
|
|
|
|
|
|
for (p1 = (u_char *) base + size;
|
|
|
|
p1 < (u_char *) base + n * size;
|
|
|
|
p1 += size)
|
|
|
|
{
|
2008-03-24 03:58:54 +08:00
|
|
|
ngx_memcpy(p, p1, size);
|
2007-05-21 22:05:23 +08:00
|
|
|
|
|
|
|
for (p2 = p1;
|
2008-03-24 03:58:54 +08:00
|
|
|
p2 > (u_char *) base && cmp(p2 - size, p) > 0;
|
2007-05-21 22:05:23 +08:00
|
|
|
p2 -= size)
|
|
|
|
{
|
|
|
|
ngx_memcpy(p2, p2 - size, size);
|
|
|
|
}
|
|
|
|
|
2008-03-24 03:58:54 +08:00
|
|
|
ngx_memcpy(p2, p, size);
|
2007-05-21 22:05:23 +08:00
|
|
|
}
|
2008-03-24 03:58:54 +08:00
|
|
|
|
|
|
|
ngx_free(p);
|
2007-05-21 22:05:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
#if (NGX_MEMCPY_LIMIT)
|
|
|
|
|
|
|
|
void *
|
|
|
|
ngx_memcpy(void *dst, void *src, size_t n)
|
|
|
|
{
|
|
|
|
if (n > NGX_MEMCPY_LIMIT) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "memcpy %uz bytes", n);
|
|
|
|
ngx_debug_point();
|
|
|
|
}
|
|
|
|
|
|
|
|
return memcpy(dst, src, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|