util.c revision 3d72602d1d9e6f0d49d20f0d0a71831c160ef589
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering/* ====================================================================
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * The Apache Software License, Version 1.1
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Redistribution and use in source and binary forms, with or without
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * modification, are permitted provided that the following conditions
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 1. Redistributions of source code must retain the above copyright
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * notice, this list of conditions and the following disclaimer.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 2. Redistributions in binary form must reproduce the above copyright
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * notice, this list of conditions and the following disclaimer in
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * the documentation and/or other materials provided with the
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * distribution.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * 3. The end-user documentation included with the redistribution,
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * if any, must include the following acknowledgment:
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * "This product includes software developed by the
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Apache Software Foundation (http://www.apache.org/)."
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Alternately, this acknowledgment may appear in the software itself,
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * if and wherever such third-party acknowledgments normally appear.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 4. The names "Apache" and "Apache Software Foundation" must
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * not be used to endorse or promote products derived from this
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * software without prior written permission. For written
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * permission, please contact apache@apache.org.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 5. Products derived from this software may not be called "Apache",
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * nor may "Apache" appear in their name, without prior written
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * permission of the Apache Software Foundation.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * SUCH DAMAGE.
34511ca7b166b0e89d08ff9870b0cf2624a7815fLennart Poettering * ====================================================================
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * This software consists of voluntary contributions made by many
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * individuals on behalf of the Apache Software Foundation. For more
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * information on the Apache Software Foundation, please see
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Portions of this software are based upon public domain software
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * originally written at the National Center for Supercomputing Applications,
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * University of Illinois, Urbana-Champaign.
4aa6e7782a1b693a8e4fa1d84c87dd76fe1e536dLennart Poettering * util.c: string utility things
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 3/21/93 Rob McCool
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * 1995-96 Many changes by the Apache Software Foundation
409dee2e44e7dc73d6bf00d782938e4cb4105f5bJan Engelhardt/* Debugging aid:
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * #define DEBUG_CFG_LINES to trace every line read from the config files
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering#include <netdb.h> /* for gethostbyname() */
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering/* A bunch of functions in util.c scan strings looking for certain characters.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * To make that more efficient we encode a lookup table. The test_char_table
07bceef2a9c520e24da427c7ce500ad36ad37571Lennart Poettering * is generated automatically by gen_test_char.c.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering/* we assume the folks using this ensure 0 <= c < 256... which means
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * you need a cast to (unsigned char) first, you can't just plug a
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * char in here and get it to work, because if char is signed then it
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * will first be sign extended.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering * Examine a field value (such as a media-/content-type) string and return
c53158818d8cdaf46b3f1b5299b9bda118a1043fThomas Hindoe Paaboel Andersen * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart PoetteringAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
76e7bc8dfbb1139254c3819f448246592f17f317Lennart Poettering while ((semi > intype) && apr_isspace(semi[-1])) {
76e7bc8dfbb1139254c3819f448246592f17f317Lennart Poettering return apr_pstrndup(p, intype, semi - intype);
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart PoetteringAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
ee48647271132188e9ecc3507e62b6c7c7a6c9ebLennart Poettering const char *f;
; 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; exp[y]; ++y, ++x) {
if (!exp[y])
while (str[x]) {
int ret;
return ret;
for (x = 0, y = 0; exp[y]; ++y, ++x) {
if (!exp[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((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;
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
return NULL;
memcpy(d, s, l);
for (x = 0, n = 0; path[x]; x++)
char *res;
if (!pos) {
return res;
++pos;
return res;
char *res;
for (x = 0; (*line)[x]; x++) {
pos = x;
return res;
++pos;
return res;
char *res;
if (!pos) {
return res;
++pos;
return res;
for (i = 0; i < len; ++i) {
return result;
char *res;
char quote;
++str;
if (!*str) {
++strend;
++strend;
++strend;
++strend;
return res;
return word;
const char *e2 = e;
#ifdef DEBUG
char ch;
return ch;
return (int)EOF;
return buf;
return NULL;
AP_DECLARE(apr_status_t) ap_pcfg_openfile(ap_configfile_t **ret_cfg, apr_pool_t *p, const char *name)
#ifdef DEBUG
return APR_EBADF;
#ifdef DEBUG
return status;
return status;
name);
return APR_EBADF;
return APR_SUCCESS;
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 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;
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;
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;
if (badesc)
return HTTP_BAD_REQUEST;
else if (badpath)
return HTTP_NOT_FOUND;
return OK;
return where;
const unsigned char *s = (const unsigned char *)segment;
unsigned char *d = (unsigned char *)copy;
d = c2x(c, d);
return copy;
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
if (!partial) {
d = c2x(c, d);
return copy;
return apr_pstrndup(p, s, i);
const char *src2)
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;
for (x = 0; p->h_aliases[x]; ++x) {
return NULL;
#ifndef MAXHOSTNAMELEN
struct hostent *p;
#ifdef BEOS_R5
if (p && p->h_addr_list[0]) {
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) {
while (*str) {
++str;
if (semi) {
int newlen = 0;
newlen++;
newlen++;
inchr++;
newlen++;
inchr++;
return outstring;