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