/*
+ * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
+ *
* ja_JP.SJIS locale table for BSD4.4/rune
* version 1.0
* (C) Sin'ichiro MIYATANI / Phase One, Inc
static char sccsid[] = "@(#)mskanji.c 1.0 (Phase One) 5/5/95";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/locale/mskanji.c,v 1.8 2002/10/14 01:50:45 tjr Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/locale/mskanji.c,v 1.18 2007/10/13 16:28:22 ache Exp $");
-#include <sys/types.h>
+#include "xlocale_private.h"
-#include <rune.h>
-#include <stddef.h>
-#include <stdio.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <runetype.h>
#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+#include "mblocal.h"
-rune_t _MSKanji_sgetrune(const char *, size_t, char const **);
-int _MSKanji_sputrune(rune_t, char *, size_t, char **);
+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);
-int
-_MSKanji_init(rl)
- _RuneLocale *rl;
+typedef struct {
+ wchar_t ch;
+} _MSKanjiState;
+
+__private_extern__ int
+_MSKanji_init(struct __xlocale_st_runelocale *xrl)
{
- rl->sgetrune = _MSKanji_sgetrune;
- rl->sputrune = _MSKanji_sputrune;
- _CurrentRuneLocale = rl;
- __mb_cur_max = 2;
+ xrl->__mbrtowc = _MSKanji_mbrtowc;
+ xrl->__wcrtomb = _MSKanji_wcrtomb;
+ xrl->__mbsinit = _MSKanji_mbsinit;
+ xrl->__mb_cur_max = 2;
+ xrl->__mb_sb_limit = 256;
return (0);
}
-rune_t
-_MSKanji_sgetrune(string, n, result)
- const char *string;
- size_t n;
- char const **result;
+static int
+_MSKanji_mbsinit(const mbstate_t *ps, locale_t loc __unused)
{
- rune_t rune = 0;
- if (n < 1) {
- if (result != NULL)
- *result = string;
- return (_INVALID_RUNE);
+ 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 __unused)
+{
+ _MSKanjiState *ms;
+ wchar_t wc;
+
+ ms = (_MSKanjiState *)ps;
+
+ if ((ms->ch & ~0xFF) != 0) {
+ /* Bad conversion state. */
+ errno = EINVAL;
+ return ((size_t)-1);
}
- rune = *string++ & 0xff;
- if ((rune > 0x80 && rune < 0xa0) ||
- (rune >= 0xe0 && rune < 0xfd)) {
- if (n < 2) {
- rune = _INVALID_RUNE;
- --string;
- } else
- rune = (rune << 8) | (*string++ & 0xff);
+ if (s == NULL) {
+ s = "";
+ n = 1;
+ pwc = NULL;
}
- if (result != NULL)
- *result = string;
- return (rune);
+ if (n == 0)
+ /* Incomplete multibyte sequence */
+ 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) {
+ /* Incomplete multibyte sequence */
+ 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);
+ }
}
-int
-_MSKanji_sputrune(c, string, n, result)
- rune_t c;
- char *string, **result;
- size_t n;
+static size_t
+_MSKanji_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps, locale_t loc __unused)
{
+ _MSKanjiState *ms;
int len, i;
- len = (c > 0x100) ? 2 : 1;
- if (n < len) {
- if (result != NULL)
- *result = NULL;
- } else {
- if (result != NULL)
- *result = string + len;
- for (i = len; i-- > 0; )
- *string++ = c >> (i << 3);
+ ms = (_MSKanjiState *)ps;
+
+ if (ms->ch != 0) {
+ errno = EINVAL;
+ return ((size_t)-1);
}
+ if (s == NULL)
+ /* Reset to initial shift state (no-op) */
+ return (1);
+ len = (wc > 0x100) ? 2 : 1;
+ for (i = len; i-- > 0; )
+ *s++ = wc >> (i << 3);
return (len);
}