loop_unicode.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright (C) 1999-2003, 2005-2006, 2008 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either version 2.1
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /* This file defines the conversion loop via Unicode as a pivot encoding. */
  20. /* Attempt to transliterate wc. Return code as in xxx_wctomb. */
  21. static int unicode_transliterate (conv_t cd, ucs4_t wc,
  22. unsigned char* outptr, size_t outleft)
  23. {
  24. if (cd->oflags & HAVE_HANGUL_JAMO) {
  25. /* Decompose Hangul into Jamo. Use double-width Jamo (contained
  26. in all Korean encodings and ISO-2022-JP-2), not half-width Jamo
  27. (contained in Unicode only). */
  28. ucs4_t buf[3];
  29. int ret = johab_hangul_decompose(cd,buf,wc);
  30. if (ret != RET_ILUNI) {
  31. /* we know 1 <= ret <= 3 */
  32. state_t backup_state = cd->ostate;
  33. unsigned char* backup_outptr = outptr;
  34. size_t backup_outleft = outleft;
  35. int i, sub_outcount;
  36. for (i = 0; i < ret; i++) {
  37. if (outleft == 0) {
  38. sub_outcount = RET_TOOSMALL;
  39. goto johab_hangul_failed;
  40. }
  41. sub_outcount = cd->ofuncs.xxx_wctomb(cd,outptr,buf[i],outleft);
  42. if (sub_outcount <= RET_ILUNI)
  43. goto johab_hangul_failed;
  44. if (!(sub_outcount <= outleft)) abort();
  45. outptr += sub_outcount; outleft -= sub_outcount;
  46. }
  47. return outptr-backup_outptr;
  48. johab_hangul_failed:
  49. cd->ostate = backup_state;
  50. outptr = backup_outptr;
  51. outleft = backup_outleft;
  52. if (sub_outcount != RET_ILUNI)
  53. return RET_TOOSMALL;
  54. }
  55. }
  56. {
  57. /* Try to use a variant, but postfix it with
  58. U+303E IDEOGRAPHIC VARIATION INDICATOR
  59. (cf. Ken Lunde's "CJKV information processing", p. 188). */
  60. int indx = -1;
  61. if (wc == 0x3006)
  62. indx = 0;
  63. else if (wc == 0x30f6)
  64. indx = 1;
  65. else if (wc >= 0x4e00 && wc < 0xa000)
  66. indx = cjk_variants_indx[wc-0x4e00];
  67. if (indx >= 0) {
  68. for (;; indx++) {
  69. ucs4_t buf[2];
  70. unsigned short variant = cjk_variants[indx];
  71. unsigned short last = variant & 0x8000;
  72. variant &= 0x7fff;
  73. variant += 0x3000;
  74. buf[0] = variant; buf[1] = 0x303e;
  75. {
  76. state_t backup_state = cd->ostate;
  77. unsigned char* backup_outptr = outptr;
  78. size_t backup_outleft = outleft;
  79. int i, sub_outcount;
  80. for (i = 0; i < 2; i++) {
  81. if (outleft == 0) {
  82. sub_outcount = RET_TOOSMALL;
  83. goto variant_failed;
  84. }
  85. sub_outcount = cd->ofuncs.xxx_wctomb(cd,outptr,buf[i],outleft);
  86. if (sub_outcount <= RET_ILUNI)
  87. goto variant_failed;
  88. if (!(sub_outcount <= outleft)) abort();
  89. outptr += sub_outcount; outleft -= sub_outcount;
  90. }
  91. return outptr-backup_outptr;
  92. variant_failed:
  93. cd->ostate = backup_state;
  94. outptr = backup_outptr;
  95. outleft = backup_outleft;
  96. if (sub_outcount != RET_ILUNI)
  97. return RET_TOOSMALL;
  98. }
  99. if (last)
  100. break;
  101. }
  102. }
  103. }
  104. if (wc >= 0x2018 && wc <= 0x201a) {
  105. /* Special case for quotation marks 0x2018, 0x2019, 0x201a */
  106. ucs4_t substitute =
  107. (cd->oflags & HAVE_QUOTATION_MARKS
  108. ? (wc == 0x201a ? 0x2018 : wc)
  109. : (cd->oflags & HAVE_ACCENTS
  110. ? (wc==0x2019 ? 0x00b4 : 0x0060) /* use accents */
  111. : 0x0027 /* use apostrophe */
  112. ) );
  113. int outcount = cd->ofuncs.xxx_wctomb(cd,outptr,substitute,outleft);
  114. if (outcount != RET_ILUNI)
  115. return outcount;
  116. }
  117. {
  118. /* Use the transliteration table. */
  119. int indx = translit_index(wc);
  120. if (indx >= 0) {
  121. const unsigned int * cp = &translit_data[indx];
  122. unsigned int num = *cp++;
  123. state_t backup_state = cd->ostate;
  124. unsigned char* backup_outptr = outptr;
  125. size_t backup_outleft = outleft;
  126. unsigned int i;
  127. int sub_outcount;
  128. for (i = 0; i < num; i++) {
  129. if (outleft == 0) {
  130. sub_outcount = RET_TOOSMALL;
  131. goto translit_failed;
  132. }
  133. sub_outcount = cd->ofuncs.xxx_wctomb(cd,outptr,cp[i],outleft);
  134. if (sub_outcount == RET_ILUNI)
  135. /* Recursive transliteration. */
  136. sub_outcount = unicode_transliterate(cd,cp[i],outptr,outleft);
  137. if (sub_outcount <= RET_ILUNI)
  138. goto translit_failed;
  139. if (!(sub_outcount <= outleft)) abort();
  140. outptr += sub_outcount; outleft -= sub_outcount;
  141. }
  142. return outptr-backup_outptr;
  143. translit_failed:
  144. cd->ostate = backup_state;
  145. outptr = backup_outptr;
  146. outleft = backup_outleft;
  147. if (sub_outcount != RET_ILUNI)
  148. return RET_TOOSMALL;
  149. }
  150. }
  151. return RET_ILUNI;
  152. }
  153. #ifndef LIBICONV_PLUG
  154. struct uc_to_mb_fallback_locals {
  155. unsigned char* l_outbuf;
  156. size_t l_outbytesleft;
  157. int l_errno;
  158. };
  159. static void uc_to_mb_write_replacement (const char *buf, size_t buflen,
  160. void* callback_arg)
  161. {
  162. struct uc_to_mb_fallback_locals * plocals =
  163. (struct uc_to_mb_fallback_locals *) callback_arg;
  164. /* Do nothing if already encountered an error in a previous call. */
  165. if (plocals->l_errno == 0) {
  166. /* Attempt to copy the passed buffer to the output buffer. */
  167. if (plocals->l_outbytesleft < buflen)
  168. plocals->l_errno = E2BIG;
  169. else {
  170. memcpy(plocals->l_outbuf, buf, buflen);
  171. plocals->l_outbuf += buflen;
  172. plocals->l_outbytesleft -= buflen;
  173. }
  174. }
  175. }
  176. struct mb_to_uc_fallback_locals {
  177. conv_t l_cd;
  178. unsigned char* l_outbuf;
  179. size_t l_outbytesleft;
  180. int l_errno;
  181. };
  182. static void mb_to_uc_write_replacement (const unsigned int *buf, size_t buflen,
  183. void* callback_arg)
  184. {
  185. struct mb_to_uc_fallback_locals * plocals =
  186. (struct mb_to_uc_fallback_locals *) callback_arg;
  187. /* Do nothing if already encountered an error in a previous call. */
  188. if (plocals->l_errno == 0) {
  189. /* Attempt to convert the passed buffer to the target encoding. */
  190. conv_t cd = plocals->l_cd;
  191. unsigned char* outptr = plocals->l_outbuf;
  192. size_t outleft = plocals->l_outbytesleft;
  193. for (; buflen > 0; buf++, buflen--) {
  194. ucs4_t wc = *buf;
  195. int outcount;
  196. if (outleft == 0) {
  197. plocals->l_errno = E2BIG;
  198. break;
  199. }
  200. outcount = cd->ofuncs.xxx_wctomb(cd,outptr,wc,outleft);
  201. if (outcount != RET_ILUNI)
  202. goto outcount_ok;
  203. /* Handle Unicode tag characters (range U+E0000..U+E007F). */
  204. if ((wc >> 7) == (0xe0000 >> 7))
  205. goto outcount_zero;
  206. /* Try transliteration. */
  207. if (cd->transliterate) {
  208. outcount = unicode_transliterate(cd,wc,outptr,outleft);
  209. if (outcount != RET_ILUNI)
  210. goto outcount_ok;
  211. }
  212. if (cd->discard_ilseq) {
  213. outcount = 0;
  214. goto outcount_ok;
  215. }
  216. #ifndef LIBICONV_PLUG
  217. else if (cd->fallbacks.uc_to_mb_fallback != NULL) {
  218. struct uc_to_mb_fallback_locals locals;
  219. locals.l_outbuf = outptr;
  220. locals.l_outbytesleft = outleft;
  221. locals.l_errno = 0;
  222. cd->fallbacks.uc_to_mb_fallback(wc,
  223. uc_to_mb_write_replacement,
  224. &locals,
  225. cd->fallbacks.data);
  226. if (locals.l_errno != 0) {
  227. plocals->l_errno = locals.l_errno;
  228. break;
  229. }
  230. outptr = locals.l_outbuf;
  231. outleft = locals.l_outbytesleft;
  232. outcount = 0;
  233. goto outcount_ok;
  234. }
  235. #endif
  236. outcount = cd->ofuncs.xxx_wctomb(cd,outptr,0xFFFD,outleft);
  237. if (outcount != RET_ILUNI)
  238. goto outcount_ok;
  239. plocals->l_errno = EILSEQ;
  240. break;
  241. outcount_ok:
  242. if (outcount < 0) {
  243. plocals->l_errno = E2BIG;
  244. break;
  245. }
  246. #ifndef LIBICONV_PLUG
  247. if (cd->hooks.uc_hook)
  248. (*cd->hooks.uc_hook)(wc, cd->hooks.data);
  249. #endif
  250. if (!(outcount <= outleft)) abort();
  251. outptr += outcount; outleft -= outcount;
  252. outcount_zero: ;
  253. }
  254. plocals->l_outbuf = outptr;
  255. plocals->l_outbytesleft = outleft;
  256. }
  257. }
  258. #endif /* !LIBICONV_PLUG */
  259. static size_t unicode_loop_convert (iconv_t icd,
  260. const char* * inbuf, size_t *inbytesleft,
  261. char* * outbuf, size_t *outbytesleft)
  262. {
  263. conv_t cd = (conv_t) icd;
  264. size_t result = 0;
  265. const unsigned char* inptr = (const unsigned char*) *inbuf;
  266. size_t inleft = *inbytesleft;
  267. unsigned char* outptr = (unsigned char*) *outbuf;
  268. size_t outleft = *outbytesleft;
  269. while (inleft > 0) {
  270. state_t last_istate = cd->istate;
  271. ucs4_t wc;
  272. int incount;
  273. int outcount;
  274. incount = cd->ifuncs.xxx_mbtowc(cd,&wc,inptr,inleft);
  275. if (incount < 0) {
  276. if ((unsigned int)(-1-incount) % 2 == (unsigned int)(-1-RET_ILSEQ) % 2) {
  277. /* Case 1: invalid input, possibly after a shift sequence */
  278. incount = DECODE_SHIFT_ILSEQ(incount);
  279. if (cd->discard_ilseq) {
  280. switch (cd->iindex) {
  281. case ei_ucs4: case ei_ucs4be: case ei_ucs4le:
  282. case ei_utf32: case ei_utf32be: case ei_utf32le:
  283. case ei_ucs4internal: case ei_ucs4swapped:
  284. incount += 4; break;
  285. case ei_ucs2: case ei_ucs2be: case ei_ucs2le:
  286. case ei_utf16: case ei_utf16be: case ei_utf16le:
  287. case ei_ucs2internal: case ei_ucs2swapped:
  288. incount += 2; break;
  289. default:
  290. incount += 1; break;
  291. }
  292. goto outcount_zero;
  293. }
  294. #ifndef LIBICONV_PLUG
  295. else if (cd->fallbacks.mb_to_uc_fallback != NULL) {
  296. unsigned int incount2;
  297. struct mb_to_uc_fallback_locals locals;
  298. switch (cd->iindex) {
  299. case ei_ucs4: case ei_ucs4be: case ei_ucs4le:
  300. case ei_utf32: case ei_utf32be: case ei_utf32le:
  301. case ei_ucs4internal: case ei_ucs4swapped:
  302. incount2 = 4; break;
  303. case ei_ucs2: case ei_ucs2be: case ei_ucs2le:
  304. case ei_utf16: case ei_utf16be: case ei_utf16le:
  305. case ei_ucs2internal: case ei_ucs2swapped:
  306. incount2 = 2; break;
  307. default:
  308. incount2 = 1; break;
  309. }
  310. locals.l_cd = cd;
  311. locals.l_outbuf = outptr;
  312. locals.l_outbytesleft = outleft;
  313. locals.l_errno = 0;
  314. cd->fallbacks.mb_to_uc_fallback((const char*)inptr+incount, incount2,
  315. mb_to_uc_write_replacement,
  316. &locals,
  317. cd->fallbacks.data);
  318. if (locals.l_errno != 0) {
  319. inptr += incount; inleft -= incount;
  320. errno = locals.l_errno;
  321. result = -1;
  322. break;
  323. }
  324. incount += incount2;
  325. outptr = locals.l_outbuf;
  326. outleft = locals.l_outbytesleft;
  327. result += 1;
  328. goto outcount_zero;
  329. }
  330. #endif
  331. inptr += incount; inleft -= incount;
  332. errno = EILSEQ;
  333. result = -1;
  334. break;
  335. }
  336. if (incount == RET_TOOFEW(0)) {
  337. /* Case 2: not enough bytes available to detect anything */
  338. errno = EINVAL;
  339. result = -1;
  340. break;
  341. }
  342. /* Case 3: k bytes read, but only a shift sequence */
  343. incount = DECODE_TOOFEW(incount);
  344. } else {
  345. /* Case 4: k bytes read, making up a wide character */
  346. if (outleft == 0) {
  347. cd->istate = last_istate;
  348. errno = E2BIG;
  349. result = -1;
  350. break;
  351. }
  352. outcount = cd->ofuncs.xxx_wctomb(cd,outptr,wc,outleft);
  353. if (outcount != RET_ILUNI)
  354. goto outcount_ok;
  355. /* Handle Unicode tag characters (range U+E0000..U+E007F). */
  356. if ((wc >> 7) == (0xe0000 >> 7))
  357. goto outcount_zero;
  358. /* Try transliteration. */
  359. result++;
  360. if (cd->transliterate) {
  361. outcount = unicode_transliterate(cd,wc,outptr,outleft);
  362. if (outcount != RET_ILUNI)
  363. goto outcount_ok;
  364. }
  365. if (cd->discard_ilseq) {
  366. outcount = 0;
  367. goto outcount_ok;
  368. }
  369. #ifndef LIBICONV_PLUG
  370. else if (cd->fallbacks.uc_to_mb_fallback != NULL) {
  371. struct uc_to_mb_fallback_locals locals;
  372. locals.l_outbuf = outptr;
  373. locals.l_outbytesleft = outleft;
  374. locals.l_errno = 0;
  375. cd->fallbacks.uc_to_mb_fallback(wc,
  376. uc_to_mb_write_replacement,
  377. &locals,
  378. cd->fallbacks.data);
  379. if (locals.l_errno != 0) {
  380. cd->istate = last_istate;
  381. errno = locals.l_errno;
  382. return -1;
  383. }
  384. outptr = locals.l_outbuf;
  385. outleft = locals.l_outbytesleft;
  386. outcount = 0;
  387. goto outcount_ok;
  388. }
  389. #endif
  390. outcount = cd->ofuncs.xxx_wctomb(cd,outptr,0xFFFD,outleft);
  391. if (outcount != RET_ILUNI)
  392. goto outcount_ok;
  393. cd->istate = last_istate;
  394. errno = EILSEQ;
  395. result = -1;
  396. break;
  397. outcount_ok:
  398. if (outcount < 0) {
  399. cd->istate = last_istate;
  400. errno = E2BIG;
  401. result = -1;
  402. break;
  403. }
  404. #ifndef LIBICONV_PLUG
  405. if (cd->hooks.uc_hook)
  406. (*cd->hooks.uc_hook)(wc, cd->hooks.data);
  407. #endif
  408. if (!(outcount <= outleft)) abort();
  409. outptr += outcount; outleft -= outcount;
  410. }
  411. outcount_zero:
  412. if (!(incount <= inleft)) abort();
  413. inptr += incount; inleft -= incount;
  414. }
  415. *inbuf = (const char*) inptr;
  416. *inbytesleft = inleft;
  417. *outbuf = (char*) outptr;
  418. *outbytesleft = outleft;
  419. return result;
  420. }
  421. static size_t unicode_loop_reset (iconv_t icd,
  422. char* * outbuf, size_t *outbytesleft)
  423. {
  424. conv_t cd = (conv_t) icd;
  425. if (outbuf == NULL || *outbuf == NULL) {
  426. /* Reset the states. */
  427. memset(&cd->istate,'\0',sizeof(state_t));
  428. memset(&cd->ostate,'\0',sizeof(state_t));
  429. return 0;
  430. } else {
  431. size_t result = 0;
  432. if (cd->ifuncs.xxx_flushwc) {
  433. state_t last_istate = cd->istate;
  434. ucs4_t wc;
  435. if (cd->ifuncs.xxx_flushwc(cd, &wc)) {
  436. unsigned char* outptr = (unsigned char*) *outbuf;
  437. size_t outleft = *outbytesleft;
  438. int outcount = cd->ofuncs.xxx_wctomb(cd,outptr,wc,outleft);
  439. if (outcount != RET_ILUNI)
  440. goto outcount_ok;
  441. /* Handle Unicode tag characters (range U+E0000..U+E007F). */
  442. if ((wc >> 7) == (0xe0000 >> 7))
  443. goto outcount_zero;
  444. /* Try transliteration. */
  445. result++;
  446. if (cd->transliterate) {
  447. outcount = unicode_transliterate(cd,wc,outptr,outleft);
  448. if (outcount != RET_ILUNI)
  449. goto outcount_ok;
  450. }
  451. if (cd->discard_ilseq) {
  452. outcount = 0;
  453. goto outcount_ok;
  454. }
  455. #ifndef LIBICONV_PLUG
  456. else if (cd->fallbacks.uc_to_mb_fallback != NULL) {
  457. struct uc_to_mb_fallback_locals locals;
  458. locals.l_outbuf = outptr;
  459. locals.l_outbytesleft = outleft;
  460. locals.l_errno = 0;
  461. cd->fallbacks.uc_to_mb_fallback(wc,
  462. uc_to_mb_write_replacement,
  463. &locals,
  464. cd->fallbacks.data);
  465. if (locals.l_errno != 0) {
  466. cd->istate = last_istate;
  467. errno = locals.l_errno;
  468. return -1;
  469. }
  470. outptr = locals.l_outbuf;
  471. outleft = locals.l_outbytesleft;
  472. outcount = 0;
  473. goto outcount_ok;
  474. }
  475. #endif
  476. outcount = cd->ofuncs.xxx_wctomb(cd,outptr,0xFFFD,outleft);
  477. if (outcount != RET_ILUNI)
  478. goto outcount_ok;
  479. cd->istate = last_istate;
  480. errno = EILSEQ;
  481. return -1;
  482. outcount_ok:
  483. if (outcount < 0) {
  484. cd->istate = last_istate;
  485. errno = E2BIG;
  486. return -1;
  487. }
  488. #ifndef LIBICONV_PLUG
  489. if (cd->hooks.uc_hook)
  490. (*cd->hooks.uc_hook)(wc, cd->hooks.data);
  491. #endif
  492. if (!(outcount <= outleft)) abort();
  493. outptr += outcount;
  494. outleft -= outcount;
  495. outcount_zero:
  496. *outbuf = (char*) outptr;
  497. *outbytesleft = outleft;
  498. }
  499. }
  500. if (cd->ofuncs.xxx_reset) {
  501. unsigned char* outptr = (unsigned char*) *outbuf;
  502. size_t outleft = *outbytesleft;
  503. int outcount = cd->ofuncs.xxx_reset(cd,outptr,outleft);
  504. if (outcount < 0) {
  505. errno = E2BIG;
  506. return -1;
  507. }
  508. if (!(outcount <= outleft)) abort();
  509. *outbuf = (char*) (outptr + outcount);
  510. *outbytesleft = outleft - outcount;
  511. }
  512. memset(&cd->istate,'\0',sizeof(state_t));
  513. memset(&cd->ostate,'\0',sizeof(state_t));
  514. return result;
  515. }
  516. }