util.c revision 066877f1a045103acfdd376d48cdd473c33f409b
d29201dd5328b88140ce050100693c501852657dChristian Maeder/* ====================================================================
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * The Apache Software License, Version 1.1
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Copyright (c) 2000 The Apache Software Foundation. All rights
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * Redistribution and use in source and binary forms, with or without
cacbb5e3100fb85d23d1614cace3a8662801f2e6Eugen Kuksa * modification, are permitted provided that the following conditions
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 1. Redistributions of source code must retain the above copyright
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * notice, this list of conditions and the following disclaimer.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 2. Redistributions in binary form must reproduce the above copyright
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * notice, this list of conditions and the following disclaimer in
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * the documentation and/or other materials provided with the
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * distribution.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 3. The end-user documentation included with the redistribution,
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * if any, must include the following acknowledgment:
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * "This product includes software developed by the
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Apache Software Foundation (http://www.apache.org/)."
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Alternately, this acknowledgment may appear in the software itself,
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * if and wherever such third-party acknowledgments normally appear.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * 4. The names "Apache" and "Apache Software Foundation" must
d75d2d11170f1339ebe37d9d9c06aff148637b13Christian Maeder * not be used to endorse or promote products derived from this
f6eb05386b068f5968180b21cc225ef0d7d836e7Eugen Kuksa * software without prior written permission. For written
66977d201b3ff7ee9c1f992c0f3f701b69eac2c9Karl Luc * permission, please contact apache@apache.org.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * 5. Products derived from this software may not be called "Apache",
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * nor may "Apache" appear in their name, without prior written
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * permission of the Apache Software Foundation.
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a35bacbc16daf5c10f9accfdfadc4971e9d6f648Iulia Ignatov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * SUCH DAMAGE.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ====================================================================
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * This software consists of voluntary contributions made by many
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * individuals on behalf of the Apache Software Foundation. For more
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * information on the Apache Software Foundation, please see
b3138d7e20d2d6dd26a325b844a8b21b0ecbb602Eugen Kuksa * Portions of this software are based upon public domain software
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * originally written at the National Center for Supercomputing Applications,
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * University of Illinois, Urbana-Champaign.
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * util.c: string utility things
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * 3/21/93 Rob McCool
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * 1995-96 Many changes by the Apache Software Foundation
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa/* Debugging aid:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * #define DEBUG_CFG_LINES to trace every line read from the config files
c96c73387a0c41614ba325f737edad6abbf07e79Eugen Kuksa/* A bunch of functions in util.c scan strings looking for certain characters.
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * To make that more efficient we encode a lookup table. The test_char_table
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * is generated automatically by gen_test_char.c.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc/* we assume the folks using this ensure 0 <= c < 256... which means
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * you need a cast to (unsigned char) first, you can't just plug a
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * char in here and get it to work, because if char is signed then it
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * will first be sign extended.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * Examine a field value (such as a media-/content-type) string and return
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen KuksaAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa const char *semi;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa while ((semi > intype) && apr_isspace(semi[-1])) {
99a1bfbb82c875580410b4745617fea2297fdc2bEugen KuksaAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa const char *f;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa /* Convert %Z to "GMT" and %z to "+0000";
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * on hosts that do not have a time zone string in struct tm,
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * strftime must assume its argument is local time.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa if (*f != '%') continue;
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa switch (f[1]) {
* 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;
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
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;
#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)
while (*str) {
++str;
for (x = 0; p->h_aliases[x]; ++x) {
return NULL;
#ifndef MAXHOSTNAMELEN
struct hostent *p;
#ifdef BEOS_R5
if (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;