xref: /titanic_41/usr/src/lib/libc/port/locale/gb18030.c (revision 8d0c3d29bb99f6521f2dc5058a7e4debebad7899)
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 /*
28  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 /*
33  * PRC National Standard GB 18030-2000 encoding of Chinese text.
34  *
35  * See gb18030(5) for details.
36  */
37 
38 #include "lint.h"
39 #include <sys/types.h>
40 #include <errno.h>
41 #include "runetype.h"
42 #include <stdlib.h>
43 #include <string.h>
44 #include <wchar.h>
45 #include "mblocal.h"
46 
47 
48 static size_t	_GB18030_mbrtowc(wchar_t *_RESTRICT_KYWD,
49 		    const char *_RESTRICT_KYWD,
50 		    size_t, mbstate_t *_RESTRICT_KYWD);
51 static int	_GB18030_mbsinit(const mbstate_t *);
52 static size_t	_GB18030_wcrtomb(char *_RESTRICT_KYWD, wchar_t,
53 		    mbstate_t *_RESTRICT_KYWD);
54 
55 typedef struct {
56 	int	count;
57 	uchar_t	bytes[4];
58 } _GB18030State;
59 
60 int
61 _GB18030_init(_RuneLocale *rl)
62 {
63 
64 	__mbrtowc = _GB18030_mbrtowc;
65 	__wcrtomb = _GB18030_wcrtomb;
66 	__mbsinit = _GB18030_mbsinit;
67 	_CurrentRuneLocale = rl;
68 	__ctype[520] = 4;
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_KYWD pwc, const char *_RESTRICT_KYWD s,
82     size_t n, mbstate_t *_RESTRICT_KYWD 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 	(void) 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_KYWD s, wchar_t wc,
166     mbstate_t *_RESTRICT_KYWD ps)
167 {
168 	_GB18030State *gs;
169 	size_t len;
170 	int c;
171 
172 	gs = (_GB18030State *)ps;
173 
174 	if (gs->count != 0) {
175 		errno = EINVAL;
176 		return ((size_t)-1);
177 	}
178 
179 	if (s == NULL)
180 		/* Reset to initial shift state (no-op) */
181 		return (1);
182 	if ((wc & ~0x7fffffff) != 0)
183 		goto ilseq;
184 	if (wc & 0x7f000000) {
185 		/* Replace high bit that mbrtowc() removed. */
186 		wc |= 0x80000000;
187 		c = (wc >> 24) & 0xff;
188 		if (c < 0x81 || c > 0xfe)
189 			goto ilseq;
190 		*s++ = c;
191 		c = (wc >> 16) & 0xff;
192 		if (c < 0x30 || c > 0x39)
193 			goto ilseq;
194 		*s++ = c;
195 		c = (wc >> 8) & 0xff;
196 		if (c < 0x81 || c > 0xfe)
197 			goto ilseq;
198 		*s++ = c;
199 		c = wc & 0xff;
200 		if (c < 0x30 || c > 0x39)
201 			goto ilseq;
202 		*s++ = c;
203 		len = 4;
204 	} else if (wc & 0x00ff0000)
205 		goto ilseq;
206 	else if (wc & 0x0000ff00) {
207 		c = (wc >> 8) & 0xff;
208 		if (c < 0x81 || c > 0xfe)
209 			goto ilseq;
210 		*s++ = c;
211 		c = wc & 0xff;
212 		if (c < 0x40 || c == 0x7f || c == 0xff)
213 			goto ilseq;
214 		*s++ = c;
215 		len = 2;
216 	} else if (wc <= 0x7f) {
217 		*s++ = wc;
218 		len = 1;
219 	} else
220 		goto ilseq;
221 
222 	return (len);
223 ilseq:
224 	errno = EILSEQ;
225 	return ((size_t)-1);
226 }
227