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_pmemdup(a, s, len);
82 APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
90 end = memchr(s, '\0', n);
93 res = apr_palloc(a, n + 1);
99 APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
106 res = apr_palloc(a, n + 1);
112 APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
118 res = apr_palloc(a, n);
123 APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
125 char *cp, *argp, *res;
126 apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
129 /* Pass one --- find length of required string */
136 while ((cp = va_arg(adummy, char *)) != NULL) {
137 apr_size_t cplen = strlen(cp);
138 if (nargs < MAX_SAVED_LENGTHS) {
139 saved_lengths[nargs++] = cplen;
146 /* Allocate the required string */
148 res = (char *) apr_palloc(a, len + 1);
151 /* Pass two --- copy the argument strings into the result space */
156 while ((argp = va_arg(adummy, char *)) != NULL) {
157 if (nargs < MAX_SAVED_LENGTHS) {
158 len = saved_lengths[nargs++];
164 memcpy(cp, argp, len);
170 /* Return the result string */
177 APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
178 apr_size_t nvec, apr_size_t *nbytes)
182 const struct iovec *src;
186 /* Pass one --- find length of required string */
189 for (i = nvec; i; i--) {
197 /* Allocate the required string */
198 res = (char *) apr_palloc(a, len + 1);
200 /* Pass two --- copy the argument strings into the result space */
203 for (i = nvec; i; i--) {
204 memcpy(dst, src->iov_base, src->iov_len);
209 /* Return the result string */
215 #if (!APR_HAVE_MEMCHR)
216 void *memchr(const void *s, int c, size_t n)
220 for (cp = s; n > 0; n--, cp++) {
222 return (char *) cp; /* Casting away the const here */
230 #define INT64_MAX APR_INT64_C(0x7fffffffffffffff)
233 #define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
236 APR_DECLARE(apr_status_t) apr_strtoff(apr_off_t *offset, const char *nptr,
237 char **endptr, int base)
240 *offset = APR_OFF_T_STRFN(nptr, endptr, base);
241 return APR_FROM_OS_ERROR(errno);
244 APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
246 #ifdef APR_INT64_STRFN
248 return APR_INT64_STRFN(nptr, endptr, base);
258 * Skip white space and pick up leading +/- sign if any.
259 * If base is 0, allow 0x for hex and 0 for octal, else
260 * assume decimal; if base is already 16, allow 0x.
265 } while (apr_isspace(c));
274 if ((base == 0 || base == 16) &&
275 c == '0' && (*s == 'x' || *s == 'X')) {
281 base = c == '0' ? 8 : 10;
283 if (base < 2 || base > 36) {
286 *endptr = (char *)(any ? s - 1 : nptr);
290 /* The classic bsd implementation requires div/mod operators
291 * to compute a cutoff. Benchmarking proves that is very, very
292 * evil to some 32 bit processors. Instead, look for underflow
293 * in both the mult and add/sub operation. Unlike the bsd impl,
294 * we also work strictly in a signed int64 word as we haven't
295 * implemented the unsigned type in win32.
297 * Set 'any' if any `digits' consumed; make it negative to indicate
301 for ( ; ; c = *s++) {
302 if (c >= '0' && c <= '9')
304 #if (('Z' - 'A') == 25)
305 else if (c >= 'A' && c <= 'Z')
307 else if (c >= 'a' && c <= 'z')
309 #elif APR_CHARSET_EBCDIC
310 else if (c >= 'A' && c <= 'I')
312 else if (c >= 'J' && c <= 'R')
314 else if (c >= 'S' && c <= 'Z')
316 else if (c >= 'a' && c <= 'i')
318 else if (c >= 'j' && c <= 'r')
320 else if (c >= 's' && c <= 'z')
323 #error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported"
330 if ( (any < 0) /* already noted an over/under flow - short circuit */
331 || (neg && (val > acc || (val -= c) > acc)) /* underflow */
332 || (!neg && (val < acc || (val += c) < acc))) { /* overflow */
333 any = -1; /* once noted, over/underflows never go away */
334 #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
344 acc = neg ? INT64_MIN : INT64_MAX;
350 *endptr = (char *)(any ? s - 1 : nptr);
355 APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
357 return apr_strtoi64(buf, NULL, 10);
360 APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
362 const int BUFFER_SIZE = sizeof(int) * 3 + 2;
363 char *buf = apr_palloc(p, BUFFER_SIZE);
364 char *start = buf + BUFFER_SIZE - 1;
375 *--start = '0' + (n % 10);
384 APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
386 const int BUFFER_SIZE = sizeof(long) * 3 + 2;
387 char *buf = apr_palloc(p, BUFFER_SIZE);
388 char *start = buf + BUFFER_SIZE - 1;
399 *--start = (char)('0' + (n % 10));
408 APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
410 const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
411 char *buf = apr_palloc(p, BUFFER_SIZE);
412 char *start = buf + BUFFER_SIZE - 1;
423 *--start = '0' + (char)(n % 10);
432 APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
434 const char ord[] = "KMGTPE";
439 return strcpy(buf, " - ");
442 if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
443 return strcpy(buf, "****");
447 remain = (int)(size & 1023);
453 if (size < 9 || (size == 9 && remain < 973)) {
454 if ((remain = ((remain * 5) + 256) / 512) >= 10)
456 if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
457 return strcpy(buf, "****");
462 if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
463 return strcpy(buf, "****");