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.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.
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 *);
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;
67 __archive_string_copy(struct archive_string
*dest
, struct archive_string
*src
)
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;
81 __archive_string_free(struct archive_string
*as
)
84 as
->buffer_length
= 0;
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
))
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;
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
) {
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
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
);
137 struct archive_string
*
138 __archive_strncat(struct archive_string
*as
, const char *p
, size_t n
)
143 /* Like strlen(p), except won't examine positions beyond p[n]. */
146 while (*pp
&& s
< n
) {
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";
165 __archive_strappend_char(as
, '-');
169 __archive_strappend_int(as
, d
/base
, base
);
170 __archive_strappend_char(as
, digits
[d
% base
]);
175 * Home-grown wcrtomb for UTF-8.
178 my_wcrtomb_utf8(char *p
, wchar_t wc
, mbstate_t *s
)
180 (void)s
; /* UNUSED */
189 p
[0] = 0xc0 | ((wc
>> 6) & 0x1f);
190 p
[1] = 0x80 | (wc
& 0x3f);
194 p
[0] = 0xe0 | ((wc
>> 12) & 0x0f);
195 p
[1] = 0x80 | ((wc
>> 6) & 0x3f);
196 p
[2] = 0x80 | (wc
& 0x3f);
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);
206 /* Unicode has no codes larger than 0x1fffff. */
208 * Awkward point: UTF-8 <-> wchar_t conversions
215 my_wcstombs(struct archive_string
*as
, const wchar_t *w
,
216 size_t (*func
)(char *, wchar_t, mbstate_t *))
220 mbstate_t shift_state
;
224 * Convert one wide char at a time into 'buff', whenever that
225 * fills, append it to the string.
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)) {
234 archive_strcat(as
, buff
);
237 n
= (*func
)(p
, *w
++, &shift_state
);
243 archive_strcat(as
, buff
);
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
))
260 * Translates a wide character string into current locale character set
261 * and appends to the archive_string. Note: returns NULL if conversion
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
))
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!
282 my_mbrtowc_utf8(wchar_t *pwc
, const char *s
, size_t n
, mbstate_t *ps
)
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. */
296 /* If length argument is zero, don't look at the first character. */
301 * Decode 1-4 bytes depending on the value of the first byte.
303 ch
= (unsigned char)*s
;
305 return (0); /* Standard: return 0 for end-of-string. */
307 if ((ch
& 0x80) == 0) {
311 if ((ch
& 0xe0) == 0xc0) {
314 if ((s
[1] & 0xc0) != 0x80) return (size_t)-1;
315 *pwc
= ((ch
& 0x1f) << 6) | (s
[1] & 0x3f);
318 if ((ch
& 0xf0) == 0xe0) {
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)
328 if ((ch
& 0xf8) == 0xf0) {
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)
340 /* Invalid first byte. */
345 * Return a wide-character string by converting this archive_string
349 __archive_string_utf8_w(struct archive_string
*as
)
356 ws
= (wchar_t *)malloc((as
->length
+ 1) * sizeof(wchar_t));
358 __archive_errx(1, "Out of memory");
362 while (*src
!= '\0') {
363 n
= my_mbrtowc_utf8(dest
, src
, 8, NULL
);
366 if (n
== (size_t)-1 || n
== (size_t)-2) {