citrus_euc.c revision 1.4 1 /* $NetBSD: citrus_euc.c,v 1.4 2002/03/28 10:29:11 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) 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Paul Borman at Krystal Technologies.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 __RCSID("$NetBSD: citrus_euc.c,v 1.4 2002/03/28 10:29:11 yamt Exp $");
68 #endif /* LIBC_SCCS and not lint */
69
70 #include <assert.h>
71 #include <errno.h>
72 #include <string.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <stddef.h>
76 #include <locale.h>
77 #include <wchar.h>
78 #include <sys/types.h>
79 #include <limits.h>
80 #include "citrus_module.h"
81 #include "citrus_ctype.h"
82 #include "citrus_euc.h"
83
84
85 /* ----------------------------------------------------------------------
86 * private stuffs used by templates
87 */
88
89 typedef struct {
90 char ch[3];
91 int chlen;
92 } _EUCState;
93
94 typedef struct {
95 unsigned count[4];
96 wchar_t bits[4];
97 wchar_t mask;
98 unsigned mb_cur_max;
99 } _EUCEncodingInfo;
100
101 typedef struct {
102 _EUCEncodingInfo ei;
103 struct {
104 /* for future multi-locale facility */
105 _EUCState s_mblen;
106 _EUCState s_mbrlen;
107 _EUCState s_mbrtowc;
108 _EUCState s_mbtowc;
109 _EUCState s_mbsrtowcs;
110 _EUCState s_wcrtomb;
111 _EUCState s_wcsrtombs;
112 _EUCState s_wctomb;
113 } states;
114 } _EUCCTypeInfo;
115
116 #define _SS2 0x008e
117 #define _SS3 0x008f
118
119 #define _TO_EI(_cl_) ((_EUCEncodingInfo *)(_cl_))
120 #define _TO_CEI(_cl_) ((_EUCCTypeInfo *)(_cl_))
121 #define _TO_STATE(_ps_) ((_EUCState *)(_ps_))
122 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
123 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
124
125 #define _FUNCNAME(m) _citrus_EUC_##m
126 #define _ENCODING_INFO _EUCEncodingInfo
127 #define _CTYPE_INFO _EUCCTypeInfo
128 #define _ENCODING_STATE _EUCState
129 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
130 #define _ENCODING_IS_STATE_DEPENDENT 0
131 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
132
133
134 static __inline int
135 _citrus_EUC_cs(unsigned int c)
136 {
137 c &= 0xff;
138
139 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
140 }
141
142 static __inline int
143 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei,
144 const void *var, size_t lenvar)
145 {
146 const char *v, *e;
147 int x;
148
149 /* parse variable string */
150 if (!var)
151 return (EFTYPE);
152
153 v = (const char *) var;
154
155 while (*v == ' ' || *v == '\t')
156 ++v;
157
158 ei->mb_cur_max = 1;
159 for (x = 0; x < 4; ++x) {
160 ei->count[x] = (int) strtol(v, (char **)&e, 0);
161 if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) {
162 return (EFTYPE);
163 }
164 if (ei->mb_cur_max < ei->count[x])
165 ei->mb_cur_max = ei->count[x];
166 while (*v == ' ' || *v == '\t')
167 ++v;
168 ei->bits[x] = (int) strtol(v, (char **)&e, 0);
169 if (v == e || !(v = e)) {
170 return (EFTYPE);
171 }
172 while (*v == ' ' || *v == '\t')
173 ++v;
174 }
175 ei->mask = (int)strtol(v, (char **)&e, 0);
176 if (v == e || !(v = e)) {
177 return (EFTYPE);
178 }
179
180 return 0;
181 }
182
183
184 static __inline void
185 /*ARGSUSED*/
186 _citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s)
187 {
188 memset(s, 0, sizeof(*s));
189 }
190
191 static __inline void
192 /*ARGSUSED*/
193 _citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s)
194 {
195 memcpy(pspriv, (const void *)s, sizeof(*s));
196 }
197
198 static __inline void
199 /*ARGSUSED*/
200 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s,
201 const void *pspriv)
202 {
203 memcpy((void *)s, pspriv, sizeof(*s));
204 }
205
206 static int
207 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s,
208 size_t n, _EUCState *psenc, size_t *nresult)
209 {
210 wchar_t wchar;
211 int c, cs, len;
212 int chlenbak;
213 const char *s0, *s1 = NULL;
214
215 _DIAGASSERT(nresult != 0);
216 _DIAGASSERT(ei != NULL);
217 _DIAGASSERT(psenc != NULL);
218 _DIAGASSERT(s != NULL);
219
220 s0 = *s;
221
222 if (s0 == NULL) {
223 _citrus_EUC_init_state(ei, psenc);
224 *nresult = 0; /* state independent */
225 return (0);
226 }
227
228 chlenbak = psenc->chlen;
229
230 /* make sure we have the first byte in the buffer */
231 switch (psenc->chlen) {
232 case 0:
233 if (n < 1)
234 goto restart;
235 psenc->ch[0] = *s0++;
236 psenc->chlen = 1;
237 n--;
238 break;
239 case 1:
240 case 2:
241 break;
242 default:
243 /* illgeal state */
244 goto encoding_error;
245 }
246
247 c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
248 if (c == 0)
249 goto encoding_error;
250 while (psenc->chlen < c) {
251 if (n < 1)
252 goto restart;
253 psenc->ch[psenc->chlen] = *s0++;
254 psenc->chlen++;
255 n--;
256 }
257 *s = s0;
258
259 switch (cs) {
260 case 3:
261 case 2:
262 /* skip SS2/SS3 */
263 len = c - 1;
264 s1 = &psenc->ch[1];
265 break;
266 case 1:
267 case 0:
268 len = c;
269 s1 = &psenc->ch[0];
270 break;
271 }
272 wchar = 0;
273 while (len-- > 0)
274 wchar = (wchar << 8) | (*s1++ & 0xff);
275 wchar = (wchar & ~ei->mask) | ei->bits[cs];
276
277 psenc->chlen = 0;
278 if (pwc)
279 *pwc = wchar;
280
281 if (!wchar) {
282 *nresult = 0;
283 } else {
284 *nresult = (size_t)(c - chlenbak);
285 }
286
287 return 0;
288
289 encoding_error:
290 psenc->chlen = 0;
291 *nresult = (size_t)-1;
292 return (EILSEQ);
293
294 restart:
295 *nresult = (size_t)-2;
296 *s = s0;
297 return (0);
298 }
299
300 static int
301 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
302 _EUCState *psenc, size_t *nresult)
303 {
304 wchar_t m, nm;
305 int cs, i;
306
307 _DIAGASSERT(ei != NULL);
308 _DIAGASSERT(nresult != 0);
309 _DIAGASSERT(s != NULL);
310
311 m = wc & ei->mask;
312 nm = wc & ~m;
313
314 for (cs = 0;
315 cs < sizeof(ei->count)/sizeof(ei->count[0]);
316 cs++) {
317 if (m == ei->bits[cs])
318 break;
319 }
320 /* fallback case - not sure if it is necessary */
321 if (cs == sizeof(ei->count)/sizeof(ei->count[0]))
322 cs = 1;
323
324 i = ei->count[cs];
325 if (n < i)
326 goto ilseq;
327 m = (cs % 2) ? 0x80 : 0x00;
328 switch (cs) {
329 case 2:
330 *s++ = _SS2;
331 i--;
332 break;
333 case 3:
334 *s++ = _SS3;
335 i--;
336 break;
337 }
338
339 while (i-- > 0)
340 *s++ = ((nm >> (i << 3)) & 0xff) | m;
341
342 *nresult = (size_t)ei->count[cs];
343 return 0;
344
345 ilseq:
346 *nresult = (size_t)-1;
347 return EILSEQ; /*XXX*/
348 }
349
350 static int
351 /*ARGSUSED*/
352 _citrus_EUC_stdencoding_init(_EUCEncodingInfo * __restrict ei,
353 const void * __restrict var, size_t lenvar)
354 {
355
356 _DIAGASSERT(ei != NULL);
357
358 return (_citrus_EUC_parse_variable(ei, var, lenvar));
359 }
360
361 static void
362 /*ARGSUSED*/
363 _citrus_EUC_stdencoding_uninit(_EUCEncodingInfo * __restrict ei)
364 {
365 }
366
367 /* ----------------------------------------------------------------------
368 * public interface for ctype
369 */
370
371 _CITRUS_CTYPE_DECLS(EUC);
372 _CITRUS_CTYPE_DEF_OPS(EUC);
373
374 #include "citrus_ctype_template.h"
375