1 /* $NetBSD: citrus_euc.c,v 1.14 2009/01/11 02:46:24 christos 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 * SPDX-License-Identifier: BSD-3-Clause
31 *
32 * Copyright (c) 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * This code is derived from software contributed to Berkeley by
36 * Paul Borman at Krystal Technologies.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
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
_citrus_EUC_cs(unsigned int c)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
_citrus_EUC_parse_variable(_EUCEncodingInfo * ei,const void * var,size_t lenvar __unused)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*/
_citrus_EUC_init_state(_EUCEncodingInfo * ei __unused,_EUCState * s)166 _citrus_EUC_init_state(_EUCEncodingInfo *ei __unused, _EUCState *s)
167 {
168
169 memset(s, 0, sizeof(*s));
170 }
171
172 #if 0
173 static __inline void
174 /*ARGSUSED*/
175 _citrus_EUC_pack_state(_EUCEncodingInfo *ei __unused, void *pspriv,
176 const _EUCState *s)
177 {
178
179 memcpy(pspriv, (const void *)s, sizeof(*s));
180 }
181
182 static __inline void
183 /*ARGSUSED*/
184 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei __unused, _EUCState *s,
185 const void *pspriv)
186 {
187
188 memcpy((void *)s, pspriv, sizeof(*s));
189 }
190 #endif
191
192 static int
_citrus_EUC_mbrtowc_priv(_EUCEncodingInfo * ei,wchar_t * pwc,char ** s,size_t n,_EUCState * psenc,size_t * nresult)193 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, char **s,
194 size_t n, _EUCState *psenc, size_t *nresult)
195 {
196 wchar_t wchar;
197 int c, chlenbak, cs, len;
198 char *s0, *s1 = NULL;
199
200 s0 = *s;
201
202 if (s0 == NULL) {
203 _citrus_EUC_init_state(ei, psenc);
204 *nresult = 0; /* state independent */
205 return (0);
206 }
207
208 chlenbak = psenc->chlen;
209
210 /* make sure we have the first byte in the buffer */
211 switch (psenc->chlen) {
212 case 0:
213 if (n < 1)
214 goto restart;
215 psenc->ch[0] = *s0++;
216 psenc->chlen = 1;
217 n--;
218 break;
219 case 1:
220 case 2:
221 break;
222 default:
223 /* illgeal state */
224 goto encoding_error;
225 }
226
227 c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
228 if (c == 0)
229 goto encoding_error;
230 while (psenc->chlen < c) {
231 if (n < 1)
232 goto restart;
233 psenc->ch[psenc->chlen] = *s0++;
234 psenc->chlen++;
235 n--;
236 }
237 *s = s0;
238
239 switch (cs) {
240 case 3:
241 case 2:
242 /* skip SS2/SS3 */
243 len = c - 1;
244 s1 = &psenc->ch[1];
245 break;
246 case 1:
247 case 0:
248 len = c;
249 s1 = &psenc->ch[0];
250 break;
251 default:
252 goto encoding_error;
253 }
254 wchar = 0;
255 while (len-- > 0)
256 wchar = (wchar << 8) | (*s1++ & 0xff);
257 wchar = (wchar & ~ei->mask) | ei->bits[cs];
258
259 psenc->chlen = 0;
260 if (pwc)
261 *pwc = wchar;
262 *nresult = wchar ? (size_t)(c - chlenbak) : 0;
263 return (0);
264
265 encoding_error:
266 psenc->chlen = 0;
267 *nresult = (size_t)-1;
268 return (EILSEQ);
269
270 restart:
271 *nresult = (size_t)-2;
272 *s = s0;
273 return (0);
274 }
275
276 static int
_citrus_EUC_wcrtomb_priv(_EUCEncodingInfo * ei,char * s,size_t n,wchar_t wc,_EUCState * psenc __unused,size_t * nresult)277 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
278 _EUCState *psenc __unused, size_t *nresult)
279 {
280 wchar_t m, nm;
281 unsigned int cs;
282 int ret;
283 short i;
284
285 m = wc & ei->mask;
286 nm = wc & ~m;
287
288 for (cs = 0; cs < sizeof(ei->count) / sizeof(ei->count[0]); cs++)
289 if (m == ei->bits[cs])
290 break;
291 /* fallback case - not sure if it is necessary */
292 if (cs == sizeof(ei->count) / sizeof(ei->count[0]))
293 cs = 1;
294
295 i = ei->count[cs];
296 if (n < (unsigned)i) {
297 ret = E2BIG;
298 goto err;
299 }
300 m = (cs) ? 0x80 : 0x00;
301 switch (cs) {
302 case 2:
303 *s++ = _SS2;
304 i--;
305 break;
306 case 3:
307 *s++ = _SS3;
308 i--;
309 break;
310 }
311
312 while (i-- > 0)
313 *s++ = ((nm >> (i << 3)) & 0xff) | m;
314
315 *nresult = (size_t)ei->count[cs];
316 return (0);
317
318 err:
319 *nresult = (size_t)-1;
320 return (ret);
321 }
322
323 static __inline int
324 /*ARGSUSED*/
_citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)325 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
326 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
327 {
328 wchar_t m, nm;
329
330 m = wc & ei->mask;
331 nm = wc & ~m;
332
333 *csid = (_citrus_csid_t)m;
334 *idx = (_citrus_index_t)nm;
335
336 return (0);
337 }
338
339 static __inline int
340 /*ARGSUSED*/
_citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,wchar_t * __restrict wc,_csid_t csid,_index_t idx)341 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
342 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
343 {
344
345 if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
346 return (EINVAL);
347
348 *wc = (wchar_t)csid | (wchar_t)idx;
349
350 return (0);
351 }
352
353 static __inline int
354 /*ARGSUSED*/
_citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei __unused,_EUCState * __restrict psenc,int * __restrict rstate)355 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei __unused,
356 _EUCState * __restrict psenc, int * __restrict rstate)
357 {
358
359 *rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
360 _STDENC_SDGEN_INCOMPLETE_CHAR;
361 return (0);
362 }
363
364 static int
365 /*ARGSUSED*/
_citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,const void * __restrict var,size_t lenvar)366 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
367 const void * __restrict var, size_t lenvar)
368 {
369
370 return (_citrus_EUC_parse_variable(ei, var, lenvar));
371 }
372
373 static void
374 /*ARGSUSED*/
_citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei __unused)375 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei __unused)
376 {
377
378 }
379
380 /* ----------------------------------------------------------------------
381 * public interface for stdenc
382 */
383
384 _CITRUS_STDENC_DECLS(EUC);
385 _CITRUS_STDENC_DEF_OPS(EUC);
386
387 #include "citrus_stdenc_template.h"
388