mbrtowc.c 11 KB

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