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