xref: /freebsd/contrib/libarchive/libarchive/archive_string.c (revision bd15d6ef126ee4c0eac931117f6bbbf6f9a3fc72)
1 /*-
2  * Copyright (c) 2003-2011 Tim Kientzle
3  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 
29 /*
30  * Basic resizable string support, to simplify manipulating arbitrary-sized
31  * strings while minimizing heap activity.
32  *
33  * In particular, the buffer used by a string object is only grown, it
34  * never shrinks, so you can clear and reuse the same string object
35  * without incurring additional memory allocations.
36  */
37 
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #ifdef HAVE_ICONV_H
42 #include <iconv.h>
43 #endif
44 #ifdef HAVE_LANGINFO_H
45 #include <langinfo.h>
46 #endif
47 #ifdef HAVE_LOCALCHARSET_H
48 #include <localcharset.h>
49 #endif
50 #ifdef HAVE_STDLIB_H
51 #include <stdlib.h>
52 #endif
53 #ifdef HAVE_STRING_H
54 #include <string.h>
55 #endif
56 #ifdef HAVE_WCHAR_H
57 #include <wchar.h>
58 #endif
59 #if defined(_WIN32) && !defined(__CYGWIN__)
60 #include <windows.h>
61 #include <locale.h>
62 #endif
63 
64 #include "archive_endian.h"
65 #include "archive_private.h"
66 #include "archive_string.h"
67 #include "archive_string_composition.h"
68 
69 #if !defined(HAVE_WMEMCPY) && !defined(wmemcpy)
70 #define wmemcpy(a,b,i)  (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
71 #endif
72 
73 #if !defined(HAVE_WMEMMOVE) && !defined(wmemmove)
74 #define wmemmove(a,b,i)  (wchar_t *)memmove((a), (b), (i) * sizeof(wchar_t))
75 #endif
76 
77 #undef max
78 #define max(a, b)       ((a)>(b)?(a):(b))
79 
80 struct archive_string_conv {
81 	struct archive_string_conv	*next;
82 	char				*from_charset;
83 	char				*to_charset;
84 	unsigned			 from_cp;
85 	unsigned			 to_cp;
86 	/* Set 1 if from_charset and to_charset are the same. */
87 	int				 same;
88 	int				 flag;
89 #define SCONV_TO_CHARSET	1	/* MBS is being converted to specified
90 					 * charset. */
91 #define SCONV_FROM_CHARSET	(1<<1)	/* MBS is being converted from
92 					 * specified charset. */
93 #define SCONV_BEST_EFFORT 	(1<<2)	/* Copy at least ASCII code. */
94 #define SCONV_WIN_CP	 	(1<<3)	/* Use Windows API for converting
95 					 * MBS. */
96 #define SCONV_UTF8_LIBARCHIVE_2 (1<<4)	/* Incorrect UTF-8 made by libarchive
97 					 * 2.x in the wrong assumption. */
98 #define SCONV_NORMALIZATION_C	(1<<6)	/* Need normalization to be Form C.
99 					 * Before UTF-8 characters are actually
100 					 * processed. */
101 #define SCONV_NORMALIZATION_D	(1<<7)	/* Need normalization to be Form D.
102 					 * Before UTF-8 characters are actually
103 					 * processed.
104 					 * Currently this only for MAC OS X. */
105 #define SCONV_TO_UTF8		(1<<8)	/* "to charset" side is UTF-8. */
106 #define SCONV_FROM_UTF8		(1<<9)	/* "from charset" side is UTF-8. */
107 #define SCONV_TO_UTF16BE 	(1<<10)	/* "to charset" side is UTF-16BE. */
108 #define SCONV_FROM_UTF16BE 	(1<<11)	/* "from charset" side is UTF-16BE. */
109 #define SCONV_TO_UTF16LE 	(1<<12)	/* "to charset" side is UTF-16LE. */
110 #define SCONV_FROM_UTF16LE 	(1<<13)	/* "from charset" side is UTF-16LE. */
111 #define SCONV_TO_UTF16		(SCONV_TO_UTF16BE | SCONV_TO_UTF16LE)
112 #define SCONV_FROM_UTF16	(SCONV_FROM_UTF16BE | SCONV_FROM_UTF16LE)
113 
114 #if HAVE_ICONV
115 	iconv_t				 cd;
116 	iconv_t				 cd_w;/* Use at archive_mstring on
117 				 	       * Windows. */
118 #endif
119 	/* A temporary buffer for normalization. */
120 	struct archive_string		 utftmp;
121 	int (*converter[2])(struct archive_string *, const void *, size_t,
122 	    struct archive_string_conv *);
123 	int				 nconverter;
124 };
125 
126 #define CP_C_LOCALE	0	/* "C" locale only for this file. */
127 #define CP_UTF16LE	1200
128 #define CP_UTF16BE	1201
129 
130 #define IS_HIGH_SURROGATE_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDBFF)
131 #define IS_LOW_SURROGATE_LA(uc)	 ((uc) >= 0xDC00 && (uc) <= 0xDFFF)
132 #define IS_SURROGATE_PAIR_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDFFF)
133 #define UNICODE_MAX		0x10FFFF
134 #define UNICODE_R_CHAR		0xFFFD	/* Replacement character. */
135 /* Set U+FFFD(Replacement character) in UTF-8. */
136 static const char utf8_replacement_char[] = {0xef, 0xbf, 0xbd};
137 
138 static struct archive_string_conv *find_sconv_object(struct archive *,
139 	const char *, const char *);
140 static void add_sconv_object(struct archive *, struct archive_string_conv *);
141 static struct archive_string_conv *create_sconv_object(const char *,
142 	const char *, unsigned, int);
143 static void free_sconv_object(struct archive_string_conv *);
144 static struct archive_string_conv *get_sconv_object(struct archive *,
145 	const char *, const char *, int);
146 static unsigned make_codepage_from_charset(const char *);
147 static unsigned get_current_codepage(void);
148 static unsigned get_current_oemcp(void);
149 static size_t mbsnbytes(const void *, size_t);
150 static size_t utf16nbytes(const void *, size_t);
151 #if defined(_WIN32) && !defined(__CYGWIN__)
152 static int archive_wstring_append_from_mbs_in_codepage(
153     struct archive_wstring *, const char *, size_t,
154     struct archive_string_conv *);
155 static int archive_string_append_from_wcs_in_codepage(struct archive_string *,
156     const wchar_t *, size_t, struct archive_string_conv *);
157 static int strncat_in_codepage(struct archive_string *, const void *,
158     size_t, struct archive_string_conv *);
159 static int win_strncat_from_utf16be(struct archive_string *, const void *,
160     size_t, struct archive_string_conv *);
161 static int win_strncat_from_utf16le(struct archive_string *, const void *,
162     size_t, struct archive_string_conv *);
163 static int win_strncat_to_utf16be(struct archive_string *, const void *,
164     size_t, struct archive_string_conv *);
165 static int win_strncat_to_utf16le(struct archive_string *, const void *,
166     size_t, struct archive_string_conv *);
167 #endif
168 static int best_effort_strncat_from_utf16be(struct archive_string *,
169     const void *, size_t, struct archive_string_conv *);
170 static int best_effort_strncat_from_utf16le(struct archive_string *,
171     const void *, size_t, struct archive_string_conv *);
172 static int best_effort_strncat_to_utf16be(struct archive_string *,
173     const void *, size_t, struct archive_string_conv *);
174 static int best_effort_strncat_to_utf16le(struct archive_string *,
175     const void *, size_t, struct archive_string_conv *);
176 #if defined(HAVE_ICONV)
177 static int iconv_strncat_in_locale(struct archive_string *, const void *,
178     size_t, struct archive_string_conv *);
179 #endif
180 static int best_effort_strncat_in_locale(struct archive_string *,
181     const void *, size_t, struct archive_string_conv *);
182 static int _utf8_to_unicode(uint32_t *, const char *, size_t);
183 static int utf8_to_unicode(uint32_t *, const char *, size_t);
184 static inline uint32_t combine_surrogate_pair(uint32_t, uint32_t);
185 static int cesu8_to_unicode(uint32_t *, const char *, size_t);
186 static size_t unicode_to_utf8(char *, size_t, uint32_t);
187 static int utf16_to_unicode(uint32_t *, const char *, size_t, int);
188 static size_t unicode_to_utf16be(char *, size_t, uint32_t);
189 static size_t unicode_to_utf16le(char *, size_t, uint32_t);
190 static int strncat_from_utf8_libarchive2(struct archive_string *,
191     const void *, size_t, struct archive_string_conv *);
192 static int strncat_from_utf8_to_utf8(struct archive_string *, const void *,
193     size_t, struct archive_string_conv *);
194 static int archive_string_normalize_C(struct archive_string *, const void *,
195     size_t, struct archive_string_conv *);
196 static int archive_string_normalize_D(struct archive_string *, const void *,
197     size_t, struct archive_string_conv *);
198 static int archive_string_append_unicode(struct archive_string *,
199     const void *, size_t, struct archive_string_conv *);
200 
201 #if defined __LITTLE_ENDIAN__
202   #define IS_BIG_ENDIAN 0
203 #elif defined __BIG_ENDIAN__
204   #define IS_BIG_ENDIAN 1
205 #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
206   #define IS_BIG_ENDIAN 0
207 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
208   #define IS_BIG_ENDIAN 1
209 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || defined(_M_ARM64))
210   #define IS_BIG_ENDIAN 0
211 #else
212 // Detect endianness at runtime.
213 static int
is_big_endian(void)214 is_big_endian(void)
215 {
216 	uint16_t d = 1;
217 
218 	return (archive_be16dec(&d) == 1);
219 }
220 
221 #define IS_BIG_ENDIAN is_big_endian()
222 #endif
223 
224 static struct archive_string *
archive_string_append(struct archive_string * as,const char * p,size_t s)225 archive_string_append(struct archive_string *as, const char *p, size_t s)
226 {
227 	if (archive_string_ensure(as, as->length + s + 1) == NULL)
228 		return (NULL);
229 	if (s)
230 		memmove(as->s + as->length, p, s);
231 	as->length += s;
232 	as->s[as->length] = 0;
233 	return (as);
234 }
235 
236 static struct archive_wstring *
archive_wstring_append(struct archive_wstring * as,const wchar_t * p,size_t s)237 archive_wstring_append(struct archive_wstring *as, const wchar_t *p, size_t s)
238 {
239 	if (archive_wstring_ensure(as, as->length + s + 1) == NULL)
240 		return (NULL);
241 	if (s)
242 		wmemmove(as->s + as->length, p, s);
243 	as->length += s;
244 	as->s[as->length] = 0;
245 	return (as);
246 }
247 
248 struct archive_string *
archive_array_append(struct archive_string * as,const char * p,size_t s)249 archive_array_append(struct archive_string *as, const char *p, size_t s)
250 {
251 	return archive_string_append(as, p, s);
252 }
253 
254 void
archive_string_concat(struct archive_string * dest,struct archive_string * src)255 archive_string_concat(struct archive_string *dest, struct archive_string *src)
256 {
257 	if (archive_string_append(dest, src->s, src->length) == NULL)
258 		__archive_errx(1, "Out of memory");
259 }
260 
261 void
archive_wstring_concat(struct archive_wstring * dest,struct archive_wstring * src)262 archive_wstring_concat(struct archive_wstring *dest,
263     struct archive_wstring *src)
264 {
265 	if (archive_wstring_append(dest, src->s, src->length) == NULL)
266 		__archive_errx(1, "Out of memory");
267 }
268 
269 void
archive_string_free(struct archive_string * as)270 archive_string_free(struct archive_string *as)
271 {
272 	as->length = 0;
273 	as->buffer_length = 0;
274 	free(as->s);
275 	as->s = NULL;
276 }
277 
278 void
archive_wstring_free(struct archive_wstring * as)279 archive_wstring_free(struct archive_wstring *as)
280 {
281 	as->length = 0;
282 	as->buffer_length = 0;
283 	free(as->s);
284 	as->s = NULL;
285 }
286 
287 struct archive_wstring *
archive_wstring_ensure(struct archive_wstring * as,size_t s)288 archive_wstring_ensure(struct archive_wstring *as, size_t s)
289 {
290 	return (struct archive_wstring *)
291 		archive_string_ensure((struct archive_string *)as,
292 					s * sizeof(wchar_t));
293 }
294 
295 /* Returns NULL on any allocation failure. */
296 struct archive_string *
archive_string_ensure(struct archive_string * as,size_t s)297 archive_string_ensure(struct archive_string *as, size_t s)
298 {
299 	char *p;
300 	size_t new_length;
301 
302 	/* If buffer is already big enough, don't reallocate. */
303 	if (as->s && (s <= as->buffer_length))
304 		return (as);
305 
306 	/*
307 	 * Growing the buffer at least exponentially ensures that
308 	 * append operations are always linear in the number of
309 	 * characters appended.  Using a smaller growth rate for
310 	 * larger buffers reduces memory waste somewhat at the cost of
311 	 * a larger constant factor.
312 	 */
313 	if (as->buffer_length < 32)
314 		/* Start with a minimum 32-character buffer. */
315 		new_length = 32;
316 	else if (as->buffer_length < 8192)
317 		/* Buffers under 8k are doubled for speed. */
318 		new_length = as->buffer_length + as->buffer_length;
319 	else {
320 		/* Buffers 8k and over grow by at least 25% each time. */
321 		new_length = as->buffer_length + as->buffer_length / 4;
322 		/* Be safe: If size wraps, fail. */
323 		if (new_length < as->buffer_length) {
324 			/* On failure, wipe the string and return NULL. */
325 			archive_string_free(as);
326 			errno = ENOMEM;/* Make sure errno has ENOMEM. */
327 			return (NULL);
328 		}
329 	}
330 	/*
331 	 * The computation above is a lower limit to how much we'll
332 	 * grow the buffer.  In any case, we have to grow it enough to
333 	 * hold the request.
334 	 */
335 	if (new_length < s)
336 		new_length = s;
337 	/* Now we can reallocate the buffer. */
338 	p = realloc(as->s, new_length);
339 	if (p == NULL) {
340 		/* On failure, wipe the string and return NULL. */
341 		archive_string_free(as);
342 		errno = ENOMEM;/* Make sure errno has ENOMEM. */
343 		return (NULL);
344 	}
345 
346 	as->s = p;
347 	as->buffer_length = new_length;
348 	return (as);
349 }
350 
351 /*
352  * TODO: See if there's a way to avoid scanning
353  * the source string twice.  Then test to see
354  * if it actually helps (remember that we're almost
355  * always called with pretty short arguments, so
356  * such an optimization might not help).
357  */
358 struct archive_string *
archive_strncat(struct archive_string * as,const void * _p,size_t n)359 archive_strncat(struct archive_string *as, const void *_p, size_t n)
360 {
361 	size_t s;
362 	const char *p, *pp;
363 
364 	p = (const char *)_p;
365 
366 	/* Like strlen(p), except won't examine positions beyond p[n]. */
367 	s = 0;
368 	pp = p;
369 	while (s < n && *pp) {
370 		pp++;
371 		s++;
372 	}
373 	if ((as = archive_string_append(as, p, s)) == NULL)
374 		__archive_errx(1, "Out of memory");
375 	return (as);
376 }
377 
378 struct archive_wstring *
archive_wstrncat(struct archive_wstring * as,const wchar_t * p,size_t n)379 archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
380 {
381 	size_t s;
382 	const wchar_t *pp;
383 
384 	/* Like strlen(p), except won't examine positions beyond p[n]. */
385 	s = 0;
386 	pp = p;
387 	while (s < n && *pp) {
388 		pp++;
389 		s++;
390 	}
391 	if ((as = archive_wstring_append(as, p, s)) == NULL)
392 		__archive_errx(1, "Out of memory");
393 	return (as);
394 }
395 
396 struct archive_string *
archive_strcat(struct archive_string * as,const void * p)397 archive_strcat(struct archive_string *as, const void *p)
398 {
399 	/* strcat is just strncat without an effective limit.
400 	 * Assert that we'll never get called with a source
401 	 * string over 16MB.
402 	 * TODO: Review all uses of strcat in the source
403 	 * and try to replace them with strncat().
404 	 */
405 	return archive_strncat(as, p, 0x1000000);
406 }
407 
408 struct archive_wstring *
archive_wstrcat(struct archive_wstring * as,const wchar_t * p)409 archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
410 {
411 	/* Ditto. */
412 	return archive_wstrncat(as, p, 0x1000000);
413 }
414 
415 struct archive_string *
archive_strappend_char(struct archive_string * as,char c)416 archive_strappend_char(struct archive_string *as, char c)
417 {
418 	if ((as = archive_string_append(as, &c, 1)) == NULL)
419 		__archive_errx(1, "Out of memory");
420 	return (as);
421 }
422 
423 struct archive_wstring *
archive_wstrappend_wchar(struct archive_wstring * as,wchar_t c)424 archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
425 {
426 	if ((as = archive_wstring_append(as, &c, 1)) == NULL)
427 		__archive_errx(1, "Out of memory");
428 	return (as);
429 }
430 
431 /*
432  * Get the "current character set" name to use with iconv.
433  * On FreeBSD, the empty character set name "" chooses
434  * the correct character encoding for the current locale,
435  * so this isn't necessary.
436  * But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
437  * on that system, we have to explicitly call nl_langinfo()
438  * to get the right name.  Not sure about other platforms.
439  *
440  * NOTE: GNU libiconv does not recognize the character-set name
441  * which some platform nl_langinfo(CODESET) returns, so we should
442  * use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
443  */
444 static const char *
default_iconv_charset(const char * charset)445 default_iconv_charset(const char *charset) {
446 	if (charset != NULL && charset[0] != '\0')
447 		return charset;
448 #if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
449 	/* locale_charset() is broken on Mac OS */
450 	return locale_charset();
451 #elif HAVE_NL_LANGINFO
452 	return nl_langinfo(CODESET);
453 #else
454 	return "";
455 #endif
456 }
457 
458 #if defined(_WIN32) && !defined(__CYGWIN__)
459 
460 /*
461  * Convert MBS to WCS.
462  * Note: returns -1 if conversion fails.
463  */
464 int
archive_wstring_append_from_mbs(struct archive_wstring * dest,const char * p,size_t len)465 archive_wstring_append_from_mbs(struct archive_wstring *dest,
466     const char *p, size_t len)
467 {
468 	return archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
469 }
470 
471 static int
archive_wstring_append_from_mbs_in_codepage(struct archive_wstring * dest,const char * s,size_t length,struct archive_string_conv * sc)472 archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
473     const char *s, size_t length, struct archive_string_conv *sc)
474 {
475 	int ret = 0;
476 	size_t count;
477 	UINT from_cp;
478 
479 	if (sc != NULL)
480 		from_cp = sc->from_cp;
481 	else
482 		from_cp = get_current_codepage();
483 
484 	if (from_cp == CP_C_LOCALE) {
485 		/*
486 		 * "C" locale special processing.
487 		 */
488 		wchar_t *ws;
489 		const unsigned char *mp;
490 
491 		if (NULL == archive_wstring_ensure(dest,
492 		    dest->length + length + 1))
493 			return (-1);
494 
495 		ws = dest->s + dest->length;
496 		mp = (const unsigned char *)s;
497 		count = 0;
498 		while (count < length && *mp) {
499 			*ws++ = (wchar_t)*mp++;
500 			count++;
501 		}
502 	} else if (sc != NULL &&
503 	    (sc->flag & (SCONV_NORMALIZATION_C | SCONV_NORMALIZATION_D))) {
504 		/*
505 		 * Normalize UTF-8 and UTF-16BE and convert it directly
506 		 * to UTF-16 as wchar_t.
507 		 */
508 		struct archive_string u16;
509 		int saved_flag = sc->flag;/* save current flag. */
510 
511 		if (IS_BIG_ENDIAN)
512 			sc->flag |= SCONV_TO_UTF16BE;
513 		else
514 			sc->flag |= SCONV_TO_UTF16LE;
515 
516 		if (sc->flag & SCONV_FROM_UTF16) {
517 			/*
518 			 *  UTF-16BE/LE NFD ===> UTF-16 NFC
519 			 *  UTF-16BE/LE NFC ===> UTF-16 NFD
520 			 */
521 			count = utf16nbytes(s, length);
522 		} else {
523 			/*
524 			 *  UTF-8 NFD ===> UTF-16 NFC
525 			 *  UTF-8 NFC ===> UTF-16 NFD
526 			 */
527 			count = mbsnbytes(s, length);
528 		}
529 		u16.s = (char *)dest->s;
530 		u16.length = dest->length << 1;
531 		u16.buffer_length = dest->buffer_length;
532 		if (sc->flag & SCONV_NORMALIZATION_C)
533 			ret = archive_string_normalize_C(&u16, s, count, sc);
534 		else
535 			ret = archive_string_normalize_D(&u16, s, count, sc);
536 		dest->s = (wchar_t *)u16.s;
537 		dest->length = u16.length >> 1;
538 		dest->buffer_length = u16.buffer_length;
539 		sc->flag = saved_flag;/* restore the saved flag. */
540 		return (ret);
541 	} else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
542 		count = utf16nbytes(s, length);
543 		count >>= 1; /* to be WCS length */
544 		/* Allocate memory for WCS. */
545 		if (NULL == archive_wstring_ensure(dest,
546 		    dest->length + count + 1))
547 			return (-1);
548 		wmemcpy(dest->s + dest->length, (const wchar_t *)s, count);
549 		if ((sc->flag & SCONV_FROM_UTF16BE) && !IS_BIG_ENDIAN) {
550 			uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
551 			size_t b;
552 			for (b = 0; b < count; b++) {
553 				uint16_t val = archive_le16dec(u16+b);
554 				archive_be16enc(u16+b, val);
555 			}
556 		} else if ((sc->flag & SCONV_FROM_UTF16LE) && IS_BIG_ENDIAN) {
557 			uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
558 			size_t b;
559 			for (b = 0; b < count; b++) {
560 				uint16_t val = archive_be16dec(u16+b);
561 				archive_le16enc(u16+b, val);
562 			}
563 		}
564 	} else {
565 		DWORD mbflag;
566 		size_t buffsize;
567 
568 		if (sc == NULL)
569 			mbflag = 0;
570 		else if (sc->flag & SCONV_FROM_CHARSET) {
571 			/* Do not trust the length which comes from
572 			 * an archive file. */
573 			length = mbsnbytes(s, length);
574 			mbflag = 0;
575 		} else
576 			mbflag = MB_PRECOMPOSED;
577 
578 		mbflag |= MB_ERR_INVALID_CHARS;
579 
580 		buffsize = dest->length + length + 1;
581 		do {
582 			int r;
583 
584 			/* MultiByteToWideChar is limited to int. */
585 			if (length > (size_t)INT_MAX ||
586 				(dest->buffer_length >> 1) > (size_t)INT_MAX)
587 				return (-1);
588 			/* Allocate memory for WCS. */
589 			if (NULL == archive_wstring_ensure(dest, buffsize))
590 				return (-1);
591 			/* Convert MBS to WCS. */
592 			r = MultiByteToWideChar(from_cp,
593 			    mbflag, s, (int)length, dest->s + dest->length,
594 			    (int)(dest->buffer_length >> 1) -1);
595 			if (r == 0 &&
596 			    GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
597 				/* Expand the WCS buffer. */
598 				buffsize = dest->buffer_length << 1;
599 				continue;
600 			}
601 			if (r == 0 && length != 0)
602 				ret = -1;
603 			count = (size_t)r;
604 			break;
605 		} while (1);
606 	}
607 	dest->length += count;
608 	dest->s[dest->length] = L'\0';
609 	return (ret);
610 }
611 
612 #else
613 
614 /*
615  * Convert MBS to WCS.
616  * Note: returns -1 if conversion fails.
617  */
618 int
archive_wstring_append_from_mbs(struct archive_wstring * dest,const char * p,size_t len)619 archive_wstring_append_from_mbs(struct archive_wstring *dest,
620     const char *p, size_t len)
621 {
622 	size_t r;
623 	int ret_val = 0;
624 	/*
625 	 * No single byte will be more than one wide character,
626 	 * so this length estimate will always be big enough.
627 	 */
628 	// size_t wcs_length = len;
629 	size_t mbs_length = len;
630 	const char *mbs = p;
631 	wchar_t *wcs;
632 #if HAVE_MBRTOWC
633 	mbstate_t shift_state;
634 
635 	memset(&shift_state, 0, sizeof(shift_state));
636 #endif
637 	/*
638 	 * As we decided to have wcs_length == mbs_length == len
639 	 * we can use len here instead of wcs_length
640 	 */
641 	if (NULL == archive_wstring_ensure(dest, dest->length + len + 1))
642 		return (-1);
643 	wcs = dest->s + dest->length;
644 	/*
645 	 * We cannot use mbsrtowcs/mbstowcs here because those may convert
646 	 * extra MBS when strlen(p) > len and one wide character consists of
647 	 * multi bytes.
648 	 */
649 	while (*mbs && mbs_length > 0) {
650 		/*
651 		 * The buffer we allocated is always big enough.
652 		 * Keep this code path in a comment if we decide to choose
653 		 * smaller wcs_length in the future
654 		 */
655 /*
656 		if (wcs_length == 0) {
657 			dest->length = wcs - dest->s;
658 			dest->s[dest->length] = L'\0';
659 			wcs_length = mbs_length;
660 			if (NULL == archive_wstring_ensure(dest,
661 			    dest->length + wcs_length + 1))
662 				return (-1);
663 			wcs = dest->s + dest->length;
664 		}
665 */
666 #if HAVE_MBRTOWC
667 		r = mbrtowc(wcs, mbs, mbs_length, &shift_state);
668 #else
669 		r = mbtowc(wcs, mbs, mbs_length);
670 #endif
671 		if (r == (size_t)-1 || r == (size_t)-2) {
672 			ret_val = -1;
673 			break;
674 		}
675 		if (r == 0 || r > mbs_length)
676 			break;
677 		wcs++;
678 		// wcs_length--;
679 		mbs += r;
680 		mbs_length -= r;
681 	}
682 	dest->length = wcs - dest->s;
683 	dest->s[dest->length] = L'\0';
684 	return (ret_val);
685 }
686 
687 #endif
688 
689 #if defined(_WIN32) && !defined(__CYGWIN__)
690 
691 /*
692  * WCS ==> MBS.
693  * Note: returns -1 if conversion fails.
694  *
695  * Win32 builds use WideCharToMultiByte from the Windows API.
696  * (Maybe Cygwin should too?  WideCharToMultiByte will know a
697  * lot more about local character encodings than the wcrtomb()
698  * wrapper is going to know.)
699  */
700 int
archive_string_append_from_wcs(struct archive_string * as,const wchar_t * w,size_t len)701 archive_string_append_from_wcs(struct archive_string *as,
702     const wchar_t *w, size_t len)
703 {
704 	return archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
705 }
706 
707 static int
archive_string_append_from_wcs_in_codepage(struct archive_string * as,const wchar_t * ws,size_t len,struct archive_string_conv * sc)708 archive_string_append_from_wcs_in_codepage(struct archive_string *as,
709     const wchar_t *ws, size_t len, struct archive_string_conv *sc)
710 {
711 	BOOL defchar_used, *dp;
712 	int ret = 0;
713 	UINT to_cp;
714 	size_t count, wslen = len;
715 
716 	if (sc != NULL)
717 		to_cp = sc->to_cp;
718 	else
719 		to_cp = get_current_codepage();
720 
721 	if (to_cp == CP_C_LOCALE) {
722 		/*
723 		 * "C" locale special processing.
724 		 */
725 		const wchar_t *wp = ws;
726 		char *p;
727 
728 		if (NULL == archive_string_ensure(as,
729 		    as->length + wslen +1))
730 			return (-1);
731 		p = as->s + as->length;
732 		count = 0;
733 		defchar_used = 0;
734 		while (count < wslen && *wp) {
735 			if (*wp > 255) {
736 				*p++ = '?';
737 				wp++;
738 				defchar_used = 1;
739 			} else
740 				*p++ = (char)*wp++;
741 			count++;
742 		}
743 	} else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
744 		uint16_t *u16;
745 
746 		if (NULL ==
747 		    archive_string_ensure(as, as->length + len * 2 + 2))
748 			return (-1);
749 		u16 = (uint16_t *)(as->s + as->length);
750 		count = 0;
751 		defchar_used = 0;
752 		if (sc->flag & SCONV_TO_UTF16BE) {
753 			while (count < len && *ws) {
754 				archive_be16enc(u16+count, *ws);
755 				ws++;
756 				count++;
757 			}
758 		} else {
759 			while (count < len && *ws) {
760 				archive_le16enc(u16+count, *ws);
761 				ws++;
762 				count++;
763 			}
764 		}
765 		count <<= 1; /* to be byte size */
766 	} else {
767 		/* Make sure the MBS buffer has plenty to set. */
768 		if (NULL ==
769 		    archive_string_ensure(as, as->length + len * 2 + 1))
770 			return (-1);
771 		do {
772 			int r;
773 
774 			defchar_used = 0;
775 			if (to_cp == CP_UTF8)
776 				dp = NULL;
777 			else
778 				dp = &defchar_used;
779 			/* WideCharToMultiByte is limited to int. */
780 			if (as->buffer_length - as->length - 1 > (size_t)INT_MAX ||
781 				wslen > (size_t)INT_MAX)
782 				return (-1);
783 			r = WideCharToMultiByte(to_cp, 0, ws, (int)wslen,
784 			    as->s + as->length,
785 			    (int)(as->buffer_length - as->length - 1), NULL, dp);
786 			if (r == 0 &&
787 			    GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
788 				/* Expand the MBS buffer and retry. */
789 				if (NULL == archive_string_ensure(as,
790 					as->buffer_length + len))
791 					return (-1);
792 				continue;
793 			}
794 			if (r == 0)
795 				ret = -1;
796 			count = (size_t)r;
797 			break;
798 		} while (1);
799 	}
800 	as->length += count;
801 	as->s[as->length] = '\0';
802 	return (defchar_used?-1:ret);
803 }
804 
805 #elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
806 
807 /*
808  * Translates a wide character string into current locale character set
809  * and appends to the archive_string.  Note: returns -1 if conversion
810  * fails.
811  */
812 int
archive_string_append_from_wcs(struct archive_string * as,const wchar_t * w,size_t len)813 archive_string_append_from_wcs(struct archive_string *as,
814     const wchar_t *w, size_t len)
815 {
816 	/* We cannot use the standard wcstombs() here because it
817 	 * cannot tell us how big the output buffer should be.  So
818 	 * I've built a loop around wcrtomb() or wctomb() that
819 	 * converts a character at a time and resizes the string as
820 	 * needed.  We prefer wcrtomb() when it's available because
821 	 * it's thread-safe. */
822 	int n, ret_val = 0;
823 	char *p;
824 	char *end;
825 #if HAVE_WCRTOMB
826 	mbstate_t shift_state;
827 
828 	memset(&shift_state, 0, sizeof(shift_state));
829 #else
830 	/* Clear the shift state before starting. */
831 	wctomb(NULL, L'\0');
832 #endif
833 	/*
834 	 * Allocate buffer for MBS.
835 	 * We need this allocation here since it is possible that
836 	 * as->s is still NULL.
837 	 */
838 	if (archive_string_ensure(as, as->length + len + 1) == NULL)
839 		return (-1);
840 
841 	p = as->s + as->length;
842 	end = as->s + as->buffer_length - MB_CUR_MAX -1;
843 	while (*w != L'\0' && len > 0) {
844 		if (p >= end) {
845 			as->length = p - as->s;
846 			as->s[as->length] = '\0';
847 			/* Re-allocate buffer for MBS. */
848 			if (archive_string_ensure(as,
849 			    as->length + max(len * 2,
850 			    (size_t)MB_CUR_MAX) + 1) == NULL)
851 				return (-1);
852 			p = as->s + as->length;
853 			end = as->s + as->buffer_length - MB_CUR_MAX -1;
854 		}
855 #if HAVE_WCRTOMB
856 		n = wcrtomb(p, *w++, &shift_state);
857 #else
858 		n = wctomb(p, *w++);
859 #endif
860 		if (n == -1) {
861 			if (errno == EILSEQ) {
862 				/* Skip an illegal wide char. */
863 				*p++ = '?';
864 				ret_val = -1;
865 			} else {
866 				ret_val = -1;
867 				break;
868 			}
869 		} else
870 			p += n;
871 		len--;
872 	}
873 	as->length = p - as->s;
874 	as->s[as->length] = '\0';
875 	return (ret_val);
876 }
877 
878 #else /* HAVE_WCTOMB || HAVE_WCRTOMB */
879 
880 /*
881  * TODO: Test if __STDC_ISO_10646__ is defined.
882  * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
883  * one character at a time.  If a non-Windows platform doesn't have
884  * either of these, fall back to the built-in UTF8 conversion.
885  */
886 int
archive_string_append_from_wcs(struct archive_string * as,const wchar_t * w,size_t len)887 archive_string_append_from_wcs(struct archive_string *as,
888     const wchar_t *w, size_t len)
889 {
890 	(void)as;/* UNUSED */
891 	(void)w;/* UNUSED */
892 	(void)len;/* UNUSED */
893 	errno = ENOSYS;
894 	return (-1);
895 }
896 
897 #endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
898 
899 /*
900  * Find a string conversion object by a pair of 'from' charset name
901  * and 'to' charset name from an archive object.
902  * Return NULL if not found.
903  */
904 static struct archive_string_conv *
find_sconv_object(struct archive * a,const char * fc,const char * tc)905 find_sconv_object(struct archive *a, const char *fc, const char *tc)
906 {
907 	struct archive_string_conv *sc;
908 
909 	if (a == NULL)
910 		return (NULL);
911 
912 	for (sc = a->sconv; sc != NULL; sc = sc->next) {
913 		if (strcmp(sc->from_charset, fc) == 0 &&
914 		    strcmp(sc->to_charset, tc) == 0)
915 			break;
916 	}
917 	return (sc);
918 }
919 
920 /*
921  * Register a string object to an archive object.
922  */
923 static void
add_sconv_object(struct archive * a,struct archive_string_conv * sc)924 add_sconv_object(struct archive *a, struct archive_string_conv *sc)
925 {
926 	struct archive_string_conv **psc;
927 
928 	/* Add a new sconv to sconv list. */
929 	psc = &(a->sconv);
930 	while (*psc != NULL)
931 		psc = &((*psc)->next);
932 	*psc = sc;
933 }
934 
935 static void
add_converter(struct archive_string_conv * sc,int (* converter)(struct archive_string *,const void *,size_t,struct archive_string_conv *))936 add_converter(struct archive_string_conv *sc, int (*converter)
937     (struct archive_string *, const void *, size_t,
938      struct archive_string_conv *))
939 {
940 	if (sc == NULL || sc->nconverter >= 2)
941 		__archive_errx(1, "Programming error");
942 	sc->converter[sc->nconverter++] = converter;
943 }
944 
945 static void
setup_converter(struct archive_string_conv * sc)946 setup_converter(struct archive_string_conv *sc)
947 {
948 
949 	/* Reset. */
950 	sc->nconverter = 0;
951 
952 	/*
953 	 * Perform special sequence for the incorrect UTF-8 filenames
954 	 * made by libarchive2.x.
955 	 */
956 	if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
957 		add_converter(sc, strncat_from_utf8_libarchive2);
958 		return;
959 	}
960 
961 	/*
962 	 * Convert a string to UTF-16BE/LE.
963 	 */
964 	if (sc->flag & SCONV_TO_UTF16) {
965 		/*
966 		 * If the current locale is UTF-8, we can translate
967 		 * a UTF-8 string into a UTF-16BE string.
968 		 */
969 		if (sc->flag & SCONV_FROM_UTF8) {
970 			add_converter(sc, archive_string_append_unicode);
971 			return;
972 		}
973 
974 #if defined(_WIN32) && !defined(__CYGWIN__)
975 		if (sc->flag & SCONV_WIN_CP) {
976 			if (sc->flag & SCONV_TO_UTF16BE)
977 				add_converter(sc, win_strncat_to_utf16be);
978 			else
979 				add_converter(sc, win_strncat_to_utf16le);
980 			return;
981 		}
982 #endif
983 
984 #if defined(HAVE_ICONV)
985 		if (sc->cd != (iconv_t)-1) {
986 			add_converter(sc, iconv_strncat_in_locale);
987 			return;
988 		}
989 #endif
990 
991 		if (sc->flag & SCONV_BEST_EFFORT) {
992 			if (sc->flag & SCONV_TO_UTF16BE)
993 				add_converter(sc,
994 					best_effort_strncat_to_utf16be);
995 			else
996 				add_converter(sc,
997 					best_effort_strncat_to_utf16le);
998 		} else
999 			/* Make sure we have no converter. */
1000 			sc->nconverter = 0;
1001 		return;
1002 	}
1003 
1004 	/*
1005 	 * Convert a string from UTF-16BE/LE.
1006 	 */
1007 	if (sc->flag & SCONV_FROM_UTF16) {
1008 		/*
1009 		 * At least we should normalize a UTF-16BE string.
1010 		 */
1011 		if (sc->flag & SCONV_NORMALIZATION_D)
1012 			add_converter(sc,archive_string_normalize_D);
1013 		else if (sc->flag & SCONV_NORMALIZATION_C)
1014 			add_converter(sc, archive_string_normalize_C);
1015 
1016 		if (sc->flag & SCONV_TO_UTF8) {
1017 			/*
1018 			 * If the current locale is UTF-8, we can translate
1019 			 * a UTF-16BE/LE string into a UTF-8 string directly.
1020 			 */
1021 			if (!(sc->flag &
1022 			    (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
1023 				add_converter(sc,
1024 				    archive_string_append_unicode);
1025 			return;
1026 		}
1027 
1028 #if defined(_WIN32) && !defined(__CYGWIN__)
1029 		if (sc->flag & SCONV_WIN_CP) {
1030 			if (sc->flag & SCONV_FROM_UTF16BE)
1031 				add_converter(sc, win_strncat_from_utf16be);
1032 			else
1033 				add_converter(sc, win_strncat_from_utf16le);
1034 			return;
1035 		}
1036 #endif
1037 
1038 #if defined(HAVE_ICONV)
1039 		if (sc->cd != (iconv_t)-1) {
1040 			add_converter(sc, iconv_strncat_in_locale);
1041 			return;
1042 		}
1043 #endif
1044 
1045 		if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
1046 		    == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
1047 			add_converter(sc, best_effort_strncat_from_utf16be);
1048 		else if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
1049 		    == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
1050 			add_converter(sc, best_effort_strncat_from_utf16le);
1051 		else
1052 			/* Make sure we have no converter. */
1053 			sc->nconverter = 0;
1054 		return;
1055 	}
1056 
1057 	if (sc->flag & SCONV_FROM_UTF8) {
1058 		/*
1059 		 * At least we should normalize a UTF-8 string.
1060 		 */
1061 		if (sc->flag & SCONV_NORMALIZATION_D)
1062 			add_converter(sc,archive_string_normalize_D);
1063 		else if (sc->flag & SCONV_NORMALIZATION_C)
1064 			add_converter(sc, archive_string_normalize_C);
1065 
1066 		/*
1067 		 * Copy UTF-8 string with a check of CESU-8.
1068 		 * Apparently, iconv does not check surrogate pairs in UTF-8
1069 		 * when both from-charset and to-charset are UTF-8, and then
1070 		 * we use our UTF-8 copy code.
1071 		 */
1072 		if (sc->flag & SCONV_TO_UTF8) {
1073 			/*
1074 			 * If the current locale is UTF-8, we can translate
1075 			 * a UTF-16BE string into a UTF-8 string directly.
1076 			 */
1077 			if (!(sc->flag &
1078 			    (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
1079 				add_converter(sc, strncat_from_utf8_to_utf8);
1080 			return;
1081 		}
1082 	}
1083 
1084 #if defined(_WIN32) && !defined(__CYGWIN__)
1085 	/*
1086 	 * On Windows we can use Windows API for a string conversion.
1087 	 */
1088 	if (sc->flag & SCONV_WIN_CP) {
1089 		add_converter(sc, strncat_in_codepage);
1090 		return;
1091 	}
1092 #endif
1093 
1094 #if HAVE_ICONV
1095 	if (sc->cd != (iconv_t)-1) {
1096 		add_converter(sc, iconv_strncat_in_locale);
1097 		/*
1098 		 * iconv generally does not support UTF-8-MAC and so
1099 		 * we have to the output of iconv from NFC to NFD if
1100 		 * need.
1101 		 */
1102 		if ((sc->flag & SCONV_FROM_CHARSET) &&
1103 		    (sc->flag & SCONV_TO_UTF8)) {
1104 			if (sc->flag & SCONV_NORMALIZATION_D)
1105 				add_converter(sc, archive_string_normalize_D);
1106 		}
1107 		return;
1108 	}
1109 #endif
1110 
1111 	/*
1112 	 * Try conversion in the best effort or no conversion.
1113 	 */
1114 	if ((sc->flag & SCONV_BEST_EFFORT) || sc->same)
1115 		add_converter(sc, best_effort_strncat_in_locale);
1116 	else
1117 		/* Make sure we have no converter. */
1118 		sc->nconverter = 0;
1119 }
1120 
1121 /*
1122  * Return canonicalized charset-name but this supports just UTF-8, UTF-16BE
1123  * and CP932 which are referenced in create_sconv_object().
1124  */
1125 static const char *
canonical_charset_name(const char * charset)1126 canonical_charset_name(const char *charset)
1127 {
1128 	char cs[16];
1129 	char *p;
1130 	const char *s;
1131 
1132 	if (charset == NULL || charset[0] == '\0'
1133 	    || strlen(charset) > 15)
1134 		return (charset);
1135 
1136 	/* Copy name to uppercase. */
1137 	p = cs;
1138 	s = charset;
1139 	while (*s) {
1140 		char c = *s++;
1141 		if (c >= 'a' && c <= 'z')
1142 			c -= 'a' - 'A';
1143 		*p++ = c;
1144 	}
1145 	*p++ = '\0';
1146 
1147 	if (strcmp(cs, "UTF-8") == 0 ||
1148 	    strcmp(cs, "UTF8") == 0)
1149 		return ("UTF-8");
1150 	if (strcmp(cs, "UTF-16BE") == 0 ||
1151 	    strcmp(cs, "UTF16BE") == 0)
1152 		return ("UTF-16BE");
1153 	if (strcmp(cs, "UTF-16LE") == 0 ||
1154 	    strcmp(cs, "UTF16LE") == 0)
1155 		return ("UTF-16LE");
1156 	if (strcmp(cs, "CP932") == 0)
1157 		return ("CP932");
1158 	return (charset);
1159 }
1160 
1161 /*
1162  * Create a string conversion object.
1163  */
1164 static struct archive_string_conv *
create_sconv_object(const char * fc,const char * tc,unsigned current_codepage,int flag)1165 create_sconv_object(const char *fc, const char *tc,
1166     unsigned current_codepage, int flag)
1167 {
1168 	struct archive_string_conv *sc;
1169 
1170 	sc = calloc(1, sizeof(*sc));
1171 	if (sc == NULL)
1172 		return (NULL);
1173 	sc->next = NULL;
1174 	sc->from_charset = strdup(fc);
1175 	if (sc->from_charset == NULL) {
1176 		free(sc);
1177 		return (NULL);
1178 	}
1179 	sc->to_charset = strdup(tc);
1180 	if (sc->to_charset == NULL) {
1181 		free(sc->from_charset);
1182 		free(sc);
1183 		return (NULL);
1184 	}
1185 	archive_string_init(&sc->utftmp);
1186 
1187 	if (flag & SCONV_TO_CHARSET) {
1188 		/*
1189 		 * Convert characters from the current locale charset to
1190 		 * a specified charset.
1191 		 */
1192 		sc->from_cp = current_codepage;
1193 		sc->to_cp = make_codepage_from_charset(tc);
1194 #if defined(_WIN32) && !defined(__CYGWIN__)
1195 		if (IsValidCodePage(sc->to_cp))
1196 			flag |= SCONV_WIN_CP;
1197 #endif
1198 	} else if (flag & SCONV_FROM_CHARSET) {
1199 		/*
1200 		 * Convert characters from a specified charset to
1201 		 * the current locale charset.
1202 		 */
1203 		sc->to_cp = current_codepage;
1204 		sc->from_cp = make_codepage_from_charset(fc);
1205 #if defined(_WIN32) && !defined(__CYGWIN__)
1206 		if (IsValidCodePage(sc->from_cp))
1207 			flag |= SCONV_WIN_CP;
1208 #endif
1209 	}
1210 
1211 	/*
1212 	 * Check if "from charset" and "to charset" are the same.
1213 	 */
1214 	if (strcmp(fc, tc) == 0 ||
1215 	    (sc->from_cp != (unsigned)-1 && sc->from_cp == sc->to_cp))
1216 		sc->same = 1;
1217 	else
1218 		sc->same = 0;
1219 
1220 	/*
1221 	 * Mark if "from charset" or "to charset" are UTF-8 or UTF-16BE/LE.
1222 	 */
1223 	if (strcmp(tc, "UTF-8") == 0)
1224 		flag |= SCONV_TO_UTF8;
1225 	else if (strcmp(tc, "UTF-16BE") == 0)
1226 		flag |= SCONV_TO_UTF16BE;
1227 	else if (strcmp(tc, "UTF-16LE") == 0)
1228 		flag |= SCONV_TO_UTF16LE;
1229 	if (strcmp(fc, "UTF-8") == 0)
1230 		flag |= SCONV_FROM_UTF8;
1231 	else if (strcmp(fc, "UTF-16BE") == 0)
1232 		flag |= SCONV_FROM_UTF16BE;
1233 	else if (strcmp(fc, "UTF-16LE") == 0)
1234 		flag |= SCONV_FROM_UTF16LE;
1235 #if defined(_WIN32) && !defined(__CYGWIN__)
1236 	if (sc->to_cp == CP_UTF8)
1237 		flag |= SCONV_TO_UTF8;
1238 	else if (sc->to_cp == CP_UTF16BE)
1239 		flag |= SCONV_TO_UTF16BE | SCONV_WIN_CP;
1240 	else if (sc->to_cp == CP_UTF16LE)
1241 		flag |= SCONV_TO_UTF16LE | SCONV_WIN_CP;
1242 	if (sc->from_cp == CP_UTF8)
1243 		flag |= SCONV_FROM_UTF8;
1244 	else if (sc->from_cp == CP_UTF16BE)
1245 		flag |= SCONV_FROM_UTF16BE | SCONV_WIN_CP;
1246 	else if (sc->from_cp == CP_UTF16LE)
1247 		flag |= SCONV_FROM_UTF16LE | SCONV_WIN_CP;
1248 #endif
1249 
1250 	/*
1251 	 * Set a flag for Unicode NFD. Usually iconv cannot correctly
1252 	 * handle it. So we have to translate NFD characters to NFC ones
1253 	 * ourselves before iconv handles. Another reason is to prevent
1254 	 * that the same sight of two filenames, one is NFC and other
1255 	 * is NFD, would be in its directory.
1256 	 * On Mac OS X, although its filesystem layer automatically
1257 	 * convert filenames to NFD, it would be useful for filename
1258 	 * comparing to find out the same filenames that we normalize
1259 	 * that to be NFD ourselves.
1260 	 */
1261 	if ((flag & SCONV_FROM_CHARSET) &&
1262 	    (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8))) {
1263 #if defined(__APPLE__)
1264 		if (flag & SCONV_TO_UTF8)
1265 			flag |= SCONV_NORMALIZATION_D;
1266 		else
1267 #endif
1268 			flag |= SCONV_NORMALIZATION_C;
1269 	}
1270 #if defined(__APPLE__)
1271 	/*
1272 	 * In case writing an archive file, make sure that a filename
1273 	 * going to be passed to iconv is a Unicode NFC string since
1274 	 * a filename in HFS Plus filesystem is a Unicode NFD one and
1275 	 * iconv cannot handle it with "UTF-8" charset. It is simpler
1276 	 * than a use of "UTF-8-MAC" charset.
1277 	 */
1278 	if ((flag & SCONV_TO_CHARSET) &&
1279 	    (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1280 	    !(flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1281 		flag |= SCONV_NORMALIZATION_C;
1282 	/*
1283 	 * In case reading an archive file. make sure that a filename
1284 	 * will be passed to users is a Unicode NFD string in order to
1285 	 * correctly compare the filename with other one which comes
1286 	 * from HFS Plus filesystem.
1287 	 */
1288 	if ((flag & SCONV_FROM_CHARSET) &&
1289 	   !(flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1290 	    (flag & SCONV_TO_UTF8))
1291 		flag |= SCONV_NORMALIZATION_D;
1292 #endif
1293 
1294 #if defined(HAVE_ICONV)
1295 	sc->cd_w = (iconv_t)-1;
1296 	/*
1297 	 * Create an iconv object.
1298 	 */
1299 	if (((flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) &&
1300 	    (flag & (SCONV_FROM_UTF8 | SCONV_FROM_UTF16))) ||
1301 	    (flag & SCONV_WIN_CP)) {
1302 		/* This case we won't use iconv. */
1303 		sc->cd = (iconv_t)-1;
1304 	} else {
1305 		sc->cd = iconv_open(tc, fc);
1306 		if (sc->cd == (iconv_t)-1 && (sc->flag & SCONV_BEST_EFFORT)) {
1307 			/*
1308 			 * Unfortunately, all of iconv implements do support
1309 			 * "CP932" character-set, so we should use "SJIS"
1310 			 * instead if iconv_open failed.
1311 			 */
1312 			if (strcmp(tc, "CP932") == 0)
1313 				sc->cd = iconv_open("SJIS", fc);
1314 			else if (strcmp(fc, "CP932") == 0)
1315 				sc->cd = iconv_open(tc, "SJIS");
1316 		}
1317 #if defined(__FreeBSD__) && !defined(HAVE_LIBICONV)
1318 		/*
1319 		 * FreeBSD's native iconv() by default returns the number of
1320 		 * invalid characters in the input string, as specified by
1321 		 * POSIX, but iconv_strncat_in_locale() assumes GNU iconv
1322 		 * semantics.
1323 		 */
1324 		int v = 1;
1325 
1326 		(void)iconvctl(sc->cd, ICONV_SET_ILSEQ_INVALID, &v);
1327 #elif defined(_WIN32) && !defined(__CYGWIN__)
1328 		/*
1329 		 * archive_mstring on Windows directly convert multi-bytes
1330 		 * into archive_wstring in order not to depend on locale
1331 		 * so that you can do a I18N programming. This will be
1332 		 * used only in archive_mstring_copy_mbs_len_l so far.
1333 		 */
1334 		if (flag & SCONV_FROM_CHARSET) {
1335 			sc->cd_w = iconv_open("UTF-8", fc);
1336 			if (sc->cd_w == (iconv_t)-1 &&
1337 			    (sc->flag & SCONV_BEST_EFFORT)) {
1338 				if (strcmp(fc, "CP932") == 0)
1339 					sc->cd_w = iconv_open("UTF-8", "SJIS");
1340 			}
1341 		}
1342 #endif /* _WIN32 && !__CYGWIN__ */
1343 	}
1344 #endif	/* HAVE_ICONV */
1345 
1346 	sc->flag = flag;
1347 
1348 	/*
1349 	 * Set up converters.
1350 	 */
1351 	setup_converter(sc);
1352 
1353 	return (sc);
1354 }
1355 
1356 /*
1357  * Free a string conversion object.
1358  */
1359 static void
free_sconv_object(struct archive_string_conv * sc)1360 free_sconv_object(struct archive_string_conv *sc)
1361 {
1362 	free(sc->from_charset);
1363 	free(sc->to_charset);
1364 	archive_string_free(&sc->utftmp);
1365 #if HAVE_ICONV
1366 	if (sc->cd != (iconv_t)-1)
1367 		iconv_close(sc->cd);
1368 	if (sc->cd_w != (iconv_t)-1)
1369 		iconv_close(sc->cd_w);
1370 #endif
1371 	free(sc);
1372 }
1373 
1374 #if defined(_WIN32) && !defined(__CYGWIN__)
1375 # if defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
1376 #  define GetOEMCP() CP_OEMCP
1377 # endif
1378 
1379 static unsigned
my_atoi(const char * p)1380 my_atoi(const char *p)
1381 {
1382 	unsigned cp;
1383 
1384 	cp = 0;
1385 	while (*p) {
1386 		if (*p >= '0' && *p <= '9')
1387 			cp = cp * 10 + (*p - '0');
1388 		else
1389 			return (-1);
1390 		p++;
1391 	}
1392 	return (cp);
1393 }
1394 
1395 /*
1396  * Translate Charset name (as used by iconv) into CodePage (as used by Windows)
1397  * Return -1 if failed.
1398  *
1399  * Note: This translation code may be insufficient.
1400  */
1401 static struct charset {
1402 	const char *name;
1403 	unsigned cp;
1404 } charsets[] = {
1405 	/* MUST BE SORTED! */
1406 	{"ASCII", 1252},
1407 	{"ASMO-708", 708},
1408 	{"BIG5", 950},
1409 	{"CHINESE", 936},
1410 	{"CP367", 1252},
1411 	{"CP819", 1252},
1412 	{"CP1025", 21025},
1413 	{"DOS-720", 720},
1414 	{"DOS-862", 862},
1415 	{"EUC-CN", 51936},
1416 	{"EUC-JP", 51932},
1417 	{"EUC-KR", 949},
1418 	{"EUCCN", 51936},
1419 	{"EUCJP", 51932},
1420 	{"EUCKR", 949},
1421 	{"GB18030", 54936},
1422 	{"GB2312", 936},
1423 	{"HEBREW", 1255},
1424 	{"HZ-GB-2312", 52936},
1425 	{"IBM273", 20273},
1426 	{"IBM277", 20277},
1427 	{"IBM278", 20278},
1428 	{"IBM280", 20280},
1429 	{"IBM284", 20284},
1430 	{"IBM285", 20285},
1431 	{"IBM290", 20290},
1432 	{"IBM297", 20297},
1433 	{"IBM367", 1252},
1434 	{"IBM420", 20420},
1435 	{"IBM423", 20423},
1436 	{"IBM424", 20424},
1437 	{"IBM819", 1252},
1438 	{"IBM871", 20871},
1439 	{"IBM880", 20880},
1440 	{"IBM905", 20905},
1441 	{"IBM924", 20924},
1442 	{"ISO-8859-1", 28591},
1443 	{"ISO-8859-13", 28603},
1444 	{"ISO-8859-15", 28605},
1445 	{"ISO-8859-2", 28592},
1446 	{"ISO-8859-3", 28593},
1447 	{"ISO-8859-4", 28594},
1448 	{"ISO-8859-5", 28595},
1449 	{"ISO-8859-6", 28596},
1450 	{"ISO-8859-7", 28597},
1451 	{"ISO-8859-8", 28598},
1452 	{"ISO-8859-9", 28599},
1453 	{"ISO8859-1", 28591},
1454 	{"ISO8859-13", 28603},
1455 	{"ISO8859-15", 28605},
1456 	{"ISO8859-2", 28592},
1457 	{"ISO8859-3", 28593},
1458 	{"ISO8859-4", 28594},
1459 	{"ISO8859-5", 28595},
1460 	{"ISO8859-6", 28596},
1461 	{"ISO8859-7", 28597},
1462 	{"ISO8859-8", 28598},
1463 	{"ISO8859-9", 28599},
1464 	{"JOHAB", 1361},
1465 	{"KOI8-R", 20866},
1466 	{"KOI8-U", 21866},
1467 	{"KS_C_5601-1987", 949},
1468 	{"LATIN1", 1252},
1469 	{"LATIN2", 28592},
1470 	{"MACINTOSH", 10000},
1471 	{"SHIFT-JIS", 932},
1472 	{"SHIFT_JIS", 932},
1473 	{"SJIS", 932},
1474 	{"US", 1252},
1475 	{"US-ASCII", 1252},
1476 	{"UTF-16", 1200},
1477 	{"UTF-16BE", 1201},
1478 	{"UTF-16LE", 1200},
1479 	{"UTF-8", CP_UTF8},
1480 	{"X-EUROPA", 29001},
1481 	{"X-MAC-ARABIC", 10004},
1482 	{"X-MAC-CE", 10029},
1483 	{"X-MAC-CHINESEIMP", 10008},
1484 	{"X-MAC-CHINESETRAD", 10002},
1485 	{"X-MAC-CROATIAN", 10082},
1486 	{"X-MAC-CYRILLIC", 10007},
1487 	{"X-MAC-GREEK", 10006},
1488 	{"X-MAC-HEBREW", 10005},
1489 	{"X-MAC-ICELANDIC", 10079},
1490 	{"X-MAC-JAPANESE", 10001},
1491 	{"X-MAC-KOREAN", 10003},
1492 	{"X-MAC-ROMANIAN", 10010},
1493 	{"X-MAC-THAI", 10021},
1494 	{"X-MAC-TURKISH", 10081},
1495 	{"X-MAC-UKRAINIAN", 10017},
1496 };
1497 static unsigned
make_codepage_from_charset(const char * charset)1498 make_codepage_from_charset(const char *charset)
1499 {
1500 	char cs[16];
1501 	char *p;
1502 	unsigned cp;
1503 	int a, b;
1504 
1505 	if (charset == NULL || strlen(charset) > 15)
1506 		return -1;
1507 
1508 	/* Copy name to uppercase. */
1509 	p = cs;
1510 	while (*charset) {
1511 		char c = *charset++;
1512 		if (c >= 'a' && c <= 'z')
1513 			c -= 'a' - 'A';
1514 		*p++ = c;
1515 	}
1516 	*p++ = '\0';
1517 	cp = -1;
1518 
1519 	/* Look it up in the table first, so that we can easily
1520 	 * override CP367, which we map to 1252 instead of 367. */
1521 	a = 0;
1522 	b = sizeof(charsets)/sizeof(charsets[0]);
1523 	while (b > a) {
1524 		int c = (b + a) / 2;
1525 		int r = strcmp(charsets[c].name, cs);
1526 		if (r < 0)
1527 			a = c + 1;
1528 		else if (r > 0)
1529 			b = c;
1530 		else
1531 			return charsets[c].cp;
1532 	}
1533 
1534 	/* If it's not in the table, try to parse it. */
1535 	switch (*cs) {
1536 	case 'C':
1537 		if (cs[1] == 'P' && cs[2] >= '0' && cs[2] <= '9') {
1538 			cp = my_atoi(cs + 2);
1539 		} else if (strcmp(cs, "CP_ACP") == 0)
1540 			cp = get_current_codepage();
1541 		else if (strcmp(cs, "CP_OEMCP") == 0)
1542 			cp = get_current_oemcp();
1543 		break;
1544 	case 'I':
1545 		if (cs[1] == 'B' && cs[2] == 'M' &&
1546 		    cs[3] >= '0' && cs[3] <= '9') {
1547 			cp = my_atoi(cs + 3);
1548 		}
1549 		break;
1550 	case 'W':
1551 		if (strncmp(cs, "WINDOWS-", 8) == 0) {
1552 			cp = my_atoi(cs + 8);
1553 			if (cp != 874 && (cp < 1250 || cp > 1258))
1554 				cp = -1;/* This may invalid code. */
1555 		}
1556 		break;
1557 	}
1558 	return (cp);
1559 }
1560 
1561 /*
1562  * Return ANSI Code Page of current locale set by setlocale().
1563  */
1564 static unsigned
get_current_codepage(void)1565 get_current_codepage(void)
1566 {
1567 	char *locale, *p;
1568 	unsigned cp;
1569 
1570 	locale = setlocale(LC_CTYPE, NULL);
1571 	if (locale == NULL)
1572 		return (GetACP());
1573 	if (locale[0] == 'C' && locale[1] == '\0')
1574 		return (CP_C_LOCALE);
1575 	p = strrchr(locale, '.');
1576 	if (p == NULL)
1577 		return (GetACP());
1578 	if ((strcmp(p+1, "utf8") == 0) || (strcmp(p+1, "UTF-8") == 0))
1579 		return CP_UTF8;
1580 	cp = my_atoi(p+1);
1581 	if ((int)cp <= 0)
1582 		return (GetACP());
1583 	return (cp);
1584 }
1585 
1586 /*
1587  * Translation table between Locale Name and ACP/OEMCP.
1588  */
1589 static struct {
1590 	unsigned acp;
1591 	unsigned ocp;
1592 	const char *locale;
1593 } acp_ocp_map[] = {
1594 	{  950,  950, "Chinese_Taiwan" },
1595 	{  936,  936, "Chinese_People's Republic of China" },
1596 	{  950,  950, "Chinese_Taiwan" },
1597 	{ 1250,  852, "Czech_Czech Republic" },
1598 	{ 1252,  850, "Danish_Denmark" },
1599 	{ 1252,  850, "Dutch_Netherlands" },
1600 	{ 1252,  850, "Dutch_Belgium" },
1601 	{ 1252,  437, "English_United States" },
1602 	{ 1252,  850, "English_Australia" },
1603 	{ 1252,  850, "English_Canada" },
1604 	{ 1252,  850, "English_New Zealand" },
1605 	{ 1252,  850, "English_United Kingdom" },
1606 	{ 1252,  437, "English_United States" },
1607 	{ 1252,  850, "Finnish_Finland" },
1608 	{ 1252,  850, "French_France" },
1609 	{ 1252,  850, "French_Belgium" },
1610 	{ 1252,  850, "French_Canada" },
1611 	{ 1252,  850, "French_Switzerland" },
1612 	{ 1252,  850, "German_Germany" },
1613 	{ 1252,  850, "German_Austria" },
1614 	{ 1252,  850, "German_Switzerland" },
1615 	{ 1253,  737, "Greek_Greece" },
1616 	{ 1250,  852, "Hungarian_Hungary" },
1617 	{ 1252,  850, "Icelandic_Iceland" },
1618 	{ 1252,  850, "Italian_Italy" },
1619 	{ 1252,  850, "Italian_Switzerland" },
1620 	{  932,  932, "Japanese_Japan" },
1621 	{  949,  949, "Korean_Korea" },
1622 	{ 1252,  850, "Norwegian (BokmOl)_Norway" },
1623 	{ 1252,  850, "Norwegian (BokmOl)_Norway" },
1624 	{ 1252,  850, "Norwegian-Nynorsk_Norway" },
1625 	{ 1250,  852, "Polish_Poland" },
1626 	{ 1252,  850, "Portuguese_Portugal" },
1627 	{ 1252,  850, "Portuguese_Brazil" },
1628 	{ 1251,  866, "Russian_Russia" },
1629 	{ 1250,  852, "Slovak_Slovakia" },
1630 	{ 1252,  850, "Spanish_Spain" },
1631 	{ 1252,  850, "Spanish_Mexico" },
1632 	{ 1252,  850, "Spanish_Spain" },
1633 	{ 1252,  850, "Swedish_Sweden" },
1634 	{ 1254,  857, "Turkish_Turkey" },
1635 	{ 0, 0, NULL}
1636 };
1637 
1638 /*
1639  * Return OEM Code Page of current locale set by setlocale().
1640  */
1641 static unsigned
get_current_oemcp(void)1642 get_current_oemcp(void)
1643 {
1644 	int i;
1645 	char *locale, *p;
1646 	size_t len;
1647 
1648 	locale = setlocale(LC_CTYPE, NULL);
1649 	if (locale == NULL)
1650 		return (GetOEMCP());
1651 	if (locale[0] == 'C' && locale[1] == '\0')
1652 		return (CP_C_LOCALE);
1653 
1654 	p = strrchr(locale, '.');
1655 	if (p == NULL)
1656 		return (GetOEMCP());
1657 	len = p - locale;
1658 	for (i = 0; acp_ocp_map[i].acp; i++) {
1659 		if (strncmp(acp_ocp_map[i].locale, locale, len) == 0)
1660 			return (acp_ocp_map[i].ocp);
1661 	}
1662 	return (GetOEMCP());
1663 }
1664 #else
1665 
1666 /*
1667  * POSIX platform does not use CodePage.
1668  */
1669 
1670 static unsigned
get_current_codepage(void)1671 get_current_codepage(void)
1672 {
1673 	return (-1);/* Unknown */
1674 }
1675 static unsigned
make_codepage_from_charset(const char * charset)1676 make_codepage_from_charset(const char *charset)
1677 {
1678 	(void)charset; /* UNUSED */
1679 	return (-1);/* Unknown */
1680 }
1681 static unsigned
get_current_oemcp(void)1682 get_current_oemcp(void)
1683 {
1684 	return (-1);/* Unknown */
1685 }
1686 
1687 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1688 
1689 /*
1690  * Return a string conversion object.
1691  */
1692 static struct archive_string_conv *
get_sconv_object(struct archive * a,const char * fc,const char * tc,int flag)1693 get_sconv_object(struct archive *a, const char *fc, const char *tc, int flag)
1694 {
1695 	struct archive_string_conv *sc;
1696 	unsigned current_codepage;
1697 
1698 	/* Check if we have made the sconv object. */
1699 	sc = find_sconv_object(a, fc, tc);
1700 	if (sc != NULL)
1701 		return (sc);
1702 
1703 	if (a == NULL)
1704 		current_codepage = get_current_codepage();
1705 	else
1706 		current_codepage = a->current_codepage;
1707 
1708 	sc = create_sconv_object(canonical_charset_name(fc),
1709 	    canonical_charset_name(tc), current_codepage, flag);
1710 	if (sc == NULL) {
1711 		if (a != NULL)
1712 			archive_set_error(a, ENOMEM,
1713 			    "Could not allocate memory for "
1714 			    "a string conversion object");
1715 		return (NULL);
1716 	}
1717 
1718 	/*
1719 	 * If there is no converter for current string conversion object,
1720 	 * we cannot handle this conversion.
1721 	 */
1722 	if (sc->nconverter == 0) {
1723 		if (a != NULL) {
1724 #if HAVE_ICONV
1725 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
1726 			    "iconv_open failed: Cannot handle ``%s''",
1727 			    (flag & SCONV_TO_CHARSET)?tc:fc);
1728 #else
1729 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
1730 			    "A character-set conversion not fully supported "
1731 			    "on this platform");
1732 #endif
1733 		}
1734 		/* Failed; free a sconv object. */
1735 		free_sconv_object(sc);
1736 		return (NULL);
1737 	}
1738 
1739 	/*
1740 	 * Success!
1741 	 */
1742 	if (a != NULL)
1743 		add_sconv_object(a, sc);
1744 	return (sc);
1745 }
1746 
1747 static const char *
get_current_charset(struct archive * a)1748 get_current_charset(struct archive *a)
1749 {
1750 	const char *cur_charset;
1751 
1752 	if (a == NULL)
1753 		cur_charset = default_iconv_charset("");
1754 	else {
1755 		cur_charset = default_iconv_charset(a->current_code);
1756 		if (a->current_code == NULL) {
1757 			a->current_code = strdup(cur_charset);
1758 			a->current_codepage = get_current_codepage();
1759 			a->current_oemcp = get_current_oemcp();
1760 		}
1761 	}
1762 	return (cur_charset);
1763 }
1764 
1765 /*
1766  * Make and Return a string conversion object.
1767  * Return NULL if the platform does not support the specified conversion
1768  * and best_effort is 0.
1769  * If best_effort is set, A string conversion object must be returned
1770  * unless memory allocation for the object fails, but the conversion
1771  * might fail when non-ASCII code is found.
1772  */
1773 struct archive_string_conv *
archive_string_conversion_to_charset(struct archive * a,const char * charset,int best_effort)1774 archive_string_conversion_to_charset(struct archive *a, const char *charset,
1775     int best_effort)
1776 {
1777 	int flag = SCONV_TO_CHARSET;
1778 
1779 	if (best_effort)
1780 		flag |= SCONV_BEST_EFFORT;
1781 	return (get_sconv_object(a, get_current_charset(a), charset, flag));
1782 }
1783 
1784 struct archive_string_conv *
archive_string_conversion_from_charset(struct archive * a,const char * charset,int best_effort)1785 archive_string_conversion_from_charset(struct archive *a, const char *charset,
1786     int best_effort)
1787 {
1788 	int flag = SCONV_FROM_CHARSET;
1789 
1790 	if (best_effort)
1791 		flag |= SCONV_BEST_EFFORT;
1792 	return (get_sconv_object(a, charset, get_current_charset(a), flag));
1793 }
1794 
1795 /*
1796  * archive_string_default_conversion_*_archive() are provided for Windows
1797  * platform because other archiver application use CP_OEMCP for
1798  * MultiByteToWideChar() and WideCharToMultiByte() for the filenames
1799  * in tar or zip files. But mbstowcs/wcstombs(CRT) usually use CP_ACP
1800  * unless you use setlocale(LC_ALL, ".OCP")(specify CP_OEMCP).
1801  * So we should make a string conversion between CP_ACP and CP_OEMCP
1802  * for compatibility.
1803  */
1804 #if defined(_WIN32) && !defined(__CYGWIN__)
1805 struct archive_string_conv *
archive_string_default_conversion_for_read(struct archive * a)1806 archive_string_default_conversion_for_read(struct archive *a)
1807 {
1808 	const char *cur_charset = get_current_charset(a);
1809 	char oemcp[16];
1810 
1811 	/* NOTE: a check of cur_charset is unneeded but we need
1812 	 * that get_current_charset() has been surely called at
1813 	 * this time whatever C compiler optimized. */
1814 	if (cur_charset != NULL &&
1815 	    (a->current_codepage == CP_C_LOCALE ||
1816 	     a->current_codepage == a->current_oemcp))
1817 		return (NULL);/* no conversion. */
1818 
1819 	_snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1820 	/* Make sure a null termination must be set. */
1821 	oemcp[sizeof(oemcp)-1] = '\0';
1822 	return (get_sconv_object(a, oemcp, cur_charset,
1823 	    SCONV_FROM_CHARSET));
1824 }
1825 
1826 struct archive_string_conv *
archive_string_default_conversion_for_write(struct archive * a)1827 archive_string_default_conversion_for_write(struct archive *a)
1828 {
1829 	const char *cur_charset = get_current_charset(a);
1830 	char oemcp[16];
1831 
1832 	/* NOTE: a check of cur_charset is unneeded but we need
1833 	 * that get_current_charset() has been surely called at
1834 	 * this time whatever C compiler optimized. */
1835 	if (cur_charset != NULL &&
1836 	    (a->current_codepage == CP_C_LOCALE ||
1837 	     a->current_codepage == a->current_oemcp))
1838 		return (NULL);/* no conversion. */
1839 
1840 	_snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1841 	/* Make sure a null termination must be set. */
1842 	oemcp[sizeof(oemcp)-1] = '\0';
1843 	return (get_sconv_object(a, cur_charset, oemcp,
1844 	    SCONV_TO_CHARSET));
1845 }
1846 #else
1847 struct archive_string_conv *
archive_string_default_conversion_for_read(struct archive * a)1848 archive_string_default_conversion_for_read(struct archive *a)
1849 {
1850 	(void)a; /* UNUSED */
1851 	return (NULL);
1852 }
1853 
1854 struct archive_string_conv *
archive_string_default_conversion_for_write(struct archive * a)1855 archive_string_default_conversion_for_write(struct archive *a)
1856 {
1857 	(void)a; /* UNUSED */
1858 	return (NULL);
1859 }
1860 #endif
1861 
1862 /*
1863  * Dispose of all character conversion objects in the archive object.
1864  */
1865 void
archive_string_conversion_free(struct archive * a)1866 archive_string_conversion_free(struct archive *a)
1867 {
1868 	struct archive_string_conv *sc;
1869 	struct archive_string_conv *sc_next;
1870 
1871 	for (sc = a->sconv; sc != NULL; sc = sc_next) {
1872 		sc_next = sc->next;
1873 		free_sconv_object(sc);
1874 	}
1875 	a->sconv = NULL;
1876 	free(a->current_code);
1877 	a->current_code = NULL;
1878 }
1879 
1880 /*
1881  * Return a conversion charset name.
1882  */
1883 const char *
archive_string_conversion_charset_name(struct archive_string_conv * sc)1884 archive_string_conversion_charset_name(struct archive_string_conv *sc)
1885 {
1886 	if (sc == NULL) {
1887 		return "current locale";
1888 	}
1889 	if (sc->flag & SCONV_TO_CHARSET)
1890 		return (sc->to_charset);
1891 	else
1892 		return (sc->from_charset);
1893 }
1894 
1895 /*
1896  * Change the behavior of a string conversion.
1897  */
1898 void
archive_string_conversion_set_opt(struct archive_string_conv * sc,int opt)1899 archive_string_conversion_set_opt(struct archive_string_conv *sc, int opt)
1900 {
1901 	switch (opt) {
1902 	/*
1903 	 * A filename in UTF-8 was made with libarchive 2.x in a wrong
1904 	 * assumption that wchar_t was Unicode.
1905 	 * This option enables simulating the assumption in order to read
1906 	 * that filename correctly.
1907 	 */
1908 	case SCONV_SET_OPT_UTF8_LIBARCHIVE2X:
1909 #if (defined(_WIN32) && !defined(__CYGWIN__)) \
1910 	 || defined(__STDC_ISO_10646__) || defined(__APPLE__)
1911 		/*
1912 		 * Nothing to do for it since wchar_t on these platforms
1913 		 * is really Unicode.
1914 		 */
1915 		(void)sc; /* UNUSED */
1916 #else
1917 		if ((sc->flag & SCONV_UTF8_LIBARCHIVE_2) == 0) {
1918 			sc->flag |= SCONV_UTF8_LIBARCHIVE_2;
1919 			/* Set up string converters. */
1920 			setup_converter(sc);
1921 		}
1922 #endif
1923 		break;
1924 	case SCONV_SET_OPT_NORMALIZATION_C:
1925 		if ((sc->flag & SCONV_NORMALIZATION_C) == 0) {
1926 			sc->flag |= SCONV_NORMALIZATION_C;
1927 			sc->flag &= ~SCONV_NORMALIZATION_D;
1928 			/* Set up string converters. */
1929 			setup_converter(sc);
1930 		}
1931 		break;
1932 	case SCONV_SET_OPT_NORMALIZATION_D:
1933 #if defined(HAVE_ICONV)
1934 		/*
1935 		 * If iconv will take the string, do not change the
1936 		 * setting of the normalization.
1937 		 */
1938 		if (!(sc->flag & SCONV_WIN_CP) &&
1939 		     (sc->flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1940 		    !(sc->flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1941 			break;
1942 #endif
1943 		if ((sc->flag & SCONV_NORMALIZATION_D) == 0) {
1944 			sc->flag |= SCONV_NORMALIZATION_D;
1945 			sc->flag &= ~SCONV_NORMALIZATION_C;
1946 			/* Set up string converters. */
1947 			setup_converter(sc);
1948 		}
1949 		break;
1950 	default:
1951 		break;
1952 	}
1953 }
1954 
1955 /*
1956  *
1957  * Copy one archive_string to another in locale conversion.
1958  *
1959  *	archive_strncat_l();
1960  *	archive_strncpy_l();
1961  *
1962  */
1963 
1964 static size_t
mbsnbytes(const void * _p,size_t n)1965 mbsnbytes(const void *_p, size_t n)
1966 {
1967 	size_t s;
1968 	const char *p, *pp;
1969 
1970 	if (_p == NULL)
1971 		return (0);
1972 	p = (const char *)_p;
1973 
1974 	/* Like strlen(p), except won't examine positions beyond p[n]. */
1975 	s = 0;
1976 	pp = p;
1977 	while (s < n && *pp) {
1978 		pp++;
1979 		s++;
1980 	}
1981 	return (s);
1982 }
1983 
1984 static size_t
utf16nbytes(const void * _p,size_t n)1985 utf16nbytes(const void *_p, size_t n)
1986 {
1987 	size_t s;
1988 	const char *p, *pp;
1989 
1990 	if (_p == NULL)
1991 		return (0);
1992 	p = (const char *)_p;
1993 
1994 	/* Like strlen(p), except won't examine positions beyond p[n]. */
1995 	s = 0;
1996 	pp = p;
1997 	n >>= 1;
1998 	while (s < n && (pp[0] || pp[1])) {
1999 		pp += 2;
2000 		s++;
2001 	}
2002 	return (s<<1);
2003 }
2004 
2005 int
archive_strncpy_l(struct archive_string * as,const void * _p,size_t n,struct archive_string_conv * sc)2006 archive_strncpy_l(struct archive_string *as, const void *_p, size_t n,
2007     struct archive_string_conv *sc)
2008 {
2009 	as->length = 0;
2010 	return (archive_strncat_l(as, _p, n, sc));
2011 }
2012 
2013 int
archive_strncat_l(struct archive_string * as,const void * _p,size_t n,struct archive_string_conv * sc)2014 archive_strncat_l(struct archive_string *as, const void *_p, size_t n,
2015     struct archive_string_conv *sc)
2016 {
2017 	const void *s;
2018 	size_t length = 0;
2019 	int i, r = 0, r2;
2020 
2021 	if (_p != NULL && n > 0) {
2022 		if (sc != NULL && (sc->flag & SCONV_FROM_UTF16))
2023 			length = utf16nbytes(_p, n);
2024 		else
2025 			length = mbsnbytes(_p, n);
2026 	}
2027 
2028 	/* We must allocate memory even if there is no data for conversion
2029 	 * or copy. This simulates archive_string_append behavior. */
2030 	if (length == 0) {
2031 		size_t tn = 1;
2032 		if (sc != NULL && (sc->flag & SCONV_TO_UTF16))
2033 			tn = 2;
2034 		if (archive_string_ensure(as, as->length + tn) == NULL)
2035 			return (-1);
2036 		as->s[as->length] = 0;
2037 		if (tn == 2)
2038 			as->s[as->length+1] = 0;
2039 		return (0);
2040 	}
2041 
2042 	/*
2043 	 * If sc is NULL, we just make a copy.
2044 	 */
2045 	if (sc == NULL) {
2046 		if (archive_string_append(as, _p, length) == NULL)
2047 			return (-1);/* No memory */
2048 		return (0);
2049 	}
2050 
2051 	s = _p;
2052 	i = 0;
2053 	if (sc->nconverter > 1) {
2054 		sc->utftmp.length = 0;
2055 		r2 = sc->converter[0](&(sc->utftmp), s, length, sc);
2056 		if (r2 != 0 && errno == ENOMEM)
2057 			return (r2);
2058 		if (r > r2)
2059 			r = r2;
2060 		s = sc->utftmp.s;
2061 		length = sc->utftmp.length;
2062 		++i;
2063 	}
2064 	r2 = sc->converter[i](as, s, length, sc);
2065 	if (r > r2)
2066 		r = r2;
2067 	return (r);
2068 }
2069 
2070 struct archive_string *
archive_string_dirname(struct archive_string * as)2071 archive_string_dirname(struct archive_string *as)
2072 {
2073 	/* strip trailing separators */
2074 	while (as->length > 1 && as->s[as->length - 1] == '/')
2075 		as->length--;
2076 	/* strip final component */
2077 	while (as->length > 0 && as->s[as->length - 1] != '/')
2078 		as->length--;
2079 	/* empty path -> cwd */
2080 	if (as->length == 0)
2081 		return (archive_strcat(as, "."));
2082 	/* strip separator(s) */
2083 	while (as->length > 1 && as->s[as->length - 1] == '/')
2084 		as->length--;
2085 	/* terminate */
2086 	as->s[as->length] = '\0';
2087 	return (as);
2088 }
2089 
2090 #if HAVE_ICONV
2091 
2092 /*
2093  * Return -1 if conversion fails.
2094  */
2095 static int
iconv_strncat_in_locale(struct archive_string * as,const void * _p,size_t length,struct archive_string_conv * sc)2096 iconv_strncat_in_locale(struct archive_string *as, const void *_p,
2097     size_t length, struct archive_string_conv *sc)
2098 {
2099 	ICONV_CONST char *itp;
2100 	size_t remaining;
2101 	iconv_t cd;
2102 	char *outp;
2103 	size_t avail, bs;
2104 	int return_value = 0; /* success */
2105 	size_t to_size, from_size;
2106 
2107 	if (sc->flag & SCONV_TO_UTF16)
2108 		to_size = 2;
2109 	else
2110 		to_size = 1;
2111 	if (sc->flag & SCONV_FROM_UTF16)
2112 		from_size = 2;
2113 	else
2114 		from_size = 1;
2115 
2116 	if (archive_string_ensure(as, as->length + length*2+to_size) == NULL)
2117 		return (-1);
2118 
2119 	cd = sc->cd;
2120 	itp = (char *)(uintptr_t)_p;
2121 	remaining = length;
2122 	outp = as->s + as->length;
2123 	avail = as->buffer_length - as->length - to_size;
2124 	while (remaining >= from_size) {
2125 		size_t result = iconv(cd, &itp, &remaining, &outp, &avail);
2126 
2127 		if (result != (size_t)-1)
2128 			break; /* Conversion completed. */
2129 
2130 		if (errno == EILSEQ || errno == EINVAL) {
2131 			/*
2132 		 	 * If an output charset is UTF-8 or UTF-16BE/LE,
2133 			 * unknown character should be U+FFFD
2134 			 * (replacement character).
2135 			 */
2136 			if (sc->flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) {
2137 				size_t rbytes;
2138 				if (sc->flag & SCONV_TO_UTF8)
2139 					rbytes = sizeof(utf8_replacement_char);
2140 				else
2141 					rbytes = 2;
2142 
2143 				if (avail < rbytes) {
2144 					as->length = outp - as->s;
2145 					bs = as->buffer_length +
2146 					    (remaining * to_size) + rbytes;
2147 					if (NULL ==
2148 					    archive_string_ensure(as, bs))
2149 						return (-1);
2150 					outp = as->s + as->length;
2151 					avail = as->buffer_length
2152 					    - as->length - to_size;
2153 				}
2154 				if (sc->flag & SCONV_TO_UTF8)
2155 					memcpy(outp, utf8_replacement_char, sizeof(utf8_replacement_char));
2156 				else if (sc->flag & SCONV_TO_UTF16BE)
2157 					archive_be16enc(outp, UNICODE_R_CHAR);
2158 				else
2159 					archive_le16enc(outp, UNICODE_R_CHAR);
2160 				outp += rbytes;
2161 				avail -= rbytes;
2162 			} else {
2163 				/* Skip the illegal input bytes. */
2164 				*outp++ = '?';
2165 				avail--;
2166 			}
2167 			itp += from_size;
2168 			remaining -= from_size;
2169 			return_value = -1; /* failure */
2170 		} else {
2171 			/* E2BIG no output buffer,
2172 			 * Increase an output buffer.  */
2173 			as->length = outp - as->s;
2174 			bs = as->buffer_length + remaining * 2;
2175 			if (NULL == archive_string_ensure(as, bs))
2176 				return (-1);
2177 			outp = as->s + as->length;
2178 			avail = as->buffer_length - as->length - to_size;
2179 		}
2180 	}
2181 	as->length = outp - as->s;
2182 	as->s[as->length] = 0;
2183 	if (to_size == 2)
2184 		as->s[as->length+1] = 0;
2185 	return (return_value);
2186 }
2187 
2188 #endif /* HAVE_ICONV */
2189 
2190 
2191 #if defined(_WIN32) && !defined(__CYGWIN__)
2192 
2193 /*
2194  * Translate a string from a some CodePage to an another CodePage by
2195  * Windows APIs, and copy the result. Return -1 if conversion fails.
2196  */
2197 static int
strncat_in_codepage(struct archive_string * as,const void * _p,size_t length,struct archive_string_conv * sc)2198 strncat_in_codepage(struct archive_string *as,
2199     const void *_p, size_t length, struct archive_string_conv *sc)
2200 {
2201 	const char *s = (const char *)_p;
2202 	struct archive_wstring aws;
2203 	size_t l;
2204 	int r, saved_flag;
2205 
2206 	archive_string_init(&aws);
2207 	saved_flag = sc->flag;
2208 	sc->flag &= ~(SCONV_NORMALIZATION_D | SCONV_NORMALIZATION_C);
2209 	r = archive_wstring_append_from_mbs_in_codepage(&aws, s, length, sc);
2210 	sc->flag = saved_flag;
2211 	if (r != 0) {
2212 		archive_wstring_free(&aws);
2213 		if (errno != ENOMEM)
2214 			archive_string_append(as, s, length);
2215 		return (-1);
2216 	}
2217 
2218 	l = as->length;
2219 	r = archive_string_append_from_wcs_in_codepage(
2220 	    as, aws.s, aws.length, sc);
2221 	if (r != 0 && errno != ENOMEM && l == as->length)
2222 		archive_string_append(as, s, length);
2223 	archive_wstring_free(&aws);
2224 	return (r);
2225 }
2226 
2227 /*
2228  * Test whether MBS ==> WCS is okay.
2229  */
2230 static int
invalid_mbs(const void * _p,size_t n,struct archive_string_conv * sc)2231 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2232 {
2233 	const char *p = (const char *)_p;
2234 	unsigned codepage;
2235 	DWORD mbflag = MB_ERR_INVALID_CHARS;
2236 
2237 	if (sc->flag & SCONV_FROM_CHARSET)
2238 		codepage = sc->to_cp;
2239 	else
2240 		codepage = sc->from_cp;
2241 
2242 	if (codepage == CP_C_LOCALE)
2243 		return (0);
2244 	if (codepage != CP_UTF8)
2245 		mbflag |= MB_PRECOMPOSED;
2246 
2247 	if (n > (size_t)INT_MAX)
2248 		return (-1); /* Invalid */
2249 	if (MultiByteToWideChar(codepage, mbflag, p, (int)n, NULL, 0) == 0)
2250 		return (-1); /* Invalid */
2251 	return (0); /* Okay */
2252 }
2253 
2254 #else
2255 
2256 /*
2257  * Test whether MBS ==> WCS is okay.
2258  */
2259 static int
invalid_mbs(const void * _p,size_t n,struct archive_string_conv * sc)2260 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2261 {
2262 	const char *p = (const char *)_p;
2263 	size_t r;
2264 
2265 #if HAVE_MBRTOWC
2266 	mbstate_t shift_state;
2267 
2268 	memset(&shift_state, 0, sizeof(shift_state));
2269 #else
2270 	/* Clear the shift state before starting. */
2271 	mbtowc(NULL, NULL, 0);
2272 #endif
2273 	while (n) {
2274 		wchar_t wc;
2275 
2276 #if HAVE_MBRTOWC
2277 		r = mbrtowc(&wc, p, n, &shift_state);
2278 #else
2279 		r = mbtowc(&wc, p, n);
2280 #endif
2281 		if (r == (size_t)-1 || r == (size_t)-2)
2282 			return (-1);/* Invalid. */
2283 		if (r == 0)
2284 			break;
2285 		p += r;
2286 		n -= r;
2287 	}
2288 	(void)sc; /* UNUSED */
2289 	return (0); /* All Okey. */
2290 }
2291 
2292 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2293 
2294 /*
2295  * Basically returns -1 because we cannot make a conversion of charset
2296  * without iconv but in some cases this would return 0.
2297  * Returns 0 if all copied characters are ASCII.
2298  * Returns 0 if both from-locale and to-locale are the same and those
2299  * can be WCS with no error.
2300  */
2301 static int
best_effort_strncat_in_locale(struct archive_string * as,const void * _p,size_t length,struct archive_string_conv * sc)2302 best_effort_strncat_in_locale(struct archive_string *as, const void *_p,
2303     size_t length, struct archive_string_conv *sc)
2304 {
2305 	size_t remaining;
2306 	const uint8_t *itp;
2307 	int return_value = 0; /* success */
2308 
2309 	/*
2310 	 * If both from-locale and to-locale is the same, this makes a copy.
2311 	 * And then this checks all copied MBS can be WCS if so returns 0.
2312 	 */
2313 	if (sc->same) {
2314 		if (archive_string_append(as, _p, length) == NULL)
2315 			return (-1);/* No memory */
2316 		return (invalid_mbs(_p, length, sc));
2317 	}
2318 
2319 	/*
2320 	 * If a character is ASCII, this just copies it. If not, this
2321 	 * assigns '?' character instead but in UTF-8 locale this assigns
2322 	 * byte sequence 0xEF 0xBD 0xBD, which are code point U+FFFD,
2323 	 * a Replacement Character in Unicode.
2324 	 */
2325 
2326 	remaining = length;
2327 	itp = (const uint8_t *)_p;
2328 	while (*itp && remaining > 0) {
2329 		if (*itp > 127) {
2330 			// Non-ASCII: Substitute with suitable replacement
2331 			if (sc->flag & SCONV_TO_UTF8) {
2332 				if (archive_string_append(as, utf8_replacement_char, sizeof(utf8_replacement_char)) == NULL) {
2333 					__archive_errx(1, "Out of memory");
2334 				}
2335 			} else {
2336 				archive_strappend_char(as, '?');
2337 			}
2338 			return_value = -1;
2339 		} else {
2340 			archive_strappend_char(as, *itp);
2341 		}
2342 		++itp;
2343 	}
2344 	return (return_value);
2345 }
2346 
2347 
2348 /*
2349  * Unicode conversion functions.
2350  *   - UTF-8 <===> UTF-8 in removing surrogate pairs.
2351  *   - UTF-8 NFD ===> UTF-8 NFC in removing surrogate pairs.
2352  *   - UTF-8 made by libarchive 2.x ===> UTF-8.
2353  *   - UTF-16BE <===> UTF-8.
2354  *
2355  */
2356 
2357 /*
2358  * Utility to convert a single UTF-8 sequence.
2359  *
2360  * Usually return used bytes, return used byte in negative value when
2361  * a unicode character is replaced with U+FFFD.
2362  * See also http://unicode.org/review/pr-121.html Public Review Issue #121
2363  * Recommended Practice for Replacement Characters.
2364  */
2365 static int
_utf8_to_unicode(uint32_t * pwc,const char * s,size_t n)2366 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2367 {
2368 	static const char utf8_count[256] = {
2369 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
2370 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
2371 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
2372 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
2373 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
2374 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
2375 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
2376 		 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
2377 		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
2378 		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
2379 		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
2380 		 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
2381 		 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
2382 		 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
2383 		 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
2384 		 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
2385 	};
2386 	int ch, i;
2387 	int cnt;
2388 	uint32_t wc;
2389 
2390 	/* Sanity check. */
2391 	if (n == 0)
2392 		return (0);
2393 	/*
2394 	 * Decode 1-4 bytes depending on the value of the first byte.
2395 	 */
2396 	ch = (unsigned char)*s;
2397 	if (ch == 0)
2398 		return (0); /* Standard:  return 0 for end-of-string. */
2399 	cnt = utf8_count[ch];
2400 
2401 	/* Invalid sequence or there are not plenty bytes. */
2402 	if (n < (size_t)cnt) {
2403 		cnt = (int)n;
2404 		for (i = 1; i < cnt; i++) {
2405 			if ((s[i] & 0xc0) != 0x80) {
2406 				cnt = i;
2407 				break;
2408 			}
2409 		}
2410 		goto invalid_sequence;
2411 	}
2412 
2413 	/* Make a Unicode code point from a single UTF-8 sequence. */
2414 	switch (cnt) {
2415 	case 1:	/* 1 byte sequence. */
2416 		*pwc = ch & 0x7f;
2417 		return (cnt);
2418 	case 2:	/* 2 bytes sequence. */
2419 		if ((s[1] & 0xc0) != 0x80) {
2420 			cnt = 1;
2421 			goto invalid_sequence;
2422 		}
2423 		*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
2424 		return (cnt);
2425 	case 3:	/* 3 bytes sequence. */
2426 		if ((s[1] & 0xc0) != 0x80) {
2427 			cnt = 1;
2428 			goto invalid_sequence;
2429 		}
2430 		if ((s[2] & 0xc0) != 0x80) {
2431 			cnt = 2;
2432 			goto invalid_sequence;
2433 		}
2434 		wc = ((ch & 0x0f) << 12)
2435 		    | ((s[1] & 0x3f) << 6)
2436 		    | (s[2] & 0x3f);
2437 		if (wc < 0x800)
2438 			goto invalid_sequence;/* Overlong sequence. */
2439 		break;
2440 	case 4:	/* 4 bytes sequence. */
2441 		if ((s[1] & 0xc0) != 0x80) {
2442 			cnt = 1;
2443 			goto invalid_sequence;
2444 		}
2445 		if ((s[2] & 0xc0) != 0x80) {
2446 			cnt = 2;
2447 			goto invalid_sequence;
2448 		}
2449 		if ((s[3] & 0xc0) != 0x80) {
2450 			cnt = 3;
2451 			goto invalid_sequence;
2452 		}
2453 		wc = ((ch & 0x07) << 18)
2454 		    | ((s[1] & 0x3f) << 12)
2455 		    | ((s[2] & 0x3f) << 6)
2456 		    | (s[3] & 0x3f);
2457 		if (wc < 0x10000)
2458 			goto invalid_sequence;/* Overlong sequence. */
2459 		break;
2460 	default: /* Others are all invalid sequence. */
2461 		if (ch == 0xc0 || ch == 0xc1)
2462 			cnt = 2;
2463 		else if (ch >= 0xf5 && ch <= 0xf7)
2464 			cnt = 4;
2465 		else if (ch >= 0xf8 && ch <= 0xfb)
2466 			cnt = 5;
2467 		else if (ch == 0xfc || ch == 0xfd)
2468 			cnt = 6;
2469 		else
2470 			cnt = 1;
2471 		if (n < (size_t)cnt)
2472 			cnt = (int)n;
2473 		for (i = 1; i < cnt; i++) {
2474 			if ((s[i] & 0xc0) != 0x80) {
2475 				cnt = i;
2476 				break;
2477 			}
2478 		}
2479 		goto invalid_sequence;
2480 	}
2481 
2482 	/* The code point larger than 0x10FFFF is not legal
2483 	 * Unicode values. */
2484 	if (wc > UNICODE_MAX)
2485 		goto invalid_sequence;
2486 	/* Correctly gets a Unicode, returns used bytes. */
2487 	*pwc = wc;
2488 	return (cnt);
2489 invalid_sequence:
2490 	*pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2491 	return (cnt * -1);
2492 }
2493 
2494 static int
utf8_to_unicode(uint32_t * pwc,const char * s,size_t n)2495 utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2496 {
2497 	int cnt;
2498 
2499 	cnt = _utf8_to_unicode(pwc, s, n);
2500 	/* Any of Surrogate pair is not legal Unicode values. */
2501 	if (cnt == 3 && IS_SURROGATE_PAIR_LA(*pwc))
2502 		return (-3);
2503 	return (cnt);
2504 }
2505 
2506 static inline uint32_t
combine_surrogate_pair(uint32_t uc,uint32_t uc2)2507 combine_surrogate_pair(uint32_t uc, uint32_t uc2)
2508 {
2509 	uc -= 0xD800;
2510 	uc *= 0x400;
2511 	uc += uc2 - 0xDC00;
2512 	uc += 0x10000;
2513 	return (uc);
2514 }
2515 
2516 /*
2517  * Convert a single UTF-8/CESU-8 sequence to a Unicode code point in
2518  * removing surrogate pairs.
2519  *
2520  * CESU-8: The Compatibility Encoding Scheme for UTF-16.
2521  *
2522  * Usually return used bytes, return used byte in negative value when
2523  * a unicode character is replaced with U+FFFD.
2524  */
2525 static int
cesu8_to_unicode(uint32_t * pwc,const char * s,size_t n)2526 cesu8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2527 {
2528 	uint32_t wc = 0;
2529 	int cnt;
2530 
2531 	cnt = _utf8_to_unicode(&wc, s, n);
2532 	if (cnt == 3 && IS_HIGH_SURROGATE_LA(wc)) {
2533 		uint32_t wc2 = 0;
2534 		if (n - 3 < 3) {
2535 			/* Invalid byte sequence. */
2536 			goto invalid_sequence;
2537 		}
2538 		cnt = _utf8_to_unicode(&wc2, s+3, n-3);
2539 		if (cnt != 3 || !IS_LOW_SURROGATE_LA(wc2)) {
2540 			/* Invalid byte sequence. */
2541 			goto invalid_sequence;
2542 		}
2543 		wc = combine_surrogate_pair(wc, wc2);
2544 		cnt = 6;
2545 	} else if (cnt == 3 && IS_LOW_SURROGATE_LA(wc)) {
2546 		/* Invalid byte sequence. */
2547 		goto invalid_sequence;
2548 	}
2549 	*pwc = wc;
2550 	return (cnt);
2551 invalid_sequence:
2552 	*pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2553 	if (cnt > 0)
2554 		cnt *= -1;
2555 	return (cnt);
2556 }
2557 
2558 /*
2559  * Convert a Unicode code point to a single UTF-8 sequence.
2560  *
2561  * NOTE:This function does not check if the Unicode is legal or not.
2562  * Please you definitely check it before calling this.
2563  */
2564 static size_t
unicode_to_utf8(char * p,size_t remaining,uint32_t uc)2565 unicode_to_utf8(char *p, size_t remaining, uint32_t uc)
2566 {
2567 	char *_p = p;
2568 
2569 	/* Invalid Unicode char maps to Replacement character */
2570 	if (uc > UNICODE_MAX)
2571 		uc = UNICODE_R_CHAR;
2572 	/* Translate code point to UTF8 */
2573 	if (uc <= 0x7f) {
2574 		if (remaining == 0)
2575 			return (0);
2576 		*p++ = (char)uc;
2577 	} else if (uc <= 0x7ff) {
2578 		if (remaining < 2)
2579 			return (0);
2580 		*p++ = 0xc0 | ((uc >> 6) & 0x1f);
2581 		*p++ = 0x80 | (uc & 0x3f);
2582 	} else if (uc <= 0xffff) {
2583 		if (remaining < 3)
2584 			return (0);
2585 		*p++ = 0xe0 | ((uc >> 12) & 0x0f);
2586 		*p++ = 0x80 | ((uc >> 6) & 0x3f);
2587 		*p++ = 0x80 | (uc & 0x3f);
2588 	} else {
2589 		if (remaining < 4)
2590 			return (0);
2591 		*p++ = 0xf0 | ((uc >> 18) & 0x07);
2592 		*p++ = 0x80 | ((uc >> 12) & 0x3f);
2593 		*p++ = 0x80 | ((uc >> 6) & 0x3f);
2594 		*p++ = 0x80 | (uc & 0x3f);
2595 	}
2596 	return (p - _p);
2597 }
2598 
2599 static int
utf16be_to_unicode(uint32_t * pwc,const char * s,size_t n)2600 utf16be_to_unicode(uint32_t *pwc, const char *s, size_t n)
2601 {
2602 	return (utf16_to_unicode(pwc, s, n, 1));
2603 }
2604 
2605 static int
utf16le_to_unicode(uint32_t * pwc,const char * s,size_t n)2606 utf16le_to_unicode(uint32_t *pwc, const char *s, size_t n)
2607 {
2608 	return (utf16_to_unicode(pwc, s, n, 0));
2609 }
2610 
2611 static int
utf16_to_unicode(uint32_t * pwc,const char * s,size_t n,int be)2612 utf16_to_unicode(uint32_t *pwc, const char *s, size_t n, int be)
2613 {
2614 	const char *utf16 = s;
2615 	unsigned uc;
2616 
2617 	if (n == 0)
2618 		return (0);
2619 	if (n == 1) {
2620 		/* set the Replacement Character instead. */
2621 		*pwc = UNICODE_R_CHAR;
2622 		return (-1);
2623 	}
2624 
2625 	if (be)
2626 		uc = archive_be16dec(utf16);
2627 	else
2628 		uc = archive_le16dec(utf16);
2629 	utf16 += 2;
2630 
2631 	/* If this is a surrogate pair, assemble the full code point.*/
2632 	if (IS_HIGH_SURROGATE_LA(uc)) {
2633 		unsigned uc2;
2634 
2635 		if (n >= 4) {
2636 			if (be)
2637 				uc2 = archive_be16dec(utf16);
2638 			else
2639 				uc2 = archive_le16dec(utf16);
2640 		} else
2641 			uc2 = 0;
2642 		if (IS_LOW_SURROGATE_LA(uc2)) {
2643 			uc = combine_surrogate_pair(uc, uc2);
2644 			utf16 += 2;
2645 		} else {
2646 	 		/* Undescribed code point should be U+FFFD
2647 		 	* (replacement character). */
2648 			*pwc = UNICODE_R_CHAR;
2649 			return (-2);
2650 		}
2651 	}
2652 
2653 	/*
2654 	 * Surrogate pair values(0xd800 through 0xdfff) are only
2655 	 * used by UTF-16, so, after above calculation, the code
2656 	 * must not be surrogate values, and Unicode has no codes
2657 	 * larger than 0x10ffff. Thus, those are not legal Unicode
2658 	 * values.
2659 	 */
2660 	if (IS_SURROGATE_PAIR_LA(uc) || uc > UNICODE_MAX) {
2661 	 	/* Undescribed code point should be U+FFFD
2662 	 	* (replacement character). */
2663 		*pwc = UNICODE_R_CHAR;
2664 		return (((int)(utf16 - s)) * -1);
2665 	}
2666 	*pwc = uc;
2667 	return ((int)(utf16 - s));
2668 }
2669 
2670 static size_t
unicode_to_utf16be(char * p,size_t remaining,uint32_t uc)2671 unicode_to_utf16be(char *p, size_t remaining, uint32_t uc)
2672 {
2673 	char *utf16 = p;
2674 
2675 	if (uc > 0xffff) {
2676 		/* We have a code point that won't fit into a
2677 		 * wchar_t; convert it to a surrogate pair. */
2678 		if (remaining < 4)
2679 			return (0);
2680 		uc -= 0x10000;
2681 		archive_be16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2682 		archive_be16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2683 		return (4);
2684 	} else {
2685 		if (remaining < 2)
2686 			return (0);
2687 		archive_be16enc(utf16, (uint16_t)uc);
2688 		return (2);
2689 	}
2690 }
2691 
2692 static size_t
unicode_to_utf16le(char * p,size_t remaining,uint32_t uc)2693 unicode_to_utf16le(char *p, size_t remaining, uint32_t uc)
2694 {
2695 	char *utf16 = p;
2696 
2697 	if (uc > 0xffff) {
2698 		/* We have a code point that won't fit into a
2699 		 * wchar_t; convert it to a surrogate pair. */
2700 		if (remaining < 4)
2701 			return (0);
2702 		uc -= 0x10000;
2703 		archive_le16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2704 		archive_le16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2705 		return (4);
2706 	} else {
2707 		if (remaining < 2)
2708 			return (0);
2709 		archive_le16enc(utf16, (uint16_t)uc);
2710 		return (2);
2711 	}
2712 }
2713 
2714 /*
2715  * Append new UTF-8 string to existing UTF-8 string.
2716  * Existing string is assumed to already be in proper form;
2717  * the new string will have invalid sequences replaced and
2718  * surrogate pairs canonicalized.
2719  */
2720 static int
strncat_from_utf8_to_utf8(struct archive_string * as,const void * _src,size_t len,struct archive_string_conv * sc)2721 strncat_from_utf8_to_utf8(struct archive_string *as, const void *_src,
2722     size_t len, struct archive_string_conv *sc)
2723 {
2724 	int ret = 0;
2725 	const char *src = _src;
2726 	(void)sc; /* UNUSED */
2727 
2728 	/* Pre-extend the destination */
2729 	if (archive_string_ensure(as, as->length + len + 1) == NULL)
2730 		return (-1);
2731 
2732 	/* Invariant: src points to the first UTF8 byte that hasn't
2733 	 * been copied to the destination `as`. */
2734 	for (;;) {
2735 		int n;
2736 		uint32_t uc;
2737 		const char *e = src;
2738 
2739 		/* Skip UTF-8 sequences until we reach end-of-string or
2740 		 * a code point that needs conversion. */
2741 		while ((n = utf8_to_unicode(&uc, e, len)) > 0) {
2742 			e += n;
2743 			len -= n;
2744 		}
2745 		/* Copy the part that doesn't need conversion */
2746 		if (e > src) {
2747 			if (archive_string_append(as, src, e - src) == NULL)
2748 				return (-1);
2749 			src = e;
2750 		}
2751 
2752 		if (n == 0) {
2753 			/* We reached end-of-string */
2754 			return (ret);
2755 		} else {
2756 			/* Next code point needs conversion */
2757 			char t[4];
2758 			size_t w;
2759 
2760 			/* Try decoding a surrogate pair */
2761 			if (n == -3 && IS_SURROGATE_PAIR_LA(uc)) {
2762 				n = cesu8_to_unicode(&uc, src, len);
2763 			}
2764 			/* Not a (valid) surrogate, so use a replacement char */
2765 			if (n < 0) {
2766 				ret = -1; /* Return -1 if we used any replacement */
2767 				n *= -1;
2768 			}
2769 			/* Consume converted code point */
2770 			src += n;
2771 			len -= n;
2772 			/* Convert and append new UTF-8 sequence. */
2773 			w = unicode_to_utf8(t, sizeof(t), uc);
2774 			if (archive_string_append(as, t, w) == NULL)
2775 				return (-1);
2776 		}
2777 	}
2778 }
2779 
2780 static int
archive_string_append_unicode(struct archive_string * as,const void * _p,size_t len,struct archive_string_conv * sc)2781 archive_string_append_unicode(struct archive_string *as, const void *_p,
2782     size_t len, struct archive_string_conv *sc)
2783 {
2784 	const char *s;
2785 	char *p, *endp;
2786 	uint32_t uc;
2787 	size_t w;
2788 	size_t ts, tm;
2789 	int n, ret = 0;
2790 	int (*parse)(uint32_t *, const char *, size_t);
2791 	size_t (*unparse)(char *, size_t, uint32_t);
2792 
2793 	if (sc->flag & SCONV_TO_UTF16BE) {
2794 		unparse = unicode_to_utf16be;
2795 		ts = 2;
2796 	} else if (sc->flag & SCONV_TO_UTF16LE) {
2797 		unparse = unicode_to_utf16le;
2798 		ts = 2;
2799 	} else if (sc->flag & SCONV_TO_UTF8) {
2800 		unparse = unicode_to_utf8;
2801 		ts = 1;
2802 	} else {
2803 		/*
2804 		 * This case is going to be converted to another
2805 		 * character-set through iconv.
2806 		 */
2807 		if (sc->flag & SCONV_FROM_UTF16BE) {
2808 			unparse = unicode_to_utf16be;
2809 			ts = 2;
2810 		} else if (sc->flag & SCONV_FROM_UTF16LE) {
2811 			unparse = unicode_to_utf16le;
2812 			ts = 2;
2813 		} else {
2814 			unparse = unicode_to_utf8;
2815 			ts = 1;
2816 		}
2817 	}
2818 
2819 	if (sc->flag & SCONV_FROM_UTF16BE) {
2820 		parse = utf16be_to_unicode;
2821 		tm = 1;
2822 	} else if (sc->flag & SCONV_FROM_UTF16LE) {
2823 		parse = utf16le_to_unicode;
2824 		tm = 1;
2825 	} else {
2826 		parse = cesu8_to_unicode;
2827 		tm = ts;
2828 	}
2829 
2830 	if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2831 		return (-1);
2832 
2833 	s = (const char *)_p;
2834 	p = as->s + as->length;
2835 	endp = as->s + as->buffer_length - ts;
2836 	while ((n = parse(&uc, s, len)) != 0) {
2837 		if (n < 0) {
2838 			/* Use a replaced unicode character. */
2839 			n *= -1;
2840 			ret = -1;
2841 		}
2842 		s += n;
2843 		len -= n;
2844 		while ((w = unparse(p, endp - p, uc)) == 0) {
2845 			/* There is not enough output buffer so
2846 			 * we have to expand it. */
2847 			as->length = p - as->s;
2848 			if (archive_string_ensure(as,
2849 			    as->buffer_length + len * tm + ts) == NULL)
2850 				return (-1);
2851 			p = as->s + as->length;
2852 			endp = as->s + as->buffer_length - ts;
2853 		}
2854 		p += w;
2855 	}
2856 	as->length = p - as->s;
2857 	as->s[as->length] = '\0';
2858 	if (ts == 2)
2859 		as->s[as->length+1] = '\0';
2860 	return (ret);
2861 }
2862 
2863 /*
2864  * Following Constants for Hangul compositions this information comes from
2865  * Unicode Standard Annex #15  http://unicode.org/reports/tr15/
2866  */
2867 #define HC_SBASE	0xAC00
2868 #define HC_LBASE	0x1100
2869 #define HC_VBASE	0x1161
2870 #define HC_TBASE	0x11A7
2871 #define HC_LCOUNT	19
2872 #define HC_VCOUNT	21
2873 #define HC_TCOUNT	28
2874 #define HC_NCOUNT	(HC_VCOUNT * HC_TCOUNT)
2875 #define HC_SCOUNT	(HC_LCOUNT * HC_NCOUNT)
2876 
2877 static uint32_t
get_nfc(uint32_t uc,uint32_t uc2)2878 get_nfc(uint32_t uc, uint32_t uc2)
2879 {
2880 	int t, b;
2881 
2882 	t = 0;
2883 	b = sizeof(u_composition_table)/sizeof(u_composition_table[0]) -1;
2884 	while (b >= t) {
2885 		int m = (t + b) / 2;
2886 		if (u_composition_table[m].cp1 < uc)
2887 			t = m + 1;
2888 		else if (u_composition_table[m].cp1 > uc)
2889 			b = m - 1;
2890 		else if (u_composition_table[m].cp2 < uc2)
2891 			t = m + 1;
2892 		else if (u_composition_table[m].cp2 > uc2)
2893 			b = m - 1;
2894 		else
2895 			return (u_composition_table[m].nfc);
2896 	}
2897 	return (0);
2898 }
2899 
2900 #define FDC_MAX 10	/* The maximum number of Following Decomposable
2901 			 * Characters. */
2902 
2903 /*
2904  * Update first code point.
2905  */
2906 #define UPDATE_UC(new_uc)	do {		\
2907 	uc = new_uc;				\
2908 	ucptr = NULL;				\
2909 } while (0)
2910 
2911 /*
2912  * Replace first code point with second code point.
2913  */
2914 #define REPLACE_UC_WITH_UC2() do {		\
2915 	uc = uc2;				\
2916 	ucptr = uc2ptr;				\
2917 	n = n2;					\
2918 } while (0)
2919 
2920 #define EXPAND_BUFFER() do {			\
2921 	as->length = p - as->s;			\
2922 	if (archive_string_ensure(as,		\
2923 	    as->buffer_length + len * tm + ts) == NULL)\
2924 		return (-1);			\
2925 	p = as->s + as->length;			\
2926 	endp = as->s + as->buffer_length - ts;	\
2927 } while (0)
2928 
2929 #define UNPARSE(p, endp, uc)	do {		\
2930 	while ((w = unparse(p, (endp) - (p), uc)) == 0) {\
2931 		EXPAND_BUFFER();		\
2932 	}					\
2933 	p += w;					\
2934 } while (0)
2935 
2936 /*
2937  * Write first code point.
2938  * If the code point has not be changed from its original code,
2939  * this just copies it from its original buffer pointer.
2940  * If not, this converts it to UTF-8 byte sequence and copies it.
2941  */
2942 #define WRITE_UC()	do {			\
2943 	if (ucptr) {				\
2944 		if (p + n > endp)		\
2945 			EXPAND_BUFFER();	\
2946 		switch (n) {			\
2947 		case 4:				\
2948 			*p++ = *ucptr++;	\
2949 			/* FALL THROUGH */	\
2950 		case 3:				\
2951 			*p++ = *ucptr++;	\
2952 			/* FALL THROUGH */	\
2953 		case 2:				\
2954 			*p++ = *ucptr++;	\
2955 			/* FALL THROUGH */	\
2956 		case 1:				\
2957 			*p++ = *ucptr;		\
2958 			break;			\
2959 		}				\
2960 		ucptr = NULL;			\
2961 	} else {				\
2962 		UNPARSE(p, endp, uc);		\
2963 	}					\
2964 } while (0)
2965 
2966 /*
2967  * Collect following decomposable code points.
2968  */
2969 #define COLLECT_CPS(start)	do {		\
2970 	int _i;					\
2971 	for (_i = start; _i < FDC_MAX ; _i++) {	\
2972 		nx = parse(&ucx[_i], s, len);	\
2973 		if (nx <= 0)			\
2974 			break;			\
2975 		cx = CCC(ucx[_i]);		\
2976 		if (cl >= cx && cl != 228 && cx != 228)\
2977 			break;			\
2978 		s += nx;			\
2979 		len -= nx;			\
2980 		cl = cx;			\
2981 		ccx[_i] = cx;			\
2982 	}					\
2983 	if (_i >= FDC_MAX) {			\
2984 		ret = -1;			\
2985 		ucx_size = FDC_MAX;		\
2986 	} else					\
2987 		ucx_size = _i;			\
2988 } while (0)
2989 
2990 /*
2991  * Normalize UTF-8/UTF-16BE characters to Form C and copy the result.
2992  *
2993  * TODO: Convert composition exclusions, which are never converted
2994  * from NFC,NFD,NFKC and NFKD, to Form C.
2995  */
2996 static int
archive_string_normalize_C(struct archive_string * as,const void * _p,size_t len,struct archive_string_conv * sc)2997 archive_string_normalize_C(struct archive_string *as, const void *_p,
2998     size_t len, struct archive_string_conv *sc)
2999 {
3000 	const char *s = (const char *)_p;
3001 	char *p, *endp;
3002 	uint32_t uc, uc2;
3003 	size_t w;
3004 	int always_replace, n, n2, ret = 0, spair, ts, tm;
3005 	int (*parse)(uint32_t *, const char *, size_t);
3006 	size_t (*unparse)(char *, size_t, uint32_t);
3007 
3008 	always_replace = 1;
3009 	ts = 1;/* text size. */
3010 	if (sc->flag & SCONV_TO_UTF16BE) {
3011 		unparse = unicode_to_utf16be;
3012 		ts = 2;
3013 		if (sc->flag & SCONV_FROM_UTF16BE)
3014 			always_replace = 0;
3015 	} else if (sc->flag & SCONV_TO_UTF16LE) {
3016 		unparse = unicode_to_utf16le;
3017 		ts = 2;
3018 		if (sc->flag & SCONV_FROM_UTF16LE)
3019 			always_replace = 0;
3020 	} else if (sc->flag & SCONV_TO_UTF8) {
3021 		unparse = unicode_to_utf8;
3022 		if (sc->flag & SCONV_FROM_UTF8)
3023 			always_replace = 0;
3024 	} else {
3025 		/*
3026 		 * This case is going to be converted to another
3027 		 * character-set through iconv.
3028 		 */
3029 		always_replace = 0;
3030 		if (sc->flag & SCONV_FROM_UTF16BE) {
3031 			unparse = unicode_to_utf16be;
3032 			ts = 2;
3033 		} else if (sc->flag & SCONV_FROM_UTF16LE) {
3034 			unparse = unicode_to_utf16le;
3035 			ts = 2;
3036 		} else {
3037 			unparse = unicode_to_utf8;
3038 		}
3039 	}
3040 
3041 	if (sc->flag & SCONV_FROM_UTF16BE) {
3042 		parse = utf16be_to_unicode;
3043 		tm = 1;
3044 		spair = 4;/* surrogate pair size in UTF-16. */
3045 	} else if (sc->flag & SCONV_FROM_UTF16LE) {
3046 		parse = utf16le_to_unicode;
3047 		tm = 1;
3048 		spair = 4;/* surrogate pair size in UTF-16. */
3049 	} else {
3050 		parse = cesu8_to_unicode;
3051 		tm = ts;
3052 		spair = 6;/* surrogate pair size in UTF-8. */
3053 	}
3054 
3055 	if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
3056 		return (-1);
3057 
3058 	p = as->s + as->length;
3059 	endp = as->s + as->buffer_length - ts;
3060 	while ((n = parse(&uc, s, len)) != 0) {
3061 		const char *ucptr, *uc2ptr;
3062 
3063 		if (n < 0) {
3064 			/* Use a replaced unicode character. */
3065 			UNPARSE(p, endp, uc);
3066 			s += n*-1;
3067 			len -= n*-1;
3068 			ret = -1;
3069 			continue;
3070 		} else if (n == spair || always_replace)
3071 			/* uc is converted from a surrogate pair.
3072 			 * this should be treated as a changed code. */
3073 			ucptr = NULL;
3074 		else
3075 			ucptr = s;
3076 		s += n;
3077 		len -= n;
3078 
3079 		/* Read second code point. */
3080 		while ((n2 = parse(&uc2, s, len)) > 0) {
3081 			uint32_t ucx[FDC_MAX];
3082 			int ccx[FDC_MAX];
3083 			int cl, cx, i, nx, ucx_size;
3084 			int LIndex,SIndex;
3085 			uint32_t nfc;
3086 
3087 			if (n2 == spair || always_replace)
3088 				/* uc2 is converted from a surrogate pair.
3089 			 	 * this should be treated as a changed code. */
3090 				uc2ptr = NULL;
3091 			else
3092 				uc2ptr = s;
3093 			s += n2;
3094 			len -= n2;
3095 
3096 			/*
3097 			 * If current second code point is out of decomposable
3098 			 * code points, finding compositions is unneeded.
3099 			 */
3100 			if (!IS_DECOMPOSABLE_BLOCK(uc2)) {
3101 				WRITE_UC();
3102 				REPLACE_UC_WITH_UC2();
3103 				continue;
3104 			}
3105 
3106 			/*
3107 			 * Try to combine current code points.
3108 			 */
3109 			/*
3110 			 * We have to combine Hangul characters according to
3111 			 * http://uniicode.org/reports/tr15/#Hangul
3112 			 */
3113 			if (0 <= (LIndex = uc - HC_LBASE) &&
3114 			    LIndex < HC_LCOUNT) {
3115 				/*
3116 				 * Hangul Composition.
3117 				 * 1. Two current code points are L and V.
3118 				 */
3119 				int VIndex = uc2 - HC_VBASE;
3120 				if (0 <= VIndex && VIndex < HC_VCOUNT) {
3121 					/* Make syllable of form LV. */
3122 					UPDATE_UC(HC_SBASE +
3123 					    (LIndex * HC_VCOUNT + VIndex) *
3124 					     HC_TCOUNT);
3125 				} else {
3126 					WRITE_UC();
3127 					REPLACE_UC_WITH_UC2();
3128 				}
3129 				continue;
3130 			} else if (0 <= (SIndex = uc - HC_SBASE) &&
3131 			    SIndex < HC_SCOUNT && (SIndex % HC_TCOUNT) == 0) {
3132 				/*
3133 				 * Hangul Composition.
3134 				 * 2. Two current code points are LV and T.
3135 				 */
3136 				int TIndex = uc2 - HC_TBASE;
3137 				if (0 < TIndex && TIndex < HC_TCOUNT) {
3138 					/* Make syllable of form LVT. */
3139 					UPDATE_UC(uc + TIndex);
3140 				} else {
3141 					WRITE_UC();
3142 					REPLACE_UC_WITH_UC2();
3143 				}
3144 				continue;
3145 			} else if ((nfc = get_nfc(uc, uc2)) != 0) {
3146 				/* A composition to current code points
3147 				 * is found. */
3148 				UPDATE_UC(nfc);
3149 				continue;
3150 			} else if ((cl = CCC(uc2)) == 0) {
3151 				/* Clearly 'uc2' the second code point is not
3152 				 * a decomposable code. */
3153 				WRITE_UC();
3154 				REPLACE_UC_WITH_UC2();
3155 				continue;
3156 			}
3157 
3158 			/*
3159 			 * Collect following decomposable code points.
3160 			 */
3161 			cx = 0;
3162 			ucx[0] = uc2;
3163 			ccx[0] = cl;
3164 			COLLECT_CPS(1);
3165 
3166 			/*
3167 			 * Find a composed code in the collected code points.
3168 			 */
3169 			i = 1;
3170 			while (i < ucx_size) {
3171 				int j;
3172 
3173 				if ((nfc = get_nfc(uc, ucx[i])) == 0) {
3174 					i++;
3175 					continue;
3176 				}
3177 
3178 				/*
3179 				 * nfc is composed of uc and ucx[i].
3180 				 */
3181 				UPDATE_UC(nfc);
3182 
3183 				/*
3184 				 * Remove ucx[i] by shifting
3185 				 * following code points.
3186 				 */
3187 				for (j = i; j+1 < ucx_size; j++) {
3188 					ucx[j] = ucx[j+1];
3189 					ccx[j] = ccx[j+1];
3190 				}
3191 				ucx_size --;
3192 
3193 				/*
3194 				 * Collect following code points blocked
3195 				 * by ucx[i] the removed code point.
3196 				 */
3197 				if (ucx_size > 0 && i == ucx_size &&
3198 				    nx > 0 && cx == cl) {
3199 					cl =  ccx[ucx_size-1];
3200 					COLLECT_CPS(ucx_size);
3201 				}
3202 				/*
3203 				 * Restart finding a composed code with
3204 				 * the updated uc from the top of the
3205 				 * collected code points.
3206 				 */
3207 				i = 0;
3208 			}
3209 
3210 			/*
3211 			 * Apparently the current code points are not
3212 			 * decomposed characters or already composed.
3213 			 */
3214 			WRITE_UC();
3215 			for (i = 0; i < ucx_size; i++)
3216 				UNPARSE(p, endp, ucx[i]);
3217 
3218 			/*
3219 			 * Flush out remaining canonical combining characters.
3220 			 */
3221 			if (nx > 0 && cx == cl && len > 0) {
3222 				while ((nx = parse(&ucx[0], s, len))
3223 				    > 0) {
3224 					cx = CCC(ucx[0]);
3225 					if (cl > cx)
3226 						break;
3227 					s += nx;
3228 					len -= nx;
3229 					cl = cx;
3230 					UNPARSE(p, endp, ucx[0]);
3231 				}
3232 			}
3233 			break;
3234 		}
3235 		if (n2 < 0) {
3236 			WRITE_UC();
3237 			/* Use a replaced unicode character. */
3238 			UNPARSE(p, endp, uc2);
3239 			s += n2*-1;
3240 			len -= n2*-1;
3241 			ret = -1;
3242 			continue;
3243 		} else if (n2 == 0) {
3244 			WRITE_UC();
3245 			break;
3246 		}
3247 	}
3248 	as->length = p - as->s;
3249 	as->s[as->length] = '\0';
3250 	if (ts == 2)
3251 		as->s[as->length+1] = '\0';
3252 	return (ret);
3253 }
3254 
3255 static int
get_nfd(uint32_t * cp1,uint32_t * cp2,uint32_t uc)3256 get_nfd(uint32_t *cp1, uint32_t *cp2, uint32_t uc)
3257 {
3258 	int t, b;
3259 
3260 	/*
3261 	 * These are not converted to NFD on Mac OS.
3262 	 */
3263 	if ((uc >= 0x2000 && uc <= 0x2FFF) ||
3264 	    (uc >= 0xF900 && uc <= 0xFAFF) ||
3265 	    (uc >= 0x2F800 && uc <= 0x2FAFF))
3266 		return (0);
3267 	/*
3268 	 * Those code points are not converted to NFD on Mac OS.
3269 	 * I do not know the reason because it is undocumented.
3270 	 *   NFC        NFD
3271 	 *   1109A  ==> 11099 110BA
3272 	 *   1109C  ==> 1109B 110BA
3273 	 *   110AB  ==> 110A5 110BA
3274 	 */
3275 	if (uc == 0x1109A || uc == 0x1109C || uc == 0x110AB)
3276 		return (0);
3277 
3278 	t = 0;
3279 	b = sizeof(u_decomposition_table)/sizeof(u_decomposition_table[0]) -1;
3280 	while (b >= t) {
3281 		int m = (t + b) / 2;
3282 		if (u_decomposition_table[m].nfc < uc)
3283 			t = m + 1;
3284 		else if (u_decomposition_table[m].nfc > uc)
3285 			b = m - 1;
3286 		else {
3287 			*cp1 = u_decomposition_table[m].cp1;
3288 			*cp2 = u_decomposition_table[m].cp2;
3289 			return (1);
3290 		}
3291 	}
3292 	return (0);
3293 }
3294 
3295 #define REPLACE_UC_WITH(cp) do {		\
3296 	uc = cp;				\
3297 	ucptr = NULL;				\
3298 } while (0)
3299 
3300 /*
3301  * Normalize UTF-8 characters to Form D and copy the result.
3302  */
3303 static int
archive_string_normalize_D(struct archive_string * as,const void * _p,size_t len,struct archive_string_conv * sc)3304 archive_string_normalize_D(struct archive_string *as, const void *_p,
3305     size_t len, struct archive_string_conv *sc)
3306 {
3307 	const char *s = (const char *)_p;
3308 	char *p, *endp;
3309 	uint32_t uc, uc2;
3310 	size_t w;
3311 	int always_replace, n, n2, ret = 0, spair, ts, tm;
3312 	int (*parse)(uint32_t *, const char *, size_t);
3313 	size_t (*unparse)(char *, size_t, uint32_t);
3314 
3315 	always_replace = 1;
3316 	ts = 1;/* text size. */
3317 	if (sc->flag & SCONV_TO_UTF16BE) {
3318 		unparse = unicode_to_utf16be;
3319 		ts = 2;
3320 		if (sc->flag & SCONV_FROM_UTF16BE)
3321 			always_replace = 0;
3322 	} else if (sc->flag & SCONV_TO_UTF16LE) {
3323 		unparse = unicode_to_utf16le;
3324 		ts = 2;
3325 		if (sc->flag & SCONV_FROM_UTF16LE)
3326 			always_replace = 0;
3327 	} else if (sc->flag & SCONV_TO_UTF8) {
3328 		unparse = unicode_to_utf8;
3329 		if (sc->flag & SCONV_FROM_UTF8)
3330 			always_replace = 0;
3331 	} else {
3332 		/*
3333 		 * This case is going to be converted to another
3334 		 * character-set through iconv.
3335 		 */
3336 		always_replace = 0;
3337 		if (sc->flag & SCONV_FROM_UTF16BE) {
3338 			unparse = unicode_to_utf16be;
3339 			ts = 2;
3340 		} else if (sc->flag & SCONV_FROM_UTF16LE) {
3341 			unparse = unicode_to_utf16le;
3342 			ts = 2;
3343 		} else {
3344 			unparse = unicode_to_utf8;
3345 		}
3346 	}
3347 
3348 	if (sc->flag & SCONV_FROM_UTF16BE) {
3349 		parse = utf16be_to_unicode;
3350 		tm = 1;
3351 		spair = 4;/* surrogate pair size in UTF-16. */
3352 	} else if (sc->flag & SCONV_FROM_UTF16LE) {
3353 		parse = utf16le_to_unicode;
3354 		tm = 1;
3355 		spair = 4;/* surrogate pair size in UTF-16. */
3356 	} else {
3357 		parse = cesu8_to_unicode;
3358 		tm = ts;
3359 		spair = 6;/* surrogate pair size in UTF-8. */
3360 	}
3361 
3362 	if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
3363 		return (-1);
3364 
3365 	p = as->s + as->length;
3366 	endp = as->s + as->buffer_length - ts;
3367 	while ((n = parse(&uc, s, len)) != 0) {
3368 		const char *ucptr;
3369 		uint32_t cp1, cp2;
3370 		int SIndex;
3371 		struct {
3372 			uint32_t uc;
3373 			int ccc;
3374 		} fdc[FDC_MAX];
3375 		int fdi, fdj;
3376 		int ccc;
3377 
3378 check_first_code:
3379 		if (n < 0) {
3380 			/* Use a replaced unicode character. */
3381 			UNPARSE(p, endp, uc);
3382 			s += n*-1;
3383 			len -= n*-1;
3384 			ret = -1;
3385 			continue;
3386 		} else if (n == spair || always_replace)
3387 			/* uc is converted from a surrogate pair.
3388 			 * this should be treated as a changed code. */
3389 			ucptr = NULL;
3390 		else
3391 			ucptr = s;
3392 		s += n;
3393 		len -= n;
3394 
3395 		/* Hangul Decomposition. */
3396 		if ((SIndex = uc - HC_SBASE) >= 0 && SIndex < HC_SCOUNT) {
3397 			int L = HC_LBASE + SIndex / HC_NCOUNT;
3398 			int V = HC_VBASE + (SIndex % HC_NCOUNT) / HC_TCOUNT;
3399 			int T = HC_TBASE + SIndex % HC_TCOUNT;
3400 
3401 			REPLACE_UC_WITH(L);
3402 			WRITE_UC();
3403 			REPLACE_UC_WITH(V);
3404 			WRITE_UC();
3405 			if (T != HC_TBASE) {
3406 				REPLACE_UC_WITH(T);
3407 				WRITE_UC();
3408 			}
3409 			continue;
3410 		}
3411 		if (IS_DECOMPOSABLE_BLOCK(uc) && CCC(uc) != 0) {
3412 			WRITE_UC();
3413 			continue;
3414 		}
3415 
3416 		fdi = 0;
3417 		while (get_nfd(&cp1, &cp2, uc) && fdi < FDC_MAX) {
3418 			int k;
3419 
3420 			for (k = fdi; k > 0; k--)
3421 				fdc[k] = fdc[k-1];
3422 			fdc[0].ccc = CCC(cp2);
3423 			fdc[0].uc = cp2;
3424 			fdi++;
3425 			REPLACE_UC_WITH(cp1);
3426 		}
3427 
3428 		/* Read following code points. */
3429 		while ((n2 = parse(&uc2, s, len)) > 0 &&
3430 		    (ccc = CCC(uc2)) != 0 && fdi < FDC_MAX) {
3431 			int j, k;
3432 
3433 			s += n2;
3434 			len -= n2;
3435 			for (j = 0; j < fdi; j++) {
3436 				if (fdc[j].ccc > ccc)
3437 					break;
3438 			}
3439 			if (j < fdi) {
3440 				for (k = fdi; k > j; k--)
3441 					fdc[k] = fdc[k-1];
3442 				fdc[j].ccc = ccc;
3443 				fdc[j].uc = uc2;
3444 			} else {
3445 				fdc[fdi].ccc = ccc;
3446 				fdc[fdi].uc = uc2;
3447 			}
3448 			fdi++;
3449 		}
3450 
3451 		WRITE_UC();
3452 		for (fdj = 0; fdj < fdi; fdj++) {
3453 			REPLACE_UC_WITH(fdc[fdj].uc);
3454 			WRITE_UC();
3455 		}
3456 
3457 		if (n2 == 0)
3458 			break;
3459 		REPLACE_UC_WITH(uc2);
3460 		n = n2;
3461 		goto check_first_code;
3462 	}
3463 	as->length = p - as->s;
3464 	as->s[as->length] = '\0';
3465 	if (ts == 2)
3466 		as->s[as->length+1] = '\0';
3467 	return (ret);
3468 }
3469 
3470 /*
3471  * libarchive 2.x made incorrect UTF-8 strings in the wrong assumption
3472  * that WCS is Unicode. It is true for several platforms but some are false.
3473  * And then people who did not use UTF-8 locale on the non Unicode WCS
3474  * platform and made a tar file with libarchive(mostly bsdtar) 2.x. Those
3475  * now cannot get right filename from libarchive 3.x and later since we
3476  * fixed the wrong assumption and it is incompatible to older its versions.
3477  * So we provide special option, "compat-2x.x", for resolving it.
3478  * That option enable the string conversion of libarchive 2.x.
3479  *
3480  * Translates the wrong UTF-8 string made by libarchive 2.x into current
3481  * locale character set and appends to the archive_string.
3482  * Note: returns -1 if conversion fails.
3483  */
3484 static int
strncat_from_utf8_libarchive2(struct archive_string * as,const void * _p,size_t len,struct archive_string_conv * sc)3485 strncat_from_utf8_libarchive2(struct archive_string *as,
3486     const void *_p, size_t len, struct archive_string_conv *sc)
3487 {
3488 	const char *s;
3489 	int n;
3490 	char *p;
3491 	char *end;
3492 	uint32_t unicode;
3493 #if HAVE_WCRTOMB
3494 	mbstate_t shift_state;
3495 
3496 	memset(&shift_state, 0, sizeof(shift_state));
3497 #else
3498 	/* Clear the shift state before starting. */
3499 	wctomb(NULL, L'\0');
3500 #endif
3501 	(void)sc; /* UNUSED */
3502 	/*
3503 	 * Allocate buffer for MBS.
3504 	 * We need this allocation here since it is possible that
3505 	 * as->s is still NULL.
3506 	 */
3507 	if (archive_string_ensure(as, as->length + len + 1) == NULL)
3508 		return (-1);
3509 
3510 	s = (const char *)_p;
3511 	p = as->s + as->length;
3512 	end = as->s + as->buffer_length - MB_CUR_MAX -1;
3513 	while ((n = _utf8_to_unicode(&unicode, s, len)) != 0) {
3514 		wchar_t wc;
3515 
3516 		if (p >= end) {
3517 			as->length = p - as->s;
3518 			/* Re-allocate buffer for MBS. */
3519 			if (archive_string_ensure(as,
3520 			    as->length + max(len * 2,
3521 			    (size_t)MB_CUR_MAX) + 1) == NULL)
3522 				return (-1);
3523 			p = as->s + as->length;
3524 			end = as->s + as->buffer_length - MB_CUR_MAX -1;
3525 		}
3526 
3527 		/*
3528 		 * As libarchive 2.x, translates the UTF-8 characters into
3529 		 * wide-characters in the assumption that WCS is Unicode.
3530 		 */
3531 		if (n < 0) {
3532 			n *= -1;
3533 			wc = L'?';
3534 		} else
3535 			wc = (wchar_t)unicode;
3536 
3537 		s += n;
3538 		len -= n;
3539 		/*
3540 		 * Translates the wide-character into the current locale MBS.
3541 		 */
3542 #if HAVE_WCRTOMB
3543 		n = (int)wcrtomb(p, wc, &shift_state);
3544 #else
3545 		n = (int)wctomb(p, wc);
3546 #endif
3547 		if (n == -1)
3548 			return (-1);
3549 		p += n;
3550 	}
3551 	as->length = p - as->s;
3552 	as->s[as->length] = '\0';
3553 	return (0);
3554 }
3555 
3556 
3557 /*
3558  * Conversion functions between current locale dependent MBS and UTF-16BE.
3559  *   strncat_from_utf16be() : UTF-16BE --> MBS
3560  *   strncat_to_utf16be()   : MBS --> UTF16BE
3561  */
3562 
3563 #if defined(_WIN32) && !defined(__CYGWIN__)
3564 
3565 /*
3566  * Convert a UTF-16BE/LE string to current locale and copy the result.
3567  * Return -1 if conversion fails.
3568  */
3569 static int
win_strncat_from_utf16(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc,int be)3570 win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
3571     struct archive_string_conv *sc, int be)
3572 {
3573 	struct archive_string tmp;
3574 	const char *u16;
3575 	BOOL defchar;
3576 	char *mbs;
3577 	size_t mbs_size, b, ll;
3578 	int ret = 0;
3579 
3580 	bytes &= ~1;
3581 	if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3582 		return (-1);
3583 
3584 	mbs = as->s + as->length;
3585 	mbs_size = as->buffer_length - as->length -1;
3586 
3587 	if (sc->to_cp == CP_C_LOCALE) {
3588 		/*
3589 		 * "C" locale special processing.
3590 		 */
3591 		u16 = _p;
3592 		ll = 0;
3593 		for (b = 0; b < bytes; b += 2) {
3594 			uint16_t val;
3595 			if (be)
3596 				val = archive_be16dec(u16+b);
3597 			else
3598 				val = archive_le16dec(u16+b);
3599 			if (val > 255) {
3600 				*mbs++ = '?';
3601 				ret = -1;
3602 			} else
3603 				*mbs++ = (char)(val&0xff);
3604 			ll++;
3605 		}
3606 		as->length += ll;
3607 		as->s[as->length] = '\0';
3608 		return (ret);
3609 	}
3610 
3611 	archive_string_init(&tmp);
3612 	if (be) {
3613 		if (IS_BIG_ENDIAN) {
3614 			u16 = _p;
3615 		} else {
3616 			if (archive_string_ensure(&tmp, bytes+2) == NULL)
3617 				return (-1);
3618 			memcpy(tmp.s, _p, bytes);
3619 			for (b = 0; b < bytes; b += 2) {
3620 				uint16_t val = archive_be16dec(tmp.s+b);
3621 				archive_le16enc(tmp.s+b, val);
3622 			}
3623 			u16 = tmp.s;
3624 		}
3625 	} else {
3626 		if (!IS_BIG_ENDIAN) {
3627 			u16 = _p;
3628 		} else {
3629 			if (archive_string_ensure(&tmp, bytes+2) == NULL)
3630 				return (-1);
3631 			memcpy(tmp.s, _p, bytes);
3632 			for (b = 0; b < bytes; b += 2) {
3633 				uint16_t val = archive_le16dec(tmp.s+b);
3634 				archive_be16enc(tmp.s+b, val);
3635 			}
3636 			u16 = tmp.s;
3637 		}
3638 	}
3639 
3640 	do {
3641 		int r;
3642 		defchar = 0;
3643 		/* WideCharToMultiByte is limited to int. */
3644 		if (bytes > (size_t)INT_MAX || mbs_size > (size_t)INT_MAX)
3645 			return (-1);
3646 		r = WideCharToMultiByte(sc->to_cp, 0,
3647 		    (LPCWSTR)u16, (int)bytes>>1, mbs, (int)mbs_size,
3648 			NULL, &defchar);
3649 		/* Exit loop if we succeeded */
3650 		if (r != 0 ||
3651 		    GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3652 			ll = (size_t)r;
3653 			break;
3654 		}
3655 		/* Else expand buffer and loop to try again. */
3656 		r = WideCharToMultiByte(sc->to_cp, 0,
3657 		    (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
3658 		ll = (size_t)r;
3659 		if (archive_string_ensure(as, ll +1) == NULL)
3660 			return (-1);
3661 		mbs = as->s + as->length;
3662 		mbs_size = as->buffer_length - as->length -1;
3663 	} while (1);
3664 	archive_string_free(&tmp);
3665 	as->length += ll;
3666 	as->s[as->length] = '\0';
3667 	if (ll == 0 || defchar)
3668 		ret = -1;
3669 	return (ret);
3670 }
3671 
3672 static int
win_strncat_from_utf16be(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc)3673 win_strncat_from_utf16be(struct archive_string *as, const void *_p,
3674     size_t bytes, struct archive_string_conv *sc)
3675 {
3676 	return (win_strncat_from_utf16(as, _p, bytes, sc, 1));
3677 }
3678 
3679 static int
win_strncat_from_utf16le(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc)3680 win_strncat_from_utf16le(struct archive_string *as, const void *_p,
3681     size_t bytes, struct archive_string_conv *sc)
3682 {
3683 	return (win_strncat_from_utf16(as, _p, bytes, sc, 0));
3684 }
3685 
3686 /*
3687  * Convert a current locale string to UTF-16BE/LE and copy the result.
3688  * Return -1 if conversion fails.
3689  */
3690 static int
win_strncat_to_utf16(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc,int bigendian)3691 win_strncat_to_utf16(struct archive_string *as16, const void *_p,
3692     size_t length, struct archive_string_conv *sc, int bigendian)
3693 {
3694 	const char *s = (const char *)_p;
3695 	char *u16;
3696 	size_t count, avail;
3697 
3698 	if (archive_string_ensure(as16,
3699 	    as16->length + (length + 1) * 2) == NULL)
3700 		return (-1);
3701 
3702 	u16 = as16->s + as16->length;
3703 	avail = as16->buffer_length - 2;
3704 	if (sc->from_cp == CP_C_LOCALE) {
3705 		/*
3706 		 * "C" locale special processing.
3707 		 */
3708 		count = 0;
3709 		while (count < length && *s) {
3710 			if (bigendian)
3711 				archive_be16enc(u16, *s);
3712 			else
3713 				archive_le16enc(u16, *s);
3714 			u16 += 2;
3715 			s++;
3716 			count++;
3717 		}
3718 		as16->length += count << 1;
3719 		as16->s[as16->length] = 0;
3720 		as16->s[as16->length+1] = 0;
3721 		return (0);
3722 	}
3723 	do {
3724 		int r;
3725 		if (length > (size_t)INT_MAX || (avail >> 1) > (size_t)INT_MAX)
3726 			return (-1);
3727 		r = MultiByteToWideChar(sc->from_cp,
3728 		    MB_PRECOMPOSED, s, (int)length, (LPWSTR)u16, (int)avail>>1);
3729 		/* Exit loop if we succeeded */
3730 		if (r != 0 ||
3731 		    GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3732 			count = (size_t)r;
3733 			break;
3734 		}
3735 		/* Expand buffer and try again */
3736 		r = MultiByteToWideChar(sc->from_cp,
3737 		    MB_PRECOMPOSED, s, (int)length, NULL, 0);
3738 		count = (size_t)r;
3739 		if (archive_string_ensure(as16, (count +1) * 2)
3740 		    == NULL)
3741 			return (-1);
3742 		u16 = as16->s + as16->length;
3743 		avail = as16->buffer_length - 2;
3744 	} while (1);
3745 	as16->length += count * 2;
3746 	as16->s[as16->length] = 0;
3747 	as16->s[as16->length+1] = 0;
3748 	if (count == 0)
3749 		return (-1);
3750 
3751 	if (IS_BIG_ENDIAN) {
3752 		if (!bigendian) {
3753 			while (count > 0) {
3754 				uint16_t v = archive_be16dec(u16);
3755 				archive_le16enc(u16, v);
3756 				u16 += 2;
3757 				count--;
3758 			}
3759 		}
3760 	} else {
3761 		if (bigendian) {
3762 			while (count > 0) {
3763 				uint16_t v = archive_le16dec(u16);
3764 				archive_be16enc(u16, v);
3765 				u16 += 2;
3766 				count--;
3767 			}
3768 		}
3769 	}
3770 	return (0);
3771 }
3772 
3773 static int
win_strncat_to_utf16be(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc)3774 win_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3775     size_t length, struct archive_string_conv *sc)
3776 {
3777 	return (win_strncat_to_utf16(as16, _p, length, sc, 1));
3778 }
3779 
3780 static int
win_strncat_to_utf16le(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc)3781 win_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3782     size_t length, struct archive_string_conv *sc)
3783 {
3784 	return (win_strncat_to_utf16(as16, _p, length, sc, 0));
3785 }
3786 
3787 #endif /* _WIN32 && !__CYGWIN__ */
3788 
3789 /*
3790  * Do the best effort for conversions.
3791  * We cannot handle UTF-16BE character-set without such iconv,
3792  * but there is a chance if a string consists just ASCII code or
3793  * a current locale is UTF-8.
3794  */
3795 
3796 /*
3797  * Convert a UTF-16BE string to current locale and copy the result.
3798  * Return -1 if conversion fails.
3799  */
3800 static int
best_effort_strncat_from_utf16(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc,int be)3801 best_effort_strncat_from_utf16(struct archive_string *as, const void *_p,
3802     size_t bytes, struct archive_string_conv *sc, int be)
3803 {
3804 	const char *utf16 = (const char *)_p;
3805 	char *mbs;
3806 	uint32_t uc;
3807 	int n, ret;
3808 
3809 	(void)sc; /* UNUSED */
3810 	/*
3811 	 * Other case, we should do the best effort.
3812 	 * If all character are ASCII(<0x7f), we can convert it.
3813 	 * if not , we set a alternative character and return -1.
3814 	 */
3815 	ret = 0;
3816 	if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3817 		return (-1);
3818 	mbs = as->s + as->length;
3819 
3820 	while ((n = utf16_to_unicode(&uc, utf16, bytes, be)) != 0) {
3821 		if (n < 0) {
3822 			n *= -1;
3823 			ret =  -1;
3824 		}
3825 		bytes -= n;
3826 		utf16 += n;
3827 
3828 		if (uc > 127) {
3829 			/* We cannot handle it. */
3830 			*mbs++ = '?';
3831 			ret =  -1;
3832 		} else
3833 			*mbs++ = (char)uc;
3834 	}
3835 	as->length = mbs - as->s;
3836 	as->s[as->length] = '\0';
3837 	return (ret);
3838 }
3839 
3840 static int
best_effort_strncat_from_utf16be(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc)3841 best_effort_strncat_from_utf16be(struct archive_string *as, const void *_p,
3842     size_t bytes, struct archive_string_conv *sc)
3843 {
3844 	return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 1));
3845 }
3846 
3847 static int
best_effort_strncat_from_utf16le(struct archive_string * as,const void * _p,size_t bytes,struct archive_string_conv * sc)3848 best_effort_strncat_from_utf16le(struct archive_string *as, const void *_p,
3849     size_t bytes, struct archive_string_conv *sc)
3850 {
3851 	return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 0));
3852 }
3853 
3854 /*
3855  * Convert a current locale string to UTF-16BE/LE and copy the result.
3856  * Return -1 if conversion fails.
3857  */
3858 static int
best_effort_strncat_to_utf16(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc,int bigendian)3859 best_effort_strncat_to_utf16(struct archive_string *as16, const void *_p,
3860     size_t length, struct archive_string_conv *sc, int bigendian)
3861 {
3862 	const char *s = (const char *)_p;
3863 	char *utf16;
3864 	size_t remaining;
3865 	int ret;
3866 
3867 	(void)sc; /* UNUSED */
3868 	/*
3869 	 * Other case, we should do the best effort.
3870 	 * If all character are ASCII(<0x7f), we can convert it.
3871 	 * if not , we set a alternative character and return -1.
3872 	 */
3873 	ret = 0;
3874 	remaining = length;
3875 
3876 	if (archive_string_ensure(as16,
3877 	    as16->length + (length + 1) * 2) == NULL)
3878 		return (-1);
3879 
3880 	utf16 = as16->s + as16->length;
3881 	while (remaining--) {
3882 		unsigned c = *s++;
3883 		if (c > 127) {
3884 			/* We cannot handle it. */
3885 			c = UNICODE_R_CHAR;
3886 			ret = -1;
3887 		}
3888 		if (bigendian)
3889 			archive_be16enc(utf16, (uint16_t)c);
3890 		else
3891 			archive_le16enc(utf16, (uint16_t)c);
3892 		utf16 += 2;
3893 	}
3894 	as16->length = utf16 - as16->s;
3895 	as16->s[as16->length] = 0;
3896 	as16->s[as16->length+1] = 0;
3897 	return (ret);
3898 }
3899 
3900 static int
best_effort_strncat_to_utf16be(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc)3901 best_effort_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3902     size_t length, struct archive_string_conv *sc)
3903 {
3904 	return (best_effort_strncat_to_utf16(as16, _p, length, sc, 1));
3905 }
3906 
3907 static int
best_effort_strncat_to_utf16le(struct archive_string * as16,const void * _p,size_t length,struct archive_string_conv * sc)3908 best_effort_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3909     size_t length, struct archive_string_conv *sc)
3910 {
3911 	return (best_effort_strncat_to_utf16(as16, _p, length, sc, 0));
3912 }
3913 
3914 
3915 /*
3916  * Multistring operations.
3917  */
3918 
3919 void
archive_mstring_clean(struct archive_mstring * aes)3920 archive_mstring_clean(struct archive_mstring *aes)
3921 {
3922 	archive_wstring_free(&(aes->aes_wcs));
3923 	archive_string_free(&(aes->aes_mbs));
3924 	archive_string_free(&(aes->aes_utf8));
3925 	archive_string_free(&(aes->aes_mbs_in_locale));
3926 	aes->aes_set = 0;
3927 }
3928 
3929 void
archive_mstring_copy(struct archive_mstring * dest,struct archive_mstring * src)3930 archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src)
3931 {
3932 	dest->aes_set = src->aes_set;
3933 	archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
3934 	archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
3935 	archive_wstring_copy(&(dest->aes_wcs), &(src->aes_wcs));
3936 }
3937 
3938 int
archive_mstring_get_utf8(struct archive * a,struct archive_mstring * aes,const char ** p)3939 archive_mstring_get_utf8(struct archive *a, struct archive_mstring *aes,
3940   const char **p)
3941 {
3942 	struct archive_string_conv *sc;
3943 	int r;
3944 
3945 	/* If we already have a UTF8 form, return that immediately. */
3946 	if (aes->aes_set & AES_SET_UTF8) {
3947 		*p = aes->aes_utf8.s;
3948 		return (0);
3949 	}
3950 
3951 	*p = NULL;
3952 #if defined(_WIN32) && !defined(__CYGWIN__)
3953 	/*
3954 	 * On Windows, first try converting from WCS because (1) there's no
3955 	 * guarantee that the conversion to MBS will succeed, e.g. when using
3956 	 * CP_ACP, and (2) that's more efficient than converting to MBS, just to
3957 	 * convert back to WCS again before finally converting to UTF-8
3958 	 */
3959 	if ((aes->aes_set & AES_SET_WCS) != 0) {
3960 		sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
3961 		if (sc == NULL)
3962 			return (-1);/* Couldn't allocate memory for sc. */
3963 		archive_string_empty(&(aes->aes_utf8));
3964 		r = archive_string_append_from_wcs_in_codepage(&(aes->aes_utf8),
3965 			aes->aes_wcs.s, aes->aes_wcs.length, sc);
3966 		if (a == NULL)
3967 			free_sconv_object(sc);
3968 		if (r == 0) {
3969 			aes->aes_set |= AES_SET_UTF8;
3970 			*p = aes->aes_utf8.s;
3971 			return (0);/* success. */
3972 		} else
3973 			return (-1);/* failure. */
3974 	}
3975 #endif
3976 	/* Try converting WCS to MBS first if MBS does not exist yet. */
3977 	if ((aes->aes_set & AES_SET_MBS) == 0) {
3978 		const char *pm; /* unused */
3979 		archive_mstring_get_mbs(a, aes, &pm); /* ignore errors, we'll handle it later */
3980 	}
3981 	if (aes->aes_set & AES_SET_MBS) {
3982 		sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
3983 		if (sc == NULL)
3984 			return (-1);/* Couldn't allocate memory for sc. */
3985 		r = archive_strncpy_l(&(aes->aes_utf8), aes->aes_mbs.s,
3986 		    aes->aes_mbs.length, sc);
3987 		if (a == NULL)
3988 			free_sconv_object(sc);
3989 		if (r == 0) {
3990 			aes->aes_set |= AES_SET_UTF8;
3991 			*p = aes->aes_utf8.s;
3992 			return (0);/* success. */
3993 		} else
3994 			return (-1);/* failure. */
3995 	}
3996 	return (0);/* success. */
3997 }
3998 
3999 int
archive_mstring_get_mbs(struct archive * a,struct archive_mstring * aes,const char ** p)4000 archive_mstring_get_mbs(struct archive *a, struct archive_mstring *aes,
4001     const char **p)
4002 {
4003 	struct archive_string_conv *sc;
4004 	int r, ret = 0;
4005 
4006 	/* If we already have an MBS form, return that immediately. */
4007 	if (aes->aes_set & AES_SET_MBS) {
4008 		*p = aes->aes_mbs.s;
4009 		return (ret);
4010 	}
4011 
4012 	*p = NULL;
4013 	/* If there's a WCS form, try converting with the native locale. */
4014 	if (aes->aes_set & AES_SET_WCS) {
4015 		archive_string_empty(&(aes->aes_mbs));
4016 		r = archive_string_append_from_wcs(&(aes->aes_mbs),
4017 		    aes->aes_wcs.s, aes->aes_wcs.length);
4018 		*p = aes->aes_mbs.s;
4019 		if (r == 0) {
4020 			aes->aes_set |= AES_SET_MBS;
4021 			return (ret);
4022 		} else
4023 			ret = -1;
4024 	}
4025 
4026 	/* If there's a UTF-8 form, try converting with the native locale. */
4027 	if (aes->aes_set & AES_SET_UTF8) {
4028 		archive_string_empty(&(aes->aes_mbs));
4029 		sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4030 		if (sc == NULL)
4031 			return (-1);/* Couldn't allocate memory for sc. */
4032 		r = archive_strncpy_l(&(aes->aes_mbs),
4033 			aes->aes_utf8.s, aes->aes_utf8.length, sc);
4034 		if (a == NULL)
4035 			free_sconv_object(sc);
4036 		*p = aes->aes_mbs.s;
4037 		if (r == 0) {
4038 			aes->aes_set |= AES_SET_MBS;
4039 			ret = 0;/* success; overwrite previous error. */
4040 		} else
4041 			ret = -1;/* failure. */
4042 	}
4043 	return (ret);
4044 }
4045 
4046 int
archive_mstring_get_wcs(struct archive * a,struct archive_mstring * aes,const wchar_t ** wp)4047 archive_mstring_get_wcs(struct archive *a, struct archive_mstring *aes,
4048     const wchar_t **wp)
4049 {
4050 	int r, ret = 0;
4051 
4052 	(void)a;/* UNUSED */
4053 	/* Return WCS form if we already have it. */
4054 	if (aes->aes_set & AES_SET_WCS) {
4055 		*wp = aes->aes_wcs.s;
4056 		return (ret);
4057 	}
4058 
4059 	*wp = NULL;
4060 #if defined(_WIN32) && !defined(__CYGWIN__)
4061 	/*
4062 	 * On Windows, prefer converting from UTF-8 directly to WCS because:
4063 	 * (1) there's no guarantee that the string can be represented in MBS (e.g.
4064 	 * with CP_ACP), and (2) in order to convert from UTF-8 to MBS, we're going
4065 	 * to need to convert from UTF-8 to WCS anyway and its wasteful to throw
4066 	 * away that intermediate result
4067 	 */
4068 	if (aes->aes_set & AES_SET_UTF8) {
4069 		struct archive_string_conv *sc;
4070 
4071 		sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4072 		if (sc != NULL) {
4073 			archive_wstring_empty((&aes->aes_wcs));
4074 			r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4075 			    aes->aes_utf8.s, aes->aes_utf8.length, sc);
4076 			if (a == NULL)
4077 				free_sconv_object(sc);
4078 			if (r == 0) {
4079 				aes->aes_set |= AES_SET_WCS;
4080 				*wp = aes->aes_wcs.s;
4081 				return (0);
4082 			}
4083 		}
4084 	}
4085 #endif
4086 	/* Try converting UTF8 to MBS first if MBS does not exist yet. */
4087 	if ((aes->aes_set & AES_SET_MBS) == 0) {
4088 		const char *p; /* unused */
4089 		archive_mstring_get_mbs(a, aes, &p); /* ignore errors, we'll handle it later */
4090 	}
4091 	/* Try converting MBS to WCS using native locale. */
4092 	if (aes->aes_set & AES_SET_MBS) {
4093 		archive_wstring_empty(&(aes->aes_wcs));
4094 		r = archive_wstring_append_from_mbs(&(aes->aes_wcs),
4095 		    aes->aes_mbs.s, aes->aes_mbs.length);
4096 		if (r == 0) {
4097 			aes->aes_set |= AES_SET_WCS;
4098 			*wp = aes->aes_wcs.s;
4099 		} else
4100 			ret = -1;/* failure. */
4101 	}
4102 	return (ret);
4103 }
4104 
4105 int
archive_mstring_get_mbs_l(struct archive * a,struct archive_mstring * aes,const char ** p,size_t * length,struct archive_string_conv * sc)4106 archive_mstring_get_mbs_l(struct archive *a, struct archive_mstring *aes,
4107     const char **p, size_t *length, struct archive_string_conv *sc)
4108 {
4109 	int ret = 0;
4110 #if defined(_WIN32) && !defined(__CYGWIN__)
4111 	int r;
4112 
4113 	/*
4114 	 * Internationalization programming on Windows must use Wide
4115 	 * characters because Windows platform cannot make locale UTF-8.
4116 	 */
4117 	if (sc != NULL && (aes->aes_set & AES_SET_WCS) != 0) {
4118 		archive_string_empty(&(aes->aes_mbs_in_locale));
4119 		r = archive_string_append_from_wcs_in_codepage(
4120 		    &(aes->aes_mbs_in_locale), aes->aes_wcs.s,
4121 		    aes->aes_wcs.length, sc);
4122 		if (r == 0) {
4123 			*p = aes->aes_mbs_in_locale.s;
4124 			if (length != NULL)
4125 				*length = aes->aes_mbs_in_locale.length;
4126 			return (0);
4127 		} else if (errno == ENOMEM)
4128 			return (-1);
4129 		else
4130 			ret = -1;
4131 	}
4132 #endif
4133 
4134 	/* If there is not an MBS form but there is a WCS or UTF8 form, try converting
4135 	 * with the native locale to be used for translating it to specified
4136 	 * character-set. */
4137 	if ((aes->aes_set & AES_SET_MBS) == 0) {
4138 		const char *pm; /* unused */
4139 		if (archive_mstring_get_mbs(a, aes, &pm) != 0) {
4140 			/* We have another form, but failed to convert it to
4141 			 * the native locale.  Transitively, we've failed to
4142 			 * convert it to the specified character set. */
4143 			ret = -1;
4144 		}
4145 	}
4146 	/* If we already have an MBS form, use it to be translated to
4147 	 * specified character-set. */
4148 	if (aes->aes_set & AES_SET_MBS) {
4149 		if (sc == NULL) {
4150 			/* Conversion is unneeded. */
4151 			*p = aes->aes_mbs.s;
4152 			if (length != NULL)
4153 				*length = aes->aes_mbs.length;
4154 			return (0);
4155 		}
4156 		ret = archive_strncpy_l(&(aes->aes_mbs_in_locale),
4157 		    aes->aes_mbs.s, aes->aes_mbs.length, sc);
4158 		*p = aes->aes_mbs_in_locale.s;
4159 		if (length != NULL)
4160 			*length = aes->aes_mbs_in_locale.length;
4161 	} else {
4162 		/* Either we have no string in any form,
4163 		 * or conversion failed and set 'ret != 0'.  */
4164 		*p = NULL;
4165 		if (length != NULL)
4166 			*length = 0;
4167 	}
4168 	return (ret);
4169 }
4170 
4171 int
archive_mstring_copy_mbs(struct archive_mstring * aes,const char * mbs)4172 archive_mstring_copy_mbs(struct archive_mstring *aes, const char *mbs)
4173 {
4174 	if (mbs == NULL) {
4175 		aes->aes_set = 0;
4176 		return (0);
4177 	}
4178 	return (archive_mstring_copy_mbs_len(aes, mbs, strlen(mbs)));
4179 }
4180 
4181 int
archive_mstring_copy_mbs_len(struct archive_mstring * aes,const char * mbs,size_t len)4182 archive_mstring_copy_mbs_len(struct archive_mstring *aes, const char *mbs,
4183     size_t len)
4184 {
4185 	if (mbs == NULL) {
4186 		aes->aes_set = 0;
4187 		return (0);
4188 	}
4189 	aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4190 	archive_strncpy(&(aes->aes_mbs), mbs, len);
4191 	archive_string_empty(&(aes->aes_utf8));
4192 	archive_wstring_empty(&(aes->aes_wcs));
4193 	return (0);
4194 }
4195 
4196 int
archive_mstring_copy_wcs(struct archive_mstring * aes,const wchar_t * wcs)4197 archive_mstring_copy_wcs(struct archive_mstring *aes, const wchar_t *wcs)
4198 {
4199 	return archive_mstring_copy_wcs_len(aes, wcs,
4200 				wcs == NULL ? 0 : wcslen(wcs));
4201 }
4202 
4203 int
archive_mstring_copy_utf8(struct archive_mstring * aes,const char * utf8)4204 archive_mstring_copy_utf8(struct archive_mstring *aes, const char *utf8)
4205 {
4206   if (utf8 == NULL) {
4207     aes->aes_set = 0;
4208     return (0);
4209   }
4210   aes->aes_set = AES_SET_UTF8;
4211   archive_string_empty(&(aes->aes_mbs));
4212   archive_string_empty(&(aes->aes_wcs));
4213   archive_strncpy(&(aes->aes_utf8), utf8, strlen(utf8));
4214   return (int)strlen(utf8);
4215 }
4216 
4217 int
archive_mstring_copy_wcs_len(struct archive_mstring * aes,const wchar_t * wcs,size_t len)4218 archive_mstring_copy_wcs_len(struct archive_mstring *aes, const wchar_t *wcs,
4219     size_t len)
4220 {
4221 	if (wcs == NULL) {
4222 		aes->aes_set = 0;
4223 		return (0);
4224 	}
4225 	aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
4226 	archive_string_empty(&(aes->aes_mbs));
4227 	archive_string_empty(&(aes->aes_utf8));
4228 	archive_wstrncpy(&(aes->aes_wcs), wcs, len);
4229 	return (0);
4230 }
4231 
4232 int
archive_mstring_copy_mbs_len_l(struct archive_mstring * aes,const char * mbs,size_t len,struct archive_string_conv * sc)4233 archive_mstring_copy_mbs_len_l(struct archive_mstring *aes,
4234     const char *mbs, size_t len, struct archive_string_conv *sc)
4235 {
4236 	int r;
4237 
4238 	if (mbs == NULL) {
4239 		aes->aes_set = 0;
4240 		return (0);
4241 	}
4242 	archive_string_empty(&(aes->aes_mbs));
4243 	archive_wstring_empty(&(aes->aes_wcs));
4244 	archive_string_empty(&(aes->aes_utf8));
4245 #if defined(_WIN32) && !defined(__CYGWIN__)
4246 	/*
4247 	 * Internationalization programming on Windows must use Wide
4248 	 * characters because Windows platform cannot make locale UTF-8.
4249 	 */
4250 	if (sc == NULL) {
4251 		if (archive_string_append(&(aes->aes_mbs),
4252 			mbs, mbsnbytes(mbs, len)) == NULL) {
4253 			aes->aes_set = 0;
4254 			r = -1;
4255 		} else {
4256 			aes->aes_set = AES_SET_MBS;
4257 			r = 0;
4258 		}
4259 #if defined(HAVE_ICONV)
4260 	} else if (sc != NULL && sc->cd_w != (iconv_t)-1) {
4261 		/*
4262 		 * This case happens only when MultiByteToWideChar() cannot
4263 		 * handle sc->from_cp, and we have to iconv in order to
4264 		 * translate character-set to wchar_t,UTF-16.
4265 		 */
4266 		iconv_t cd = sc->cd;
4267 		unsigned from_cp;
4268 		int flag;
4269 
4270 		/*
4271 		 * Translate multi-bytes from some character-set to UTF-8.
4272 		 */
4273 		sc->cd = sc->cd_w;
4274 		r = archive_strncpy_l(&(aes->aes_utf8), mbs, len, sc);
4275 		sc->cd = cd;
4276 		if (r != 0) {
4277 			aes->aes_set = 0;
4278 			return (r);
4279 		}
4280 		aes->aes_set = AES_SET_UTF8;
4281 
4282 		/*
4283 		 * Append the UTF-8 string into wstring.
4284 		 */
4285 		flag = sc->flag;
4286 		sc->flag &= ~(SCONV_NORMALIZATION_C
4287 				| SCONV_TO_UTF16| SCONV_FROM_UTF16);
4288 		from_cp = sc->from_cp;
4289 		sc->from_cp = CP_UTF8;
4290 		r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4291 			aes->aes_utf8.s, aes->aes_utf8.length, sc);
4292 		sc->flag = flag;
4293 		sc->from_cp = from_cp;
4294 		if (r == 0)
4295 			aes->aes_set |= AES_SET_WCS;
4296 #endif
4297 	} else {
4298 		r = archive_wstring_append_from_mbs_in_codepage(
4299 		    &(aes->aes_wcs), mbs, len, sc);
4300 		if (r == 0)
4301 			aes->aes_set = AES_SET_WCS;
4302 		else
4303 			aes->aes_set = 0;
4304 	}
4305 #else
4306 	r = archive_strncpy_l(&(aes->aes_mbs), mbs, len, sc);
4307 	if (r == 0)
4308 		aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4309 	else
4310 		aes->aes_set = 0;
4311 #endif
4312 	return (r);
4313 }
4314 
4315 /*
4316  * The 'update' form tries to proactively update all forms of
4317  * this string (WCS and MBS) and returns an error if any of
4318  * them fail.  This is used by the 'pax' handler, for instance,
4319  * to detect and report character-conversion failures early while
4320  * still allowing clients to get potentially useful values from
4321  * the more tolerant lazy conversions.  (get_mbs and get_wcs will
4322  * strive to give the user something useful, so you can get hopefully
4323  * usable values even if some of the character conversions are failing.)
4324  */
4325 int
archive_mstring_update_utf8(struct archive * a,struct archive_mstring * aes,const char * utf8)4326 archive_mstring_update_utf8(struct archive *a, struct archive_mstring *aes,
4327     const char *utf8)
4328 {
4329 	struct archive_string_conv *sc;
4330 	int r;
4331 
4332 	if (utf8 == NULL) {
4333 		aes->aes_set = 0;
4334 		return (0); /* Succeeded in clearing everything. */
4335 	}
4336 
4337 	/* Save the UTF8 string. */
4338 	archive_strcpy(&(aes->aes_utf8), utf8);
4339 
4340 	/* Empty the mbs and wcs strings. */
4341 	archive_string_empty(&(aes->aes_mbs));
4342 	archive_wstring_empty(&(aes->aes_wcs));
4343 
4344 	aes->aes_set = AES_SET_UTF8;	/* Only UTF8 is set now. */
4345 
4346 	sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4347 	if (sc == NULL)
4348 		return (-1);/* Couldn't allocate memory for sc. */
4349 
4350 #if defined(_WIN32) && !defined(__CYGWIN__)
4351 	/* On Windows, there's no good way to convert from UTF8 -> MBS directly, so
4352 	 * prefer to first convert to WCS as (1) it's wasteful to throw away the
4353 	 * intermediate result, and (2) WCS will still be set even if we fail to
4354 	 * convert to MBS (e.g. with ACP that can't represent the characters) */
4355 	r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4356 		aes->aes_utf8.s, aes->aes_utf8.length, sc);
4357 
4358 	if (a == NULL)
4359 		free_sconv_object(sc);
4360 	if (r != 0)
4361 		return (-1); /* This will guarantee we can't convert to MBS */
4362 	aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
4363 
4364 	/* Try converting WCS to MBS, return false on failure. */
4365 	if (archive_string_append_from_wcs(&(aes->aes_mbs), aes->aes_wcs.s,
4366 	    aes->aes_wcs.length))
4367 		return (-1);
4368 #else
4369 	/* Try converting UTF-8 to MBS, return false on failure. */
4370 	r = archive_strcpy_l(&(aes->aes_mbs), utf8, sc);
4371 
4372 	if (a == NULL)
4373 		free_sconv_object(sc);
4374 	if (r != 0)
4375 		return (-1);
4376 	aes->aes_set = AES_SET_UTF8 | AES_SET_MBS; /* Both UTF8 and MBS set. */
4377 
4378 	/* Try converting MBS to WCS, return false on failure. */
4379 	if (archive_wstring_append_from_mbs(&(aes->aes_wcs), aes->aes_mbs.s,
4380 	    aes->aes_mbs.length))
4381 		return (-1);
4382 #endif
4383 
4384 	/* All conversions succeeded. */
4385 	aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
4386 
4387 	return (0);
4388 }
4389