mbrtowc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* Convert multibyte character to wide character.
  2. Copyright (C) 1999-2002, 2005-2012 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2008.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <wchar.h>
  17. #if GNULIB_defined_mbstate_t
  18. /* Implement mbrtowc() on top of mbtowc(). */
  19. # include <errno.h>
  20. # include <stdlib.h>
  21. # include "localcharset.h"
  22. # include "streq.h"
  23. # include "verify.h"
  24. verify (sizeof (mbstate_t) >= 4);
  25. static char internal_state[4];
  26. size_t
  27. mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
  28. {
  29. char *pstate = (char *)ps;
  30. if (s == NULL)
  31. {
  32. pwc = NULL;
  33. s = "";
  34. n = 1;
  35. }
  36. if (n == 0)
  37. return (size_t)(-2);
  38. /* Here n > 0. */
  39. if (pstate == NULL)
  40. pstate = internal_state;
  41. {
  42. size_t nstate = pstate[0];
  43. char buf[4];
  44. const char *p;
  45. size_t m;
  46. switch (nstate)
  47. {
  48. case 0:
  49. p = s;
  50. m = n;
  51. break;
  52. case 3:
  53. buf[2] = pstate[3];
  54. /*FALLTHROUGH*/
  55. case 2:
  56. buf[1] = pstate[2];
  57. /*FALLTHROUGH*/
  58. case 1:
  59. buf[0] = pstate[1];
  60. p = buf;
  61. m = nstate;
  62. buf[m++] = s[0];
  63. if (n >= 2 && m < 4)
  64. {
  65. buf[m++] = s[1];
  66. if (n >= 3 && m < 4)
  67. buf[m++] = s[2];
  68. }
  69. break;
  70. default:
  71. errno = EINVAL;
  72. return (size_t)(-1);
  73. }
  74. /* Here m > 0. */
  75. # if __GLIBC__ || defined __UCLIBC__
  76. /* Work around bug <http://sourceware.org/bugzilla/show_bug.cgi?id=9674> */
  77. mbtowc (NULL, NULL, 0);
  78. # endif
  79. {
  80. int res = mbtowc (pwc, p, m);
  81. if (res >= 0)
  82. {
  83. if (pwc != NULL && ((*pwc == 0) != (res == 0)))
  84. abort ();
  85. if (nstate >= (res > 0 ? res : 1))
  86. abort ();
  87. res -= nstate;
  88. pstate[0] = 0;
  89. return res;
  90. }
  91. /* mbtowc does not distinguish between invalid and incomplete multibyte
  92. sequences. But mbrtowc needs to make this distinction.
  93. There are two possible approaches:
  94. - Use iconv() and its return value.
  95. - Use built-in knowledge about the possible encodings.
  96. Given the low quality of implementation of iconv() on the systems that
  97. lack mbrtowc(), we use the second approach.
  98. The possible encodings are:
  99. - 8-bit encodings,
  100. - EUC-JP, EUC-KR, GB2312, EUC-TW, BIG5, GB18030, SJIS,
  101. - UTF-8.
  102. Use specialized code for each. */
  103. if (m >= 4 || m >= MB_CUR_MAX)
  104. goto invalid;
  105. /* Here MB_CUR_MAX > 1 and 0 < m < 4. */
  106. {
  107. const char *encoding = locale_charset ();
  108. if (STREQ (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
  109. {
  110. /* Cf. unistr/u8-mblen.c. */
  111. unsigned char c = (unsigned char) p[0];
  112. if (c >= 0xc2)
  113. {
  114. if (c < 0xe0)
  115. {
  116. if (m == 1)
  117. goto incomplete;
  118. }
  119. else if (c < 0xf0)
  120. {
  121. if (m == 1)
  122. goto incomplete;
  123. if (m == 2)
  124. {
  125. unsigned char c2 = (unsigned char) p[1];
  126. if ((c2 ^ 0x80) < 0x40
  127. && (c >= 0xe1 || c2 >= 0xa0)
  128. && (c != 0xed || c2 < 0xa0))
  129. goto incomplete;
  130. }
  131. }
  132. else if (c <= 0xf4)
  133. {
  134. if (m == 1)
  135. goto incomplete;
  136. else /* m == 2 || m == 3 */
  137. {
  138. unsigned char c2 = (unsigned char) p[1];
  139. if ((c2 ^ 0x80) < 0x40
  140. && (c >= 0xf1 || c2 >= 0x90)
  141. && (c < 0xf4 || (c == 0xf4 && c2 < 0x90)))
  142. {
  143. if (m == 2)
  144. goto incomplete;
  145. else /* m == 3 */
  146. {
  147. unsigned char c3 = (unsigned char) p[2];
  148. if ((c3 ^ 0x80) < 0x40)
  149. goto incomplete;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. goto invalid;
  156. }
  157. /* As a reference for this code, you can use the GNU libiconv
  158. implementation. Look for uses of the RET_TOOFEW macro. */
  159. if (STREQ (encoding, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
  160. {
  161. if (m == 1)
  162. {
  163. unsigned char c = (unsigned char) p[0];
  164. if ((c >= 0xa1 && c < 0xff) || c == 0x8e || c == 0x8f)
  165. goto incomplete;
  166. }
  167. if (m == 2)
  168. {
  169. unsigned char c = (unsigned char) p[0];
  170. if (c == 0x8f)
  171. {
  172. unsigned char c2 = (unsigned char) p[1];
  173. if (c2 >= 0xa1 && c2 < 0xff)
  174. goto incomplete;
  175. }
  176. }
  177. goto invalid;
  178. }
  179. if (STREQ (encoding, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
  180. || STREQ (encoding, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
  181. || STREQ (encoding, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
  182. {
  183. if (m == 1)
  184. {
  185. unsigned char c = (unsigned char) p[0];
  186. if (c >= 0xa1 && c < 0xff)
  187. goto incomplete;
  188. }
  189. goto invalid;
  190. }
  191. if (STREQ (encoding, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
  192. {
  193. if (m == 1)
  194. {
  195. unsigned char c = (unsigned char) p[0];
  196. if ((c >= 0xa1 && c < 0xff) || c == 0x8e)
  197. goto incomplete;
  198. }
  199. else /* m == 2 || m == 3 */
  200. {
  201. unsigned char c = (unsigned char) p[0];
  202. if (c == 0x8e)
  203. goto incomplete;
  204. }
  205. goto invalid;
  206. }
  207. if (STREQ (encoding, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
  208. {
  209. if (m == 1)
  210. {
  211. unsigned char c = (unsigned char) p[0];
  212. if ((c >= 0x90 && c <= 0xe3) || (c >= 0xf8 && c <= 0xfe))
  213. goto incomplete;
  214. }
  215. else /* m == 2 || m == 3 */
  216. {
  217. unsigned char c = (unsigned char) p[0];
  218. if (c >= 0x90 && c <= 0xe3)
  219. {
  220. unsigned char c2 = (unsigned char) p[1];
  221. if (c2 >= 0x30 && c2 <= 0x39)
  222. {
  223. if (m == 2)
  224. goto incomplete;
  225. else /* m == 3 */
  226. {
  227. unsigned char c3 = (unsigned char) p[2];
  228. if (c3 >= 0x81 && c3 <= 0xfe)
  229. goto incomplete;
  230. }
  231. }
  232. }
  233. }
  234. goto invalid;
  235. }
  236. if (STREQ (encoding, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
  237. {
  238. if (m == 1)
  239. {
  240. unsigned char c = (unsigned char) p[0];
  241. if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)
  242. || (c >= 0xf0 && c <= 0xf9))
  243. goto incomplete;
  244. }
  245. goto invalid;
  246. }
  247. /* An unknown multibyte encoding. */
  248. goto incomplete;
  249. }
  250. incomplete:
  251. {
  252. size_t k = nstate;
  253. /* Here 0 <= k < m < 4. */
  254. pstate[++k] = s[0];
  255. if (k < m)
  256. {
  257. pstate[++k] = s[1];
  258. if (k < m)
  259. pstate[++k] = s[2];
  260. }
  261. if (k != m)
  262. abort ();
  263. }
  264. pstate[0] = m;
  265. return (size_t)(-2);
  266. invalid:
  267. errno = EILSEQ;
  268. /* The conversion state is undefined, says POSIX. */
  269. return (size_t)(-1);
  270. }
  271. }
  272. }
  273. #else
  274. /* Override the system's mbrtowc() function. */
  275. # undef mbrtowc
  276. size_t
  277. rpl_mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
  278. {
  279. # if MBRTOWC_NULL_ARG2_BUG || MBRTOWC_RETVAL_BUG
  280. if (s == NULL)
  281. {
  282. pwc = NULL;
  283. s = "";
  284. n = 1;
  285. }
  286. # endif
  287. # if MBRTOWC_RETVAL_BUG
  288. {
  289. static mbstate_t internal_state;
  290. /* Override mbrtowc's internal state. We cannot call mbsinit() on the
  291. hidden internal state, but we can call it on our variable. */
  292. if (ps == NULL)
  293. ps = &internal_state;
  294. if (!mbsinit (ps))
  295. {
  296. /* Parse the rest of the multibyte character byte for byte. */
  297. size_t count = 0;
  298. for (; n > 0; s++, n--)
  299. {
  300. wchar_t wc;
  301. size_t ret = mbrtowc (&wc, s, 1, ps);
  302. if (ret == (size_t)(-1))
  303. return (size_t)(-1);
  304. count++;
  305. if (ret != (size_t)(-2))
  306. {
  307. /* The multibyte character has been completed. */
  308. if (pwc != NULL)
  309. *pwc = wc;
  310. return (wc == 0 ? 0 : count);
  311. }
  312. }
  313. return (size_t)(-2);
  314. }
  315. }
  316. # endif
  317. # if MBRTOWC_NUL_RETVAL_BUG
  318. {
  319. wchar_t wc;
  320. size_t ret = mbrtowc (&wc, s, n, ps);
  321. if (ret != (size_t)(-1) && ret != (size_t)(-2))
  322. {
  323. if (pwc != NULL)
  324. *pwc = wc;
  325. if (wc == 0)
  326. ret = 0;
  327. }
  328. return ret;
  329. }
  330. # else
  331. {
  332. # if MBRTOWC_NULL_ARG1_BUG
  333. wchar_t dummy;
  334. if (pwc == NULL)
  335. pwc = &dummy;
  336. # endif
  337. return mbrtowc (pwc, s, n, ps);
  338. }
  339. # endif
  340. }
  341. #endif