More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / contrib / libarchive / archive_string.c
blob2227afc148c3dedfd88084c4ae0f2e9e5168a116
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_string.c,v 1.6 2004/12/22 06:12:40 kientzle Exp $");
31 * Basic resizable string support, to simplify manipulating arbitrary-sized
32 * strings while minimizing heap activity.
35 #include <stdlib.h>
36 #include <string.h>
38 #include "archive_private.h"
39 #include "archive_string.h"
41 struct archive_string *
42 __archive_string_append(struct archive_string *as, const char *p, size_t s)
44 __archive_string_ensure(as, as->length + s + 1);
45 memcpy(as->s + as->length, p, s);
46 as->s[as->length + s] = 0;
47 as->length += s;
48 return (as);
51 void
52 __archive_string_free(struct archive_string *as)
54 as->length = 0;
55 as->buffer_length = 0;
56 if (as->s != NULL)
57 free(as->s);
60 struct archive_string *
61 __archive_string_ensure(struct archive_string *as, size_t s)
63 if (as->s && (s <= as->buffer_length))
64 return (as);
66 if (as->buffer_length < 32)
67 as->buffer_length = 32;
68 while (as->buffer_length < s)
69 as->buffer_length *= 2;
70 as->s = realloc(as->s, as->buffer_length);
71 /* TODO: Return null instead and fix up all of our callers to
72 * handle this correctly. */
73 if (as->s == NULL)
74 __archive_errx(1, "Out of memory");
75 return (as);
78 struct archive_string *
79 __archive_strncat(struct archive_string *as, const char *p, size_t n)
81 size_t s;
82 const char *pp;
84 /* Like strlen(p), except won't examine positions beyond p[n]. */
85 s = 0;
86 pp = p;
87 while (*pp && s < n) {
88 pp++;
89 s++;
91 return (__archive_string_append(as, p, s));
94 struct archive_string *
95 __archive_strappend_char(struct archive_string *as, char c)
97 return (__archive_string_append(as, &c, 1));
100 struct archive_string *
101 __archive_strappend_int(struct archive_string *as, int d, int base)
103 static const char *digits = "0123457890abcdef";
105 if (d < 0) {
106 __archive_strappend_char(as, '-');
107 d = -d;
109 if (d >= base)
110 __archive_strappend_int(as, d/base, base);
111 __archive_strappend_char(as, digits[d % base]);
112 return (as);