xref: /titanic_51/usr/src/lib/libc/port/locale/mskanji.c (revision 9a4a12bd7ce60cd60eae508b25eb7a8dae765274)
14297a3b0SGarrett D'Amore /*
22d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
54297a3b0SGarrett D'Amore  *
64297a3b0SGarrett D'Amore  *    ja_JP.SJIS locale table for BSD4.4/rune
74297a3b0SGarrett D'Amore  *    version 1.0
84297a3b0SGarrett D'Amore  *    (C) Sin'ichiro MIYATANI / Phase One, Inc
94297a3b0SGarrett D'Amore  *    May 12, 1995
104297a3b0SGarrett D'Amore  *
114297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
124297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
134297a3b0SGarrett D'Amore  * are met:
144297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
154297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
164297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
174297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
184297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
194297a3b0SGarrett D'Amore  * 3. All advertising materials mentioning features or use of this software
204297a3b0SGarrett D'Amore  *    must display the following acknowledgement:
214297a3b0SGarrett D'Amore  *      This product includes software developed by Phase One, Inc.
224297a3b0SGarrett D'Amore  * 4. The name of Phase One, Inc. may be used to endorse or promote products
234297a3b0SGarrett D'Amore  *    derived from this software without specific prior written permission.
244297a3b0SGarrett D'Amore  *
254297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354297a3b0SGarrett D'Amore  * SUCH DAMAGE.
364297a3b0SGarrett D'Amore  */
374297a3b0SGarrett D'Amore 
384297a3b0SGarrett D'Amore #include "lint.h"
394297a3b0SGarrett D'Amore #include <sys/types.h>
404297a3b0SGarrett D'Amore #include <errno.h>
414297a3b0SGarrett D'Amore #include <stdlib.h>
424297a3b0SGarrett D'Amore #include <string.h>
434297a3b0SGarrett D'Amore #include <wchar.h>
444297a3b0SGarrett D'Amore #include "mblocal.h"
452d08521bSGarrett D'Amore #include "lctype.h"
464297a3b0SGarrett D'Amore 
474297a3b0SGarrett D'Amore static size_t	_MSKanji_mbrtowc(wchar_t *_RESTRICT_KYWD,
484297a3b0SGarrett D'Amore 		    const char *_RESTRICT_KYWD,
49*d8e0a9a1SRobert Mustacchi 		    size_t, mbstate_t *_RESTRICT_KYWD, boolean_t);
504297a3b0SGarrett D'Amore static int	_MSKanji_mbsinit(const mbstate_t *);
514297a3b0SGarrett D'Amore static size_t	_MSKanji_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
524297a3b0SGarrett D'Amore 		    mbstate_t *_RESTRICT_KYWD);
532d08521bSGarrett D'Amore static size_t	_MSKanji_mbsnrtowcs(wchar_t *_RESTRICT_KYWD,
542d08521bSGarrett D'Amore 		    const char **_RESTRICT_KYWD, size_t, size_t,
552d08521bSGarrett D'Amore 		    mbstate_t *_RESTRICT_KYWD);
562d08521bSGarrett D'Amore static size_t	_MSKanji_wcsnrtombs(char *_RESTRICT_KYWD,
572d08521bSGarrett D'Amore 		    const wchar_t **_RESTRICT_KYWD, size_t, size_t,
582d08521bSGarrett D'Amore 		    mbstate_t *_RESTRICT_KYWD);
594297a3b0SGarrett D'Amore 
602d08521bSGarrett D'Amore void
612d08521bSGarrett D'Amore _MSKanji_init(struct lc_ctype *lct)
624297a3b0SGarrett D'Amore {
634297a3b0SGarrett D'Amore 
642d08521bSGarrett D'Amore 	lct->lc_mbrtowc = _MSKanji_mbrtowc;
652d08521bSGarrett D'Amore 	lct->lc_wcrtomb = _MSKanji_wcrtomb;
662d08521bSGarrett D'Amore 	lct->lc_mbsnrtowcs = _MSKanji_mbsnrtowcs;
672d08521bSGarrett D'Amore 	lct->lc_wcsnrtombs = _MSKanji_wcsnrtombs;
682d08521bSGarrett D'Amore 	lct->lc_mbsinit = _MSKanji_mbsinit;
692d08521bSGarrett D'Amore 	lct->lc_max_mblen = 2;
702d08521bSGarrett D'Amore 	lct->lc_is_ascii = 0;
714297a3b0SGarrett D'Amore }
724297a3b0SGarrett D'Amore 
734297a3b0SGarrett D'Amore static int
744297a3b0SGarrett D'Amore _MSKanji_mbsinit(const mbstate_t *ps)
754297a3b0SGarrett D'Amore {
764297a3b0SGarrett D'Amore 
774297a3b0SGarrett D'Amore 	return (ps == NULL || ((const _MSKanjiState *)ps)->ch == 0);
784297a3b0SGarrett D'Amore }
794297a3b0SGarrett D'Amore 
804297a3b0SGarrett D'Amore static size_t
814297a3b0SGarrett D'Amore _MSKanji_mbrtowc(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
82*d8e0a9a1SRobert Mustacchi     size_t n, mbstate_t *_RESTRICT_KYWD ps, boolean_t zero)
834297a3b0SGarrett D'Amore {
844297a3b0SGarrett D'Amore 	_MSKanjiState *ms;
854297a3b0SGarrett D'Amore 	wchar_t wc;
864297a3b0SGarrett D'Amore 
874297a3b0SGarrett D'Amore 	ms = (_MSKanjiState *)ps;
884297a3b0SGarrett D'Amore 
894297a3b0SGarrett D'Amore 	if ((ms->ch & ~0xFF) != 0) {
904297a3b0SGarrett D'Amore 		/* Bad conversion state. */
914297a3b0SGarrett D'Amore 		errno = EINVAL;
924297a3b0SGarrett D'Amore 		return ((size_t)-1);
934297a3b0SGarrett D'Amore 	}
944297a3b0SGarrett D'Amore 
954297a3b0SGarrett D'Amore 	if (s == NULL) {
964297a3b0SGarrett D'Amore 		s = "";
974297a3b0SGarrett D'Amore 		n = 1;
984297a3b0SGarrett D'Amore 		pwc = NULL;
994297a3b0SGarrett D'Amore 	}
1004297a3b0SGarrett D'Amore 
1014297a3b0SGarrett D'Amore 	if (n == 0)
1024297a3b0SGarrett D'Amore 		/* Incomplete multibyte sequence */
1034297a3b0SGarrett D'Amore 		return ((size_t)-2);
1044297a3b0SGarrett D'Amore 
1054297a3b0SGarrett D'Amore 	if (ms->ch != 0) {
1064297a3b0SGarrett D'Amore 		if (*s == '\0') {
1074297a3b0SGarrett D'Amore 			errno = EILSEQ;
1084297a3b0SGarrett D'Amore 			return ((size_t)-1);
1094297a3b0SGarrett D'Amore 		}
1104297a3b0SGarrett D'Amore 		wc = (ms->ch << 8) | (*s & 0xFF);
1114297a3b0SGarrett D'Amore 		if (pwc != NULL)
1124297a3b0SGarrett D'Amore 			*pwc = wc;
1134297a3b0SGarrett D'Amore 		ms->ch = 0;
1144297a3b0SGarrett D'Amore 		return (1);
1154297a3b0SGarrett D'Amore 	}
1164297a3b0SGarrett D'Amore 	wc = *s++ & 0xff;
1174297a3b0SGarrett D'Amore 	if ((wc > 0x80 && wc < 0xa0) || (wc >= 0xe0 && wc < 0xfd)) {
1184297a3b0SGarrett D'Amore 		if (n < 2) {
1194297a3b0SGarrett D'Amore 			/* Incomplete multibyte sequence */
1204297a3b0SGarrett D'Amore 			ms->ch = wc;
1214297a3b0SGarrett D'Amore 			return ((size_t)-2);
1224297a3b0SGarrett D'Amore 		}
1234297a3b0SGarrett D'Amore 		if (*s == '\0') {
1244297a3b0SGarrett D'Amore 			errno = EILSEQ;
1254297a3b0SGarrett D'Amore 			return ((size_t)-1);
1264297a3b0SGarrett D'Amore 		}
1274297a3b0SGarrett D'Amore 		wc = (wc << 8) | (*s++ & 0xff);
1284297a3b0SGarrett D'Amore 		if (pwc != NULL)
1294297a3b0SGarrett D'Amore 			*pwc = wc;
1304297a3b0SGarrett D'Amore 		return (2);
1314297a3b0SGarrett D'Amore 	} else {
1324297a3b0SGarrett D'Amore 		if (pwc != NULL)
1334297a3b0SGarrett D'Amore 			*pwc = wc;
134*d8e0a9a1SRobert Mustacchi 		if (zero || wc != L'\0') {
135*d8e0a9a1SRobert Mustacchi 			return (1);
136*d8e0a9a1SRobert Mustacchi 		} else {
137*d8e0a9a1SRobert Mustacchi 			return (0);
138*d8e0a9a1SRobert Mustacchi 		}
1394297a3b0SGarrett D'Amore 	}
1404297a3b0SGarrett D'Amore }
1414297a3b0SGarrett D'Amore 
1424297a3b0SGarrett D'Amore static size_t
1434297a3b0SGarrett D'Amore _MSKanji_wcrtomb(char *_RESTRICT_KYWD s, wchar_t wc,
1444297a3b0SGarrett D'Amore     mbstate_t *_RESTRICT_KYWD ps)
1454297a3b0SGarrett D'Amore {
1464297a3b0SGarrett D'Amore 	_MSKanjiState *ms;
1474297a3b0SGarrett D'Amore 	int len, i;
1484297a3b0SGarrett D'Amore 
1494297a3b0SGarrett D'Amore 	ms = (_MSKanjiState *)ps;
1504297a3b0SGarrett D'Amore 
1514297a3b0SGarrett D'Amore 	if (ms->ch != 0) {
1524297a3b0SGarrett D'Amore 		errno = EINVAL;
1534297a3b0SGarrett D'Amore 		return ((size_t)-1);
1544297a3b0SGarrett D'Amore 	}
1554297a3b0SGarrett D'Amore 
1564297a3b0SGarrett D'Amore 	if (s == NULL)
1574297a3b0SGarrett D'Amore 		/* Reset to initial shift state (no-op) */
1584297a3b0SGarrett D'Amore 		return (1);
1594297a3b0SGarrett D'Amore 	len = (wc > 0x100) ? 2 : 1;
1604297a3b0SGarrett D'Amore 	for (i = len; i-- > 0; )
1614297a3b0SGarrett D'Amore 		*s++ = wc >> (i << 3);
1624297a3b0SGarrett D'Amore 	return (len);
1634297a3b0SGarrett D'Amore }
1642d08521bSGarrett D'Amore 
1652d08521bSGarrett D'Amore static size_t
1662d08521bSGarrett D'Amore _MSKanji_mbsnrtowcs(wchar_t *_RESTRICT_KYWD dst,
1672d08521bSGarrett D'Amore     const char **_RESTRICT_KYWD src, size_t nms,
1682d08521bSGarrett D'Amore     size_t len, mbstate_t *_RESTRICT_KYWD ps)
1692d08521bSGarrett D'Amore {
1702d08521bSGarrett D'Amore 	return (__mbsnrtowcs_std(dst, src, nms, len, ps, _MSKanji_mbrtowc));
1712d08521bSGarrett D'Amore }
1722d08521bSGarrett D'Amore 
1732d08521bSGarrett D'Amore static size_t
1742d08521bSGarrett D'Amore _MSKanji_wcsnrtombs(char *_RESTRICT_KYWD dst,
1752d08521bSGarrett D'Amore     const wchar_t **_RESTRICT_KYWD src, size_t nwc,
1762d08521bSGarrett D'Amore     size_t len, mbstate_t *_RESTRICT_KYWD ps)
1772d08521bSGarrett D'Amore {
1782d08521bSGarrett D'Amore 	return (__wcsnrtombs_std(dst, src, nwc, len, ps, _MSKanji_wcrtomb));
1792d08521bSGarrett D'Amore }
180