util.c revision 9257af3c6aaeaf201fccb3e3c87cf8c41783fdb7
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Licensed to the Apache Software Foundation (ASF) under one or more
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner * contributor license agreements. See the NOTICE file distributed with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * this work for additional information regarding copyright ownership.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * The ASF licenses this file to You under the Apache License, Version 2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * (the "License"); you may not use this file except in compliance with
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * the License. You may obtain a copy of the License at
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * http://www.apache.org/licenses/LICENSE-2.0
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Unless required by applicable law or agreed to in writing, software
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * distributed under the License is distributed on an "AS IS" BASIS,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * See the License for the specific language governing permissions and
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * limitations under the License.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * util.c: string utility things
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * 3/21/93 Rob McCool
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * 1995-96 Many changes by the Apache Software Foundation
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Debugging aid:
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * #define DEBUG_CFG_LINES to trace every line read from the config files
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#include <netdb.h> /* for gethostbyname() */
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* A bunch of functions in util.c scan strings looking for certain characters.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * To make that more efficient we encode a lookup table. The test_char_table
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * is generated automatically by gen_test_char.c.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* we assume the folks using this ensure 0 <= c < 256... which means
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * you need a cast to (unsigned char) first, you can't just plug a
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * char in here and get it to work, because if char is signed then it
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * will first be sign extended.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder/* Win32/NetWare/OS2 need to check for both forward and back slashes
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * in ap_getparents() and ap_escape_url.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder#define IS_SLASH(s) ((s == '/') || (s == '\\'))
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * Examine a field value (such as a media-/content-type) string and return
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von SchroederAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder while ((semi > intype) && apr_isspace(semi[-1])) {
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder return apr_pstrndup(p, intype, semi - intype);
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von SchroederAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder /* Convert %Z to "GMT" and %z to "+0000";
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * on hosts that do not have a time zone string in struct tm,
081559cfba7150d19604bdeafdc2d9983f7216b3Jonathan von Schroeder * strftime must assume its argument is local time.
; f++, strp++) {
*++strp = *++f;
* replaced the old later_than() routine with util_date.c.
* Based loosely on sections of wildmat.c by Rich Salz
for (x = 0, y = 0; expected[y]; ++y, ++x) {
if (!expected[y])
while (str[x]) {
int ret;
return ret;
for (x = 0, y = 0; expected[y]; ++y, ++x) {
if (!expected[y])
while (str[x]) {
int ret;
return ret;
const char *newpath;
for (x = 0; str[x]; x++)
return APR_SUCCESS;
int cflags)
return NULL;
return preg;
return((char *)s1);
return(NULL);
return((char *)s1);
s1++;
return((char *)s1);
const char *prefix)
const char *p1;
return bigstring;
return bigstring;
return p1;
return bigstring;
int len;
if (!source)
return NULL;
if (!nmatch)
len = 0;
no = 0;
c = *src++;
len++;
no = 0;
c = *src++;
*dst++ = c;
return dest;
char *next;
int l, w, first_dot;
l = first_dot;
s = d = name;
#ifdef HAVE_UNC_PATHS
* and s == "e:/test.html", "e:/" is returned in d
* *** See also directory_walk in modules/http/http_request.c
memcpy(d, s, l);
for (x = 0, n = 0; path[x]; x++)
int len;
char *res;
++pos;
if (stop) {
++pos;
return res;
int len;
char *res;
++pos;
++pos;
return res;
char stop)
char stop)
char *res;
if (!pos) {
return res;
++pos;
return res;
char quote)
for (i = 0; i < len; ++i) {
return result;
char *res;
char quote;
++str;
if (!*str) {
++strend;
++strend;
++strend;
++strend;
return res;
struct sll {
const char *string;
const char *s, *e, *ep;
unsigned spc;
return word;
spc = 0;
if (word) {
++outlen;
word = s;
} while (result);
return res_buf;
#ifdef DEBUG
char ch;
return ch;
return (int)EOF;
return buf;
return NULL;
#ifdef DEBUG
return APR_EBADF;
APR_OS_DEFAULT, p);
#ifdef DEBUG
return status;
return status;
name);
return APR_EBADF;
#ifdef WIN32
return APR_SUCCESS;
const char *descr,
void *param,
#ifdef DEBUG
return new_cfg;
return ch;
char *cp;
cp++;
cp--;
cp--;
cp--;
++src;
#ifdef DEBUG_CFG_LINES
register size_t i = 0;
if (c == EOF)
if (c == CR) {
if (c == LF) {
#ifdef DEBUG_CFG_LINES
buf[i] = c;
const unsigned char *token;
++ptr;
++ptr) {
if (in_qpair) {
in_qpair = 0;
switch (*ptr) {
++in_com;
--in_com;
return NULL;
++ptr;
return (const char *)token;
const char *tok_start;
const unsigned char *ptr;
unsigned char *pos;
char *token;
return NULL;
++ptr) {
if (in_qpair) {
in_qpair = 0;
switch (*ptr) {
addspace = 0;
addspace = 0;
++in_com;
addspace = 0;
--in_com;
addspace = 0;
addspace = 0;
return token;
const char *tok)
const unsigned char *pos;
++ptr;
if (*ptr)
++ptr) {
if (in_qpair) {
in_qpair = 0;
if (good)
switch (*ptr) {
addspace = 0;
addspace = 0;
++in_com;
addspace = 0;
--in_com;
addspace = 0;
default : if (!good)
addspace = 0;
return good;
int accept_white)
const char *tok_start;
char *token;
int tok_len;
++ptr;
while (*ptr)
++ptr;
return token;
const unsigned char *start_token;
if (!line)
s = (const unsigned char *)line;
start_token = s;
s - start_token)) {
const char *tok)
if (!line)
if (lidx < 0 ||
char *cmd;
d = (unsigned char *)cmd;
s = (const unsigned char *)str;
return cmd;
register char digit;
#if !APR_CHARSET_EBCDIC
return (digit);
badesc = 0;
badpath = 0;
if (y == NULL) {
return OK;
char decoded;
*x = decoded;
*x = decoded;
if (badesc) {
return HTTP_BAD_REQUEST;
else if (badpath) {
return HTTP_NOT_FOUND;
return OK;
#ifdef CASE_BLIND_FILESYSTEM
#ifdef NEW_APIS
unsigned char *where)
return where;
const unsigned char *s = (const unsigned char *)segment;
unsigned char *d = (unsigned char *)copy;
return copy;
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
if (!partial) {
return copy;
return apr_pstrmemdup(p, s, i);
char *ret;
if (!str) {
return NULL;
d = (unsigned char *)ret;
s = (const unsigned char *)str;
return ret;
unsigned char *d, *ep;
d = (unsigned char *)dest;
s = (const unsigned char *)source;
for (; d < ep && *s; ++s) {
if (d >= ep) {
return (d - (unsigned char *)dest);
const char *src2)
char *path;
if (len1 == 0) {
char *next;
return path;
const char *p = ap_strchr_c(s, c);
if (p == NULL)
const char *p = ap_strrchr_c(s, c);
if (p == NULL)
while (*str) {
++str;
#ifndef MAXHOSTNAMELEN
char *hostname;
return server_hostname;
if (!server_hostname)
return server_hostname;
/* simple 'pool' alloc()ing glue to apr_base64.c
char *decoded;
return decoded;
char *encoded;
return encoded;
char *semi;
if (semi) {
if (semi) {
int newlen = 0;
newlen++;
newlen++;
inchr++;
newlen++;
inchr++;
return outstring;
* a macro, to avoid unistd.h dependency
const char *delim)
const char *timeout_parameter,
const char *default_time_unit)
char *endp;
const char *time_str;
if (errno) {
return errno;
switch (*time_str) {
switch (*(++time_str)) {
return APR_EGENERAL;
return APR_EGENERAL;
return APR_SUCCESS;