iconv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* Character set conversion.
  2. Copyright (C) 1999-2001, 2007, 2009-2012 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <iconv.h>
  17. #include <stddef.h>
  18. #if REPLACE_ICONV_UTF
  19. # include <errno.h>
  20. # include <stdint.h>
  21. # include <stdlib.h>
  22. # include "unistr.h"
  23. # ifndef uintptr_t
  24. # define uintptr_t unsigned long
  25. # endif
  26. #endif
  27. #if REPLACE_ICONV_UTF
  28. /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
  29. /* Return code if invalid. (xxx_mbtowc) */
  30. # define RET_ILSEQ -1
  31. /* Return code if no bytes were read. (xxx_mbtowc) */
  32. # define RET_TOOFEW -2
  33. /* Return code if invalid. (xxx_wctomb) */
  34. # define RET_ILUNI -1
  35. /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
  36. # define RET_TOOSMALL -2
  37. /*
  38. * UTF-16BE
  39. */
  40. /* Specification: RFC 2781 */
  41. static int
  42. utf16be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
  43. {
  44. if (n >= 2)
  45. {
  46. ucs4_t wc = (s[0] << 8) + s[1];
  47. if (wc >= 0xd800 && wc < 0xdc00)
  48. {
  49. if (n >= 4)
  50. {
  51. ucs4_t wc2 = (s[2] << 8) + s[3];
  52. if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
  53. return RET_ILSEQ;
  54. *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
  55. return 4;
  56. }
  57. }
  58. else if (wc >= 0xdc00 && wc < 0xe000)
  59. {
  60. return RET_ILSEQ;
  61. }
  62. else
  63. {
  64. *pwc = wc;
  65. return 2;
  66. }
  67. }
  68. return RET_TOOFEW;
  69. }
  70. static int
  71. utf16be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
  72. {
  73. if (!(wc >= 0xd800 && wc < 0xe000))
  74. {
  75. if (wc < 0x10000)
  76. {
  77. if (n >= 2)
  78. {
  79. r[0] = (unsigned char) (wc >> 8);
  80. r[1] = (unsigned char) wc;
  81. return 2;
  82. }
  83. else
  84. return RET_TOOSMALL;
  85. }
  86. else if (wc < 0x110000)
  87. {
  88. if (n >= 4)
  89. {
  90. ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
  91. ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
  92. r[0] = (unsigned char) (wc1 >> 8);
  93. r[1] = (unsigned char) wc1;
  94. r[2] = (unsigned char) (wc2 >> 8);
  95. r[3] = (unsigned char) wc2;
  96. return 4;
  97. }
  98. else
  99. return RET_TOOSMALL;
  100. }
  101. }
  102. return RET_ILUNI;
  103. }
  104. /*
  105. * UTF-16LE
  106. */
  107. /* Specification: RFC 2781 */
  108. static int
  109. utf16le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
  110. {
  111. if (n >= 2)
  112. {
  113. ucs4_t wc = s[0] + (s[1] << 8);
  114. if (wc >= 0xd800 && wc < 0xdc00)
  115. {
  116. if (n >= 4)
  117. {
  118. ucs4_t wc2 = s[2] + (s[3] << 8);
  119. if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
  120. return RET_ILSEQ;
  121. *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
  122. return 4;
  123. }
  124. }
  125. else if (wc >= 0xdc00 && wc < 0xe000)
  126. {
  127. return RET_ILSEQ;
  128. }
  129. else
  130. {
  131. *pwc = wc;
  132. return 2;
  133. }
  134. }
  135. return RET_TOOFEW;
  136. }
  137. static int
  138. utf16le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
  139. {
  140. if (!(wc >= 0xd800 && wc < 0xe000))
  141. {
  142. if (wc < 0x10000)
  143. {
  144. if (n >= 2)
  145. {
  146. r[0] = (unsigned char) wc;
  147. r[1] = (unsigned char) (wc >> 8);
  148. return 2;
  149. }
  150. else
  151. return RET_TOOSMALL;
  152. }
  153. else if (wc < 0x110000)
  154. {
  155. if (n >= 4)
  156. {
  157. ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
  158. ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
  159. r[0] = (unsigned char) wc1;
  160. r[1] = (unsigned char) (wc1 >> 8);
  161. r[2] = (unsigned char) wc2;
  162. r[3] = (unsigned char) (wc2 >> 8);
  163. return 4;
  164. }
  165. else
  166. return RET_TOOSMALL;
  167. }
  168. }
  169. return RET_ILUNI;
  170. }
  171. /*
  172. * UTF-32BE
  173. */
  174. /* Specification: Unicode 3.1 Standard Annex #19 */
  175. static int
  176. utf32be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
  177. {
  178. if (n >= 4)
  179. {
  180. ucs4_t wc = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
  181. if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
  182. {
  183. *pwc = wc;
  184. return 4;
  185. }
  186. else
  187. return RET_ILSEQ;
  188. }
  189. return RET_TOOFEW;
  190. }
  191. static int
  192. utf32be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
  193. {
  194. if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
  195. {
  196. if (n >= 4)
  197. {
  198. r[0] = 0;
  199. r[1] = (unsigned char) (wc >> 16);
  200. r[2] = (unsigned char) (wc >> 8);
  201. r[3] = (unsigned char) wc;
  202. return 4;
  203. }
  204. else
  205. return RET_TOOSMALL;
  206. }
  207. return RET_ILUNI;
  208. }
  209. /*
  210. * UTF-32LE
  211. */
  212. /* Specification: Unicode 3.1 Standard Annex #19 */
  213. static int
  214. utf32le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
  215. {
  216. if (n >= 4)
  217. {
  218. ucs4_t wc = s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24);
  219. if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
  220. {
  221. *pwc = wc;
  222. return 4;
  223. }
  224. else
  225. return RET_ILSEQ;
  226. }
  227. return RET_TOOFEW;
  228. }
  229. static int
  230. utf32le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
  231. {
  232. if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
  233. {
  234. if (n >= 4)
  235. {
  236. r[0] = (unsigned char) wc;
  237. r[1] = (unsigned char) (wc >> 8);
  238. r[2] = (unsigned char) (wc >> 16);
  239. r[3] = 0;
  240. return 4;
  241. }
  242. else
  243. return RET_TOOSMALL;
  244. }
  245. return RET_ILUNI;
  246. }
  247. #endif
  248. size_t
  249. rpl_iconv (iconv_t cd,
  250. ICONV_CONST char **inbuf, size_t *inbytesleft,
  251. char **outbuf, size_t *outbytesleft)
  252. #undef iconv
  253. {
  254. #if REPLACE_ICONV_UTF
  255. switch ((uintptr_t) cd)
  256. {
  257. {
  258. int (*xxx_wctomb) (unsigned char *, ucs4_t, size_t);
  259. case (uintptr_t) _ICONV_UTF8_UTF16BE:
  260. xxx_wctomb = utf16be_wctomb;
  261. goto loop_from_utf8;
  262. case (uintptr_t) _ICONV_UTF8_UTF16LE:
  263. xxx_wctomb = utf16le_wctomb;
  264. goto loop_from_utf8;
  265. case (uintptr_t) _ICONV_UTF8_UTF32BE:
  266. xxx_wctomb = utf32be_wctomb;
  267. goto loop_from_utf8;
  268. case (uintptr_t) _ICONV_UTF8_UTF32LE:
  269. xxx_wctomb = utf32le_wctomb;
  270. goto loop_from_utf8;
  271. loop_from_utf8:
  272. if (inbuf == NULL || *inbuf == NULL)
  273. return 0;
  274. {
  275. ICONV_CONST char *inptr = *inbuf;
  276. size_t inleft = *inbytesleft;
  277. char *outptr = *outbuf;
  278. size_t outleft = *outbytesleft;
  279. size_t res = 0;
  280. while (inleft > 0)
  281. {
  282. ucs4_t uc;
  283. int m = u8_mbtoucr (&uc, (const uint8_t *) inptr, inleft);
  284. if (m <= 0)
  285. {
  286. if (m == -1)
  287. {
  288. errno = EILSEQ;
  289. res = (size_t)(-1);
  290. break;
  291. }
  292. if (m == -2)
  293. {
  294. errno = EINVAL;
  295. res = (size_t)(-1);
  296. break;
  297. }
  298. abort ();
  299. }
  300. else
  301. {
  302. int n = xxx_wctomb ((uint8_t *) outptr, uc, outleft);
  303. if (n < 0)
  304. {
  305. if (n == RET_ILUNI)
  306. {
  307. errno = EILSEQ;
  308. res = (size_t)(-1);
  309. break;
  310. }
  311. if (n == RET_TOOSMALL)
  312. {
  313. errno = E2BIG;
  314. res = (size_t)(-1);
  315. break;
  316. }
  317. abort ();
  318. }
  319. else
  320. {
  321. inptr += m;
  322. inleft -= m;
  323. outptr += n;
  324. outleft -= n;
  325. }
  326. }
  327. }
  328. *inbuf = inptr;
  329. *inbytesleft = inleft;
  330. *outbuf = outptr;
  331. *outbytesleft = outleft;
  332. return res;
  333. }
  334. }
  335. {
  336. int (*xxx_mbtowc) (ucs4_t *, const unsigned char *, size_t);
  337. case (uintptr_t) _ICONV_UTF16BE_UTF8:
  338. xxx_mbtowc = utf16be_mbtowc;
  339. goto loop_to_utf8;
  340. case (uintptr_t) _ICONV_UTF16LE_UTF8:
  341. xxx_mbtowc = utf16le_mbtowc;
  342. goto loop_to_utf8;
  343. case (uintptr_t) _ICONV_UTF32BE_UTF8:
  344. xxx_mbtowc = utf32be_mbtowc;
  345. goto loop_to_utf8;
  346. case (uintptr_t) _ICONV_UTF32LE_UTF8:
  347. xxx_mbtowc = utf32le_mbtowc;
  348. goto loop_to_utf8;
  349. loop_to_utf8:
  350. if (inbuf == NULL || *inbuf == NULL)
  351. return 0;
  352. {
  353. ICONV_CONST char *inptr = *inbuf;
  354. size_t inleft = *inbytesleft;
  355. char *outptr = *outbuf;
  356. size_t outleft = *outbytesleft;
  357. size_t res = 0;
  358. while (inleft > 0)
  359. {
  360. ucs4_t uc;
  361. int m = xxx_mbtowc (&uc, (const uint8_t *) inptr, inleft);
  362. if (m <= 0)
  363. {
  364. if (m == RET_ILSEQ)
  365. {
  366. errno = EILSEQ;
  367. res = (size_t)(-1);
  368. break;
  369. }
  370. if (m == RET_TOOFEW)
  371. {
  372. errno = EINVAL;
  373. res = (size_t)(-1);
  374. break;
  375. }
  376. abort ();
  377. }
  378. else
  379. {
  380. int n = u8_uctomb ((uint8_t *) outptr, uc, outleft);
  381. if (n < 0)
  382. {
  383. if (n == -1)
  384. {
  385. errno = EILSEQ;
  386. res = (size_t)(-1);
  387. break;
  388. }
  389. if (n == -2)
  390. {
  391. errno = E2BIG;
  392. res = (size_t)(-1);
  393. break;
  394. }
  395. abort ();
  396. }
  397. else
  398. {
  399. inptr += m;
  400. inleft -= m;
  401. outptr += n;
  402. outleft -= n;
  403. }
  404. }
  405. }
  406. *inbuf = inptr;
  407. *inbytesleft = inleft;
  408. *outbuf = outptr;
  409. *outbytesleft = outleft;
  410. return res;
  411. }
  412. }
  413. }
  414. #endif
  415. return iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft);
  416. }