xref: /freebsd/lib/libc/locale/gb18030.c (revision ca2dae42)
1 /*-
2  * Copyright (c) 2002-2004 Tim J. Robbins
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /*
27  * PRC National Standard GB 18030-2000 encoding of Chinese text.
28  *
29  * See gb18030(5) for details.
30  */
31 
32 #include <sys/param.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <errno.h>
36 #include <runetype.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40 
41 extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict,
42     size_t, mbstate_t * __restrict);
43 extern int (*__mbsinit)(const mbstate_t *);
44 extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict);
45 
46 int	_GB18030_init(_RuneLocale *);
47 size_t	_GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t,
48 	    mbstate_t * __restrict);
49 int	_GB18030_mbsinit(const mbstate_t *);
50 size_t	_GB18030_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict);
51 
52 typedef struct {
53 	int	count;
54 	u_char	bytes[4];
55 } _GB18030State;
56 
57 int
58 _GB18030_init(_RuneLocale *rl)
59 {
60 
61 	__mbrtowc = _GB18030_mbrtowc;
62 	__wcrtomb = _GB18030_wcrtomb;
63 	__mbsinit = _GB18030_mbsinit;
64 	_CurrentRuneLocale = rl;
65 	__mb_cur_max = 4;
66 
67 	return (0);
68 }
69 
70 int
71 _GB18030_mbsinit(const mbstate_t *ps)
72 {
73 
74 	return (ps == NULL || ((_GB18030State *)ps)->count == 0);
75 }
76 
77 size_t
78 _GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
79     size_t n, mbstate_t * __restrict ps)
80 {
81 	_GB18030State *gs;
82 	wchar_t wch;
83 	int ch, len, ocount;
84 	size_t ncopy;
85 
86 	gs = (_GB18030State *)ps;
87 
88 	if (s == NULL) {
89 		s = "";
90 		n = 1;
91 		pwc = NULL;
92 	}
93 
94 	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
95 	memcpy(gs->bytes + gs->count, s, ncopy);
96 	ocount = gs->count;
97 	gs->count += ncopy;
98 	s = (char *)gs->bytes;
99 	n = gs->count;
100 
101 	if (n == 0)
102 		/* Incomplete multibyte sequence */
103 		return ((size_t)-2);
104 
105 	/*
106 	 * Single byte:		[00-7f]
107 	 * Two byte:		[81-fe][40-7e,80-fe]
108 	 * Four byte:		[81-fe][30-39][81-fe][30-39]
109 	 */
110 	ch = (unsigned char)*s++;
111 	if (ch <= 0x7f) {
112 		len = 1;
113 		wch = ch;
114 	} else if (ch >= 0x81 && ch <= 0xfe) {
115 		wch = ch;
116 		if (n < 2)
117 			return ((size_t)-2);
118 		ch = (unsigned char)*s++;
119 		if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
120 			wch = (wch << 8) | ch;
121 			len = 2;
122 		} else if (ch >= 0x30 && ch <= 0x39) {
123 			/*
124 			 * Strip high bit off the wide character we will
125 			 * eventually output so that it is positive when
126 			 * cast to wint_t on 32-bit twos-complement machines.
127 			 */
128 			wch = ((wch & 0x7f) << 8) | ch;
129 			if (n < 3)
130 				return ((size_t)-2);
131 			ch = (unsigned char)*s++;
132 			if (ch < 0x81 || ch > 0xfe)
133 				goto ilseq;
134 			wch = (wch << 8) | ch;
135 			if (n < 4)
136 				return ((size_t)-2);
137 			ch = (unsigned char)*s++;
138 			if (ch < 0x30 || ch > 0x39)
139 				goto ilseq;
140 			wch = (wch << 8) | ch;
141 			len = 4;
142 		} else
143 			goto ilseq;
144 	} else
145 		goto ilseq;
146 
147 	if (pwc != NULL)
148 		*pwc = wch;
149 	gs->count = 0;
150 	return (wch == L'\0' ? 0 : len - ocount);
151 ilseq:
152 	errno = EILSEQ;
153 	return ((size_t)-1);
154 }
155 
156 size_t
157 _GB18030_wcrtomb(char * __restrict s, wchar_t wc,
158     mbstate_t * __restrict ps __unused)
159 {
160 	size_t len;
161 	int c;
162 
163 	if (s == NULL)
164 		/* Reset to initial shift state (no-op) */
165 		return (1);
166 	if ((wc & ~0x7fffffff) != 0)
167 		goto ilseq;
168 	if (wc & 0x7f000000) {
169 		/* Replace high bit that mbrtowc() removed. */
170 		wc |= 0x80000000;
171 		c = (wc >> 24) & 0xff;
172 		if (c < 0x81 || c > 0xfe)
173 			goto ilseq;
174 		*s++ = c;
175 		c = (wc >> 16) & 0xff;
176 		if (c < 0x30 || c > 0x39)
177 			goto ilseq;
178 		*s++ = c;
179 		c = (wc >> 8) & 0xff;
180 		if (c < 0x81 || c > 0xfe)
181 			goto ilseq;
182 		*s++ = c;
183 		c = wc & 0xff;
184 		if (c < 0x30 || c > 0x39)
185 			goto ilseq;
186 		*s++ = c;
187 		len = 4;
188 	} else if (wc & 0x00ff0000)
189 		goto ilseq;
190 	else if (wc & 0x0000ff00) {
191 		c = (wc >> 8) & 0xff;
192 		if (c < 0x81 || c > 0xfe)
193 			goto ilseq;
194 		*s++ = c;
195 		c = wc & 0xff;
196 		if (c < 0x40 || c == 0x7f || c == 0xff)
197 			goto ilseq;
198 		*s++ = c;
199 		len = 2;
200 	} else if (wc <= 0x7f) {
201 		*s++ = wc;
202 		len = 1;
203 	} else
204 		goto ilseq;
205 
206 	return (len);
207 ilseq:
208 	errno = EILSEQ;
209 	return ((size_t)-1);
210 }
211