Import libarchive-2.2.5 which fixes a forgotten 'break'. Without this,
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_string.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_string.c,v 1.9 2007/01/13 03:30:14 kientzle Exp $");
28
29 /*
30  * Basic resizable string support, to simplify manipulating arbitrary-sized
31  * strings while minimizing heap activity.
32  */
33
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40
41 #include "archive_private.h"
42 #include "archive_string.h"
43
44 struct archive_string *
45 __archive_string_append(struct archive_string *as, const char *p, size_t s)
46 {
47         __archive_string_ensure(as, as->length + s + 1);
48         memcpy(as->s + as->length, p, s);
49         as->s[as->length + s] = 0;
50         as->length += s;
51         return (as);
52 }
53
54 void
55 __archive_string_free(struct archive_string *as)
56 {
57         as->length = 0;
58         as->buffer_length = 0;
59         if (as->s != NULL)
60                 free(as->s);
61 }
62
63 struct archive_string *
64 __archive_string_ensure(struct archive_string *as, size_t s)
65 {
66         if (as->s && (s <= as->buffer_length))
67                 return (as);
68
69         if (as->buffer_length < 32)
70                 as->buffer_length = 32;
71         while (as->buffer_length < s)
72                 as->buffer_length *= 2;
73         as->s = (char *)realloc(as->s, as->buffer_length);
74         /* TODO: Return null instead and fix up all of our callers to
75          * handle this correctly. */
76         if (as->s == NULL)
77                 __archive_errx(1, "Out of memory");
78         return (as);
79 }
80
81 struct archive_string *
82 __archive_strncat(struct archive_string *as, const char *p, size_t n)
83 {
84         size_t s;
85         const char *pp;
86
87         /* Like strlen(p), except won't examine positions beyond p[n]. */
88         s = 0;
89         pp = p;
90         while (*pp && s < n) {
91                 pp++;
92                 s++;
93         }
94         return (__archive_string_append(as, p, s));
95 }
96
97 struct archive_string *
98 __archive_strappend_char(struct archive_string *as, char c)
99 {
100         return (__archive_string_append(as, &c, 1));
101 }
102
103 struct archive_string *
104 __archive_strappend_int(struct archive_string *as, int d, int base)
105 {
106         static const char *digits = "0123456789abcdef";
107
108         if (d < 0) {
109                 __archive_strappend_char(as, '-');
110                 d = -d;
111         }
112         if (d >= base)
113                 __archive_strappend_int(as, d/base, base);
114         __archive_strappend_char(as, digits[d % base]);
115         return (as);
116 }