loop_wchar.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (C) 2000-2002, 2005-2006, 2008-2009, 2011 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 three conversion loops:
  20. - from wchar_t to anything else,
  21. - from anything else to wchar_t,
  22. - from wchar_t to wchar_t.
  23. */
  24. #if HAVE_WCRTOMB || HAVE_MBRTOWC
  25. /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
  26. <wchar.h>.
  27. BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
  28. included before <wchar.h>.
  29. In some builds of uClibc, <wchar.h> is nonexistent and wchar_t is defined
  30. by <stddef.h>. */
  31. # include <stddef.h>
  32. # include <stdio.h>
  33. # include <time.h>
  34. # include <wchar.h>
  35. # define BUF_SIZE 64 /* assume MB_LEN_MAX <= 64 */
  36. /* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
  37. extern size_t mbrtowc ();
  38. # ifdef mbstate_t
  39. # define mbrtowc(pwc, s, n, ps) (mbrtowc)(pwc, s, n, 0)
  40. # define mbsinit(ps) 1
  41. # endif
  42. # ifndef mbsinit
  43. # if !HAVE_MBSINIT
  44. # define mbsinit(ps) 1
  45. # endif
  46. # endif
  47. #endif
  48. /*
  49. * The first two conversion loops have an extended conversion descriptor.
  50. */
  51. struct wchar_conv_struct {
  52. struct conv_struct parent;
  53. #if HAVE_WCRTOMB || HAVE_MBRTOWC
  54. mbstate_t state;
  55. #endif
  56. };
  57. #if HAVE_WCRTOMB
  58. /* From wchar_t to anything else. */
  59. #ifndef LIBICONV_PLUG
  60. #if 0
  61. struct wc_to_mb_fallback_locals {
  62. struct wchar_conv_struct * l_wcd;
  63. char* l_outbuf;
  64. size_t l_outbytesleft;
  65. int l_errno;
  66. };
  67. /* A callback that writes a string given in the locale encoding. */
  68. static void wc_to_mb_write_replacement (const char *buf, size_t buflen,
  69. void* callback_arg)
  70. {
  71. struct wc_to_mb_fallback_locals * plocals =
  72. (struct wc_to_mb_fallback_locals *) callback_arg;
  73. /* Do nothing if already encountered an error in a previous call. */
  74. if (plocals->l_errno == 0) {
  75. /* Attempt to convert the passed buffer to the target encoding.
  76. Here we don't support characters split across multiple calls. */
  77. const char* bufptr = buf;
  78. size_t bufleft = buflen;
  79. size_t res = unicode_loop_convert(&plocals->l_wcd->parent,
  80. &bufptr,&bufleft,
  81. &plocals->l_outbuf,&plocals->l_outbytesleft);
  82. if (res == (size_t)(-1)) {
  83. if (errno == EILSEQ || errno == EINVAL)
  84. /* Invalid buf contents. */
  85. plocals->l_errno = EILSEQ;
  86. else if (errno == E2BIG)
  87. /* Output buffer too small. */
  88. plocals->l_errno = E2BIG;
  89. else
  90. abort();
  91. } else {
  92. /* Successful conversion. */
  93. if (bufleft > 0)
  94. abort();
  95. }
  96. }
  97. }
  98. #else
  99. struct wc_to_mb_fallback_locals {
  100. char* l_outbuf;
  101. size_t l_outbytesleft;
  102. int l_errno;
  103. };
  104. /* A callback that writes a string given in the target encoding. */
  105. static void wc_to_mb_write_replacement (const char *buf, size_t buflen,
  106. void* callback_arg)
  107. {
  108. struct wc_to_mb_fallback_locals * plocals =
  109. (struct wc_to_mb_fallback_locals *) callback_arg;
  110. /* Do nothing if already encountered an error in a previous call. */
  111. if (plocals->l_errno == 0) {
  112. /* Attempt to copy the passed buffer to the output buffer. */
  113. if (plocals->l_outbytesleft < buflen)
  114. plocals->l_errno = E2BIG;
  115. else {
  116. memcpy(plocals->l_outbuf, buf, buflen);
  117. plocals->l_outbuf += buflen;
  118. plocals->l_outbytesleft -= buflen;
  119. }
  120. }
  121. }
  122. #endif
  123. #endif /* !LIBICONV_PLUG */
  124. static size_t wchar_from_loop_convert (iconv_t icd,
  125. const char* * inbuf, size_t *inbytesleft,
  126. char* * outbuf, size_t *outbytesleft)
  127. {
  128. struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
  129. size_t result = 0;
  130. while (*inbytesleft >= sizeof(wchar_t)) {
  131. const wchar_t * inptr = (const wchar_t *) *inbuf;
  132. size_t inleft = *inbytesleft;
  133. char buf[BUF_SIZE];
  134. mbstate_t state = wcd->state;
  135. size_t bufcount = 0;
  136. while (inleft >= sizeof(wchar_t)) {
  137. /* Convert one wchar_t to multibyte representation. */
  138. size_t count = wcrtomb(buf+bufcount,*inptr,&state);
  139. if (count == (size_t)(-1)) {
  140. /* Invalid input. */
  141. if (wcd->parent.discard_ilseq) {
  142. count = 0;
  143. }
  144. #ifndef LIBICONV_PLUG
  145. else if (wcd->parent.fallbacks.wc_to_mb_fallback != NULL) {
  146. /* Drop the contents of buf[] accumulated so far, and instead
  147. pass all queued wide characters to the fallback handler. */
  148. struct wc_to_mb_fallback_locals locals;
  149. const wchar_t * fallback_inptr;
  150. #if 0
  151. locals.l_wcd = wcd;
  152. #endif
  153. locals.l_outbuf = *outbuf;
  154. locals.l_outbytesleft = *outbytesleft;
  155. locals.l_errno = 0;
  156. for (fallback_inptr = (const wchar_t *) *inbuf;
  157. fallback_inptr <= inptr;
  158. fallback_inptr++)
  159. wcd->parent.fallbacks.wc_to_mb_fallback(*fallback_inptr,
  160. wc_to_mb_write_replacement,
  161. &locals,
  162. wcd->parent.fallbacks.data);
  163. if (locals.l_errno != 0) {
  164. errno = locals.l_errno;
  165. return -1;
  166. }
  167. wcd->state = state;
  168. *inbuf = (const char *) (inptr + 1);
  169. *inbytesleft = inleft - sizeof(wchar_t);
  170. *outbuf = locals.l_outbuf;
  171. *outbytesleft = locals.l_outbytesleft;
  172. result += 1;
  173. break;
  174. }
  175. #endif
  176. else {
  177. errno = EILSEQ;
  178. return -1;
  179. }
  180. }
  181. inptr++;
  182. inleft -= sizeof(wchar_t);
  183. bufcount += count;
  184. if (count == 0) {
  185. /* Continue, append next wchar_t. */
  186. } else {
  187. /* Attempt to convert the accumulated multibyte representations
  188. to the target encoding. */
  189. const char* bufptr = buf;
  190. size_t bufleft = bufcount;
  191. char* outptr = *outbuf;
  192. size_t outleft = *outbytesleft;
  193. size_t res = unicode_loop_convert(&wcd->parent,
  194. &bufptr,&bufleft,
  195. &outptr,&outleft);
  196. if (res == (size_t)(-1)) {
  197. if (errno == EILSEQ)
  198. /* Invalid input. */
  199. return -1;
  200. else if (errno == E2BIG)
  201. /* Output buffer too small. */
  202. return -1;
  203. else if (errno == EINVAL) {
  204. /* Continue, append next wchar_t, but avoid buffer overrun. */
  205. if (bufcount + MB_CUR_MAX > BUF_SIZE)
  206. abort();
  207. } else
  208. abort();
  209. } else {
  210. /* Successful conversion. */
  211. wcd->state = state;
  212. *inbuf = (const char *) inptr;
  213. *inbytesleft = inleft;
  214. *outbuf = outptr;
  215. *outbytesleft = outleft;
  216. result += res;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. return result;
  223. }
  224. static size_t wchar_from_loop_reset (iconv_t icd,
  225. char* * outbuf, size_t *outbytesleft)
  226. {
  227. struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
  228. if (outbuf == NULL || *outbuf == NULL) {
  229. /* Reset the states. */
  230. memset(&wcd->state,'\0',sizeof(mbstate_t));
  231. return unicode_loop_reset(&wcd->parent,NULL,NULL);
  232. } else {
  233. if (!mbsinit(&wcd->state)) {
  234. mbstate_t state = wcd->state;
  235. char buf[BUF_SIZE];
  236. size_t bufcount = wcrtomb(buf,(wchar_t)0,&state);
  237. if (bufcount == (size_t)(-1) || bufcount == 0 || buf[bufcount-1] != '\0')
  238. abort();
  239. else {
  240. const char* bufptr = buf;
  241. size_t bufleft = bufcount-1;
  242. char* outptr = *outbuf;
  243. size_t outleft = *outbytesleft;
  244. size_t res = unicode_loop_convert(&wcd->parent,
  245. &bufptr,&bufleft,
  246. &outptr,&outleft);
  247. if (res == (size_t)(-1)) {
  248. if (errno == E2BIG)
  249. return -1;
  250. else
  251. abort();
  252. } else {
  253. res = unicode_loop_reset(&wcd->parent,&outptr,&outleft);
  254. if (res == (size_t)(-1))
  255. return res;
  256. else {
  257. /* Successful. */
  258. wcd->state = state;
  259. *outbuf = outptr;
  260. *outbytesleft = outleft;
  261. return 0;
  262. }
  263. }
  264. }
  265. } else
  266. return unicode_loop_reset(&wcd->parent,outbuf,outbytesleft);
  267. }
  268. }
  269. #endif
  270. #if HAVE_MBRTOWC
  271. /* From anything else to wchar_t. */
  272. #ifndef LIBICONV_PLUG
  273. struct mb_to_wc_fallback_locals {
  274. char* l_outbuf;
  275. size_t l_outbytesleft;
  276. int l_errno;
  277. };
  278. static void mb_to_wc_write_replacement (const wchar_t *buf, size_t buflen,
  279. void* callback_arg)
  280. {
  281. struct mb_to_wc_fallback_locals * plocals =
  282. (struct mb_to_wc_fallback_locals *) callback_arg;
  283. /* Do nothing if already encountered an error in a previous call. */
  284. if (plocals->l_errno == 0) {
  285. /* Attempt to copy the passed buffer to the output buffer. */
  286. if (plocals->l_outbytesleft < sizeof(wchar_t)*buflen)
  287. plocals->l_errno = E2BIG;
  288. else {
  289. for (; buflen > 0; buf++, buflen--) {
  290. *(wchar_t*) plocals->l_outbuf = *buf;
  291. plocals->l_outbuf += sizeof(wchar_t);
  292. plocals->l_outbytesleft -= sizeof(wchar_t);
  293. }
  294. }
  295. }
  296. }
  297. #endif /* !LIBICONV_PLUG */
  298. static size_t wchar_to_loop_convert (iconv_t icd,
  299. const char* * inbuf, size_t *inbytesleft,
  300. char* * outbuf, size_t *outbytesleft)
  301. {
  302. struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
  303. size_t result = 0;
  304. while (*inbytesleft > 0) {
  305. size_t incount;
  306. for (incount = 1; ; ) {
  307. /* Here incount <= *inbytesleft. */
  308. char buf[BUF_SIZE];
  309. const char* inptr = *inbuf;
  310. size_t inleft = incount;
  311. char* bufptr = buf;
  312. size_t bufleft = BUF_SIZE;
  313. size_t res = unicode_loop_convert(&wcd->parent,
  314. &inptr,&inleft,
  315. &bufptr,&bufleft);
  316. if (res == (size_t)(-1)) {
  317. if (errno == EILSEQ)
  318. /* Invalid input. */
  319. return -1;
  320. else if (errno == EINVAL) {
  321. /* Incomplete input. Next try with one more input byte. */
  322. } else
  323. /* E2BIG shouldn't occur. */
  324. abort();
  325. } else {
  326. /* Successful conversion. */
  327. size_t bufcount = bufptr-buf; /* = BUF_SIZE-bufleft */
  328. mbstate_t state = wcd->state;
  329. wchar_t wc;
  330. res = mbrtowc(&wc,buf,bufcount,&state);
  331. if (res == (size_t)(-2)) {
  332. /* Next try with one more input byte. */
  333. } else {
  334. if (res == (size_t)(-1)) {
  335. /* Invalid input. */
  336. if (wcd->parent.discard_ilseq) {
  337. }
  338. #ifndef LIBICONV_PLUG
  339. else if (wcd->parent.fallbacks.mb_to_wc_fallback != NULL) {
  340. /* Drop the contents of buf[] accumulated so far, and instead
  341. pass all queued chars to the fallback handler. */
  342. struct mb_to_wc_fallback_locals locals;
  343. locals.l_outbuf = *outbuf;
  344. locals.l_outbytesleft = *outbytesleft;
  345. locals.l_errno = 0;
  346. wcd->parent.fallbacks.mb_to_wc_fallback(*inbuf, incount,
  347. mb_to_wc_write_replacement,
  348. &locals,
  349. wcd->parent.fallbacks.data);
  350. if (locals.l_errno != 0) {
  351. errno = locals.l_errno;
  352. return -1;
  353. }
  354. /* Restoring the state is not needed because it is the initial
  355. state anyway: For all known locale encodings, the multibyte
  356. to wchar_t conversion doesn't have shift state, and we have
  357. excluded partial accumulated characters. */
  358. /* wcd->state = state; */
  359. *inbuf += incount;
  360. *inbytesleft -= incount;
  361. *outbuf = locals.l_outbuf;
  362. *outbytesleft = locals.l_outbytesleft;
  363. result += 1;
  364. break;
  365. }
  366. #endif
  367. else
  368. return -1;
  369. } else {
  370. if (*outbytesleft < sizeof(wchar_t)) {
  371. errno = E2BIG;
  372. return -1;
  373. }
  374. *(wchar_t*) *outbuf = wc;
  375. /* Restoring the state is not needed because it is the initial
  376. state anyway: For all known locale encodings, the multibyte
  377. to wchar_t conversion doesn't have shift state, and we have
  378. excluded partial accumulated characters. */
  379. /* wcd->state = state; */
  380. *outbuf += sizeof(wchar_t);
  381. *outbytesleft -= sizeof(wchar_t);
  382. }
  383. *inbuf += incount;
  384. *inbytesleft -= incount;
  385. result += res;
  386. break;
  387. }
  388. }
  389. incount++;
  390. if (incount > *inbytesleft) {
  391. /* Incomplete input. */
  392. errno = EINVAL;
  393. return -1;
  394. }
  395. }
  396. }
  397. return result;
  398. }
  399. static size_t wchar_to_loop_reset (iconv_t icd,
  400. char* * outbuf, size_t *outbytesleft)
  401. {
  402. struct wchar_conv_struct * wcd = (struct wchar_conv_struct *) icd;
  403. size_t res = unicode_loop_reset(&wcd->parent,outbuf,outbytesleft);
  404. if (res == (size_t)(-1))
  405. return res;
  406. memset(&wcd->state,0,sizeof(mbstate_t));
  407. return 0;
  408. }
  409. #endif
  410. /* From wchar_t to wchar_t. */
  411. static size_t wchar_id_loop_convert (iconv_t icd,
  412. const char* * inbuf, size_t *inbytesleft,
  413. char* * outbuf, size_t *outbytesleft)
  414. {
  415. struct conv_struct * cd = (struct conv_struct *) icd;
  416. const wchar_t* inptr = (const wchar_t*) *inbuf;
  417. size_t inleft = *inbytesleft / sizeof(wchar_t);
  418. wchar_t* outptr = (wchar_t*) *outbuf;
  419. size_t outleft = *outbytesleft / sizeof(wchar_t);
  420. size_t count = (inleft <= outleft ? inleft : outleft);
  421. if (count > 0) {
  422. *inbytesleft -= count * sizeof(wchar_t);
  423. *outbytesleft -= count * sizeof(wchar_t);
  424. do {
  425. wchar_t wc = *inptr++;
  426. *outptr++ = wc;
  427. #ifndef LIBICONV_PLUG
  428. if (cd->hooks.wc_hook)
  429. (*cd->hooks.wc_hook)(wc, cd->hooks.data);
  430. #endif
  431. } while (--count > 0);
  432. *inbuf = (const char*) inptr;
  433. *outbuf = (char*) outptr;
  434. }
  435. return 0;
  436. }
  437. static size_t wchar_id_loop_reset (iconv_t icd,
  438. char* * outbuf, size_t *outbytesleft)
  439. {
  440. return 0;
  441. }