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;
222 struct archive_string *
223 archive_array_append(struct archive_string *as, const char *p, size_t s)
225 return archive_string_append(as, p, s);
229 archive_string_concat(struct archive_string *dest, struct archive_string *src)
231 if (archive_string_append(dest, src->s, src->length) == NULL)
232 __archive_errx(1, "Out of memory");
236 archive_wstring_concat(struct archive_wstring *dest,
237 struct archive_wstring *src)
239 if (archive_wstring_append(dest, src->s, src->length) == NULL)
240 __archive_errx(1, "Out of memory");
244 archive_string_free(struct archive_string *as)
247 as->buffer_length = 0;
253 archive_wstring_free(struct archive_wstring *as)
256 as->buffer_length = 0;
261 struct archive_wstring *
262 archive_wstring_ensure(struct archive_wstring *as, size_t s)
264 return (struct archive_wstring *)
265 archive_string_ensure((struct archive_string *)as,
266 s * sizeof(wchar_t));
269 /* Returns NULL on any allocation failure. */
270 struct archive_string *
271 archive_string_ensure(struct archive_string *as, size_t s)
276 /* If buffer is already big enough, don't reallocate. */
277 if (as->s && (s <= as->buffer_length))
281 * Growing the buffer at least exponentially ensures that
282 * append operations are always linear in the number of
283 * characters appended. Using a smaller growth rate for
284 * larger buffers reduces memory waste somewhat at the cost of
285 * a larger constant factor.
287 if (as->buffer_length < 32)
288 /* Start with a minimum 32-character buffer. */
290 else if (as->buffer_length < 8192)
291 /* Buffers under 8k are doubled for speed. */
292 new_length = as->buffer_length + as->buffer_length;
294 /* Buffers 8k and over grow by at least 25% each time. */
295 new_length = as->buffer_length + as->buffer_length / 4;
296 /* Be safe: If size wraps, fail. */
297 if (new_length < as->buffer_length) {
298 /* On failure, wipe the string and return NULL. */
299 archive_string_free(as);
300 errno = ENOMEM;/* Make sure errno has ENOMEM. */
305 * The computation above is a lower limit to how much we'll
306 * grow the buffer. In any case, we have to grow it enough to
311 /* Now we can reallocate the buffer. */
312 p = (char *)realloc(as->s, new_length);
314 /* On failure, wipe the string and return NULL. */
315 archive_string_free(as);
316 errno = ENOMEM;/* Make sure errno has ENOMEM. */
321 as->buffer_length = new_length;
326 * TODO: See if there's a way to avoid scanning
327 * the source string twice. Then test to see
328 * if it actually helps (remember that we're almost
329 * always called with pretty short arguments, so
330 * such an optimization might not help).
332 struct archive_string *
333 archive_strncat(struct archive_string *as, const void *_p, size_t n)
338 p = (const char *)_p;
340 /* Like strlen(p), except won't examine positions beyond p[n]. */
343 while (s < n && *pp) {
347 if ((as = archive_string_append(as, p, s)) == NULL)
348 __archive_errx(1, "Out of memory");
352 struct archive_wstring *
353 archive_wstrncat(struct archive_wstring *as, const wchar_t *p, size_t n)
358 /* Like strlen(p), except won't examine positions beyond p[n]. */
361 while (s < n && *pp) {
365 if ((as = archive_wstring_append(as, p, s)) == NULL)
366 __archive_errx(1, "Out of memory");
370 struct archive_string *
371 archive_strcat(struct archive_string *as, const void *p)
373 /* strcat is just strncat without an effective limit.
374 * Assert that we'll never get called with a source
376 * TODO: Review all uses of strcat in the source
377 * and try to replace them with strncat().
379 return archive_strncat(as, p, 0x1000000);
382 struct archive_wstring *
383 archive_wstrcat(struct archive_wstring *as, const wchar_t *p)
386 return archive_wstrncat(as, p, 0x1000000);
389 struct archive_string *
390 archive_strappend_char(struct archive_string *as, char c)
392 if ((as = archive_string_append(as, &c, 1)) == NULL)
393 __archive_errx(1, "Out of memory");
397 struct archive_wstring *
398 archive_wstrappend_wchar(struct archive_wstring *as, wchar_t c)
400 if ((as = archive_wstring_append(as, &c, 1)) == NULL)
401 __archive_errx(1, "Out of memory");
406 * Get the "current character set" name to use with iconv.
407 * On FreeBSD, the empty character set name "" chooses
408 * the correct character encoding for the current locale,
409 * so this isn't necessary.
410 * But iconv on Mac OS 10.6 doesn't seem to handle this correctly;
411 * on that system, we have to explicitly call nl_langinfo()
412 * to get the right name. Not sure about other platforms.
414 * NOTE: GNU libiconv does not recognize the character-set name
415 * which some platform nl_langinfo(CODESET) returns, so we should
416 * use locale_charset() instead of nl_langinfo(CODESET) for GNU libiconv.
419 default_iconv_charset(const char *charset) {
420 if (charset != NULL && charset[0] != '\0')
422 #if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
423 /* locale_charset() is broken on Mac OS */
424 return locale_charset();
425 #elif HAVE_NL_LANGINFO
426 return nl_langinfo(CODESET);
432 #if defined(_WIN32) && !defined(__CYGWIN__)
435 * Convert MBS to WCS.
436 * Note: returns -1 if conversion fails.
439 archive_wstring_append_from_mbs(struct archive_wstring *dest,
440 const char *p, size_t len)
442 return archive_wstring_append_from_mbs_in_codepage(dest, p, len, NULL);
446 archive_wstring_append_from_mbs_in_codepage(struct archive_wstring *dest,
447 const char *s, size_t length, struct archive_string_conv *sc)
453 from_cp = sc->from_cp;
455 from_cp = get_current_codepage();
457 if (from_cp == CP_C_LOCALE) {
459 * "C" locale special process.
462 const unsigned char *mp;
464 if (NULL == archive_wstring_ensure(dest,
465 dest->length + length + 1))
468 ws = dest->s + dest->length;
469 mp = (const unsigned char *)s;
471 while (count < (int)length && *mp) {
472 *ws++ = (wchar_t)*mp++;
475 } else if (sc != NULL &&
476 (sc->flag & (SCONV_NORMALIZATION_C | SCONV_NORMALIZATION_D))) {
478 * Normalize UTF-8 and UTF-16BE and convert it directly
479 * to UTF-16 as wchar_t.
481 struct archive_string u16;
482 int saved_flag = sc->flag;/* save current flag. */
485 sc->flag |= SCONV_TO_UTF16BE;
487 sc->flag |= SCONV_TO_UTF16LE;
489 if (sc->flag & SCONV_FROM_UTF16) {
491 * UTF-16BE/LE NFD ===> UTF-16 NFC
492 * UTF-16BE/LE NFC ===> UTF-16 NFD
494 count = (int)utf16nbytes(s, length);
497 * UTF-8 NFD ===> UTF-16 NFC
498 * UTF-8 NFC ===> UTF-16 NFD
500 count = (int)mbsnbytes(s, length);
502 u16.s = (char *)dest->s;
503 u16.length = dest->length << 1;;
504 u16.buffer_length = dest->buffer_length;
505 if (sc->flag & SCONV_NORMALIZATION_C)
506 ret = archive_string_normalize_C(&u16, s, count, sc);
508 ret = archive_string_normalize_D(&u16, s, count, sc);
509 dest->s = (wchar_t *)u16.s;
510 dest->length = u16.length >> 1;
511 dest->buffer_length = u16.buffer_length;
512 sc->flag = saved_flag;/* restore the saved flag. */
514 } else if (sc != NULL && (sc->flag & SCONV_FROM_UTF16)) {
515 count = (int)utf16nbytes(s, length);
516 count >>= 1; /* to be WCS length */
517 /* Allocate memory for WCS. */
518 if (NULL == archive_wstring_ensure(dest,
519 dest->length + count + 1))
521 wmemcpy(dest->s + dest->length, (const wchar_t *)s, count);
522 if ((sc->flag & SCONV_FROM_UTF16BE) && !is_big_endian()) {
523 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
525 for (b = 0; b < count; b++) {
526 uint16_t val = archive_le16dec(u16+b);
527 archive_be16enc(u16+b, val);
529 } else if ((sc->flag & SCONV_FROM_UTF16LE) && is_big_endian()) {
530 uint16_t *u16 = (uint16_t *)(dest->s + dest->length);
532 for (b = 0; b < count; b++) {
533 uint16_t val = archive_be16dec(u16+b);
534 archive_le16enc(u16+b, val);
543 else if (sc->flag & SCONV_FROM_CHARSET) {
544 /* Do not trust the length which comes from
545 * an archive file. */
546 length = mbsnbytes(s, length);
549 mbflag = MB_PRECOMPOSED;
551 buffsize = dest->length + length + 1;
553 /* Allocate memory for WCS. */
554 if (NULL == archive_wstring_ensure(dest, buffsize))
556 /* Convert MBS to WCS. */
557 count = MultiByteToWideChar(from_cp,
558 mbflag, s, (int)length, dest->s + dest->length,
559 (int)(dest->buffer_length >> 1) -1);
561 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
562 /* Expand the WCS buffer. */
563 buffsize = dest->buffer_length << 1;
566 if (count == 0 && length != 0)
571 dest->length += count;
572 dest->s[dest->length] = L'\0';
579 * Convert MBS to WCS.
580 * Note: returns -1 if conversion fails.
583 archive_wstring_append_from_mbs(struct archive_wstring *dest,
584 const char *p, size_t len)
589 * No single byte will be more than one wide character,
590 * so this length estimate will always be big enough.
592 size_t wcs_length = len;
593 size_t mbs_length = len;
597 mbstate_t shift_state;
599 memset(&shift_state, 0, sizeof(shift_state));
601 if (NULL == archive_wstring_ensure(dest, dest->length + wcs_length + 1))
603 wcs = dest->s + dest->length;
605 * We cannot use mbsrtowcs/mbstowcs here because those may convert
606 * extra MBS when strlen(p) > len and one wide character consists of
609 while (*mbs && mbs_length > 0) {
610 if (wcs_length == 0) {
611 dest->length = wcs - dest->s;
612 dest->s[dest->length] = L'\0';
613 wcs_length = mbs_length;
614 if (NULL == archive_wstring_ensure(dest,
615 dest->length + wcs_length + 1))
617 wcs = dest->s + dest->length;
620 r = mbrtowc(wcs, mbs, wcs_length, &shift_state);
622 r = mbtowc(wcs, mbs, wcs_length);
624 if (r == (size_t)-1 || r == (size_t)-2) {
626 if (errno == EILSEQ) {
633 if (r == 0 || r > mbs_length)
640 dest->length = wcs - dest->s;
641 dest->s[dest->length] = L'\0';
647 #if defined(_WIN32) && !defined(__CYGWIN__)
651 * Note: returns -1 if conversion fails.
653 * Win32 builds use WideCharToMultiByte from the Windows API.
654 * (Maybe Cygwin should too? WideCharToMultiByte will know a
655 * lot more about local character encodings than the wcrtomb()
656 * wrapper is going to know.)
659 archive_string_append_from_wcs(struct archive_string *as,
660 const wchar_t *w, size_t len)
662 return archive_string_append_from_wcs_in_codepage(as, w, len, NULL);
666 archive_string_append_from_wcs_in_codepage(struct archive_string *as,
667 const wchar_t *ws, size_t len, struct archive_string_conv *sc)
669 BOOL defchar_used, *dp;
672 int wslen = (int)len;
677 to_cp = get_current_codepage();
679 if (to_cp == CP_C_LOCALE) {
681 * "C" locale special process.
683 const wchar_t *wp = ws;
686 if (NULL == archive_string_ensure(as,
687 as->length + wslen +1))
689 p = as->s + as->length;
692 while (count < wslen && *wp) {
701 } else if (sc != NULL && (sc->flag & SCONV_TO_UTF16)) {
705 archive_string_ensure(as, as->length + len * 2 + 2))
707 u16 = (uint16_t *)(as->s + as->length);
710 if (sc->flag & SCONV_TO_UTF16BE) {
711 while (count < (int)len && *ws) {
712 archive_be16enc(u16+count, *ws);
717 while (count < (int)len && *ws) {
718 archive_le16enc(u16+count, *ws);
723 count <<= 1; /* to be byte size */
725 /* Make sure the MBS buffer has plenty to set. */
727 archive_string_ensure(as, as->length + len * 2 + 1))
731 if (to_cp == CP_UTF8 || sc == NULL)
735 count = WideCharToMultiByte(to_cp, 0, ws, wslen,
736 as->s + as->length, (int)as->buffer_length-1, NULL, dp);
738 GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
739 /* Expand the MBS buffer and retry. */
740 if (NULL == archive_string_ensure(as,
741 as->buffer_length + len))
751 as->s[as->length] = '\0';
752 return (defchar_used?-1:ret);
755 #elif defined(HAVE_WCTOMB) || defined(HAVE_WCRTOMB)
758 * Translates a wide character string into current locale character set
759 * and appends to the archive_string. Note: returns -1 if conversion
763 archive_string_append_from_wcs(struct archive_string *as,
764 const wchar_t *w, size_t len)
766 /* We cannot use the standard wcstombs() here because it
767 * cannot tell us how big the output buffer should be. So
768 * I've built a loop around wcrtomb() or wctomb() that
769 * converts a character at a time and resizes the string as
770 * needed. We prefer wcrtomb() when it's available because
771 * it's thread-safe. */
776 mbstate_t shift_state;
778 memset(&shift_state, 0, sizeof(shift_state));
780 /* Clear the shift state before starting. */
784 * Allocate buffer for MBS.
785 * We need this allocation here since it is possible that
786 * as->s is still NULL.
788 if (archive_string_ensure(as, as->length + len + 1) == NULL)
791 p = as->s + as->length;
792 end = as->s + as->buffer_length - MB_CUR_MAX -1;
793 while (*w != L'\0' && len > 0) {
795 as->length = p - as->s;
796 as->s[as->length] = '\0';
797 /* Re-allocate buffer for MBS. */
798 if (archive_string_ensure(as,
799 as->length + len * 2 + 1) == NULL)
801 p = as->s + as->length;
802 end = as->s + as->buffer_length - MB_CUR_MAX -1;
805 n = wcrtomb(p, *w++, &shift_state);
810 if (errno == EILSEQ) {
811 /* Skip an illegal wide char. */
822 as->length = p - as->s;
823 as->s[as->length] = '\0';
827 #else /* HAVE_WCTOMB || HAVE_WCRTOMB */
830 * TODO: Test if __STDC_ISO_10646__ is defined.
831 * Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
832 * one character at a time. If a non-Windows platform doesn't have
833 * either of these, fall back to the built-in UTF8 conversion.
836 archive_string_append_from_wcs(struct archive_string *as,
837 const wchar_t *w, size_t len)
839 (void)as;/* UNUSED */
841 (void)len;/* UNUSED */
846 #endif /* HAVE_WCTOMB || HAVE_WCRTOMB */
849 * Find a string conversion object by a pair of 'from' charset name
850 * and 'to' charset name from an archive object.
851 * Return NULL if not found.
853 static struct archive_string_conv *
854 find_sconv_object(struct archive *a, const char *fc, const char *tc)
856 struct archive_string_conv *sc;
861 for (sc = a->sconv; sc != NULL; sc = sc->next) {
862 if (strcmp(sc->from_charset, fc) == 0 &&
863 strcmp(sc->to_charset, tc) == 0)
870 * Register a string object to an archive object.
873 add_sconv_object(struct archive *a, struct archive_string_conv *sc)
875 struct archive_string_conv **psc;
877 /* Add a new sconv to sconv list. */
880 psc = &((*psc)->next);
885 add_converter(struct archive_string_conv *sc, int (*converter)
886 (struct archive_string *, const void *, size_t,
887 struct archive_string_conv *))
889 if (sc == NULL || sc->nconverter >= 2)
890 __archive_errx(1, "Programing error");
891 sc->converter[sc->nconverter++] = converter;
895 setup_converter(struct archive_string_conv *sc)
902 * Perform special sequence for the incorrect UTF-8 filenames
903 * made by libarchive2.x.
905 if (sc->flag & SCONV_UTF8_LIBARCHIVE_2) {
906 add_converter(sc, strncat_from_utf8_libarchive2);
911 * Convert a string to UTF-16BE/LE.
913 if (sc->flag & SCONV_TO_UTF16) {
915 * If the current locale is UTF-8, we can translate
916 * a UTF-8 string into a UTF-16BE string.
918 if (sc->flag & SCONV_FROM_UTF8) {
919 add_converter(sc, archive_string_append_unicode);
923 #if defined(_WIN32) && !defined(__CYGWIN__)
924 if (sc->flag & SCONV_WIN_CP) {
925 if (sc->flag & SCONV_TO_UTF16BE)
926 add_converter(sc, win_strncat_to_utf16be);
928 add_converter(sc, win_strncat_to_utf16le);
933 #if defined(HAVE_ICONV)
934 if (sc->cd != (iconv_t)-1) {
935 add_converter(sc, iconv_strncat_in_locale);
940 if (sc->flag & SCONV_BEST_EFFORT) {
941 if (sc->flag & SCONV_TO_UTF16BE)
943 best_effort_strncat_to_utf16be);
946 best_effort_strncat_to_utf16le);
948 /* Make sure we have no converter. */
954 * Convert a string from UTF-16BE/LE.
956 if (sc->flag & SCONV_FROM_UTF16) {
958 * At least we should normalize a UTF-16BE string.
960 if (sc->flag & SCONV_NORMALIZATION_D)
961 add_converter(sc,archive_string_normalize_D);
962 else if (sc->flag & SCONV_NORMALIZATION_C)
963 add_converter(sc, archive_string_normalize_C);
965 if (sc->flag & SCONV_TO_UTF8) {
967 * If the current locale is UTF-8, we can translate
968 * a UTF-16BE/LE string into a UTF-8 string directly.
971 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
973 archive_string_append_unicode);
977 #if defined(_WIN32) && !defined(__CYGWIN__)
978 if (sc->flag & SCONV_WIN_CP) {
979 if (sc->flag & SCONV_FROM_UTF16BE)
980 add_converter(sc, win_strncat_from_utf16be);
982 add_converter(sc, win_strncat_from_utf16le);
987 #if defined(HAVE_ICONV)
988 if (sc->cd != (iconv_t)-1) {
989 add_converter(sc, iconv_strncat_in_locale);
994 if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
995 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16BE))
996 add_converter(sc, best_effort_strncat_from_utf16be);
997 else if ((sc->flag & (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
998 == (SCONV_BEST_EFFORT | SCONV_FROM_UTF16LE))
999 add_converter(sc, best_effort_strncat_from_utf16le);
1001 /* Make sure we have no converter. */
1006 if (sc->flag & SCONV_FROM_UTF8) {
1008 * At least we should normalize a UTF-8 string.
1010 if (sc->flag & SCONV_NORMALIZATION_D)
1011 add_converter(sc,archive_string_normalize_D);
1012 else if (sc->flag & SCONV_NORMALIZATION_C)
1013 add_converter(sc, archive_string_normalize_C);
1016 * Copy UTF-8 string with a check of CESU-8.
1017 * Apparently, iconv does not check surrogate pairs in UTF-8
1018 * when both from-charset and to-charset are UTF-8, and then
1019 * we use our UTF-8 copy code.
1021 if (sc->flag & SCONV_TO_UTF8) {
1023 * If the current locale is UTF-8, we can translate
1024 * a UTF-16BE string into a UTF-8 string directly.
1027 (SCONV_NORMALIZATION_D |SCONV_NORMALIZATION_C)))
1028 add_converter(sc, strncat_from_utf8_to_utf8);
1033 #if defined(_WIN32) && !defined(__CYGWIN__)
1035 * On Windows we can use Windows API for a string conversion.
1037 if (sc->flag & SCONV_WIN_CP) {
1038 add_converter(sc, strncat_in_codepage);
1044 if (sc->cd != (iconv_t)-1) {
1045 add_converter(sc, iconv_strncat_in_locale);
1047 * iconv generally does not support UTF-8-MAC and so
1048 * we have to the output of iconv from NFC to NFD if
1051 if ((sc->flag & SCONV_FROM_CHARSET) &&
1052 (sc->flag & SCONV_TO_UTF8)) {
1053 if (sc->flag & SCONV_NORMALIZATION_D)
1054 add_converter(sc, archive_string_normalize_D);
1061 * Try conversion in the best effort or no conversion.
1063 if ((sc->flag & SCONV_BEST_EFFORT) || sc->same)
1064 add_converter(sc, best_effort_strncat_in_locale);
1066 /* Make sure we have no converter. */
1071 * Return canonicalized charset-name but this supports just UTF-8, UTF-16BE
1072 * and CP932 which are referenced in create_sconv_object().
1075 canonical_charset_name(const char *charset)
1081 if (charset == NULL || charset[0] == '\0'
1082 || strlen(charset) > 15)
1085 /* Copy name to uppercase. */
1090 if (c >= 'a' && c <= 'z')
1096 if (strcmp(cs, "UTF-8") == 0 ||
1097 strcmp(cs, "UTF8") == 0)
1099 if (strcmp(cs, "UTF-16BE") == 0 ||
1100 strcmp(cs, "UTF16BE") == 0)
1101 return ("UTF-16BE");
1102 if (strcmp(cs, "UTF-16LE") == 0 ||
1103 strcmp(cs, "UTF16LE") == 0)
1104 return ("UTF-16LE");
1105 if (strcmp(cs, "CP932") == 0)
1111 * Create a string conversion object.
1113 static struct archive_string_conv *
1114 create_sconv_object(const char *fc, const char *tc,
1115 unsigned current_codepage, int flag)
1117 struct archive_string_conv *sc;
1119 sc = calloc(1, sizeof(*sc));
1123 sc->from_charset = strdup(fc);
1124 if (sc->from_charset == NULL) {
1128 sc->to_charset = strdup(tc);
1129 if (sc->to_charset == NULL) {
1130 free(sc->from_charset);
1134 archive_string_init(&sc->utftmp);
1136 if (flag & SCONV_TO_CHARSET) {
1138 * Convert characters from the current locale charset to
1139 * a specified charset.
1141 sc->from_cp = current_codepage;
1142 sc->to_cp = make_codepage_from_charset(tc);
1143 #if defined(_WIN32) && !defined(__CYGWIN__)
1144 if (IsValidCodePage(sc->to_cp))
1145 flag |= SCONV_WIN_CP;
1147 } else if (flag & SCONV_FROM_CHARSET) {
1149 * Convert characters from a specified charset to
1150 * the current locale charset.
1152 sc->to_cp = current_codepage;
1153 sc->from_cp = make_codepage_from_charset(fc);
1154 #if defined(_WIN32) && !defined(__CYGWIN__)
1155 if (IsValidCodePage(sc->from_cp))
1156 flag |= SCONV_WIN_CP;
1161 * Check if "from charset" and "to charset" are the same.
1163 if (strcmp(fc, tc) == 0 ||
1164 (sc->from_cp != (unsigned)-1 && sc->from_cp == sc->to_cp))
1170 * Mark if "from charset" or "to charset" are UTF-8 or UTF-16BE/LE.
1172 if (strcmp(tc, "UTF-8") == 0)
1173 flag |= SCONV_TO_UTF8;
1174 else if (strcmp(tc, "UTF-16BE") == 0)
1175 flag |= SCONV_TO_UTF16BE;
1176 else if (strcmp(tc, "UTF-16LE") == 0)
1177 flag |= SCONV_TO_UTF16LE;
1178 if (strcmp(fc, "UTF-8") == 0)
1179 flag |= SCONV_FROM_UTF8;
1180 else if (strcmp(fc, "UTF-16BE") == 0)
1181 flag |= SCONV_FROM_UTF16BE;
1182 else if (strcmp(fc, "UTF-16LE") == 0)
1183 flag |= SCONV_FROM_UTF16LE;
1184 #if defined(_WIN32) && !defined(__CYGWIN__)
1185 if (sc->to_cp == CP_UTF8)
1186 flag |= SCONV_TO_UTF8;
1187 else if (sc->to_cp == CP_UTF16BE)
1188 flag |= SCONV_TO_UTF16BE | SCONV_WIN_CP;
1189 else if (sc->to_cp == CP_UTF16LE)
1190 flag |= SCONV_TO_UTF16LE | SCONV_WIN_CP;
1191 if (sc->from_cp == CP_UTF8)
1192 flag |= SCONV_FROM_UTF8;
1193 else if (sc->from_cp == CP_UTF16BE)
1194 flag |= SCONV_FROM_UTF16BE | SCONV_WIN_CP;
1195 else if (sc->from_cp == CP_UTF16LE)
1196 flag |= SCONV_FROM_UTF16LE | SCONV_WIN_CP;
1200 * Set a flag for Unicode NFD. Usually iconv cannot correctly
1201 * handle it. So we have to translate NFD characters to NFC ones
1202 * ourselves before iconv handles. Another reason is to prevent
1203 * that the same sight of two filenames, one is NFC and other
1204 * is NFD, would be in its directory.
1205 * On Mac OS X, although its filesystem layer automatically
1206 * convert filenames to NFD, it would be useful for filename
1207 * comparing to find out the same filenames that we normalize
1208 * that to be NFD ourselves.
1210 if ((flag & SCONV_FROM_CHARSET) &&
1211 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8))) {
1212 #if defined(__APPLE__)
1213 if (flag & SCONV_TO_UTF8)
1214 flag |= SCONV_NORMALIZATION_D;
1217 flag |= SCONV_NORMALIZATION_C;
1219 #if defined(__APPLE__)
1221 * In case writing an archive file, make sure that a filename
1222 * going to be passed to iconv is a Unicode NFC string since
1223 * a filename in HFS Plus filesystem is a Unicode NFD one and
1224 * iconv cannot handle it with "UTF-8" charset. It is simpler
1225 * than a use of "UTF-8-MAC" charset.
1227 if ((flag & SCONV_TO_CHARSET) &&
1228 (flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1229 !(flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1230 flag |= SCONV_NORMALIZATION_C;
1232 * In case reading an archive file. make sure that a filename
1233 * will be passed to users is a Unicode NFD string in order to
1234 * correctly compare the filename with other one which comes
1235 * from HFS Plus filesystem.
1237 if ((flag & SCONV_FROM_CHARSET) &&
1238 !(flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1239 (flag & SCONV_TO_UTF8))
1240 flag |= SCONV_NORMALIZATION_D;
1243 #if defined(HAVE_ICONV)
1244 sc->cd_w = (iconv_t)-1;
1246 * Create an iconv object.
1248 if (((flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) &&
1249 (flag & (SCONV_FROM_UTF8 | SCONV_FROM_UTF16))) ||
1250 (flag & SCONV_WIN_CP)) {
1251 /* This case we won't use iconv. */
1252 sc->cd = (iconv_t)-1;
1254 sc->cd = iconv_open(tc, fc);
1255 if (sc->cd == (iconv_t)-1 && (sc->flag & SCONV_BEST_EFFORT)) {
1257 * Unfortunately, all of iconv implements do support
1258 * "CP932" character-set, so we should use "SJIS"
1259 * instead if iconv_open failed.
1261 if (strcmp(tc, "CP932") == 0)
1262 sc->cd = iconv_open("SJIS", fc);
1263 else if (strcmp(fc, "CP932") == 0)
1264 sc->cd = iconv_open(tc, "SJIS");
1266 #if defined(_WIN32) && !defined(__CYGWIN__)
1268 * archive_mstring on Windows directly convert multi-bytes
1269 * into archive_wstring in order not to depend on locale
1270 * so that you can do a I18N programming. This will be
1271 * used only in archive_mstring_copy_mbs_len_l so far.
1273 if (flag & SCONV_FROM_CHARSET) {
1274 sc->cd_w = iconv_open("UTF-8", fc);
1275 if (sc->cd_w == (iconv_t)-1 &&
1276 (sc->flag & SCONV_BEST_EFFORT)) {
1277 if (strcmp(fc, "CP932") == 0)
1278 sc->cd_w = iconv_open("UTF-8", "SJIS");
1281 #endif /* _WIN32 && !__CYGWIN__ */
1283 #endif /* HAVE_ICONV */
1288 * Set up converters.
1290 setup_converter(sc);
1296 * Free a string conversion object.
1299 free_sconv_object(struct archive_string_conv *sc)
1301 free(sc->from_charset);
1302 free(sc->to_charset);
1303 archive_string_free(&sc->utftmp);
1305 if (sc->cd != (iconv_t)-1)
1306 iconv_close(sc->cd);
1307 if (sc->cd_w != (iconv_t)-1)
1308 iconv_close(sc->cd_w);
1313 #if defined(_WIN32) && !defined(__CYGWIN__)
1315 my_atoi(const char *p)
1321 if (*p >= '0' && *p <= '9')
1322 cp = cp * 10 + (*p - '0');
1331 * Translate Charset name (as used by iconv) into CodePage (as used by Windows)
1332 * Return -1 if failed.
1334 * Note: This translation code may be insufficient.
1336 static struct charset {
1340 /* MUST BE SORTED! */
1359 {"HZ-GB-2312", 52936},
1377 {"ISO-8859-1", 28591},
1378 {"ISO-8859-13", 28603},
1379 {"ISO-8859-15", 28605},
1380 {"ISO-8859-2", 28592},
1381 {"ISO-8859-3", 28593},
1382 {"ISO-8859-4", 28594},
1383 {"ISO-8859-5", 28595},
1384 {"ISO-8859-6", 28596},
1385 {"ISO-8859-7", 28597},
1386 {"ISO-8859-8", 28598},
1387 {"ISO-8859-9", 28599},
1388 {"ISO8859-1", 28591},
1389 {"ISO8859-13", 28603},
1390 {"ISO8859-15", 28605},
1391 {"ISO8859-2", 28592},
1392 {"ISO8859-3", 28593},
1393 {"ISO8859-4", 28594},
1394 {"ISO8859-5", 28595},
1395 {"ISO8859-6", 28596},
1396 {"ISO8859-7", 28597},
1397 {"ISO8859-8", 28598},
1398 {"ISO8859-9", 28599},
1402 {"KS_C_5601-1987", 949},
1405 {"MACINTOSH", 10000},
1415 {"X-EUROPA", 29001},
1416 {"X-MAC-ARABIC", 10004},
1417 {"X-MAC-CE", 10029},
1418 {"X-MAC-CHINESEIMP", 10008},
1419 {"X-MAC-CHINESETRAD", 10002},
1420 {"X-MAC-CROATIAN", 10082},
1421 {"X-MAC-CYRILLIC", 10007},
1422 {"X-MAC-GREEK", 10006},
1423 {"X-MAC-HEBREW", 10005},
1424 {"X-MAC-ICELANDIC", 10079},
1425 {"X-MAC-JAPANESE", 10001},
1426 {"X-MAC-KOREAN", 10003},
1427 {"X-MAC-ROMANIAN", 10010},
1428 {"X-MAC-THAI", 10021},
1429 {"X-MAC-TURKISH", 10081},
1430 {"X-MAC-UKRAINIAN", 10017},
1433 make_codepage_from_charset(const char *charset)
1440 if (charset == NULL || strlen(charset) > 15)
1443 /* Copy name to uppercase. */
1446 char c = *charset++;
1447 if (c >= 'a' && c <= 'z')
1454 /* Look it up in the table first, so that we can easily
1455 * override CP367, which we map to 1252 instead of 367. */
1457 b = sizeof(charsets)/sizeof(charsets[0]);
1459 int c = (b + a) / 2;
1460 int r = strcmp(charsets[c].name, cs);
1466 return charsets[c].cp;
1469 /* If it's not in the table, try to parse it. */
1472 if (cs[1] == 'P' && cs[2] >= '0' && cs[2] <= '9') {
1473 cp = my_atoi(cs + 2);
1474 } else if (strcmp(cs, "CP_ACP") == 0)
1475 cp = get_current_codepage();
1476 else if (strcmp(cs, "CP_OEMCP") == 0)
1477 cp = get_current_oemcp();
1480 if (cs[1] == 'B' && cs[2] == 'M' &&
1481 cs[3] >= '0' && cs[3] <= '9') {
1482 cp = my_atoi(cs + 3);
1486 if (strncmp(cs, "WINDOWS-", 8) == 0) {
1487 cp = my_atoi(cs + 8);
1488 if (cp != 874 && (cp < 1250 || cp > 1258))
1489 cp = -1;/* This may invalid code. */
1497 * Return ANSI Code Page of current locale set by setlocale().
1500 get_current_codepage(void)
1505 locale = setlocale(LC_CTYPE, NULL);
1508 if (locale[0] == 'C' && locale[1] == '\0')
1509 return (CP_C_LOCALE);
1510 p = strrchr(locale, '.');
1520 * Translation table between Locale Name and ACP/OEMCP.
1527 { 950, 950, "Chinese_Taiwan" },
1528 { 936, 936, "Chinese_People's Republic of China" },
1529 { 950, 950, "Chinese_Taiwan" },
1530 { 1250, 852, "Czech_Czech Republic" },
1531 { 1252, 850, "Danish_Denmark" },
1532 { 1252, 850, "Dutch_Netherlands" },
1533 { 1252, 850, "Dutch_Belgium" },
1534 { 1252, 437, "English_United States" },
1535 { 1252, 850, "English_Australia" },
1536 { 1252, 850, "English_Canada" },
1537 { 1252, 850, "English_New Zealand" },
1538 { 1252, 850, "English_United Kingdom" },
1539 { 1252, 437, "English_United States" },
1540 { 1252, 850, "Finnish_Finland" },
1541 { 1252, 850, "French_France" },
1542 { 1252, 850, "French_Belgium" },
1543 { 1252, 850, "French_Canada" },
1544 { 1252, 850, "French_Switzerland" },
1545 { 1252, 850, "German_Germany" },
1546 { 1252, 850, "German_Austria" },
1547 { 1252, 850, "German_Switzerland" },
1548 { 1253, 737, "Greek_Greece" },
1549 { 1250, 852, "Hungarian_Hungary" },
1550 { 1252, 850, "Icelandic_Iceland" },
1551 { 1252, 850, "Italian_Italy" },
1552 { 1252, 850, "Italian_Switzerland" },
1553 { 932, 932, "Japanese_Japan" },
1554 { 949, 949, "Korean_Korea" },
1555 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1556 { 1252, 850, "Norwegian (BokmOl)_Norway" },
1557 { 1252, 850, "Norwegian-Nynorsk_Norway" },
1558 { 1250, 852, "Polish_Poland" },
1559 { 1252, 850, "Portuguese_Portugal" },
1560 { 1252, 850, "Portuguese_Brazil" },
1561 { 1251, 866, "Russian_Russia" },
1562 { 1250, 852, "Slovak_Slovakia" },
1563 { 1252, 850, "Spanish_Spain" },
1564 { 1252, 850, "Spanish_Mexico" },
1565 { 1252, 850, "Spanish_Spain" },
1566 { 1252, 850, "Swedish_Sweden" },
1567 { 1254, 857, "Turkish_Turkey" },
1572 * Return OEM Code Page of current locale set by setlocale().
1575 get_current_oemcp(void)
1581 locale = setlocale(LC_CTYPE, NULL);
1583 return (GetOEMCP());
1584 if (locale[0] == 'C' && locale[1] == '\0')
1585 return (CP_C_LOCALE);
1587 p = strrchr(locale, '.');
1589 return (GetOEMCP());
1591 for (i = 0; acp_ocp_map[i].acp; i++) {
1592 if (strncmp(acp_ocp_map[i].locale, locale, len) == 0)
1593 return (acp_ocp_map[i].ocp);
1595 return (GetOEMCP());
1600 * POSIX platform does not use CodePage.
1604 get_current_codepage(void)
1606 return (-1);/* Unknown */
1609 make_codepage_from_charset(const char *charset)
1611 (void)charset; /* UNUSED */
1612 return (-1);/* Unknown */
1615 get_current_oemcp(void)
1617 return (-1);/* Unknown */
1620 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
1623 * Return a string conversion object.
1625 static struct archive_string_conv *
1626 get_sconv_object(struct archive *a, const char *fc, const char *tc, int flag)
1628 struct archive_string_conv *sc;
1629 unsigned current_codepage;
1631 /* Check if we have made the sconv object. */
1632 sc = find_sconv_object(a, fc, tc);
1637 current_codepage = get_current_codepage();
1639 current_codepage = a->current_codepage;
1641 sc = create_sconv_object(canonical_charset_name(fc),
1642 canonical_charset_name(tc), current_codepage, flag);
1645 archive_set_error(a, ENOMEM,
1646 "Could not allocate memory for "
1647 "a string conversion object");
1652 * If there is no converter for current string conversion object,
1653 * we cannot handle this conversion.
1655 if (sc->nconverter == 0) {
1658 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1659 "iconv_open failed : Cannot handle ``%s''",
1660 (flag & SCONV_TO_CHARSET)?tc:fc);
1662 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1663 "A character-set conversion not fully supported "
1664 "on this platform");
1667 /* Failed; free a sconv object. */
1668 free_sconv_object(sc);
1676 add_sconv_object(a, sc);
1681 get_current_charset(struct archive *a)
1683 const char *cur_charset;
1686 cur_charset = default_iconv_charset("");
1688 cur_charset = default_iconv_charset(a->current_code);
1689 if (a->current_code == NULL) {
1690 a->current_code = strdup(cur_charset);
1691 a->current_codepage = get_current_codepage();
1692 a->current_oemcp = get_current_oemcp();
1695 return (cur_charset);
1699 * Make and Return a string conversion object.
1700 * Return NULL if the platform does not support the specified conversion
1701 * and best_effort is 0.
1702 * If best_effort is set, A string conversion object must be returned
1703 * unless memory allocation for the object fails, but the conversion
1704 * might fail when non-ASCII code is found.
1706 struct archive_string_conv *
1707 archive_string_conversion_to_charset(struct archive *a, const char *charset,
1710 int flag = SCONV_TO_CHARSET;
1713 flag |= SCONV_BEST_EFFORT;
1714 return (get_sconv_object(a, get_current_charset(a), charset, flag));
1717 struct archive_string_conv *
1718 archive_string_conversion_from_charset(struct archive *a, const char *charset,
1721 int flag = SCONV_FROM_CHARSET;
1724 flag |= SCONV_BEST_EFFORT;
1725 return (get_sconv_object(a, charset, get_current_charset(a), flag));
1729 * archive_string_default_conversion_*_archive() are provided for Windows
1730 * platform because other archiver application use CP_OEMCP for
1731 * MultiByteToWideChar() and WideCharToMultiByte() for the filenames
1732 * in tar or zip files. But mbstowcs/wcstombs(CRT) usually use CP_ACP
1733 * unless you use setlocale(LC_ALL, ".OCP")(specify CP_OEMCP).
1734 * So we should make a string conversion between CP_ACP and CP_OEMCP
1735 * for compatibility.
1737 #if defined(_WIN32) && !defined(__CYGWIN__)
1738 struct archive_string_conv *
1739 archive_string_default_conversion_for_read(struct archive *a)
1741 const char *cur_charset = get_current_charset(a);
1744 /* NOTE: a check of cur_charset is unneeded but we need
1745 * that get_current_charset() has been surely called at
1746 * this time whatever C compiler optimized. */
1747 if (cur_charset != NULL &&
1748 (a->current_codepage == CP_C_LOCALE ||
1749 a->current_codepage == a->current_oemcp))
1750 return (NULL);/* no conversion. */
1752 _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1753 /* Make sure a null termination must be set. */
1754 oemcp[sizeof(oemcp)-1] = '\0';
1755 return (get_sconv_object(a, oemcp, cur_charset,
1756 SCONV_FROM_CHARSET));
1759 struct archive_string_conv *
1760 archive_string_default_conversion_for_write(struct archive *a)
1762 const char *cur_charset = get_current_charset(a);
1765 /* NOTE: a check of cur_charset is unneeded but we need
1766 * that get_current_charset() has been surely called at
1767 * this time whatever C compiler optimized. */
1768 if (cur_charset != NULL &&
1769 (a->current_codepage == CP_C_LOCALE ||
1770 a->current_codepage == a->current_oemcp))
1771 return (NULL);/* no conversion. */
1773 _snprintf(oemcp, sizeof(oemcp)-1, "CP%d", a->current_oemcp);
1774 /* Make sure a null termination must be set. */
1775 oemcp[sizeof(oemcp)-1] = '\0';
1776 return (get_sconv_object(a, cur_charset, oemcp,
1780 struct archive_string_conv *
1781 archive_string_default_conversion_for_read(struct archive *a)
1783 (void)a; /* UNUSED */
1787 struct archive_string_conv *
1788 archive_string_default_conversion_for_write(struct archive *a)
1790 (void)a; /* UNUSED */
1796 * Dispose of all character conversion objects in the archive object.
1799 archive_string_conversion_free(struct archive *a)
1801 struct archive_string_conv *sc;
1802 struct archive_string_conv *sc_next;
1804 for (sc = a->sconv; sc != NULL; sc = sc_next) {
1806 free_sconv_object(sc);
1809 free(a->current_code);
1810 a->current_code = NULL;
1814 * Return a conversion charset name.
1817 archive_string_conversion_charset_name(struct archive_string_conv *sc)
1819 if (sc->flag & SCONV_TO_CHARSET)
1820 return (sc->to_charset);
1822 return (sc->from_charset);
1826 * Change the behavior of a string conversion.
1829 archive_string_conversion_set_opt(struct archive_string_conv *sc, int opt)
1833 * A filename in UTF-8 was made with libarchive 2.x in a wrong
1834 * assumption that wchar_t was Unicode.
1835 * This option enables simulating the assumption in order to read
1836 * that filename correctly.
1838 case SCONV_SET_OPT_UTF8_LIBARCHIVE2X:
1839 #if (defined(_WIN32) && !defined(__CYGWIN__)) \
1840 || defined(__STDC_ISO_10646__) || defined(__APPLE__)
1842 * Nothing to do for it since wchar_t on these platforms
1843 * is really Unicode.
1845 (void)sc; /* UNUSED */
1847 if ((sc->flag & SCONV_UTF8_LIBARCHIVE_2) == 0) {
1848 sc->flag |= SCONV_UTF8_LIBARCHIVE_2;
1849 /* Set up string converters. */
1850 setup_converter(sc);
1854 case SCONV_SET_OPT_NORMALIZATION_C:
1855 if ((sc->flag & SCONV_NORMALIZATION_C) == 0) {
1856 sc->flag |= SCONV_NORMALIZATION_C;
1857 sc->flag &= ~SCONV_NORMALIZATION_D;
1858 /* Set up string converters. */
1859 setup_converter(sc);
1862 case SCONV_SET_OPT_NORMALIZATION_D:
1863 #if defined(HAVE_ICONV)
1865 * If iconv will take the string, do not change the
1866 * setting of the normalization.
1868 if (!(sc->flag & SCONV_WIN_CP) &&
1869 (sc->flag & (SCONV_FROM_UTF16 | SCONV_FROM_UTF8)) &&
1870 !(sc->flag & (SCONV_TO_UTF16 | SCONV_TO_UTF8)))
1873 if ((sc->flag & SCONV_NORMALIZATION_D) == 0) {
1874 sc->flag |= SCONV_NORMALIZATION_D;
1875 sc->flag &= ~SCONV_NORMALIZATION_C;
1876 /* Set up string converters. */
1877 setup_converter(sc);
1887 * Copy one archive_string to another in locale conversion.
1889 * archive_strncat_l();
1890 * archive_strncpy_l();
1895 mbsnbytes(const void *_p, size_t n)
1902 p = (const char *)_p;
1904 /* Like strlen(p), except won't examine positions beyond p[n]. */
1907 while (s < n && *pp) {
1915 utf16nbytes(const void *_p, size_t n)
1922 p = (const char *)_p;
1924 /* Like strlen(p), except won't examine positions beyond p[n]. */
1928 while (s < n && (pp[0] || pp[1])) {
1936 archive_strncpy_l(struct archive_string *as, const void *_p, size_t n,
1937 struct archive_string_conv *sc)
1940 return (archive_strncat_l(as, _p, n, sc));
1944 archive_strncat_l(struct archive_string *as, const void *_p, size_t n,
1945 struct archive_string_conv *sc)
1951 if (_p != NULL && n > 0) {
1952 if (sc != NULL && (sc->flag & SCONV_FROM_UTF16))
1953 length = utf16nbytes(_p, n);
1955 length = mbsnbytes(_p, n);
1958 /* We must allocate memory even if there is no data for conversion
1959 * or copy. This simulates archive_string_append behavior. */
1962 if (sc != NULL && (sc->flag & SCONV_TO_UTF16))
1964 if (archive_string_ensure(as, as->length + tn) == NULL)
1966 as->s[as->length] = 0;
1968 as->s[as->length+1] = 0;
1973 * If sc is NULL, we just make a copy.
1976 if (archive_string_append(as, _p, length) == NULL)
1977 return (-1);/* No memory */
1983 if (sc->nconverter > 1) {
1984 sc->utftmp.length = 0;
1985 r2 = sc->converter[0](&(sc->utftmp), s, length, sc);
1986 if (r2 != 0 && errno == ENOMEM)
1991 length = sc->utftmp.length;
1994 r2 = sc->converter[i](as, s, length, sc);
2003 * Return -1 if conversion fails.
2006 iconv_strncat_in_locale(struct archive_string *as, const void *_p,
2007 size_t length, struct archive_string_conv *sc)
2009 ICONV_CONST char *itp;
2014 int return_value = 0; /* success */
2015 int to_size, from_size;
2017 if (sc->flag & SCONV_TO_UTF16)
2021 if (sc->flag & SCONV_FROM_UTF16)
2026 if (archive_string_ensure(as, as->length + length*2+to_size) == NULL)
2030 itp = (char *)(uintptr_t)_p;
2032 outp = as->s + as->length;
2033 avail = as->buffer_length - as->length - to_size;
2034 while (remaining >= (size_t)from_size) {
2035 size_t result = iconv(cd, &itp, &remaining, &outp, &avail);
2037 if (result != (size_t)-1)
2038 break; /* Conversion completed. */
2040 if (errno == EILSEQ || errno == EINVAL) {
2042 * If an output charset is UTF-8 or UTF-16BE/LE,
2043 * unknown character should be U+FFFD
2044 * (replacement character).
2046 if (sc->flag & (SCONV_TO_UTF8 | SCONV_TO_UTF16)) {
2048 if (sc->flag & SCONV_TO_UTF8)
2049 rbytes = sizeof(utf8_replacement_char);
2053 if (avail < rbytes) {
2054 as->length = outp - as->s;
2055 bs = as->buffer_length +
2056 (remaining * to_size) + rbytes;
2058 archive_string_ensure(as, bs))
2060 outp = as->s + as->length;
2061 avail = as->buffer_length
2062 - as->length - to_size;
2064 if (sc->flag & SCONV_TO_UTF8)
2065 memcpy(outp, utf8_replacement_char, sizeof(utf8_replacement_char));
2066 else if (sc->flag & SCONV_TO_UTF16BE)
2067 archive_be16enc(outp, UNICODE_R_CHAR);
2069 archive_le16enc(outp, UNICODE_R_CHAR);
2073 /* Skip the illegal input bytes. */
2078 remaining -= from_size;
2079 return_value = -1; /* failure */
2081 /* E2BIG no output buffer,
2082 * Increase an output buffer. */
2083 as->length = outp - as->s;
2084 bs = as->buffer_length + remaining * 2;
2085 if (NULL == archive_string_ensure(as, bs))
2087 outp = as->s + as->length;
2088 avail = as->buffer_length - as->length - to_size;
2091 as->length = outp - as->s;
2092 as->s[as->length] = 0;
2094 as->s[as->length+1] = 0;
2095 return (return_value);
2098 #endif /* HAVE_ICONV */
2101 #if defined(_WIN32) && !defined(__CYGWIN__)
2104 * Translate a string from a some CodePage to an another CodePage by
2105 * Windows APIs, and copy the result. Return -1 if conversion fails.
2108 strncat_in_codepage(struct archive_string *as,
2109 const void *_p, size_t length, struct archive_string_conv *sc)
2111 const char *s = (const char *)_p;
2112 struct archive_wstring aws;
2116 archive_string_init(&aws);
2117 saved_flag = sc->flag;
2118 sc->flag &= ~(SCONV_NORMALIZATION_D | SCONV_NORMALIZATION_C);
2119 r = archive_wstring_append_from_mbs_in_codepage(&aws, s, length, sc);
2120 sc->flag = saved_flag;
2122 archive_wstring_free(&aws);
2123 if (errno != ENOMEM)
2124 archive_string_append(as, s, length);
2129 r = archive_string_append_from_wcs_in_codepage(
2130 as, aws.s, aws.length, sc);
2131 if (r != 0 && errno != ENOMEM && l == as->length)
2132 archive_string_append(as, s, length);
2133 archive_wstring_free(&aws);
2138 * Test whether MBS ==> WCS is okay.
2141 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2143 const char *p = (const char *)_p;
2145 DWORD mbflag = MB_ERR_INVALID_CHARS;
2147 if (sc->flag & SCONV_FROM_CHARSET)
2148 codepage = sc->to_cp;
2150 codepage = sc->from_cp;
2152 if (codepage == CP_C_LOCALE)
2154 if (codepage != CP_UTF8)
2155 mbflag |= MB_PRECOMPOSED;
2157 if (MultiByteToWideChar(codepage, mbflag, p, (int)n, NULL, 0) == 0)
2158 return (-1); /* Invalid */
2159 return (0); /* Okay */
2165 * Test whether MBS ==> WCS is okay.
2168 invalid_mbs(const void *_p, size_t n, struct archive_string_conv *sc)
2170 const char *p = (const char *)_p;
2174 mbstate_t shift_state;
2176 memset(&shift_state, 0, sizeof(shift_state));
2178 /* Clear the shift state before starting. */
2179 mbtowc(NULL, NULL, 0);
2185 r = mbrtowc(&wc, p, n, &shift_state);
2187 r = mbtowc(&wc, p, n);
2189 if (r == (size_t)-1 || r == (size_t)-2)
2190 return (-1);/* Invalid. */
2196 (void)sc; /* UNUSED */
2197 return (0); /* All Okey. */
2200 #endif /* defined(_WIN32) && !defined(__CYGWIN__) */
2203 * Basically returns -1 because we cannot make a conversion of charset
2204 * without iconv but in some cases this would return 0.
2205 * Returns 0 if all copied characters are ASCII.
2206 * Returns 0 if both from-locale and to-locale are the same and those
2207 * can be WCS with no error.
2210 best_effort_strncat_in_locale(struct archive_string *as, const void *_p,
2211 size_t length, struct archive_string_conv *sc)
2215 int return_value = 0; /* success */
2218 * If both from-locale and to-locale is the same, this makes a copy.
2219 * And then this checks all copied MBS can be WCS if so returns 0.
2222 if (archive_string_append(as, _p, length) == NULL)
2223 return (-1);/* No memory */
2224 return (invalid_mbs(_p, length, sc));
2228 * If a character is ASCII, this just copies it. If not, this
2229 * assigns '?' character instead but in UTF-8 locale this assigns
2230 * byte sequence 0xEF 0xBD 0xBD, which are code point U+FFFD,
2231 * a Replacement Character in Unicode.
2235 itp = (const uint8_t *)_p;
2236 while (*itp && remaining > 0) {
2238 // Non-ASCII: Substitute with suitable replacement
2239 if (sc->flag & SCONV_TO_UTF8) {
2240 if (archive_string_append(as, utf8_replacement_char, sizeof(utf8_replacement_char)) == NULL) {
2241 __archive_errx(1, "Out of memory");
2244 archive_strappend_char(as, '?');
2248 archive_strappend_char(as, *itp);
2252 return (return_value);
2257 * Unicode conversion functions.
2258 * - UTF-8 <===> UTF-8 in removing surrogate pairs.
2259 * - UTF-8 NFD ===> UTF-8 NFC in removing surrogate pairs.
2260 * - UTF-8 made by libarchive 2.x ===> UTF-8.
2261 * - UTF-16BE <===> UTF-8.
2266 * Utility to convert a single UTF-8 sequence.
2268 * Usually return used bytes, return used byte in negative value when
2269 * a unicode character is replaced with U+FFFD.
2270 * See also http://unicode.org/review/pr-121.html Public Review Issue #121
2271 * Recommended Practice for Replacement Characters.
2274 _utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2276 static const char utf8_count[256] = {
2277 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 00 - 0F */
2278 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 10 - 1F */
2279 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 20 - 2F */
2280 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 30 - 3F */
2281 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 40 - 4F */
2282 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 50 - 5F */
2283 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 60 - 6F */
2284 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,/* 70 - 7F */
2285 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 80 - 8F */
2286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* 90 - 9F */
2287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* A0 - AF */
2288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,/* B0 - BF */
2289 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* C0 - CF */
2290 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,/* D0 - DF */
2291 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,/* E0 - EF */
2292 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* F0 - FF */
2302 * Decode 1-4 bytes depending on the value of the first byte.
2304 ch = (unsigned char)*s;
2306 return (0); /* Standard: return 0 for end-of-string. */
2307 cnt = utf8_count[ch];
2309 /* Invalid sequence or there are not plenty bytes. */
2312 for (i = 1; i < cnt; i++) {
2313 if ((s[i] & 0xc0) != 0x80) {
2318 goto invalid_sequence;
2321 /* Make a Unicode code point from a single UTF-8 sequence. */
2323 case 1: /* 1 byte sequence. */
2326 case 2: /* 2 bytes sequence. */
2327 if ((s[1] & 0xc0) != 0x80) {
2329 goto invalid_sequence;
2331 *pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
2333 case 3: /* 3 bytes sequence. */
2334 if ((s[1] & 0xc0) != 0x80) {
2336 goto invalid_sequence;
2338 if ((s[2] & 0xc0) != 0x80) {
2340 goto invalid_sequence;
2342 wc = ((ch & 0x0f) << 12)
2343 | ((s[1] & 0x3f) << 6)
2346 goto invalid_sequence;/* Overlong sequence. */
2348 case 4: /* 4 bytes sequence. */
2349 if ((s[1] & 0xc0) != 0x80) {
2351 goto invalid_sequence;
2353 if ((s[2] & 0xc0) != 0x80) {
2355 goto invalid_sequence;
2357 if ((s[3] & 0xc0) != 0x80) {
2359 goto invalid_sequence;
2361 wc = ((ch & 0x07) << 18)
2362 | ((s[1] & 0x3f) << 12)
2363 | ((s[2] & 0x3f) << 6)
2366 goto invalid_sequence;/* Overlong sequence. */
2368 default: /* Others are all invalid sequence. */
2369 if (ch == 0xc0 || ch == 0xc1)
2371 else if (ch >= 0xf5 && ch <= 0xf7)
2373 else if (ch >= 0xf8 && ch <= 0xfb)
2375 else if (ch == 0xfc || ch == 0xfd)
2381 for (i = 1; i < cnt; i++) {
2382 if ((s[i] & 0xc0) != 0x80) {
2387 goto invalid_sequence;
2390 /* The code point larger than 0x10FFFF is not legal
2391 * Unicode values. */
2392 if (wc > UNICODE_MAX)
2393 goto invalid_sequence;
2394 /* Correctly gets a Unicode, returns used bytes. */
2398 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2403 utf8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2407 cnt = _utf8_to_unicode(pwc, s, n);
2408 /* Any of Surrogate pair is not legal Unicode values. */
2409 if (cnt == 3 && IS_SURROGATE_PAIR_LA(*pwc))
2414 static inline uint32_t
2415 combine_surrogate_pair(uint32_t uc, uint32_t uc2)
2425 * Convert a single UTF-8/CESU-8 sequence to a Unicode code point in
2426 * removing surrogate pairs.
2428 * CESU-8: The Compatibility Encoding Scheme for UTF-16.
2430 * Usually return used bytes, return used byte in negative value when
2431 * a unicode character is replaced with U+FFFD.
2434 cesu8_to_unicode(uint32_t *pwc, const char *s, size_t n)
2439 cnt = _utf8_to_unicode(&wc, s, n);
2440 if (cnt == 3 && IS_HIGH_SURROGATE_LA(wc)) {
2443 /* Invalid byte sequence. */
2444 goto invalid_sequence;
2446 cnt = _utf8_to_unicode(&wc2, s+3, n-3);
2447 if (cnt != 3 || !IS_LOW_SURROGATE_LA(wc2)) {
2448 /* Invalid byte sequence. */
2449 goto invalid_sequence;
2451 wc = combine_surrogate_pair(wc, wc2);
2453 } else if (cnt == 3 && IS_LOW_SURROGATE_LA(wc)) {
2454 /* Invalid byte sequence. */
2455 goto invalid_sequence;
2460 *pwc = UNICODE_R_CHAR;/* set the Replacement Character instead. */
2467 * Convert a Unicode code point to a single UTF-8 sequence.
2469 * NOTE:This function does not check if the Unicode is legal or not.
2470 * Please you definitely check it before calling this.
2473 unicode_to_utf8(char *p, size_t remaining, uint32_t uc)
2477 /* Invalid Unicode char maps to Replacement character */
2478 if (uc > UNICODE_MAX)
2479 uc = UNICODE_R_CHAR;
2480 /* Translate code point to UTF8 */
2485 } else if (uc <= 0x7ff) {
2488 *p++ = 0xc0 | ((uc >> 6) & 0x1f);
2489 *p++ = 0x80 | (uc & 0x3f);
2490 } else if (uc <= 0xffff) {
2493 *p++ = 0xe0 | ((uc >> 12) & 0x0f);
2494 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2495 *p++ = 0x80 | (uc & 0x3f);
2499 *p++ = 0xf0 | ((uc >> 18) & 0x07);
2500 *p++ = 0x80 | ((uc >> 12) & 0x3f);
2501 *p++ = 0x80 | ((uc >> 6) & 0x3f);
2502 *p++ = 0x80 | (uc & 0x3f);
2508 utf16be_to_unicode(uint32_t *pwc, const char *s, size_t n)
2510 return (utf16_to_unicode(pwc, s, n, 1));
2514 utf16le_to_unicode(uint32_t *pwc, const char *s, size_t n)
2516 return (utf16_to_unicode(pwc, s, n, 0));
2520 utf16_to_unicode(uint32_t *pwc, const char *s, size_t n, int be)
2522 const char *utf16 = s;
2528 /* set the Replacement Character instead. */
2529 *pwc = UNICODE_R_CHAR;
2534 uc = archive_be16dec(utf16);
2536 uc = archive_le16dec(utf16);
2539 /* If this is a surrogate pair, assemble the full code point.*/
2540 if (IS_HIGH_SURROGATE_LA(uc)) {
2545 uc2 = archive_be16dec(utf16);
2547 uc2 = archive_le16dec(utf16);
2550 if (IS_LOW_SURROGATE_LA(uc2)) {
2551 uc = combine_surrogate_pair(uc, uc2);
2554 /* Undescribed code point should be U+FFFD
2555 * (replacement character). */
2556 *pwc = UNICODE_R_CHAR;
2562 * Surrogate pair values(0xd800 through 0xdfff) are only
2563 * used by UTF-16, so, after above calculation, the code
2564 * must not be surrogate values, and Unicode has no codes
2565 * larger than 0x10ffff. Thus, those are not legal Unicode
2568 if (IS_SURROGATE_PAIR_LA(uc) || uc > UNICODE_MAX) {
2569 /* Undescribed code point should be U+FFFD
2570 * (replacement character). */
2571 *pwc = UNICODE_R_CHAR;
2572 return (((int)(utf16 - s)) * -1);
2575 return ((int)(utf16 - s));
2579 unicode_to_utf16be(char *p, size_t remaining, uint32_t uc)
2584 /* We have a code point that won't fit into a
2585 * wchar_t; convert it to a surrogate pair. */
2589 archive_be16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2590 archive_be16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2595 archive_be16enc(utf16, uc);
2601 unicode_to_utf16le(char *p, size_t remaining, uint32_t uc)
2606 /* We have a code point that won't fit into a
2607 * wchar_t; convert it to a surrogate pair. */
2611 archive_le16enc(utf16, ((uc >> 10) & 0x3ff) + 0xD800);
2612 archive_le16enc(utf16+2, (uc & 0x3ff) + 0xDC00);
2617 archive_le16enc(utf16, uc);
2623 * Copy UTF-8 string in checking surrogate pair.
2624 * If any surrogate pair are found, it would be canonicalized.
2627 strncat_from_utf8_to_utf8(struct archive_string *as, const void *_p,
2628 size_t len, struct archive_string_conv *sc)
2634 (void)sc; /* UNUSED */
2636 if (archive_string_ensure(as, as->length + len + 1) == NULL)
2639 s = (const char *)_p;
2640 p = as->s + as->length;
2641 endp = as->s + as->buffer_length -1;
2648 * Forward byte sequence until a conversion of that is needed.
2650 while ((n = utf8_to_unicode(&uc, s, len)) > 0) {
2655 if (p + (s - ss) > endp) {
2656 as->length = p - as->s;
2657 if (archive_string_ensure(as,
2658 as->buffer_length + len + 1) == NULL)
2660 p = as->s + as->length;
2661 endp = as->s + as->buffer_length -1;
2664 memcpy(p, ss, s - ss);
2669 * If n is negative, current byte sequence needs a replacement.
2672 if (n == -3 && IS_SURROGATE_PAIR_LA(uc)) {
2673 /* Current byte sequence may be CESU-8. */
2674 n = cesu8_to_unicode(&uc, s, len);
2678 n *= -1;/* Use a replaced unicode character. */
2681 /* Rebuild UTF-8 byte sequence. */
2682 while ((w = unicode_to_utf8(p, endp - p, uc)) == 0) {
2683 as->length = p - as->s;
2684 if (archive_string_ensure(as,
2685 as->buffer_length + len + 1) == NULL)
2687 p = as->s + as->length;
2688 endp = as->s + as->buffer_length -1;
2695 as->length = p - as->s;
2696 as->s[as->length] = '\0';
2701 archive_string_append_unicode(struct archive_string *as, const void *_p,
2702 size_t len, struct archive_string_conv *sc)
2708 int n, ret = 0, ts, tm;
2709 int (*parse)(uint32_t *, const char *, size_t);
2710 size_t (*unparse)(char *, size_t, uint32_t);
2712 if (sc->flag & SCONV_TO_UTF16BE) {
2713 unparse = unicode_to_utf16be;
2715 } else if (sc->flag & SCONV_TO_UTF16LE) {
2716 unparse = unicode_to_utf16le;
2718 } else if (sc->flag & SCONV_TO_UTF8) {
2719 unparse = unicode_to_utf8;
2723 * This case is going to be converted to another
2724 * character-set through iconv.
2726 if (sc->flag & SCONV_FROM_UTF16BE) {
2727 unparse = unicode_to_utf16be;
2729 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2730 unparse = unicode_to_utf16le;
2733 unparse = unicode_to_utf8;
2738 if (sc->flag & SCONV_FROM_UTF16BE) {
2739 parse = utf16be_to_unicode;
2741 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2742 parse = utf16le_to_unicode;
2745 parse = cesu8_to_unicode;
2749 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2752 s = (const char *)_p;
2753 p = as->s + as->length;
2754 endp = as->s + as->buffer_length - ts;
2755 while ((n = parse(&uc, s, len)) != 0) {
2757 /* Use a replaced unicode character. */
2763 while ((w = unparse(p, endp - p, uc)) == 0) {
2764 /* There is not enough output buffer so
2765 * we have to expand it. */
2766 as->length = p - as->s;
2767 if (archive_string_ensure(as,
2768 as->buffer_length + len * tm + ts) == NULL)
2770 p = as->s + as->length;
2771 endp = as->s + as->buffer_length - ts;
2775 as->length = p - as->s;
2776 as->s[as->length] = '\0';
2778 as->s[as->length+1] = '\0';
2783 * Following Constants for Hangul compositions this information comes from
2784 * Unicode Standard Annex #15 http://unicode.org/reports/tr15/
2786 #define HC_SBASE 0xAC00
2787 #define HC_LBASE 0x1100
2788 #define HC_VBASE 0x1161
2789 #define HC_TBASE 0x11A7
2790 #define HC_LCOUNT 19
2791 #define HC_VCOUNT 21
2792 #define HC_TCOUNT 28
2793 #define HC_NCOUNT (HC_VCOUNT * HC_TCOUNT)
2794 #define HC_SCOUNT (HC_LCOUNT * HC_NCOUNT)
2797 get_nfc(uint32_t uc, uint32_t uc2)
2802 b = sizeof(u_composition_table)/sizeof(u_composition_table[0]) -1;
2804 int m = (t + b) / 2;
2805 if (u_composition_table[m].cp1 < uc)
2807 else if (u_composition_table[m].cp1 > uc)
2809 else if (u_composition_table[m].cp2 < uc2)
2811 else if (u_composition_table[m].cp2 > uc2)
2814 return (u_composition_table[m].nfc);
2819 #define FDC_MAX 10 /* The maximum number of Following Decomposable
2823 * Update first code point.
2825 #define UPDATE_UC(new_uc) do { \
2831 * Replace first code point with second code point.
2833 #define REPLACE_UC_WITH_UC2() do { \
2839 #define EXPAND_BUFFER() do { \
2840 as->length = p - as->s; \
2841 if (archive_string_ensure(as, \
2842 as->buffer_length + len * tm + ts) == NULL)\
2844 p = as->s + as->length; \
2845 endp = as->s + as->buffer_length - ts; \
2848 #define UNPARSE(p, endp, uc) do { \
2849 while ((w = unparse(p, (endp) - (p), uc)) == 0) {\
2856 * Write first code point.
2857 * If the code point has not be changed from its original code,
2858 * this just copies it from its original buffer pointer.
2859 * If not, this converts it to UTF-8 byte sequence and copies it.
2861 #define WRITE_UC() do { \
2868 /* FALL THROUGH */ \
2871 /* FALL THROUGH */ \
2874 /* FALL THROUGH */ \
2881 UNPARSE(p, endp, uc); \
2886 * Collect following decomposable code points.
2888 #define COLLECT_CPS(start) do { \
2890 for (_i = start; _i < FDC_MAX ; _i++) { \
2891 nx = parse(&ucx[_i], s, len); \
2894 cx = CCC(ucx[_i]); \
2895 if (cl >= cx && cl != 228 && cx != 228)\
2902 if (_i >= FDC_MAX) { \
2904 ucx_size = FDC_MAX; \
2910 * Normalize UTF-8/UTF-16BE characters to Form C and copy the result.
2912 * TODO: Convert composition exclusions, which are never converted
2913 * from NFC,NFD,NFKC and NFKD, to Form C.
2916 archive_string_normalize_C(struct archive_string *as, const void *_p,
2917 size_t len, struct archive_string_conv *sc)
2919 const char *s = (const char *)_p;
2923 int always_replace, n, n2, ret = 0, spair, ts, tm;
2924 int (*parse)(uint32_t *, const char *, size_t);
2925 size_t (*unparse)(char *, size_t, uint32_t);
2928 ts = 1;/* text size. */
2929 if (sc->flag & SCONV_TO_UTF16BE) {
2930 unparse = unicode_to_utf16be;
2932 if (sc->flag & SCONV_FROM_UTF16BE)
2934 } else if (sc->flag & SCONV_TO_UTF16LE) {
2935 unparse = unicode_to_utf16le;
2937 if (sc->flag & SCONV_FROM_UTF16LE)
2939 } else if (sc->flag & SCONV_TO_UTF8) {
2940 unparse = unicode_to_utf8;
2941 if (sc->flag & SCONV_FROM_UTF8)
2945 * This case is going to be converted to another
2946 * character-set through iconv.
2949 if (sc->flag & SCONV_FROM_UTF16BE) {
2950 unparse = unicode_to_utf16be;
2952 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2953 unparse = unicode_to_utf16le;
2956 unparse = unicode_to_utf8;
2960 if (sc->flag & SCONV_FROM_UTF16BE) {
2961 parse = utf16be_to_unicode;
2963 spair = 4;/* surrogate pair size in UTF-16. */
2964 } else if (sc->flag & SCONV_FROM_UTF16LE) {
2965 parse = utf16le_to_unicode;
2967 spair = 4;/* surrogate pair size in UTF-16. */
2969 parse = cesu8_to_unicode;
2971 spair = 6;/* surrogate pair size in UTF-8. */
2974 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
2977 p = as->s + as->length;
2978 endp = as->s + as->buffer_length - ts;
2979 while ((n = parse(&uc, s, len)) != 0) {
2980 const char *ucptr, *uc2ptr;
2983 /* Use a replaced unicode character. */
2984 UNPARSE(p, endp, uc);
2989 } else if (n == spair || always_replace)
2990 /* uc is converted from a surrogate pair.
2991 * this should be treated as a changed code. */
2998 /* Read second code point. */
2999 while ((n2 = parse(&uc2, s, len)) > 0) {
3000 uint32_t ucx[FDC_MAX];
3002 int cl, cx, i, nx, ucx_size;
3006 if (n2 == spair || always_replace)
3007 /* uc2 is converted from a surrogate pair.
3008 * this should be treated as a changed code. */
3016 * If current second code point is out of decomposable
3017 * code points, finding compositions is unneeded.
3019 if (!IS_DECOMPOSABLE_BLOCK(uc2)) {
3021 REPLACE_UC_WITH_UC2();
3026 * Try to combine current code points.
3029 * We have to combine Hangul characters according to
3030 * http://uniicode.org/reports/tr15/#Hangul
3032 if (0 <= (LIndex = uc - HC_LBASE) &&
3033 LIndex < HC_LCOUNT) {
3035 * Hangul Composition.
3036 * 1. Two current code points are L and V.
3038 int VIndex = uc2 - HC_VBASE;
3039 if (0 <= VIndex && VIndex < HC_VCOUNT) {
3040 /* Make syllable of form LV. */
3041 UPDATE_UC(HC_SBASE +
3042 (LIndex * HC_VCOUNT + VIndex) *
3046 REPLACE_UC_WITH_UC2();
3049 } else if (0 <= (SIndex = uc - HC_SBASE) &&
3050 SIndex < HC_SCOUNT && (SIndex % HC_TCOUNT) == 0) {
3052 * Hangul Composition.
3053 * 2. Two current code points are LV and T.
3055 int TIndex = uc2 - HC_TBASE;
3056 if (0 < TIndex && TIndex < HC_TCOUNT) {
3057 /* Make syllable of form LVT. */
3058 UPDATE_UC(uc + TIndex);
3061 REPLACE_UC_WITH_UC2();
3064 } else if ((nfc = get_nfc(uc, uc2)) != 0) {
3065 /* A composition to current code points
3069 } else if ((cl = CCC(uc2)) == 0) {
3070 /* Clearly 'uc2' the second code point is not
3071 * a decomposable code. */
3073 REPLACE_UC_WITH_UC2();
3078 * Collect following decomposable code points.
3086 * Find a composed code in the collected code points.
3089 while (i < ucx_size) {
3092 if ((nfc = get_nfc(uc, ucx[i])) == 0) {
3098 * nfc is composed of uc and ucx[i].
3103 * Remove ucx[i] by shifting
3104 * following code points.
3106 for (j = i; j+1 < ucx_size; j++) {
3113 * Collect following code points blocked
3114 * by ucx[i] the removed code point.
3116 if (ucx_size > 0 && i == ucx_size &&
3117 nx > 0 && cx == cl) {
3118 cl = ccx[ucx_size-1];
3119 COLLECT_CPS(ucx_size);
3122 * Restart finding a composed code with
3123 * the updated uc from the top of the
3124 * collected code points.
3130 * Apparently the current code points are not
3131 * decomposed characters or already composed.
3134 for (i = 0; i < ucx_size; i++)
3135 UNPARSE(p, endp, ucx[i]);
3138 * Flush out remaining canonical combining characters.
3140 if (nx > 0 && cx == cl && len > 0) {
3141 while ((nx = parse(&ucx[0], s, len))
3149 UNPARSE(p, endp, ucx[0]);
3156 /* Use a replaced unicode character. */
3157 UNPARSE(p, endp, uc2);
3162 } else if (n2 == 0) {
3167 as->length = p - as->s;
3168 as->s[as->length] = '\0';
3170 as->s[as->length+1] = '\0';
3175 get_nfd(uint32_t *cp1, uint32_t *cp2, uint32_t uc)
3180 * These are not converted to NFD on Mac OS.
3182 if ((uc >= 0x2000 && uc <= 0x2FFF) ||
3183 (uc >= 0xF900 && uc <= 0xFAFF) ||
3184 (uc >= 0x2F800 && uc <= 0x2FAFF))
3187 * Those code points are not converted to NFD on Mac OS.
3188 * I do not know the reason because it is undocumented.
3190 * 1109A ==> 11099 110BA
3191 * 1109C ==> 1109B 110BA
3192 * 110AB ==> 110A5 110BA
3194 if (uc == 0x1109A || uc == 0x1109C || uc == 0x110AB)
3198 b = sizeof(u_decomposition_table)/sizeof(u_decomposition_table[0]) -1;
3200 int m = (t + b) / 2;
3201 if (u_decomposition_table[m].nfc < uc)
3203 else if (u_decomposition_table[m].nfc > uc)
3206 *cp1 = u_decomposition_table[m].cp1;
3207 *cp2 = u_decomposition_table[m].cp2;
3214 #define REPLACE_UC_WITH(cp) do { \
3220 * Normalize UTF-8 characters to Form D and copy the result.
3223 archive_string_normalize_D(struct archive_string *as, const void *_p,
3224 size_t len, struct archive_string_conv *sc)
3226 const char *s = (const char *)_p;
3230 int always_replace, n, n2, ret = 0, spair, ts, tm;
3231 int (*parse)(uint32_t *, const char *, size_t);
3232 size_t (*unparse)(char *, size_t, uint32_t);
3235 ts = 1;/* text size. */
3236 if (sc->flag & SCONV_TO_UTF16BE) {
3237 unparse = unicode_to_utf16be;
3239 if (sc->flag & SCONV_FROM_UTF16BE)
3241 } else if (sc->flag & SCONV_TO_UTF16LE) {
3242 unparse = unicode_to_utf16le;
3244 if (sc->flag & SCONV_FROM_UTF16LE)
3246 } else if (sc->flag & SCONV_TO_UTF8) {
3247 unparse = unicode_to_utf8;
3248 if (sc->flag & SCONV_FROM_UTF8)
3252 * This case is going to be converted to another
3253 * character-set through iconv.
3256 if (sc->flag & SCONV_FROM_UTF16BE) {
3257 unparse = unicode_to_utf16be;
3259 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3260 unparse = unicode_to_utf16le;
3263 unparse = unicode_to_utf8;
3267 if (sc->flag & SCONV_FROM_UTF16BE) {
3268 parse = utf16be_to_unicode;
3270 spair = 4;/* surrogate pair size in UTF-16. */
3271 } else if (sc->flag & SCONV_FROM_UTF16LE) {
3272 parse = utf16le_to_unicode;
3274 spair = 4;/* surrogate pair size in UTF-16. */
3276 parse = cesu8_to_unicode;
3278 spair = 6;/* surrogate pair size in UTF-8. */
3281 if (archive_string_ensure(as, as->length + len * tm + ts) == NULL)
3284 p = as->s + as->length;
3285 endp = as->s + as->buffer_length - ts;
3286 while ((n = parse(&uc, s, len)) != 0) {
3299 /* Use a replaced unicode character. */
3300 UNPARSE(p, endp, uc);
3305 } else if (n == spair || always_replace)
3306 /* uc is converted from a surrogate pair.
3307 * this should be treated as a changed code. */
3314 /* Hangul Decomposition. */
3315 if ((SIndex = uc - HC_SBASE) >= 0 && SIndex < HC_SCOUNT) {
3316 int L = HC_LBASE + SIndex / HC_NCOUNT;
3317 int V = HC_VBASE + (SIndex % HC_NCOUNT) / HC_TCOUNT;
3318 int T = HC_TBASE + SIndex % HC_TCOUNT;
3324 if (T != HC_TBASE) {
3330 if (IS_DECOMPOSABLE_BLOCK(uc) && CCC(uc) != 0) {
3336 while (get_nfd(&cp1, &cp2, uc) && fdi < FDC_MAX) {
3339 for (k = fdi; k > 0; k--)
3341 fdc[0].ccc = CCC(cp2);
3344 REPLACE_UC_WITH(cp1);
3347 /* Read following code points. */
3348 while ((n2 = parse(&uc2, s, len)) > 0 &&
3349 (ccc = CCC(uc2)) != 0 && fdi < FDC_MAX) {
3354 for (j = 0; j < fdi; j++) {
3355 if (fdc[j].ccc > ccc)
3359 for (k = fdi; k > j; k--)
3371 for (fdj = 0; fdj < fdi; fdj++) {
3372 REPLACE_UC_WITH(fdc[fdj].uc);
3378 REPLACE_UC_WITH(uc2);
3380 goto check_first_code;
3382 as->length = p - as->s;
3383 as->s[as->length] = '\0';
3385 as->s[as->length+1] = '\0';
3390 * libarchive 2.x made incorrect UTF-8 strings in the wrong assumption
3391 * that WCS is Unicode. It is true for several platforms but some are false.
3392 * And then people who did not use UTF-8 locale on the non Unicode WCS
3393 * platform and made a tar file with libarchive(mostly bsdtar) 2.x. Those
3394 * now cannot get right filename from libarchive 3.x and later since we
3395 * fixed the wrong assumption and it is incompatible to older its versions.
3396 * So we provide special option, "compat-2x.x", for resolving it.
3397 * That option enable the string conversion of libarchive 2.x.
3399 * Translates the wrong UTF-8 string made by libarchive 2.x into current
3400 * locale character set and appends to the archive_string.
3401 * Note: returns -1 if conversion fails.
3404 strncat_from_utf8_libarchive2(struct archive_string *as,
3405 const void *_p, size_t len, struct archive_string_conv *sc)
3413 mbstate_t shift_state;
3415 memset(&shift_state, 0, sizeof(shift_state));
3417 /* Clear the shift state before starting. */
3418 wctomb(NULL, L'\0');
3420 (void)sc; /* UNUSED */
3422 * Allocate buffer for MBS.
3423 * We need this allocation here since it is possible that
3424 * as->s is still NULL.
3426 if (archive_string_ensure(as, as->length + len + 1) == NULL)
3429 s = (const char *)_p;
3430 p = as->s + as->length;
3431 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3432 while ((n = _utf8_to_unicode(&unicode, s, len)) != 0) {
3436 as->length = p - as->s;
3437 /* Re-allocate buffer for MBS. */
3438 if (archive_string_ensure(as,
3439 as->length + len * 2 + 1) == NULL)
3441 p = as->s + as->length;
3442 end = as->s + as->buffer_length - MB_CUR_MAX -1;
3446 * As libarchive 2.x, translates the UTF-8 characters into
3447 * wide-characters in the assumption that WCS is Unicode.
3453 wc = (wchar_t)unicode;
3458 * Translates the wide-character into the current locale MBS.
3461 n = (int)wcrtomb(p, wc, &shift_state);
3463 n = (int)wctomb(p, wc);
3469 as->length = p - as->s;
3470 as->s[as->length] = '\0';
3476 * Conversion functions between current locale dependent MBS and UTF-16BE.
3477 * strncat_from_utf16be() : UTF-16BE --> MBS
3478 * strncat_to_utf16be() : MBS --> UTF16BE
3481 #if defined(_WIN32) && !defined(__CYGWIN__)
3484 * Convert a UTF-16BE/LE string to current locale and copy the result.
3485 * Return -1 if conversion fails.
3488 win_strncat_from_utf16(struct archive_string *as, const void *_p, size_t bytes,
3489 struct archive_string_conv *sc, int be)
3491 struct archive_string tmp;
3500 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3503 mbs = as->s + as->length;
3504 mbs_size = as->buffer_length - as->length -1;
3506 if (sc->to_cp == CP_C_LOCALE) {
3508 * "C" locale special process.
3512 for (b = 0; b < bytes; b += 2) {
3515 val = archive_be16dec(u16+b);
3517 val = archive_le16dec(u16+b);
3522 *mbs++ = (char)(val&0xff);
3526 as->s[as->length] = '\0';
3530 archive_string_init(&tmp);
3532 if (is_big_endian()) {
3535 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3537 memcpy(tmp.s, _p, bytes);
3538 for (b = 0; b < bytes; b += 2) {
3539 uint16_t val = archive_be16dec(tmp.s+b);
3540 archive_le16enc(tmp.s+b, val);
3545 if (!is_big_endian()) {
3548 if (archive_string_ensure(&tmp, bytes+2) == NULL)
3550 memcpy(tmp.s, _p, bytes);
3551 for (b = 0; b < bytes; b += 2) {
3552 uint16_t val = archive_le16dec(tmp.s+b);
3553 archive_be16enc(tmp.s+b, val);
3561 ll = WideCharToMultiByte(sc->to_cp, 0,
3562 (LPCWSTR)u16, (int)bytes>>1, mbs, (int)mbs_size,
3564 /* Exit loop if we succeeded */
3566 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3569 /* Else expand buffer and loop to try again. */
3570 ll = WideCharToMultiByte(sc->to_cp, 0,
3571 (LPCWSTR)u16, (int)bytes, NULL, 0, NULL, NULL);
3572 if (archive_string_ensure(as, ll +1) == NULL)
3574 mbs = as->s + as->length;
3575 mbs_size = as->buffer_length - as->length -1;
3577 archive_string_free(&tmp);
3579 as->s[as->length] = '\0';
3580 if (ll == 0 || defchar)
3586 win_strncat_from_utf16be(struct archive_string *as, const void *_p,
3587 size_t bytes, struct archive_string_conv *sc)
3589 return (win_strncat_from_utf16(as, _p, bytes, sc, 1));
3593 win_strncat_from_utf16le(struct archive_string *as, const void *_p,
3594 size_t bytes, struct archive_string_conv *sc)
3596 return (win_strncat_from_utf16(as, _p, bytes, sc, 0));
3604 return (archive_be16dec(&d) == 1);
3608 * Convert a current locale string to UTF-16BE/LE and copy the result.
3609 * Return -1 if conversion fails.
3612 win_strncat_to_utf16(struct archive_string *as16, const void *_p,
3613 size_t length, struct archive_string_conv *sc, int bigendian)
3615 const char *s = (const char *)_p;
3617 size_t count, avail;
3619 if (archive_string_ensure(as16,
3620 as16->length + (length + 1) * 2) == NULL)
3623 u16 = as16->s + as16->length;
3624 avail = as16->buffer_length - 2;
3625 if (sc->from_cp == CP_C_LOCALE) {
3627 * "C" locale special process.
3630 while (count < length && *s) {
3632 archive_be16enc(u16, *s);
3634 archive_le16enc(u16, *s);
3639 as16->length += count << 1;
3640 as16->s[as16->length] = 0;
3641 as16->s[as16->length+1] = 0;
3645 count = MultiByteToWideChar(sc->from_cp,
3646 MB_PRECOMPOSED, s, (int)length, (LPWSTR)u16, (int)avail>>1);
3647 /* Exit loop if we succeeded */
3649 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
3652 /* Expand buffer and try again */
3653 count = MultiByteToWideChar(sc->from_cp,
3654 MB_PRECOMPOSED, s, (int)length, NULL, 0);
3655 if (archive_string_ensure(as16, (count +1) * 2)
3658 u16 = as16->s + as16->length;
3659 avail = as16->buffer_length - 2;
3661 as16->length += count * 2;
3662 as16->s[as16->length] = 0;
3663 as16->s[as16->length+1] = 0;
3667 if (is_big_endian()) {
3670 uint16_t v = archive_be16dec(u16);
3671 archive_le16enc(u16, v);
3679 uint16_t v = archive_le16dec(u16);
3680 archive_be16enc(u16, v);
3690 win_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3691 size_t length, struct archive_string_conv *sc)
3693 return (win_strncat_to_utf16(as16, _p, length, sc, 1));
3697 win_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3698 size_t length, struct archive_string_conv *sc)
3700 return (win_strncat_to_utf16(as16, _p, length, sc, 0));
3703 #endif /* _WIN32 && !__CYGWIN__ */
3706 * Do the best effort for conversions.
3707 * We cannot handle UTF-16BE character-set without such iconv,
3708 * but there is a chance if a string consists just ASCII code or
3709 * a current locale is UTF-8.
3713 * Convert a UTF-16BE string to current locale and copy the result.
3714 * Return -1 if conversion fails.
3717 best_effort_strncat_from_utf16(struct archive_string *as, const void *_p,
3718 size_t bytes, struct archive_string_conv *sc, int be)
3720 const char *utf16 = (const char *)_p;
3725 (void)sc; /* UNUSED */
3727 * Other case, we should do the best effort.
3728 * If all character are ASCII(<0x7f), we can convert it.
3729 * if not , we set a alternative character and return -1.
3732 if (archive_string_ensure(as, as->length + bytes +1) == NULL)
3734 mbs = as->s + as->length;
3736 while ((n = utf16_to_unicode(&uc, utf16, bytes, be)) != 0) {
3745 /* We cannot handle it. */
3751 as->length = mbs - as->s;
3752 as->s[as->length] = '\0';
3757 best_effort_strncat_from_utf16be(struct archive_string *as, const void *_p,
3758 size_t bytes, struct archive_string_conv *sc)
3760 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 1));
3764 best_effort_strncat_from_utf16le(struct archive_string *as, const void *_p,
3765 size_t bytes, struct archive_string_conv *sc)
3767 return (best_effort_strncat_from_utf16(as, _p, bytes, sc, 0));
3771 * Convert a current locale string to UTF-16BE/LE and copy the result.
3772 * Return -1 if conversion fails.
3775 best_effort_strncat_to_utf16(struct archive_string *as16, const void *_p,
3776 size_t length, struct archive_string_conv *sc, int bigendian)
3778 const char *s = (const char *)_p;
3783 (void)sc; /* UNUSED */
3785 * Other case, we should do the best effort.
3786 * If all character are ASCII(<0x7f), we can convert it.
3787 * if not , we set a alternative character and return -1.
3792 if (archive_string_ensure(as16,
3793 as16->length + (length + 1) * 2) == NULL)
3796 utf16 = as16->s + as16->length;
3797 while (remaining--) {
3800 /* We cannot handle it. */
3805 archive_be16enc(utf16, c);
3807 archive_le16enc(utf16, c);
3810 as16->length = utf16 - as16->s;
3811 as16->s[as16->length] = 0;
3812 as16->s[as16->length+1] = 0;
3817 best_effort_strncat_to_utf16be(struct archive_string *as16, const void *_p,
3818 size_t length, struct archive_string_conv *sc)
3820 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 1));
3824 best_effort_strncat_to_utf16le(struct archive_string *as16, const void *_p,
3825 size_t length, struct archive_string_conv *sc)
3827 return (best_effort_strncat_to_utf16(as16, _p, length, sc, 0));
3832 * Multistring operations.
3836 archive_mstring_clean(struct archive_mstring *aes)
3838 archive_wstring_free(&(aes->aes_wcs));
3839 archive_string_free(&(aes->aes_mbs));
3840 archive_string_free(&(aes->aes_utf8));
3841 archive_string_free(&(aes->aes_mbs_in_locale));
3846 archive_mstring_copy(struct archive_mstring *dest, struct archive_mstring *src)
3848 dest->aes_set = src->aes_set;
3849 archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
3850 archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
3851 archive_wstring_copy(&(dest->aes_wcs), &(src->aes_wcs));
3855 archive_mstring_get_utf8(struct archive *a, struct archive_mstring *aes,
3858 struct archive_string_conv *sc;
3861 /* If we already have a UTF8 form, return that immediately. */
3862 if (aes->aes_set & AES_SET_UTF8) {
3863 *p = aes->aes_utf8.s;
3868 if (aes->aes_set & AES_SET_MBS) {
3869 sc = archive_string_conversion_to_charset(a, "UTF-8", 1);
3871 return (-1);/* Couldn't allocate memory for sc. */
3872 r = archive_strncpy_l(&(aes->aes_utf8), aes->aes_mbs.s,
3873 aes->aes_mbs.length, sc);
3875 free_sconv_object(sc);
3877 aes->aes_set |= AES_SET_UTF8;
3878 *p = aes->aes_utf8.s;
3879 return (0);/* success. */
3881 return (-1);/* failure. */
3883 return (0);/* success. */
3887 archive_mstring_get_mbs(struct archive *a, struct archive_mstring *aes,
3892 (void)a; /* UNUSED */
3893 /* If we already have an MBS form, return that immediately. */
3894 if (aes->aes_set & AES_SET_MBS) {
3895 *p = aes->aes_mbs.s;
3900 /* If there's a WCS form, try converting with the native locale. */
3901 if (aes->aes_set & AES_SET_WCS) {
3902 archive_string_empty(&(aes->aes_mbs));
3903 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3904 aes->aes_wcs.s, aes->aes_wcs.length);
3905 *p = aes->aes_mbs.s;
3907 aes->aes_set |= AES_SET_MBS;
3914 * Only a UTF-8 form cannot avail because its conversion already
3915 * failed at archive_mstring_update_utf8().
3921 archive_mstring_get_wcs(struct archive *a, struct archive_mstring *aes,
3926 (void)a;/* UNUSED */
3927 /* Return WCS form if we already have it. */
3928 if (aes->aes_set & AES_SET_WCS) {
3929 *wp = aes->aes_wcs.s;
3934 /* Try converting MBS to WCS using native locale. */
3935 if (aes->aes_set & AES_SET_MBS) {
3936 archive_wstring_empty(&(aes->aes_wcs));
3937 r = archive_wstring_append_from_mbs(&(aes->aes_wcs),
3938 aes->aes_mbs.s, aes->aes_mbs.length);
3940 aes->aes_set |= AES_SET_WCS;
3941 *wp = aes->aes_wcs.s;
3943 ret = -1;/* failure. */
3949 archive_mstring_get_mbs_l(struct archive_mstring *aes,
3950 const char **p, size_t *length, struct archive_string_conv *sc)
3954 #if defined(_WIN32) && !defined(__CYGWIN__)
3956 * Internationalization programming on Windows must use Wide
3957 * characters because Windows platform cannot make locale UTF-8.
3959 if (sc != NULL && (aes->aes_set & AES_SET_WCS) != 0) {
3960 archive_string_empty(&(aes->aes_mbs_in_locale));
3961 r = archive_string_append_from_wcs_in_codepage(
3962 &(aes->aes_mbs_in_locale), aes->aes_wcs.s,
3963 aes->aes_wcs.length, sc);
3965 *p = aes->aes_mbs_in_locale.s;
3967 *length = aes->aes_mbs_in_locale.length;
3969 } else if (errno == ENOMEM)
3976 /* If there is not an MBS form but is a WCS form, try converting
3977 * with the native locale to be used for translating it to specified
3979 if ((aes->aes_set & AES_SET_MBS) == 0 &&
3980 (aes->aes_set & AES_SET_WCS) != 0) {
3981 archive_string_empty(&(aes->aes_mbs));
3982 r = archive_string_append_from_wcs(&(aes->aes_mbs),
3983 aes->aes_wcs.s, aes->aes_wcs.length);
3985 aes->aes_set |= AES_SET_MBS;
3986 else if (errno == ENOMEM)
3991 /* If we already have an MBS form, use it to be translated to
3992 * specified character-set. */
3993 if (aes->aes_set & AES_SET_MBS) {
3995 /* Conversion is unneeded. */
3996 *p = aes->aes_mbs.s;
3998 *length = aes->aes_mbs.length;
4001 ret = archive_strncpy_l(&(aes->aes_mbs_in_locale),
4002 aes->aes_mbs.s, aes->aes_mbs.length, sc);
4003 *p = aes->aes_mbs_in_locale.s;
4005 *length = aes->aes_mbs_in_locale.length;
4015 archive_mstring_copy_mbs(struct archive_mstring *aes, const char *mbs)
4021 return (archive_mstring_copy_mbs_len(aes, mbs, strlen(mbs)));
4025 archive_mstring_copy_mbs_len(struct archive_mstring *aes, const char *mbs,
4032 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4033 archive_strncpy(&(aes->aes_mbs), mbs, len);
4034 archive_string_empty(&(aes->aes_utf8));
4035 archive_wstring_empty(&(aes->aes_wcs));
4040 archive_mstring_copy_wcs(struct archive_mstring *aes, const wchar_t *wcs)
4042 return archive_mstring_copy_wcs_len(aes, wcs,
4043 wcs == NULL ? 0 : wcslen(wcs));
4047 archive_mstring_copy_utf8(struct archive_mstring *aes, const char *utf8)
4052 aes->aes_set = AES_SET_UTF8;
4053 archive_string_empty(&(aes->aes_mbs));
4054 archive_string_empty(&(aes->aes_wcs));
4055 archive_strncpy(&(aes->aes_utf8), utf8, strlen(utf8));
4056 return (int)strlen(utf8);
4060 archive_mstring_copy_wcs_len(struct archive_mstring *aes, const wchar_t *wcs,
4066 aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
4067 archive_string_empty(&(aes->aes_mbs));
4068 archive_string_empty(&(aes->aes_utf8));
4069 archive_wstrncpy(&(aes->aes_wcs), wcs, len);
4074 archive_mstring_copy_mbs_len_l(struct archive_mstring *aes,
4075 const char *mbs, size_t len, struct archive_string_conv *sc)
4083 archive_string_empty(&(aes->aes_mbs));
4084 archive_wstring_empty(&(aes->aes_wcs));
4085 archive_string_empty(&(aes->aes_utf8));
4086 #if defined(_WIN32) && !defined(__CYGWIN__)
4088 * Internationalization programming on Windows must use Wide
4089 * characters because Windows platform cannot make locale UTF-8.
4092 if (archive_string_append(&(aes->aes_mbs),
4093 mbs, mbsnbytes(mbs, len)) == NULL) {
4097 aes->aes_set = AES_SET_MBS;
4100 #if defined(HAVE_ICONV)
4101 } else if (sc != NULL && sc->cd_w != (iconv_t)-1) {
4103 * This case happens only when MultiByteToWideChar() cannot
4104 * handle sc->from_cp, and we have to iconv in order to
4105 * translate character-set to wchar_t,UTF-16.
4107 iconv_t cd = sc->cd;
4112 * Translate multi-bytes from some character-set to UTF-8.
4115 r = archive_strncpy_l(&(aes->aes_utf8), mbs, len, sc);
4121 aes->aes_set = AES_SET_UTF8;
4124 * Append the UTF-8 string into wstring.
4127 sc->flag &= ~(SCONV_NORMALIZATION_C
4128 | SCONV_TO_UTF16| SCONV_FROM_UTF16);
4129 from_cp = sc->from_cp;
4130 sc->from_cp = CP_UTF8;
4131 r = archive_wstring_append_from_mbs_in_codepage(&(aes->aes_wcs),
4132 aes->aes_utf8.s, aes->aes_utf8.length, sc);
4134 sc->from_cp = from_cp;
4136 aes->aes_set |= AES_SET_WCS;
4139 r = archive_wstring_append_from_mbs_in_codepage(
4140 &(aes->aes_wcs), mbs, len, sc);
4142 aes->aes_set = AES_SET_WCS;
4147 r = archive_strncpy_l(&(aes->aes_mbs), mbs, len, sc);
4149 aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
4157 * The 'update' form tries to proactively update all forms of
4158 * this string (WCS and MBS) and returns an error if any of
4159 * them fail. This is used by the 'pax' handler, for instance,
4160 * to detect and report character-conversion failures early while
4161 * still allowing clients to get potentially useful values from
4162 * the more tolerant lazy conversions. (get_mbs and get_wcs will
4163 * strive to give the user something useful, so you can get hopefully
4164 * usable values even if some of the character conversions are failing.)
4167 archive_mstring_update_utf8(struct archive *a, struct archive_mstring *aes,
4170 struct archive_string_conv *sc;
4175 return (0); /* Succeeded in clearing everything. */
4178 /* Save the UTF8 string. */
4179 archive_strcpy(&(aes->aes_utf8), utf8);
4181 /* Empty the mbs and wcs strings. */
4182 archive_string_empty(&(aes->aes_mbs));
4183 archive_wstring_empty(&(aes->aes_wcs));
4185 aes->aes_set = AES_SET_UTF8; /* Only UTF8 is set now. */
4187 /* Try converting UTF-8 to MBS, return false on failure. */
4188 sc = archive_string_conversion_from_charset(a, "UTF-8", 1);
4190 return (-1);/* Couldn't allocate memory for sc. */
4191 r = archive_strcpy_l(&(aes->aes_mbs), utf8, sc);
4193 free_sconv_object(sc);
4196 aes->aes_set = AES_SET_UTF8 | AES_SET_MBS; /* Both UTF8 and MBS set. */
4198 /* Try converting MBS to WCS, return false on failure. */
4199 if (archive_wstring_append_from_mbs(&(aes->aes_wcs), aes->aes_mbs.s,
4200 aes->aes_mbs.length))
4202 aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
4204 /* All conversions succeeded. */