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
|
|
|
|
|
|
|
|
2004-03-16 15:10:12 +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
|
|
|
|
|
|
|
for (/* void */; --n; dst++, src++) {
|
|
|
|
*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
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*dst = '\0';
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
/*
|
|
|
|
* supported formats:
|
2004-11-11 22:07:14 +08:00
|
|
|
* %[0][width]O off_t
|
|
|
|
* %[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
|
|
|
|
* %P ngx_pid_t
|
|
|
|
* %r rlim_t
|
|
|
|
* %p pointer
|
|
|
|
* %V pointer to ngx_str_t
|
|
|
|
* %s null-terminated string
|
|
|
|
* %Z '\0'
|
|
|
|
* %c char
|
|
|
|
* %% %
|
2004-10-21 23:34:38 +08:00
|
|
|
*
|
2004-11-11 22:07:14 +08:00
|
|
|
* TODO:
|
|
|
|
* %M ngx_msec_t
|
|
|
|
* %A ngx_atomic_t
|
|
|
|
*
|
|
|
|
* reserved:
|
|
|
|
* %t ptrdiff_t
|
|
|
|
* %S null-teminated wchar string
|
|
|
|
* %C wchar
|
2004-10-21 23:34:38 +08:00
|
|
|
*/
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
u_char *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);
|
|
|
|
p = ngx_vsnprintf(buf, /* STUB */ 65536, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u_char *ngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
p = ngx_vsnprintf(buf, max, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u_char *ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
u_char *p, zero, *last, temp[NGX_MAX_INT_LEN];
|
|
|
|
int d;
|
|
|
|
size_t len;
|
|
|
|
uint32_t ui32;
|
|
|
|
int64_t i64;
|
|
|
|
uint64_t ui64;
|
|
|
|
ngx_str_t *s;
|
|
|
|
ngx_uint_t width, sign, hexadecimal;
|
|
|
|
static u_char hex[] = "0123456789abcdef";
|
|
|
|
static u_char HEX[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
if (max == 0) {
|
|
|
|
return buf;
|
|
|
|
}
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
last = buf + max;
|
|
|
|
|
|
|
|
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;
|
|
|
|
hexadecimal = 0;
|
|
|
|
|
|
|
|
p = temp + NGX_MAX_INT_LEN;
|
|
|
|
|
|
|
|
while (*fmt >= '0' && *fmt <= '9') {
|
|
|
|
width = width * 10 + *fmt++ - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
switch (*fmt) {
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
sign = 0;
|
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'X':
|
|
|
|
hexadecimal = 2;
|
|
|
|
sign = 0;
|
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
case 'x':
|
|
|
|
hexadecimal = 1;
|
2004-11-11 22:07:14 +08:00
|
|
|
sign = 0;
|
2004-10-21 23:34:38 +08:00
|
|
|
fmt++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
case 'V':
|
|
|
|
s = va_arg(args, ngx_str_t *);
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
len = (buf + s->len < last) ? s->len : (size_t) (last - buf);
|
|
|
|
buf = ngx_cpymem(buf, s->data, len);
|
|
|
|
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 's':
|
|
|
|
p = va_arg(args, u_char *);
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
while (*p && buf < last) {
|
|
|
|
*buf++ = *p++;
|
2004-10-21 23:34:38 +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
|
|
|
|
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
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
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 *);
|
|
|
|
hexadecimal = 2;
|
|
|
|
sign = 0;
|
|
|
|
zero = '0';
|
|
|
|
width = 8;
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hexadecimal == 1) {
|
|
|
|
do {
|
|
|
|
|
|
|
|
/* the "(uint32_t)" cast disables the BCC's warning */
|
|
|
|
*--p = hex[(uint32_t) (ui64 & 0xf)];
|
|
|
|
|
|
|
|
} while (ui64 >>= 4);
|
|
|
|
|
|
|
|
} else if (hexadecimal == 2) {
|
|
|
|
do {
|
|
|
|
|
|
|
|
/* the "(uint32_t)" cast disables the BCC's warning */
|
|
|
|
*--p = HEX[(uint32_t) (ui64 & 0xf)];
|
|
|
|
|
|
|
|
} while (ui64 >>= 4);
|
|
|
|
|
|
|
|
} else if (ui64 <= NGX_MAX_UINT32_VALUE) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To divide 64-bit number and to find the remainder
|
|
|
|
* on the x86 platform gcc and icc call the libc functions
|
|
|
|
* [u]divdi3() and [u]moddi3(), they call another function
|
|
|
|
* in return. 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 gcc and icc use the 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);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
*--p = (u_char) (ui64 % 10 + '0');
|
|
|
|
} while (ui64 /= 10);
|
|
|
|
}
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
len = (temp + NGX_MAX_INT_LEN) - p;
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
while (len++ < width && buf < last) {
|
|
|
|
*buf++ = zero;
|
|
|
|
}
|
2004-10-21 23:34:38 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
len = (temp + NGX_MAX_INT_LEN) - p;
|
|
|
|
if (buf + len > last) {
|
|
|
|
len = last - buf;
|
2004-10-21 23:34:38 +08:00
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
buf = ngx_cpymem(buf, p, len);
|
2004-10-21 23:34:38 +08:00
|
|
|
|
|
|
|
fmt++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*buf++ = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 15:10:12 +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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 23:34:38 +08:00
|
|
|
ngx_int_t ngx_rstrncasecmp(u_char *s1, u_char *s2, size_t n)
|
|
|
|
{
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 15:10:12 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_hextoi(u_char *line, size_t n)
|
|
|
|
{
|
|
|
|
u_char ch;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch >= 'A' && ch <= 'F') {
|
2004-05-22 00:12:13 +08:00
|
|
|
value = value * 16 + (ch - 'A' + 10);
|
2004-05-21 01:33:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch >= 'a' && ch <= 'f') {
|
2004-05-22 00:12:13 +08:00
|
|
|
value = value * 16 + (ch - '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
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
void ngx_md5_text(u_char *text, u_char *md5)
|
2003-11-05 06:12:39 +08:00
|
|
|
{
|
2004-03-16 15:10:12 +08:00
|
|
|
int i;
|
|
|
|
static u_char hex[] = "0123456789abcdef";
|
2003-11-06 01:03:41 +08:00
|
|
|
|
2003-11-10 04:03:38 +08:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
*text++ = hex[md5[i] >> 4];
|
|
|
|
*text++ = hex[md5[i] & 0xf];
|
|
|
|
}
|
|
|
|
|
|
|
|
*text = '\0';
|
2003-11-05 06:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-11 23:07:03 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
ngx_int_t ngx_decode_base64(ngx_str_t *dst, ngx_str_t *src)
|
2004-07-31 01:05:14 +08:00
|
|
|
{
|
2004-08-27 23:40:59 +08:00
|
|
|
size_t len;
|
|
|
|
u_char *d, *s;
|
|
|
|
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 };
|
|
|
|
|
|
|
|
for (len = 0; len < src->len; len++) {
|
|
|
|
if (src->data[len] == '=') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (basis64[src->data[len]] == 77) {
|
|
|
|
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) {
|
2004-08-29 11:55:41 +08:00
|
|
|
*d++ = (u_char) (basis64[s[0]] << 2 | basis64[s[1]] >> 4);
|
|
|
|
*d++ = (u_char) (basis64[s[1]] << 4 | basis64[s[2]] >> 2);
|
|
|
|
*d++ = (u_char) (basis64[s[2]] << 6 | basis64[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) {
|
2004-08-29 11:55:41 +08:00
|
|
|
*d++ = (u_char) (basis64[s[0]] << 2 | basis64[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) {
|
2004-08-29 11:55:41 +08:00
|
|
|
*d++ = (u_char) (basis64[s[1]] << 4 | basis64[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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_uint_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
|
|
|
{
|
2004-11-11 22:07:14 +08:00
|
|
|
ngx_uint_t i, n;
|
|
|
|
uint32_t *escape;
|
2004-10-11 23:07:03 +08:00
|
|
|
static u_char hex[] = "0123456789abcdef";
|
2004-11-11 22:07:14 +08:00
|
|
|
|
|
|
|
static uint32_t uri[] =
|
2004-10-11 23:07:03 +08:00
|
|
|
{ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
|
|
0x80000021, /* 1000 0000 0000 0000 0000 0000 0010 0001 */
|
|
|
|
|
|
|
|
/* _^]\ [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 */ };
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
static uint32_t html[] =
|
|
|
|
{ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
|
|
|
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
|
|
|
0x80000021, /* 0000 0000 0000 0000 0000 0000 1010 0101 */
|
|
|
|
|
|
|
|
/* _^]\ [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 */ };
|
|
|
|
|
|
|
|
|
|
|
|
if (type == NGX_ESCAPE_HTML) {
|
|
|
|
escape = html;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
escape = uri;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
return 0;
|
2003-03-21 00:09:44 +08:00
|
|
|
}
|