upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / strings / apr_strings.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * Copyright (c) 1990, 1993
18  *      The Regents of the University of California.  All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
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.
35  *
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
46  * SUCH DAMAGE.
47  */
48
49 #include "apr.h"
50 #include "apr_strings.h"
51 #include "apr_general.h"
52 #include "apr_private.h"
53 #include "apr_lib.h"
54 #define APR_WANT_STDIO
55 #define APR_WANT_STRFUNC
56 #include "apr_want.h"
57
58 #ifdef HAVE_STDDEF_H
59 #include <stddef.h> /* NULL */
60 #endif
61
62 #ifdef HAVE_STDLIB_H
63 #include <stdlib.h> /* strtol and strtoll */
64 #endif
65
66 /** this is used to cache lengths in apr_pstrcat */
67 #define MAX_SAVED_LENGTHS  6
68
69 APR_DECLARE(char *) apr_pstrdup(apr_pool_t *a, const char *s)
70 {
71     char *res;
72     apr_size_t len;
73
74     if (s == NULL) {
75         return NULL;
76     }
77     len = strlen(s) + 1;
78     res = apr_palloc(a, len);
79     memcpy(res, s, len);
80     return res;
81 }
82
83 APR_DECLARE(char *) apr_pstrndup(apr_pool_t *a, const char *s, apr_size_t n)
84 {
85     char *res;
86     const char *end;
87
88     if (s == NULL) {
89         return NULL;
90     }
91     end = memchr(s, '\0', n);
92     if (end != NULL)
93         n = end - s;
94     res = apr_palloc(a, n + 1);
95     memcpy(res, s, n);
96     res[n] = '\0';
97     return res;
98 }
99
100 APR_DECLARE(char *) apr_pstrmemdup(apr_pool_t *a, const char *s, apr_size_t n)
101 {
102     char *res;
103
104     if (s == NULL) {
105         return NULL;
106     }
107     res = apr_palloc(a, n + 1);
108     memcpy(res, s, n);
109     res[n] = '\0';
110     return res;
111 }
112
113 APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
114 {
115     void *res;
116
117     if (m == NULL)
118         return NULL;
119     res = apr_palloc(a, n);
120     memcpy(res, m, n);
121     return res;
122 }
123
124 APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
125 {
126     char *cp, *argp, *res;
127     apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
128     int nargs = 0;
129
130     /* Pass one --- find length of required string */
131
132     apr_size_t len = 0;
133     va_list adummy;
134
135     va_start(adummy, a);
136
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;
141         }
142         len += cplen;
143     }
144
145     va_end(adummy);
146
147     /* Allocate the required string */
148
149     res = (char *) apr_palloc(a, len + 1);
150     cp = res;
151
152     /* Pass two --- copy the argument strings into the result space */
153
154     va_start(adummy, a);
155
156     nargs = 0;
157     while ((argp = va_arg(adummy, char *)) != NULL) {
158         if (nargs < MAX_SAVED_LENGTHS) {
159             len = saved_lengths[nargs++];
160         }
161         else {
162             len = strlen(argp);
163         }
164  
165         memcpy(cp, argp, len);
166         cp += len;
167     }
168
169     va_end(adummy);
170
171     /* Return the result string */
172
173     *cp = '\0';
174
175     return res;
176 }
177
178 APR_DECLARE(char *) apr_pstrcatv(apr_pool_t *a, const struct iovec *vec,
179                                  apr_size_t nvec, apr_size_t *nbytes)
180 {
181     apr_size_t i;
182     apr_size_t len;
183     const struct iovec *src;
184     char *res;
185     char *dst;
186
187     /* Pass one --- find length of required string */
188     len = 0;
189     src = vec;
190     for (i = nvec; i; i--) {
191         len += src->iov_len;
192         src++;
193     }
194     if (nbytes) {
195         *nbytes = len;
196     }
197
198     /* Allocate the required string */
199     res = (char *) apr_palloc(a, len + 1);
200     
201     /* Pass two --- copy the argument strings into the result space */
202     src = vec;
203     dst = res;
204     for (i = nvec; i; i--) {
205         memcpy(dst, src->iov_base, src->iov_len);
206         dst += src->iov_len;
207         src++;
208     }
209
210     /* Return the result string */
211     *dst = '\0';
212
213     return res;
214 }
215
216 #if (!APR_HAVE_MEMCHR)
217 void *memchr(const void *s, int c, size_t n)
218 {
219     const char *cp;
220
221     for (cp = s; n > 0; n--, cp++) {
222         if (*cp == c)
223             return (char *) cp; /* Casting away the const here */
224     }
225
226     return NULL;
227 }
228 #endif
229
230 #ifndef INT64_MAX
231 #define INT64_MAX  APR_INT64_C(0x7fffffffffffffff)
232 #endif
233 #ifndef INT64_MIN
234 #define INT64_MIN (-APR_INT64_C(0x7fffffffffffffff) - APR_INT64_C(1))
235 #endif
236
237 APR_DECLARE(apr_int64_t) apr_strtoi64(const char *nptr, char **endptr, int base)
238 {
239 #if (APR_HAVE_INT64_STRFN)
240     return APR_INT64_STRFN(nptr, endptr, base);
241 #else
242     const char *s;
243     apr_int64_t acc;
244     apr_int64_t val;
245     int neg, any;
246     char c;
247
248     /*
249      * Skip white space and pick up leading +/- sign if any.
250      * If base is 0, allow 0x for hex and 0 for octal, else
251      * assume decimal; if base is already 16, allow 0x.
252      */
253     s = nptr;
254     do {
255         c = *s++;
256     } while (apr_isspace(c));
257     if (c == '-') {
258         neg = 1;
259         c = *s++;
260     } else {
261         neg = 0;
262         if (c == '+')
263             c = *s++;
264     }
265     if ((base == 0 || base == 16) &&
266         c == '0' && (*s == 'x' || *s == 'X')) {
267             c = s[1];
268             s += 2;
269             base = 16;
270     }
271     if (base == 0)
272         base = c == '0' ? 8 : 10;
273     acc = any = 0;
274     if (base < 2 || base > 36) {
275         errno = EINVAL;
276         if (endptr != NULL)
277             *endptr = (char *)(any ? s - 1 : nptr);
278         return acc;
279     }
280
281     /* The classic bsd implementation requires div/mod operators
282      * to compute a cutoff.  Benchmarking proves that is very, very
283      * evil to some 32 bit processors.  Instead, look for underflow
284      * in both the mult and add/sub operation.  Unlike the bsd impl,
285      * we also work strictly in a signed int64 word as we haven't
286      * implemented the unsigned type in win32.
287      * 
288      * Set 'any' if any `digits' consumed; make it negative to indicate
289      * overflow.
290      */
291     val = 0;
292     for ( ; ; c = *s++) {
293         if (c >= '0' && c <= '9')
294             c -= '0';
295 #if (('Z' - 'A') == 25)
296         else if (c >= 'A' && c <= 'Z')
297             c -= 'A' - 10;
298         else if (c >= 'a' && c <= 'z')
299             c -= 'a' - 10;
300 #elif APR_CHARSET_EBCDIC
301         else if (c >= 'A' && c <= 'I')
302             c -= 'A' - 10;
303         else if (c >= 'J' && c <= 'R')
304             c -= 'J' - 19;
305         else if (c >= 'S' && c <= 'Z')
306             c -= 'S' - 28;
307         else if (c >= 'a' && c <= 'i')
308             c -= 'a' - 10;
309         else if (c >= 'j' && c <= 'r')
310             c -= 'j' - 19;
311         else if (c >= 's' && c <= 'z')
312             c -= 'z' - 28;
313 #else
314 #error "CANNOT COMPILE apr_strtoi64(), only ASCII and EBCDIC supported" 
315 #endif
316         else
317             break;
318         if (c >= base)
319             break;
320         val *= base;
321         if ( (any < 0)  /* already noted an over/under flow - short circuit */
322            || (neg && (val > acc || (val -= c) > acc)) /* underflow */
323            || (!neg && (val < acc || (val += c) < acc))) {       /* overflow */
324             any = -1;   /* once noted, over/underflows never go away */
325 #ifdef APR_STRTOI64_OVERFLOW_IS_BAD_CHAR
326             break;
327 #endif
328         } else {
329             acc = val;
330             any = 1;
331         }
332     }
333
334     if (any < 0) {
335         acc = neg ? INT64_MIN : INT64_MAX;
336         errno = ERANGE;
337     } else if (!any) {
338         errno = EINVAL;
339     }
340     if (endptr != NULL)
341         *endptr = (char *)(any ? s - 1 : nptr);
342     return (acc);
343 #endif
344 }
345
346 APR_DECLARE(apr_int64_t) apr_atoi64(const char *buf)
347 {
348     return apr_strtoi64(buf, NULL, 10);
349 }
350
351 APR_DECLARE(char *) apr_itoa(apr_pool_t *p, int n)
352 {
353     const int BUFFER_SIZE = sizeof(int) * 3 + 2;
354     char *buf = apr_palloc(p, BUFFER_SIZE);
355     char *start = buf + BUFFER_SIZE - 1;
356     int negative;
357     if (n < 0) {
358         negative = 1;
359         n = -n;
360     }
361     else {
362         negative = 0;
363     }
364     *start = 0;
365     do {
366         *--start = '0' + (n % 10);
367         n /= 10;
368     } while (n);
369     if (negative) {
370         *--start = '-';
371     }
372     return start;
373 }
374
375 APR_DECLARE(char *) apr_ltoa(apr_pool_t *p, long n)
376 {
377     const int BUFFER_SIZE = sizeof(long) * 3 + 2;
378     char *buf = apr_palloc(p, BUFFER_SIZE);
379     char *start = buf + BUFFER_SIZE - 1;
380     int negative;
381     if (n < 0) {
382         negative = 1;
383         n = -n;
384     }
385     else {
386         negative = 0;
387     }
388     *start = 0;
389     do {
390         *--start = (char)('0' + (n % 10));
391         n /= 10;
392     } while (n);
393     if (negative) {
394         *--start = '-';
395     }
396     return start;
397 }
398
399 APR_DECLARE(char *) apr_off_t_toa(apr_pool_t *p, apr_off_t n)
400 {
401     const int BUFFER_SIZE = sizeof(apr_off_t) * 3 + 2;
402     char *buf = apr_palloc(p, BUFFER_SIZE);
403     char *start = buf + BUFFER_SIZE - 1;
404     int negative;
405     if (n < 0) {
406         negative = 1;
407         n = -n;
408     }
409     else {
410         negative = 0;
411     }
412     *start = 0;
413     do {
414         *--start = '0' + (char)(n % 10);
415         n /= 10;
416     } while (n);
417     if (negative) {
418         *--start = '-';
419     }
420     return start;
421 }
422
423 APR_DECLARE(char *) apr_strfsize(apr_off_t size, char *buf)
424 {
425     const char ord[] = "KMGTPE";
426     const char *o = ord;
427     int remain;
428
429     if (size < 0) {
430         return strcpy(buf, "  - ");
431     }
432     if (size < 973) {
433         if (apr_snprintf(buf, 5, "%3d ", (int) size) < 0)
434             return strcpy(buf, "****");
435         return buf;
436     }
437     do {
438         remain = (int)(size & 1023);
439         size >>= 10;
440         if (size >= 973) {
441             ++o;
442             continue;
443         }
444         if (size < 9 || (size == 9 && remain < 973)) {
445             if ((remain = ((remain * 5) + 256) / 512) >= 10)
446                 ++size, remain = 0;
447             if (apr_snprintf(buf, 5, "%d.%d%c", (int) size, remain, *o) < 0)
448                 return strcpy(buf, "****");
449             return buf;
450         }
451         if (remain >= 512)
452             ++size;
453         if (apr_snprintf(buf, 5, "%3d%c", (int) size, *o) < 0)
454             return strcpy(buf, "****");
455         return buf;
456     } while (1);
457 }
458