iconv.c 11 KB

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