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