1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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
249 return APR_INT64_STRFN(nptr, endptr, base);
259 * Skip white space and pick up leading +/- sign if any.
260 * If base is 0, allow 0x for hex and 0 for octal, else
261 * assume decimal; if base is already 16, allow 0x.
266 } while (apr_isspace(c));
275 if ((base == 0 || base == 16) &&
276 c == '0' && (*s == 'x' || *s == 'X')) {
282 base = c == '0' ? 8 : 10;
284 if (base < 2 || base > 36) {
287 *endptr = (char *)(any ? s - 1 : nptr);
291 /* The classic bsd implementation requires div/mod operators
292 * to compute a cutoff. Benchmarking proves that is very, very
293 * evil to some 32 bit processors. Instead, look for underflow
294 * in both the mult and add/sub operation. Unlike the bsd impl,
295 * we also work strictly in a signed int64 word as we haven't
296 * implemented the unsigned type in win32.
298 * Set 'any' if any `digits' consumed; make it negative to indicate
302 for ( ; ; c = *s++) {
303 if (c >= '0' && c <= '9')
305 #if (('Z' - 'A') == 25)
306 else if (c >= 'A' && c <= 'Z')
308 else if (c >= 'a' && c <= 'z')
310 #elif APR_CHARSET_EBCDIC
311 else if (c >= 'A' && c <= 'I')
313 else if (c >= 'J' && c <= 'R')
315 else if (c >= 'S' && c <= 'Z')
317 else if (c >= 'a' && c <= 'i')
319 else if (c >= 'j' && c <= 'r')
321 else if (c >= 's' && c <= 'z')
324 #error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
331 if ( (any < 0) /* already noted an over/under flow - short circuit */
332 || (neg && (val > acc || (val -= c) > acc)) /* underflow */
333 || (!neg && (val < acc || (val += c) < acc))) { /* overflow */
334 any = -1; /* once noted, over/underflows never go away */
335 #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
345 acc = neg ? INT64_MIN : INT64_MAX;
351 *endptr = (char *)(any ? s - 1 : nptr);
356 APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
358 return apr_strtoi64(buf, NULL, 10);
361 APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
363 const int BUFFER_SIZE = sizeof(int) * 3 + 2;
364 char *buf = apr_palloc(p, BUFFER_SIZE);
365 char *start = buf + BUFFER_SIZE - 1;
376 *--start = '0' + (n % 10);
385 APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
387 const int BUFFER_SIZE = sizeof(long) * 3 + 2;
388 char *buf = apr_palloc(p, BUFFER_SIZE);
389 char *start = buf + BUFFER_SIZE - 1;
400 *--start = (char)('0' + (n % 10));
409 APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
411 const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
412 char *buf = apr_palloc(p, BUFFER_SIZE);
413 char *start = buf + BUFFER_SIZE - 1;
424 *--start = '0' + (char)(n % 10);
433 APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
435 const char ord[] = "KMGTPE";
440 return strcpy(buf, " - ");
443 if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
444 return strcpy(buf, "****");
448 remain = (int)(size & 1023);
454 if (size < 9 || (size == 9 && remain < 973)) {
455 if ((remain = ((remain * 5) + 256) / 512) >= 10)
457 if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
458 return strcpy(buf, "****");
463 if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
464 return strcpy(buf, "****");