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