2001-01-25 04:25:05 +03:00
|
|
|
/* $NetBSD: mskanji.c,v 1.6 2001/01/25 01:25:07 itojun Exp $ */
|
2000-12-21 15:17:35 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ja_JP.SJIS locale table for BSD4.4/rune
|
|
|
|
* version 1.0
|
|
|
|
* (C) Sin'ichiro MIYATANI / Phase One, Inc
|
|
|
|
* May 12, 1995
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Phase One, Inc.
|
|
|
|
* 4. The name of Phase One, Inc. may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)mskanji.c 1.0 (Phase One) 5/5/95";
|
|
|
|
#else
|
2001-01-25 04:25:05 +03:00
|
|
|
__RCSID("$NetBSD: mskanji.c,v 1.6 2001/01/25 01:25:07 itojun Exp $");
|
2000-12-21 15:17:35 +03:00
|
|
|
#endif
|
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
#include <assert.h>
|
2000-12-21 15:17:35 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include "rune.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
static int _mskanji1 __P((int));
|
|
|
|
static int _mskanji2 __P((int));
|
2001-01-25 04:25:05 +03:00
|
|
|
const char *_MSKanji_magic __P((void));
|
2000-12-21 15:17:35 +03:00
|
|
|
int _MSKanji_init __P((_RuneLocale *));
|
2000-12-28 08:22:27 +03:00
|
|
|
size_t _MSKanji_mbrtowc __P((struct _RuneLocale *, rune_t *, const char *,
|
|
|
|
size_t, void *));
|
|
|
|
size_t _MSKanji_wcrtomb __P((struct _RuneLocale *, char *, size_t,
|
|
|
|
const rune_t, void *));
|
|
|
|
void _MSKanji_initstate __P((_RuneLocale *, void *));
|
|
|
|
void _MSKanji_packstate __P((_RuneLocale *, mbstate_t *, void *));
|
|
|
|
void _MSKanji_unpackstate __P((_RuneLocale *, void *, const mbstate_t *));
|
|
|
|
|
|
|
|
typedef struct {
|
2000-12-30 08:05:57 +03:00
|
|
|
void *runelocale; /* reserved for future thread-safeness */
|
2000-12-28 08:22:27 +03:00
|
|
|
char ch[2];
|
|
|
|
int chlen;
|
|
|
|
} _MSKanjiState;
|
2000-12-21 15:17:35 +03:00
|
|
|
|
|
|
|
static _RuneState _MSKanji_RuneState = {
|
2000-12-28 08:22:27 +03:00
|
|
|
sizeof(_MSKanjiState), /* sizestate */
|
|
|
|
_MSKanji_initstate, /* initstate */
|
|
|
|
_MSKanji_packstate, /* packstate */
|
|
|
|
_MSKanji_unpackstate /* unpackstate */
|
2000-12-21 15:17:35 +03:00
|
|
|
};
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
static int
|
|
|
|
_mskanji1(c)
|
|
|
|
int c;
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_mskanji2(c)
|
|
|
|
int c;
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-25 04:25:05 +03:00
|
|
|
const char *
|
|
|
|
_MSKanji_magic()
|
|
|
|
{
|
|
|
|
|
|
|
|
return _RUNE_MODULE_1("LC_CTYPE");
|
|
|
|
}
|
|
|
|
|
2000-12-21 15:17:35 +03:00
|
|
|
int
|
|
|
|
_MSKanji_init(rl)
|
|
|
|
_RuneLocale *rl;
|
|
|
|
{
|
2000-12-28 08:22:27 +03:00
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
_DIAGASSERT(rl != NULL);
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
/* sanity check to avoid overruns */
|
|
|
|
if (sizeof(_MSKanjiState) > sizeof(mbstate_t))
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
rl->__rune_mbrtowc = _MSKanji_mbrtowc;
|
|
|
|
rl->__rune_wcrtomb = _MSKanji_wcrtomb;
|
2000-12-21 15:17:35 +03:00
|
|
|
|
|
|
|
rl->__rune_RuneState = &_MSKanji_RuneState;
|
|
|
|
rl->__rune_mb_cur_max = 2;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
/* s is non-null */
|
|
|
|
size_t
|
|
|
|
_MSKanji_mbrtowc(rl, pwcs, s, n, state)
|
2000-12-21 15:17:35 +03:00
|
|
|
_RuneLocale *rl;
|
2000-12-28 08:22:27 +03:00
|
|
|
rune_t *pwcs;
|
|
|
|
const char *s;
|
2000-12-21 15:17:35 +03:00
|
|
|
size_t n;
|
|
|
|
void *state;
|
|
|
|
{
|
2000-12-28 08:22:27 +03:00
|
|
|
_MSKanjiState *ps;
|
|
|
|
rune_t rune;
|
|
|
|
int len;
|
2000-12-21 15:17:35 +03:00
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
/* rl appears to be unused */
|
|
|
|
/* pwcs may be NULL */
|
|
|
|
_DIAGASSERT(s != NULL);
|
|
|
|
_DIAGASSERT(state != NULL);
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
ps = state;
|
|
|
|
|
|
|
|
/* make sure we have the first byte in the buffer */
|
|
|
|
switch (ps->chlen) {
|
|
|
|
case 0:
|
|
|
|
if (n < 1)
|
|
|
|
return (size_t)-2;
|
|
|
|
ps->ch[0] = *s++;
|
|
|
|
ps->chlen = 1;
|
|
|
|
n--;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* illegal state */
|
|
|
|
goto encoding_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = _mskanji1(ps->ch[0] & 0xff) ? 2 : 1;
|
|
|
|
while (ps->chlen < len) {
|
|
|
|
if (n < 1)
|
|
|
|
return (size_t)-2;
|
|
|
|
ps->ch[ps->chlen] = *s++;
|
|
|
|
ps->chlen++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
rune = ps->ch[0] & 0xff;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (!_mskanji2(ps->ch[1] & 0xff))
|
|
|
|
goto encoding_error;
|
|
|
|
rune = ((ps->ch[0] & 0xff) << 8) | (ps->ch[1] & 0xff);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* illegal state */
|
|
|
|
goto encoding_error;
|
2000-12-21 15:17:35 +03:00
|
|
|
}
|
2000-12-28 08:22:27 +03:00
|
|
|
|
|
|
|
ps->chlen = 0;
|
|
|
|
if (pwcs)
|
|
|
|
*pwcs = rune;
|
|
|
|
if (!rune)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return len;
|
|
|
|
|
|
|
|
encoding_error:
|
|
|
|
ps->chlen = 0;
|
|
|
|
return (size_t)-1;
|
2000-12-21 15:17:35 +03:00
|
|
|
}
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
/* s is non-null */
|
|
|
|
size_t
|
|
|
|
_MSKanji_wcrtomb(rl, s, n, wc, state)
|
|
|
|
_RuneLocale *rl;
|
|
|
|
char *s;
|
2000-12-21 15:17:35 +03:00
|
|
|
size_t n;
|
2000-12-28 08:22:27 +03:00
|
|
|
const rune_t wc;
|
|
|
|
void *state;
|
2000-12-21 15:17:35 +03:00
|
|
|
{
|
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
/* rl appears to be unused */
|
|
|
|
_DIAGASSERT(s != NULL);
|
|
|
|
_DIAGASSERT(state != NULL);
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
/* check invalid sequence */
|
|
|
|
if (wc & ~0xffff) {
|
|
|
|
errno = EILSEQ;
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wc & 0xff00) {
|
|
|
|
if (n < 2) {
|
|
|
|
/* bound check failure */
|
|
|
|
errno = EILSEQ; /*XXX*/
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s[0] = (wc >> 8) & 0xff;
|
|
|
|
s[1] = wc & 0xff;
|
|
|
|
if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) {
|
|
|
|
errno = EILSEQ;
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 2;
|
2000-12-21 15:17:35 +03:00
|
|
|
} else {
|
2000-12-28 08:22:27 +03:00
|
|
|
s[0] = wc & 0xff;
|
|
|
|
if (_mskanji1(s[0] & 0xff)) {
|
|
|
|
errno = EILSEQ;
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2000-12-21 15:17:35 +03:00
|
|
|
}
|
2000-12-28 08:22:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_MSKanji_initstate(rl, s)
|
|
|
|
_RuneLocale *rl;
|
|
|
|
void *s;
|
|
|
|
{
|
|
|
|
_MSKanjiState *state;
|
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
/* rl appears to be unused */
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
state = s;
|
|
|
|
memset(state, 0, sizeof(_MSKanjiState));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_MSKanji_packstate(rl, dst, src)
|
|
|
|
_RuneLocale *rl;
|
|
|
|
mbstate_t *dst;
|
|
|
|
void* src;
|
|
|
|
{
|
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
/* rl appears to be unused */
|
|
|
|
_DIAGASSERT(dst != NULL);
|
|
|
|
_DIAGASSERT(src != NULL);
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
memcpy((caddr_t)dst, (caddr_t)src, sizeof(_MSKanjiState));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_MSKanji_unpackstate(rl, dst, src)
|
|
|
|
_RuneLocale *rl;
|
|
|
|
void* dst;
|
|
|
|
const mbstate_t *src;
|
|
|
|
{
|
|
|
|
|
2001-01-03 18:23:26 +03:00
|
|
|
/* rl appears to be unused */
|
|
|
|
_DIAGASSERT(dst != NULL);
|
|
|
|
_DIAGASSERT(src != NULL);
|
|
|
|
|
2000-12-28 08:22:27 +03:00
|
|
|
memcpy((caddr_t)dst, (caddr_t)src, sizeof(_MSKanjiState));
|
|
|
|
return;
|
2000-12-21 15:17:35 +03:00
|
|
|
}
|