citrus_euctw.c revision 1.1.2.3 1 /* $NetBSD: citrus_euctw.c,v 1.1.2.3 2002/04/25 04:01:40 nathanw Exp $ */
2
3 /*-
4 * Copyright (c)2002 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright (c)1999 Citrus Project,
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
41 *
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
52 * SUCH DAMAGE.
53 *
54 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/euctw.c,v 1.13 2001/06/21 01:51:44 yamt Exp $
55 */
56
57 #include <sys/cdefs.h>
58 #if defined(LIBC_SCCS) && !defined(lint)
59 __RCSID("$NetBSD: citrus_euctw.c,v 1.1.2.3 2002/04/25 04:01:40 nathanw Exp $");
60 #endif /* LIBC_SCCS and not lint */
61
62 #include <assert.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <stddef.h>
68 #include <locale.h>
69 #include <wchar.h>
70 #include <sys/types.h>
71 #include <limits.h>
72 #include "citrus_module.h"
73 #include "citrus_ctype.h"
74 #include "citrus_euctw.h"
75
76
77 /* ----------------------------------------------------------------------
78 * private stuffs used by templates
79 */
80
81 typedef struct {
82 char ch[4];
83 int chlen;
84 } _EUCTWState;
85
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;
103
104 #define _SS2 0x008e
105 #define _SS3 0x008f
106
107 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
108 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
109
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
117
118 static __inline int
119 _citrus_EUCTW_cs(u_int c)
120 {
121 c &= 0xff;
122
123 return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
124 }
125
126 static __inline int
127 _citrus_EUCTW_count(int cs)
128 {
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*/
139 }
140 return 0;
141 }
142
143 static __inline void
144 /*ARGSUSED*/
145 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei,
146 _EUCTWState * __restrict s)
147 {
148 memset(s, 0, sizeof(*s));
149 }
150
151 static __inline void
152 /*ARGSUSED*/
153 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei,
154 void * __restrict pspriv,
155 const _EUCTWState * __restrict s)
156 {
157 memcpy(pspriv, (const void *)s, sizeof(*s));
158 }
159
160 static __inline void
161 /*ARGSUSED*/
162 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei,
163 _EUCTWState * __restrict s,
164 const void * __restrict pspriv)
165 {
166 memcpy((void *)s, pspriv, sizeof(*s));
167 }
168
169 static int
170 /*ARGSUSED*/
171 _citrus_EUCTW_stdencoding_init(_EUCTWEncodingInfo * __restrict ei,
172 const void * __restrict var, size_t lenvar)
173 {
174
175 _DIAGASSERT(ei != NULL);
176
177 memset((void *)ei, 0, sizeof(*ei));
178
179 return 0;
180 }
181
182 static void
183 /*ARGSUSED*/
184 _citrus_EUCTW_stdencoding_uninit(_EUCTWEncodingInfo *ei)
185 {
186 }
187
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)
194 {
195 wchar_t wchar;
196 int c, cs;
197 int chlenbak;
198 const char *s0;
199
200 _DIAGASSERT(nresult != 0);
201 _DIAGASSERT(ei != NULL);
202 _DIAGASSERT(psenc != NULL);
203 _DIAGASSERT(s != NULL);
204
205 s0 = *s;
206
207 if (s0 == NULL) {
208 _citrus_EUCTW_init_state(ei, psenc);
209 *nresult = 0; /* state independent */
210 return (0);
211 }
212
213 /* make sure we have the first byte in the buffer */
214 switch (psenc->chlen) {
215 case 0:
216 if (n < 1)
217 goto restart;
218 psenc->ch[0] = *s0++;
219 psenc->chlen = 1;
220 n--;
221 break;
222 case 1:
223 case 2:
224 break;
225 default:
226 /* illgeal state */
227 goto ilseq;
228 }
229
230 c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
231 if (c == 0)
232 goto ilseq;
233 while (psenc->chlen < c) {
234 if (n < 1)
235 goto ilseq;
236 psenc->ch[psenc->chlen] = *s0++;
237 psenc->chlen++;
238 n--;
239 }
240
241 wchar = 0;
242 switch (cs) {
243 case 0:
244 if (psenc->ch[0] & 0x80)
245 goto ilseq;
246 wchar = psenc->ch[0] & 0xff;
247 break;
248 case 1:
249 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
250 goto ilseq;
251 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
252 wchar |= 'G' << 24;
253 break;
254 case 2:
255 if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1])
256 goto ilseq;
257 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
258 goto ilseq;
259 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
260 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
261 break;
262 default:
263 goto ilseq;
264 }
265
266 *s = s0;
267 psenc->chlen = 0;
268
269 if (pwc)
270 *pwc = wchar;
271
272 if (!wchar)
273 *nresult = 0;
274 else
275 *nresult = c - chlenbak;
276
277 return (0);
278
279 ilseq:
280 psenc->chlen = 0;
281 *nresult = (size_t)-1;
282 return (EILSEQ);
283
284 restart:
285 *s = s0;
286 *nresult = (size_t)-1;
287 return (0);
288 }
289
290 static int
291 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei,
292 char * __restrict s, size_t n, wchar_t wc,
293 _EUCTWState * __restrict psenc,
294 size_t * __restrict nresult)
295 {
296 wchar_t cs = wc & 0x7f000080;
297 wchar_t v;
298 int i, len, clen;
299
300 _DIAGASSERT(ei != NULL);
301 _DIAGASSERT(nresult != 0);
302 _DIAGASSERT(s != NULL);
303
304 clen = 1;
305 if (wc & 0x00007f00)
306 clen = 2;
307 if ((wc & 0x007f0000) && !(wc & 0x00800000))
308 clen = 3;
309
310 if (clen == 1 && cs == 0x00000000) {
311 /* ASCII */
312 len = 1;
313 if (n < len)
314 goto ilseq;
315 v = wc & 0x0000007f;
316 } else if (clen == 2 && cs == ('G' << 24)) {
317 /* CNS-11643-1 */
318 len = 2;
319 if (n < len)
320 goto ilseq;
321 v = wc & 0x00007f7f;
322 v |= 0x00008080;
323 } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
324 /* CNS-11643-[2-7] */
325 len = 4;
326 if (n < len)
327 goto ilseq;
328 *s++ = _SS2;
329 *s++ = (cs >> 24) - 'H' + 0xa2;
330 v = wc & 0x00007f7f;
331 v |= 0x00008080;
332 } else
333 goto ilseq;
334
335 i = clen;
336 while (i-- > 0)
337 *s++ = (v >> (i << 3)) & 0xff;
338
339 *nresult = len;
340 return (0);
341
342 ilseq:
343 *nresult = (size_t)-1;
344 return (EILSEQ);
345 }
346
347 /* ----------------------------------------------------------------------
348 * public interface for ctype
349 */
350
351 _CITRUS_CTYPE_DECLS(EUCTW);
352 _CITRUS_CTYPE_DEF_OPS(EUCTW);
353
354 #include "citrus_ctype_template.h"
355