xref: /freebsd/lib/libc/locale/mskanji.c (revision e858faa9bb929e040dfa2ee6f9e4806cec4d685d)
1 /*
2  * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved.
3  *    ja_JP.SJIS locale table for BSD4.4/rune
4  *    version 1.0
5  *    (C) Sin'ichiro MIYATANI / Phase One, Inc
6  *    May 12, 1995
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Phase One, Inc.
19  * 4. The name of Phase One, Inc. may be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)mskanji.c	1.0 (Phase One) 5/5/95";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/types.h>
42 #include <runetype.h>
43 #include <stdlib.h>
44 #include <wchar.h>
45 
46 extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
47     size_t, mbstate_t * __restrict);
48 extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
49 
50 int	_MSKanji_init(_RuneLocale *);
51 size_t  _MSKanji_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
52 	    mbstate_t * __restrict);
53 size_t  _MSKanji_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
54 
55 int
56 _MSKanji_init(_RuneLocale *rl)
57 {
58 
59 	__mbrtowc = _MSKanji_mbrtowc;
60 	__wcrtomb = _MSKanji_wcrtomb;
61 	_CurrentRuneLocale = rl;
62 	__mb_cur_max = 2;
63 	return (0);
64 }
65 
66 size_t
67 _MSKanji_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
68     mbstate_t * __restrict ps __unused)
69 {
70 	wchar_t wc;
71 	int len;
72 
73 	if (s == NULL)
74 		/* Reset to initial shift state (no-op) */
75 		return (0);
76 	if (n == 0)
77 		/* Incomplete multibyte sequence */
78 		return ((size_t)-2);
79 	len = 1;
80 	wc = *s++ & 0xff;
81 	if ((wc > 0x80 && wc < 0xa0) || (wc >= 0xe0 && wc < 0xfd)) {
82 		if (n < 2)
83 			/* Incomplete multibyte sequence */
84 			return ((size_t)-2);
85 		wc = (wc << 8) | (*s++ & 0xff);
86 		len = 2;
87 	}
88 	if (pwc != NULL)
89 		*pwc = wc;
90 	return (wc == L'\0' ? 0 : len);
91 }
92 
93 size_t
94 _MSKanji_wcrtomb(char * __restrict s, wchar_t wc,
95     mbstate_t * __restrict ps __unused)
96 {
97 	int len, i;
98 
99 	if (s == NULL)
100 		/* Reset to initial shift state (no-op) */
101 		return (1);
102 
103 	len = (wc > 0x100) ? 2 : 1;
104 	for (i = len; i-- > 0; )
105 		*s++ = wc >> (i << 3);
106 	return (len);
107 }
108