2 * Copyright (c) 2003-2011 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
31 * Basic resizable string support, to simplify manipulating arbitrary-sized
32 * strings while minimizing heap activity.
34 * In particular, the buffer used by a string object is only grown, it
35 * never shrinks, so you can clear and reuse the same string object
36 * without incurring additional memory allocations.
45 #ifdef HAVE_LANGINFO_H
48 #ifdef HAVE_LOCALCHARSET_H
49 #include <localcharset.h>
60 #if defined(_WIN32) && !defined(__CYGWIN__)
65 #include "archive_endian.h"
66 #include "archive_private.h"
67 #include "archive_string.h"
68 #include "archive_string_composition.h"
70 #if !defined(HAVE_WMEMCPY) && !defined(wmemcpy)
71 #define wmemcpy(a,b,i) (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
74 #if !defined(HAVE_WMEMMOVE) && !defined(wmemmove)
75 #define wmemmove(a,b,i) (wchar_t *)memmove((a), (b), (i) * sizeof(wchar_t))
79 #define max(a, b) ((a)>(b)?(a):(b))
81 struct archive_string_conv {
82 struct archive_string_conv *next;
87 /* Set 1 if from_charset and to_charset are the same. */
90 #define SCONV_TO_CHARSET 1 /* MBS is being converted to specified
92 #define SCONV_FROM_CHARSET (1<<1) /* MBS is being converted from
93 * specified charset. */
94 #define SCONV_BEST_EFFORT (1<<2) /* Copy at least ASCII code. */
95 #define SCONV_WIN_CP (1<<3) /* Use Windows API for converting
97 #define SCONV_UTF8_LIBARCHIVE_2 (1<<4) /* Incorrect UTF-8 made by libarchive
98 * 2.x in the wrong assumption. */
99 #define SCONV_NORMALIZATION_C (1<<6) /* Need normalization to be Form C.
100 * Before UTF-8 characters are actually
102 #define SCONV_NORMALIZATION_D (1<<7) /* Need normalization to be Form D.
103 * Before UTF-8 characters are actually
105 * Currently this only for MAC OS X. */
106 #define SCONV_TO_UTF8 (1<<8) /* "to charset" side is UTF-8. */
107 #define SCONV_FROM_UTF8 (1<<9) /* "from charset" side is UTF-8. */
108 #define SCONV_TO_UTF16BE (1<<10) /* "to charset" side is UTF-16BE. */
109 #define SCONV_FROM_UTF16BE (1<<11) /* "from charset" side is UTF-16BE. */
110 #define SCONV_TO_UTF16LE (1<<12) /* "to charset" side is UTF-16LE. */
111 #define SCONV_FROM_UTF16LE (1<<13) /* "from charset" side is UTF-16LE. */
112 #define SCONV_TO_UTF16 (SCONV_TO_UTF16BE | SCONV_TO_UTF16LE)
113 #define SCONV_FROM_UTF16 (SCONV_FROM_UTF16BE | SCONV_FROM_UTF16LE)
117 iconv_t cd_w;/* Use at archive_mstring on
120 /* A temporary buffer for normalization. */
121 struct archive_string utftmp;
122 int (*converter[2])(struct archive_string *, const void *, size_t,
123 struct archive_string_conv *);
127 #define CP_C_LOCALE 0 /* "C" locale only for this file. */
128 #define CP_UTF16LE 1200
129 #define CP_UTF16BE 1201
131 #define IS_HIGH_SURROGATE_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDBFF)
132 #define IS_LOW_SURROGATE_LA(uc) ((uc) >= 0xDC00 && (uc) <= 0xDFFF)
133 #define IS_SURROGATE_PAIR_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDFFF)
134 #define UNICODE_MAX 0x10FFFF
135 #define UNICODE_R_CHAR 0xFFFD /* Replacement character. */
136 /* Set U+FFFD(Replacement character) in UTF-8. */
137 static const char utf8_replacement_char[] = {0xef, 0xbf, 0xbd};
139 static struct archive_string_conv *find_sconv_object(struct archive *,
140 const char *, const char *);
141 static void add_sconv_object(struct archive *, struct archive_string_conv *);
142 static struct archive_string_conv *create_sconv_object(const char *,
143 const char *, unsigned, int);
144 static void free_sconv_object(struct archive_string_conv *);
145 static struct archive_string_conv *get_sconv_object(struct archive *,
146 const char *, const char *, int);
147 static unsigned make_codepage_from_charset(const char *);
148 static unsigned get_current_codepage(void);
149 static unsigned get_current_oemcp(void);
150 static size_t mbsnbytes(const void *, size_t);
151 static size_t utf16nbytes(const void *, size_t);
152 #if defined(_WIN32) && !defined(__CYGWIN__)
153 static int archive_wstring_append_from_mbs_in_codepage(
154 struct archive_wstring *, const char *, size_t,
155 struct archive_string_conv *);
156 static int archive_string_append_from_wcs_in_codepage(struct archive_string *,
157 const wchar_t *, size_t, struct archive_string_conv *);
158 static int is_big_endian(void);
159 static int strncat_in_codepage(struct archive_string *, const void *,
160 size_t, struct archive_string_conv *);
161 static int win_strncat_from_utf16be(struct archive_string *, const void *,
162 size_t, struct archive_string_conv *);
163 static int win_strncat_from_utf16le(struct archive_string *, const void *,
164 size_t, struct archive_string_conv *);
165 static int win_strncat_to_utf16be(struct archive_string *, const void *,
166 size_t, struct archive_string_conv *);
167 static int win_strncat_to_utf16le(struct archive_string *, const void *,
168 size_t, struct archive_string_conv *);
170 static int best_effort_strncat_from_utf16be(struct archive_string *,
171 const void *, size_t, struct archive_string_conv *);
172 static int best_effort_strncat_from_utf16le(struct archive_string *,
173 const void *, size_t, struct archive_string_conv *);
174 static int best_effort_strncat_to_utf16be(struct archive_string *,
175 const void *, size_t, struct archive_string_conv *);
176 static int best_effort_strncat_to_utf16le(struct archive_string *,
177 const void *, size_t, struct archive_string_conv *);
178 #if defined(HAVE_ICONV)
179 static int iconv_strncat_in_locale(struct archive_string *, const void *,
180 size_t, struct archive_string_conv *);
182 static int best_effort_strncat_in_locale(struct archive_string *,
183 const void *, size_t, struct archive_string_conv *);
184 static int _utf8_to_unicode(uint32_t *, const char *, size_t);
185 static int utf8_to_unicode(uint32_t *, const char *, size_t);
186 static inline uint32_t combine_surrogate_pair(uint32_t, uint32_t);
187 static int cesu8_to_unicode(uint32_t *, const char *, size_t);
188 static size_t unicode_to_utf8(char *, size_t, uint32_t);
189 static int utf16_to_unicode(uint32_t *, const char *, size_t, int);
190 static size_t unicode_to_utf16be(char *, size_t, uint32_t);
191 static size_t unicode_to_utf16le(char *, size_t, uint32_t);
192 static int strncat_from_utf8_libarchive2(struct archive_string *,
193 const void *, size_t, struct archive_string_conv *);
194 static int strncat_from_utf8_to_utf8(struct archive_string *, const void *,
195 size_t, struct archive_string_conv *);
196 static int archive_string_normalize_C(struct archive_string *, const void *,
197 size_t, struct archive_string_conv *);
198 static int archive_string_normalize_D(struct archive_string *, const void *,
199 size_t, struct archive_string_conv *);
200 static int archive_string_append_unicode(struct archive_string *,
201 const void *, size_t, struct archive_string_conv *);
203 static struct archive_string *
204 archive_string_append(struct archive_string *as, const char *p, size_t s)
206 if (archive_string_ensure(as, as->length + s + 1) == NULL)
209 memmove(as->s + as->length, p, s);
211 as->s[as->length] = 0;
215 static struct archive_wstring *
216 archive_wstring_append(struct archive_wstring *as, const wchar_t *p, size_t s)
218 if (archive_wstring_ensure(as, as->length + s + 1) == NULL)
221 wmemmove(as->s + as->length, p, s);
223 as->s[as->length] = 0;
227 struct archive_string *
228 archive_array_append(struct archive_string *as, const char *p, size_t s)
230 return archive_string_append(as, p, s);
234 archive_string_concat(struct archive_string *dest, struct archive_string *src)
236 if (archive_string_append(dest, src->s, src->length) == NULL)
237 __archive_errx(1, "Out of memory");
241 archive_wstring_concat(struct archive_wstring *dest,
242 struct archive_wstring *src)
244 if (archive_wstring_append(dest, src->s, src->length) == NULL)
245 __archive_errx(1, "Out of memory");
249 archive_string_free(struct archive_string *as)
252 as->buffer_length = 0;
258 archive_wstring_free(struct archive_wstring *as)
261 as->buffer_length = 0;
266 struct archive_wstring *
267 archive_wstring_ensure(struct archive_wstring *as, size_t s)
269 return (struct archive_wstring *)
270 archive_string_ensure((struct archive_string *)as,
271 s * sizeof(wchar_t));
274 /* Returns NULL on any allocation failure. */
275 struct archive_string *
276 archive_string_ensure(struct archive_string *as, size_t s)
281 /* If buffer is already big enough, don't reallocate. */
282 if (as->s && (s <= as->buffer_length))
286 * Growing the buffer at least exponentially ensures that
287 * append operations are always linear in the number of
288 * characters appended. Using a smaller growth rate for
289 * larger buffers reduces memory waste somewhat at the cost of
290 * a larger constant factor.
292 if (as->buffer_length < 32)
293 /* Start with a minimum 32-character buffer. */
295 else if (as->buffer_length < 8192)
296 /* Buffers under 8k are doubled for speed. */
297 new_length = as->buffer_length + as->buffer_length;
299 /* Buffers 8k and over grow by at least 25% each time. */
300 new_length = as->buffer_length + as->buffer_length / 4;
301 /* Be safe: If size wraps, fail. */
302 if (new_length < as->buffer_length) {
303 /* On failure, wipe the string and return NULL. */
304 archive_string_free(as);
305 errno = ENOMEM;/* Make sure errno has ENOMEM. */
310 * The computation above is a lower limit to how much we'll
311 * grow the buffer. In any case, we have to grow it enough to
316 /* Now we can reallocate the buffer. */
317 p = (char *)realloc(as->s, new_length);
319 /* On failure, wipe the string and return NULL. */
320 archive_string_free(as);
321 errno = ENOMEM;/* Make sure errno has ENOMEM. */
326 as->buffer_length = new_length;
331 * TODO: See if there's a way to avoid scanning
332 * the source string twice. Then test to see
333 * if it actually helps (remember that we're almost
334 * always called with pretty short arguments, so
335 * such an optimization might not help).
337 struct archive_string *
338 archive_strncat(struct archive_string *as, const void *_p, size_t n)
343 p = (const char *)_p;
345 /* Like strlen(p), except won't examine positions beyond p[n]. */
348 while (s < n && *pp) {
352 if ((as = archive_string_append(as, p, s)) == NULL)
353 __archive_errx(1, "Out of memory");
357 struct archive_wstring *
358 archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
363 /* Like strlen(p), except won't examine positions beyond p[n]. */
366 while (s < n && *pp) {
370 if ((as = archive_wstring_append(as, p, s)) == NULL)
371 __archive_errx(1, "Out of memory");
375 struct archive_string *
376 archive_strcat(struct archive_string *as, const void *p)
378 /* strcat is just strncat without an effective limit.
379 * Assert that we'll never get called with a source
381 * TODO: Review all uses of strcat in the source
382 * and try to replace them with strncat().
384 return archive_strncat(as, p, 0x1000000);
387 struct archive_wstring *
388 archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
391 return archive_wstrncat(as, p, 0x1000000);
394 struct archive_string *
395 archive_strappend_char(struct archive_string *as, char c)
397 if ((as = archive_string_append(as, &c, 1)) == NULL)
398 __archive_errx(1, "Out of memory");
402 struct archive_wstring *
403 archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
405 if ((as = archive_wstring_append(as, &c, 1)) == NULL)
406 __archive_errx(1, "Out of memory");
411 * Get the "current character set" name to use with iconv.
412 * On FreeBSD, the empty character set name "" chooses
413 * the correct character encoding for the current locale,
414 * so this isn't necessary.
415 * But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
416 * on that system, we have to explicitly call nl_langinfo()
417 * to get the right name. Not sure about other platforms.
419 * NOTE: GNU libiconv does not recognize the character-set name
420 * which some platform nl_langinfo(CODESET) returns, so we should
421 * use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
424 default_iconv_charset(const char *charset) {
425 if (charset != NULL && charset[0] != '\0')
427 #if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
428 /* locale_charset() is broken on Mac OS */
429 return locale_charset();
430 #elif HAVE_NL_LANGINFO
431 return nl_langinfo(CODESET);
437 #if defined(_WIN32) && !defined(__CYGWIN__)
440 * Convert MBS to WCS.
441 * Note: returns -1 if conversion fails.
444 archive_wstring_append_from_mbs(struct archive_wstring *dest,
445 const char *p, size_t len)
447 return archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
451 archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
452 const char *s, size_t length, struct archive_string_conv *sc)
458 from_cp = sc->from_cp;
460 from_cp = get_current_codepage();
462 if (from_cp == CP_C_LOCALE) {
464 * "C" locale special processing.
467 const unsigned char *mp;
469 if (NULL == archive_wstring_ensure(dest,
470 dest->length + length + 1))
473 ws = dest->s + dest->length;
474 mp = (const unsigned char *)s;
476 while (count < (int)length && *mp) {
477 *ws++ = (wchar_t)*mp++;
480 } else if (sc != NULL &&
481 (sc->flag & (SCONV_NORMALIZATION_C | SCONV_NORMALIZATION_D))) {
483 * Normalize UTF-8 and UTF-16BE and convert it directly
484 * to UTF-16 as wchar_t.
486 struct archive_string u16;
487 int saved_flag = sc->flag;/* save current flag. */
490 sc->flag |= SCONV_TO_UTF16BE;
492 sc->flag |= SCONV_TO_UTF16LE;
494 if (sc->flag & SCONV_FROM_UTF16) {
496 * UTF-16BE/LE NFD ===> UTF-16 NFC
497 * UTF-16BE/LE NFC ===> UTF-16 NFD
499 count = (int)utf16nbytes(s, length);
502 * UTF-8 NFD ===> UTF-16 NFC
503 * UTF-8 NFC ===> UTF-16 NFD
505 count = (int)mbsnbytes(s, length);
507 u16.s = (char *)dest->s;
508 u16.length = dest->length << 1;;
509 u16.buffer_length = dest->buffer_length;
510 if (sc->flag & SCONV_NORMALIZATION_C)
511 ret = archive_string_normalize_C(&u16, s, count, sc);
513 ret = archive_string_normalize_D(&u16, s, count, sc);
514 dest->s = (wchar_t *)u16.s;
515 dest->length = u16.length >> 1;
516 dest->buffer_length = u16.buffer_length;
517 sc->flag = saved_flag;/* restore the saved flag. */
519 } else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
520 count = (int)utf16nbytes(s, length);
521 count >>= 1; /* to be WCS length */
522 /* Allocate memory for WCS. */
523 if (NULL == archive_wstring_ensure(dest,
524 dest->length + count + 1))
526 wmemcpy(dest->s + dest->length, (const wchar_t *)s, count);
527 if ((sc->flag & SCONV_FROM_UTF16BE) && !is_big_endian()) {
528 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
530 for (b = 0; b < count; b++) {
531 uint16_t val = archive_le16dec(u16+b);
532 archive_be16enc(u16+b, val);
534 } else if ((sc->flag & SCONV_FROM_UTF16LE) && is_big_endian()) {
535 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
537 for (b = 0; b < count; b++) {
538 uint16_t val = archive_be16dec(u16+b);
539 archive_le16enc(u16+b, val);
548 else if (sc->flag & SCONV_FROM_CHARSET) {
549 /* Do not trust the length which comes from
550 * an archive file. */
551 length = mbsnbytes(s, length);
554 mbflag = MB_PRECOMPOSED;
556 buffsize = dest->length + length + 1;
558 /* Allocate memory for WCS. */
559 if (NULL == archive_wstring_ensure(dest, buffsize))
561 /* Convert MBS to WCS. */
562 count = MultiByteToWideChar(from_cp,
563 mbflag, s, (int)length, dest->s + dest->length,
564 (int)(dest->buffer_length >> 1) -1);
566 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
567 /* Expand the WCS buffer. */
568 buffsize = dest->buffer_length << 1;
571 if (count == 0 && length != 0)
576 dest->length += count;
577 dest->s[dest->length] = L'\0';
584 * Convert MBS to WCS.
585 * Note: returns -1 if conversion fails.
588 archive_wstring_append_from_mbs(struct archive_wstring *dest,
589 const char *p, size_t len)
594 * No single byte will be more than one wide character,
595 * so this length estimate will always be big enough.
597 // size_t wcs_length = len;
598 size_t mbs_length = len;
602 mbstate_t shift_state;
604 memset(&shift_state, 0, sizeof(shift_state));
607 * As we decided to have wcs_length == mbs_length == len
608 * we can use len here instead of wcs_length
610 if (NULL == archive_wstring_ensure(dest, dest->length + len + 1))
612 wcs = dest->s + dest->length;
614 * We cannot use mbsrtowcs/mbstowcs here because those may convert
615 * extra MBS when strlen(p) > len and one wide character consists of
618 while (*mbs && mbs_length > 0) {
620 * The buffer we allocated is always big enough.
621 * Keep this code path in a comment if we decide to choose
622 * smaller wcs_length in the future
625 if (wcs_length == 0) {
626 dest->length = wcs - dest->s;
627 dest->s[dest->length] = L'\0';
628 wcs_length = mbs_length;
629 if (NULL == archive_wstring_ensure(dest,
630 dest->length + wcs_length + 1))
632 wcs = dest->s + dest->length;
636 r = mbrtowc(wcs, mbs, mbs_length, &shift_state);
638 r = mbtowc(wcs, mbs, mbs_length);
640 if (r == (size_t)-1 || r == (size_t)-2) {
644 if (r == 0 || r > mbs_length)
651 dest->length = wcs - dest->s;
652 dest->s[dest->length] = L'\0';
658 #if defined(_WIN32) && !defined(__CYGWIN__)
662 * Note: returns -1 if conversion fails.
664 * Win32 builds use WideCharToMultiByte from the Windows API.
665 * (Maybe Cygwin should too? WideCharToMultiByte will know a
666 * lot more about local character encodings than the wcrtomb()
667 * wrapper is going to know.)
670 archive_string_append_from_wcs(struct archive_string *as,
671 const wchar_t *w, size_t len)
673 return archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
677 archive_string_append_from_wcs_in_codepage(struct archive_string *as,
678 const wchar_t *ws, size_t len, struct archive_string_conv *sc)
680 BOOL defchar_used, *dp;
683 int wslen = (int)len;
688 to_cp = get_current_codepage();
690 if (to_cp == CP_C_LOCALE) {
692 * "C" locale special processing.
694 const wchar_t *wp = ws;
697 if (NULL == archive_string_ensure(as,
698 as->length + wslen +1))
700 p = as->s + as->length;
703 while (count < wslen && *wp) {
712 } else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
716 archive_string_ensure(as, as->length + len * 2 + 2))
718 u16 = (uint16_t *)(as->s + as->length);
721 if (sc->flag & SCONV_TO_UTF16BE) {
722 while (count < (int)len && *ws) {
723 archive_be16enc(u16+count, *ws);
728 while (count < (int)len && *ws) {
729 archive_le16enc(u16+count, *ws);
734 count <<= 1; /* to be byte size */
736 /* Make sure the MBS buffer has plenty to set. */
738 archive_string_ensure(as, as->length + len * 2 + 1))
742 if (to_cp == CP_UTF8 || sc == NULL)
746 count = WideCharToMultiByte(to_cp, 0, ws, wslen,
748 (int)as->buffer_length - as->length - 1, NULL, dp);
750 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
751 /* Expand the MBS buffer and retry. */
752 if (NULL == archive_string_ensure(as,
753 as->buffer_length + len))
763 as->s[as->length] = '\0';
764 return (defchar_used?-1:ret);
767 #elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
770 * Translates a wide character string into current locale character set
771 * and appends to the archive_string. Note: returns -1 if conversion
775 archive_string_append_from_wcs(struct archive_string *as,
776 const wchar_t *w, size_t len)
778 /* We cannot use the standard wcstombs() here because it
779 * cannot tell us how big the output buffer should be. So
780 * I've built a loop around wcrtomb() or wctomb() that
781 * converts a character at a time and resizes the string as
782 * needed. We prefer wcrtomb() when it's available because
783 * it's thread-safe. */
788 mbstate_t shift_state;
790 memset(&shift_state, 0, sizeof(shift_state));
792 /* Clear the shift state before starting. */
796 * Allocate buffer for MBS.
797 * We need this allocation here since it is possible that
798 * as->s is still NULL.
800 if (archive_string_ensure(as, as->length + len + 1) == NULL)
803 p = as->s + as->length;
804 end = as->s + as->buffer_length - MB_CUR_MAX -1;
805 while (*w != L'\0' && len > 0) {
807 as->length = p - as->s;
808 as->s[as->length] = '\0';
809 /* Re-allocate buffer for MBS. */
810 if (archive_string_ensure(as,
811 as->length + max(len * 2,
812 (size_t)MB_CUR_MAX) + 1) == NULL)
814 p = as->s + as->length;
815 end = as->s + as->buffer_length - MB_CUR_MAX -1;
818 n = wcrtomb(p, *w++, &shift_state);
823 if (errno == EILSEQ) {
824 /* Skip an illegal wide char. */
835 as->length = p - as->s;
836 as->s[as->length] = '\0';
840 #else /* HAVE_WCTOMB || HAVE_WCRTOMB */
843 * TODO: Test if __STDC_ISO_10646__ is defined.
844 * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
845 * one character at a time. If a non-Windows platform doesn't have
846 * either of these, fall back to the built-in UTF8 conversion.
849 archive_string_append_from_wcs(struct archive_string *as,
850 const wchar_t *w, size_t len)
852 (void)as;/* UNUSED */
854 (void)len;/* UNUSED */
859 #endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
862 * Find a string conversion object by a pair of 'from' charset name
863 * and 'to' charset name from an archive object.
864 * Return NULL if not found.
866 static struct archive_string_conv *
867 find_sconv_object(struct archive *a, const char *fc, const char *tc)
869 struct archive_string_conv *sc;
874 for (sc = a->sconv; sc != NULL; sc = sc->next) {
875 if (strcmp(sc->from_charset, fc) == 0 &&
876 strcmp(sc->to_charset, tc) == 0)
883 * Register a string object to an archive object.
886 add_sconv_object(struct archive *a, struct archive_string_conv *sc)
888 struct archive_string_conv **psc;
890 /* Add a new sconv to sconv list. */
893 psc = &((*psc)->next);
898 add_converter(struct archive_string_conv *sc, int (*converter)
899 (struct archive_string *, const void *, size_t,
900 struct archive_string_conv *))
902 if (sc == NULL || sc->nconverter >= 2)
903 __archive_errx(1, "Programming error");
904 sc->converter[sc->nconverter++] = converter;
908 setup_converter(struct archive_string_conv *sc)
915 * Perform special sequence for the incorrect UTF-8 filenames
916 * made by libarchive2.x.
918 if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
919 add_converter(sc, strncat_from_utf8_libarchive2);
924 * Convert a string to UTF-16BE/LE.
926 if (sc->flag & SCONV_TO_UTF16) {
928 * If the current locale is UTF-8, we can translate
929 * a UTF-8 string into a UTF-16BE string.
931 if (sc->flag & SCONV_FROM_UTF8) {
932 add_converter(sc, archive_string_append_unicode);
936 #if defined(_WIN32) && !defined(__CYGWIN__)
937 if (sc->flag & SCONV_WIN_CP) {
938 if (sc->flag & SCONV_TO_UTF16BE)
939 add_converter(sc, win_strncat_to_utf16be);
941 add_converter(sc, win_strncat_to_utf16le);
946 #if defined(HAVE_ICONV)
947 if (sc->cd != (iconv_t)-1) {
948 add_converter(sc, iconv_strncat_in_locale);
953 if (sc->flag & SCONV_BEST_EFFORT) {
954 if (sc->flag & SCONV_TO_UTF16BE)
956 best_effort_strncat_to_utf16be);
959 best_effort_strncat_to_utf16le);
961 /* Make sure we have no converter. */
967 * Convert a string from UTF-16BE/LE.
969 if (sc->flag & SCONV_FROM_UTF16) {
971 * At least we should normalize a UTF-16BE string.
973 if (sc->flag & SCONV_NORMALIZATION_D)
974 add_converter(sc,archive_string_normalize_D);
975 else if (sc->flag & SCONV_NORMALIZATION_C)
976 add_converter(sc, archive_string_normalize_C);
978 if (sc->flag & SCONV_TO_UTF8) {
980 * If the current locale is UTF-8, we can translate
981 * a UTF-16BE/LE string into a UTF-8 string directly.
984 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
986 archive_string_append_unicode);
990 #if defined(_WIN32) && !defined(__CYGWIN__)
991 if (sc->flag & SCONV_WIN_CP) {
992 if (sc->flag & SCONV_FROM_UTF16BE)
993 add_converter(sc, win_strncat_from_utf16be);
995 add_converter(sc, win_strncat_from_utf16le);
1000 #if defined(HAVE_ICONV)
1001 if (sc->cd != (iconv_t)-1) {
1002 add_converter(sc, iconv_strncat_in_locale);
1007 if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
1008 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
1009 add_converter(sc, best_effort_strncat_from_utf16be);
1010 else if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
1011 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
1012 add_converter(sc, best_effort_strncat_from_utf16le);
1014 /* Make sure we have no converter. */
1019 if (sc->flag & SCONV_FROM_UTF8) {
1021 * At least we should normalize a UTF-8 string.
1023 if (sc->flag & SCONV_NORMALIZATION_D)
1024 add_converter(sc,archive_string_normalize_D);
1025 else if (sc->flag & SCONV_NORMALIZATION_C)
1026 add_converter(sc, archive_string_normalize_C);
1029 * Copy UTF-8 string with a check of CESU-8.
1030 * Apparently, iconv does not check surrogate pairs in UTF-8
1031 * when both from-charset and to-charset are UTF-8, and then
1032 * we use our UTF-8 copy code.
1034 if (sc->flag & SCONV_TO_UTF8) {
1036 * If the current locale is UTF-8, we can translate
1037 * a UTF-16BE string into a UTF-8 string directly.
1040 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
1041 add_converter(sc, strncat_from_utf8_to_utf8);
1046 #if defined(_WIN32) && !defined(__CYGWIN__)
1048 * On Windows we can use Windows API for a string conversion.
1050 if (sc->flag & SCONV_WIN_CP) {
1051 add_converter(sc, strncat_in_codepage);
1057 if (sc->cd != (iconv_t)-1) {
1058 add_converter(sc, iconv_strncat_in_locale);
1060 * iconv generally does not support UTF-8-MAC and so
1061 * we have to the output of iconv from NFC to NFD if
1064 if ((sc->flag & SCONV_FROM_CHARSET) &&
1065 (sc->flag & SCONV_TO_UTF8)) {
1066 if (sc->flag & SCONV_NORMALIZATION_D)
1067 add_converter(sc, archive_string_normalize_D);
1074 * Try conversion in the best effort or no conversion.
1076 if ((sc->flag & SCONV_BEST_EFFORT) || sc->same)
1077 add_converter(sc, best_effort_strncat_in_locale);
1079 /* Make sure we have no converter. */
1084 * Return canonicalized charset-name but this supports just UTF-8, UTF-16BE
1085 * and CP932 which are referenced in create_sconv_object().
1088 canonical_charset_name(const char *charset)
1094 if (charset == NULL || charset[0] == '\0'
1095 || strlen(charset) > 15)
1098 /* Copy name to uppercase. */
1103 if (c >= 'a' && c <= 'z')
1109 if (strcmp(cs, "UTF-8") == 0 ||
1110 strcmp(cs, "UTF8") == 0)
1112 if (strcmp(cs, "UTF-16BE") == 0 ||
1113 strcmp(cs, "UTF16BE") == 0)
1114 return ("UTF-16BE");
1115 if (strcmp(cs, "UTF-16LE") == 0 ||
1116 strcmp(cs, "UTF16LE") == 0)
1117 return ("UTF-16LE");
1118 if (strcmp(cs, "CP932") == 0)
1124 * Create a string conversion object.
1126 static struct archive_string_conv *
1127 create_sconv_object(const char *fc, const char *tc,
1128 unsigned current_codepage, int flag)
1130 struct archive_string_conv *sc;
1132 sc = calloc(1, sizeof(*sc));
1136 sc->from_charset = strdup(fc);
1137 if (sc->from_charset == NULL) {
1141 sc->to_charset = strdup(tc);
1142 if (sc->to_charset == NULL) {
1143 free(sc->from_charset);
1147 archive_string_init(&sc->utftmp);
1149 if (flag & SCONV_TO_CHARSET) {
1151 * Convert characters from the current locale charset to
1152 * a specified charset.
1154 sc->from_cp = current_codepage;
1155 sc->to_cp = make_codepage_from_charset(tc);
1156 #if defined(_WIN32) && !defined(__CYGWIN__)
1157 if (IsValidCodePage(sc->to_cp))
1158 flag |= SCONV_WIN_CP;
1160 } else if (flag & SCONV_FROM_CHARSET) {
1162 * Convert characters from a specified charset to
1163 * the current locale charset.
1165 sc->to_cp = current_codepage;
1166 sc->from_cp = make_codepage_from_charset(fc);
1167 #if defined(_WIN32) && !defined(__CYGWIN__)
1168 if (IsValidCodePage(sc->from_cp))
1169 flag |= SCONV_WIN_CP;
1174 * Check if "from charset" and "to charset" are the same.
1176 if (strcmp(fc, tc) == 0 ||
1177 (sc->from_cp != (unsigned)-1 && sc->from_cp == sc->to_cp))
1183 * Mark if "from charset" or "to charset" are UTF-8 or UTF-16BE/LE.
1185 if (strcmp(tc, "UTF-8") == 0)
1186 flag |= SCONV_TO_UTF8;
1187 else if (strcmp(tc, "UTF-16BE") == 0)
1188 flag |= SCONV_TO_UTF16BE;
1189 else if (strcmp(tc, "UTF-16LE") == 0)
1190 flag |= SCONV_TO_UTF16LE;
1191 if (strcmp(fc, "UTF-8") == 0)
1192 flag |= SCONV_FROM_UTF8;
1193 else if (strcmp(fc, "UTF-16BE") == 0)
1194 flag |= SCONV_FROM_UTF16BE;
1195 else if (strcmp(fc, "UTF-16LE") == 0)
1196 flag |= SCONV_FROM_UTF16LE;
1197 #if defined(_WIN32) && !defined(__CYGWIN__)
1198 if (sc->to_cp == CP_UTF8)
1199 flag |= SCONV_TO_UTF8;
1200 else if (sc->to_cp == CP_UTF16BE)
1201 flag |= SCONV_TO_UTF16BE | SCONV_WIN_CP;
1202 else if (sc->to_cp == CP_UTF16LE)
1203 flag |= SCONV_TO_UTF16LE | SCONV_WIN_CP;
1204 if (sc->from_cp == CP_UTF8)
1205 flag |= SCONV_FROM_UTF8;
1206 else if (sc->from_cp == CP_UTF16BE)
1207 flag |= SCONV_FROM_UTF16BE | SCONV_WIN_CP;
1208 else if (sc->from_cp == CP_UTF16LE)
1209 flag |= SCONV_FROM_UTF16LE | SCONV_WIN_CP;
1213 * Set a flag for Unicode NFD. Usually iconv cannot correctly
1214 * handle it. So we have to translate NFD characters to NFC ones
1215 * ourselves before iconv handles. Another reason is to prevent
1216 * that the same sight of two filenames, one is NFC and other
1217 * is NFD, would be in its directory.
1218 * On Mac OS X, although its filesystem layer automatically
1219 * convert filenames to NFD, it would be useful for filename
1220 * comparing to find out the same filenames that we normalize
1221 * that to be NFD ourselves.
1223 if ((flag & SCONV_FROM_CHARSET) &&
1224 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8))) {
1225 #if defined(__APPLE__)
1226 if (flag & SCONV_TO_UTF8)
1227 flag |= SCONV_NORMALIZATION_D;
1230 flag |= SCONV_NORMALIZATION_C;
1232 #if defined(__APPLE__)
1234 * In case writing an archive file, make sure that a filename
1235 * going to be passed to iconv is a Unicode NFC string since
1236 * a filename in HFS Plus filesystem is a Unicode NFD one and
1237 * iconv cannot handle it with "UTF-8" charset. It is simpler
1238 * than a use of "UTF-8-MAC" charset.
1240 if ((flag & SCONV_TO_CHARSET) &&
1241 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1242 !(flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1243 flag |= SCONV_NORMALIZATION_C;
1245 * In case reading an archive file. make sure that a filename
1246 * will be passed to users is a Unicode NFD string in order to
1247 * correctly compare the filename with other one which comes
1248 * from HFS Plus filesystem.
1250 if ((flag & SCONV_FROM_CHARSET) &&
1251 !(flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1252 (flag & SCONV_TO_UTF8))
1253 flag |= SCONV_NORMALIZATION_D;
1256 #if defined(HAVE_ICONV)
1257 sc->cd_w = (iconv_t)-1;
1259 * Create an iconv object.
1261 if (((flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) &&
1262 (flag & (SCONV_FROM_UTF8 | SCONV_FROM_UTF16))) ||
1263 (flag & SCONV_WIN_CP)) {
1264 /* This case we won't use iconv. */
1265 sc->cd = (iconv_t)-1;
1267 sc->cd = iconv_open(tc, fc);
1268 if (sc->cd == (iconv_t)-1 && (sc->flag & SCONV_BEST_EFFORT)) {
1270 * Unfortunately, all of iconv implements do support
1271 * "CP932" character-set, so we should use "SJIS"
1272 * instead if iconv_open failed.
1274 if (strcmp(tc, "CP932") == 0)
1275 sc->cd = iconv_open("SJIS", fc);
1276 else if (strcmp(fc, "CP932") == 0)
1277 sc->cd = iconv_open(tc, "SJIS");
1279 #if defined(_WIN32) && !defined(__CYGWIN__)
1281 * archive_mstring on Windows directly convert multi-bytes
1282 * into archive_wstring in order not to depend on locale
1283 * so that you can do a I18N programming. This will be
1284 * used only in archive_mstring_copy_mbs_len_l so far.
1286 if (flag & SCONV_FROM_CHARSET) {
1287 sc->cd_w = iconv_open("UTF-8", fc);
1288 if (sc->cd_w == (iconv_t)-1 &&
1289 (sc->flag & SCONV_BEST_EFFORT)) {
1290 if (strcmp(fc, "CP932") == 0)
1291 sc->cd_w = iconv_open("UTF-8", "SJIS");
1294 #endif /* _WIN32 && !__CYGWIN__ */
1296 #endif /* HAVE_ICONV */
1301 * Set up converters.
1303 setup_converter(sc);
1309 * Free a string conversion object.
1312 free_sconv_object(struct archive_string_conv *sc)
1314 free(sc->from_charset);
1315 free(sc->to_charset);
1316 archive_string_free(&sc->utftmp);
1318 if (sc->cd != (iconv_t)-1)
1319 iconv_close(sc->cd);
1320 if (sc->cd_w != (iconv_t)-1)
1321 iconv_close(sc->cd_w);
1326 #if defined(_WIN32) && !defined(__CYGWIN__)
1328 my_atoi(const char *p)
1334 if (*p >= '0' && *p <= '9')
1335 cp = cp * 10 + (*p - '0');
1344 * Translate Charset name (as used by iconv) into CodePage (as used by Windows)
1345 * Return -1 if failed.
1347 * Note: This translation code may be insufficient.
1349 static struct charset {
1353 /* MUST BE SORTED! */
1372 {"HZ-GB-2312", 52936},
1390 {"ISO-8859-1", 28591},
1391 {"ISO-8859-13", 28603},
1392 {"ISO-8859-15", 28605},
1393 {"ISO-8859-2", 28592},
1394 {"ISO-8859-3", 28593},
1395 {"ISO-8859-4", 28594},
1396 {"ISO-8859-5", 28595},
1397 {"ISO-8859-6", 28596},
1398 {"ISO-8859-7", 28597},
1399 {"ISO-8859-8", 28598},
1400 {"ISO-8859-9", 28599},
1401 {"ISO8859-1", 28591},
1402 {"ISO8859-13", 28603},
1403 {"ISO8859-15", 28605},
1404 {"ISO8859-2", 28592},
1405 {"ISO8859-3", 28593},
1406 {"ISO8859-4", 28594},
1407 {"ISO8859-5", 28595},
1408 {"ISO8859-6", 28596},
1409 {"ISO8859-7", 28597},
1410 {"ISO8859-8", 28598},
1411 {"ISO8859-9", 28599},
1415 {"KS_C_5601-1987", 949},
1418 {"MACINTOSH", 10000},
1428 {"X-EUROPA", 29001},
1429 {"X-MAC-ARABIC", 10004},
1430 {"X-MAC-CE", 10029},
1431 {"X-MAC-CHINESEIMP", 10008},
1432 {"X-MAC-CHINESETRAD", 10002},
1433 {"X-MAC-CROATIAN", 10082},
1434 {"X-MAC-CYRILLIC", 10007},
1435 {"X-MAC-GREEK", 10006},
1436 {"X-MAC-HEBREW", 10005},
1437 {"X-MAC-ICELANDIC", 10079},
1438 {"X-MAC-JAPANESE", 10001},
1439 {"X-MAC-KOREAN", 10003},
1440 {"X-MAC-ROMANIAN", 10010},
1441 {"X-MAC-THAI", 10021},
1442 {"X-MAC-TURKISH", 10081},
1443 {"X-MAC-UKRAINIAN", 10017},
1446 make_codepage_from_charset(const char *charset)
1453 if (charset == NULL || strlen(charset) > 15)
1456 /* Copy name to uppercase. */
1459 char c = *charset++;
1460 if (c >= 'a' && c <= 'z')
1467 /* Look it up in the table first, so that we can easily
1468 * override CP367, which we map to 1252 instead of 367. */
1470 b = sizeof(charsets)/sizeof(charsets[0]);
1472 int c = (b + a) / 2;
1473 int r = strcmp(charsets[c].name, cs);
1479 return charsets[c].cp;
1482 /* If it's not in the table, try to parse it. */
1485 if (cs[1] == 'P' && cs[2] >= '0' && cs[2] <= '9') {
1486 cp = my_atoi(cs + 2);
1487 } else if (strcmp(cs, "CP_ACP") == 0)
1488 cp = get_current_codepage();
1489 else if (strcmp(cs, "CP_OEMCP") == 0)
1490 cp = get_current_oemcp();
1493 if (cs[1] == 'B' && cs[2] == 'M' &&
1494 cs[3] >= '0' && cs[3] <= '9') {
1495 cp = my_atoi(cs + 3);
1499 if (strncmp(cs, "WINDOWS-", 8) == 0) {
1500 cp = my_atoi(cs + 8);
1501 if (cp != 874 && (cp < 1250 || cp > 1258))
1502 cp = -1;/* This may invalid code. */
1510 * Return ANSI Code Page of current locale set by setlocale().
1513 get_current_codepage(void)
1518 locale = setlocale(LC_CTYPE, NULL);
1521 if (locale[0] == 'C' && locale[1] == '\0')
1522 return (CP_C_LOCALE);
1523 p = strrchr(locale, '.');
1526 if (strcmp(p+1, "utf8") == 0)
1535 * Translation table between Locale Name and ACP/OEMCP.
1542 { 950, 950, "Chinese_Taiwan" },
1543 { 936, 936, "Chinese_People's Republic of China" },
1544 { 950, 950, "Chinese_Taiwan" },
1545 { 1250, 852, "Czech_Czech Republic" },
1546 { 1252, 850, "Danish_Denmark" },
1547 { 1252, 850, "Dutch_Netherlands" },
1548 { 1252, 850, "Dutch_Belgium" },
1549 { 1252, 437, "English_United States" },
1550 { 1252, 850, "English_Australia" },
1551 { 1252, 850, "English_Canada" },
1552 { 1252, 850, "English_New Zealand" },
1553 { 1252, 850, "English_United Kingdom" },
1554 { 1252, 437, "English_United States" },
1555 { 1252, 850, "Finnish_Finland" },
1556 { 1252, 850, "French_France" },
1557 { 1252, 850, "French_Belgium" },
1558 { 1252, 850, "French_Canada" },
1559 { 1252, 850, "French_Switzerland" },
1560 { 1252, 850, "German_Germany" },
1561 { 1252, 850, "German_Austria" },
1562 { 1252, 850, "German_Switzerland" },
1563 { 1253, 737, "Greek_Greece" },
1564 { 1250, 852, "Hungarian_Hungary" },
1565 { 1252, 850, "Icelandic_Iceland" },
1566 { 1252, 850, "Italian_Italy" },
1567 { 1252, 850, "Italian_Switzerland" },
1568 { 932, 932, "Japanese_Japan" },
1569 { 949, 949, "Korean_Korea" },
1570 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1571 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1572 { 1252, 850, "Norwegian-Nynorsk_Norway" },
1573 { 1250, 852, "Polish_Poland" },
1574 { 1252, 850, "Portuguese_Portugal" },
1575 { 1252, 850, "Portuguese_Brazil" },
1576 { 1251, 866, "Russian_Russia" },
1577 { 1250, 852, "Slovak_Slovakia" },
1578 { 1252, 850, "Spanish_Spain" },
1579 { 1252, 850, "Spanish_Mexico" },
1580 { 1252, 850, "Spanish_Spain" },
1581 { 1252, 850, "Swedish_Sweden" },
1582 { 1254, 857, "Turkish_Turkey" },
1587 * Return OEM Code Page of current locale set by setlocale().
1590 get_current_oemcp(void)
1596 locale = setlocale(LC_CTYPE, NULL);
1598 return (GetOEMCP());
1599 if (locale[0] == 'C' && locale[1] == '\0')
1600 return (CP_C_LOCALE);
1602 p = strrchr(locale, '.');
1604 return (GetOEMCP());
1606 for (i = 0; acp_ocp_map[i].acp; i++) {
1607 if (strncmp(acp_ocp_map[i].locale, locale, len) == 0)
1608 return (acp_ocp_map[i].ocp);
1610 return (GetOEMCP());
1615 * POSIX platform does not use CodePage.
1619 get_current_codepage(void)
1621 return (-1);/* Unknown */
1624 make_codepage_from_charset(const char *charset)
1626 (void)charset; /* UNUSED */
1627 return (-1);/* Unknown */
1630 get_current_oemcp(void)
1632 return (-1);/* Unknown */
1635 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1638 * Return a string conversion object.
1640 static struct archive_string_conv *
1641 get_sconv_object(struct archive *a, const char *fc, const char *tc, int flag)
1643 struct archive_string_conv *sc;
1644 unsigned current_codepage;
1646 /* Check if we have made the sconv object. */
1647 sc = find_sconv_object(a, fc, tc);
1652 current_codepage = get_current_codepage();
1654 current_codepage = a->current_codepage;
1656 sc = create_sconv_object(canonical_charset_name(fc),
1657 canonical_charset_name(tc), current_codepage, flag);
1660 archive_set_error(a, ENOMEM,
1661 "Could not allocate memory for "
1662 "a string conversion object");
1667 * If there is no converter for current string conversion object,
1668 * we cannot handle this conversion.
1670 if (sc->nconverter == 0) {
1673 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1674 "iconv_open failed : Cannot handle ``%s''",
1675 (flag & SCONV_TO_CHARSET)?tc:fc);
1677 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1678 "A character-set conversion not fully supported "
1679 "on this platform");
1682 /* Failed; free a sconv object. */
1683 free_sconv_object(sc);
1691 add_sconv_object(a, sc);
1696 get_current_charset(struct archive *a)
1698 const char *cur_charset;
1701 cur_charset = default_iconv_charset("");
1703 cur_charset = default_iconv_charset(a->current_code);
1704 if (a->current_code == NULL) {
1705 a->current_code = strdup(cur_charset);
1706 a->current_codepage = get_current_codepage();
1707 a->current_oemcp = get_current_oemcp();
1710 return (cur_charset);
1714 * Make and Return a string conversion object.
1715 * Return NULL if the platform does not support the specified conversion
1716 * and best_effort is 0.
1717 * If best_effort is set, A string conversion object must be returned
1718 * unless memory allocation for the object fails, but the conversion
1719 * might fail when non-ASCII code is found.
1721 struct archive_string_conv *
1722 archive_string_conversion_to_charset(struct archive *a, const char *charset,
1725 int flag = SCONV_TO_CHARSET;
1728 flag |= SCONV_BEST_EFFORT;
1729 return (get_sconv_object(a, get_current_charset(a), charset, flag));
1732 struct archive_string_conv *
1733 archive_string_conversion_from_charset(struct archive *a, const char *charset,
1736 int flag = SCONV_FROM_CHARSET;
1739 flag |= SCONV_BEST_EFFORT;
1740 return (get_sconv_object(a, charset, get_current_charset(a), flag));
1744 * archive_string_default_conversion_*_archive() are provided for Windows
1745 * platform because other archiver application use CP_OEMCP for
1746 * MultiByteToWideChar() and WideCharToMultiByte() for the filenames
1747 * in tar or zip files. But mbstowcs/wcstombs(CRT) usually use CP_ACP
1748 * unless you use setlocale(LC_ALL, ".OCP")(specify CP_OEMCP).
1749 * So we should make a string conversion between CP_ACP and CP_OEMCP
1750 * for compatibility.
1752 #if defined(_WIN32) && !defined(__CYGWIN__)
1753 struct archive_string_conv *
1754 archive_string_default_conversion_for_read(struct archive *a)
1756 const char *cur_charset = get_current_charset(a);
1759 /* NOTE: a check of cur_charset is unneeded but we need
1760 * that get_current_charset() has been surely called at
1761 * this time whatever C compiler optimized. */
1762 if (cur_charset != NULL &&
1763 (a->current_codepage == CP_C_LOCALE ||
1764 a->current_codepage == a->current_oemcp))
1765 return (NULL);/* no conversion. */
1767 _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1768 /* Make sure a null termination must be set. */
1769 oemcp[sizeof(oemcp)-1] = '\0';
1770 return (get_sconv_object(a, oemcp, cur_charset,
1771 SCONV_FROM_CHARSET));
1774 struct archive_string_conv *
1775 archive_string_default_conversion_for_write(struct archive *a)
1777 const char *cur_charset = get_current_charset(a);
1780 /* NOTE: a check of cur_charset is unneeded but we need
1781 * that get_current_charset() has been surely called at
1782 * this time whatever C compiler optimized. */
1783 if (cur_charset != NULL &&
1784 (a->current_codepage == CP_C_LOCALE ||
1785 a->current_codepage == a->current_oemcp))
1786 return (NULL);/* no conversion. */
1788 _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1789 /* Make sure a null termination must be set. */
1790 oemcp[sizeof(oemcp)-1] = '\0';
1791 return (get_sconv_object(a, cur_charset, oemcp,
1795 struct archive_string_conv *
1796 archive_string_default_conversion_for_read(struct archive *a)
1798 (void)a; /* UNUSED */
1802 struct archive_string_conv *
1803 archive_string_default_conversion_for_write(struct archive *a)
1805 (void)a; /* UNUSED */
1811 * Dispose of all character conversion objects in the archive object.
1814 archive_string_conversion_free(struct archive *a)
1816 struct archive_string_conv *sc;
1817 struct archive_string_conv *sc_next;
1819 for (sc = a->sconv; sc != NULL; sc = sc_next) {
1821 free_sconv_object(sc);
1824 free(a->current_code);
1825 a->current_code = NULL;
1829 * Return a conversion charset name.
1832 archive_string_conversion_charset_name(struct archive_string_conv *sc)
1834 if (sc->flag & SCONV_TO_CHARSET)
1835 return (sc->to_charset);
1837 return (sc->from_charset);
1841 * Change the behavior of a string conversion.
1844 archive_string_conversion_set_opt(struct archive_string_conv *sc, int opt)
1848 * A filename in UTF-8 was made with libarchive 2.x in a wrong
1849 * assumption that wchar_t was Unicode.
1850 * This option enables simulating the assumption in order to read
1851 * that filename correctly.
1853 case SCONV_SET_OPT_UTF8_LIBARCHIVE2X:
1854 #if (defined(_WIN32) && !defined(__CYGWIN__)) \
1855 || defined(__STDC_ISO_10646__) || defined(__APPLE__)
1857 * Nothing to do for it since wchar_t on these platforms
1858 * is really Unicode.
1860 (void)sc; /* UNUSED */
1862 if ((sc->flag & SCONV_UTF8_LIBARCHIVE_2) == 0) {
1863 sc->flag |= SCONV_UTF8_LIBARCHIVE_2;
1864 /* Set up string converters. */
1865 setup_converter(sc);
1869 case SCONV_SET_OPT_NORMALIZATION_C:
1870 if ((sc->flag & SCONV_NORMALIZATION_C) == 0) {
1871 sc->flag |= SCONV_NORMALIZATION_C;
1872 sc->flag &= ~SCONV_NORMALIZATION_D;
1873 /* Set up string converters. */
1874 setup_converter(sc);
1877 case SCONV_SET_OPT_NORMALIZATION_D:
1878 #if defined(HAVE_ICONV)
1880 * If iconv will take the string, do not change the
1881 * setting of the normalization.
1883 if (!(sc->flag & SCONV_WIN_CP) &&
1884 (sc->flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1885 !(sc->flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1888 if ((sc->flag & SCONV_NORMALIZATION_D) == 0) {
1889 sc->flag |= SCONV_NORMALIZATION_D;
1890 sc->flag &= ~SCONV_NORMALIZATION_C;
1891 /* Set up string converters. */
1892 setup_converter(sc);
1902 * Copy one archive_string to another in locale conversion.
1904 * archive_strncat_l();
1905 * archive_strncpy_l();
1910 mbsnbytes(const void *_p, size_t n)
1917 p = (const char *)_p;
1919 /* Like strlen(p), except won't examine positions beyond p[n]. */
1922 while (s < n && *pp) {
1930 utf16nbytes(const void *_p, size_t n)
1937 p = (const char *)_p;
1939 /* Like strlen(p), except won't examine positions beyond p[n]. */
1943 while (s < n && (pp[0] || pp[1])) {
1951 archive_strncpy_l(struct archive_string *as, const void *_p, size_t n,
1952 struct archive_string_conv *sc)
1955 return (archive_strncat_l(as, _p, n, sc));
1959 archive_strncat_l(struct archive_string *as, const void *_p, size_t n,
1960 struct archive_string_conv *sc)
1966 if (_p != NULL && n > 0) {
1967 if (sc != NULL && (sc->flag & SCONV_FROM_UTF16))
1968 length = utf16nbytes(_p, n);
1970 length = mbsnbytes(_p, n);
1973 /* We must allocate memory even if there is no data for conversion
1974 * or copy. This simulates archive_string_append behavior. */
1977 if (sc != NULL && (sc->flag & SCONV_TO_UTF16))
1979 if (archive_string_ensure(as, as->length + tn) == NULL)
1981 as->s[as->length] = 0;
1983 as->s[as->length+1] = 0;
1988 * If sc is NULL, we just make a copy.
1991 if (archive_string_append(as, _p, length) == NULL)
1992 return (-1);/* No memory */
1998 if (sc->nconverter > 1) {
1999 sc->utftmp.length = 0;
2000 r2 = sc->converter[0](&(sc->utftmp), s, length, sc);
2001 if (r2 != 0 && errno == ENOMEM)
2006 length = sc->utftmp.length;
2009 r2 = sc->converter[i](as, s, length, sc);
2018 * Return -1 if conversion fails.
2021 iconv_strncat_in_locale(struct archive_string *as, const void *_p,
2022 size_t length, struct archive_string_conv *sc)
2024 ICONV_CONST char *itp;
2029 int return_value = 0; /* success */
2030 int to_size, from_size;
2032 if (sc->flag & SCONV_TO_UTF16)
2036 if (sc->flag & SCONV_FROM_UTF16)
2041 if (archive_string_ensure(as, as->length + length*2+to_size) == NULL)
2045 itp = (char *)(uintptr_t)_p;
2047 outp = as->s + as->length;
2048 avail = as->buffer_length - as->length - to_size;
2049 while (remaining >= (size_t)from_size) {
2050 size_t result = iconv(cd, &itp, &remaining, &outp, &avail);
2052 if (result != (size_t)-1)
2053 break; /* Conversion completed. */
2055 if (errno == EILSEQ || errno == EINVAL) {
2057 * If an output charset is UTF-8 or UTF-16BE/LE,
2058 * unknown character should be U+FFFD
2059 * (replacement character).
2061 if (sc->flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) {
2063 if (sc->flag & SCONV_TO_UTF8)
2064 rbytes = sizeof(utf8_replacement_char);
2068 if (avail < rbytes) {
2069 as->length = outp - as->s;
2070 bs = as->buffer_length +
2071 (remaining * to_size) + rbytes;
2073 archive_string_ensure(as, bs))
2075 outp = as->s + as->length;
2076 avail = as->buffer_length
2077 - as->length - to_size;
2079 if (sc->flag & SCONV_TO_UTF8)
2080 memcpy(outp, utf8_replacement_char, sizeof(utf8_replacement_char));
2081 else if (sc->flag & SCONV_TO_UTF16BE)
2082 archive_be16enc(outp, UNICODE_R_CHAR);
2084 archive_le16enc(outp, UNICODE_R_CHAR);
2088 /* Skip the illegal input bytes. */
2093 remaining -= from_size;
2094 return_value = -1; /* failure */
2096 /* E2BIG no output buffer,
2097 * Increase an output buffer. */
2098 as->length = outp - as->s;
2099 bs = as->buffer_length + remaining * 2;
2100 if (NULL == archive_string_ensure(as, bs))
2102 outp = as->s + as->length;
2103 avail = as->buffer_length - as->length - to_size;
2106 as->length = outp - as->s;
2107 as->s[as->length] = 0;
2109 as->s[as->length+1] = 0;
2110 return (return_value);
2113 #endif /* HAVE_ICONV */
2116 #if defined(_WIN32) && !defined(__CYGWIN__)
2119 * Translate a string from a some CodePage to an another CodePage by
2120 * Windows APIs, and copy the result. Return -1 if conversion fails.
2123 strncat_in_codepage(struct archive_string *as,
2124 const void *_p, size_t length, struct archive_string_conv *sc)
2126 const char *s = (const char *)_p;
2127 struct archive_wstring aws;
2131 archive_string_init(&aws);
2132 saved_flag = sc->flag;
2133 sc->flag &= ~(SCONV_NORMALIZATION_D | SCONV_NORMALIZATION_C);
2134 r = archive_wstring_append_from_mbs_in_codepage(&aws, s, length, sc);
2135 sc->flag = saved_flag;
2137 archive_wstring_free(&aws);
2138 if (errno != ENOMEM)
2139 archive_string_append(as, s, length);
2144 r = archive_string_append_from_wcs_in_codepage(
2145 as, aws.s, aws.length, sc);
2146 if (r != 0 && errno != ENOMEM && l == as->length)
2147 archive_string_append(as, s, length);
2148 archive_wstring_free(&aws);
2153 * Test whether MBS ==> WCS is okay.
2156 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2158 const char *p = (const char *)_p;
2160 DWORD mbflag = MB_ERR_INVALID_CHARS;
2162 if (sc->flag & SCONV_FROM_CHARSET)
2163 codepage = sc->to_cp;
2165 codepage = sc->from_cp;
2167 if (codepage == CP_C_LOCALE)
2169 if (codepage != CP_UTF8)
2170 mbflag |= MB_PRECOMPOSED;
2172 if (MultiByteToWideChar(codepage, mbflag, p, (int)n, NULL, 0) == 0)
2173 return (-1); /* Invalid */
2174 return (0); /* Okay */
2180 * Test whether MBS ==> WCS is okay.
2183 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2185 const char *p = (const char *)_p;
2189 mbstate_t shift_state;
2191 memset(&shift_state, 0, sizeof(shift_state));
2193 /* Clear the shift state before starting. */
2194 mbtowc(NULL, NULL, 0);
2200 r = mbrtowc(&wc, p, n, &shift_state);
2202 r = mbtowc(&wc, p, n);
2204 if (r == (size_t)-1 || r == (size_t)-2)
2205 return (-1);/* Invalid. */
2211 (void)sc; /* UNUSED */
2212 return (0); /* All Okey. */
2215 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2218 * Basically returns -1 because we cannot make a conversion of charset
2219 * without iconv but in some cases this would return 0.
2220 * Returns 0 if all copied characters are ASCII.
2221 * Returns 0 if both from-locale and to-locale are the same and those
2222 * can be WCS with no error.
2225 best_effort_strncat_in_locale(struct archive_string *as, const void *_p,
2226 size_t length, struct archive_string_conv *sc)
2230 int return_value = 0; /* success */
2233 * If both from-locale and to-locale is the same, this makes a copy.
2234 * And then this checks all copied MBS can be WCS if so returns 0.
2237 if (archive_string_append(as, _p, length) == NULL)
2238 return (-1);/* No memory */
2239 return (invalid_mbs(_p, length, sc));
2243 * If a character is ASCII, this just copies it. If not, this
2244 * assigns '?' character instead but in UTF-8 locale this assigns
2245 * byte sequence 0xEF 0xBD 0xBD, which are code point U+FFFD,
2246 * a Replacement Character in Unicode.
2250 itp = (const uint8_t *)_p;
2251 while (*itp && remaining > 0) {
2253 // Non-ASCII: Substitute with suitable replacement
2254 if (sc->flag & SCONV_TO_UTF8) {
2255 if (archive_string_append(as, utf8_replacement_char, sizeof(utf8_replacement_char)) == NULL) {
2256 __archive_errx(1, "Out of memory");
2259 archive_strappend_char(as, '?');
2263 archive_strappend_char(as, *itp);
2267 return (return_value);
2272 * Unicode conversion functions.
2273 * - UTF-8 <===> UTF-8 in removing surrogate pairs.
2274 * - UTF-8 NFD ===> UTF-8 NFC in removing surrogate pairs.
2275 * - UTF-8 made by libarchive 2.x ===> UTF-8.
2276 * - UTF-16BE <===> UTF-8.
2281 * Utility to convert a single UTF-8 sequence.
2283 * Usually return used bytes, return used byte in negative value when
2284 * a unicode character is replaced with U+FFFD.
2285 * See also http://unicode.org/review/pr-121.html Public Review Issue #121
2286 * Recommended Practice for Replacement Characters.
2289 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2291 static const char utf8_count[256] = {
2292 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
2293 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
2294 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
2295 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
2296 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
2297 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
2298 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
2299 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
2300 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
2301 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
2302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
2303 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
2304 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
2305 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
2306 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
2307 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
2317 * Decode 1-4 bytes depending on the value of the first byte.
2319 ch = (unsigned char)*s;
2321 return (0); /* Standard: return 0 for end-of-string. */
2322 cnt = utf8_count[ch];
2324 /* Invalid sequence or there are not plenty bytes. */
2327 for (i = 1; i < cnt; i++) {
2328 if ((s[i] & 0xc0) != 0x80) {
2333 goto invalid_sequence;
2336 /* Make a Unicode code point from a single UTF-8 sequence. */
2338 case 1: /* 1 byte sequence. */
2341 case 2: /* 2 bytes sequence. */
2342 if ((s[1] & 0xc0) != 0x80) {
2344 goto invalid_sequence;
2346 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
2348 case 3: /* 3 bytes sequence. */
2349 if ((s[1] & 0xc0) != 0x80) {
2351 goto invalid_sequence;
2353 if ((s[2] & 0xc0) != 0x80) {
2355 goto invalid_sequence;
2357 wc = ((ch & 0x0f) << 12)
2358 | ((s[1] & 0x3f) << 6)
2361 goto invalid_sequence;/* Overlong sequence. */
2363 case 4: /* 4 bytes sequence. */
2364 if ((s[1] & 0xc0) != 0x80) {
2366 goto invalid_sequence;
2368 if ((s[2] & 0xc0) != 0x80) {
2370 goto invalid_sequence;
2372 if ((s[3] & 0xc0) != 0x80) {
2374 goto invalid_sequence;
2376 wc = ((ch & 0x07) << 18)
2377 | ((s[1] & 0x3f) << 12)
2378 | ((s[2] & 0x3f) << 6)
2381 goto invalid_sequence;/* Overlong sequence. */
2383 default: /* Others are all invalid sequence. */
2384 if (ch == 0xc0 || ch == 0xc1)
2386 else if (ch >= 0xf5 && ch <= 0xf7)
2388 else if (ch >= 0xf8 && ch <= 0xfb)
2390 else if (ch == 0xfc || ch == 0xfd)
2396 for (i = 1; i < cnt; i++) {
2397 if ((s[i] & 0xc0) != 0x80) {
2402 goto invalid_sequence;
2405 /* The code point larger than 0x10FFFF is not legal
2406 * Unicode values. */
2407 if (wc > UNICODE_MAX)
2408 goto invalid_sequence;
2409 /* Correctly gets a Unicode, returns used bytes. */
2413 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2418 utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2422 cnt = _utf8_to_unicode(pwc, s, n);
2423 /* Any of Surrogate pair is not legal Unicode values. */
2424 if (cnt == 3 && IS_SURROGATE_PAIR_LA(*pwc))
2429 static inline uint32_t
2430 combine_surrogate_pair(uint32_t uc, uint32_t uc2)
2440 * Convert a single UTF-8/CESU-8 sequence to a Unicode code point in
2441 * removing surrogate pairs.
2443 * CESU-8: The Compatibility Encoding Scheme for UTF-16.
2445 * Usually return used bytes, return used byte in negative value when
2446 * a unicode character is replaced with U+FFFD.
2449 cesu8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2454 cnt = _utf8_to_unicode(&wc, s, n);
2455 if (cnt == 3 && IS_HIGH_SURROGATE_LA(wc)) {
2458 /* Invalid byte sequence. */
2459 goto invalid_sequence;
2461 cnt = _utf8_to_unicode(&wc2, s+3, n-3);
2462 if (cnt != 3 || !IS_LOW_SURROGATE_LA(wc2)) {
2463 /* Invalid byte sequence. */
2464 goto invalid_sequence;
2466 wc = combine_surrogate_pair(wc, wc2);
2468 } else if (cnt == 3 && IS_LOW_SURROGATE_LA(wc)) {
2469 /* Invalid byte sequence. */
2470 goto invalid_sequence;
2475 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2482 * Convert a Unicode code point to a single UTF-8 sequence.
2484 * NOTE:This function does not check if the Unicode is legal or not.
2485 * Please you definitely check it before calling this.
2488 unicode_to_utf8(char *p, size_t remaining, uint32_t uc)
2492 /* Invalid Unicode char maps to Replacement character */
2493 if (uc > UNICODE_MAX)
2494 uc = UNICODE_R_CHAR;
2495 /* Translate code point to UTF8 */
2500 } else if (uc <= 0x7ff) {
2503 *p++ = 0xc0 | ((uc >> 6) & 0x1f);
2504 *p++ = 0x80 | (uc & 0x3f);
2505 } else if (uc <= 0xffff) {
2508 *p++ = 0xe0 | ((uc >> 12) & 0x0f);
2509 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2510 *p++ = 0x80 | (uc & 0x3f);
2514 *p++ = 0xf0 | ((uc >> 18) & 0x07);
2515 *p++ = 0x80 | ((uc >> 12) & 0x3f);
2516 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2517 *p++ = 0x80 | (uc & 0x3f);
2523 utf16be_to_unicode(uint32_t *pwc, const char *s, size_t n)
2525 return (utf16_to_unicode(pwc, s, n, 1));
2529 utf16le_to_unicode(uint32_t *pwc, const char *s, size_t n)
2531 return (utf16_to_unicode(pwc, s, n, 0));
2535 utf16_to_unicode(uint32_t *pwc, const char *s, size_t n, int be)
2537 const char *utf16 = s;
2543 /* set the Replacement Character instead. */
2544 *pwc = UNICODE_R_CHAR;
2549 uc = archive_be16dec(utf16);
2551 uc = archive_le16dec(utf16);
2554 /* If this is a surrogate pair, assemble the full code point.*/
2555 if (IS_HIGH_SURROGATE_LA(uc)) {
2560 uc2 = archive_be16dec(utf16);
2562 uc2 = archive_le16dec(utf16);
2565 if (IS_LOW_SURROGATE_LA(uc2)) {
2566 uc = combine_surrogate_pair(uc, uc2);
2569 /* Undescribed code point should be U+FFFD
2570 * (replacement character). */
2571 *pwc = UNICODE_R_CHAR;
2577 * Surrogate pair values(0xd800 through 0xdfff) are only
2578 * used by UTF-16, so, after above calculation, the code
2579 * must not be surrogate values, and Unicode has no codes
2580 * larger than 0x10ffff. Thus, those are not legal Unicode
2583 if (IS_SURROGATE_PAIR_LA(uc) || uc > UNICODE_MAX) {
2584 /* Undescribed code point should be U+FFFD
2585 * (replacement character). */
2586 *pwc = UNICODE_R_CHAR;
2587 return (((int)(utf16 - s)) * -1);
2590 return ((int)(utf16 - s));
2594 unicode_to_utf16be(char *p, size_t remaining, uint32_t uc)
2599 /* We have a code point that won't fit into a
2600 * wchar_t; convert it to a surrogate pair. */
2604 archive_be16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2605 archive_be16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2610 archive_be16enc(utf16, uc);
2616 unicode_to_utf16le(char *p, size_t remaining, uint32_t uc)
2621 /* We have a code point that won't fit into a
2622 * wchar_t; convert it to a surrogate pair. */
2626 archive_le16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2627 archive_le16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2632 archive_le16enc(utf16, uc);
2638 * Copy UTF-8 string in checking surrogate pair.
2639 * If any surrogate pair are found, it would be canonicalized.
2642 strncat_from_utf8_to_utf8(struct archive_string *as, const void *_p,
2643 size_t len, struct archive_string_conv *sc)
2649 (void)sc; /* UNUSED */
2651 if (archive_string_ensure(as, as->length + len + 1) == NULL)
2654 s = (const char *)_p;
2655 p = as->s + as->length;
2656 endp = as->s + as->buffer_length -1;
2663 * Forward byte sequence until a conversion of that is needed.
2665 while ((n = utf8_to_unicode(&uc, s, len)) > 0) {
2670 if (p + (s - ss) > endp) {
2671 as->length = p - as->s;
2672 if (archive_string_ensure(as,
2673 as->buffer_length + len + 1) == NULL)
2675 p = as->s + as->length;
2676 endp = as->s + as->buffer_length -1;
2679 memcpy(p, ss, s - ss);
2684 * If n is negative, current byte sequence needs a replacement.
2687 if (n == -3 && IS_SURROGATE_PAIR_LA(uc)) {
2688 /* Current byte sequence may be CESU-8. */
2689 n = cesu8_to_unicode(&uc, s, len);
2693 n *= -1;/* Use a replaced unicode character. */
2696 /* Rebuild UTF-8 byte sequence. */
2697 while ((w = unicode_to_utf8(p, endp - p, uc)) == 0) {
2698 as->length = p - as->s;
2699 if (archive_string_ensure(as,
2700 as->buffer_length + len + 1) == NULL)
2702 p = as->s + as->length;
2703 endp = as->s + as->buffer_length -1;
2710 as->length = p - as->s;
2711 as->s[as->length] = '\0';
2716 archive_string_append_unicode(struct archive_string *as, const void *_p,
2717 size_t len, struct archive_string_conv *sc)
2723 int n, ret = 0, ts, tm;
2724 int (*parse)(uint32_t *, const char *, size_t);
2725 size_t (*unparse)(char *, size_t, uint32_t);
2727 if (sc->flag & SCONV_TO_UTF16BE) {
2728 unparse = unicode_to_utf16be;
2730 } else if (sc->flag & SCONV_TO_UTF16LE) {
2731 unparse = unicode_to_utf16le;
2733 } else if (sc->flag & SCONV_TO_UTF8) {
2734 unparse = unicode_to_utf8;
2738 * This case is going to be converted to another
2739 * character-set through iconv.
2741 if (sc->flag & SCONV_FROM_UTF16BE) {
2742 unparse = unicode_to_utf16be;
2744 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2745 unparse = unicode_to_utf16le;
2748 unparse = unicode_to_utf8;
2753 if (sc->flag & SCONV_FROM_UTF16BE) {
2754 parse = utf16be_to_unicode;
2756 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2757 parse = utf16le_to_unicode;
2760 parse = cesu8_to_unicode;
2764 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2767 s = (const char *)_p;
2768 p = as->s + as->length;
2769 endp = as->s + as->buffer_length - ts;
2770 while ((n = parse(&uc, s, len)) != 0) {
2772 /* Use a replaced unicode character. */
2778 while ((w = unparse(p, endp - p, uc)) == 0) {
2779 /* There is not enough output buffer so
2780 * we have to expand it. */
2781 as->length = p - as->s;
2782 if (archive_string_ensure(as,
2783 as->buffer_length + len * tm + ts) == NULL)
2785 p = as->s + as->length;
2786 endp = as->s + as->buffer_length - ts;
2790 as->length = p - as->s;
2791 as->s[as->length] = '\0';
2793 as->s[as->length+1] = '\0';
2798 * Following Constants for Hangul compositions this information comes from
2799 * Unicode Standard Annex #15 http://unicode.org/reports/tr15/
2801 #define HC_SBASE 0xAC00
2802 #define HC_LBASE 0x1100
2803 #define HC_VBASE 0x1161
2804 #define HC_TBASE 0x11A7
2805 #define HC_LCOUNT 19
2806 #define HC_VCOUNT 21
2807 #define HC_TCOUNT 28
2808 #define HC_NCOUNT (HC_VCOUNT * HC_TCOUNT)
2809 #define HC_SCOUNT (HC_LCOUNT * HC_NCOUNT)
2812 get_nfc(uint32_t uc, uint32_t uc2)
2817 b = sizeof(u_composition_table)/sizeof(u_composition_table[0]) -1;
2819 int m = (t + b) / 2;
2820 if (u_composition_table[m].cp1 < uc)
2822 else if (u_composition_table[m].cp1 > uc)
2824 else if (u_composition_table[m].cp2 < uc2)
2826 else if (u_composition_table[m].cp2 > uc2)
2829 return (u_composition_table[m].nfc);
2834 #define FDC_MAX 10 /* The maximum number of Following Decomposable
2838 * Update first code point.
2840 #define UPDATE_UC(new_uc) do { \
2846 * Replace first code point with second code point.
2848 #define REPLACE_UC_WITH_UC2() do { \
2854 #define EXPAND_BUFFER() do { \
2855 as->length = p - as->s; \
2856 if (archive_string_ensure(as, \
2857 as->buffer_length + len * tm + ts) == NULL)\
2859 p = as->s + as->length; \
2860 endp = as->s + as->buffer_length - ts; \
2863 #define UNPARSE(p, endp, uc) do { \
2864 while ((w = unparse(p, (endp) - (p), uc)) == 0) {\
2871 * Write first code point.
2872 * If the code point has not be changed from its original code,
2873 * this just copies it from its original buffer pointer.
2874 * If not, this converts it to UTF-8 byte sequence and copies it.
2876 #define WRITE_UC() do { \
2883 /* FALL THROUGH */ \
2886 /* FALL THROUGH */ \
2889 /* FALL THROUGH */ \
2896 UNPARSE(p, endp, uc); \
2901 * Collect following decomposable code points.
2903 #define COLLECT_CPS(start) do { \
2905 for (_i = start; _i < FDC_MAX ; _i++) { \
2906 nx = parse(&ucx[_i], s, len); \
2909 cx = CCC(ucx[_i]); \
2910 if (cl >= cx && cl != 228 && cx != 228)\
2917 if (_i >= FDC_MAX) { \
2919 ucx_size = FDC_MAX; \
2925 * Normalize UTF-8/UTF-16BE characters to Form C and copy the result.
2927 * TODO: Convert composition exclusions, which are never converted
2928 * from NFC,NFD,NFKC and NFKD, to Form C.
2931 archive_string_normalize_C(struct archive_string *as, const void *_p,
2932 size_t len, struct archive_string_conv *sc)
2934 const char *s = (const char *)_p;
2938 int always_replace, n, n2, ret = 0, spair, ts, tm;
2939 int (*parse)(uint32_t *, const char *, size_t);
2940 size_t (*unparse)(char *, size_t, uint32_t);
2943 ts = 1;/* text size. */
2944 if (sc->flag & SCONV_TO_UTF16BE) {
2945 unparse = unicode_to_utf16be;
2947 if (sc->flag & SCONV_FROM_UTF16BE)
2949 } else if (sc->flag & SCONV_TO_UTF16LE) {
2950 unparse = unicode_to_utf16le;
2952 if (sc->flag & SCONV_FROM_UTF16LE)
2954 } else if (sc->flag & SCONV_TO_UTF8) {
2955 unparse = unicode_to_utf8;
2956 if (sc->flag & SCONV_FROM_UTF8)
2960 * This case is going to be converted to another
2961 * character-set through iconv.
2964 if (sc->flag & SCONV_FROM_UTF16BE) {
2965 unparse = unicode_to_utf16be;
2967 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2968 unparse = unicode_to_utf16le;
2971 unparse = unicode_to_utf8;
2975 if (sc->flag & SCONV_FROM_UTF16BE) {
2976 parse = utf16be_to_unicode;
2978 spair = 4;/* surrogate pair size in UTF-16. */
2979 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2980 parse = utf16le_to_unicode;
2982 spair = 4;/* surrogate pair size in UTF-16. */
2984 parse = cesu8_to_unicode;
2986 spair = 6;/* surrogate pair size in UTF-8. */
2989 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2992 p = as->s + as->length;
2993 endp = as->s + as->buffer_length - ts;
2994 while ((n = parse(&uc, s, len)) != 0) {
2995 const char *ucptr, *uc2ptr;
2998 /* Use a replaced unicode character. */
2999 UNPARSE(p, endp, uc);
3004 } else if (n == spair || always_replace)
3005 /* uc is converted from a surrogate pair.
3006 * this should be treated as a changed code. */
3013 /* Read second code point. */
3014 while ((n2 = parse(&uc2, s, len)) > 0) {
3015 uint32_t ucx[FDC_MAX];
3017 int cl, cx, i, nx, ucx_size;
3021 if (n2 == spair || always_replace)
3022 /* uc2 is converted from a surrogate pair.
3023 * this should be treated as a changed code. */
3031 * If current second code point is out of decomposable
3032 * code points, finding compositions is unneeded.
3034 if (!IS_DECOMPOSABLE_BLOCK(uc2)) {
3036 REPLACE_UC_WITH_UC2();
3041 * Try to combine current code points.
3044 * We have to combine Hangul characters according to
3045 * http://uniicode.org/reports/tr15/#Hangul
3047 if (0 <= (LIndex = uc - HC_LBASE) &&
3048 LIndex < HC_LCOUNT) {
3050 * Hangul Composition.
3051 * 1. Two current code points are L and V.
3053 int VIndex = uc2 - HC_VBASE;
3054 if (0 <= VIndex && VIndex < HC_VCOUNT) {
3055 /* Make syllable of form LV. */
3056 UPDATE_UC(HC_SBASE +
3057 (LIndex * HC_VCOUNT + VIndex) *
3061 REPLACE_UC_WITH_UC2();
3064 } else if (0 <= (SIndex = uc - HC_SBASE) &&
3065 SIndex < HC_SCOUNT && (SIndex % HC_TCOUNT) == 0) {
3067 * Hangul Composition.
3068 * 2. Two current code points are LV and T.
3070 int TIndex = uc2 - HC_TBASE;
3071 if (0 < TIndex && TIndex < HC_TCOUNT) {
3072 /* Make syllable of form LVT. */
3073 UPDATE_UC(uc + TIndex);
3076 REPLACE_UC_WITH_UC2();
3079 } else if ((nfc = get_nfc(uc, uc2)) != 0) {
3080 /* A composition to current code points
3084 } else if ((cl = CCC(uc2)) == 0) {
3085 /* Clearly 'uc2' the second code point is not
3086 * a decomposable code. */
3088 REPLACE_UC_WITH_UC2();
3093 * Collect following decomposable code points.
3101 * Find a composed code in the collected code points.
3104 while (i < ucx_size) {
3107 if ((nfc = get_nfc(uc, ucx[i])) == 0) {
3113 * nfc is composed of uc and ucx[i].
3118 * Remove ucx[i] by shifting
3119 * following code points.
3121 for (j = i; j+1 < ucx_size; j++) {
3128 * Collect following code points blocked
3129 * by ucx[i] the removed code point.
3131 if (ucx_size > 0 && i == ucx_size &&
3132 nx > 0 && cx == cl) {
3133 cl = ccx[ucx_size-1];
3134 COLLECT_CPS(ucx_size);
3137 * Restart finding a composed code with
3138 * the updated uc from the top of the
3139 * collected code points.
3145 * Apparently the current code points are not
3146 * decomposed characters or already composed.
3149 for (i = 0; i < ucx_size; i++)
3150 UNPARSE(p, endp, ucx[i]);
3153 * Flush out remaining canonical combining characters.
3155 if (nx > 0 && cx == cl && len > 0) {
3156 while ((nx = parse(&ucx[0], s, len))
3164 UNPARSE(p, endp, ucx[0]);
3171 /* Use a replaced unicode character. */
3172 UNPARSE(p, endp, uc2);
3177 } else if (n2 == 0) {
3182 as->length = p - as->s;
3183 as->s[as->length] = '\0';
3185 as->s[as->length+1] = '\0';
3190 get_nfd(uint32_t *cp1, uint32_t *cp2, uint32_t uc)
3195 * These are not converted to NFD on Mac OS.
3197 if ((uc >= 0x2000 && uc <= 0x2FFF) ||
3198 (uc >= 0xF900 && uc <= 0xFAFF) ||
3199 (uc >= 0x2F800 && uc <= 0x2FAFF))
3202 * Those code points are not converted to NFD on Mac OS.
3203 * I do not know the reason because it is undocumented.
3205 * 1109A ==> 11099 110BA
3206 * 1109C ==> 1109B 110BA
3207 * 110AB ==> 110A5 110BA
3209 if (uc == 0x1109A || uc == 0x1109C || uc == 0x110AB)
3213 b = sizeof(u_decomposition_table)/sizeof(u_decomposition_table[0]) -1;
3215 int m = (t + b) / 2;
3216 if (u_decomposition_table[m].nfc < uc)
3218 else if (u_decomposition_table[m].nfc > uc)
3221 *cp1 = u_decomposition_table[m].cp1;
3222 *cp2 = u_decomposition_table[m].cp2;
3229 #define REPLACE_UC_WITH(cp) do { \
3235 * Normalize UTF-8 characters to Form D and copy the result.
3238 archive_string_normalize_D(struct archive_string *as, const void *_p,
3239 size_t len, struct archive_string_conv *sc)
3241 const char *s = (const char *)_p;
3245 int always_replace, n, n2, ret = 0, spair, ts, tm;
3246 int (*parse)(uint32_t *, const char *, size_t);
3247 size_t (*unparse)(char *, size_t, uint32_t);
3250 ts = 1;/* text size. */
3251 if (sc->flag & SCONV_TO_UTF16BE) {
3252 unparse = unicode_to_utf16be;
3254 if (sc->flag & SCONV_FROM_UTF16BE)
3256 } else if (sc->flag & SCONV_TO_UTF16LE) {
3257 unparse = unicode_to_utf16le;
3259 if (sc->flag & SCONV_FROM_UTF16LE)
3261 } else if (sc->flag & SCONV_TO_UTF8) {
3262 unparse = unicode_to_utf8;
3263 if (sc->flag & SCONV_FROM_UTF8)
3267 * This case is going to be converted to another
3268 * character-set through iconv.
3271 if (sc->flag & SCONV_FROM_UTF16BE) {
3272 unparse = unicode_to_utf16be;
3274 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3275 unparse = unicode_to_utf16le;
3278 unparse = unicode_to_utf8;
3282 if (sc->flag & SCONV_FROM_UTF16BE) {
3283 parse = utf16be_to_unicode;
3285 spair = 4;/* surrogate pair size in UTF-16. */
3286 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3287 parse = utf16le_to_unicode;
3289 spair = 4;/* surrogate pair size in UTF-16. */
3291 parse = cesu8_to_unicode;
3293 spair = 6;/* surrogate pair size in UTF-8. */
3296 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
3299 p = as->s + as->length;
3300 endp = as->s + as->buffer_length - ts;
3301 while ((n = parse(&uc, s, len)) != 0) {
3314 /* Use a replaced unicode character. */
3315 UNPARSE(p, endp, uc);
3320 } else if (n == spair || always_replace)
3321 /* uc is converted from a surrogate pair.
3322 * this should be treated as a changed code. */
3329 /* Hangul Decomposition. */
3330 if ((SIndex = uc - HC_SBASE) >= 0 && SIndex < HC_SCOUNT) {
3331 int L = HC_LBASE + SIndex / HC_NCOUNT;
3332 int V = HC_VBASE + (SIndex % HC_NCOUNT) / HC_TCOUNT;
3333 int T = HC_TBASE + SIndex % HC_TCOUNT;
3339 if (T != HC_TBASE) {
3345 if (IS_DECOMPOSABLE_BLOCK(uc) && CCC(uc) != 0) {
3351 while (get_nfd(&cp1, &cp2, uc) && fdi < FDC_MAX) {
3354 for (k = fdi; k > 0; k--)
3356 fdc[0].ccc = CCC(cp2);
3359 REPLACE_UC_WITH(cp1);
3362 /* Read following code points. */
3363 while ((n2 = parse(&uc2, s, len)) > 0 &&
3364 (ccc = CCC(uc2)) != 0 && fdi < FDC_MAX) {
3369 for (j = 0; j < fdi; j++) {
3370 if (fdc[j].ccc > ccc)
3374 for (k = fdi; k > j; k--)
3386 for (fdj = 0; fdj < fdi; fdj++) {
3387 REPLACE_UC_WITH(fdc[fdj].uc);
3393 REPLACE_UC_WITH(uc2);
3395 goto check_first_code;
3397 as->length = p - as->s;
3398 as->s[as->length] = '\0';
3400 as->s[as->length+1] = '\0';
3405 * libarchive 2.x made incorrect UTF-8 strings in the wrong assumption
3406 * that WCS is Unicode. It is true for several platforms but some are false.
3407 * And then people who did not use UTF-8 locale on the non Unicode WCS
3408 * platform and made a tar file with libarchive(mostly bsdtar) 2.x. Those
3409 * now cannot get right filename from libarchive 3.x and later since we
3410 * fixed the wrong assumption and it is incompatible to older its versions.
3411 * So we provide special option, "compat-2x.x", for resolving it.
3412 * That option enable the string conversion of libarchive 2.x.
3414 * Translates the wrong UTF-8 string made by libarchive 2.x into current
3415 * locale character set and appends to the archive_string.
3416 * Note: returns -1 if conversion fails.
3419 strncat_from_utf8_libarchive2(struct archive_string *as,
3420 const void *_p, size_t len, struct archive_string_conv *sc)
3428 mbstate_t shift_state;
3430 memset(&shift_state, 0, sizeof(shift_state));
3432 /* Clear the shift state before starting. */
3433 wctomb(NULL, L'\0');
3435 (void)sc; /* UNUSED */
3437 * Allocate buffer for MBS.
3438 * We need this allocation here since it is possible that
3439 * as->s is still NULL.
3441 if (archive_string_ensure(as, as->length + len + 1) == NULL)
3444 s = (const char *)_p;
3445 p = as->s + as->length;
3446 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3447 while ((n = _utf8_to_unicode(&unicode, s, len)) != 0) {
3451 as->length = p - as->s;
3452 /* Re-allocate buffer for MBS. */
3453 if (archive_string_ensure(as,
3454 as->length + max(len * 2,
3455 (size_t)MB_CUR_MAX) + 1) == NULL)
3457 p = as->s + as->length;
3458 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3462 * As libarchive 2.x, translates the UTF-8 characters into
3463 * wide-characters in the assumption that WCS is Unicode.
3469 wc = (wchar_t)unicode;
3474 * Translates the wide-character into the current locale MBS.
3477 n = (int)wcrtomb(p, wc, &shift_state);
3479 n = (int)wctomb(p, wc);
3485 as->length = p - as->s;
3486 as->s[as->length] = '\0';
3492 * Conversion functions between current locale dependent MBS and UTF-16BE.
3493 * strncat_from_utf16be() : UTF-16BE --> MBS
3494 * strncat_to_utf16be() : MBS --> UTF16BE
3497 #if defined(_WIN32) && !defined(__CYGWIN__)
3500 * Convert a UTF-16BE/LE string to current locale and copy the result.
3501 * Return -1 if conversion fails.
3504 win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
3505 struct archive_string_conv *sc, int be)
3507 struct archive_string tmp;
3516 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3519 mbs = as->s + as->length;
3520 mbs_size = as->buffer_length - as->length -1;
3522 if (sc->to_cp == CP_C_LOCALE) {
3524 * "C" locale special process.
3528 for (b = 0; b < bytes; b += 2) {
3531 val = archive_be16dec(u16+b);
3533 val = archive_le16dec(u16+b);
3538 *mbs++ = (char)(val&0xff);
3542 as->s[as->length] = '\0';
3546 archive_string_init(&tmp);
3548 if (is_big_endian()) {
3551 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3553 memcpy(tmp.s, _p, bytes);
3554 for (b = 0; b < bytes; b += 2) {
3555 uint16_t val = archive_be16dec(tmp.s+b);
3556 archive_le16enc(tmp.s+b, val);
3561 if (!is_big_endian()) {
3564 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3566 memcpy(tmp.s, _p, bytes);
3567 for (b = 0; b < bytes; b += 2) {
3568 uint16_t val = archive_le16dec(tmp.s+b);
3569 archive_be16enc(tmp.s+b, val);
3577 ll = WideCharToMultiByte(sc->to_cp, 0,
3578 (LPCWSTR)u16, (int)bytes>>1, mbs, (int)mbs_size,
3580 /* Exit loop if we succeeded */
3582 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3585 /* Else expand buffer and loop to try again. */
3586 ll = WideCharToMultiByte(sc->to_cp, 0,
3587 (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
3588 if (archive_string_ensure(as, ll +1) == NULL)
3590 mbs = as->s + as->length;
3591 mbs_size = as->buffer_length - as->length -1;
3593 archive_string_free(&tmp);
3595 as->s[as->length] = '\0';
3596 if (ll == 0 || defchar)
3602 win_strncat_from_utf16be(struct archive_string *as, const void *_p,
3603 size_t bytes, struct archive_string_conv *sc)
3605 return (win_strncat_from_utf16(as, _p, bytes, sc, 1));
3609 win_strncat_from_utf16le(struct archive_string *as, const void *_p,
3610 size_t bytes, struct archive_string_conv *sc)
3612 return (win_strncat_from_utf16(as, _p, bytes, sc, 0));
3620 return (archive_be16dec(&d) == 1);
3624 * Convert a current locale string to UTF-16BE/LE and copy the result.
3625 * Return -1 if conversion fails.
3628 win_strncat_to_utf16(struct archive_string *as16, const void *_p,
3629 size_t length, struct archive_string_conv *sc, int bigendian)
3631 const char *s = (const char *)_p;
3633 size_t count, avail;
3635 if (archive_string_ensure(as16,
3636 as16->length + (length + 1) * 2) == NULL)
3639 u16 = as16->s + as16->length;
3640 avail = as16->buffer_length - 2;
3641 if (sc->from_cp == CP_C_LOCALE) {
3643 * "C" locale special process.
3646 while (count < length && *s) {
3648 archive_be16enc(u16, *s);
3650 archive_le16enc(u16, *s);
3655 as16->length += count << 1;
3656 as16->s[as16->length] = 0;
3657 as16->s[as16->length+1] = 0;
3661 count = MultiByteToWideChar(sc->from_cp,
3662 MB_PRECOMPOSED, s, (int)length, (LPWSTR)u16, (int)avail>>1);
3663 /* Exit loop if we succeeded */
3665 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3668 /* Expand buffer and try again */
3669 count = MultiByteToWideChar(sc->from_cp,
3670 MB_PRECOMPOSED, s, (int)length, NULL, 0);
3671 if (archive_string_ensure(as16, (count +1) * 2)
3674 u16 = as16->s + as16->length;
3675 avail = as16->buffer_length - 2;
3677 as16->length += count * 2;
3678 as16->s[as16->length] = 0;
3679 as16->s[as16->length+1] = 0;
3683 if (is_big_endian()) {
3686 uint16_t v = archive_be16dec(u16);
3687 archive_le16enc(u16, v);
3695 uint16_t v = archive_le16dec(u16);
3696 archive_be16enc(u16, v);
3706 win_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3707 size_t length, struct archive_string_conv *sc)
3709 return (win_strncat_to_utf16(as16, _p, length, sc, 1));
3713 win_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3714 size_t length, struct archive_string_conv *sc)
3716 return (win_strncat_to_utf16(as16, _p, length, sc, 0));
3719 #endif /* _WIN32 && !__CYGWIN__ */
3722 * Do the best effort for conversions.
3723 * We cannot handle UTF-16BE character-set without such iconv,
3724 * but there is a chance if a string consists just ASCII code or
3725 * a current locale is UTF-8.
3729 * Convert a UTF-16BE string to current locale and copy the result.
3730 * Return -1 if conversion fails.
3733 best_effort_strncat_from_utf16(struct archive_string *as, const void *_p,
3734 size_t bytes, struct archive_string_conv *sc, int be)
3736 const char *utf16 = (const char *)_p;
3741 (void)sc; /* UNUSED */
3743 * Other case, we should do the best effort.
3744 * If all character are ASCII(<0x7f), we can convert it.
3745 * if not , we set a alternative character and return -1.
3748 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3750 mbs = as->s + as->length;
3752 while ((n = utf16_to_unicode(&uc, utf16, bytes, be)) != 0) {
3761 /* We cannot handle it. */
3767 as->length = mbs - as->s;
3768 as->s[as->length] = '\0';
3773 best_effort_strncat_from_utf16be(struct archive_string *as, const void *_p,
3774 size_t bytes, struct archive_string_conv *sc)
3776 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 1));
3780 best_effort_strncat_from_utf16le(struct archive_string *as, const void *_p,
3781 size_t bytes, struct archive_string_conv *sc)
3783 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 0));
3787 * Convert a current locale string to UTF-16BE/LE and copy the result.
3788 * Return -1 if conversion fails.
3791 best_effort_strncat_to_utf16(struct archive_string *as16, const void *_p,
3792 size_t length, struct archive_string_conv *sc, int bigendian)
3794 const char *s = (const char *)_p;
3799 (void)sc; /* UNUSED */
3801 * Other case, we should do the best effort.
3802 * If all character are ASCII(<0x7f), we can convert it.
3803 * if not , we set a alternative character and return -1.
3808 if (archive_string_ensure(as16,
3809 as16->length + (length + 1) * 2) == NULL)
3812 utf16 = as16->s + as16->length;
3813 while (remaining--) {
3816 /* We cannot handle it. */
3821 archive_be16enc(utf16, c);
3823 archive_le16enc(utf16, c);
3826 as16->length = utf16 - as16->s;
3827 as16->s[as16->length] = 0;
3828 as16->s[as16->length+1] = 0;
3833 best_effort_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3834 size_t length, struct archive_string_conv *sc)
3836 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 1));
3840 best_effort_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3841 size_t length, struct archive_string_conv *sc)
3843 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 0));
3848 * Multistring operations.
3852 archive_mstring_clean(struct archive_mstring *aes)
3854 archive_wstring_free(&(aes->aes_wcs));
3855 archive_string_free(&(aes->aes_mbs));
3856 archive_string_free(&(aes->aes_utf8));
3857 archive_string_free(&(aes->aes_mbs_in_locale));
3862 archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src)
3864 dest->aes_set = src->aes_set;
3865 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
3866 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
3867 archive_wstring_copy(&(dest->aes_wcs), &(src->aes_wcs));
3871 archive_mstring_get_utf8(struct archive *a, struct archive_mstring *aes,
3874 struct archive_string_conv *sc;
3877 /* If we already have a UTF8 form, return that immediately. */
3878 if (aes->aes_set & AES_SET_UTF8) {
3879 *p = aes->aes_utf8.s;
3884 if (aes->aes_set & AES_SET_MBS) {
3885 sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
3887 return (-1);/* Couldn't allocate memory for sc. */
3888 r = archive_strncpy_l(&(aes->aes_utf8), aes->aes_mbs.s,
3889 aes->aes_mbs.length, sc);
3891 free_sconv_object(sc);
3893 aes->aes_set |= AES_SET_UTF8;
3894 *p = aes->aes_utf8.s;
3895 return (0);/* success. */
3897 return (-1);/* failure. */
3899 return (0);/* success. */
3903 archive_mstring_get_mbs(struct archive *a, struct archive_mstring *aes,
3908 (void)a; /* UNUSED */
3909 /* If we already have an MBS form, return that immediately. */
3910 if (aes->aes_set & AES_SET_MBS) {
3911 *p = aes->aes_mbs.s;
3916 /* If there's a WCS form, try converting with the native locale. */
3917 if (aes->aes_set & AES_SET_WCS) {
3918 archive_string_empty(&(aes->aes_mbs));
3919 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3920 aes->aes_wcs.s, aes->aes_wcs.length);
3921 *p = aes->aes_mbs.s;
3923 aes->aes_set |= AES_SET_MBS;
3930 * Only a UTF-8 form cannot avail because its conversion already
3931 * failed at archive_mstring_update_utf8().
3937 archive_mstring_get_wcs(struct archive *a, struct archive_mstring *aes,
3942 (void)a;/* UNUSED */
3943 /* Return WCS form if we already have it. */
3944 if (aes->aes_set & AES_SET_WCS) {
3945 *wp = aes->aes_wcs.s;
3950 /* Try converting MBS to WCS using native locale. */
3951 if (aes->aes_set & AES_SET_MBS) {
3952 archive_wstring_empty(&(aes->aes_wcs));
3953 r = archive_wstring_append_from_mbs(&(aes->aes_wcs),
3954 aes->aes_mbs.s, aes->aes_mbs.length);
3956 aes->aes_set |= AES_SET_WCS;
3957 *wp = aes->aes_wcs.s;
3959 ret = -1;/* failure. */
3965 archive_mstring_get_mbs_l(struct archive_mstring *aes,
3966 const char **p, size_t *length, struct archive_string_conv *sc)
3970 #if defined(_WIN32) && !defined(__CYGWIN__)
3972 * Internationalization programming on Windows must use Wide
3973 * characters because Windows platform cannot make locale UTF-8.
3975 if (sc != NULL && (aes->aes_set & AES_SET_WCS) != 0) {
3976 archive_string_empty(&(aes->aes_mbs_in_locale));
3977 r = archive_string_append_from_wcs_in_codepage(
3978 &(aes->aes_mbs_in_locale), aes->aes_wcs.s,
3979 aes->aes_wcs.length, sc);
3981 *p = aes->aes_mbs_in_locale.s;
3983 *length = aes->aes_mbs_in_locale.length;
3985 } else if (errno == ENOMEM)
3992 /* If there is not an MBS form but is a WCS form, try converting
3993 * with the native locale to be used for translating it to specified
3995 if ((aes->aes_set & AES_SET_MBS) == 0 &&
3996 (aes->aes_set & AES_SET_WCS) != 0) {
3997 archive_string_empty(&(aes->aes_mbs));
3998 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3999 aes->aes_wcs.s, aes->aes_wcs.length);
4001 aes->aes_set |= AES_SET_MBS;
4002 else if (errno == ENOMEM)
4007 /* If we already have an MBS form, use it to be translated to
4008 * specified character-set. */
4009 if (aes->aes_set & AES_SET_MBS) {
4011 /* Conversion is unneeded. */
4012 *p = aes->aes_mbs.s;
4014 *length = aes->aes_mbs.length;
4017 ret = archive_strncpy_l(&(aes->aes_mbs_in_locale),
4018 aes->aes_mbs.s, aes->aes_mbs.length, sc);
4019 *p = aes->aes_mbs_in_locale.s;
4021 *length = aes->aes_mbs_in_locale.length;
4031 archive_mstring_copy_mbs(struct archive_mstring *aes, const char *mbs)
4037 return (archive_mstring_copy_mbs_len(aes, mbs, strlen(mbs)));
4041 archive_mstring_copy_mbs_len(struct archive_mstring *aes, const char *mbs,
4048 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4049 archive_strncpy(&(aes->aes_mbs), mbs, len);
4050 archive_string_empty(&(aes->aes_utf8));
4051 archive_wstring_empty(&(aes->aes_wcs));
4056 archive_mstring_copy_wcs(struct archive_mstring *aes, const wchar_t *wcs)
4058 return archive_mstring_copy_wcs_len(aes, wcs,
4059 wcs == NULL ? 0 : wcslen(wcs));
4063 archive_mstring_copy_utf8(struct archive_mstring *aes, const char *utf8)
4069 aes->aes_set = AES_SET_UTF8;
4070 archive_string_empty(&(aes->aes_mbs));
4071 archive_string_empty(&(aes->aes_wcs));
4072 archive_strncpy(&(aes->aes_utf8), utf8, strlen(utf8));
4073 return (int)strlen(utf8);
4077 archive_mstring_copy_wcs_len(struct archive_mstring *aes, const wchar_t *wcs,
4084 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
4085 archive_string_empty(&(aes->aes_mbs));
4086 archive_string_empty(&(aes->aes_utf8));
4087 archive_wstrncpy(&(aes->aes_wcs), wcs, len);
4092 archive_mstring_copy_mbs_len_l(struct archive_mstring *aes,
4093 const char *mbs, size_t len, struct archive_string_conv *sc)
4101 archive_string_empty(&(aes->aes_mbs));
4102 archive_wstring_empty(&(aes->aes_wcs));
4103 archive_string_empty(&(aes->aes_utf8));
4104 #if defined(_WIN32) && !defined(__CYGWIN__)
4106 * Internationalization programming on Windows must use Wide
4107 * characters because Windows platform cannot make locale UTF-8.
4110 if (archive_string_append(&(aes->aes_mbs),
4111 mbs, mbsnbytes(mbs, len)) == NULL) {
4115 aes->aes_set = AES_SET_MBS;
4118 #if defined(HAVE_ICONV)
4119 } else if (sc != NULL && sc->cd_w != (iconv_t)-1) {
4121 * This case happens only when MultiByteToWideChar() cannot
4122 * handle sc->from_cp, and we have to iconv in order to
4123 * translate character-set to wchar_t,UTF-16.
4125 iconv_t cd = sc->cd;
4130 * Translate multi-bytes from some character-set to UTF-8.
4133 r = archive_strncpy_l(&(aes->aes_utf8), mbs, len, sc);
4139 aes->aes_set = AES_SET_UTF8;
4142 * Append the UTF-8 string into wstring.
4145 sc->flag &= ~(SCONV_NORMALIZATION_C
4146 | SCONV_TO_UTF16| SCONV_FROM_UTF16);
4147 from_cp = sc->from_cp;
4148 sc->from_cp = CP_UTF8;
4149 r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4150 aes->aes_utf8.s, aes->aes_utf8.length, sc);
4152 sc->from_cp = from_cp;
4154 aes->aes_set |= AES_SET_WCS;
4157 r = archive_wstring_append_from_mbs_in_codepage(
4158 &(aes->aes_wcs), mbs, len, sc);
4160 aes->aes_set = AES_SET_WCS;
4165 r = archive_strncpy_l(&(aes->aes_mbs), mbs, len, sc);
4167 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4175 * The 'update' form tries to proactively update all forms of
4176 * this string (WCS and MBS) and returns an error if any of
4177 * them fail. This is used by the 'pax' handler, for instance,
4178 * to detect and report character-conversion failures early while
4179 * still allowing clients to get potentially useful values from
4180 * the more tolerant lazy conversions. (get_mbs and get_wcs will
4181 * strive to give the user something useful, so you can get hopefully
4182 * usable values even if some of the character conversions are failing.)
4185 archive_mstring_update_utf8(struct archive *a, struct archive_mstring *aes,
4188 struct archive_string_conv *sc;
4193 return (0); /* Succeeded in clearing everything. */
4196 /* Save the UTF8 string. */
4197 archive_strcpy(&(aes->aes_utf8), utf8);
4199 /* Empty the mbs and wcs strings. */
4200 archive_string_empty(&(aes->aes_mbs));
4201 archive_wstring_empty(&(aes->aes_wcs));
4203 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
4205 /* Try converting UTF-8 to MBS, return false on failure. */
4206 sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4208 return (-1);/* Couldn't allocate memory for sc. */
4209 r = archive_strcpy_l(&(aes->aes_mbs), utf8, sc);
4211 free_sconv_object(sc);
4214 aes->aes_set = AES_SET_UTF8 | AES_SET_MBS; /* Both UTF8 and MBS set. */
4216 /* Try converting MBS to WCS, return false on failure. */
4217 if (archive_wstring_append_from_mbs(&(aes->aes_wcs), aes->aes_mbs.s,
4218 aes->aes_mbs.length))
4220 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
4222 /* All conversions succeeded. */