HardenedBSD src tree
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4224 lines
103KB

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