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