		/* Copyright 2000-2004 The Apache Software Foundation
		 *
		 * Licensed under the Apache License, Version 2.0 (the "License");
		 * you may not use this file except in compliance with the License.
		 * You may obtain a copy of the License at
		 *
		 *     http://www.apache.org/licenses/LICENSE-2.0
		 *
		 * Unless required by applicable law or agreed to in writing, software
		 * distributed under the License is distributed on an "AS IS" BASIS,
		 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		 * See the License for the specific language governing permissions and
		 * limitations under the License.
		 */
		/*
		 * Copyright (c) 1990, 1993
		 *	The Regents of the University of California.  All rights reserved.
		 *
		 * Redistribution and use in source and binary forms, with or without
		 * modification, are permitted provided that the following conditions
		 * are met:
		 * 1. Redistributions of source code must retain the above copyright
		 *    notice, this list of conditions and the following disclaimer.
		 * 2. Redistributions in binary form must reproduce the above copyright
		 *    notice, this list of conditions and the following disclaimer in the
		 *    documentation and/or other materials provided with the distribution.
		 * 3. All advertising materials mentioning features or use of this software
		 *    must display the following acknowledgement:
		 *	This product includes software developed by the University of
		 *	California, Berkeley and its contributors.
		 * 4. Neither the name of the University nor the names of its contributors
		 *    may be used to endorse or promote products derived from this software
		 *    without specific prior written permission.
		 *
		 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
		 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
		 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
		 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
		 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
		 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
		 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
		 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
		 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
		 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
		 * SUCH DAMAGE.
		 */
		
		#include "apr.h"
		#include "apr_strings.h"
		#include "apr_general.h"
		#include "apr_private.h"
		#include "apr_lib.h"
		#define APR_WANT_STDIO
		#define APR_WANT_STRFUNC
		#include "apr_want.h"
		
		#ifdef HAVE_STDDEF_H
		#include <stddef.h> /* NULL */
		#endif
		
		#ifdef HAVE_STDLIB_H
		#include <stdlib.h> /* strtol and strtoll */
		#endif
		
		/** this is used to cache lengths in apr_pstrcat */
		#define MAX_SAVED_LENGTHS  6
		
		APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s)
         324    {
         324        char *res;
         324        apr_size_t len;
		
         324        if (s == NULL) {
           6            return NULL;
		    }
         318        len = strlen(s) + 1;
         318        res = apr_palloc(a, len);
         318        memcpy(res, s, len);
         318        return res;
		}
		
		APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
           3    {
           3        char *res;
           3        const char *end;
		
           3        if (s == NULL) {
      ######            return NULL;
		    }
           3        end = memchr(s, '\0', n);
           3        if (end != NULL)
      ######            n = end - s;
           3        res = apr_palloc(a, n + 1);
           3        memcpy(res, s, n);
           3        res[n] = '\0';
           3        return res;
		}
		
		APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
      ######    {
      ######        char *res;
		
      ######        if (s == NULL) {
      ######            return NULL;
		    }
      ######        res = apr_palloc(a, n + 1);
      ######        memcpy(res, s, n);
      ######        res[n] = '\0';
      ######        return res;
		}
		
		APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
      ######    {
      ######        void *res;
		
      ######        if (m == NULL)
      ######    	return NULL;
      ######        res = apr_palloc(a, n);
      ######        memcpy(res, m, n);
      ######        return res;
		}
		
		APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
           6    {
           6        char *cp, *argp, *res;
           6        apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
           6        int nargs = 0;
		
		    /* Pass one --- find length of required string */
		
           6        apr_size_t len = 0;
           6        va_list adummy;
		
           6        va_start(adummy, a);
		
          20        while ((cp = va_arg(adummy, char *)) != NULL) {
          14            apr_size_t cplen = strlen(cp);
          14            if (nargs < MAX_SAVED_LENGTHS) {
          14                saved_lengths[nargs++] = cplen;
		        }
          14            len += cplen;
		    }
		
           6        va_end(adummy);
		
		    /* Allocate the required string */
		
           6        res = (char *) apr_palloc(a, len + 1);
           6        cp = res;
		
		    /* Pass two --- copy the argument strings into the result space */
		
           6        va_start(adummy, a);
		
           6        nargs = 0;
          20        while ((argp = va_arg(adummy, char *)) != NULL) {
          14            if (nargs < MAX_SAVED_LENGTHS) {
          14                len = saved_lengths[nargs++];
		        }
		        else {
      ######                len = strlen(argp);
		        }
		 
          14            memcpy(cp, argp, len);
          14            cp += len;
		    }
		
           6        va_end(adummy);
		
		    /* Return the result string */
		
           6        *cp = '\0';
		
           6        return res;
		}
		
		APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
		                                 apr_size_t nvec, apr_size_t *nbytes)
      ######    {
      ######        apr_size_t i;
      ######        apr_size_t len;
      ######        const struct iovec *src;
      ######        char *res;
      ######        char *dst;
		
		    /* Pass one --- find length of required string */
      ######        len = 0;
      ######        src = vec;
      ######        for (i = nvec; i; i--) {
      ######            len += src->iov_len;
      ######            src++;
		    }
      ######        if (nbytes) {
      ######            *nbytes = len;
		    }
		
		    /* Allocate the required string */
      ######        res = (char *) apr_palloc(a, len + 1);
		    
		    /* Pass two --- copy the argument strings into the result space */
      ######        src = vec;
      ######        dst = res;
      ######        for (i = nvec; i; i--) {
      ######            memcpy(dst, src->iov_base, src->iov_len);
      ######            dst += src->iov_len;
      ######            src++;
		    }
		
		    /* Return the result string */
      ######        *dst = '\0';
		
      ######        return res;
		}
		
		#if (!APR_HAVE_MEMCHR)
		void *memchr(const void *s, int c, size_t n)
		{
		    const char *cp;
		
		    for (cp = s; n > 0; n--, cp++) {
		        if (*cp == c)
		            return (char *) cp; /* Casting away the const here */
		    }
		
		    return NULL;
		}
		#endif
		
		#ifndef INT64_MAX
		#define INT64_MAX  APR_INT64_C(0x7fffffffffffffff)
		#endif
		#ifndef INT64_MIN
		#define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
		#endif
		
		APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
           1    {
		#if (APR_HAVE_INT64_STRFN)
           1        return APR_INT64_STRFN(nptr, endptr, base);
		#else
		    const char *s;
		    apr_int64_t acc;
		    apr_int64_t val;
		    int neg, any;
		    char c;
		
		    /*
		     * Skip white space and pick up leading +/- sign if any.
		     * If base is 0, allow 0x for hex and 0 for octal, else
		     * assume decimal; if base is already 16, allow 0x.
		     */
		    s = nptr;
		    do {
			c = *s++;
		    } while (apr_isspace(c));
		    if (c == '-') {
			neg = 1;
			c = *s++;
		    } else {
			neg = 0;
			if (c == '+')
			    c = *s++;
		    }
		    if ((base == 0 || base == 16) &&
			c == '0' && (*s == 'x' || *s == 'X')) {
			    c = s[1];
			    s += 2;
			    base = 16;
		    }
		    if (base == 0)
			base = c == '0' ? 8 : 10;
		    acc = any = 0;
		    if (base < 2 || base > 36) {
			errno = EINVAL;
		        if (endptr != NULL)
			    *endptr = (char *)(any ? s - 1 : nptr);
		        return acc;
		    }
		
		    /* The classic bsd implementation requires div/mod operators
		     * to compute a cutoff.  Benchmarking proves that iss very, very
		     * evil to some 32 bit processors.  Instead, look for underflow
		     * in both the mult and add/sub operation.  Unlike the bsd impl,
		     * we also work strictly in a signed int64 word as we haven't
		     * implemented the unsigned type in win32.
		     * 
		     * Set 'any' if any `digits' consumed; make it negative to indicate
		     * overflow.
		     */
		    val = 0;
		    for ( ; ; c = *s++) {
		        if (c >= '0' && c <= '9')
			    c -= '0';
		#if (('Z' - 'A') == 25)
			else if (c >= 'A' && c <= 'Z')
			    c -= 'A' - 10;
			else if (c >= 'a' && c <= 'z')
			    c -= 'a' - 10;
		#elif APR_CHARSET_EBCDIC
			else if (c >= 'A' && c <= 'I')
			    c -= 'A' - 10;
			else if (c >= 'J' && c <= 'R')
			    c -= 'J' - 19;
			else if (c >= 'S' && c <= 'Z')
			    c -= 'S' - 28;
			else if (c >= 'a' && c <= 'i')
			    c -= 'a' - 10;
			else if (c >= 'j' && c <= 'r')
			    c -= 'j' - 19;
			else if (c >= 's' && c <= 'z')
			    c -= 'z' - 28;
		#else
		#error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported" 
		#endif
			else
			    break;
			if (c >= base)
			    break;
			val *= base;
		        if ( (any < 0)	/* already noted an over/under flow - short circuit */
		           || (neg && (val > acc || (val -= c) > acc)) /* underflow */
		           || (val < acc || (val += c) < acc)) {       /* overflow */
		            any = -1;	/* once noted, over/underflows never go away */
		#ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
		            break;
		#endif
		        } else {
		            acc = val;
			    any = 1;
		        }
		    }
		
		    if (any < 0) {
			acc = neg ? INT64_MIN : INT64_MAX;
			errno = ERANGE;
		    } else if (!any) {
			errno = EINVAL;
		    }
		    if (endptr != NULL)
			*endptr = (char *)(any ? s - 1 : nptr);
		    return (acc);
		#endif
		}
		
		APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
      ######    {
      ######        return apr_strtoi64(buf, NULL, 10);
		}
		
		APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
           5    {
           5        const int BUFFER_SIZE = sizeof(int) * 3 + 2;
           5        char *buf = apr_palloc(p, BUFFER_SIZE);
           5        char *start = buf + BUFFER_SIZE - 1;
           5        int negative;
           5        if (n < 0) {
      ######    	negative = 1;
      ######    	n = -n;
		    }
		    else {
           5    	negative = 0;
		    }
           5        *start = 0;
          18        do {
          18    	*--start = '0' + (n % 10);
          18    	n /= 10;
          18        } while (n);
           5        if (negative) {
      ######    	*--start = '-';
		    }
           5        return start;
		}
		
		APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
      ######    {
      ######        const int BUFFER_SIZE = sizeof(long) * 3 + 2;
      ######        char *buf = apr_palloc(p, BUFFER_SIZE);
      ######        char *start = buf + BUFFER_SIZE - 1;
      ######        int negative;
      ######        if (n < 0) {
      ######    	negative = 1;
      ######    	n = -n;
		    }
		    else {
      ######    	negative = 0;
		    }
      ######        *start = 0;
      ######        do {
      ######    	*--start = (char)('0' + (n % 10));
      ######    	n /= 10;
      ######        } while (n);
      ######        if (negative) {
      ######    	*--start = '-';
		    }
      ######        return start;
		}
		
		APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
      ######    {
      ######        const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
      ######        char *buf = apr_palloc(p, BUFFER_SIZE);
      ######        char *start = buf + BUFFER_SIZE - 1;
      ######        int negative;
      ######        if (n < 0) {
      ######    	negative = 1;
      ######    	n = -n;
		    }
		    else {
      ######    	negative = 0;
		    }
      ######        *start = 0;
      ######        do {
      ######    	*--start = '0' + (char)(n % 10);
      ######    	n /= 10;
      ######        } while (n);
      ######        if (negative) {
      ######    	*--start = '-';
		    }
      ######        return start;
		}
		
		APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
      ######    {
      ######        const char ord[] = "KMGTPE";
      ######        const char *o = ord;
      ######        int remain;
		
      ######        if (size < 0) {
      ######            return strcpy(buf, "  - ");
		    }
      ######        if (size < 973) {
      ######            sprintf(buf, "%3d ", (int) size);
      ######            return buf;
		    }
      ######        do {
      ######            remain = (int)(size & 1023);
      ######            size >>= 10;
      ######            if (size >= 973) {
      ######                ++o;
      ######                continue;
		        }
      ######            if (size < 9 || (size == 9 && remain < 973)) {
      ######                if ((remain = ((remain * 5) + 256) / 512) >= 10)
      ######                    ++size, remain = 0;
      ######                sprintf(buf, "%d.%d%c", (int) size, remain, *o);
      ######                return buf;
		        }
      ######            if (remain >= 512)
      ######                ++size;
      ######            sprintf(buf, "%3d%c", (int) size, *o);
      ######            return buf;
      ######        } while (1);
		}
		
