]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/archive_string.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libarchive / archive_string.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 /*
30  * Basic resizable string support, to simplify manipulating arbitrary-sized
31  * strings while minimizing heap activity.
32  */
33
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
43
44 #include "archive_private.h"
45 #include "archive_string.h"
46
47 struct archive_string *
48 __archive_string_append(struct archive_string *as, const char *p, size_t s)
49 {
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;
54         as->length += s;
55         return (as);
56 }
57
58 void
59 __archive_string_copy(struct archive_string *dest, struct archive_string *src)
60 {
61         if (src->length == 0)
62                 dest->length = 0;
63         else {
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;
69         }
70 }
71
72 void
73 __archive_string_concat(struct archive_string *dest, struct archive_string *src)
74 {
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;
81         }
82 }
83
84 void
85 __archive_string_free(struct archive_string *as)
86 {
87         as->length = 0;
88         as->buffer_length = 0;
89         if (as->s != NULL) {
90                 free(as->s);
91                 as->s = NULL;
92         }
93 }
94
95 /* Returns NULL on any allocation failure. */
96 struct archive_string *
97 __archive_string_ensure(struct archive_string *as, size_t s)
98 {
99         /* If buffer is already big enough, don't reallocate. */
100         if (as->s && (s <= as->buffer_length))
101                 return (as);
102
103         /*
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.
109          */
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;
116         else {
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) {
122                         free(as->s);
123                         as->s = NULL;
124                         return (NULL);
125                 }
126         }
127         /*
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
130          * hold the request.
131          */
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);
136         if (as->s == NULL)
137                 return (NULL);
138         return (as);
139 }
140
141 struct archive_string *
142 __archive_strncat(struct archive_string *as, const char *p, size_t n)
143 {
144         size_t s;
145         const char *pp;
146
147         /* Like strlen(p), except won't examine positions beyond p[n]. */
148         s = 0;
149         pp = p;
150         while (*pp && s < n) {
151                 pp++;
152                 s++;
153         }
154         return (__archive_string_append(as, p, s));
155 }
156
157 struct archive_string *
158 __archive_strappend_char(struct archive_string *as, char c)
159 {
160         return (__archive_string_append(as, &c, 1));
161 }
162
163 struct archive_string *
164 __archive_strappend_int(struct archive_string *as, int d, int base)
165 {
166         static const char *digits = "0123456789abcdef";
167
168         if (d < 0) {
169                 __archive_strappend_char(as, '-');
170                 d = -d;
171         }
172         if (d >= base)
173                 __archive_strappend_int(as, d/base, base);
174         __archive_strappend_char(as, digits[d % base]);
175         return (as);
176 }
177
178 /*
179  * Home-grown wctomb for UTF-8.
180  */
181 static int
182 my_wctomb_utf8(char *p, wchar_t wc)
183 {
184         if (p == NULL)
185                 /* UTF-8 doesn't use shift states. */
186                 return (0);
187         if (wc <= 0x7f) {
188                 p[0] = (char)wc;
189                 return (1);
190         }
191         if (wc <= 0x7ff) {
192                 p[0] = 0xc0 | ((wc >> 6) & 0x1f);
193                 p[1] = 0x80 | (wc & 0x3f);
194                 return (2);
195         }
196         if (wc <= 0xffff) {
197                 p[0] = 0xe0 | ((wc >> 12) & 0x0f);
198                 p[1] = 0x80 | ((wc >> 6) & 0x3f);
199                 p[2] = 0x80 | (wc & 0x3f);
200                 return (3);
201         }
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);
207                 return (4);
208         }
209         /* Unicode has no codes larger than 0x1fffff. */
210         /*
211          * Awkward point:  UTF-8 <-> wchar_t conversions
212          * can actually fail.
213          */
214         return (-1);
215 }
216
217 static int
218 my_wcstombs(struct archive_string *as, const wchar_t *w,
219     int (*func)(char *, wchar_t))
220 {
221         int n;
222         char *p;
223         char buff[256];
224
225         /* Clear the shift state before starting. */
226         (*func)(NULL, L'\0');
227
228         /*
229          * Convert one wide char at a time into 'buff', whenever that
230          * fills, append it to the string.
231          */
232         p = buff;
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)) {
237                         *p = '\0';
238                         archive_strcat(as, buff);
239                         p = buff;
240                 }
241                 n = (*func)(p, *w++);
242                 if (n == -1)
243                         return (-1);
244                 p += n;
245         }
246         *p = '\0';
247         archive_strcat(as, buff);
248         return (0);
249 }
250
251 /*
252  * Translates a wide character string into UTF-8 and appends
253  * to the archive_string.  Note: returns NULL if conversion fails.
254  */
255 struct archive_string *
256 __archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
257 {
258         if (my_wcstombs(as, w, my_wctomb_utf8))
259                 return (NULL);
260         return (as);
261 }
262
263 /*
264  * Translates a wide character string into current locale character set
265  * and appends to the archive_string.  Note: returns NULL if conversion
266  * fails.
267  */
268 struct archive_string *
269 __archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
270 {
271 #if HAVE_WCTOMB
272         if (my_wcstombs(as, w, wctomb))
273                 return (NULL);
274 #else
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))
278                 return (NULL);
279 #endif
280         return (as);
281 }
282
283
284 /*
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!
289  */
290 static int
291 my_mbtowc_utf8(wchar_t *pwc, const char *s, size_t n)
292 {
293         int ch;
294
295         /* Standard behavior:  a NULL value for 's' just resets shift state. */
296         if (s == NULL)
297                 return (0);
298         /* If length argument is zero, don't look at the first character. */
299         if (n <= 0)
300                 return (-1);
301
302         /*
303          * Decode 1-4 bytes depending on the value of the first byte.
304          */
305         ch = (unsigned char)*s;
306         if (ch == 0) {
307                 return (0); /* Standard:  return 0 for end-of-string. */
308         }
309         if ((ch & 0x80) == 0) {
310                 *pwc = ch & 0x7f;
311                 return (1);
312         }
313         if ((ch & 0xe0) == 0xc0) {
314                 if (n < 2)
315                         return (-1);
316                 if ((s[1] & 0xc0) != 0x80) return (-1);
317                 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
318                 return (2);
319         }
320         if ((ch & 0xf0) == 0xe0) {
321                 if (n < 3)
322                         return (-1);
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)
327                     | (s[2] & 0x3f);
328                 return (3);
329         }
330         if ((ch & 0xf8) == 0xf0) {
331                 if (n < 4)
332                         return (-1);
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)
339                     | (s[3] & 0x3f);
340                 return (4);
341         }
342         /* Invalid first byte. */
343         return (-1);
344 }
345
346 /*
347  * Return a wide-character string by converting this archive_string
348  * from UTF-8.
349  */
350 wchar_t *
351 __archive_string_utf8_w(struct archive_string *as)
352 {
353         wchar_t *ws, *dest;
354         const char *src;
355         int n;
356         int err;
357
358         ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
359         if (ws == NULL)
360                 __archive_errx(1, "Out of memory");
361         err = 0;
362         dest = ws;
363         src = as->s;
364         while (*src != '\0') {
365                 n = my_mbtowc_utf8(dest, src, 8);
366                 if (n == 0)
367                         break;
368                 if (n < 0) {
369                         free(ws);
370                         return (NULL);
371                 }
372                 dest++;
373                 src += n;
374         }
375         *dest++ = L'\0';
376         return (ws);
377 }