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