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: src/lib/libarchive/archive_string.c,v 1.16 2008/06/15 11:28:56 kientzle Exp $");
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_free(struct archive_string
*as
)
76 as
->buffer_length
= 0;
83 /* Returns NULL on any allocation failure. */
84 struct archive_string
*
85 __archive_string_ensure(struct archive_string
*as
, size_t s
)
87 /* If buffer is already big enough, don't reallocate. */
88 if (as
->s
&& (s
<= as
->buffer_length
))
92 * Growing the buffer at least exponentially ensures that
93 * append operations are always linear in the number of
94 * characters appended. Using a smaller growth rate for
95 * larger buffers reduces memory waste somewhat at the cost of
96 * a larger constant factor.
98 if (as
->buffer_length
< 32)
99 /* Start with a minimum 32-character buffer. */
100 as
->buffer_length
= 32;
101 else if (as
->buffer_length
< 8192)
102 /* Buffers under 8k are doubled for speed. */
103 as
->buffer_length
*= 2;
105 /* Buffers 8k and over grow by at least 25% each time. */
106 size_t old_length
= as
->buffer_length
;
107 as
->buffer_length
= (as
->buffer_length
* 5) / 4;
108 /* Be safe: If size wraps, release buffer and return NULL. */
109 if (as
->buffer_length
< old_length
) {
116 * The computation above is a lower limit to how much we'll
117 * grow the buffer. In any case, we have to grow it enough to
120 if (as
->buffer_length
< s
)
121 as
->buffer_length
= s
;
122 /* Now we can reallocate the buffer. */
123 as
->s
= (char *)realloc(as
->s
, as
->buffer_length
);
129 struct archive_string
*
130 __archive_strncat(struct archive_string
*as
, const char *p
, size_t n
)
135 /* Like strlen(p), except won't examine positions beyond p[n]. */
138 while (*pp
&& s
< n
) {
142 return (__archive_string_append(as
, p
, s
));
145 struct archive_string
*
146 __archive_strappend_char(struct archive_string
*as
, char c
)
148 return (__archive_string_append(as
, &c
, 1));
151 struct archive_string
*
152 __archive_strappend_int(struct archive_string
*as
, int d
, int base
)
154 static const char *digits
= "0123456789abcdef";
157 __archive_strappend_char(as
, '-');
161 __archive_strappend_int(as
, d
/base
, base
);
162 __archive_strappend_char(as
, digits
[d
% base
]);
167 * Home-grown wctomb for UTF-8.
170 my_wctomb_utf8(char *p
, wchar_t wc
)
173 /* UTF-8 doesn't use shift states. */
180 p
[0] = 0xc0 | ((wc
>> 6) & 0x1f);
181 p
[1] = 0x80 | (wc
& 0x3f);
185 p
[0] = 0xe0 | ((wc
>> 12) & 0x0f);
186 p
[1] = 0x80 | ((wc
>> 6) & 0x3f);
187 p
[2] = 0x80 | (wc
& 0x3f);
190 if (wc
<= 0x1fffff) {
191 p
[0] = 0xf0 | ((wc
>> 18) & 0x07);
192 p
[1] = 0x80 | ((wc
>> 12) & 0x3f);
193 p
[2] = 0x80 | ((wc
>> 6) & 0x3f);
194 p
[3] = 0x80 | (wc
& 0x3f);
197 /* Unicode has no codes larger than 0x1fffff. */
199 * Awkward point: UTF-8 <-> wchar_t conversions
206 my_wcstombs(struct archive_string
*as
, const wchar_t *w
,
207 int (*func
)(char *, wchar_t))
213 /* Clear the shift state before starting. */
214 (*func
)(NULL
, L
'\0');
217 * Convert one wide char at a time into 'buff', whenever that
218 * fills, append it to the string.
221 while (*w
!= L
'\0') {
222 /* Flush the buffer when we have <=16 bytes free. */
223 /* (No encoding has a single character >16 bytes.) */
224 if ((size_t)(p
- buff
) >= (size_t)(sizeof(buff
) - 16)) {
226 archive_strcat(as
, buff
);
229 n
= (*func
)(p
, *w
++);
235 archive_strcat(as
, buff
);
240 * Translates a wide character string into UTF-8 and appends
241 * to the archive_string. Note: returns NULL if conversion fails.
243 struct archive_string
*
244 __archive_strappend_w_utf8(struct archive_string
*as
, const wchar_t *w
)
246 if (my_wcstombs(as
, w
, my_wctomb_utf8
))
252 * Translates a wide character string into current locale character set
253 * and appends to the archive_string. Note: returns NULL if conversion
256 struct archive_string
*
257 __archive_strappend_w_mbs(struct archive_string
*as
, const wchar_t *w
)
260 if (my_wcstombs(as
, w
, wctomb
))
263 /* TODO: Can we do better than this? Are there platforms
264 * that have locale support but don't have wctomb()? */
265 if (my_wcstombs(as
, w
, my_wctomb_utf8
))
273 * Home-grown mbtowc for UTF-8. Some systems lack UTF-8
274 * (or even lack mbtowc()) and we need UTF-8 support for pax
275 * format. So please don't replace this with a call to the
276 * standard mbtowc() function!
279 my_mbtowc_utf8(wchar_t *pwc
, const char *s
, size_t n
)
283 /* Standard behavior: a NULL value for 's' just resets shift state. */
286 /* If length argument is zero, don't look at the first character. */
291 * Decode 1-4 bytes depending on the value of the first byte.
293 ch
= (unsigned char)*s
;
295 return (0); /* Standard: return 0 for end-of-string. */
297 if ((ch
& 0x80) == 0) {
301 if ((ch
& 0xe0) == 0xc0) {
304 if ((s
[1] & 0xc0) != 0x80) return (-1);
305 *pwc
= ((ch
& 0x1f) << 6) | (s
[1] & 0x3f);
308 if ((ch
& 0xf0) == 0xe0) {
311 if ((s
[1] & 0xc0) != 0x80) return (-1);
312 if ((s
[2] & 0xc0) != 0x80) return (-1);
313 *pwc
= ((ch
& 0x0f) << 12)
314 | ((s
[1] & 0x3f) << 6)
318 if ((ch
& 0xf8) == 0xf0) {
321 if ((s
[1] & 0xc0) != 0x80) return (-1);
322 if ((s
[2] & 0xc0) != 0x80) return (-1);
323 if ((s
[3] & 0xc0) != 0x80) return (-1);
324 *pwc
= ((ch
& 0x07) << 18)
325 | ((s
[1] & 0x3f) << 12)
326 | ((s
[2] & 0x3f) << 6)
330 /* Invalid first byte. */
335 * Return a wide-character string by converting this archive_string
339 __archive_string_utf8_w(struct archive_string
*as
)
346 ws
= (wchar_t *)malloc((as
->length
+ 1) * sizeof(wchar_t));
348 __archive_errx(1, "Out of memory");
352 while (*src
!= '\0') {
353 n
= my_mbtowc_utf8(dest
, src
, 8);