1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
* Copyright (c) 1990, 1993
18
* The Regents of the University of California. All rights reserved.
20
* Redistribution and use in source and binary forms, with or without
21
* modification, are permitted provided that the following conditions
23
* 1. Redistributions of source code must retain the above copyright
24
* notice, this list of conditions and the following disclaimer.
25
* 2. Redistributions in binary form must reproduce the above copyright
26
* notice, this list of conditions and the following disclaimer in the
27
* documentation and/or other materials provided with the distribution.
28
* 3. All advertising materials mentioning features or use of this software
29
* must display the following acknowledgement:
30
* This product includes software developed by the University of
31
* California, Berkeley and its contributors.
32
* 4. Neither the name of the University nor the names of its contributors
33
* may be used to endorse or promote products derived from this software
34
* without specific prior written permission.
36
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
#include "apr_strings.h"
51
#include "apr_general.h"
52
#include "apr_private.h"
54
#define APR_WANT_STDIO
55
#define APR_WANT_STRFUNC
59
#include <stddef.h> /* NULL */
63
#include <stdlib.h> /* strtol and strtoll */
66
/** this is used to cache lengths in apr_pstrcat */
67
#define MAX_SAVED_LENGTHS 6
69
APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s)
78
res = apr_palloc(a, len);
83
APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
91
end = memchr(s, '\0', n);
94
res = apr_palloc(a, n + 1);
100
APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
107
res = apr_palloc(a, n + 1);
113
APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
119
res = apr_palloc(a, n);
124
APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
126
char *cp, *argp, *res;
127
apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
130
/* Pass one --- find length of required string */
137
while ((cp = va_arg(adummy, char *)) != NULL) {
138
apr_size_t cplen = strlen(cp);
139
if (nargs < MAX_SAVED_LENGTHS) {
140
saved_lengths[nargs++] = cplen;
147
/* Allocate the required string */
149
res = (char *) apr_palloc(a, len + 1);
152
/* Pass two --- copy the argument strings into the result space */
157
while ((argp = va_arg(adummy, char *)) != NULL) {
158
if (nargs < MAX_SAVED_LENGTHS) {
159
len = saved_lengths[nargs++];
165
memcpy(cp, argp, len);
171
/* Return the result string */
178
APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
179
apr_size_t nvec, apr_size_t *nbytes)
183
const struct iovec *src;
187
/* Pass one --- find length of required string */
190
for (i = nvec; i; i--) {
198
/* Allocate the required string */
199
res = (char *) apr_palloc(a, len + 1);
201
/* Pass two --- copy the argument strings into the result space */
204
for (i = nvec; i; i--) {
205
memcpy(dst, src->iov_base, src->iov_len);
210
/* Return the result string */
216
#if (!APR_HAVE_MEMCHR)
217
void *memchr(const void *s, int c, size_t n)
221
for (cp = s; n > 0; n--, cp++) {
223
return (char *) cp; /* Casting away the const here */
231
#define INT64_MAX APR_INT64_C(0x7fffffffffffffff)
234
#define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
237
APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *nptr,
238
char **endptr, int base)
241
*offset = APR_OFF_T_STRFN(nptr, endptr, base);
242
return APR_FROM_OS_ERROR(errno);
245
APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
247
#ifdef APR_INT64_STRFN
248
return APR_INT64_STRFN(nptr, endptr, base);
257
* Skip white space and pick up leading +/- sign if any.
258
* If base is 0, allow 0x for hex and 0 for octal, else
259
* assume decimal; if base is already 16, allow 0x.
264
} while (apr_isspace(c));
273
if ((base == 0 || base == 16) &&
274
c == '0' && (*s == 'x' || *s == 'X')) {
280
base = c == '0' ? 8 : 10;
282
if (base < 2 || base > 36) {
285
*endptr = (char *)(any ? s - 1 : nptr);
289
/* The classic bsd implementation requires div/mod operators
290
* to compute a cutoff. Benchmarking proves that is very, very
291
* evil to some 32 bit processors. Instead, look for underflow
292
* in both the mult and add/sub operation. Unlike the bsd impl,
293
* we also work strictly in a signed int64 word as we haven't
294
* implemented the unsigned type in win32.
296
* Set 'any' if any `digits' consumed; make it negative to indicate
300
for ( ; ; c = *s++) {
301
if (c >= '0' && c <= '9')
303
#if (('Z' - 'A') == 25)
304
else if (c >= 'A' && c <= 'Z')
306
else if (c >= 'a' && c <= 'z')
308
#elif APR_CHARSET_EBCDIC
309
else if (c >= 'A' && c <= 'I')
311
else if (c >= 'J' && c <= 'R')
313
else if (c >= 'S' && c <= 'Z')
315
else if (c >= 'a' && c <= 'i')
317
else if (c >= 'j' && c <= 'r')
319
else if (c >= 's' && c <= 'z')
322
#error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
329
if ( (any < 0) /* already noted an over/under flow - short circuit */
330
|| (neg && (val > acc || (val -= c) > acc)) /* underflow */
331
|| (!neg && (val < acc || (val += c) < acc))) { /* overflow */
332
any = -1; /* once noted, over/underflows never go away */
333
#ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
343
acc = neg ? INT64_MIN : INT64_MAX;
349
*endptr = (char *)(any ? s - 1 : nptr);
354
APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
356
return apr_strtoi64(buf, NULL, 10);
359
APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
361
const int BUFFER_SIZE = sizeof(int) * 3 + 2;
362
char *buf = apr_palloc(p, BUFFER_SIZE);
363
char *start = buf + BUFFER_SIZE - 1;
374
*--start = '0' + (n % 10);
383
APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
385
const int BUFFER_SIZE = sizeof(long) * 3 + 2;
386
char *buf = apr_palloc(p, BUFFER_SIZE);
387
char *start = buf + BUFFER_SIZE - 1;
398
*--start = (char)('0' + (n % 10));
407
APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
409
const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
410
char *buf = apr_palloc(p, BUFFER_SIZE);
411
char *start = buf + BUFFER_SIZE - 1;
422
*--start = '0' + (char)(n % 10);
431
APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
433
const char ord[] = "KMGTPE";
438
return strcpy(buf, " - ");
441
if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
442
return strcpy(buf, "****");
446
remain = (int)(size & 1023);
452
if (size < 9 || (size == 9 && remain < 973)) {
453
if ((remain = ((remain * 5) + 256) / 512) >= 10)
455
if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
456
return strcpy(buf, "****");
461
if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
462
return strcpy(buf, "****");