util.c revision 6eac5bfce4c68cf5f1d0615586090af2ae688bc1
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco/* Licensed to the Apache Software Foundation (ASF) under one or more
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * contributor license agreements. See the NOTICE file distributed with
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * this work for additional information regarding copyright ownership.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * The ASF licenses this file to You under the Apache License, Version 2.0
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * (the "License"); you may not use this file except in compliance with
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * the License. You may obtain a copy of the License at
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * Unless required by applicable law or agreed to in writing, software
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * distributed under the License is distributed on an "AS IS" BASIS,
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * See the License for the specific language governing permissions and
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * limitations under the License.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * util.c: string utility things
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * 3/21/93 Rob McCool
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * 1995-96 Many changes by the Apache Software Foundation
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco/* Debugging aid:
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * #define DEBUG to trace all cfg_open*()/cfg_closefile() calls
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * #define DEBUG_CFG_LINES to trace every line read from the config files
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco/* A bunch of functions in util.c scan strings looking for certain characters.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * To make that more efficient we encode a lookup table. The test_char_table
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * is generated automatically by gen_test_char.c.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco/* we assume the folks using this ensure 0 <= c < 256... which means
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * you need a cast to (unsigned char) first, you can't just plug a
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * char in here and get it to work, because if char is signed then it
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * will first be sign extended.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco#define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco/* Win32/NetWare/OS2 need to check for both forward and back slashes
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * in ap_getparents() and ap_escape_url.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco#define IS_SLASH(s) ((s == '/') || (s == '\\'))
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * Examine a field value (such as a media-/content-type) string and return
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián RiescoAP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco while ((semi > intype) && apr_isspace(semi[-1])) {
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco return apr_pstrndup(p, intype, semi - intype);
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián RiescoAP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt,
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco const char *f;
d72e314a1952b4418fb1c98b17dbab0d16bba585Adrián Riesco /* Convert %Z to "GMT" and %z to "+0000";
; 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
d = apr_pstrmemdup(p, 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;