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: src/lib/libarchive/archive_string.c,v 1.17 2008/12/06 05:56:43 kientzle Exp $");
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 #if defined(_WIN32) && !defined(__CYGWIN__)
44 #include <windows.h>
45 #endif
46 
47 #include "archive_private.h"
48 #include "archive_string.h"
49 
50 struct archive_string *
51 __archive_string_append(struct archive_string *as, const char *p, size_t s)
52 {
53 	if (__archive_string_ensure(as, as->length + s + 1) == NULL)
54 		__archive_errx(1, "Out of memory");
55 	memcpy(as->s + as->length, p, s);
56 	as->s[as->length + s] = 0;
57 	as->length += s;
58 	return (as);
59 }
60 
61 void
62 __archive_string_copy(struct archive_string *dest, struct archive_string *src)
63 {
64 	if (src->length == 0)
65 		dest->length = 0;
66 	else {
67 		if (__archive_string_ensure(dest, src->length + 1) == NULL)
68 			__archive_errx(1, "Out of memory");
69 		memcpy(dest->s, src->s, src->length);
70 		dest->length = src->length;
71 		dest->s[dest->length] = 0;
72 	}
73 }
74 
75 void
76 __archive_string_concat(struct archive_string *dest, struct archive_string *src)
77 {
78 	if (src->length > 0) {
79 		if (__archive_string_ensure(dest, dest->length + src->length + 1) == NULL)
80 			__archive_errx(1, "Out of memory");
81 		memcpy(dest->s + dest->length, src->s, src->length);
82 		dest->length += src->length;
83 		dest->s[dest->length] = 0;
84 	}
85 }
86 
87 void
88 __archive_string_free(struct archive_string *as)
89 {
90 	as->length = 0;
91 	as->buffer_length = 0;
92 	if (as->s != NULL) {
93 		free(as->s);
94 		as->s = NULL;
95 	}
96 }
97 
98 /* Returns NULL on any allocation failure. */
99 struct archive_string *
100 __archive_string_ensure(struct archive_string *as, size_t s)
101 {
102 	/* If buffer is already big enough, don't reallocate. */
103 	if (as->s && (s <= as->buffer_length))
104 		return (as);
105 
106 	/*
107 	 * Growing the buffer at least exponentially ensures that
108 	 * append operations are always linear in the number of
109 	 * characters appended.  Using a smaller growth rate for
110 	 * larger buffers reduces memory waste somewhat at the cost of
111 	 * a larger constant factor.
112 	 */
113 	if (as->buffer_length < 32)
114 		/* Start with a minimum 32-character buffer. */
115 		as->buffer_length = 32;
116 	else if (as->buffer_length < 8192)
117 		/* Buffers under 8k are doubled for speed. */
118 		as->buffer_length += as->buffer_length;
119 	else {
120 		/* Buffers 8k and over grow by at least 25% each time. */
121 		size_t old_length = as->buffer_length;
122 		as->buffer_length += as->buffer_length / 4;
123 		/* Be safe: If size wraps, release buffer and return NULL. */
124 		if (as->buffer_length < old_length) {
125 			free(as->s);
126 			as->s = NULL;
127 			return (NULL);
128 		}
129 	}
130 	/*
131 	 * The computation above is a lower limit to how much we'll
132 	 * grow the buffer.  In any case, we have to grow it enough to
133 	 * hold the request.
134 	 */
135 	if (as->buffer_length < s)
136 		as->buffer_length = s;
137 	/* Now we can reallocate the buffer. */
138 	as->s = (char *)realloc(as->s, as->buffer_length);
139 	if (as->s == NULL)
140 		return (NULL);
141 	return (as);
142 }
143 
144 struct archive_string *
145 __archive_strncat(struct archive_string *as, const void *_p, size_t n)
146 {
147 	size_t s;
148 	const char *p, *pp;
149 
150 	p = (const char *)_p;
151 
152 	/* Like strlen(p), except won't examine positions beyond p[n]. */
153 	s = 0;
154 	pp = p;
155 	while (*pp && s < n) {
156 		pp++;
157 		s++;
158 	}
159 	return (__archive_string_append(as, p, s));
160 }
161 
162 struct archive_string *
163 __archive_strappend_char(struct archive_string *as, char c)
164 {
165 	return (__archive_string_append(as, &c, 1));
166 }
167 
168 /*
169  * Translates a wide character string into UTF-8 and appends
170  * to the archive_string.  Note: returns NULL if conversion fails,
171  * but still leaves a best-effort conversion in the argument as.
172  */
173 struct archive_string *
174 __archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
175 {
176 	char *p;
177 	unsigned wc;
178 	char buff[256];
179 	struct archive_string *return_val = as;
180 
181 	/*
182 	 * Convert one wide char at a time into 'buff', whenever that
183 	 * fills, append it to the string.
184 	 */
185 	p = buff;
186 	while (*w != L'\0') {
187 		/* Flush the buffer when we have <=16 bytes free. */
188 		/* (No encoding has a single character >16 bytes.) */
189 		if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - 16)) {
190 			*p = '\0';
191 			archive_strcat(as, buff);
192 			p = buff;
193 		}
194 		wc = *w++;
195 		/* If this is a surrogate pair, assemble the full code point.*/
196 		/* Note: wc must not be wchar_t here, because the full code
197 		 * point can be more than 16 bits! */
198 		if (wc >= 0xD800 && wc <= 0xDBff
199 		    && *w >= 0xDC00 && *w <= 0xDFFF) {
200 			wc -= 0xD800;
201 			wc *= 0x400;
202 			wc += (*w - 0xDC00);
203 			wc += 0x10000;
204 			++w;
205 		}
206 		/* Translate code point to UTF8 */
207 		if (wc <= 0x7f) {
208 			*p++ = (char)wc;
209 		} else if (wc <= 0x7ff) {
210 			*p++ = 0xc0 | ((wc >> 6) & 0x1f);
211 			*p++ = 0x80 | (wc & 0x3f);
212 		} else if (wc <= 0xffff) {
213 			*p++ = 0xe0 | ((wc >> 12) & 0x0f);
214 			*p++ = 0x80 | ((wc >> 6) & 0x3f);
215 			*p++ = 0x80 | (wc & 0x3f);
216 		} else if (wc <= 0x1fffff) {
217 			*p++ = 0xf0 | ((wc >> 18) & 0x07);
218 			*p++ = 0x80 | ((wc >> 12) & 0x3f);
219 			*p++ = 0x80 | ((wc >> 6) & 0x3f);
220 			*p++ = 0x80 | (wc & 0x3f);
221 		} else {
222 			/* Unicode has no codes larger than 0x1fffff. */
223 			/* TODO: use \uXXXX escape here instead of ? */
224 			*p++ = '?';
225 			return_val = NULL;
226 		}
227 	}
228 	*p = '\0';
229 	archive_strcat(as, buff);
230 	return (return_val);
231 }
232 
233 static int
234 utf8_to_unicode(int *pwc, const char *s, size_t n)
235 {
236         int ch;
237 
238         /*
239 	 * Decode 1-4 bytes depending on the value of the first byte.
240 	 */
241         ch = (unsigned char)*s;
242 	if (ch == 0) {
243 		return (0); /* Standard:  return 0 for end-of-string. */
244 	}
245 	if ((ch & 0x80) == 0) {
246                 *pwc = ch & 0x7f;
247 		return (1);
248         }
249 	if ((ch & 0xe0) == 0xc0) {
250 		if (n < 2)
251 			return (-1);
252 		if ((s[1] & 0xc0) != 0x80) return (-1);
253                 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
254 		return (2);
255         }
256 	if ((ch & 0xf0) == 0xe0) {
257 		if (n < 3)
258 			return (-1);
259 		if ((s[1] & 0xc0) != 0x80) return (-1);
260 		if ((s[2] & 0xc0) != 0x80) return (-1);
261                 *pwc = ((ch & 0x0f) << 12)
262 		    | ((s[1] & 0x3f) << 6)
263 		    | (s[2] & 0x3f);
264 		return (3);
265         }
266 	if ((ch & 0xf8) == 0xf0) {
267 		if (n < 4)
268 			return (-1);
269 		if ((s[1] & 0xc0) != 0x80) return (-1);
270 		if ((s[2] & 0xc0) != 0x80) return (-1);
271 		if ((s[3] & 0xc0) != 0x80) return (-1);
272                 *pwc = ((ch & 0x07) << 18)
273 		    | ((s[1] & 0x3f) << 12)
274 		    | ((s[2] & 0x3f) << 6)
275 		    | (s[3] & 0x3f);
276 		return (4);
277         }
278 	/* Invalid first byte. */
279 	return (-1);
280 }
281 
282 /*
283  * Return a wide-character Unicode string by converting this archive_string
284  * from UTF-8.  We assume that systems with 16-bit wchar_t always use
285  * UTF16 and systems with 32-bit wchar_t can accept UCS4.
286  */
287 wchar_t *
288 __archive_string_utf8_w(struct archive_string *as)
289 {
290 	wchar_t *ws, *dest;
291 	int wc, wc2;/* Must be large enough for a 21-bit Unicode code point. */
292 	const char *src;
293 	int n;
294 	int err;
295 
296 	ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
297 	if (ws == NULL)
298 		__archive_errx(1, "Out of memory");
299 	err = 0;
300 	dest = ws;
301 	src = as->s;
302 	while (*src != '\0') {
303 		n = utf8_to_unicode(&wc, src, 8);
304 		if (n == 0)
305 			break;
306 		if (n < 0) {
307 			free(ws);
308 			return (NULL);
309 		}
310 		src += n;
311 		if (wc >= 0xDC00 && wc <= 0xDBFF) {
312 			/* This is a leading surrogate; some idiot
313 			 * has translated UTF16 to UTF8 without combining
314 			 * surrogates; rebuild the full code point before
315 			 * continuing. */
316 			n = utf8_to_unicode(&wc2, src, 8);
317 			if (n < 0) {
318 				free(ws);
319 				return (NULL);
320 			}
321 			if (n == 0) /* Ignore the leading surrogate */
322 				break;
323 			if (wc2 < 0xDC00 || wc2 > 0xDFFF) {
324 				/* If the second character isn't a
325 				 * trailing surrogate, then someone
326 				 * has really screwed up and this is
327 				 * invalid. */
328 				free(ws);
329 				return (NULL);
330 			} else {
331 				src += n;
332 				wc -= 0xD800;
333 				wc *= 0x400;
334 				wc += wc2 - 0xDC00;
335 				wc += 0x10000;
336 			}
337 		}
338 		if ((sizeof(wchar_t) < 4) && (wc > 0xffff)) {
339 			/* We have a code point that won't fit into a
340 			 * wchar_t; convert it to a surrogate pair. */
341 			wc -= 0x10000;
342 			*dest++ = ((wc >> 10) & 0x3ff) + 0xD800;
343 			*dest++ = (wc & 0x3ff) + 0xDC00;
344 		} else
345 			*dest++ = wc;
346 	}
347 	*dest++ = L'\0';
348 	return (ws);
349 }
350 
351 #if defined(_WIN32) && !defined(__CYGWIN__)
352 
353 /*
354  * Translates a wide character string into current locale character set
355  * and appends to the archive_string.  Note: returns NULL if conversion
356  * fails.
357  *
358  * Win32 builds use WideCharToMultiByte from the Windows API.
359  * (Maybe Cygwin should too?  WideCharToMultiByte will know a
360  * lot more about local character encodings than the wcrtomb()
361  * wrapper is going to know.)
362  */
363 struct archive_string *
364 __archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
365 {
366 	char *p;
367 	int l, wl;
368 	BOOL useDefaultChar = FALSE;
369 
370 	wl = (int)wcslen(w);
371 	l = wl * 4 + 4;
372 	p = malloc(l);
373 	if (p == NULL)
374 		__archive_errx(1, "Out of memory");
375 	/* To check a useDefaultChar is to simulate error handling of
376 	 * the my_wcstombs() which is running on non Windows system with
377 	 * wctomb().
378 	 * And to set NULL for last argument is necessary when a codepage
379 	 * is not CP_ACP(current locale).
380 	 */
381 	l = WideCharToMultiByte(CP_ACP, 0, w, wl, p, l, NULL, &useDefaultChar);
382 	if (l == 0) {
383 		free(p);
384 		return (NULL);
385 	}
386 	__archive_string_append(as, p, l);
387 	free(p);
388 	return (as);
389 }
390 
391 #else
392 
393 /*
394  * Translates a wide character string into current locale character set
395  * and appends to the archive_string.  Note: returns NULL if conversion
396  * fails.
397  *
398  * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
399  * one character at a time.  If a non-Windows platform doesn't have
400  * either of these, fall back to the built-in UTF8 conversion.
401  */
402 struct archive_string *
403 __archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
404 {
405 #if !defined(HAVE_WCTOMB) && !defined(HAVE_WCRTOMB)
406 	/* If there's no built-in locale support, fall back to UTF8 always. */
407 	return __archive_strappend_w_utf8(as, w);
408 #else
409 	/* We cannot use the standard wcstombs() here because it
410 	 * cannot tell us how big the output buffer should be.  So
411 	 * I've built a loop around wcrtomb() or wctomb() that
412 	 * converts a character at a time and resizes the string as
413 	 * needed.  We prefer wcrtomb() when it's available because
414 	 * it's thread-safe. */
415 	int n;
416 	char *p;
417 	char buff[256];
418 #if HAVE_WCRTOMB
419 	mbstate_t shift_state;
420 
421 	memset(&shift_state, 0, sizeof(shift_state));
422 #else
423 	/* Clear the shift state before starting. */
424 	wctomb(NULL, L'\0');
425 #endif
426 
427 	/*
428 	 * Convert one wide char at a time into 'buff', whenever that
429 	 * fills, append it to the string.
430 	 */
431 	p = buff;
432 	while (*w != L'\0') {
433 		/* Flush the buffer when we have <=16 bytes free. */
434 		/* (No encoding has a single character >16 bytes.) */
435 		if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - MB_CUR_MAX)) {
436 			*p = '\0';
437 			archive_strcat(as, buff);
438 			p = buff;
439 		}
440 #if HAVE_WCRTOMB
441 		n = wcrtomb(p, *w++, &shift_state);
442 #else
443 		n = wctomb(p, *w++);
444 #endif
445 		if (n == -1)
446 			return (NULL);
447 		p += n;
448 	}
449 	*p = '\0';
450 	archive_strcat(as, buff);
451 	return (as);
452 #endif
453 }
454 
455 #endif /* _WIN32 && ! __CYGWIN__ */
456