Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / modules / citrus_euctw.c
blobf2fa7027a6056a8a27e09f9cb81f6d5e39a0fae4
1 /* $NetBSD: citrus_euctw.c,v 1.10 2005/10/29 18:02:04 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_euctw.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
4 /*-
5 * Copyright (c)2002 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 /*-
31 * Copyright (c)1999 Citrus Project,
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
55 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
58 #include <sys/types.h>
59 #include <assert.h>
60 #include <errno.h>
61 #include <limits.h>
62 #include <locale.h>
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <wchar.h>
69 #include "citrus_namespace.h"
70 #include "citrus_types.h"
71 #include "citrus_module.h"
72 #include "citrus_ctype.h"
73 #include "citrus_stdenc.h"
74 #include "citrus_euctw.h"
77 /* ----------------------------------------------------------------------
78 * private stuffs used by templates
81 typedef struct {
82 char ch[4];
83 int chlen;
84 } _EUCTWState;
86 typedef struct {
87 int dummy;
88 } _EUCTWEncodingInfo;
89 typedef struct {
90 _EUCTWEncodingInfo ei;
91 struct {
92 /* for future multi-locale facility */
93 _EUCTWState s_mblen;
94 _EUCTWState s_mbrlen;
95 _EUCTWState s_mbrtowc;
96 _EUCTWState s_mbtowc;
97 _EUCTWState s_mbsrtowcs;
98 _EUCTWState s_wcrtomb;
99 _EUCTWState s_wcsrtombs;
100 _EUCTWState s_wctomb;
101 } states;
102 } _EUCTWCTypeInfo;
104 #define _SS2 0x008e
105 #define _SS3 0x008f
107 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
108 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
110 #define _FUNCNAME(m) _citrus_EUCTW_##m
111 #define _ENCODING_INFO _EUCTWEncodingInfo
112 #define _CTYPE_INFO _EUCTWCTypeInfo
113 #define _ENCODING_STATE _EUCTWState
114 #define _ENCODING_MB_CUR_MAX(_ei_) 4
115 #define _ENCODING_IS_STATE_DEPENDENT 0
116 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
118 static __inline int
119 _citrus_EUCTW_cs(u_int c)
121 c &= 0xff;
123 return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
126 static __inline int
127 _citrus_EUCTW_count(int cs)
129 switch (cs) {
130 case 0:
131 return 1;
132 case 1:
133 return 2;
134 case 2:
135 return 4;
136 case 3:
137 abort();
138 /*NOTREACHED*/
140 return 0;
143 static __inline void
144 /*ARGSUSED*/
145 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei,
146 _EUCTWState * __restrict s)
148 memset(s, 0, sizeof(*s));
151 static __inline void
152 /*ARGSUSED*/
153 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei,
154 void * __restrict pspriv,
155 const _EUCTWState * __restrict s)
157 memcpy(pspriv, (const void *)s, sizeof(*s));
160 static __inline void
161 /*ARGSUSED*/
162 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei,
163 _EUCTWState * __restrict s,
164 const void * __restrict pspriv)
166 memcpy((void *)s, pspriv, sizeof(*s));
169 static int
170 /*ARGSUSED*/
171 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo * __restrict ei,
172 const void * __restrict var, size_t lenvar)
175 _DIAGASSERT(ei != NULL);
177 memset((void *)ei, 0, sizeof(*ei));
179 return 0;
182 static void
183 /*ARGSUSED*/
184 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo *ei)
188 static int
189 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo * __restrict ei,
190 wchar_t * __restrict pwc,
191 const char ** __restrict s,
192 size_t n, _EUCTWState * __restrict psenc,
193 size_t * __restrict nresult)
195 wchar_t wchar;
196 int c, cs;
197 int chlenbak;
198 const char *s0;
200 _DIAGASSERT(nresult != 0);
201 _DIAGASSERT(ei != NULL);
202 _DIAGASSERT(psenc != NULL);
203 _DIAGASSERT(s != NULL);
205 s0 = *s;
207 if (s0 == NULL) {
208 _citrus_EUCTW_init_state(ei, psenc);
209 *nresult = 0; /* state independent */
210 return (0);
213 chlenbak = psenc->chlen;
215 /* make sure we have the first byte in the buffer */
216 switch (psenc->chlen) {
217 case 0:
218 if (n < 1)
219 goto restart;
220 psenc->ch[0] = *s0++;
221 psenc->chlen = 1;
222 n--;
223 break;
224 case 1:
225 case 2:
226 break;
227 default:
228 /* illgeal state */
229 goto ilseq;
232 c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
233 if (c == 0)
234 goto ilseq;
235 while (psenc->chlen < c) {
236 if (n < 1)
237 goto ilseq;
238 psenc->ch[psenc->chlen] = *s0++;
239 psenc->chlen++;
240 n--;
243 wchar = 0;
244 switch (cs) {
245 case 0:
246 if (psenc->ch[0] & 0x80)
247 goto ilseq;
248 wchar = psenc->ch[0] & 0xff;
249 break;
250 case 1:
251 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
252 goto ilseq;
253 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
254 wchar |= 'G' << 24;
255 break;
256 case 2:
257 if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1])
258 goto ilseq;
259 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
260 goto ilseq;
261 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
262 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
263 break;
264 default:
265 goto ilseq;
268 *s = s0;
269 psenc->chlen = 0;
271 if (pwc)
272 *pwc = wchar;
274 if (!wchar)
275 *nresult = 0;
276 else
277 *nresult = c - chlenbak;
279 return (0);
281 ilseq:
282 psenc->chlen = 0;
283 *nresult = (size_t)-1;
284 return (EILSEQ);
286 restart:
287 *s = s0;
288 *nresult = (size_t)-1;
289 return (0);
292 static int
293 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei,
294 char * __restrict s, size_t n, wchar_t wc,
295 _EUCTWState * __restrict psenc,
296 size_t * __restrict nresult)
298 wchar_t cs = wc & 0x7f000080;
299 wchar_t v;
300 int i, len, clen, ret;
302 _DIAGASSERT(ei != NULL);
303 _DIAGASSERT(nresult != 0);
304 _DIAGASSERT(s != NULL);
306 clen = 1;
307 if (wc & 0x00007f00)
308 clen = 2;
309 if ((wc & 0x007f0000) && !(wc & 0x00800000))
310 clen = 3;
312 if (clen == 1 && cs == 0x00000000) {
313 /* ASCII */
314 len = 1;
315 if (n < len) {
316 ret = E2BIG;
317 goto err;
319 v = wc & 0x0000007f;
320 } else if (clen == 2 && cs == ('G' << 24)) {
321 /* CNS-11643-1 */
322 len = 2;
323 if (n < len) {
324 ret = E2BIG;
325 goto err;
327 v = wc & 0x00007f7f;
328 v |= 0x00008080;
329 } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
330 /* CNS-11643-[2-7] */
331 len = 4;
332 if (n < len) {
333 ret = E2BIG;
334 goto err;
336 *s++ = _SS2;
337 *s++ = (cs >> 24) - 'H' + 0xa2;
338 v = wc & 0x00007f7f;
339 v |= 0x00008080;
340 } else {
341 ret = EILSEQ;
342 goto err;
345 i = clen;
346 while (i-- > 0)
347 *s++ = (v >> (i << 3)) & 0xff;
349 *nresult = len;
350 return 0;
352 err:
353 *nresult = (size_t)-1;
354 return ret;
357 static __inline int
358 /*ARGSUSED*/
359 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo * __restrict ei,
360 _csid_t * __restrict csid,
361 _index_t * __restrict idx, wchar_t wc)
364 _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
366 *csid = (_csid_t)(wc >> 24) & 0xFF;
367 *idx = (_index_t)(wc & 0x7F7F);
369 return (0);
372 static __inline int
373 /*ARGSUSED*/
374 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo * __restrict ei,
375 wchar_t * __restrict wc,
376 _csid_t csid, _index_t idx)
379 _DIAGASSERT(ei != NULL && wc != NULL);
381 if (csid==0) {
382 if ((idx & ~0x7F) != 0)
383 return (EINVAL);
384 *wc = (wchar_t)idx;
385 } else {
386 if (csid < 'G' || csid > 'M' || (idx & ~0x7F7F) != 0)
387 return (EINVAL);
388 *wc = (wchar_t)idx | ((wchar_t)csid<<24);
391 return (0);
394 static __inline int
395 /*ARGSUSED*/
396 _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo * __restrict ei,
397 _EUCTWState * __restrict psenc,
398 int * __restrict rstate)
401 if (psenc->chlen == 0)
402 *rstate = _STDENC_SDGEN_INITIAL;
403 else
404 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
406 return 0;
409 /* ----------------------------------------------------------------------
410 * public interface for ctype
413 _CITRUS_CTYPE_DECLS(EUCTW);
414 _CITRUS_CTYPE_DEF_OPS(EUCTW);
416 #include "citrus_ctype_template.h"
418 /* ----------------------------------------------------------------------
419 * public interface for stdenc
422 _CITRUS_STDENC_DECLS(EUCTW);
423 _CITRUS_STDENC_DEF_OPS(EUCTW);
425 #include "citrus_stdenc_template.h"