citrus_euctw.c revision 1.2 1 /* $NetBSD: citrus_euctw.c,v 1.2 2002/03/27 15:01:17 yamt 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.2 2002/03/27 15:01:17 yamt 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_wcstombs;
101 _EUCTWState s_wctomb;
102 } states;
103 } _EUCTWCTypeInfo;
104
105 #define _SS2 0x008e
106 #define _SS3 0x008f
107
108 #define _TO_EI(_cl_) ((_EUCTWEncodingInfo *)(_cl_))
109 #define _TO_CEI(_cl_) ((_EUCTWCTypeInfo *)(_cl_))
110 #define _TO_STATE(_ps_) ((_EUCTWState *)(_ps_))
111 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
112 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
113
114 #define _FUNCNAME(m) _citrus_EUCTW_##m
115 #define _ENCODING_INFO _EUCTWEncodingInfo
116 #define _CTYPE_INFO _EUCTWCTypeInfo
117 #define _ENCODING_STATE _EUCTWState
118 #define _ENCODING_MB_CUR_MAX(_ei_) 4
119 #define _ENCODING_IS_STATE_DEPENDENT 0
120
121 static __inline int
122 _citrus_EUCTW_cs(u_int c)
123 {
124 c &= 0xff;
125
126 return ((c & 0x80) ? (c == _SS2 ? 2 : 1) : 0);
127 }
128
129 static __inline int
130 _citrus_EUCTW_count(int cs)
131 {
132 switch (cs) {
133 case 0:
134 return 1;
135 case 1:
136 return 2;
137 case 2:
138 return 4;
139 case 3:
140 abort();
141 /*NOTREACHED*/
142 }
143 return 0;
144 }
145
146 static __inline void
147 /*ARGSUSED*/
148 _citrus_EUCTW_init_state(_EUCTWEncodingInfo * __restrict ei,
149 _EUCTWState * __restrict s)
150 {
151 memset(s, 0, sizeof(*s));
152 }
153
154 static __inline void
155 /*ARGSUSED*/
156 _citrus_EUCTW_pack_state(_EUCTWEncodingInfo * __restrict ei,
157 void * __restrict pspriv,
158 const _EUCTWState * __restrict s)
159 {
160 memcpy(pspriv, (const void *)s, sizeof(*s));
161 }
162
163 static __inline void
164 /*ARGSUSED*/
165 _citrus_EUCTW_unpack_state(_EUCTWEncodingInfo * __restrict ei,
166 _EUCTWState * __restrict s,
167 const void * __restrict pspriv)
168 {
169 memcpy((void *)s, pspriv, sizeof(*s));
170 }
171
172 static int
173 /*ARGSUSED*/
174 _citrus_EUCTW_stdencoding_init(_EUCTWEncodingInfo * __restrict ei,
175 const void * __restrict var, size_t lenvar)
176 {
177
178 _DIAGASSERT(ei != NULL);
179
180 memset((void *)ei, 0, sizeof(*ei));
181
182 return 0;
183 }
184
185 static void
186 /*ARGSUSED*/
187 _citrus_EUCTW_stdencoding_uninit(_EUCTWEncodingInfo *ei)
188 {
189 }
190
191 static int
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)
197 {
198 wchar_t wchar;
199 int c, cs;
200 int chlenbak;
201 const char *s0;
202
203 _DIAGASSERT(nresult != 0);
204 _DIAGASSERT(ei != NULL);
205 _DIAGASSERT(psenc != NULL);
206 _DIAGASSERT(s != NULL);
207
208 s0 = *s;
209
210 if (s0 == NULL) {
211 _citrus_EUCTW_init_state(ei, psenc);
212 *nresult = 0; /* state independent */
213 return (0);
214 }
215
216 /* make sure we have the first byte in the buffer */
217 switch (psenc->chlen) {
218 case 0:
219 if (n < 1)
220 goto restart;
221 psenc->ch[0] = *s0++;
222 psenc->chlen = 1;
223 n--;
224 break;
225 case 1:
226 case 2:
227 break;
228 default:
229 /* illgeal state */
230 goto ilseq;
231 }
232
233 c = _citrus_EUCTW_count(cs = _citrus_EUCTW_cs(psenc->ch[0] & 0xff));
234 if (c == 0)
235 goto ilseq;
236 while (psenc->chlen < c) {
237 if (n < 1)
238 goto ilseq;
239 psenc->ch[psenc->chlen] = *s0++;
240 psenc->chlen++;
241 n--;
242 }
243
244 wchar = 0;
245 switch (cs) {
246 case 0:
247 if (psenc->ch[0] & 0x80)
248 goto ilseq;
249 wchar = psenc->ch[0] & 0xff;
250 break;
251 case 1:
252 if (!(psenc->ch[0] & 0x80) || !(psenc->ch[1] & 0x80))
253 goto ilseq;
254 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
255 wchar |= 'G' << 24;
256 break;
257 case 2:
258 if ((u_char)psenc->ch[1] < 0xa1 || 0xa7 < (u_char)psenc->ch[1])
259 goto ilseq;
260 if (!(psenc->ch[2] & 0x80) || !(psenc->ch[3] & 0x80))
261 goto ilseq;
262 wchar = ((psenc->ch[2] & 0xff) << 8) | (psenc->ch[3] & 0xff);
263 wchar |= ('G' + psenc->ch[1] - 0xa1) << 24;
264 break;
265 default:
266 goto ilseq;
267 }
268
269 *s = s0;
270 psenc->chlen = 0;
271
272 if (pwc)
273 *pwc = wchar;
274
275 if (!wchar)
276 *nresult = 0;
277 else
278 *nresult = c - chlenbak;
279
280 return (0);
281
282 ilseq:
283 psenc->chlen = 0;
284 *nresult = (size_t)-1;
285 return (EILSEQ);
286
287 restart:
288 *s = s0;
289 *nresult = (size_t)-1;
290 return (0);
291 }
292
293 static int
294 _citrus_EUCTW_wcrtomb_priv(_EUCTWEncodingInfo * __restrict ei,
295 char * __restrict s, size_t n, wchar_t wc,
296 _EUCTWState * __restrict psenc,
297 size_t * __restrict nresult)
298 {
299 wchar_t cs = wc & 0x7f000080;
300 wchar_t v;
301 int i, len, clen;
302
303 _DIAGASSERT(ei != NULL);
304 _DIAGASSERT(nresult != 0);
305 _DIAGASSERT(s != NULL);
306
307 clen = 1;
308 if (wc & 0x00007f00)
309 clen = 2;
310 if ((wc & 0x007f0000) && !(wc & 0x00800000))
311 clen = 3;
312
313 if (clen == 1 && cs == 0x00000000) {
314 /* ASCII */
315 len = 1;
316 if (n < len)
317 goto ilseq;
318 v = wc & 0x0000007f;
319 } else if (clen == 2 && cs == ('G' << 24)) {
320 /* CNS-11643-1 */
321 len = 2;
322 if (n < len)
323 goto ilseq;
324 v = wc & 0x00007f7f;
325 v |= 0x00008080;
326 } else if (clen == 2 && 'H' <= (cs >> 24) && (cs >> 24) <= 'M') {
327 /* CNS-11643-[2-7] */
328 len = 4;
329 if (n < len)
330 goto ilseq;
331 *s++ = _SS2;
332 *s++ = (cs >> 24) - 'H' + 0xa2;
333 v = wc & 0x00007f7f;
334 v |= 0x00008080;
335 } else
336 goto ilseq;
337
338 i = clen;
339 while (i-- > 0)
340 *s++ = (v >> (i << 3)) & 0xff;
341
342 *nresult = len;
343 return (0);
344
345 ilseq:
346 *nresult = (size_t)-1;
347 return (EILSEQ);
348 }
349
350 /* ----------------------------------------------------------------------
351 * public interface for ctype
352 */
353
354 _CITRUS_CTYPE_DECLS(EUCTW);
355 _CITRUS_CTYPE_DEF_OPS(EUCTW);
356
357 #include "citrus_ctype_template.h"
358