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))
78 struct archive_string_conv {
79 struct archive_string_conv *next;
84 /* Set 1 if from_charset and to_charset are the same. */
87 #define SCONV_TO_CHARSET 1 /* MBS is being converted to specified
89 #define SCONV_FROM_CHARSET (1<<1) /* MBS is being converted from
90 * specified charset. */
91 #define SCONV_BEST_EFFORT (1<<2) /* Copy at least ASCII code. */
92 #define SCONV_WIN_CP (1<<3) /* Use Windows API for converting
94 #define SCONV_UTF8_LIBARCHIVE_2 (1<<4) /* Incorrect UTF-8 made by libarchive
95 * 2.x in the wrong assumption. */
96 #define SCONV_NORMALIZATION_C (1<<6) /* Need normalization to be Form C.
97 * Before UTF-8 characters are actually
99 #define SCONV_NORMALIZATION_D (1<<7) /* Need normalization to be Form D.
100 * Before UTF-8 characters are actually
102 * Currently this only for MAC OS X. */
103 #define SCONV_TO_UTF8 (1<<8) /* "to charset" side is UTF-8. */
104 #define SCONV_FROM_UTF8 (1<<9) /* "from charset" side is UTF-8. */
105 #define SCONV_TO_UTF16BE (1<<10) /* "to charset" side is UTF-16BE. */
106 #define SCONV_FROM_UTF16BE (1<<11) /* "from charset" side is UTF-16BE. */
107 #define SCONV_TO_UTF16LE (1<<12) /* "to charset" side is UTF-16LE. */
108 #define SCONV_FROM_UTF16LE (1<<13) /* "from charset" side is UTF-16LE. */
109 #define SCONV_TO_UTF16 (SCONV_TO_UTF16BE | SCONV_TO_UTF16LE)
110 #define SCONV_FROM_UTF16 (SCONV_FROM_UTF16BE | SCONV_FROM_UTF16LE)
114 iconv_t cd_w;/* Use at archive_mstring on
117 /* A temporary buffer for normalization. */
118 struct archive_string utftmp;
119 int (*converter[2])(struct archive_string *, const void *, size_t,
120 struct archive_string_conv *);
124 #define CP_C_LOCALE 0 /* "C" locale only for this file. */
125 #define CP_UTF16LE 1200
126 #define CP_UTF16BE 1201
128 #define IS_HIGH_SURROGATE_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDBFF)
129 #define IS_LOW_SURROGATE_LA(uc) ((uc) >= 0xDC00 && (uc) <= 0xDFFF)
130 #define IS_SURROGATE_PAIR_LA(uc) ((uc) >= 0xD800 && (uc) <= 0xDFFF)
131 #define UNICODE_MAX 0x10FFFF
132 #define UNICODE_R_CHAR 0xFFFD /* Replacement character. */
133 /* Set U+FFFD(Replacement character) in UTF-8. */
134 static const char utf8_replacement_char[] = {0xef, 0xbf, 0xbd};
136 static struct archive_string_conv *find_sconv_object(struct archive *,
137 const char *, const char *);
138 static void add_sconv_object(struct archive *, struct archive_string_conv *);
139 static struct archive_string_conv *create_sconv_object(const char *,
140 const char *, unsigned, int);
141 static void free_sconv_object(struct archive_string_conv *);
142 static struct archive_string_conv *get_sconv_object(struct archive *,
143 const char *, const char *, int);
144 static unsigned make_codepage_from_charset(const char *);
145 static unsigned get_current_codepage(void);
146 static unsigned get_current_oemcp(void);
147 static size_t mbsnbytes(const void *, size_t);
148 static size_t utf16nbytes(const void *, size_t);
149 #if defined(_WIN32) && !defined(__CYGWIN__)
150 static int archive_wstring_append_from_mbs_in_codepage(
151 struct archive_wstring *, const char *, size_t,
152 struct archive_string_conv *);
153 static int archive_string_append_from_wcs_in_codepage(struct archive_string *,
154 const wchar_t *, size_t, struct archive_string_conv *);
155 static int is_big_endian(void);
156 static int strncat_in_codepage(struct archive_string *, const void *,
157 size_t, struct archive_string_conv *);
158 static int win_strncat_from_utf16be(struct archive_string *, const void *,
159 size_t, struct archive_string_conv *);
160 static int win_strncat_from_utf16le(struct archive_string *, const void *,
161 size_t, struct archive_string_conv *);
162 static int win_strncat_to_utf16be(struct archive_string *, const void *,
163 size_t, struct archive_string_conv *);
164 static int win_strncat_to_utf16le(struct archive_string *, const void *,
165 size_t, struct archive_string_conv *);
167 static int best_effort_strncat_from_utf16be(struct archive_string *,
168 const void *, size_t, struct archive_string_conv *);
169 static int best_effort_strncat_from_utf16le(struct archive_string *,
170 const void *, size_t, struct archive_string_conv *);
171 static int best_effort_strncat_to_utf16be(struct archive_string *,
172 const void *, size_t, struct archive_string_conv *);
173 static int best_effort_strncat_to_utf16le(struct archive_string *,
174 const void *, size_t, struct archive_string_conv *);
175 #if defined(HAVE_ICONV)
176 static int iconv_strncat_in_locale(struct archive_string *, const void *,
177 size_t, struct archive_string_conv *);
179 static int best_effort_strncat_in_locale(struct archive_string *,
180 const void *, size_t, struct archive_string_conv *);
181 static int _utf8_to_unicode(uint32_t *, const char *, size_t);
182 static int utf8_to_unicode(uint32_t *, const char *, size_t);
183 static inline uint32_t combine_surrogate_pair(uint32_t, uint32_t);
184 static int cesu8_to_unicode(uint32_t *, const char *, size_t);
185 static size_t unicode_to_utf8(char *, size_t, uint32_t);
186 static int utf16_to_unicode(uint32_t *, const char *, size_t, int);
187 static size_t unicode_to_utf16be(char *, size_t, uint32_t);
188 static size_t unicode_to_utf16le(char *, size_t, uint32_t);
189 static int strncat_from_utf8_libarchive2(struct archive_string *,
190 const void *, size_t, struct archive_string_conv *);
191 static int strncat_from_utf8_to_utf8(struct archive_string *, const void *,
192 size_t, struct archive_string_conv *);
193 static int archive_string_normalize_C(struct archive_string *, const void *,
194 size_t, struct archive_string_conv *);
195 static int archive_string_normalize_D(struct archive_string *, const void *,
196 size_t, struct archive_string_conv *);
197 static int archive_string_append_unicode(struct archive_string *,
198 const void *, size_t, struct archive_string_conv *);
200 static struct archive_string *
201 archive_string_append(struct archive_string *as, const char *p, size_t s)
203 if (archive_string_ensure(as, as->length + s + 1) == NULL)
205 memmove(as->s + as->length, p, s);
207 as->s[as->length] = 0;
211 static struct archive_wstring *
212 archive_wstring_append(struct archive_wstring *as, const wchar_t *p, size_t s)
214 if (archive_wstring_ensure(as, as->length + s + 1) == NULL)
216 wmemmove(as->s + as->length, p, s);
218 as->s[as->length] = 0;
223 archive_string_concat(struct archive_string *dest, struct archive_string *src)
225 if (archive_string_append(dest, src->s, src->length) == NULL)
226 __archive_errx(1, "Out of memory");
230 archive_wstring_concat(struct archive_wstring *dest,
231 struct archive_wstring *src)
233 if (archive_wstring_append(dest, src->s, src->length) == NULL)
234 __archive_errx(1, "Out of memory");
238 archive_string_free(struct archive_string *as)
241 as->buffer_length = 0;
247 archive_wstring_free(struct archive_wstring *as)
250 as->buffer_length = 0;
255 struct archive_wstring *
256 archive_wstring_ensure(struct archive_wstring *as, size_t s)
258 return (struct archive_wstring *)
259 archive_string_ensure((struct archive_string *)as,
260 s * sizeof(wchar_t));
263 /* Returns NULL on any allocation failure. */
264 struct archive_string *
265 archive_string_ensure(struct archive_string *as, size_t s)
270 /* If buffer is already big enough, don't reallocate. */
271 if (as->s && (s <= as->buffer_length))
275 * Growing the buffer at least exponentially ensures that
276 * append operations are always linear in the number of
277 * characters appended. Using a smaller growth rate for
278 * larger buffers reduces memory waste somewhat at the cost of
279 * a larger constant factor.
281 if (as->buffer_length < 32)
282 /* Start with a minimum 32-character buffer. */
284 else if (as->buffer_length < 8192)
285 /* Buffers under 8k are doubled for speed. */
286 new_length = as->buffer_length + as->buffer_length;
288 /* Buffers 8k and over grow by at least 25% each time. */
289 new_length = as->buffer_length + as->buffer_length / 4;
290 /* Be safe: If size wraps, fail. */
291 if (new_length < as->buffer_length) {
292 /* On failure, wipe the string and return NULL. */
293 archive_string_free(as);
294 errno = ENOMEM;/* Make sure errno has ENOMEM. */
299 * The computation above is a lower limit to how much we'll
300 * grow the buffer. In any case, we have to grow it enough to
305 /* Now we can reallocate the buffer. */
306 p = (char *)realloc(as->s, new_length);
308 /* On failure, wipe the string and return NULL. */
309 archive_string_free(as);
310 errno = ENOMEM;/* Make sure errno has ENOMEM. */
315 as->buffer_length = new_length;
320 * TODO: See if there's a way to avoid scanning
321 * the source string twice. Then test to see
322 * if it actually helps (remember that we're almost
323 * always called with pretty short arguments, so
324 * such an optimization might not help).
326 struct archive_string *
327 archive_strncat(struct archive_string *as, const void *_p, size_t n)
332 p = (const char *)_p;
334 /* Like strlen(p), except won't examine positions beyond p[n]. */
337 while (s < n && *pp) {
341 if ((as = archive_string_append(as, p, s)) == NULL)
342 __archive_errx(1, "Out of memory");
346 struct archive_wstring *
347 archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
352 /* Like strlen(p), except won't examine positions beyond p[n]. */
355 while (s < n && *pp) {
359 if ((as = archive_wstring_append(as, p, s)) == NULL)
360 __archive_errx(1, "Out of memory");
364 struct archive_string *
365 archive_strcat(struct archive_string *as, const void *p)
367 /* strcat is just strncat without an effective limit.
368 * Assert that we'll never get called with a source
370 * TODO: Review all uses of strcat in the source
371 * and try to replace them with strncat().
373 return archive_strncat(as, p, 0x1000000);
376 struct archive_wstring *
377 archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
380 return archive_wstrncat(as, p, 0x1000000);
383 struct archive_string *
384 archive_strappend_char(struct archive_string *as, char c)
386 if ((as = archive_string_append(as, &c, 1)) == NULL)
387 __archive_errx(1, "Out of memory");
391 struct archive_wstring *
392 archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
394 if ((as = archive_wstring_append(as, &c, 1)) == NULL)
395 __archive_errx(1, "Out of memory");
400 * Get the "current character set" name to use with iconv.
401 * On FreeBSD, the empty character set name "" chooses
402 * the correct character encoding for the current locale,
403 * so this isn't necessary.
404 * But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
405 * on that system, we have to explicitly call nl_langinfo()
406 * to get the right name. Not sure about other platforms.
408 * NOTE: GNU libiconv does not recognize the character-set name
409 * which some platform nl_langinfo(CODESET) returns, so we should
410 * use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
413 default_iconv_charset(const char *charset) {
414 if (charset != NULL && charset[0] != '\0')
416 #if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
417 /* locale_charset() is broken on Mac OS */
418 return locale_charset();
419 #elif HAVE_NL_LANGINFO
420 return nl_langinfo(CODESET);
426 #if defined(_WIN32) && !defined(__CYGWIN__)
429 * Convert MBS to WCS.
430 * Note: returns -1 if conversion fails.
433 archive_wstring_append_from_mbs(struct archive_wstring *dest,
434 const char *p, size_t len)
436 return archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
440 archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
441 const char *s, size_t length, struct archive_string_conv *sc)
447 from_cp = sc->from_cp;
449 from_cp = get_current_codepage();
451 if (from_cp == CP_C_LOCALE) {
453 * "C" locale special process.
456 const unsigned char *mp;
458 if (NULL == archive_wstring_ensure(dest,
459 dest->length + length + 1))
462 ws = dest->s + dest->length;
463 mp = (const unsigned char *)s;
465 while (count < (int)length && *mp) {
466 *ws++ = (wchar_t)*mp++;
469 } else if (sc != NULL &&
470 (sc->flag & (SCONV_NORMALIZATION_C | SCONV_NORMALIZATION_D))) {
472 * Normalize UTF-8 and UTF-16BE and convert it directly
473 * to UTF-16 as wchar_t.
475 struct archive_string u16;
476 int saved_flag = sc->flag;/* save current flag. */
479 sc->flag |= SCONV_TO_UTF16BE;
481 sc->flag |= SCONV_TO_UTF16LE;
483 if (sc->flag & SCONV_FROM_UTF16) {
485 * UTF-16BE/LE NFD ===> UTF-16 NFC
486 * UTF-16BE/LE NFC ===> UTF-16 NFD
488 count = (int)utf16nbytes(s, length);
491 * UTF-8 NFD ===> UTF-16 NFC
492 * UTF-8 NFC ===> UTF-16 NFD
494 count = (int)mbsnbytes(s, length);
496 u16.s = (char *)dest->s;
497 u16.length = dest->length << 1;;
498 u16.buffer_length = dest->buffer_length;
499 if (sc->flag & SCONV_NORMALIZATION_C)
500 ret = archive_string_normalize_C(&u16, s, count, sc);
502 ret = archive_string_normalize_D(&u16, s, count, sc);
503 dest->s = (wchar_t *)u16.s;
504 dest->length = u16.length >> 1;
505 dest->buffer_length = u16.buffer_length;
506 sc->flag = saved_flag;/* restore the saved flag. */
508 } else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
509 count = (int)utf16nbytes(s, length);
510 count >>= 1; /* to be WCS length */
511 /* Allocate memory for WCS. */
512 if (NULL == archive_wstring_ensure(dest,
513 dest->length + count + 1))
515 wmemcpy(dest->s + dest->length, (const wchar_t *)s, count);
516 if ((sc->flag & SCONV_FROM_UTF16BE) && !is_big_endian()) {
517 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
519 for (b = 0; b < count; b++) {
520 uint16_t val = archive_le16dec(u16+b);
521 archive_be16enc(u16+b, val);
523 } else if ((sc->flag & SCONV_FROM_UTF16LE) && is_big_endian()) {
524 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
526 for (b = 0; b < count; b++) {
527 uint16_t val = archive_be16dec(u16+b);
528 archive_le16enc(u16+b, val);
537 else if (sc->flag & SCONV_FROM_CHARSET) {
538 /* Do not trust the length which comes from
539 * an archive file. */
540 length = mbsnbytes(s, length);
543 mbflag = MB_PRECOMPOSED;
545 buffsize = dest->length + length + 1;
547 /* Allocate memory for WCS. */
548 if (NULL == archive_wstring_ensure(dest, buffsize))
550 /* Convert MBS to WCS. */
551 count = MultiByteToWideChar(from_cp,
552 mbflag, s, (int)length, dest->s + dest->length,
553 (int)(dest->buffer_length >> 1) -1);
555 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
556 /* Expand the WCS buffer. */
557 buffsize = dest->buffer_length << 1;
560 if (count == 0 && length != 0)
565 dest->length += count;
566 dest->s[dest->length] = L'\0';
573 * Convert MBS to WCS.
574 * Note: returns -1 if conversion fails.
577 archive_wstring_append_from_mbs(struct archive_wstring *dest,
578 const char *p, size_t len)
583 * No single byte will be more than one wide character,
584 * so this length estimate will always be big enough.
586 size_t wcs_length = len;
587 size_t mbs_length = len;
591 mbstate_t shift_state;
593 memset(&shift_state, 0, sizeof(shift_state));
595 if (NULL == archive_wstring_ensure(dest, dest->length + wcs_length + 1))
597 wcs = dest->s + dest->length;
599 * We cannot use mbsrtowcs/mbstowcs here because those may convert
600 * extra MBS when strlen(p) > len and one wide character consis of
603 while (*mbs && mbs_length > 0) {
604 if (wcs_length == 0) {
605 dest->length = wcs - dest->s;
606 dest->s[dest->length] = L'\0';
607 wcs_length = mbs_length;
608 if (NULL == archive_wstring_ensure(dest,
609 dest->length + wcs_length + 1))
611 wcs = dest->s + dest->length;
614 r = mbrtowc(wcs, mbs, wcs_length, &shift_state);
616 r = mbtowc(wcs, mbs, wcs_length);
618 if (r == (size_t)-1 || r == (size_t)-2) {
620 if (errno == EILSEQ) {
627 if (r == 0 || r > mbs_length)
634 dest->length = wcs - dest->s;
635 dest->s[dest->length] = L'\0';
641 #if defined(_WIN32) && !defined(__CYGWIN__)
645 * Note: returns -1 if conversion fails.
647 * Win32 builds use WideCharToMultiByte from the Windows API.
648 * (Maybe Cygwin should too? WideCharToMultiByte will know a
649 * lot more about local character encodings than the wcrtomb()
650 * wrapper is going to know.)
653 archive_string_append_from_wcs(struct archive_string *as,
654 const wchar_t *w, size_t len)
656 return archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
660 archive_string_append_from_wcs_in_codepage(struct archive_string *as,
661 const wchar_t *ws, size_t len, struct archive_string_conv *sc)
663 BOOL defchar_used, *dp;
666 int wslen = (int)len;
671 to_cp = get_current_codepage();
673 if (to_cp == CP_C_LOCALE) {
675 * "C" locale special process.
677 const wchar_t *wp = ws;
680 if (NULL == archive_string_ensure(as,
681 as->length + wslen +1))
683 p = as->s + as->length;
686 while (count < wslen && *wp) {
695 } else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
699 archive_string_ensure(as, as->length + len * 2 + 2))
701 u16 = (uint16_t *)(as->s + as->length);
704 if (sc->flag & SCONV_TO_UTF16BE) {
705 while (count < (int)len && *ws) {
706 archive_be16enc(u16+count, *ws);
711 while (count < (int)len && *ws) {
712 archive_le16enc(u16+count, *ws);
717 count <<= 1; /* to be byte size */
719 /* Make sure the MBS buffer has plenty to set. */
721 archive_string_ensure(as, as->length + len * 2 + 1))
725 if (to_cp == CP_UTF8 || sc == NULL)
729 count = WideCharToMultiByte(to_cp, 0, ws, wslen,
730 as->s + as->length, (int)as->buffer_length-1, NULL, dp);
732 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
733 /* Expand the MBS buffer and retry. */
734 if (NULL == archive_string_ensure(as,
735 as->buffer_length + len))
745 as->s[as->length] = '\0';
746 return (defchar_used?-1:ret);
749 #elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
752 * Translates a wide character string into current locale character set
753 * and appends to the archive_string. Note: returns -1 if conversion
757 archive_string_append_from_wcs(struct archive_string *as,
758 const wchar_t *w, size_t len)
760 /* We cannot use the standard wcstombs() here because it
761 * cannot tell us how big the output buffer should be. So
762 * I've built a loop around wcrtomb() or wctomb() that
763 * converts a character at a time and resizes the string as
764 * needed. We prefer wcrtomb() when it's available because
765 * it's thread-safe. */
770 mbstate_t shift_state;
772 memset(&shift_state, 0, sizeof(shift_state));
774 /* Clear the shift state before starting. */
778 * Allocate buffer for MBS.
779 * We need this allocation here since it is possible that
780 * as->s is still NULL.
782 if (archive_string_ensure(as, as->length + len + 1) == NULL)
785 p = as->s + as->length;
786 end = as->s + as->buffer_length - MB_CUR_MAX -1;
787 while (*w != L'\0' && len > 0) {
789 as->length = p - as->s;
790 as->s[as->length] = '\0';
791 /* Re-allocate buffer for MBS. */
792 if (archive_string_ensure(as,
793 as->length + len * 2 + 1) == NULL)
795 p = as->s + as->length;
796 end = as->s + as->buffer_length - MB_CUR_MAX -1;
799 n = wcrtomb(p, *w++, &shift_state);
804 if (errno == EILSEQ) {
805 /* Skip an illegal wide char. */
816 as->length = p - as->s;
817 as->s[as->length] = '\0';
821 #else /* HAVE_WCTOMB || HAVE_WCRTOMB */
824 * TODO: Test if __STDC_ISO_10646__ is defined.
825 * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
826 * one character at a time. If a non-Windows platform doesn't have
827 * either of these, fall back to the built-in UTF8 conversion.
830 archive_string_append_from_wcs(struct archive_string *as,
831 const wchar_t *w, size_t len)
833 (void)as;/* UNUSED */
835 (void)len;/* UNUSED */
840 #endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
843 * Find a string conversion object by a pair of 'from' charset name
844 * and 'to' charset name from an archive object.
845 * Return NULL if not found.
847 static struct archive_string_conv *
848 find_sconv_object(struct archive *a, const char *fc, const char *tc)
850 struct archive_string_conv *sc;
855 for (sc = a->sconv; sc != NULL; sc = sc->next) {
856 if (strcmp(sc->from_charset, fc) == 0 &&
857 strcmp(sc->to_charset, tc) == 0)
864 * Register a string object to an archive object.
867 add_sconv_object(struct archive *a, struct archive_string_conv *sc)
869 struct archive_string_conv **psc;
871 /* Add a new sconv to sconv list. */
874 psc = &((*psc)->next);
879 add_converter(struct archive_string_conv *sc, int (*converter)
880 (struct archive_string *, const void *, size_t,
881 struct archive_string_conv *))
883 if (sc == NULL || sc->nconverter >= 2)
884 __archive_errx(1, "Programing error");
885 sc->converter[sc->nconverter++] = converter;
889 setup_converter(struct archive_string_conv *sc)
896 * Perform special sequence for the incorrect UTF-8 filenames
897 * made by libarchive2.x.
899 if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
900 add_converter(sc, strncat_from_utf8_libarchive2);
905 * Convert a string to UTF-16BE/LE.
907 if (sc->flag & SCONV_TO_UTF16) {
909 * If the current locale is UTF-8, we can translate
910 * a UTF-8 string into a UTF-16BE string.
912 if (sc->flag & SCONV_FROM_UTF8) {
913 add_converter(sc, archive_string_append_unicode);
917 #if defined(_WIN32) && !defined(__CYGWIN__)
918 if (sc->flag & SCONV_WIN_CP) {
919 if (sc->flag & SCONV_TO_UTF16BE)
920 add_converter(sc, win_strncat_to_utf16be);
922 add_converter(sc, win_strncat_to_utf16le);
927 #if defined(HAVE_ICONV)
928 if (sc->cd != (iconv_t)-1) {
929 add_converter(sc, iconv_strncat_in_locale);
934 if (sc->flag & SCONV_BEST_EFFORT) {
935 if (sc->flag & SCONV_TO_UTF16BE)
937 best_effort_strncat_to_utf16be);
940 best_effort_strncat_to_utf16le);
942 /* Make sure we have no converter. */
948 * Convert a string from UTF-16BE/LE.
950 if (sc->flag & SCONV_FROM_UTF16) {
952 * At least we should normalize a UTF-16BE string.
954 if (sc->flag & SCONV_NORMALIZATION_D)
955 add_converter(sc,archive_string_normalize_D);
956 else if (sc->flag & SCONV_NORMALIZATION_C)
957 add_converter(sc, archive_string_normalize_C);
959 if (sc->flag & SCONV_TO_UTF8) {
961 * If the current locale is UTF-8, we can translate
962 * a UTF-16BE/LE string into a UTF-8 string directly.
965 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
967 archive_string_append_unicode);
971 #if defined(_WIN32) && !defined(__CYGWIN__)
972 if (sc->flag & SCONV_WIN_CP) {
973 if (sc->flag & SCONV_FROM_UTF16BE)
974 add_converter(sc, win_strncat_from_utf16be);
976 add_converter(sc, win_strncat_from_utf16le);
981 #if defined(HAVE_ICONV)
982 if (sc->cd != (iconv_t)-1) {
983 add_converter(sc, iconv_strncat_in_locale);
988 if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
989 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
990 add_converter(sc, best_effort_strncat_from_utf16be);
991 else if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
992 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
993 add_converter(sc, best_effort_strncat_from_utf16le);
995 /* Make sure we have no converter. */
1000 if (sc->flag & SCONV_FROM_UTF8) {
1002 * At least we should normalize a UTF-8 string.
1004 if (sc->flag & SCONV_NORMALIZATION_D)
1005 add_converter(sc,archive_string_normalize_D);
1006 else if (sc->flag & SCONV_NORMALIZATION_C)
1007 add_converter(sc, archive_string_normalize_C);
1010 * Copy UTF-8 string with a check of CESU-8.
1011 * Apparently, iconv does not check surrogate pairs in UTF-8
1012 * when both from-charset and to-charset are UTF-8, and then
1013 * we use our UTF-8 copy code.
1015 if (sc->flag & SCONV_TO_UTF8) {
1017 * If the current locale is UTF-8, we can translate
1018 * a UTF-16BE string into a UTF-8 string directly.
1021 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
1022 add_converter(sc, strncat_from_utf8_to_utf8);
1027 #if defined(_WIN32) && !defined(__CYGWIN__)
1029 * On Windows we can use Windows API for a string conversion.
1031 if (sc->flag & SCONV_WIN_CP) {
1032 add_converter(sc, strncat_in_codepage);
1038 if (sc->cd != (iconv_t)-1) {
1039 add_converter(sc, iconv_strncat_in_locale);
1041 * iconv generally does not support UTF-8-MAC and so
1042 * we have to the output of iconv from NFC to NFD if
1045 if ((sc->flag & SCONV_FROM_CHARSET) &&
1046 (sc->flag & SCONV_TO_UTF8)) {
1047 if (sc->flag & SCONV_NORMALIZATION_D)
1048 add_converter(sc, archive_string_normalize_D);
1055 * Try conversion in the best effort or no conversion.
1057 if ((sc->flag & SCONV_BEST_EFFORT) || sc->same)
1058 add_converter(sc, best_effort_strncat_in_locale);
1060 /* Make sure we have no converter. */
1065 * Return canonicalized charset-name but this supports just UTF-8, UTF-16BE
1066 * and CP932 which are referenced in create_sconv_object().
1069 canonical_charset_name(const char *charset)
1075 if (charset == NULL || charset[0] == '\0'
1076 || strlen(charset) > 15)
1079 /* Copy name to uppercase. */
1084 if (c >= 'a' && c <= 'z')
1090 if (strcmp(cs, "UTF-8") == 0 ||
1091 strcmp(cs, "UTF8") == 0)
1093 if (strcmp(cs, "UTF-16BE") == 0 ||
1094 strcmp(cs, "UTF16BE") == 0)
1095 return ("UTF-16BE");
1096 if (strcmp(cs, "UTF-16LE") == 0 ||
1097 strcmp(cs, "UTF16LE") == 0)
1098 return ("UTF-16LE");
1099 if (strcmp(cs, "CP932") == 0)
1105 * Create a string conversion object.
1107 static struct archive_string_conv *
1108 create_sconv_object(const char *fc, const char *tc,
1109 unsigned current_codepage, int flag)
1111 struct archive_string_conv *sc;
1113 sc = calloc(1, sizeof(*sc));
1117 sc->from_charset = strdup(fc);
1118 if (sc->from_charset == NULL) {
1122 sc->to_charset = strdup(tc);
1123 if (sc->to_charset == NULL) {
1124 free(sc->from_charset);
1128 archive_string_init(&sc->utftmp);
1130 if (flag & SCONV_TO_CHARSET) {
1132 * Convert characters from the current locale charset to
1133 * a specified charset.
1135 sc->from_cp = current_codepage;
1136 sc->to_cp = make_codepage_from_charset(tc);
1137 #if defined(_WIN32) && !defined(__CYGWIN__)
1138 if (IsValidCodePage(sc->to_cp))
1139 flag |= SCONV_WIN_CP;
1141 } else if (flag & SCONV_FROM_CHARSET) {
1143 * Convert characters from a specified charset to
1144 * the current locale charset.
1146 sc->to_cp = current_codepage;
1147 sc->from_cp = make_codepage_from_charset(fc);
1148 #if defined(_WIN32) && !defined(__CYGWIN__)
1149 if (IsValidCodePage(sc->from_cp))
1150 flag |= SCONV_WIN_CP;
1155 * Check if "from charset" and "to charset" are the same.
1157 if (strcmp(fc, tc) == 0 ||
1158 (sc->from_cp != (unsigned)-1 && sc->from_cp == sc->to_cp))
1164 * Mark if "from charset" or "to charset" are UTF-8 or UTF-16BE/LE.
1166 if (strcmp(tc, "UTF-8") == 0)
1167 flag |= SCONV_TO_UTF8;
1168 else if (strcmp(tc, "UTF-16BE") == 0)
1169 flag |= SCONV_TO_UTF16BE;
1170 else if (strcmp(tc, "UTF-16LE") == 0)
1171 flag |= SCONV_TO_UTF16LE;
1172 if (strcmp(fc, "UTF-8") == 0)
1173 flag |= SCONV_FROM_UTF8;
1174 else if (strcmp(fc, "UTF-16BE") == 0)
1175 flag |= SCONV_FROM_UTF16BE;
1176 else if (strcmp(fc, "UTF-16LE") == 0)
1177 flag |= SCONV_FROM_UTF16LE;
1178 #if defined(_WIN32) && !defined(__CYGWIN__)
1179 if (sc->to_cp == CP_UTF8)
1180 flag |= SCONV_TO_UTF8;
1181 else if (sc->to_cp == CP_UTF16BE)
1182 flag |= SCONV_TO_UTF16BE | SCONV_WIN_CP;
1183 else if (sc->to_cp == CP_UTF16LE)
1184 flag |= SCONV_TO_UTF16LE | SCONV_WIN_CP;
1185 if (sc->from_cp == CP_UTF8)
1186 flag |= SCONV_FROM_UTF8;
1187 else if (sc->from_cp == CP_UTF16BE)
1188 flag |= SCONV_FROM_UTF16BE | SCONV_WIN_CP;
1189 else if (sc->from_cp == CP_UTF16LE)
1190 flag |= SCONV_FROM_UTF16LE | SCONV_WIN_CP;
1194 * Set a flag for Unicode NFD. Usually iconv cannot correctly
1195 * handle it. So we have to translate NFD characters to NFC ones
1196 * ourselves before iconv handles. Another reason is to prevent
1197 * that the same sight of two filenames, one is NFC and other
1198 * is NFD, would be in its directory.
1199 * On Mac OS X, although its filesystem layer automatically
1200 * convert filenames to NFD, it would be useful for filename
1201 * comparing to find out the same filenames that we normalize
1202 * that to be NFD ourselves.
1204 if ((flag & SCONV_FROM_CHARSET) &&
1205 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8))) {
1206 #if defined(__APPLE__)
1207 if (flag & SCONV_TO_UTF8)
1208 flag |= SCONV_NORMALIZATION_D;
1211 flag |= SCONV_NORMALIZATION_C;
1213 #if defined(__APPLE__)
1215 * In case writing an archive file, make sure that a filename
1216 * going to be passed to iconv is a Unicode NFC string since
1217 * a filename in HFS Plus filesystem is a Unicode NFD one and
1218 * iconv cannot handle it with "UTF-8" charset. It is simpler
1219 * than a use of "UTF-8-MAC" charset.
1221 if ((flag & SCONV_TO_CHARSET) &&
1222 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1223 !(flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1224 flag |= SCONV_NORMALIZATION_C;
1226 * In case reading an archive file. make sure that a filename
1227 * will be passed to users is a Unicode NFD string in order to
1228 * correctly compare the filename with other one which comes
1229 * from HFS Plus filesystem.
1231 if ((flag & SCONV_FROM_CHARSET) &&
1232 !(flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1233 (flag & SCONV_TO_UTF8))
1234 flag |= SCONV_NORMALIZATION_D;
1237 #if defined(HAVE_ICONV)
1238 sc->cd_w = (iconv_t)-1;
1240 * Create an iconv object.
1242 if (((flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) &&
1243 (flag & (SCONV_FROM_UTF8 | SCONV_FROM_UTF16))) ||
1244 (flag & SCONV_WIN_CP)) {
1245 /* This case we won't use iconv. */
1246 sc->cd = (iconv_t)-1;
1248 sc->cd = iconv_open(tc, fc);
1249 if (sc->cd == (iconv_t)-1 && (sc->flag & SCONV_BEST_EFFORT)) {
1251 * Unfortunaly, all of iconv implements do support
1252 * "CP932" character-set, so we should use "SJIS"
1253 * instead if iconv_open failed.
1255 if (strcmp(tc, "CP932") == 0)
1256 sc->cd = iconv_open("SJIS", fc);
1257 else if (strcmp(fc, "CP932") == 0)
1258 sc->cd = iconv_open(tc, "SJIS");
1260 #if defined(_WIN32) && !defined(__CYGWIN__)
1262 * archive_mstring on Windows directly convert multi-bytes
1263 * into archive_wstring in order not to depend on locale
1264 * so that you can do a I18N programing. This will be
1265 * used only in archive_mstring_copy_mbs_len_l so far.
1267 if (flag & SCONV_FROM_CHARSET) {
1268 sc->cd_w = iconv_open("UTF-8", fc);
1269 if (sc->cd_w == (iconv_t)-1 &&
1270 (sc->flag & SCONV_BEST_EFFORT)) {
1271 if (strcmp(fc, "CP932") == 0)
1272 sc->cd_w = iconv_open("UTF-8", "SJIS");
1275 #endif /* _WIN32 && !__CYGWIN__ */
1277 #endif /* HAVE_ICONV */
1282 * Set up converters.
1284 setup_converter(sc);
1290 * Free a string conversion object.
1293 free_sconv_object(struct archive_string_conv *sc)
1295 free(sc->from_charset);
1296 free(sc->to_charset);
1297 archive_string_free(&sc->utftmp);
1299 if (sc->cd != (iconv_t)-1)
1300 iconv_close(sc->cd);
1301 if (sc->cd_w != (iconv_t)-1)
1302 iconv_close(sc->cd_w);
1307 #if defined(_WIN32) && !defined(__CYGWIN__)
1309 my_atoi(const char *p)
1315 if (*p >= '0' && *p <= '9')
1316 cp = cp * 10 + (*p - '0');
1325 * Translate Charset name (as used by iconv) into CodePage (as used by Windows)
1326 * Return -1 if failed.
1328 * Note: This translation code may be insufficient.
1330 static struct charset {
1334 /* MUST BE SORTED! */
1353 {"HZ-GB-2312", 52936},
1371 {"ISO-8859-1", 28591},
1372 {"ISO-8859-13", 28603},
1373 {"ISO-8859-15", 28605},
1374 {"ISO-8859-2", 28592},
1375 {"ISO-8859-3", 28593},
1376 {"ISO-8859-4", 28594},
1377 {"ISO-8859-5", 28595},
1378 {"ISO-8859-6", 28596},
1379 {"ISO-8859-7", 28597},
1380 {"ISO-8859-8", 28598},
1381 {"ISO-8859-9", 28599},
1382 {"ISO8859-1", 28591},
1383 {"ISO8859-13", 28603},
1384 {"ISO8859-15", 28605},
1385 {"ISO8859-2", 28592},
1386 {"ISO8859-3", 28593},
1387 {"ISO8859-4", 28594},
1388 {"ISO8859-5", 28595},
1389 {"ISO8859-6", 28596},
1390 {"ISO8859-7", 28597},
1391 {"ISO8859-8", 28598},
1392 {"ISO8859-9", 28599},
1396 {"KS_C_5601-1987", 949},
1399 {"MACINTOSH", 10000},
1409 {"X-EUROPA", 29001},
1410 {"X-MAC-ARABIC", 10004},
1411 {"X-MAC-CE", 10029},
1412 {"X-MAC-CHINESEIMP", 10008},
1413 {"X-MAC-CHINESETRAD", 10002},
1414 {"X-MAC-CROATIAN", 10082},
1415 {"X-MAC-CYRILLIC", 10007},
1416 {"X-MAC-GREEK", 10006},
1417 {"X-MAC-HEBREW", 10005},
1418 {"X-MAC-ICELANDIC", 10079},
1419 {"X-MAC-JAPANESE", 10001},
1420 {"X-MAC-KOREAN", 10003},
1421 {"X-MAC-ROMANIAN", 10010},
1422 {"X-MAC-THAI", 10021},
1423 {"X-MAC-TURKISH", 10081},
1424 {"X-MAC-UKRAINIAN", 10017},
1427 make_codepage_from_charset(const char *charset)
1434 if (charset == NULL || strlen(charset) > 15)
1437 /* Copy name to uppercase. */
1440 char c = *charset++;
1441 if (c >= 'a' && c <= 'z')
1448 /* Look it up in the table first, so that we can easily
1449 * override CP367, which we map to 1252 instead of 367. */
1451 b = sizeof(charsets)/sizeof(charsets[0]);
1453 int c = (b + a) / 2;
1454 int r = strcmp(charsets[c].name, cs);
1460 return charsets[c].cp;
1463 /* If it's not in the table, try to parse it. */
1466 if (cs[1] == 'P' && cs[2] >= '0' && cs[2] <= '9') {
1467 cp = my_atoi(cs + 2);
1468 } else if (strcmp(cs, "CP_ACP") == 0)
1469 cp = get_current_codepage();
1470 else if (strcmp(cs, "CP_OEMCP") == 0)
1471 cp = get_current_oemcp();
1474 if (cs[1] == 'B' && cs[2] == 'M' &&
1475 cs[3] >= '0' && cs[3] <= '9') {
1476 cp = my_atoi(cs + 3);
1480 if (strncmp(cs, "WINDOWS-", 8) == 0) {
1481 cp = my_atoi(cs + 8);
1482 if (cp != 874 && (cp < 1250 || cp > 1258))
1483 cp = -1;/* This may invalid code. */
1491 * Return ANSI Code Page of current locale set by setlocale().
1494 get_current_codepage(void)
1499 locale = setlocale(LC_CTYPE, NULL);
1502 if (locale[0] == 'C' && locale[1] == '\0')
1503 return (CP_C_LOCALE);
1504 p = strrchr(locale, '.');
1514 * Translation table between Locale Name and ACP/OEMCP.
1521 { 950, 950, "Chinese_Taiwan" },
1522 { 936, 936, "Chinese_People's Republic of China" },
1523 { 950, 950, "Chinese_Taiwan" },
1524 { 1250, 852, "Czech_Czech Republic" },
1525 { 1252, 850, "Danish_Denmark" },
1526 { 1252, 850, "Dutch_Netherlands" },
1527 { 1252, 850, "Dutch_Belgium" },
1528 { 1252, 437, "English_United States" },
1529 { 1252, 850, "English_Australia" },
1530 { 1252, 850, "English_Canada" },
1531 { 1252, 850, "English_New Zealand" },
1532 { 1252, 850, "English_United Kingdom" },
1533 { 1252, 437, "English_United States" },
1534 { 1252, 850, "Finnish_Finland" },
1535 { 1252, 850, "French_France" },
1536 { 1252, 850, "French_Belgium" },
1537 { 1252, 850, "French_Canada" },
1538 { 1252, 850, "French_Switzerland" },
1539 { 1252, 850, "German_Germany" },
1540 { 1252, 850, "German_Austria" },
1541 { 1252, 850, "German_Switzerland" },
1542 { 1253, 737, "Greek_Greece" },
1543 { 1250, 852, "Hungarian_Hungary" },
1544 { 1252, 850, "Icelandic_Iceland" },
1545 { 1252, 850, "Italian_Italy" },
1546 { 1252, 850, "Italian_Switzerland" },
1547 { 932, 932, "Japanese_Japan" },
1548 { 949, 949, "Korean_Korea" },
1549 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1550 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1551 { 1252, 850, "Norwegian-Nynorsk_Norway" },
1552 { 1250, 852, "Polish_Poland" },
1553 { 1252, 850, "Portuguese_Portugal" },
1554 { 1252, 850, "Portuguese_Brazil" },
1555 { 1251, 866, "Russian_Russia" },
1556 { 1250, 852, "Slovak_Slovakia" },
1557 { 1252, 850, "Spanish_Spain" },
1558 { 1252, 850, "Spanish_Mexico" },
1559 { 1252, 850, "Spanish_Spain" },
1560 { 1252, 850, "Swedish_Sweden" },
1561 { 1254, 857, "Turkish_Turkey" },
1566 * Return OEM Code Page of current locale set by setlocale().
1569 get_current_oemcp(void)
1575 locale = setlocale(LC_CTYPE, NULL);
1577 return (GetOEMCP());
1578 if (locale[0] == 'C' && locale[1] == '\0')
1579 return (CP_C_LOCALE);
1581 p = strrchr(locale, '.');
1583 return (GetOEMCP());
1585 for (i = 0; acp_ocp_map[i].acp; i++) {
1586 if (strncmp(acp_ocp_map[i].locale, locale, len) == 0)
1587 return (acp_ocp_map[i].ocp);
1589 return (GetOEMCP());
1594 * POSIX platform does not use CodePage.
1598 get_current_codepage(void)
1600 return (-1);/* Unknown */
1603 make_codepage_from_charset(const char *charset)
1605 (void)charset; /* UNUSED */
1606 return (-1);/* Unknown */
1609 get_current_oemcp(void)
1611 return (-1);/* Unknown */
1614 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1617 * Return a string conversion object.
1619 static struct archive_string_conv *
1620 get_sconv_object(struct archive *a, const char *fc, const char *tc, int flag)
1622 struct archive_string_conv *sc;
1623 unsigned current_codepage;
1625 /* Check if we have made the sconv object. */
1626 sc = find_sconv_object(a, fc, tc);
1631 current_codepage = get_current_codepage();
1633 current_codepage = a->current_codepage;
1635 sc = create_sconv_object(canonical_charset_name(fc),
1636 canonical_charset_name(tc), current_codepage, flag);
1639 archive_set_error(a, ENOMEM,
1640 "Could not allocate memory for "
1641 "a string conversion object");
1646 * If there is no converter for current string conversion object,
1647 * we cannot handle this conversion.
1649 if (sc->nconverter == 0) {
1652 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1653 "iconv_open failed : Cannot handle ``%s''",
1654 (flag & SCONV_TO_CHARSET)?tc:fc);
1656 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1657 "A character-set conversion not fully supported "
1658 "on this platform");
1661 /* Failed; free a sconv object. */
1662 free_sconv_object(sc);
1670 add_sconv_object(a, sc);
1675 get_current_charset(struct archive *a)
1677 const char *cur_charset;
1680 cur_charset = default_iconv_charset("");
1682 cur_charset = default_iconv_charset(a->current_code);
1683 if (a->current_code == NULL) {
1684 a->current_code = strdup(cur_charset);
1685 a->current_codepage = get_current_codepage();
1686 a->current_oemcp = get_current_oemcp();
1689 return (cur_charset);
1693 * Make and Return a string conversion object.
1694 * Return NULL if the platform does not support the specified conversion
1695 * and best_effort is 0.
1696 * If best_effort is set, A string conversion object must be returned
1697 * unless memory allocation for the object fails, but the conversion
1698 * might fail when non-ASCII code is found.
1700 struct archive_string_conv *
1701 archive_string_conversion_to_charset(struct archive *a, const char *charset,
1704 int flag = SCONV_TO_CHARSET;
1707 flag |= SCONV_BEST_EFFORT;
1708 return (get_sconv_object(a, get_current_charset(a), charset, flag));
1711 struct archive_string_conv *
1712 archive_string_conversion_from_charset(struct archive *a, const char *charset,
1715 int flag = SCONV_FROM_CHARSET;
1718 flag |= SCONV_BEST_EFFORT;
1719 return (get_sconv_object(a, charset, get_current_charset(a), flag));
1723 * archive_string_default_conversion_*_archive() are provided for Windows
1724 * platform because other archiver application use CP_OEMCP for
1725 * MultiByteToWideChar() and WideCharToMultiByte() for the filenames
1726 * in tar or zip files. But mbstowcs/wcstombs(CRT) usually use CP_ACP
1727 * unless you use setlocale(LC_ALL, ".OCP")(specify CP_OEMCP).
1728 * So we should make a string conversion between CP_ACP and CP_OEMCP
1729 * for compatibillty.
1731 #if defined(_WIN32) && !defined(__CYGWIN__)
1732 struct archive_string_conv *
1733 archive_string_default_conversion_for_read(struct archive *a)
1735 const char *cur_charset = get_current_charset(a);
1738 /* NOTE: a check of cur_charset is unneeded but we need
1739 * that get_current_charset() has been surely called at
1740 * this time whatever C compiler optimized. */
1741 if (cur_charset != NULL &&
1742 (a->current_codepage == CP_C_LOCALE ||
1743 a->current_codepage == a->current_oemcp))
1744 return (NULL);/* no conversion. */
1746 _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1747 /* Make sure a null termination must be set. */
1748 oemcp[sizeof(oemcp)-1] = '\0';
1749 return (get_sconv_object(a, oemcp, cur_charset,
1750 SCONV_FROM_CHARSET));
1753 struct archive_string_conv *
1754 archive_string_default_conversion_for_write(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, cur_charset, oemcp,
1774 struct archive_string_conv *
1775 archive_string_default_conversion_for_read(struct archive *a)
1777 (void)a; /* UNUSED */
1781 struct archive_string_conv *
1782 archive_string_default_conversion_for_write(struct archive *a)
1784 (void)a; /* UNUSED */
1790 * Dispose of all character conversion objects in the archive object.
1793 archive_string_conversion_free(struct archive *a)
1795 struct archive_string_conv *sc;
1796 struct archive_string_conv *sc_next;
1798 for (sc = a->sconv; sc != NULL; sc = sc_next) {
1800 free_sconv_object(sc);
1803 free(a->current_code);
1804 a->current_code = NULL;
1808 * Return a conversion charset name.
1811 archive_string_conversion_charset_name(struct archive_string_conv *sc)
1813 if (sc->flag & SCONV_TO_CHARSET)
1814 return (sc->to_charset);
1816 return (sc->from_charset);
1820 * Change the behavior of a string conversion.
1823 archive_string_conversion_set_opt(struct archive_string_conv *sc, int opt)
1827 * A filename in UTF-8 was made with libarchive 2.x in a wrong
1828 * assumption that wchar_t was Unicode.
1829 * This option enables simulating the assumption in order to read
1830 * that filename correctly.
1832 case SCONV_SET_OPT_UTF8_LIBARCHIVE2X:
1833 #if (defined(_WIN32) && !defined(__CYGWIN__)) \
1834 || defined(__STDC_ISO_10646__) || defined(__APPLE__)
1836 * Nothing to do for it since wchar_t on these platforms
1837 * is really Unicode.
1839 (void)sc; /* UNUSED */
1841 if ((sc->flag & SCONV_UTF8_LIBARCHIVE_2) == 0) {
1842 sc->flag |= SCONV_UTF8_LIBARCHIVE_2;
1843 /* Set up string converters. */
1844 setup_converter(sc);
1848 case SCONV_SET_OPT_NORMALIZATION_C:
1849 if ((sc->flag & SCONV_NORMALIZATION_C) == 0) {
1850 sc->flag |= SCONV_NORMALIZATION_C;
1851 sc->flag &= ~SCONV_NORMALIZATION_D;
1852 /* Set up string converters. */
1853 setup_converter(sc);
1856 case SCONV_SET_OPT_NORMALIZATION_D:
1857 #if defined(HAVE_ICONV)
1859 * If iconv will take the string, do not change the
1860 * setting of the normalization.
1862 if (!(sc->flag & SCONV_WIN_CP) &&
1863 (sc->flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1864 !(sc->flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1867 if ((sc->flag & SCONV_NORMALIZATION_D) == 0) {
1868 sc->flag |= SCONV_NORMALIZATION_D;
1869 sc->flag &= ~SCONV_NORMALIZATION_C;
1870 /* Set up string converters. */
1871 setup_converter(sc);
1881 * Copy one archive_string to another in locale conversion.
1883 * archive_strncat_l();
1884 * archive_strncpy_l();
1889 mbsnbytes(const void *_p, size_t n)
1896 p = (const char *)_p;
1898 /* Like strlen(p), except won't examine positions beyond p[n]. */
1901 while (s < n && *pp) {
1909 utf16nbytes(const void *_p, size_t n)
1916 p = (const char *)_p;
1918 /* Like strlen(p), except won't examine positions beyond p[n]. */
1922 while (s < n && (pp[0] || pp[1])) {
1930 archive_strncpy_l(struct archive_string *as, const void *_p, size_t n,
1931 struct archive_string_conv *sc)
1934 return (archive_strncat_l(as, _p, n, sc));
1938 archive_strncat_l(struct archive_string *as, const void *_p, size_t n,
1939 struct archive_string_conv *sc)
1945 if (_p != NULL && n > 0) {
1946 if (sc != NULL && (sc->flag & SCONV_FROM_UTF16))
1947 length = utf16nbytes(_p, n);
1949 length = mbsnbytes(_p, n);
1952 /* We must allocate memory even if there is no data for conversion
1953 * or copy. This simulates archive_string_append behavior. */
1956 if (sc != NULL && (sc->flag & SCONV_TO_UTF16))
1958 if (archive_string_ensure(as, as->length + tn) == NULL)
1960 as->s[as->length] = 0;
1962 as->s[as->length+1] = 0;
1967 * If sc is NULL, we just make a copy.
1970 if (archive_string_append(as, _p, length) == NULL)
1971 return (-1);/* No memory */
1977 if (sc->nconverter > 1) {
1978 sc->utftmp.length = 0;
1979 r2 = sc->converter[0](&(sc->utftmp), s, length, sc);
1980 if (r2 != 0 && errno == ENOMEM)
1985 length = sc->utftmp.length;
1988 r2 = sc->converter[i](as, s, length, sc);
1997 * Return -1 if conversion fails.
2000 iconv_strncat_in_locale(struct archive_string *as, const void *_p,
2001 size_t length, struct archive_string_conv *sc)
2003 ICONV_CONST char *itp;
2008 int return_value = 0; /* success */
2009 int to_size, from_size;
2011 if (sc->flag & SCONV_TO_UTF16)
2015 if (sc->flag & SCONV_FROM_UTF16)
2020 if (archive_string_ensure(as, as->length + length*2+to_size) == NULL)
2024 itp = (char *)(uintptr_t)_p;
2026 outp = as->s + as->length;
2027 avail = as->buffer_length - as->length - to_size;
2028 while (remaining >= (size_t)from_size) {
2029 size_t result = iconv(cd, &itp, &remaining, &outp, &avail);
2031 if (result != (size_t)-1)
2032 break; /* Conversion completed. */
2034 if (errno == EILSEQ || errno == EINVAL) {
2036 * If an output charset is UTF-8 or UTF-16BE/LE,
2037 * unknown character should be U+FFFD
2038 * (replacement character).
2040 if (sc->flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) {
2042 if (sc->flag & SCONV_TO_UTF8)
2043 rbytes = sizeof(utf8_replacement_char);
2047 if (avail < rbytes) {
2048 as->length = outp - as->s;
2049 bs = as->buffer_length +
2050 (remaining * to_size) + rbytes;
2052 archive_string_ensure(as, bs))
2054 outp = as->s + as->length;
2055 avail = as->buffer_length
2056 - as->length - to_size;
2058 if (sc->flag & SCONV_TO_UTF8)
2059 memcpy(outp, utf8_replacement_char, sizeof(utf8_replacement_char));
2060 else if (sc->flag & SCONV_TO_UTF16BE)
2061 archive_be16enc(outp, UNICODE_R_CHAR);
2063 archive_le16enc(outp, UNICODE_R_CHAR);
2067 /* Skip the illegal input bytes. */
2072 remaining -= from_size;
2073 return_value = -1; /* failure */
2075 /* E2BIG no output buffer,
2076 * Increase an output buffer. */
2077 as->length = outp - as->s;
2078 bs = as->buffer_length + remaining * 2;
2079 if (NULL == archive_string_ensure(as, bs))
2081 outp = as->s + as->length;
2082 avail = as->buffer_length - as->length - to_size;
2085 as->length = outp - as->s;
2086 as->s[as->length] = 0;
2088 as->s[as->length+1] = 0;
2089 return (return_value);
2092 #endif /* HAVE_ICONV */
2095 #if defined(_WIN32) && !defined(__CYGWIN__)
2098 * Translate a string from a some CodePage to an another CodePage by
2099 * Windows APIs, and copy the result. Return -1 if conversion fails.
2102 strncat_in_codepage(struct archive_string *as,
2103 const void *_p, size_t length, struct archive_string_conv *sc)
2105 const char *s = (const char *)_p;
2106 struct archive_wstring aws;
2110 archive_string_init(&aws);
2111 saved_flag = sc->flag;
2112 sc->flag &= ~(SCONV_NORMALIZATION_D | SCONV_NORMALIZATION_C);
2113 r = archive_wstring_append_from_mbs_in_codepage(&aws, s, length, sc);
2114 sc->flag = saved_flag;
2116 archive_wstring_free(&aws);
2117 if (errno != ENOMEM)
2118 archive_string_append(as, s, length);
2123 r = archive_string_append_from_wcs_in_codepage(
2124 as, aws.s, aws.length, sc);
2125 if (r != 0 && errno != ENOMEM && l == as->length)
2126 archive_string_append(as, s, length);
2127 archive_wstring_free(&aws);
2132 * Test whether MBS ==> WCS is okay.
2135 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2137 const char *p = (const char *)_p;
2139 DWORD mbflag = MB_ERR_INVALID_CHARS;
2141 if (sc->flag & SCONV_FROM_CHARSET)
2142 codepage = sc->to_cp;
2144 codepage = sc->from_cp;
2146 if (codepage == CP_C_LOCALE)
2148 if (codepage != CP_UTF8)
2149 mbflag |= MB_PRECOMPOSED;
2151 if (MultiByteToWideChar(codepage, mbflag, p, (int)n, NULL, 0) == 0)
2152 return (-1); /* Invalid */
2153 return (0); /* Okay */
2159 * Test whether MBS ==> WCS is okay.
2162 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2164 const char *p = (const char *)_p;
2168 mbstate_t shift_state;
2170 memset(&shift_state, 0, sizeof(shift_state));
2172 /* Clear the shift state before starting. */
2173 mbtowc(NULL, NULL, 0);
2179 r = mbrtowc(&wc, p, n, &shift_state);
2181 r = mbtowc(&wc, p, n);
2183 if (r == (size_t)-1 || r == (size_t)-2)
2184 return (-1);/* Invalid. */
2190 (void)sc; /* UNUSED */
2191 return (0); /* All Okey. */
2194 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2197 * Basically returns -1 because we cannot make a conversion of charset
2198 * without iconv but in some cases this would return 0.
2199 * Returns 0 if all copied characters are ASCII.
2200 * Returns 0 if both from-locale and to-locale are the same and those
2201 * can be WCS with no error.
2204 best_effort_strncat_in_locale(struct archive_string *as, const void *_p,
2205 size_t length, struct archive_string_conv *sc)
2209 int return_value = 0; /* success */
2212 * If both from-locale and to-locale is the same, this makes a copy.
2213 * And then this checks all copied MBS can be WCS if so returns 0.
2216 if (archive_string_append(as, _p, length) == NULL)
2217 return (-1);/* No memory */
2218 return (invalid_mbs(_p, length, sc));
2222 * If a character is ASCII, this just copies it. If not, this
2223 * assigns '?' charater instead but in UTF-8 locale this assigns
2224 * byte sequence 0xEF 0xBD 0xBD, which are code point U+FFFD,
2225 * a Replacement Character in Unicode.
2229 itp = (const uint8_t *)_p;
2230 while (*itp && remaining > 0) {
2232 // Non-ASCII: Substitute with suitable replacement
2233 if (sc->flag & SCONV_TO_UTF8) {
2234 if (archive_string_append(as, utf8_replacement_char, sizeof(utf8_replacement_char)) == NULL) {
2235 __archive_errx(1, "Out of memory");
2238 archive_strappend_char(as, '?');
2242 archive_strappend_char(as, *itp);
2246 return (return_value);
2251 * Unicode conversion functions.
2252 * - UTF-8 <===> UTF-8 in removing surrogate pairs.
2253 * - UTF-8 NFD ===> UTF-8 NFC in removing surrogate pairs.
2254 * - UTF-8 made by libarchive 2.x ===> UTF-8.
2255 * - UTF-16BE <===> UTF-8.
2260 * Utility to convert a single UTF-8 sequence.
2262 * Usually return used bytes, return used byte in negative value when
2263 * a unicode character is replaced with U+FFFD.
2264 * See also http://unicode.org/review/pr-121.html Public Review Issue #121
2265 * Recommended Practice for Replacement Characters.
2268 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2270 static const char utf8_count[256] = {
2271 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
2272 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
2273 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
2274 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
2275 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
2276 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
2277 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
2278 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
2279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
2280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
2281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
2282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
2283 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
2284 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
2285 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
2286 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
2296 * Decode 1-4 bytes depending on the value of the first byte.
2298 ch = (unsigned char)*s;
2300 return (0); /* Standard: return 0 for end-of-string. */
2301 cnt = utf8_count[ch];
2303 /* Invalid sequence or there are not plenty bytes. */
2306 for (i = 1; i < cnt; i++) {
2307 if ((s[i] & 0xc0) != 0x80) {
2312 goto invalid_sequence;
2315 /* Make a Unicode code point from a single UTF-8 sequence. */
2317 case 1: /* 1 byte sequence. */
2320 case 2: /* 2 bytes sequence. */
2321 if ((s[1] & 0xc0) != 0x80) {
2323 goto invalid_sequence;
2325 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
2327 case 3: /* 3 bytes sequence. */
2328 if ((s[1] & 0xc0) != 0x80) {
2330 goto invalid_sequence;
2332 if ((s[2] & 0xc0) != 0x80) {
2334 goto invalid_sequence;
2336 wc = ((ch & 0x0f) << 12)
2337 | ((s[1] & 0x3f) << 6)
2340 goto invalid_sequence;/* Overlong sequence. */
2342 case 4: /* 4 bytes sequence. */
2343 if ((s[1] & 0xc0) != 0x80) {
2345 goto invalid_sequence;
2347 if ((s[2] & 0xc0) != 0x80) {
2349 goto invalid_sequence;
2351 if ((s[3] & 0xc0) != 0x80) {
2353 goto invalid_sequence;
2355 wc = ((ch & 0x07) << 18)
2356 | ((s[1] & 0x3f) << 12)
2357 | ((s[2] & 0x3f) << 6)
2360 goto invalid_sequence;/* Overlong sequence. */
2362 default: /* Others are all invalid sequence. */
2363 if (ch == 0xc0 || ch == 0xc1)
2365 else if (ch >= 0xf5 && ch <= 0xf7)
2367 else if (ch >= 0xf8 && ch <= 0xfb)
2369 else if (ch == 0xfc || ch == 0xfd)
2375 for (i = 1; i < cnt; i++) {
2376 if ((s[i] & 0xc0) != 0x80) {
2381 goto invalid_sequence;
2384 /* The code point larger than 0x10FFFF is not legal
2385 * Unicode values. */
2386 if (wc > UNICODE_MAX)
2387 goto invalid_sequence;
2388 /* Correctly gets a Unicode, returns used bytes. */
2392 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2397 utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2401 cnt = _utf8_to_unicode(pwc, s, n);
2402 /* Any of Surrogate pair is not legal Unicode values. */
2403 if (cnt == 3 && IS_SURROGATE_PAIR_LA(*pwc))
2408 static inline uint32_t
2409 combine_surrogate_pair(uint32_t uc, uint32_t uc2)
2419 * Convert a single UTF-8/CESU-8 sequence to a Unicode code point in
2420 * removing surrogate pairs.
2422 * CESU-8: The Compatibility Encoding Scheme for UTF-16.
2424 * Usually return used bytes, return used byte in negative value when
2425 * a unicode character is replaced with U+FFFD.
2428 cesu8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2433 cnt = _utf8_to_unicode(&wc, s, n);
2434 if (cnt == 3 && IS_HIGH_SURROGATE_LA(wc)) {
2437 /* Invalid byte sequence. */
2438 goto invalid_sequence;
2440 cnt = _utf8_to_unicode(&wc2, s+3, n-3);
2441 if (cnt != 3 || !IS_LOW_SURROGATE_LA(wc2)) {
2442 /* Invalid byte sequence. */
2443 goto invalid_sequence;
2445 wc = combine_surrogate_pair(wc, wc2);
2447 } else if (cnt == 3 && IS_LOW_SURROGATE_LA(wc)) {
2448 /* Invalid byte sequence. */
2449 goto invalid_sequence;
2454 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2461 * Convert a Unicode code point to a single UTF-8 sequence.
2463 * NOTE:This function does not check if the Unicode is legal or not.
2464 * Please you definitely check it before calling this.
2467 unicode_to_utf8(char *p, size_t remaining, uint32_t uc)
2471 /* Invalid Unicode char maps to Replacement character */
2472 if (uc > UNICODE_MAX)
2473 uc = UNICODE_R_CHAR;
2474 /* Translate code point to UTF8 */
2479 } else if (uc <= 0x7ff) {
2482 *p++ = 0xc0 | ((uc >> 6) & 0x1f);
2483 *p++ = 0x80 | (uc & 0x3f);
2484 } else if (uc <= 0xffff) {
2487 *p++ = 0xe0 | ((uc >> 12) & 0x0f);
2488 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2489 *p++ = 0x80 | (uc & 0x3f);
2493 *p++ = 0xf0 | ((uc >> 18) & 0x07);
2494 *p++ = 0x80 | ((uc >> 12) & 0x3f);
2495 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2496 *p++ = 0x80 | (uc & 0x3f);
2502 utf16be_to_unicode(uint32_t *pwc, const char *s, size_t n)
2504 return (utf16_to_unicode(pwc, s, n, 1));
2508 utf16le_to_unicode(uint32_t *pwc, const char *s, size_t n)
2510 return (utf16_to_unicode(pwc, s, n, 0));
2514 utf16_to_unicode(uint32_t *pwc, const char *s, size_t n, int be)
2516 const char *utf16 = s;
2522 /* set the Replacement Character instead. */
2523 *pwc = UNICODE_R_CHAR;
2528 uc = archive_be16dec(utf16);
2530 uc = archive_le16dec(utf16);
2533 /* If this is a surrogate pair, assemble the full code point.*/
2534 if (IS_HIGH_SURROGATE_LA(uc)) {
2539 uc2 = archive_be16dec(utf16);
2541 uc2 = archive_le16dec(utf16);
2544 if (IS_LOW_SURROGATE_LA(uc2)) {
2545 uc = combine_surrogate_pair(uc, uc2);
2548 /* Undescribed code point should be U+FFFD
2549 * (replacement character). */
2550 *pwc = UNICODE_R_CHAR;
2556 * Surrogate pair values(0xd800 through 0xdfff) are only
2557 * used by UTF-16, so, after above culculation, the code
2558 * must not be surrogate values, and Unicode has no codes
2559 * larger than 0x10ffff. Thus, those are not legal Unicode
2562 if (IS_SURROGATE_PAIR_LA(uc) || uc > UNICODE_MAX) {
2563 /* Undescribed code point should be U+FFFD
2564 * (replacement character). */
2565 *pwc = UNICODE_R_CHAR;
2566 return (((int)(utf16 - s)) * -1);
2569 return ((int)(utf16 - s));
2573 unicode_to_utf16be(char *p, size_t remaining, uint32_t uc)
2578 /* We have a code point that won't fit into a
2579 * wchar_t; convert it to a surrogate pair. */
2583 archive_be16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2584 archive_be16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2589 archive_be16enc(utf16, uc);
2595 unicode_to_utf16le(char *p, size_t remaining, uint32_t uc)
2600 /* We have a code point that won't fit into a
2601 * wchar_t; convert it to a surrogate pair. */
2605 archive_le16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2606 archive_le16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2611 archive_le16enc(utf16, uc);
2617 * Copy UTF-8 string in checking surrogate pair.
2618 * If any surrogate pair are found, it would be canonicalized.
2621 strncat_from_utf8_to_utf8(struct archive_string *as, const void *_p,
2622 size_t len, struct archive_string_conv *sc)
2628 (void)sc; /* UNUSED */
2630 if (archive_string_ensure(as, as->length + len + 1) == NULL)
2633 s = (const char *)_p;
2634 p = as->s + as->length;
2635 endp = as->s + as->buffer_length -1;
2642 * Forward byte sequence until a conversion of that is needed.
2644 while ((n = utf8_to_unicode(&uc, s, len)) > 0) {
2649 if (p + (s - ss) > endp) {
2650 as->length = p - as->s;
2651 if (archive_string_ensure(as,
2652 as->buffer_length + len + 1) == NULL)
2654 p = as->s + as->length;
2655 endp = as->s + as->buffer_length -1;
2658 memcpy(p, ss, s - ss);
2663 * If n is negative, current byte sequence needs a replacement.
2666 if (n == -3 && IS_SURROGATE_PAIR_LA(uc)) {
2667 /* Current byte sequence may be CESU-8. */
2668 n = cesu8_to_unicode(&uc, s, len);
2672 n *= -1;/* Use a replaced unicode character. */
2675 /* Rebuild UTF-8 byte sequence. */
2676 while ((w = unicode_to_utf8(p, endp - p, uc)) == 0) {
2677 as->length = p - as->s;
2678 if (archive_string_ensure(as,
2679 as->buffer_length + len + 1) == NULL)
2681 p = as->s + as->length;
2682 endp = as->s + as->buffer_length -1;
2689 as->length = p - as->s;
2690 as->s[as->length] = '\0';
2695 archive_string_append_unicode(struct archive_string *as, const void *_p,
2696 size_t len, struct archive_string_conv *sc)
2702 int n, ret = 0, ts, tm;
2703 int (*parse)(uint32_t *, const char *, size_t);
2704 size_t (*unparse)(char *, size_t, uint32_t);
2706 if (sc->flag & SCONV_TO_UTF16BE) {
2707 unparse = unicode_to_utf16be;
2709 } else if (sc->flag & SCONV_TO_UTF16LE) {
2710 unparse = unicode_to_utf16le;
2712 } else if (sc->flag & SCONV_TO_UTF8) {
2713 unparse = unicode_to_utf8;
2717 * This case is going to be converted to another
2718 * character-set through iconv.
2720 if (sc->flag & SCONV_FROM_UTF16BE) {
2721 unparse = unicode_to_utf16be;
2723 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2724 unparse = unicode_to_utf16le;
2727 unparse = unicode_to_utf8;
2732 if (sc->flag & SCONV_FROM_UTF16BE) {
2733 parse = utf16be_to_unicode;
2735 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2736 parse = utf16le_to_unicode;
2739 parse = cesu8_to_unicode;
2743 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2746 s = (const char *)_p;
2747 p = as->s + as->length;
2748 endp = as->s + as->buffer_length - ts;
2749 while ((n = parse(&uc, s, len)) != 0) {
2751 /* Use a replaced unicode character. */
2757 while ((w = unparse(p, endp - p, uc)) == 0) {
2758 /* There is not enough output buffer so
2759 * we have to expand it. */
2760 as->length = p - as->s;
2761 if (archive_string_ensure(as,
2762 as->buffer_length + len * tm + ts) == NULL)
2764 p = as->s + as->length;
2765 endp = as->s + as->buffer_length - ts;
2769 as->length = p - as->s;
2770 as->s[as->length] = '\0';
2772 as->s[as->length+1] = '\0';
2777 * Following Constants for Hangul compositions this information comes from
2778 * Unicode Standard Annex #15 http://unicode.org/reports/tr15/
2780 #define HC_SBASE 0xAC00
2781 #define HC_LBASE 0x1100
2782 #define HC_VBASE 0x1161
2783 #define HC_TBASE 0x11A7
2784 #define HC_LCOUNT 19
2785 #define HC_VCOUNT 21
2786 #define HC_TCOUNT 28
2787 #define HC_NCOUNT (HC_VCOUNT * HC_TCOUNT)
2788 #define HC_SCOUNT (HC_LCOUNT * HC_NCOUNT)
2791 get_nfc(uint32_t uc, uint32_t uc2)
2796 b = sizeof(u_composition_table)/sizeof(u_composition_table[0]) -1;
2798 int m = (t + b) / 2;
2799 if (u_composition_table[m].cp1 < uc)
2801 else if (u_composition_table[m].cp1 > uc)
2803 else if (u_composition_table[m].cp2 < uc2)
2805 else if (u_composition_table[m].cp2 > uc2)
2808 return (u_composition_table[m].nfc);
2813 #define FDC_MAX 10 /* The maximum number of Following Decomposable
2817 * Update first code point.
2819 #define UPDATE_UC(new_uc) do { \
2825 * Replace first code point with second code point.
2827 #define REPLACE_UC_WITH_UC2() do { \
2833 #define EXPAND_BUFFER() do { \
2834 as->length = p - as->s; \
2835 if (archive_string_ensure(as, \
2836 as->buffer_length + len * tm + ts) == NULL)\
2838 p = as->s + as->length; \
2839 endp = as->s + as->buffer_length - ts; \
2842 #define UNPARSE(p, endp, uc) do { \
2843 while ((w = unparse(p, (endp) - (p), uc)) == 0) {\
2850 * Write first code point.
2851 * If the code point has not be changed from its original code,
2852 * this just copies it from its original buffer pointer.
2853 * If not, this converts it to UTF-8 byte sequence and copies it.
2855 #define WRITE_UC() do { \
2862 /* FALL THROUGH */ \
2865 /* FALL THROUGH */ \
2868 /* FALL THROUGH */ \
2875 UNPARSE(p, endp, uc); \
2880 * Collect following decomposable code points.
2882 #define COLLECT_CPS(start) do { \
2884 for (_i = start; _i < FDC_MAX ; _i++) { \
2885 nx = parse(&ucx[_i], s, len); \
2888 cx = CCC(ucx[_i]); \
2889 if (cl >= cx && cl != 228 && cx != 228)\
2896 if (_i >= FDC_MAX) { \
2898 ucx_size = FDC_MAX; \
2904 * Normalize UTF-8/UTF-16BE characters to Form C and copy the result.
2906 * TODO: Convert composition exclusions,which are never converted
2907 * from NFC,NFD,NFKC and NFKD, to Form C.
2910 archive_string_normalize_C(struct archive_string *as, const void *_p,
2911 size_t len, struct archive_string_conv *sc)
2913 const char *s = (const char *)_p;
2917 int always_replace, n, n2, ret = 0, spair, ts, tm;
2918 int (*parse)(uint32_t *, const char *, size_t);
2919 size_t (*unparse)(char *, size_t, uint32_t);
2922 ts = 1;/* text size. */
2923 if (sc->flag & SCONV_TO_UTF16BE) {
2924 unparse = unicode_to_utf16be;
2926 if (sc->flag & SCONV_FROM_UTF16BE)
2928 } else if (sc->flag & SCONV_TO_UTF16LE) {
2929 unparse = unicode_to_utf16le;
2931 if (sc->flag & SCONV_FROM_UTF16LE)
2933 } else if (sc->flag & SCONV_TO_UTF8) {
2934 unparse = unicode_to_utf8;
2935 if (sc->flag & SCONV_FROM_UTF8)
2939 * This case is going to be converted to another
2940 * character-set through iconv.
2943 if (sc->flag & SCONV_FROM_UTF16BE) {
2944 unparse = unicode_to_utf16be;
2946 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2947 unparse = unicode_to_utf16le;
2950 unparse = unicode_to_utf8;
2954 if (sc->flag & SCONV_FROM_UTF16BE) {
2955 parse = utf16be_to_unicode;
2957 spair = 4;/* surrogate pair size in UTF-16. */
2958 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2959 parse = utf16le_to_unicode;
2961 spair = 4;/* surrogate pair size in UTF-16. */
2963 parse = cesu8_to_unicode;
2965 spair = 6;/* surrogate pair size in UTF-8. */
2968 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2971 p = as->s + as->length;
2972 endp = as->s + as->buffer_length - ts;
2973 while ((n = parse(&uc, s, len)) != 0) {
2974 const char *ucptr, *uc2ptr;
2977 /* Use a replaced unicode character. */
2978 UNPARSE(p, endp, uc);
2983 } else if (n == spair || always_replace)
2984 /* uc is converted from a surrogate pair.
2985 * this should be treated as a changed code. */
2992 /* Read second code point. */
2993 while ((n2 = parse(&uc2, s, len)) > 0) {
2994 uint32_t ucx[FDC_MAX];
2996 int cl, cx, i, nx, ucx_size;
3000 if (n2 == spair || always_replace)
3001 /* uc2 is converted from a surrogate pair.
3002 * this should be treated as a changed code. */
3010 * If current second code point is out of decomposable
3011 * code points, finding compositions is unneeded.
3013 if (!IS_DECOMPOSABLE_BLOCK(uc2)) {
3015 REPLACE_UC_WITH_UC2();
3020 * Try to combine current code points.
3023 * We have to combine Hangul characters according to
3024 * http://uniicode.org/reports/tr15/#Hangul
3026 if (0 <= (LIndex = uc - HC_LBASE) &&
3027 LIndex < HC_LCOUNT) {
3029 * Hangul Composition.
3030 * 1. Two current code points are L and V.
3032 int VIndex = uc2 - HC_VBASE;
3033 if (0 <= VIndex && VIndex < HC_VCOUNT) {
3034 /* Make syllable of form LV. */
3035 UPDATE_UC(HC_SBASE +
3036 (LIndex * HC_VCOUNT + VIndex) *
3040 REPLACE_UC_WITH_UC2();
3043 } else if (0 <= (SIndex = uc - HC_SBASE) &&
3044 SIndex < HC_SCOUNT && (SIndex % HC_TCOUNT) == 0) {
3046 * Hangul Composition.
3047 * 2. Two current code points are LV and T.
3049 int TIndex = uc2 - HC_TBASE;
3050 if (0 < TIndex && TIndex < HC_TCOUNT) {
3051 /* Make syllable of form LVT. */
3052 UPDATE_UC(uc + TIndex);
3055 REPLACE_UC_WITH_UC2();
3058 } else if ((nfc = get_nfc(uc, uc2)) != 0) {
3059 /* A composition to current code points
3063 } else if ((cl = CCC(uc2)) == 0) {
3064 /* Clearly 'uc2' the second code point is not
3065 * a decomposable code. */
3067 REPLACE_UC_WITH_UC2();
3072 * Collect following decomposable code points.
3080 * Find a composed code in the collected code points.
3083 while (i < ucx_size) {
3086 if ((nfc = get_nfc(uc, ucx[i])) == 0) {
3092 * nfc is composed of uc and ucx[i].
3097 * Remove ucx[i] by shifting
3098 * following code points.
3100 for (j = i; j+1 < ucx_size; j++) {
3107 * Collect following code points blocked
3108 * by ucx[i] the removed code point.
3110 if (ucx_size > 0 && i == ucx_size &&
3111 nx > 0 && cx == cl) {
3112 cl = ccx[ucx_size-1];
3113 COLLECT_CPS(ucx_size);
3116 * Restart finding a composed code with
3117 * the updated uc from the top of the
3118 * collected code points.
3124 * Apparently the current code points are not
3125 * decomposed characters or already composed.
3128 for (i = 0; i < ucx_size; i++)
3129 UNPARSE(p, endp, ucx[i]);
3132 * Flush out remaining canonical combining characters.
3134 if (nx > 0 && cx == cl && len > 0) {
3135 while ((nx = parse(&ucx[0], s, len))
3143 UNPARSE(p, endp, ucx[0]);
3150 /* Use a replaced unicode character. */
3151 UNPARSE(p, endp, uc2);
3156 } else if (n2 == 0) {
3161 as->length = p - as->s;
3162 as->s[as->length] = '\0';
3164 as->s[as->length+1] = '\0';
3169 get_nfd(uint32_t *cp1, uint32_t *cp2, uint32_t uc)
3174 * These are not converted to NFD on Mac OS.
3176 if ((uc >= 0x2000 && uc <= 0x2FFF) ||
3177 (uc >= 0xF900 && uc <= 0xFAFF) ||
3178 (uc >= 0x2F800 && uc <= 0x2FAFF))
3181 * Those code points are not converted to NFD on Mac OS.
3182 * I do not know the reason because it is undocumented.
3184 * 1109A ==> 11099 110BA
3185 * 1109C ==> 1109B 110BA
3186 * 110AB ==> 110A5 110BA
3188 if (uc == 0x1109A || uc == 0x1109C || uc == 0x110AB)
3192 b = sizeof(u_decomposition_table)/sizeof(u_decomposition_table[0]) -1;
3194 int m = (t + b) / 2;
3195 if (u_decomposition_table[m].nfc < uc)
3197 else if (u_decomposition_table[m].nfc > uc)
3200 *cp1 = u_decomposition_table[m].cp1;
3201 *cp2 = u_decomposition_table[m].cp2;
3208 #define REPLACE_UC_WITH(cp) do { \
3214 * Normalize UTF-8 characters to Form D and copy the result.
3217 archive_string_normalize_D(struct archive_string *as, const void *_p,
3218 size_t len, struct archive_string_conv *sc)
3220 const char *s = (const char *)_p;
3224 int always_replace, n, n2, ret = 0, spair, ts, tm;
3225 int (*parse)(uint32_t *, const char *, size_t);
3226 size_t (*unparse)(char *, size_t, uint32_t);
3229 ts = 1;/* text size. */
3230 if (sc->flag & SCONV_TO_UTF16BE) {
3231 unparse = unicode_to_utf16be;
3233 if (sc->flag & SCONV_FROM_UTF16BE)
3235 } else if (sc->flag & SCONV_TO_UTF16LE) {
3236 unparse = unicode_to_utf16le;
3238 if (sc->flag & SCONV_FROM_UTF16LE)
3240 } else if (sc->flag & SCONV_TO_UTF8) {
3241 unparse = unicode_to_utf8;
3242 if (sc->flag & SCONV_FROM_UTF8)
3246 * This case is going to be converted to another
3247 * character-set through iconv.
3250 if (sc->flag & SCONV_FROM_UTF16BE) {
3251 unparse = unicode_to_utf16be;
3253 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3254 unparse = unicode_to_utf16le;
3257 unparse = unicode_to_utf8;
3261 if (sc->flag & SCONV_FROM_UTF16BE) {
3262 parse = utf16be_to_unicode;
3264 spair = 4;/* surrogate pair size in UTF-16. */
3265 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3266 parse = utf16le_to_unicode;
3268 spair = 4;/* surrogate pair size in UTF-16. */
3270 parse = cesu8_to_unicode;
3272 spair = 6;/* surrogate pair size in UTF-8. */
3275 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
3278 p = as->s + as->length;
3279 endp = as->s + as->buffer_length - ts;
3280 while ((n = parse(&uc, s, len)) != 0) {
3293 /* Use a replaced unicode character. */
3294 UNPARSE(p, endp, uc);
3299 } else if (n == spair || always_replace)
3300 /* uc is converted from a surrogate pair.
3301 * this should be treated as a changed code. */
3308 /* Hangul Decomposition. */
3309 if ((SIndex = uc - HC_SBASE) >= 0 && SIndex < HC_SCOUNT) {
3310 int L = HC_LBASE + SIndex / HC_NCOUNT;
3311 int V = HC_VBASE + (SIndex % HC_NCOUNT) / HC_TCOUNT;
3312 int T = HC_TBASE + SIndex % HC_TCOUNT;
3318 if (T != HC_TBASE) {
3324 if (IS_DECOMPOSABLE_BLOCK(uc) && CCC(uc) != 0) {
3330 while (get_nfd(&cp1, &cp2, uc) && fdi < FDC_MAX) {
3333 for (k = fdi; k > 0; k--)
3335 fdc[0].ccc = CCC(cp2);
3338 REPLACE_UC_WITH(cp1);
3341 /* Read following code points. */
3342 while ((n2 = parse(&uc2, s, len)) > 0 &&
3343 (ccc = CCC(uc2)) != 0 && fdi < FDC_MAX) {
3348 for (j = 0; j < fdi; j++) {
3349 if (fdc[j].ccc > ccc)
3353 for (k = fdi; k > j; k--)
3365 for (fdj = 0; fdj < fdi; fdj++) {
3366 REPLACE_UC_WITH(fdc[fdj].uc);
3372 REPLACE_UC_WITH(uc2);
3374 goto check_first_code;
3376 as->length = p - as->s;
3377 as->s[as->length] = '\0';
3379 as->s[as->length+1] = '\0';
3384 * libarchive 2.x made incorrect UTF-8 strings in the wrong assumption
3385 * that WCS is Unicode. It is true for several platforms but some are false.
3386 * And then people who did not use UTF-8 locale on the non Unicode WCS
3387 * platform and made a tar file with libarchive(mostly bsdtar) 2.x. Those
3388 * now cannot get right filename from libarchive 3.x and later since we
3389 * fixed the wrong assumption and it is incompatible to older its versions.
3390 * So we provide special option, "compat-2x.x", for resolving it.
3391 * That option enable the string conversion of libarchive 2.x.
3393 * Translates the wrong UTF-8 string made by libarchive 2.x into current
3394 * locale character set and appends to the archive_string.
3395 * Note: returns -1 if conversion fails.
3398 strncat_from_utf8_libarchive2(struct archive_string *as,
3399 const void *_p, size_t len, struct archive_string_conv *sc)
3407 mbstate_t shift_state;
3409 memset(&shift_state, 0, sizeof(shift_state));
3411 /* Clear the shift state before starting. */
3412 wctomb(NULL, L'\0');
3414 (void)sc; /* UNUSED */
3416 * Allocate buffer for MBS.
3417 * We need this allocation here since it is possible that
3418 * as->s is still NULL.
3420 if (archive_string_ensure(as, as->length + len + 1) == NULL)
3423 s = (const char *)_p;
3424 p = as->s + as->length;
3425 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3426 while ((n = _utf8_to_unicode(&unicode, s, len)) != 0) {
3430 as->length = p - as->s;
3431 /* Re-allocate buffer for MBS. */
3432 if (archive_string_ensure(as,
3433 as->length + len * 2 + 1) == NULL)
3435 p = as->s + as->length;
3436 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3440 * As libarchie 2.x, translates the UTF-8 characters into
3441 * wide-characters in the assumption that WCS is Unicode.
3447 wc = (wchar_t)unicode;
3452 * Translates the wide-character into the current locale MBS.
3455 n = (int)wcrtomb(p, wc, &shift_state);
3457 n = (int)wctomb(p, wc);
3463 as->length = p - as->s;
3464 as->s[as->length] = '\0';
3470 * Conversion functions between current locale dependent MBS and UTF-16BE.
3471 * strncat_from_utf16be() : UTF-16BE --> MBS
3472 * strncat_to_utf16be() : MBS --> UTF16BE
3475 #if defined(_WIN32) && !defined(__CYGWIN__)
3478 * Convert a UTF-16BE/LE string to current locale and copy the result.
3479 * Return -1 if conversion fails.
3482 win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
3483 struct archive_string_conv *sc, int be)
3485 struct archive_string tmp;
3494 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3497 mbs = as->s + as->length;
3498 mbs_size = as->buffer_length - as->length -1;
3500 if (sc->to_cp == CP_C_LOCALE) {
3502 * "C" locale special process.
3506 for (b = 0; b < bytes; b += 2) {
3509 val = archive_be16dec(u16+b);
3511 val = archive_le16dec(u16+b);
3516 *mbs++ = (char)(val&0xff);
3520 as->s[as->length] = '\0';
3524 archive_string_init(&tmp);
3526 if (is_big_endian()) {
3529 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3531 memcpy(tmp.s, _p, bytes);
3532 for (b = 0; b < bytes; b += 2) {
3533 uint16_t val = archive_be16dec(tmp.s+b);
3534 archive_le16enc(tmp.s+b, val);
3539 if (!is_big_endian()) {
3542 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3544 memcpy(tmp.s, _p, bytes);
3545 for (b = 0; b < bytes; b += 2) {
3546 uint16_t val = archive_le16dec(tmp.s+b);
3547 archive_be16enc(tmp.s+b, val);
3555 ll = WideCharToMultiByte(sc->to_cp, 0,
3556 (LPCWSTR)u16, (int)bytes>>1, mbs, (int)mbs_size,
3558 /* Exit loop if we succeeded */
3560 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3563 /* Else expand buffer and loop to try again. */
3564 ll = WideCharToMultiByte(sc->to_cp, 0,
3565 (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
3566 if (archive_string_ensure(as, ll +1) == NULL)
3568 mbs = as->s + as->length;
3569 mbs_size = as->buffer_length - as->length -1;
3571 archive_string_free(&tmp);
3573 as->s[as->length] = '\0';
3574 if (ll == 0 || defchar)
3580 win_strncat_from_utf16be(struct archive_string *as, const void *_p,
3581 size_t bytes, struct archive_string_conv *sc)
3583 return (win_strncat_from_utf16(as, _p, bytes, sc, 1));
3587 win_strncat_from_utf16le(struct archive_string *as, const void *_p,
3588 size_t bytes, struct archive_string_conv *sc)
3590 return (win_strncat_from_utf16(as, _p, bytes, sc, 0));
3598 return (archive_be16dec(&d) == 1);
3602 * Convert a current locale string to UTF-16BE/LE and copy the result.
3603 * Return -1 if conversion fails.
3606 win_strncat_to_utf16(struct archive_string *as16, const void *_p,
3607 size_t length, struct archive_string_conv *sc, int bigendian)
3609 const char *s = (const char *)_p;
3611 size_t count, avail;
3613 if (archive_string_ensure(as16,
3614 as16->length + (length + 1) * 2) == NULL)
3617 u16 = as16->s + as16->length;
3618 avail = as16->buffer_length - 2;
3619 if (sc->from_cp == CP_C_LOCALE) {
3621 * "C" locale special process.
3624 while (count < length && *s) {
3626 archive_be16enc(u16, *s);
3628 archive_le16enc(u16, *s);
3633 as16->length += count << 1;
3634 as16->s[as16->length] = 0;
3635 as16->s[as16->length+1] = 0;
3639 count = MultiByteToWideChar(sc->from_cp,
3640 MB_PRECOMPOSED, s, (int)length, (LPWSTR)u16, (int)avail>>1);
3641 /* Exit loop if we succeeded */
3643 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3646 /* Expand buffer and try again */
3647 count = MultiByteToWideChar(sc->from_cp,
3648 MB_PRECOMPOSED, s, (int)length, NULL, 0);
3649 if (archive_string_ensure(as16, (count +1) * 2)
3652 u16 = as16->s + as16->length;
3653 avail = as16->buffer_length - 2;
3655 as16->length += count * 2;
3656 as16->s[as16->length] = 0;
3657 as16->s[as16->length+1] = 0;
3661 if (is_big_endian()) {
3664 uint16_t v = archive_be16dec(u16);
3665 archive_le16enc(u16, v);
3673 uint16_t v = archive_le16dec(u16);
3674 archive_be16enc(u16, v);
3684 win_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3685 size_t length, struct archive_string_conv *sc)
3687 return (win_strncat_to_utf16(as16, _p, length, sc, 1));
3691 win_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3692 size_t length, struct archive_string_conv *sc)
3694 return (win_strncat_to_utf16(as16, _p, length, sc, 0));
3697 #endif /* _WIN32 && !__CYGWIN__ */
3700 * Do the best effort for conversions.
3701 * We cannot handle UTF-16BE character-set without such iconv,
3702 * but there is a chance if a string consists just ASCII code or
3703 * a current locale is UTF-8.
3707 * Convert a UTF-16BE string to current locale and copy the result.
3708 * Return -1 if conversion fails.
3711 best_effort_strncat_from_utf16(struct archive_string *as, const void *_p,
3712 size_t bytes, struct archive_string_conv *sc, int be)
3714 const char *utf16 = (const char *)_p;
3719 (void)sc; /* UNUSED */
3721 * Other case, we should do the best effort.
3722 * If all character are ASCII(<0x7f), we can convert it.
3723 * if not , we set a alternative character and return -1.
3726 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3728 mbs = as->s + as->length;
3730 while ((n = utf16_to_unicode(&uc, utf16, bytes, be)) != 0) {
3739 /* We cannot handle it. */
3745 as->length = mbs - as->s;
3746 as->s[as->length] = '\0';
3751 best_effort_strncat_from_utf16be(struct archive_string *as, const void *_p,
3752 size_t bytes, struct archive_string_conv *sc)
3754 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 1));
3758 best_effort_strncat_from_utf16le(struct archive_string *as, const void *_p,
3759 size_t bytes, struct archive_string_conv *sc)
3761 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 0));
3765 * Convert a current locale string to UTF-16BE/LE and copy the result.
3766 * Return -1 if conversion fails.
3769 best_effort_strncat_to_utf16(struct archive_string *as16, const void *_p,
3770 size_t length, struct archive_string_conv *sc, int bigendian)
3772 const char *s = (const char *)_p;
3777 (void)sc; /* UNUSED */
3779 * Other case, we should do the best effort.
3780 * If all character are ASCII(<0x7f), we can convert it.
3781 * if not , we set a alternative character and return -1.
3786 if (archive_string_ensure(as16,
3787 as16->length + (length + 1) * 2) == NULL)
3790 utf16 = as16->s + as16->length;
3791 while (remaining--) {
3794 /* We cannot handle it. */
3799 archive_be16enc(utf16, c);
3801 archive_le16enc(utf16, c);
3804 as16->length = utf16 - as16->s;
3805 as16->s[as16->length] = 0;
3806 as16->s[as16->length+1] = 0;
3811 best_effort_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3812 size_t length, struct archive_string_conv *sc)
3814 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 1));
3818 best_effort_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3819 size_t length, struct archive_string_conv *sc)
3821 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 0));
3826 * Multistring operations.
3830 archive_mstring_clean(struct archive_mstring *aes)
3832 archive_wstring_free(&(aes->aes_wcs));
3833 archive_string_free(&(aes->aes_mbs));
3834 archive_string_free(&(aes->aes_utf8));
3835 archive_string_free(&(aes->aes_mbs_in_locale));
3840 archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src)
3842 dest->aes_set = src->aes_set;
3843 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
3844 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
3845 archive_wstring_copy(&(dest->aes_wcs), &(src->aes_wcs));
3849 archive_mstring_get_utf8(struct archive *a, struct archive_mstring *aes,
3852 struct archive_string_conv *sc;
3855 /* If we already have a UTF8 form, return that immediately. */
3856 if (aes->aes_set & AES_SET_UTF8) {
3857 *p = aes->aes_utf8.s;
3862 if (aes->aes_set & AES_SET_MBS) {
3863 sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
3865 return (-1);/* Couldn't allocate memory for sc. */
3866 r = archive_strncpy_l(&(aes->aes_utf8), aes->aes_mbs.s,
3867 aes->aes_mbs.length, sc);
3869 free_sconv_object(sc);
3871 aes->aes_set |= AES_SET_UTF8;
3872 *p = aes->aes_utf8.s;
3873 return (0);/* success. */
3875 return (-1);/* failure. */
3877 return (0);/* success. */
3881 archive_mstring_get_mbs(struct archive *a, struct archive_mstring *aes,
3886 (void)a; /* UNUSED */
3887 /* If we already have an MBS form, return that immediately. */
3888 if (aes->aes_set & AES_SET_MBS) {
3889 *p = aes->aes_mbs.s;
3894 /* If there's a WCS form, try converting with the native locale. */
3895 if (aes->aes_set & AES_SET_WCS) {
3896 archive_string_empty(&(aes->aes_mbs));
3897 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3898 aes->aes_wcs.s, aes->aes_wcs.length);
3899 *p = aes->aes_mbs.s;
3901 aes->aes_set |= AES_SET_MBS;
3908 * Only a UTF-8 form cannot avail because its conversion already
3909 * failed at archive_mstring_update_utf8().
3915 archive_mstring_get_wcs(struct archive *a, struct archive_mstring *aes,
3920 (void)a;/* UNUSED */
3921 /* Return WCS form if we already have it. */
3922 if (aes->aes_set & AES_SET_WCS) {
3923 *wp = aes->aes_wcs.s;
3928 /* Try converting MBS to WCS using native locale. */
3929 if (aes->aes_set & AES_SET_MBS) {
3930 archive_wstring_empty(&(aes->aes_wcs));
3931 r = archive_wstring_append_from_mbs(&(aes->aes_wcs),
3932 aes->aes_mbs.s, aes->aes_mbs.length);
3934 aes->aes_set |= AES_SET_WCS;
3935 *wp = aes->aes_wcs.s;
3937 ret = -1;/* failure. */
3943 archive_mstring_get_mbs_l(struct archive_mstring *aes,
3944 const char **p, size_t *length, struct archive_string_conv *sc)
3948 #if defined(_WIN32) && !defined(__CYGWIN__)
3950 * Internationalization programing on Windows must use Wide
3951 * characters because Windows platform cannot make locale UTF-8.
3953 if (sc != NULL && (aes->aes_set & AES_SET_WCS) != 0) {
3954 archive_string_empty(&(aes->aes_mbs_in_locale));
3955 r = archive_string_append_from_wcs_in_codepage(
3956 &(aes->aes_mbs_in_locale), aes->aes_wcs.s,
3957 aes->aes_wcs.length, sc);
3959 *p = aes->aes_mbs_in_locale.s;
3961 *length = aes->aes_mbs_in_locale.length;
3963 } else if (errno == ENOMEM)
3970 /* If there is not an MBS form but is a WCS form, try converting
3971 * with the native locale to be used for translating it to specified
3973 if ((aes->aes_set & AES_SET_MBS) == 0 &&
3974 (aes->aes_set & AES_SET_WCS) != 0) {
3975 archive_string_empty(&(aes->aes_mbs));
3976 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3977 aes->aes_wcs.s, aes->aes_wcs.length);
3979 aes->aes_set |= AES_SET_MBS;
3980 else if (errno == ENOMEM)
3985 /* If we already have an MBS form, use it to be translated to
3986 * specified character-set. */
3987 if (aes->aes_set & AES_SET_MBS) {
3989 /* Conversion is unneeded. */
3990 *p = aes->aes_mbs.s;
3992 *length = aes->aes_mbs.length;
3995 ret = archive_strncpy_l(&(aes->aes_mbs_in_locale),
3996 aes->aes_mbs.s, aes->aes_mbs.length, sc);
3997 *p = aes->aes_mbs_in_locale.s;
3999 *length = aes->aes_mbs_in_locale.length;
4009 archive_mstring_copy_mbs(struct archive_mstring *aes, const char *mbs)
4015 return (archive_mstring_copy_mbs_len(aes, mbs, strlen(mbs)));
4019 archive_mstring_copy_mbs_len(struct archive_mstring *aes, const char *mbs,
4026 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4027 archive_strncpy(&(aes->aes_mbs), mbs, len);
4028 archive_string_empty(&(aes->aes_utf8));
4029 archive_wstring_empty(&(aes->aes_wcs));
4034 archive_mstring_copy_wcs(struct archive_mstring *aes, const wchar_t *wcs)
4036 return archive_mstring_copy_wcs_len(aes, wcs,
4037 wcs == NULL ? 0 : wcslen(wcs));
4041 archive_mstring_copy_utf8(struct archive_mstring *aes, const char *utf8)
4046 aes->aes_set = AES_SET_UTF8;
4047 archive_string_empty(&(aes->aes_mbs));
4048 archive_string_empty(&(aes->aes_wcs));
4049 archive_strncpy(&(aes->aes_utf8), utf8, strlen(utf8));
4050 return (int)strlen(utf8);
4054 archive_mstring_copy_wcs_len(struct archive_mstring *aes, const wchar_t *wcs,
4060 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
4061 archive_string_empty(&(aes->aes_mbs));
4062 archive_string_empty(&(aes->aes_utf8));
4063 archive_wstrncpy(&(aes->aes_wcs), wcs, len);
4068 archive_mstring_copy_mbs_len_l(struct archive_mstring *aes,
4069 const char *mbs, size_t len, struct archive_string_conv *sc)
4077 archive_string_empty(&(aes->aes_mbs));
4078 archive_wstring_empty(&(aes->aes_wcs));
4079 archive_string_empty(&(aes->aes_utf8));
4080 #if defined(_WIN32) && !defined(__CYGWIN__)
4082 * Internationalization programing on Windows must use Wide
4083 * characters because Windows platform cannot make locale UTF-8.
4086 if (archive_string_append(&(aes->aes_mbs),
4087 mbs, mbsnbytes(mbs, len)) == NULL) {
4091 aes->aes_set = AES_SET_MBS;
4094 #if defined(HAVE_ICONV)
4095 } else if (sc != NULL && sc->cd_w != (iconv_t)-1) {
4097 * This case happens only when MultiByteToWideChar() cannot
4098 * handle sc->from_cp, and we have to iconv in order to
4099 * translate character-set to wchar_t,UTF-16.
4101 iconv_t cd = sc->cd;
4106 * Translate multi-bytes from some character-set to UTF-8.
4109 r = archive_strncpy_l(&(aes->aes_utf8), mbs, len, sc);
4115 aes->aes_set = AES_SET_UTF8;
4118 * Append the UTF-8 string into wstring.
4121 sc->flag &= ~(SCONV_NORMALIZATION_C
4122 | SCONV_TO_UTF16| SCONV_FROM_UTF16);
4123 from_cp = sc->from_cp;
4124 sc->from_cp = CP_UTF8;
4125 r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4126 aes->aes_utf8.s, aes->aes_utf8.length, sc);
4128 sc->from_cp = from_cp;
4130 aes->aes_set |= AES_SET_WCS;
4133 r = archive_wstring_append_from_mbs_in_codepage(
4134 &(aes->aes_wcs), mbs, len, sc);
4136 aes->aes_set = AES_SET_WCS;
4141 r = archive_strncpy_l(&(aes->aes_mbs), mbs, len, sc);
4143 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4151 * The 'update' form tries to proactively update all forms of
4152 * this string (WCS and MBS) and returns an error if any of
4153 * them fail. This is used by the 'pax' handler, for instance,
4154 * to detect and report character-conversion failures early while
4155 * still allowing clients to get potentially useful values from
4156 * the more tolerant lazy conversions. (get_mbs and get_wcs will
4157 * strive to give the user something useful, so you can get hopefully
4158 * usable values even if some of the character conversions are failing.)
4161 archive_mstring_update_utf8(struct archive *a, struct archive_mstring *aes,
4164 struct archive_string_conv *sc;
4169 return (0); /* Succeeded in clearing everything. */
4172 /* Save the UTF8 string. */
4173 archive_strcpy(&(aes->aes_utf8), utf8);
4175 /* Empty the mbs and wcs strings. */
4176 archive_string_empty(&(aes->aes_mbs));
4177 archive_wstring_empty(&(aes->aes_wcs));
4179 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
4181 /* Try converting UTF-8 to MBS, return false on failure. */
4182 sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4184 return (-1);/* Couldn't allocate memory for sc. */
4185 r = archive_strcpy_l(&(aes->aes_mbs), utf8, sc);
4187 free_sconv_object(sc);
4190 aes->aes_set = AES_SET_UTF8 | AES_SET_MBS; /* Both UTF8 and MBS set. */
4192 /* Try converting MBS to WCS, return false on failure. */
4193 if (archive_wstring_append_from_mbs(&(aes->aes_wcs), aes->aes_mbs.s,
4194 aes->aes_mbs.length))
4196 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
4198 /* All conversions succeeded. */