mbrtowc-impl.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Convert multibyte character to wide character.
  2. Copyright (C) 1999-2002, 2005-2021 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 3 of the License, or
  6. (at your option) 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
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
  14. /* This file contains the body of the mbrtowc and mbrtoc32 functions,
  15. when GNULIB_defined_mbstate_t is defined. */
  16. char *pstate = (char *)ps;
  17. if (s == NULL)
  18. {
  19. pwc = NULL;
  20. s = "";
  21. n = 1;
  22. }
  23. if (n == 0)
  24. return (size_t)(-2);
  25. /* Here n > 0. */
  26. if (pstate == NULL)
  27. pstate = internal_state;
  28. {
  29. size_t nstate = pstate[0];
  30. char buf[4];
  31. const char *p;
  32. size_t m;
  33. enc_t enc;
  34. int res;
  35. switch (nstate)
  36. {
  37. case 0:
  38. p = s;
  39. m = n;
  40. break;
  41. case 3:
  42. buf[2] = pstate[3];
  43. FALLTHROUGH;
  44. case 2:
  45. buf[1] = pstate[2];
  46. FALLTHROUGH;
  47. case 1:
  48. buf[0] = pstate[1];
  49. p = buf;
  50. m = nstate;
  51. buf[m++] = s[0];
  52. if (n >= 2 && m < 4)
  53. {
  54. buf[m++] = s[1];
  55. if (n >= 3 && m < 4)
  56. buf[m++] = s[2];
  57. }
  58. break;
  59. default:
  60. errno = EINVAL;
  61. return (size_t)(-1);
  62. }
  63. /* Here m > 0. */
  64. enc = locale_encoding_classification ();
  65. if (enc == enc_utf8) /* UTF-8 */
  66. {
  67. /* Achieve
  68. - multi-thread safety and
  69. - the ability to produce wide character values > WCHAR_MAX
  70. by not calling mbtowc() at all. */
  71. #include "mbrtowc-impl-utf8.h"
  72. }
  73. else
  74. {
  75. /* The hidden internal state of mbtowc would make this function not
  76. multi-thread safe. Achieve multi-thread safety through a lock. */
  77. wchar_t wc;
  78. res = mbtowc_with_lock (&wc, p, m);
  79. if (res >= 0)
  80. {
  81. if ((wc == 0) != (res == 0))
  82. abort ();
  83. if (pwc != NULL)
  84. *pwc = wc;
  85. goto success;
  86. }
  87. /* mbtowc does not distinguish between invalid and incomplete multibyte
  88. sequences. But mbrtowc needs to make this distinction.
  89. There are two possible approaches:
  90. - Use iconv() and its return value.
  91. - Use built-in knowledge about the possible encodings.
  92. Given the low quality of implementation of iconv() on the systems
  93. that lack mbrtowc(), we use the second approach.
  94. The possible encodings are:
  95. - 8-bit encodings,
  96. - EUC-JP, EUC-KR, GB2312, EUC-TW, BIG5, GB18030, SJIS,
  97. - UTF-8 (already handled above).
  98. Use specialized code for each. */
  99. if (m >= 4 || m >= MB_CUR_MAX)
  100. goto invalid;
  101. /* Here MB_CUR_MAX > 1 and 0 < m < 4. */
  102. switch (enc)
  103. {
  104. /* As a reference for this code, you can use the GNU libiconv
  105. implementation. Look for uses of the RET_TOOFEW macro. */
  106. case enc_eucjp: /* EUC-JP */
  107. {
  108. if (m == 1)
  109. {
  110. unsigned char c = (unsigned char) p[0];
  111. if ((c >= 0xa1 && c < 0xff) || c == 0x8e || c == 0x8f)
  112. goto incomplete;
  113. }
  114. if (m == 2)
  115. {
  116. unsigned char c = (unsigned char) p[0];
  117. if (c == 0x8f)
  118. {
  119. unsigned char c2 = (unsigned char) p[1];
  120. if (c2 >= 0xa1 && c2 < 0xff)
  121. goto incomplete;
  122. }
  123. }
  124. goto invalid;
  125. }
  126. case enc_94: /* EUC-KR, GB2312, BIG5 */
  127. {
  128. if (m == 1)
  129. {
  130. unsigned char c = (unsigned char) p[0];
  131. if (c >= 0xa1 && c < 0xff)
  132. goto incomplete;
  133. }
  134. goto invalid;
  135. }
  136. case enc_euctw: /* EUC-TW */
  137. {
  138. if (m == 1)
  139. {
  140. unsigned char c = (unsigned char) p[0];
  141. if ((c >= 0xa1 && c < 0xff) || c == 0x8e)
  142. goto incomplete;
  143. }
  144. else /* m == 2 || m == 3 */
  145. {
  146. unsigned char c = (unsigned char) p[0];
  147. if (c == 0x8e)
  148. goto incomplete;
  149. }
  150. goto invalid;
  151. }
  152. case enc_gb18030: /* GB18030 */
  153. {
  154. if (m == 1)
  155. {
  156. unsigned char c = (unsigned char) p[0];
  157. if ((c >= 0x90 && c <= 0xe3) || (c >= 0xf8 && c <= 0xfe))
  158. goto incomplete;
  159. }
  160. else /* m == 2 || m == 3 */
  161. {
  162. unsigned char c = (unsigned char) p[0];
  163. if (c >= 0x90 && c <= 0xe3)
  164. {
  165. unsigned char c2 = (unsigned char) p[1];
  166. if (c2 >= 0x30 && c2 <= 0x39)
  167. {
  168. if (m == 2)
  169. goto incomplete;
  170. else /* m == 3 */
  171. {
  172. unsigned char c3 = (unsigned char) p[2];
  173. if (c3 >= 0x81 && c3 <= 0xfe)
  174. goto incomplete;
  175. }
  176. }
  177. }
  178. }
  179. goto invalid;
  180. }
  181. case enc_sjis: /* SJIS */
  182. {
  183. if (m == 1)
  184. {
  185. unsigned char c = (unsigned char) p[0];
  186. if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)
  187. || (c >= 0xf0 && c <= 0xf9))
  188. goto incomplete;
  189. }
  190. goto invalid;
  191. }
  192. default:
  193. /* An unknown multibyte encoding. */
  194. goto incomplete;
  195. }
  196. }
  197. success:
  198. /* res >= 0 is the corrected return value of
  199. mbtowc_with_lock (&wc, p, m). */
  200. if (nstate >= (res > 0 ? res : 1))
  201. abort ();
  202. res -= nstate;
  203. pstate[0] = 0;
  204. return res;
  205. incomplete:
  206. {
  207. size_t k = nstate;
  208. /* Here 0 <= k < m < 4. */
  209. pstate[++k] = s[0];
  210. if (k < m)
  211. {
  212. pstate[++k] = s[1];
  213. if (k < m)
  214. pstate[++k] = s[2];
  215. }
  216. if (k != m)
  217. abort ();
  218. }
  219. pstate[0] = m;
  220. return (size_t)(-2);
  221. invalid:
  222. errno = EILSEQ;
  223. /* The conversion state is undefined, says POSIX. */
  224. return (size_t)(-1);
  225. }