2N/A/* Convert multibyte character to wide character.
2N/A Copyright (C) 1999-2002, 2005-2010 Free Software Foundation, Inc.
2N/A Written by Bruno Haible <bruno@clisp.org>, 2008.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Specification. */
2N/A#include <wchar.h>
2N/A
2N/A#if GNULIB_defined_mbstate_t
2N/A/* Implement mbrtowc() on top of mbtowc(). */
2N/A
2N/A# include <errno.h>
2N/A# include <stdlib.h>
2N/A
2N/A# include "localcharset.h"
2N/A# include "streq.h"
2N/A# include "verify.h"
2N/A
2N/A
2N/Averify (sizeof (mbstate_t) >= 4);
2N/A
2N/Astatic char internal_state[4];
2N/A
2N/Asize_t
2N/Ambrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
2N/A{
2N/A char *pstate = (char *)ps;
2N/A
2N/A if (pstate == NULL)
2N/A pstate = internal_state;
2N/A
2N/A if (s == NULL)
2N/A {
2N/A pwc = NULL;
2N/A s = "";
2N/A n = 1;
2N/A }
2N/A
2N/A if (n == 0)
2N/A return (size_t)(-2);
2N/A
2N/A /* Here n > 0. */
2N/A {
2N/A size_t nstate = pstate[0];
2N/A char buf[4];
2N/A const char *p;
2N/A size_t m;
2N/A
2N/A switch (nstate)
2N/A {
2N/A case 0:
2N/A p = s;
2N/A m = n;
2N/A break;
2N/A case 3:
2N/A buf[2] = pstate[3];
2N/A /*FALLTHROUGH*/
2N/A case 2:
2N/A buf[1] = pstate[2];
2N/A /*FALLTHROUGH*/
2N/A case 1:
2N/A buf[0] = pstate[1];
2N/A p = buf;
2N/A m = nstate;
2N/A buf[m++] = s[0];
2N/A if (n >= 2 && m < 4)
2N/A {
2N/A buf[m++] = s[1];
2N/A if (n >= 3 && m < 4)
2N/A buf[m++] = s[2];
2N/A }
2N/A break;
2N/A default:
2N/A errno = EINVAL;
2N/A return (size_t)(-1);
2N/A }
2N/A
2N/A /* Here m > 0. */
2N/A
2N/A# if __GLIBC__
2N/A /* Work around bug <http://sourceware.org/bugzilla/show_bug.cgi?id=9674> */
2N/A mbtowc (NULL, NULL, 0);
2N/A# endif
2N/A {
2N/A int res = mbtowc (pwc, p, m);
2N/A
2N/A if (res >= 0)
2N/A {
2N/A if (pwc != NULL && ((*pwc == 0) != (res == 0)))
2N/A abort ();
2N/A if (nstate >= (res > 0 ? res : 1))
2N/A abort ();
2N/A res -= nstate;
2N/A pstate[0] = 0;
2N/A return res;
2N/A }
2N/A
2N/A /* mbtowc does not distinguish between invalid and incomplete multibyte
2N/A sequences. But mbrtowc needs to make this distinction.
2N/A There are two possible approaches:
2N/A - Use iconv() and its return value.
2N/A - Use built-in knowledge about the possible encodings.
2N/A Given the low quality of implementation of iconv() on the systems that
2N/A lack mbrtowc(), we use the second approach.
2N/A The possible encodings are:
2N/A - 8-bit encodings,
2N/A - EUC-JP, EUC-KR, GB2312, EUC-TW, BIG5, GB18030, SJIS,
2N/A - UTF-8.
2N/A Use specialized code for each. */
2N/A if (m >= 4 || m >= MB_CUR_MAX)
2N/A goto invalid;
2N/A /* Here MB_CUR_MAX > 1 and 0 < m < 4. */
2N/A {
2N/A const char *encoding = locale_charset ();
2N/A
2N/A if (STREQ (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
2N/A {
2N/A /* Cf. unistr/u8-mblen.c. */
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if (c >= 0xc2)
2N/A {
2N/A if (c < 0xe0)
2N/A {
2N/A if (m == 1)
2N/A goto incomplete;
2N/A }
2N/A else if (c < 0xf0)
2N/A {
2N/A if (m == 1)
2N/A goto incomplete;
2N/A if (m == 2)
2N/A {
2N/A unsigned char c2 = (unsigned char) p[1];
2N/A
2N/A if ((c2 ^ 0x80) < 0x40
2N/A && (c >= 0xe1 || c2 >= 0xa0)
2N/A && (c != 0xed || c2 < 0xa0))
2N/A goto incomplete;
2N/A }
2N/A }
2N/A else if (c <= 0xf4)
2N/A {
2N/A if (m == 1)
2N/A goto incomplete;
2N/A else /* m == 2 || m == 3 */
2N/A {
2N/A unsigned char c2 = (unsigned char) p[1];
2N/A
2N/A if ((c2 ^ 0x80) < 0x40
2N/A && (c >= 0xf1 || c2 >= 0x90)
2N/A && (c < 0xf4 || (c == 0xf4 && c2 < 0x90)))
2N/A {
2N/A if (m == 2)
2N/A goto incomplete;
2N/A else /* m == 3 */
2N/A {
2N/A unsigned char c3 = (unsigned char) p[2];
2N/A
2N/A if ((c3 ^ 0x80) < 0x40)
2N/A goto incomplete;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A }
2N/A goto invalid;
2N/A }
2N/A
2N/A /* As a reference for this code, you can use the GNU libiconv
2N/A implementation. Look for uses of the RET_TOOFEW macro. */
2N/A
2N/A if (STREQ (encoding, "EUC-JP", 'E', 'U', 'C', '-', 'J', 'P', 0, 0, 0))
2N/A {
2N/A if (m == 1)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if ((c >= 0xa1 && c < 0xff) || c == 0x8e || c == 0x8f)
2N/A goto incomplete;
2N/A }
2N/A if (m == 2)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if (c == 0x8f)
2N/A {
2N/A unsigned char c2 = (unsigned char) p[1];
2N/A
2N/A if (c2 >= 0xa1 && c2 < 0xff)
2N/A goto incomplete;
2N/A }
2N/A }
2N/A goto invalid;
2N/A }
2N/A if (STREQ (encoding, "EUC-KR", 'E', 'U', 'C', '-', 'K', 'R', 0, 0, 0)
2N/A || STREQ (encoding, "GB2312", 'G', 'B', '2', '3', '1', '2', 0, 0, 0)
2N/A || STREQ (encoding, "BIG5", 'B', 'I', 'G', '5', 0, 0, 0, 0, 0))
2N/A {
2N/A if (m == 1)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if (c >= 0xa1 && c < 0xff)
2N/A goto incomplete;
2N/A }
2N/A goto invalid;
2N/A }
2N/A if (STREQ (encoding, "EUC-TW", 'E', 'U', 'C', '-', 'T', 'W', 0, 0, 0))
2N/A {
2N/A if (m == 1)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if ((c >= 0xa1 && c < 0xff) || c == 0x8e)
2N/A goto incomplete;
2N/A }
2N/A else /* m == 2 || m == 3 */
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if (c == 0x8e)
2N/A goto incomplete;
2N/A }
2N/A goto invalid;
2N/A }
2N/A if (STREQ (encoding, "GB18030", 'G', 'B', '1', '8', '0', '3', '0', 0, 0))
2N/A {
2N/A if (m == 1)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if ((c >= 0x90 && c <= 0xe3) || (c >= 0xf8 && c <= 0xfe))
2N/A goto incomplete;
2N/A }
2N/A else /* m == 2 || m == 3 */
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if (c >= 0x90 && c <= 0xe3)
2N/A {
2N/A unsigned char c2 = (unsigned char) p[1];
2N/A
2N/A if (c2 >= 0x30 && c2 <= 0x39)
2N/A {
2N/A if (m == 2)
2N/A goto incomplete;
2N/A else /* m == 3 */
2N/A {
2N/A unsigned char c3 = (unsigned char) p[2];
2N/A
2N/A if (c3 >= 0x81 && c3 <= 0xfe)
2N/A goto incomplete;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A goto invalid;
2N/A }
2N/A if (STREQ (encoding, "SJIS", 'S', 'J', 'I', 'S', 0, 0, 0, 0, 0))
2N/A {
2N/A if (m == 1)
2N/A {
2N/A unsigned char c = (unsigned char) p[0];
2N/A
2N/A if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)
2N/A || (c >= 0xf0 && c <= 0xf9))
2N/A goto incomplete;
2N/A }
2N/A goto invalid;
2N/A }
2N/A
2N/A /* An unknown multibyte encoding. */
2N/A goto incomplete;
2N/A }
2N/A
2N/A incomplete:
2N/A {
2N/A size_t k = nstate;
2N/A /* Here 0 <= k < m < 4. */
2N/A pstate[++k] = s[0];
2N/A if (k < m)
2N/A {
2N/A pstate[++k] = s[1];
2N/A if (k < m)
2N/A pstate[++k] = s[2];
2N/A }
2N/A if (k != m)
2N/A abort ();
2N/A }
2N/A pstate[0] = m;
2N/A return (size_t)(-2);
2N/A
2N/A invalid:
2N/A errno = EILSEQ;
2N/A /* The conversion state is undefined, says POSIX. */
2N/A return (size_t)(-1);
2N/A }
2N/A }
2N/A}
2N/A
2N/A#else
2N/A/* Override the system's mbrtowc() function. */
2N/A
2N/A# undef mbrtowc
2N/A
2N/Asize_t
2N/Arpl_mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
2N/A{
2N/A# if MBRTOWC_NULL_ARG_BUG || MBRTOWC_RETVAL_BUG
2N/A if (s == NULL)
2N/A {
2N/A pwc = NULL;
2N/A s = "";
2N/A n = 1;
2N/A }
2N/A# endif
2N/A
2N/A# if MBRTOWC_RETVAL_BUG
2N/A {
2N/A static mbstate_t internal_state;
2N/A
2N/A /* Override mbrtowc's internal state. We can not call mbsinit() on the
2N/A hidden internal state, but we can call it on our variable. */
2N/A if (ps == NULL)
2N/A ps = &internal_state;
2N/A
2N/A if (!mbsinit (ps))
2N/A {
2N/A /* Parse the rest of the multibyte character byte for byte. */
2N/A size_t count = 0;
2N/A for (; n > 0; s++, n--)
2N/A {
2N/A wchar_t wc;
2N/A size_t ret = mbrtowc (&wc, s, 1, ps);
2N/A
2N/A if (ret == (size_t)(-1))
2N/A return (size_t)(-1);
2N/A count++;
2N/A if (ret != (size_t)(-2))
2N/A {
2N/A /* The multibyte character has been completed. */
2N/A if (pwc != NULL)
2N/A *pwc = wc;
2N/A return (wc == 0 ? 0 : count);
2N/A }
2N/A }
2N/A return (size_t)(-2);
2N/A }
2N/A }
2N/A# endif
2N/A
2N/A# if MBRTOWC_NUL_RETVAL_BUG
2N/A {
2N/A wchar_t wc;
2N/A size_t ret = mbrtowc (&wc, s, n, ps);
2N/A
2N/A if (ret != (size_t)(-1) && ret != (size_t)(-2))
2N/A {
2N/A if (pwc != NULL)
2N/A *pwc = wc;
2N/A if (wc == 0)
2N/A ret = 0;
2N/A }
2N/A return ret;
2N/A }
2N/A# else
2N/A return mbrtowc (pwc, s, n, ps);
2N/A# endif
2N/A}
2N/A
2N/A#endif