10d5acd74SJohn Marino /*
2*4776d4e8SJohn Marino * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3*4776d4e8SJohn Marino * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
40d5acd74SJohn Marino * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
50d5acd74SJohn Marino *
60d5acd74SJohn Marino * ja_JP.SJIS locale table for BSD4.4/rune
70d5acd74SJohn Marino * version 1.0
80d5acd74SJohn Marino * (C) Sin'ichiro MIYATANI / Phase One, Inc
90d5acd74SJohn Marino * May 12, 1995
100d5acd74SJohn Marino *
110d5acd74SJohn Marino * Copyright (c) 2011 The FreeBSD Foundation
120d5acd74SJohn Marino * All rights reserved.
130d5acd74SJohn Marino * Portions of this software were developed by David Chisnall
140d5acd74SJohn Marino * under sponsorship from the FreeBSD Foundation.
150d5acd74SJohn Marino *
160d5acd74SJohn Marino * Redistribution and use in source and binary forms, with or without
170d5acd74SJohn Marino * modification, are permitted provided that the following conditions
180d5acd74SJohn Marino * are met:
190d5acd74SJohn Marino * 1. Redistributions of source code must retain the above copyright
200d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer.
210d5acd74SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
220d5acd74SJohn Marino * notice, this list of conditions and the following disclaimer in the
230d5acd74SJohn Marino * documentation and/or other materials provided with the distribution.
240d5acd74SJohn Marino * 3. All advertising materials mentioning features or use of this software
250d5acd74SJohn Marino * must display the following acknowledgement:
260d5acd74SJohn Marino * This product includes software developed by Phase One, Inc.
270d5acd74SJohn Marino * 4. The name of Phase One, Inc. may be used to endorse or promote products
280d5acd74SJohn Marino * derived from this software without specific prior written permission.
290d5acd74SJohn Marino *
300d5acd74SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
310d5acd74SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
320d5acd74SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
330d5acd74SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
340d5acd74SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
350d5acd74SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
360d5acd74SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
370d5acd74SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
380d5acd74SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
390d5acd74SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
400d5acd74SJohn Marino * SUCH DAMAGE.
410d5acd74SJohn Marino *
420d5acd74SJohn Marino * @(#)mskanji.c 1.0 (Phase One) 5/5/95
430d5acd74SJohn Marino */
440d5acd74SJohn Marino
450d5acd74SJohn Marino
460d5acd74SJohn Marino #include <sys/types.h>
470d5acd74SJohn Marino #include <errno.h>
480d5acd74SJohn Marino #include <runetype.h>
490d5acd74SJohn Marino #include <stdlib.h>
500d5acd74SJohn Marino #include <string.h>
510d5acd74SJohn Marino #include <wchar.h>
520d5acd74SJohn Marino #include "mblocal.h"
530d5acd74SJohn Marino
540d5acd74SJohn Marino static size_t _MSKanji_mbrtowc(wchar_t * __restrict, const char * __restrict,
550d5acd74SJohn Marino size_t, mbstate_t * __restrict);
560d5acd74SJohn Marino static int _MSKanji_mbsinit(const mbstate_t *);
570d5acd74SJohn Marino static size_t _MSKanji_wcrtomb(char * __restrict, wchar_t,
580d5acd74SJohn Marino mbstate_t * __restrict);
59*4776d4e8SJohn Marino static size_t _MSKanji_mbsnrtowcs(wchar_t * __restrict,
60*4776d4e8SJohn Marino const char ** __restrict, size_t, size_t,
61*4776d4e8SJohn Marino mbstate_t * __restrict);
62*4776d4e8SJohn Marino static size_t _MSKanji_wcsnrtombs(char * __restrict,
63*4776d4e8SJohn Marino const wchar_t ** __restrict, size_t, size_t,
64*4776d4e8SJohn Marino mbstate_t * __restrict);
650d5acd74SJohn Marino
660d5acd74SJohn Marino typedef struct {
670d5acd74SJohn Marino wchar_t ch;
680d5acd74SJohn Marino } _MSKanjiState;
690d5acd74SJohn Marino
700d5acd74SJohn Marino int
_MSKanji_init(struct xlocale_ctype * l,_RuneLocale * rl)710d5acd74SJohn Marino _MSKanji_init(struct xlocale_ctype *l, _RuneLocale *rl)
720d5acd74SJohn Marino {
730d5acd74SJohn Marino
740d5acd74SJohn Marino l->__mbrtowc = _MSKanji_mbrtowc;
750d5acd74SJohn Marino l->__wcrtomb = _MSKanji_wcrtomb;
76*4776d4e8SJohn Marino l->__mbsnrtowcs = _MSKanji_mbsnrtowcs;
77*4776d4e8SJohn Marino l->__wcsnrtombs = _MSKanji_wcsnrtombs;
780d5acd74SJohn Marino l->__mbsinit = _MSKanji_mbsinit;
790d5acd74SJohn Marino l->runes = rl;
800d5acd74SJohn Marino l->__mb_cur_max = 2;
810d5acd74SJohn Marino l->__mb_sb_limit = 256;
820d5acd74SJohn Marino return (0);
830d5acd74SJohn Marino }
840d5acd74SJohn Marino
850d5acd74SJohn Marino static int
_MSKanji_mbsinit(const mbstate_t * ps)860d5acd74SJohn Marino _MSKanji_mbsinit(const mbstate_t *ps)
870d5acd74SJohn Marino {
880d5acd74SJohn Marino
890d5acd74SJohn Marino return (ps == NULL || ((const _MSKanjiState *)ps)->ch == 0);
900d5acd74SJohn Marino }
910d5acd74SJohn Marino
920d5acd74SJohn Marino static size_t
_MSKanji_mbrtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n,mbstate_t * __restrict ps)930d5acd74SJohn Marino _MSKanji_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
940d5acd74SJohn Marino mbstate_t * __restrict ps)
950d5acd74SJohn Marino {
960d5acd74SJohn Marino _MSKanjiState *ms;
970d5acd74SJohn Marino wchar_t wc;
980d5acd74SJohn Marino
990d5acd74SJohn Marino ms = (_MSKanjiState *)ps;
1000d5acd74SJohn Marino
1010d5acd74SJohn Marino if ((ms->ch & ~0xFF) != 0) {
1020d5acd74SJohn Marino /* Bad conversion state. */
1030d5acd74SJohn Marino errno = EINVAL;
1040d5acd74SJohn Marino return ((size_t)-1);
1050d5acd74SJohn Marino }
1060d5acd74SJohn Marino
1070d5acd74SJohn Marino if (s == NULL) {
1080d5acd74SJohn Marino s = "";
1090d5acd74SJohn Marino n = 1;
1100d5acd74SJohn Marino pwc = NULL;
1110d5acd74SJohn Marino }
1120d5acd74SJohn Marino
1130d5acd74SJohn Marino if (n == 0)
1140d5acd74SJohn Marino /* Incomplete multibyte sequence */
1150d5acd74SJohn Marino return ((size_t)-2);
1160d5acd74SJohn Marino
1170d5acd74SJohn Marino if (ms->ch != 0) {
1180d5acd74SJohn Marino if (*s == '\0') {
1190d5acd74SJohn Marino errno = EILSEQ;
1200d5acd74SJohn Marino return ((size_t)-1);
1210d5acd74SJohn Marino }
1220d5acd74SJohn Marino wc = (ms->ch << 8) | (*s & 0xFF);
1230d5acd74SJohn Marino if (pwc != NULL)
1240d5acd74SJohn Marino *pwc = wc;
1250d5acd74SJohn Marino ms->ch = 0;
1260d5acd74SJohn Marino return (1);
1270d5acd74SJohn Marino }
1280d5acd74SJohn Marino wc = *s++ & 0xff;
1290d5acd74SJohn Marino if ((wc > 0x80 && wc < 0xa0) || (wc >= 0xe0 && wc < 0xfd)) {
1300d5acd74SJohn Marino if (n < 2) {
1310d5acd74SJohn Marino /* Incomplete multibyte sequence */
1320d5acd74SJohn Marino ms->ch = wc;
1330d5acd74SJohn Marino return ((size_t)-2);
1340d5acd74SJohn Marino }
1350d5acd74SJohn Marino if (*s == '\0') {
1360d5acd74SJohn Marino errno = EILSEQ;
1370d5acd74SJohn Marino return ((size_t)-1);
1380d5acd74SJohn Marino }
1390d5acd74SJohn Marino wc = (wc << 8) | (*s++ & 0xff);
1400d5acd74SJohn Marino if (pwc != NULL)
1410d5acd74SJohn Marino *pwc = wc;
1420d5acd74SJohn Marino return (2);
1430d5acd74SJohn Marino } else {
1440d5acd74SJohn Marino if (pwc != NULL)
1450d5acd74SJohn Marino *pwc = wc;
1460d5acd74SJohn Marino return (wc == L'\0' ? 0 : 1);
1470d5acd74SJohn Marino }
1480d5acd74SJohn Marino }
1490d5acd74SJohn Marino
1500d5acd74SJohn Marino static size_t
_MSKanji_wcrtomb(char * __restrict s,wchar_t wc,mbstate_t * __restrict ps)1510d5acd74SJohn Marino _MSKanji_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
1520d5acd74SJohn Marino {
1530d5acd74SJohn Marino _MSKanjiState *ms;
1540d5acd74SJohn Marino int len, i;
1550d5acd74SJohn Marino
1560d5acd74SJohn Marino ms = (_MSKanjiState *)ps;
1570d5acd74SJohn Marino
1580d5acd74SJohn Marino if (ms->ch != 0) {
1590d5acd74SJohn Marino errno = EINVAL;
1600d5acd74SJohn Marino return ((size_t)-1);
1610d5acd74SJohn Marino }
1620d5acd74SJohn Marino
1630d5acd74SJohn Marino if (s == NULL)
1640d5acd74SJohn Marino /* Reset to initial shift state (no-op) */
1650d5acd74SJohn Marino return (1);
1660d5acd74SJohn Marino len = (wc > 0x100) ? 2 : 1;
1670d5acd74SJohn Marino for (i = len; i-- > 0; )
1680d5acd74SJohn Marino *s++ = wc >> (i << 3);
1690d5acd74SJohn Marino return (len);
1700d5acd74SJohn Marino }
171*4776d4e8SJohn Marino
172*4776d4e8SJohn Marino static size_t
_MSKanji_mbsnrtowcs(wchar_t * __restrict dst,const char ** __restrict src,size_t nms,size_t len,mbstate_t * __restrict ps)173*4776d4e8SJohn Marino _MSKanji_mbsnrtowcs(wchar_t * __restrict dst,
174*4776d4e8SJohn Marino const char ** __restrict src, size_t nms,
175*4776d4e8SJohn Marino size_t len, mbstate_t * __restrict ps)
176*4776d4e8SJohn Marino {
177*4776d4e8SJohn Marino return (__mbsnrtowcs_std(dst, src, nms, len, ps, _MSKanji_mbrtowc));
178*4776d4e8SJohn Marino }
179*4776d4e8SJohn Marino
180*4776d4e8SJohn Marino static size_t
_MSKanji_wcsnrtombs(char * __restrict dst,const wchar_t ** __restrict src,size_t nwc,size_t len,mbstate_t * __restrict ps)181*4776d4e8SJohn Marino _MSKanji_wcsnrtombs(char * __restrict dst,
182*4776d4e8SJohn Marino const wchar_t ** __restrict src, size_t nwc,
183*4776d4e8SJohn Marino size_t len, mbstate_t * __restrict ps)
184*4776d4e8SJohn Marino {
185*4776d4e8SJohn Marino return (__wcsnrtombs_std(dst, src, nwc, len, ps, _MSKanji_wcrtomb));
186*4776d4e8SJohn Marino }
187