xref: /freebsd/lib/libiconv_modules/EUCTW/citrus_euctw.c (revision 2a58b312b62f908ec92311d1bd8536dbaeb8e55b)
1 /* $FreeBSD$ */
2 /*	$NetBSD: citrus_euctw.c,v 1.11 2008/06/14 16:01:07 tnozaki Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7  * Copyright (c)2002 Citrus Project,
8  * All rights reserved.
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 
32 /*-
33  * Copyright (c)1999 Citrus Project,
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  *	$Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
58  */
59 
60 #include <sys/cdefs.h>
61 #include <sys/types.h>
62 
63 #include <assert.h>
64 #include <errno.h>
65 #include <limits.h>
66 #include <stddef.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <wchar.h>
71 
72 #include "citrus_namespace.h"
73 #include "citrus_types.h"
74 #include "citrus_module.h"
75 #include "citrus_stdenc.h"
76 #include "citrus_euctw.h"
77 
78 
79 /* ----------------------------------------------------------------------
80  * private stuffs used by templates
81  */
82 
83 typedef struct {
84 	int	 chlen;
85 	char	 ch[4];
86 } _EUCTWState;
87 
88 typedef struct {
89 	int	 dummy;
90 } _EUCTWEncodingInfo;
91 
92 #define	_SS2	0x008e
93 #define	_SS3	0x008f
94 
95 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
96 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
97 
98 #define _FUNCNAME(m)			_citrus_EUCTW_##m
99 #define _ENCODING_INFO			_EUCTWEncodingInfo
100 #define _ENCODING_STATE			_EUCTWState
101 #define _ENCODING_MB_CUR_MAX(_ei_)	4
102 #define _ENCODING_IS_STATE_DEPENDENT	0
103 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
104 
105 static __inline int
106 _citrus_EUCTW_cs(unsigned int c)
107 {
108 
109 	c &= 0xff;
110 
111 	return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
112 }
113 
114 static __inline int
115 _citrus_EUCTW_count(int cs)
116 {
117 
118 	switch (cs) {
119 	case 0:
120 		/*FALLTHROUGH*/
121 	case 1:
122 		/*FALLTHROUGH*/
123 	case 2:
124 		return (1 << cs);
125 	case 3:
126 		abort();
127 		/*NOTREACHED*/
128 	}
129 	return (0);
130 }
131 
132 static __inline void
133 /*ARGSUSED*/
134 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei __unused,
135     _EUCTWState * __restrict s)
136 {
137 
138 	memset(s, 0, sizeof(*s));
139 }
140 
141 #if 0
142 static __inline void
143 /*ARGSUSED*/
144 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei __unused,
145     void * __restrict pspriv, const _EUCTWState * __restrict s)
146 {
147 
148 	memcpy(pspriv, (const void *)s, sizeof(*s));
149 }
150 
151 static __inline void
152 /*ARGSUSED*/
153 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei __unused,
154     _EUCTWState * __restrict s, const void * __restrict pspriv)
155 {
156 
157 	memcpy((void *)s, pspriv, sizeof(*s));
158 }
159 #endif
160 
161 static int
162 /*ARGSUSED*/
163 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
164     const void * __restrict var __unused, size_t lenvar __unused)
165 {
166 
167 	memset((void *)ei, 0, sizeof(*ei));
168 
169 	return (0);
170 }
171 
172 static void
173 /*ARGSUSED*/
174 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei __unused)
175 {
176 
177 }
178 
179 static int
180 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
181     wchar_t * __restrict pwc, char ** __restrict s,
182     size_t n, _EUCTWState * __restrict psenc, size_t * __restrict nresult)
183 {
184 	char *s0;
185 	wchar_t wchar;
186 	int c, chlenbak, cs;
187 
188 	s0 = *s;
189 
190 	if (s0 == NULL) {
191 		_citrus_EUCTW_init_state(ei, psenc);
192 		*nresult = 0; /* state independent */
193 		return (0);
194 	}
195 
196 	chlenbak = psenc->chlen;
197 
198 	/* make sure we have the first byte in the buffer */
199 	switch (psenc->chlen) {
200 	case 0:
201 		if (n < 1)
202 			goto restart;
203 		psenc->ch[0] = *s0++;
204 		psenc->chlen = 1;
205 		n--;
206 		break;
207 	case 1:
208 	case 2:
209 		break;
210 	default:
211 		/* illgeal state */
212 		goto ilseq;
213 	}
214 
215 	c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
216 	if (c == 0)
217 		goto ilseq;
218 	while (psenc->chlen < c) {
219 		if (n < 1)
220 			goto ilseq;
221 		psenc->ch[psenc->chlen] = *s0++;
222 		psenc->chlen++;
223 		n--;
224 	}
225 
226 	wchar = 0;
227 	switch (cs) {
228 	case 0:
229 		if (psenc->ch[0] & 0x80)
230 			goto ilseq;
231 		wchar = psenc->ch[0] & 0xff;
232 		break;
233 	case 1:
234 		if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
235 			goto ilseq;
236 		wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
237 		wchar |= 'G' << 24;
238 		break;
239 	case 2:
240 		if ((unsigned char)psenc->ch[1] < 0xa1 ||
241 		    0xa7 < (unsigned char)psenc->ch[1])
242 			goto ilseq;
243 		if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
244 			goto ilseq;
245 		wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
246 		wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
247 		break;
248 	default:
249 		goto ilseq;
250 	}
251 
252 	*s = s0;
253 	psenc->chlen = 0;
254 
255 	if (pwc)
256 		*pwc = wchar;
257 	*nresult = wchar ? c - chlenbak : 0;
258 	return (0);
259 
260 ilseq:
261 	psenc->chlen = 0;
262 	*nresult = (size_t)-1;
263 	return (EILSEQ);
264 
265 restart:
266 	*s = s0;
267 	*nresult = (size_t)-1;
268 	return (0);
269 }
270 
271 static int
272 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei __unused,
273     char * __restrict s, size_t n, wchar_t wc,
274     _EUCTWState * __restrict psenc __unused, size_t * __restrict nresult)
275 {
276 	wchar_t cs, v;
277 	int clen, i, ret;
278 	size_t len;
279 
280 	cs = wc & 0x7f000080;
281 	clen = 1;
282 	if (wc & 0x00007f00)
283 		clen = 2;
284 	if ((wc & 0x007f0000) && !(wc & 0x00800000))
285 		clen = 3;
286 
287 	if (clen == 1 && cs == 0x00000000) {
288 		/* ASCII */
289 		len = 1;
290 		if (n < len) {
291 			ret = E2BIG;
292 			goto err;
293 		}
294 		v = wc & 0x0000007f;
295 	} else if (clen == 2 && cs == ('G' << 24)) {
296 		/* CNS-11643-1 */
297 		len = 2;
298 		if (n < len) {
299 			ret = E2BIG;
300 			goto err;
301 		}
302 		v = wc & 0x00007f7f;
303 		v |= 0x00008080;
304 	} else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
305 		/* CNS-11643-[2-7] */
306 		len = 4;
307 		if (n < len) {
308 			ret = E2BIG;
309 			goto err;
310 		}
311 		*s++ = _SS2;
312 		*s++ = (cs >> 24) - 'H' + 0xa2;
313 		v = wc & 0x00007f7f;
314 		v |= 0x00008080;
315 	} else {
316 		ret = EILSEQ;
317 		goto err;
318 	}
319 
320 	i = clen;
321 	while (i-- > 0)
322 		*s++ = (v >> (i << 3)) & 0xff;
323 
324 	*nresult = len;
325 	return (0);
326 
327 err:
328 	*nresult = (size_t)-1;
329 	return (ret);
330 }
331 
332 static __inline int
333 /*ARGSUSED*/
334 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei __unused,
335     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
336 {
337 
338 	*csid = (_csid_t)(wc >> 24) & 0xFF;
339 	*idx  = (_index_t)(wc & 0x7F7F);
340 
341 	return (0);
342 }
343 
344 static __inline int
345 /*ARGSUSED*/
346 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei __unused,
347     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
348 {
349 
350 	if (csid == 0) {
351 		if ((idx & ~0x7F) != 0)
352 			return (EINVAL);
353 		*wc = (wchar_t)idx;
354 	} else {
355 		if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
356 			return (EINVAL);
357 		*wc = (wchar_t)idx | ((wchar_t)csid << 24);
358 	}
359 
360 	return (0);
361 }
362 
363 static __inline int
364 /*ARGSUSED*/
365 _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei __unused,
366     _EUCTWState * __restrict psenc, int * __restrict rstate)
367 {
368 
369 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
370 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
371 	return (0);
372 }
373 
374 /* ----------------------------------------------------------------------
375  * public interface for stdenc
376  */
377 
378 _CITRUS_STDENC_DECLS(EUCTW);
379 _CITRUS_STDENC_DEF_OPS(EUCTW);
380 
381 #include "citrus_stdenc_template.h"
382