nginx/src/core/ngx_string.c

177 lines
2.8 KiB
C
Raw Normal View History

#include <ngx_config.h>
2003-03-21 00:09:44 +08:00
#include <ngx_core.h>
2004-03-16 15:10:12 +08:00
u_char *ngx_cpystrn(u_char *dst, u_char *src, size_t n)
{
2003-05-07 01:03:16 +08:00
if (n == 0) {
return dst;
2003-05-07 01:03:16 +08:00
}
for (/* void */; --n; dst++, src++) {
*dst = *src;
2003-05-07 01:03:16 +08:00
if (*dst == '\0') {
return dst;
2003-05-07 01:03:16 +08:00
}
}
*dst = '\0';
return dst;
}
2003-03-21 00:09:44 +08:00
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-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
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
}
2003-03-21 00:09:44 +08:00
#if 0
char *ngx_psprintf(ngx_pool_t *p, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
while (*fmt) {
switch(*fmt++) {
case '%':
switch(*fmt++) {
case 's':
s = va_arg(args, char *);
n += ngx_strlen(s);
break;
default:
n++;
}
default:
n++;
}
}
str = ngx_palloc(p, n);
va_start(args, fmt);
for (i = 0; i < n; i++) {
switch(*fmt++) {
case '%':
switch(*fmt++) {
case 's':
s = va_arg(args, char *);
while (str[i++] = s);
break;
default:
n++;
}
default:
str[i] = *fmt;
}
}
len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args);
va_end(args);
}
#endif