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