1 /* $NetBSD: citrus_euc.c,v 1.11 2006/03/19 01:25:44 christos Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_euc.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2002 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Paul Borman at Krystal Technologies.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
62 #include <sys/types.h>
73 #include "citrus_namespace.h"
74 #include "citrus_types.h"
75 #include "citrus_module.h"
76 #include "citrus_ctype.h"
77 #include "citrus_stdenc.h"
78 #include "citrus_euc.h"
81 /* ----------------------------------------------------------------------
82 * private stuffs used by templates
100 /* for future multi-locale facility */
105 _EUCState s_mbsrtowcs;
107 _EUCState s_wcsrtombs;
115 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
116 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
118 #define _FUNCNAME(m) _citrus_EUC_##m
119 #define _ENCODING_INFO _EUCEncodingInfo
120 #define _CTYPE_INFO _EUCCTypeInfo
121 #define _ENCODING_STATE _EUCState
122 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
123 #define _ENCODING_IS_STATE_DEPENDENT 0
124 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
128 _citrus_EUC_cs(unsigned int c)
132 return ((c & 0x80) ? c == _SS3 ? 3 : c == _SS2 ? 2 : 1 : 0);
136 _citrus_EUC_parse_variable(_EUCEncodingInfo *ei,
137 const void *var, size_t lenvar)
142 /* parse variable string */
146 v = (const char *) var;
148 while (*v == ' ' || *v == '\t')
152 for (x = 0; x < 4; ++x) {
153 ei->count[x] = (int) strtol(v, (char **)&e, 0);
154 if (v == e || !(v = e) || ei->count[x]<1 || ei->count[x]>4) {
157 if (ei->mb_cur_max < ei->count[x])
158 ei->mb_cur_max = ei->count[x];
159 while (*v == ' ' || *v == '\t')
161 ei->bits[x] = (int) strtol(v, (char **)&e, 0);
162 if (v == e || !(v = e)) {
165 while (*v == ' ' || *v == '\t')
168 ei->mask = (int)strtol(v, (char **)&e, 0);
169 if (v == e || !(v = e)) {
179 _citrus_EUC_init_state(_EUCEncodingInfo *ei, _EUCState *s)
181 memset(s, 0, sizeof(*s));
186 _citrus_EUC_pack_state(_EUCEncodingInfo *ei, void *pspriv, const _EUCState *s)
188 memcpy(pspriv, (const void *)s, sizeof(*s));
193 _citrus_EUC_unpack_state(_EUCEncodingInfo *ei, _EUCState *s,
196 memcpy((void *)s, pspriv, sizeof(*s));
200 _citrus_EUC_mbrtowc_priv(_EUCEncodingInfo *ei, wchar_t *pwc, const char **s,
201 size_t n, _EUCState *psenc, size_t *nresult)
206 const char *s0, *s1 = NULL;
208 _DIAGASSERT(nresult != 0);
209 _DIAGASSERT(ei != NULL);
210 _DIAGASSERT(psenc != NULL);
211 _DIAGASSERT(s != NULL);
216 _citrus_EUC_init_state(ei, psenc);
217 *nresult = 0; /* state independent */
221 chlenbak = psenc->chlen;
223 /* make sure we have the first byte in the buffer */
224 switch (psenc->chlen) {
228 psenc->ch[0] = *s0++;
240 c = ei->count[cs = _citrus_EUC_cs(psenc->ch[0] & 0xff)];
243 while (psenc->chlen < c) {
246 psenc->ch[psenc->chlen] = *s0++;
269 wchar = (wchar << 8) | (*s1++ & 0xff);
270 wchar = (wchar & ~ei->mask) | ei->bits[cs];
279 *nresult = (size_t)(c - chlenbak);
286 *nresult = (size_t)-1;
290 *nresult = (size_t)-2;
296 _citrus_EUC_wcrtomb_priv(_EUCEncodingInfo *ei, char *s, size_t n, wchar_t wc,
297 _EUCState *psenc, size_t *nresult)
302 _DIAGASSERT(ei != NULL);
303 _DIAGASSERT(nresult != 0);
304 _DIAGASSERT(s != NULL);
310 cs < sizeof(ei->count)/sizeof(ei->count[0]);
312 if (m == ei->bits[cs])
315 /* fallback case - not sure if it is necessary */
316 if (cs == sizeof(ei->count)/sizeof(ei->count[0]))
324 m = (cs) ? 0x80 : 0x00;
337 *s++ = ((nm >> (i << 3)) & 0xff) | m;
339 *nresult = (size_t)ei->count[cs];
343 *nresult = (size_t)-1;
349 _citrus_EUC_stdenc_wctocs(_EUCEncodingInfo * __restrict ei,
350 _csid_t * __restrict csid,
351 _index_t * __restrict idx, wchar_t wc)
355 _DIAGASSERT(ei != NULL && csid != NULL && idx != NULL);
360 *csid = (_citrus_csid_t)m;
361 *idx = (_citrus_index_t)nm;
368 _citrus_EUC_stdenc_cstowc(_EUCEncodingInfo * __restrict ei,
369 wchar_t * __restrict wc,
370 _csid_t csid, _index_t idx)
373 _DIAGASSERT(ei != NULL && wc != NULL);
375 if ((csid & ~ei->mask) != 0 || (idx & ei->mask) != 0)
378 *wc = (wchar_t)csid | (wchar_t)idx;
385 _citrus_EUC_stdenc_get_state_desc_generic(_EUCEncodingInfo * __restrict ei,
386 _EUCState * __restrict psenc,
387 int * __restrict rstate)
390 if (psenc->chlen == 0)
391 *rstate = _STDENC_SDGEN_INITIAL;
393 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
400 _citrus_EUC_encoding_module_init(_EUCEncodingInfo * __restrict ei,
401 const void * __restrict var, size_t lenvar)
404 _DIAGASSERT(ei != NULL);
406 return (_citrus_EUC_parse_variable(ei, var, lenvar));
411 _citrus_EUC_encoding_module_uninit(_EUCEncodingInfo * __restrict ei)
415 /* ----------------------------------------------------------------------
416 * public interface for ctype
419 _CITRUS_CTYPE_DECLS(EUC);
420 _CITRUS_CTYPE_DEF_OPS(EUC);
422 #include "citrus_ctype_template.h"
424 /* ----------------------------------------------------------------------
425 * public interface for stdenc
428 _CITRUS_STDENC_DECLS(EUC);
429 _CITRUS_STDENC_DEF_OPS(EUC);
431 #include "citrus_stdenc_template.h"