1 /* $NetBSD: citrus_euctw.c,v 1.10 2005/10/29 18:02:04 tshiozak Exp $ */
4 * Copyright (c)2002 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c)1999 Citrus Project,
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
57 #include <sys/cdefs.h>
58 #if defined(LIBC_SCCS) && !defined(lint)
59 __RCSID("$NetBSD: citrus_euctw.c,v 1.10 2005/10/29 18:02:04 tshiozak Exp $");
60 #endif /* LIBC_SCCS and not lint */
69 #include <sys/types.h>
72 #include "citrus_namespace.h"
73 #include "citrus_types.h"
74 #include "citrus_module.h"
75 #include "citrus_ctype.h"
76 #include "citrus_stdenc.h"
77 #include "citrus_euctw.h"
80 /* ----------------------------------------------------------------------
81 * private stuffs used by templates
93 _EUCTWEncodingInfo ei
;
95 /* for future multi-locale facility */
98 _EUCTWState s_mbrtowc
;
100 _EUCTWState s_mbsrtowcs
;
101 _EUCTWState s_wcrtomb
;
102 _EUCTWState s_wcsrtombs
;
103 _EUCTWState s_wctomb
;
110 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
111 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
113 #define _FUNCNAME(m) _citrus_EUCTW_##m
114 #define _ENCODING_INFO _EUCTWEncodingInfo
115 #define _CTYPE_INFO _EUCTWCTypeInfo
116 #define _ENCODING_STATE _EUCTWState
117 #define _ENCODING_MB_CUR_MAX(_ei_) 4
118 #define _ENCODING_IS_STATE_DEPENDENT 0
119 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
122 _citrus_EUCTW_cs(u_int c
)
126 return ((c
& 0x80) ? (c
== _SS2
? 2 : 1) : 0);
130 _citrus_EUCTW_count(int cs
)
148 _citrus_EUCTW_init_state(_EUCTWEncodingInfo
* __restrict ei
,
149 _EUCTWState
* __restrict s
)
151 memset(s
, 0, sizeof(*s
));
156 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo
* __restrict ei
,
157 void * __restrict pspriv
,
158 const _EUCTWState
* __restrict s
)
160 memcpy(pspriv
, (const void *)s
, sizeof(*s
));
165 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo
* __restrict ei
,
166 _EUCTWState
* __restrict s
,
167 const void * __restrict pspriv
)
169 memcpy((void *)s
, pspriv
, sizeof(*s
));
174 _citrus_EUCTW_encoding_module_init(_EUCTWEncodingInfo
* __restrict ei
,
175 const void * __restrict var
, size_t lenvar
)
178 _DIAGASSERT(ei
!= NULL
);
180 memset((void *)ei
, 0, sizeof(*ei
));
187 _citrus_EUCTW_encoding_module_uninit(_EUCTWEncodingInfo
*ei
)
192 _citrus_EUCTW_mbrtowc_priv(_EUCTWEncodingInfo
* __restrict ei
,
193 wchar_t * __restrict pwc
,
194 const char ** __restrict s
,
195 size_t n
, _EUCTWState
* __restrict psenc
,
196 size_t * __restrict nresult
)
203 _DIAGASSERT(nresult
!= 0);
204 _DIAGASSERT(ei
!= NULL
);
205 _DIAGASSERT(psenc
!= NULL
);
206 _DIAGASSERT(s
!= NULL
);
211 _citrus_EUCTW_init_state(ei
, psenc
);
212 *nresult
= 0; /* state independent */
216 chlenbak
= psenc
->chlen
;
218 /* make sure we have the first byte in the buffer */
219 switch (psenc
->chlen
) {
223 psenc
->ch
[0] = *s0
++;
235 c
= _citrus_EUCTW_count(cs
= _citrus_EUCTW_cs(psenc
->ch
[0] & 0xff));
238 while (psenc
->chlen
< c
) {
241 psenc
->ch
[psenc
->chlen
] = *s0
++;
249 if (psenc
->ch
[0] & 0x80)
251 wchar
= psenc
->ch
[0] & 0xff;
254 if (!(psenc
->ch
[0] & 0x80) || !(psenc
->ch
[1] & 0x80))
256 wchar
= ((psenc
->ch
[0] & 0xff) << 8) | (psenc
->ch
[1] & 0xff);
260 if ((u_char
)psenc
->ch
[1] < 0xa1 || 0xa7 < (u_char
)psenc
->ch
[1])
262 if (!(psenc
->ch
[2] & 0x80) || !(psenc
->ch
[3] & 0x80))
264 wchar
= ((psenc
->ch
[2] & 0xff) << 8) | (psenc
->ch
[3] & 0xff);
265 wchar
|= ('G' + psenc
->ch
[1] - 0xa1) << 24;
280 *nresult
= c
- chlenbak
;
286 *nresult
= (size_t)-1;
291 *nresult
= (size_t)-1;
296 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo
* __restrict ei
,
297 char * __restrict s
, size_t n
, wchar_t wc
,
298 _EUCTWState
* __restrict psenc
,
299 size_t * __restrict nresult
)
301 wchar_t cs
= wc
& 0x7f000080;
303 int i
, len
, clen
, ret
;
305 _DIAGASSERT(ei
!= NULL
);
306 _DIAGASSERT(nresult
!= 0);
307 _DIAGASSERT(s
!= NULL
);
312 if ((wc
& 0x007f0000) && !(wc
& 0x00800000))
315 if (clen
== 1 && cs
== 0x00000000) {
323 } else if (clen
== 2 && cs
== ('G' << 24)) {
332 } else if (clen
== 2 && 'H' <= (cs
>> 24) && (cs
>> 24) <= 'M') {
333 /* CNS-11643-[2-7] */
340 *s
++ = (cs
>> 24) - 'H' + 0xa2;
350 *s
++ = (v
>> (i
<< 3)) & 0xff;
356 *nresult
= (size_t)-1;
362 _citrus_EUCTW_stdenc_wctocs(_EUCTWEncodingInfo
* __restrict ei
,
363 _csid_t
* __restrict csid
,
364 _index_t
* __restrict idx
, wchar_t wc
)
367 _DIAGASSERT(ei
!= NULL
&& csid
!= NULL
&& idx
!= NULL
);
369 *csid
= (_csid_t
)(wc
>> 24) & 0xFF;
370 *idx
= (_index_t
)(wc
& 0x7F7F);
377 _citrus_EUCTW_stdenc_cstowc(_EUCTWEncodingInfo
* __restrict ei
,
378 wchar_t * __restrict wc
,
379 _csid_t csid
, _index_t idx
)
382 _DIAGASSERT(ei
!= NULL
&& wc
!= NULL
);
385 if ((idx
& ~0x7F) != 0)
389 if (csid
< 'G' || csid
> 'M' || (idx
& ~0x7F7F) != 0)
391 *wc
= (wchar_t)idx
| ((wchar_t)csid
<<24);
399 _citrus_EUCTW_stdenc_get_state_desc_generic(_EUCTWEncodingInfo
* __restrict ei
,
400 _EUCTWState
* __restrict psenc
,
401 int * __restrict rstate
)
404 if (psenc
->chlen
== 0)
405 *rstate
= _STDENC_SDGEN_INITIAL
;
407 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
412 /* ----------------------------------------------------------------------
413 * public interface for ctype
416 _CITRUS_CTYPE_DECLS(EUCTW
);
417 _CITRUS_CTYPE_DEF_OPS(EUCTW
);
419 #include "citrus_ctype_template.h"
421 /* ----------------------------------------------------------------------
422 * public interface for stdenc
425 _CITRUS_STDENC_DECLS(EUCTW
);
426 _CITRUS_STDENC_DEF_OPS(EUCTW
);
428 #include "citrus_stdenc_template.h"