2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
30 * Basic resizable string support, to simplify manipulating arbitrary-sized
31 * strings while minimizing heap activity.
44 #include "archive_private.h"
45 #include "archive_string.h"
47 struct archive_string *
48 __archive_string_append(struct archive_string *as, const char *p, size_t s)
50 if (__archive_string_ensure(as, as->length + s + 1) == NULL)
51 __archive_errx(1, "Out of memory");
52 memcpy(as->s + as->length, p, s);
53 as->s[as->length + s] = 0;
59 __archive_string_copy(struct archive_string *dest, struct archive_string *src)
64 if (__archive_string_ensure(dest, src->length + 1) == NULL)
65 __archive_errx(1, "Out of memory");
66 memcpy(dest->s, src->s, src->length);
67 dest->length = src->length;
68 dest->s[dest->length] = 0;
73 __archive_string_concat(struct archive_string *dest, struct archive_string *src)
75 if (src->length > 0) {
76 if (__archive_string_ensure(dest, dest->length + src->length + 1) == NULL)
77 __archive_errx(1, "Out of memory");
78 memcpy(dest->s + dest->length, src->s, src->length);
79 dest->length += src->length;
80 dest->s[dest->length] = 0;
85 __archive_string_free(struct archive_string *as)
88 as->buffer_length = 0;
95 /* Returns NULL on any allocation failure. */
96 struct archive_string *
97 __archive_string_ensure(struct archive_string *as, size_t s)
99 /* If buffer is already big enough, don't reallocate. */
100 if (as->s && (s <= as->buffer_length))
104 * Growing the buffer at least exponentially ensures that
105 * append operations are always linear in the number of
106 * characters appended. Using a smaller growth rate for
107 * larger buffers reduces memory waste somewhat at the cost of
108 * a larger constant factor.
110 if (as->buffer_length < 32)
111 /* Start with a minimum 32-character buffer. */
112 as->buffer_length = 32;
113 else if (as->buffer_length < 8192)
114 /* Buffers under 8k are doubled for speed. */
115 as->buffer_length *= 2;
117 /* Buffers 8k and over grow by at least 25% each time. */
118 size_t old_length = as->buffer_length;
119 as->buffer_length = (as->buffer_length * 5) / 4;
120 /* Be safe: If size wraps, release buffer and return NULL. */
121 if (as->buffer_length < old_length) {
128 * The computation above is a lower limit to how much we'll
129 * grow the buffer. In any case, we have to grow it enough to
132 if (as->buffer_length < s)
133 as->buffer_length = s;
134 /* Now we can reallocate the buffer. */
135 as->s = (char *)realloc(as->s, as->buffer_length);
141 struct archive_string *
142 __archive_strncat(struct archive_string *as, const char *p, size_t n)
147 /* Like strlen(p), except won't examine positions beyond p[n]. */
150 while (*pp && s < n) {
154 return (__archive_string_append(as, p, s));
157 struct archive_string *
158 __archive_strappend_char(struct archive_string *as, char c)
160 return (__archive_string_append(as, &c, 1));
163 struct archive_string *
164 __archive_strappend_int(struct archive_string *as, int d, int base)
166 static const char *digits = "0123456789abcdef";
169 __archive_strappend_char(as, '-');
173 __archive_strappend_int(as, d/base, base);
174 __archive_strappend_char(as, digits[d % base]);
179 * Home-grown wctomb for UTF-8.
182 my_wctomb_utf8(char *p, wchar_t wc)
185 /* UTF-8 doesn't use shift states. */
192 p[0] = 0xc0 | ((wc >> 6) & 0x1f);
193 p[1] = 0x80 | (wc & 0x3f);
197 p[0] = 0xe0 | ((wc >> 12) & 0x0f);
198 p[1] = 0x80 | ((wc >> 6) & 0x3f);
199 p[2] = 0x80 | (wc & 0x3f);
202 if (wc <= 0x1fffff) {
203 p[0] = 0xf0 | ((wc >> 18) & 0x07);
204 p[1] = 0x80 | ((wc >> 12) & 0x3f);
205 p[2] = 0x80 | ((wc >> 6) & 0x3f);
206 p[3] = 0x80 | (wc & 0x3f);
209 /* Unicode has no codes larger than 0x1fffff. */
211 * Awkward point: UTF-8 <-> wchar_t conversions
218 my_wcstombs(struct archive_string *as, const wchar_t *w,
219 int (*func)(char *, wchar_t))
225 /* Clear the shift state before starting. */
226 (*func)(NULL, L'\0');
229 * Convert one wide char at a time into 'buff', whenever that
230 * fills, append it to the string.
233 while (*w != L'\0') {
234 /* Flush the buffer when we have <=16 bytes free. */
235 /* (No encoding has a single character >16 bytes.) */
236 if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - 16)) {
238 archive_strcat(as, buff);
241 n = (*func)(p, *w++);
247 archive_strcat(as, buff);
252 * Translates a wide character string into UTF-8 and appends
253 * to the archive_string. Note: returns NULL if conversion fails.
255 struct archive_string *
256 __archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
258 if (my_wcstombs(as, w, my_wctomb_utf8))
264 * Translates a wide character string into current locale character set
265 * and appends to the archive_string. Note: returns NULL if conversion
268 struct archive_string *
269 __archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
272 if (my_wcstombs(as, w, wctomb))
275 /* TODO: Can we do better than this? Are there platforms
276 * that have locale support but don't have wctomb()? */
277 if (my_wcstombs(as, w, my_wctomb_utf8))
285 * Home-grown mbtowc for UTF-8. Some systems lack UTF-8
286 * (or even lack mbtowc()) and we need UTF-8 support for pax
287 * format. So please don't replace this with a call to the
288 * standard mbtowc() function!
291 my_mbtowc_utf8(wchar_t *pwc, const char *s, size_t n)
295 /* Standard behavior: a NULL value for 's' just resets shift state. */
298 /* If length argument is zero, don't look at the first character. */
303 * Decode 1-4 bytes depending on the value of the first byte.
305 ch = (unsigned char)*s;
307 return (0); /* Standard: return 0 for end-of-string. */
309 if ((ch & 0x80) == 0) {
313 if ((ch & 0xe0) == 0xc0) {
316 if ((s[1] & 0xc0) != 0x80) return (-1);
317 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
320 if ((ch & 0xf0) == 0xe0) {
323 if ((s[1] & 0xc0) != 0x80) return (-1);
324 if ((s[2] & 0xc0) != 0x80) return (-1);
325 *pwc = ((ch & 0x0f) << 12)
326 | ((s[1] & 0x3f) << 6)
330 if ((ch & 0xf8) == 0xf0) {
333 if ((s[1] & 0xc0) != 0x80) return (-1);
334 if ((s[2] & 0xc0) != 0x80) return (-1);
335 if ((s[3] & 0xc0) != 0x80) return (-1);
336 *pwc = ((ch & 0x07) << 18)
337 | ((s[1] & 0x3f) << 12)
338 | ((s[2] & 0x3f) << 6)
342 /* Invalid first byte. */
347 * Return a wide-character string by converting this archive_string
351 __archive_string_utf8_w(struct archive_string *as)
358 ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
360 __archive_errx(1, "Out of memory");
364 while (*src != '\0') {
365 n = my_mbtowc_utf8(dest, src, 8);