xref: /freebsd/lib/libc/locale/gb18030.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #include "mblocal.h"
41 
42 extern int __mb_sb_limit;
43 
44 static size_t	_GB18030_mbrtowc(wchar_t * __restrict, const char * __restrict,
45 		    size_t, mbstate_t * __restrict);
46 static int	_GB18030_mbsinit(const mbstate_t *);
47 static size_t	_GB18030_wcrtomb(char * __restrict, wchar_t,
48 		    mbstate_t * __restrict);
49 
50 typedef struct {
51 	int	count;
52 	u_char	bytes[4];
53 } _GB18030State;
54 
55 int
56 _GB18030_init(_RuneLocale *rl)
57 {
58 
59 	__mbrtowc = _GB18030_mbrtowc;
60 	__wcrtomb = _GB18030_wcrtomb;
61 	__mbsinit = _GB18030_mbsinit;
62 	_CurrentRuneLocale = rl;
63 	__mb_cur_max = 4;
64 	__mb_sb_limit = 128;
65 
66 	return (0);
67 }
68 
69 static int
70 _GB18030_mbsinit(const mbstate_t *ps)
71 {
72 
73 	return (ps == NULL || ((const _GB18030State *)ps)->count == 0);
74 }
75 
76 static size_t
77 _GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
78     size_t n, mbstate_t * __restrict ps)
79 {
80 	_GB18030State *gs;
81 	wchar_t wch;
82 	int ch, len, ocount;
83 	size_t ncopy;
84 
85 	gs = (_GB18030State *)ps;
86 
87 	if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
88 		errno = EINVAL;
89 		return ((size_t)-1);
90 	}
91 
92 	if (s == NULL) {
93 		s = "";
94 		n = 1;
95 		pwc = NULL;
96 	}
97 
98 	ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
99 	memcpy(gs->bytes + gs->count, s, ncopy);
100 	ocount = gs->count;
101 	gs->count += ncopy;
102 	s = (char *)gs->bytes;
103 	n = gs->count;
104 
105 	if (n == 0)
106 		/* Incomplete multibyte sequence */
107 		return ((size_t)-2);
108 
109 	/*
110 	 * Single byte:		[00-7f]
111 	 * Two byte:		[81-fe][40-7e,80-fe]
112 	 * Four byte:		[81-fe][30-39][81-fe][30-39]
113 	 */
114 	ch = (unsigned char)*s++;
115 	if (ch <= 0x7f) {
116 		len = 1;
117 		wch = ch;
118 	} else if (ch >= 0x81 && ch <= 0xfe) {
119 		wch = ch;
120 		if (n < 2)
121 			return ((size_t)-2);
122 		ch = (unsigned char)*s++;
123 		if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
124 			wch = (wch << 8) | ch;
125 			len = 2;
126 		} else if (ch >= 0x30 && ch <= 0x39) {
127 			/*
128 			 * Strip high bit off the wide character we will
129 			 * eventually output so that it is positive when
130 			 * cast to wint_t on 32-bit twos-complement machines.
131 			 */
132 			wch = ((wch & 0x7f) << 8) | ch;
133 			if (n < 3)
134 				return ((size_t)-2);
135 			ch = (unsigned char)*s++;
136 			if (ch < 0x81 || ch > 0xfe)
137 				goto ilseq;
138 			wch = (wch << 8) | ch;
139 			if (n < 4)
140 				return ((size_t)-2);
141 			ch = (unsigned char)*s++;
142 			if (ch < 0x30 || ch > 0x39)
143 				goto ilseq;
144 			wch = (wch << 8) | ch;
145 			len = 4;
146 		} else
147 			goto ilseq;
148 	} else
149 		goto ilseq;
150 
151 	if (pwc != NULL)
152 		*pwc = wch;
153 	gs->count = 0;
154 	return (wch == L'\0' ? 0 : len - ocount);
155 ilseq:
156 	errno = EILSEQ;
157 	return ((size_t)-1);
158 }
159 
160 static size_t
161 _GB18030_wcrtomb(char * __restrict s, wchar_t wc, mbstate_t * __restrict ps)
162 {
163 	_GB18030State *gs;
164 	size_t len;
165 	int c;
166 
167 	gs = (_GB18030State *)ps;
168 
169 	if (gs->count != 0) {
170 		errno = EINVAL;
171 		return ((size_t)-1);
172 	}
173 
174 	if (s == NULL)
175 		/* Reset to initial shift state (no-op) */
176 		return (1);
177 	if ((wc & ~0x7fffffff) != 0)
178 		goto ilseq;
179 	if (wc & 0x7f000000) {
180 		/* Replace high bit that mbrtowc() removed. */
181 		wc |= 0x80000000;
182 		c = (wc >> 24) & 0xff;
183 		if (c < 0x81 || c > 0xfe)
184 			goto ilseq;
185 		*s++ = c;
186 		c = (wc >> 16) & 0xff;
187 		if (c < 0x30 || c > 0x39)
188 			goto ilseq;
189 		*s++ = c;
190 		c = (wc >> 8) & 0xff;
191 		if (c < 0x81 || c > 0xfe)
192 			goto ilseq;
193 		*s++ = c;
194 		c = wc & 0xff;
195 		if (c < 0x30 || c > 0x39)
196 			goto ilseq;
197 		*s++ = c;
198 		len = 4;
199 	} else if (wc & 0x00ff0000)
200 		goto ilseq;
201 	else if (wc & 0x0000ff00) {
202 		c = (wc >> 8) & 0xff;
203 		if (c < 0x81 || c > 0xfe)
204 			goto ilseq;
205 		*s++ = c;
206 		c = wc & 0xff;
207 		if (c < 0x40 || c == 0x7f || c == 0xff)
208 			goto ilseq;
209 		*s++ = c;
210 		len = 2;
211 	} else if (wc <= 0x7f) {
212 		*s++ = wc;
213 		len = 1;
214 	} else
215 		goto ilseq;
216 
217 	return (len);
218 ilseq:
219 	errno = EILSEQ;
220 	return ((size_t)-1);
221 }
222