mskanji.c revision 121893
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)
36static 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: head/lib/libc/locale/mskanji.c 121893 2003-11-02 10:09:33Z tjr $");
40
41#include <sys/types.h>
42#include <runetype.h>
43#include <stddef.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <wchar.h>
47
48extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
49    size_t, mbstate_t * __restrict);
50extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
51
52int	_MSKanji_init(_RuneLocale *);
53size_t  _MSKanji_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
54	    mbstate_t * __restrict);
55size_t  _MSKanji_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
56
57int
58_MSKanji_init(_RuneLocale *rl)
59{
60
61	__mbrtowc = _MSKanji_mbrtowc;
62	__wcrtomb = _MSKanji_wcrtomb;
63	_CurrentRuneLocale = rl;
64	__mb_cur_max = 2;
65	return (0);
66}
67
68size_t
69_MSKanji_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
70    mbstate_t * __restrict ps __unused)
71{
72	wchar_t wc;
73	int len;
74
75	if (s == NULL)
76		/* Reset to initial shift state (no-op) */
77		return (0);
78	if (n == 0)
79		/* Incomplete multibyte sequence */
80		return ((size_t)-2);
81	len = 1;
82	wc = *s++ & 0xff;
83	if ((wc > 0x80 && wc < 0xa0) || (wc >= 0xe0 && wc < 0xfd)) {
84		if (n < 2)
85			/* Incomplete multibyte sequence */
86			return ((size_t)-2);
87		wc = (wc << 8) | (*s++ & 0xff);
88		len = 2;
89	}
90	if (pwc != NULL)
91		*pwc = wc;
92	return (wc == L'\0' ? 0 : len);
93}
94
95size_t
96_MSKanji_wcrtomb(char * __restrict s, wchar_t wc,
97    mbstate_t * __restrict ps __unused)
98{
99	int len, i;
100
101	if (s == NULL)
102		/* Reset to initial shift state (no-op) */
103		return (1);
104
105	len = (wc > 0x100) ? 2 : 1;
106	for (i = len; i-- > 0; )
107		*s++ = wc >> (i << 3);
108	return (len);
109}
110