#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)mskanji.c 1.0 (Phase One) 5/5/95";
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/lib/libc/locale/mskanji.c,v 1.16 2004/05/14 15:40:47 tjr Exp $");
#include "xlocale_private.h"
#include <sys/param.h>
#include <errno.h>
#include <runetype.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "mblocal.h"
__private_extern__ int _MSKanji_init(struct __xlocale_st_runelocale *);
static size_t _MSKanji_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
mbstate_t * __restrict, locale_t);
static int _MSKanji_mbsinit(const mbstate_t *, locale_t);
static size_t _MSKanji_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict, locale_t);
typedef struct {
wchar_t ch;
} _MSKanjiState;
__private_extern__ int
_MSKanji_init(struct __xlocale_st_runelocale *xrl)
{
xrl->__mbrtowc = _MSKanji_mbrtowc;
xrl->__wcrtomb = _MSKanji_wcrtomb;
xrl->__mbsinit = _MSKanji_mbsinit;
xrl->__mb_cur_max = 2;
return (0);
}
static int
_MSKanji_mbsinit(const mbstate_t *ps, locale_t loc)
{
return (ps == NULL || ((const _MSKanjiState *)ps)->ch == 0);
}
static size_t
_MSKanji_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
mbstate_t * __restrict ps, locale_t loc)
{
_MSKanjiState *ms;
wchar_t wc;
ms = (_MSKanjiState *)ps;
if ((ms->ch & ~0xFF) != 0) {
errno = EINVAL;
return ((size_t)-1);
}
if (s == NULL) {
s = "";
n = 1;
pwc = NULL;
}
if (n == 0)
return ((size_t)-2);
if (ms->ch != 0) {
if (*s == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = (ms->ch << 8) | (*s & 0xFF);
if (pwc != NULL)
*pwc = wc;
ms->ch = 0;
return (1);
}
wc = *s++ & 0xff;
if ((wc > 0x80 && wc < 0xa0) || (wc >= 0xe0 && wc < 0xfd)) {
if (n < 2) {
ms->ch = wc;
return ((size_t)-2);
}
if (*s == '\0') {
errno = EILSEQ;
return ((size_t)-1);
}
wc = (wc << 8) | (*s++ & 0xff);
if (pwc != NULL)
*pwc = wc;
return (2);
} else {
if (pwc != NULL)
*pwc = wc;
return (wc == L'\0' ? 0 : 1);
}
}
static size_t
_MSKanji_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps, locale_t loc)
{
_MSKanjiState *ms;
int len, i;
ms = (_MSKanjiState *)ps;
if (ms->ch != 0) {
errno = EINVAL;
return ((size_t)-1);
}
if (s == NULL)
return (1);
len = (wc > 0x100) ? 2 : 1;
for (i = len; i-- > 0; )
*s++ = wc >> (i << 3);
return (len);
}