Import libarchive-2.5.4b.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_string.c
blob7c378deead43bdde3050c35853f2acfe1cfd48ed
1 /*-
2 * Copyright (c) 2003-2007 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 * 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: src/lib/libarchive/archive_string.c,v 1.11 2007/07/15 19:13:59 kientzle Exp $");
30 * Basic resizable string support, to simplify manipulating arbitrary-sized
31 * strings while minimizing heap activity.
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_WCHAR_H
41 #include <wchar.h>
42 #endif
44 #ifdef __sgi
46 * The following prototype is missing on IRXI,
47 * even though the function is implemented in libc.
49 size_t wcrtomb(char *, wchar_t, mbstate_t *);
50 #endif
52 #include "archive_private.h"
53 #include "archive_string.h"
55 struct archive_string *
56 __archive_string_append(struct archive_string *as, const char *p, size_t s)
58 if (__archive_string_ensure(as, as->length + s + 1) == NULL)
59 __archive_errx(1, "Out of memory");
60 memcpy(as->s + as->length, p, s);
61 as->s[as->length + s] = 0;
62 as->length += s;
63 return (as);
66 void
67 __archive_string_copy(struct archive_string *dest, struct archive_string *src)
69 if (src->length == 0)
70 dest->length = 0;
71 else {
72 if (__archive_string_ensure(dest, src->length + 1) == NULL)
73 __archive_errx(1, "Out of memory");
74 memcpy(dest->s, src->s, src->length);
75 dest->length = src->length;
76 dest->s[dest->length] = 0;
80 void
81 __archive_string_free(struct archive_string *as)
83 as->length = 0;
84 as->buffer_length = 0;
85 if (as->s != NULL) {
86 free(as->s);
87 as->s = NULL;
91 /* Returns NULL on any allocation failure. */
92 struct archive_string *
93 __archive_string_ensure(struct archive_string *as, size_t s)
95 /* If buffer is already big enough, don't reallocate. */
96 if (as->s && (s <= as->buffer_length))
97 return (as);
100 * Growing the buffer at least exponentially ensures that
101 * append operations are always linear in the number of
102 * characters appended. Using a smaller growth rate for
103 * larger buffers reduces memory waste somewhat at the cost of
104 * a larger constant factor.
106 if (as->buffer_length < 32)
107 /* Start with a minimum 32-character buffer. */
108 as->buffer_length = 32;
109 else if (as->buffer_length < 8192)
110 /* Buffers under 8k are doubled for speed. */
111 as->buffer_length *= 2;
112 else {
113 /* Buffers 8k and over grow by at least 25% each time. */
114 size_t old_length = as->buffer_length;
115 as->buffer_length = (as->buffer_length * 5) / 4;
116 /* Be safe: If size wraps, release buffer and return NULL. */
117 if (as->buffer_length < old_length) {
118 free(as->s);
119 as->s = NULL;
120 return (NULL);
124 * The computation above is a lower limit to how much we'll
125 * grow the buffer. In any case, we have to grow it enough to
126 * hold the request.
128 if (as->buffer_length < s)
129 as->buffer_length = s;
130 /* Now we can reallocate the buffer. */
131 as->s = (char *)realloc(as->s, as->buffer_length);
132 if (as->s == NULL)
133 return (NULL);
134 return (as);
137 struct archive_string *
138 __archive_strncat(struct archive_string *as, const char *p, size_t n)
140 size_t s;
141 const char *pp;
143 /* Like strlen(p), except won't examine positions beyond p[n]. */
144 s = 0;
145 pp = p;
146 while (*pp && s < n) {
147 pp++;
148 s++;
150 return (__archive_string_append(as, p, s));
153 struct archive_string *
154 __archive_strappend_char(struct archive_string *as, char c)
156 return (__archive_string_append(as, &c, 1));
159 struct archive_string *
160 __archive_strappend_int(struct archive_string *as, int d, int base)
162 static const char *digits = "0123456789abcdef";
164 if (d < 0) {
165 __archive_strappend_char(as, '-');
166 d = -d;
168 if (d >= base)
169 __archive_strappend_int(as, d/base, base);
170 __archive_strappend_char(as, digits[d % base]);
171 return (as);
175 * Home-grown wcrtomb for UTF-8.
177 static size_t
178 my_wcrtomb_utf8(char *p, wchar_t wc, mbstate_t *s)
180 (void)s; /* UNUSED */
182 if (p == NULL)
183 return (0);
184 if (wc <= 0x7f) {
185 p[0] = (char)wc;
186 return (1);
188 if (wc <= 0x7ff) {
189 p[0] = 0xc0 | ((wc >> 6) & 0x1f);
190 p[1] = 0x80 | (wc & 0x3f);
191 return (2);
193 if (wc <= 0xffff) {
194 p[0] = 0xe0 | ((wc >> 12) & 0x0f);
195 p[1] = 0x80 | ((wc >> 6) & 0x3f);
196 p[2] = 0x80 | (wc & 0x3f);
197 return (3);
199 if (wc <= 0x1fffff) {
200 p[0] = 0xf0 | ((wc >> 18) & 0x07);
201 p[1] = 0x80 | ((wc >> 12) & 0x3f);
202 p[2] = 0x80 | ((wc >> 6) & 0x3f);
203 p[3] = 0x80 | (wc & 0x3f);
204 return (4);
206 /* Unicode has no codes larger than 0x1fffff. */
208 * Awkward point: UTF-8 <-> wchar_t conversions
209 * can actually fail.
211 return ((size_t)-1);
214 static int
215 my_wcstombs(struct archive_string *as, const wchar_t *w,
216 size_t (*func)(char *, wchar_t, mbstate_t *))
218 size_t n;
219 char *p;
220 mbstate_t shift_state;
221 char buff[256];
224 * Convert one wide char at a time into 'buff', whenever that
225 * fills, append it to the string.
227 p = buff;
228 wcrtomb(NULL, L'\0', &shift_state);
229 while (*w != L'\0') {
230 /* Flush the buffer when we have <=16 bytes free. */
231 /* (No encoding has a single character >16 bytes.) */
232 if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - 16)) {
233 *p = '\0';
234 archive_strcat(as, buff);
235 p = buff;
237 n = (*func)(p, *w++, &shift_state);
238 if (n == (size_t)-1)
239 return (-1);
240 p += n;
242 *p = '\0';
243 archive_strcat(as, buff);
244 return (0);
248 * Translates a wide character string into UTF-8 and appends
249 * to the archive_string. Note: returns NULL if conversion fails.
251 struct archive_string *
252 __archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
254 if (my_wcstombs(as, w, my_wcrtomb_utf8))
255 return (NULL);
256 return (as);
260 * Translates a wide character string into current locale character set
261 * and appends to the archive_string. Note: returns NULL if conversion
262 * fails.
264 * TODO: use my_wcrtomb_utf8 if !HAVE_WCRTOMB (add configure logic first!)
266 struct archive_string *
267 __archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
269 if (my_wcstombs(as, w, wcrtomb))
270 return (NULL);
271 return (as);
276 * Home-grown mbrtowc for UTF-8. Some systems lack UTF-8
277 * (or even lack mbrtowc()) and we need UTF-8 support for pax
278 * format. So please don't replace this with a call to the
279 * standard mbrtowc() function!
281 static size_t
282 my_mbrtowc_utf8(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
284 int ch;
287 * This argument is here to make the prototype identical to the
288 * standard mbrtowc(), so I can build generic string processors
289 * that just accept a pointer to a suitable mbrtowc() function.
291 (void)ps; /* UNUSED */
293 /* Standard behavior: a NULL value for 's' just resets shift state. */
294 if (s == NULL)
295 return (0);
296 /* If length argument is zero, don't look at the first character. */
297 if (n <= 0)
298 return ((size_t)-2);
301 * Decode 1-4 bytes depending on the value of the first byte.
303 ch = (unsigned char)*s;
304 if (ch == 0) {
305 return (0); /* Standard: return 0 for end-of-string. */
307 if ((ch & 0x80) == 0) {
308 *pwc = ch & 0x7f;
309 return (1);
311 if ((ch & 0xe0) == 0xc0) {
312 if (n < 2)
313 return ((size_t)-2);
314 if ((s[1] & 0xc0) != 0x80) return (size_t)-1;
315 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
316 return (2);
318 if ((ch & 0xf0) == 0xe0) {
319 if (n < 3)
320 return ((size_t)-2);
321 if ((s[1] & 0xc0) != 0x80) return (size_t)-1;
322 if ((s[2] & 0xc0) != 0x80) return (size_t)-1;
323 *pwc = ((ch & 0x0f) << 12)
324 | ((s[1] & 0x3f) << 6)
325 | (s[2] & 0x3f);
326 return (3);
328 if ((ch & 0xf8) == 0xf0) {
329 if (n < 4)
330 return ((size_t)-2);
331 if ((s[1] & 0xc0) != 0x80) return (size_t)-1;
332 if ((s[2] & 0xc0) != 0x80) return (size_t)-1;
333 if ((s[3] & 0xc0) != 0x80) return (size_t)-1;
334 *pwc = ((ch & 0x07) << 18)
335 | ((s[1] & 0x3f) << 12)
336 | ((s[2] & 0x3f) << 6)
337 | (s[3] & 0x3f);
338 return (4);
340 /* Invalid first byte. */
341 return ((size_t)-1);
345 * Return a wide-character string by converting this archive_string
346 * from UTF-8.
348 wchar_t *
349 __archive_string_utf8_w(struct archive_string *as)
351 wchar_t *ws, *dest;
352 const char *src;
353 size_t n;
354 int err;
356 ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
357 if (ws == NULL)
358 __archive_errx(1, "Out of memory");
359 err = 0;
360 dest = ws;
361 src = as->s;
362 while (*src != '\0') {
363 n = my_mbrtowc_utf8(dest, src, 8, NULL);
364 if (n == 0)
365 break;
366 if (n == (size_t)-1 || n == (size_t)-2) {
367 free(ws);
368 return (NULL);
370 dest++;
371 src += n;
373 *dest++ = L'\0';
374 return (ws);