xref: /freebsd/lib/libc/locale/gbk.c (revision 6abda1f09339e3c395822c4b834b869ecb6ec997)
1 /*-
2  * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved.
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Paul Borman at Krystal Technologies.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
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	_GBK_init(_RuneLocale *);
51 size_t	_GBK_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
52 	    mbstate_t * __restrict);
53 size_t	_GBK_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
54 
55 int
56 _GBK_init(_RuneLocale *rl)
57 {
58 
59 	__mbrtowc = _GBK_mbrtowc;
60 	__wcrtomb = _GBK_wcrtomb;
61 	_CurrentRuneLocale = rl;
62 	__mb_cur_max = 2;
63 	return (0);
64 }
65 
66 static __inline int
67 _gbk_check(u_int c)
68 {
69 
70 	c &= 0xff;
71 	return ((c >= 0x81 && c <= 0xfe) ? 2 : 1);
72 }
73 
74 size_t
75 _GBK_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n,
76     mbstate_t * __restrict ps __unused)
77 {
78 	wchar_t wc;
79 	int i, len;
80 
81 	if (s == NULL)
82 		/* Reset to initial shift state (no-op) */
83 		return (0);
84 	if (n == 0 || (size_t)(len = _gbk_check(*s)) > n)
85 		/* Incomplete multibyte sequence */
86 		return ((size_t)-2);
87 	wc = 0;
88 	i = len;
89 	while (i-- > 0)
90 		wc = (wc << 8) | (unsigned char)*s++;
91 	if (pwc != NULL)
92 		*pwc = wc;
93 	return (wc == L'\0' ? 0 : len);
94 }
95 
96 size_t
97 _GBK_wcrtomb(char * __restrict s, wchar_t wc,
98     mbstate_t * __restrict ps __unused)
99 {
100 
101 	if (s == NULL)
102 		/* Reset to initial shift state (no-op) */
103 		return (1);
104 	if (wc & 0x8000) {
105 		*s++ = (wc >> 8) & 0xff;
106 		*s = wc & 0xff;
107 		return (2);
108 	}
109 	*s = wc & 0xff;
110 	return (1);
111 }
112