2009-03-03 07:20:28 +08:00
|
|
|
#include <errno.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <stdlib.h>
|
2000-08-25 02:51:09 +08:00
|
|
|
#include <string.h>
|
2002-08-23 09:56:05 +08:00
|
|
|
#include <wchar.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <locale.h>
|
|
|
|
#include "mbctype.h"
|
|
|
|
|
2002-09-10 05:42:14 +08:00
|
|
|
/* for some conversions, we use the __count field as a place to store a state value */
|
|
|
|
#define __state __count
|
|
|
|
|
2002-09-21 04:13:11 +08:00
|
|
|
extern char __lc_ctype[12];
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
int
|
|
|
|
_DEFUN (_wctomb_r, (r, s, wchar, state),
|
|
|
|
struct _reent *r _AND
|
|
|
|
char *s _AND
|
2007-05-17 03:31:08 +08:00
|
|
|
wchar_t _wchar _AND
|
2002-08-23 09:56:05 +08:00
|
|
|
mbstate_t *state)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2007-05-17 03:31:08 +08:00
|
|
|
/* Avoids compiler warnings about comparisons that are always false
|
|
|
|
due to limited range when sizeof(wchar_t) is 2 but sizeof(wint_t)
|
|
|
|
is 4, as is the case on cygwin. */
|
|
|
|
wint_t wchar = _wchar;
|
|
|
|
|
2002-09-21 04:13:11 +08:00
|
|
|
if (strlen (__lc_ctype) <= 1)
|
2000-02-18 03:39:52 +08:00
|
|
|
{ /* fall-through */ }
|
2002-09-21 04:13:11 +08:00
|
|
|
else if (!strcmp (__lc_ctype, "C-UTF-8"))
|
2002-04-25 04:53:30 +08:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return 0; /* UTF-8 encoding is not state-dependent */
|
|
|
|
|
2009-02-25 17:10:09 +08:00
|
|
|
if (state->__count == -4 && (wchar < 0xdc00 || wchar >= 0xdfff))
|
|
|
|
{
|
|
|
|
/* At this point only the second half of a surrogate pair is valid. */
|
2009-03-03 07:20:28 +08:00
|
|
|
r->_errno = EILSEQ;
|
2009-02-25 17:10:09 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2002-04-25 04:53:30 +08:00
|
|
|
if (wchar <= 0x7f)
|
|
|
|
{
|
|
|
|
*s = wchar;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (wchar >= 0x80 && wchar <= 0x7ff)
|
|
|
|
{
|
|
|
|
*s++ = 0xc0 | ((wchar & 0x7c0) >> 6);
|
|
|
|
*s = 0x80 | (wchar & 0x3f);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (wchar >= 0x800 && wchar <= 0xffff)
|
|
|
|
{
|
|
|
|
if (wchar >= 0xd800 && wchar <= 0xdfff)
|
2009-02-25 17:10:09 +08:00
|
|
|
{
|
|
|
|
wint_t tmp;
|
|
|
|
/* UTF-16 surrogates -- must not occur in normal UCS-4 data */
|
|
|
|
if (sizeof (wchar_t) != 2)
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-25 17:10:09 +08:00
|
|
|
if (wchar >= 0xdc00)
|
|
|
|
{
|
|
|
|
/* Second half of a surrogate pair. It's not valid if
|
|
|
|
we don't have already read a first half of a surrogate
|
|
|
|
before. */
|
|
|
|
if (state->__count != -4)
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-25 17:10:09 +08:00
|
|
|
/* If it's valid, reconstruct the full Unicode value and
|
|
|
|
return the trailing three bytes of the UTF-8 char. */
|
|
|
|
tmp = (state->__value.__wchb[0] << 16)
|
|
|
|
| (state->__value.__wchb[1] << 8)
|
|
|
|
| (wchar & 0x3ff);
|
|
|
|
state->__count = 0;
|
|
|
|
*s++ = 0x80 | ((tmp & 0x3f000) >> 12);
|
|
|
|
*s++ = 0x80 | ((tmp & 0xfc0) >> 6);
|
|
|
|
*s = 0x80 | (tmp & 0x3f);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
/* First half of a surrogate pair. Store the state and return
|
|
|
|
the first byte of the UTF-8 char. */
|
|
|
|
tmp = ((wchar & 0x3ff) << 10) + 0x10000;
|
|
|
|
state->__value.__wchb[0] = (tmp >> 16) & 0xff;
|
|
|
|
state->__value.__wchb[1] = (tmp >> 8) & 0xff;
|
|
|
|
state->__count = -4;
|
|
|
|
*s = (0xf0 | ((tmp & 0x1c0000) >> 18));
|
|
|
|
return 1;
|
|
|
|
}
|
2002-04-25 04:53:30 +08:00
|
|
|
*s++ = 0xe0 | ((wchar & 0xf000) >> 12);
|
|
|
|
*s++ = 0x80 | ((wchar & 0xfc0) >> 6);
|
|
|
|
*s = 0x80 | (wchar & 0x3f);
|
|
|
|
return 3;
|
|
|
|
}
|
2009-02-25 17:10:09 +08:00
|
|
|
else if (wchar >= 0x10000 && wchar <= 0x10ffff)
|
2002-04-25 04:53:30 +08:00
|
|
|
{
|
|
|
|
*s++ = 0xf0 | ((wchar & 0x1c0000) >> 18);
|
|
|
|
*s++ = 0x80 | ((wchar & 0x3f000) >> 12);
|
|
|
|
*s++ = 0x80 | ((wchar & 0xfc0) >> 6);
|
|
|
|
*s = 0x80 | (wchar & 0x3f);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
else
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2002-04-25 04:53:30 +08:00
|
|
|
}
|
2002-09-21 04:13:11 +08:00
|
|
|
else if (!strcmp (__lc_ctype, "C-SJIS"))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
unsigned char char2 = (unsigned char)wchar;
|
|
|
|
unsigned char char1 = (unsigned char)(wchar >> 8);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 0; /* not state-dependent */
|
|
|
|
|
|
|
|
if (char1 != 0x00)
|
|
|
|
{
|
|
|
|
/* first byte is non-zero..validate multi-byte char */
|
|
|
|
if (_issjis1(char1) && _issjis2(char2))
|
|
|
|
{
|
|
|
|
*s++ = (char)char1;
|
|
|
|
*s = (char)char2;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
}
|
2002-09-21 04:13:11 +08:00
|
|
|
else if (!strcmp (__lc_ctype, "C-EUCJP"))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
unsigned char char2 = (unsigned char)wchar;
|
|
|
|
unsigned char char1 = (unsigned char)(wchar >> 8);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 0; /* not state-dependent */
|
|
|
|
|
|
|
|
if (char1 != 0x00)
|
|
|
|
{
|
|
|
|
/* first byte is non-zero..validate multi-byte char */
|
|
|
|
if (_iseucjp (char1) && _iseucjp (char2))
|
|
|
|
{
|
|
|
|
*s++ = (char)char1;
|
|
|
|
*s = (char)char2;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
}
|
2002-09-21 04:13:11 +08:00
|
|
|
else if (!strcmp (__lc_ctype, "C-JIS"))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
unsigned char char2 = (unsigned char)wchar;
|
|
|
|
unsigned char char1 = (unsigned char)(wchar >> 8);
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 1; /* state-dependent */
|
|
|
|
|
|
|
|
if (char1 != 0x00)
|
|
|
|
{
|
|
|
|
/* first byte is non-zero..validate multi-byte char */
|
|
|
|
if (_isjis (char1) && _isjis (char2))
|
|
|
|
{
|
2002-09-10 05:42:14 +08:00
|
|
|
if (state->__state == 0)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
/* must switch from ASCII to JIS state */
|
2002-09-10 05:42:14 +08:00
|
|
|
state->__state = 1;
|
2000-02-18 03:39:52 +08:00
|
|
|
*s++ = ESC_CHAR;
|
|
|
|
*s++ = '$';
|
|
|
|
*s++ = 'B';
|
|
|
|
cnt = 3;
|
|
|
|
}
|
|
|
|
*s++ = (char)char1;
|
|
|
|
*s = (char)char2;
|
|
|
|
return cnt + 2;
|
|
|
|
}
|
|
|
|
else
|
2009-03-03 07:20:28 +08:00
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-09-10 05:42:14 +08:00
|
|
|
if (state->__state != 0)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
/* must switch from JIS to ASCII state */
|
2002-09-10 05:42:14 +08:00
|
|
|
state->__state = 0;
|
2000-02-18 03:39:52 +08:00
|
|
|
*s++ = ESC_CHAR;
|
|
|
|
*s++ = '(';
|
|
|
|
*s++ = 'B';
|
|
|
|
cnt = 3;
|
|
|
|
}
|
|
|
|
*s = (char)char2;
|
|
|
|
return cnt + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* otherwise we are dealing with a single byte character */
|
2009-03-03 07:20:28 +08:00
|
|
|
if (wchar >= 0x100)
|
|
|
|
{
|
|
|
|
r->_errno = EILSEQ;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
*s = (char) wchar;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|