1 /* $NetBSD: citrus_zw.c,v 1.5 2013/05/28 16:57:56 joerg Exp $ */
4 * Copyright (c)2004, 2006 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 #if defined(LIB_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: citrus_zw.c,v 1.5 2013/05/28 16:57:56 joerg Exp $");
33 #endif /* LIB_SCCS and not lint */
35 #include <sys/types.h>
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_module.h"
49 #include "citrus_ctype.h"
50 #include "citrus_stdenc.h"
51 #include "citrus_zw.h"
53 /* ----------------------------------------------------------------------
54 * private stuffs used by templates
62 NONE
, AMBIGIOUS
, ASCII
, GB2312
74 /* for future multi-locale facility */
80 _ZWState s_mbsnrtowcs
;
83 _ZWState s_wcsnrtombs
;
88 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
89 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
91 #define _FUNCNAME(m) _citrus_ZW_##m
92 #define _ENCODING_INFO _ZWEncodingInfo
93 #define _CTYPE_INFO _ZWCTypeInfo
94 #define _ENCODING_STATE _ZWState
95 #define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX
96 #define _ENCODING_IS_STATE_DEPENDENT 1
97 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) ((_ps_)->charset != NONE)
101 _citrus_ZW_init_state(_ZWEncodingInfo
* __restrict ei
,
102 _ZWState
* __restrict psenc
)
104 /* ei my be unused */
105 _DIAGASSERT(psenc
!= NULL
);
108 psenc
->charset
= NONE
;
113 _citrus_ZW_pack_state(_ZWEncodingInfo
* __restrict ei
,
114 void *__restrict pspriv
, const _ZWState
* __restrict psenc
)
116 /* ei may be unused */
117 _DIAGASSERT(pspriv
!= NULL
);
118 _DIAGASSERT(psenc
!= NULL
);
120 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
125 _citrus_ZW_unpack_state(_ZWEncodingInfo
* __restrict ei
,
126 _ZWState
* __restrict psenc
, const void * __restrict pspriv
)
128 /* ei may be unused */
129 _DIAGASSERT(psenc
!= NULL
);
130 _DIAGASSERT(pspriv
!= NULL
);
132 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
136 _citrus_ZW_mbrtowc_priv(_ZWEncodingInfo
* __restrict ei
,
137 wchar_t * __restrict pwc
, const char **__restrict s
, size_t n
,
138 _ZWState
* __restrict psenc
, size_t * __restrict nresult
)
144 /* ei may be unused */
145 /* pwc may be null */
146 _DIAGASSERT(s
!= NULL
);
147 _DIAGASSERT(psenc
!= NULL
);
148 _DIAGASSERT(nresult
!= NULL
);
151 _citrus_ZW_init_state(ei
, psenc
);
152 *nresult
= (size_t)_ENCODING_IS_STATE_DEPENDENT
;
161 *nresult = (size_t)-2; \
165 ch = (unsigned char)*s0++; \
166 if (len++ > MB_LEN_MAX || ch > 0x7F)\
168 psenc->ch[psenc->chlen++] = ch; \
169 } while (/*CONSTCOND*/0)
172 switch (psenc
->charset
) {
174 switch (psenc
->chlen
) {
177 switch (psenc
->ch
[0]) {
178 case '\0': case '\n':
179 psenc
->charset
= NONE
;
187 ch
= (unsigned char)psenc
->ch
[0];
194 if (psenc
->chlen
!= 0)
197 ch
= (unsigned char)psenc
->ch
[0];
199 if (ch
!= '\n' && ch
!= '\0')
200 psenc
->charset
= ASCII
;
205 psenc
->charset
= AMBIGIOUS
;
209 if (psenc
->chlen
!= 0)
212 if (psenc
->ch
[0] != 'W') {
213 psenc
->charset
= ASCII
;
217 psenc
->charset
= GB2312
;
221 switch (psenc
->chlen
) {
224 ch
= (unsigned char)psenc
->ch
[0];
226 psenc
->charset
= NONE
;
230 } else if (ch
== '\n') {
231 psenc
->charset
= NONE
;
238 if (psenc
->ch
[0] == ' ') {
239 ch
= (unsigned char)psenc
->ch
[1];
243 } else if (psenc
->ch
[0] == '#') {
244 ch
= (unsigned char)psenc
->ch
[1];
246 psenc
->charset
= NONE
;
250 } else if (ch
== ' ') {
256 ch
= (unsigned char)psenc
->ch
[0];
257 if (ch
< 0x21 || ch
> 0x7E)
259 wc
= (wchar_t)(ch
<< 8);
260 ch
= (unsigned char)psenc
->ch
[1];
261 if (ch
< 0x21 || ch
> 0x7E) {
263 *nresult
= (size_t)-1;
279 *nresult
= (size_t)(wc
== 0 ? 0 : len
);
287 _citrus_ZW_wcrtomb_priv(_ZWEncodingInfo
* __restrict ei
,
288 char *__restrict s
, size_t n
, wchar_t wc
,
289 _ZWState
* __restrict psenc
, size_t * __restrict nresult
)
294 _DIAGASSERT(s
!= NULL
);
295 _DIAGASSERT(psenc
!= NULL
);
296 _DIAGASSERT(nresult
!= NULL
);
298 if (psenc
->chlen
!= 0)
300 if ((uint32_t)wc
<= 0x7F) {
301 ch
= (unsigned char)wc
;
302 switch (psenc
->charset
) {
304 if (ch
== '\0' || ch
== '\n') {
305 psenc
->ch
[psenc
->chlen
++] = ch
;
310 psenc
->ch
[psenc
->chlen
++] = 'z';
311 psenc
->ch
[psenc
->chlen
++] = 'W';
312 psenc
->ch
[psenc
->chlen
++] = ' ';
313 psenc
->ch
[psenc
->chlen
++] = ch
;
314 psenc
->charset
= GB2312
;
322 psenc
->ch
[psenc
->chlen
++] = '\n';
323 psenc
->ch
[psenc
->chlen
++] = '\0';
324 psenc
->charset
= NONE
;
325 } else if (ch
== '\n') {
326 psenc
->ch
[psenc
->chlen
++] = '#';
327 psenc
->ch
[psenc
->chlen
++] = '\n';
328 psenc
->charset
= NONE
;
330 psenc
->ch
[psenc
->chlen
++] = ' ';
331 psenc
->ch
[psenc
->chlen
++] = ch
;
337 } else if ((uint32_t)wc
<= 0x7E7E) {
338 switch (psenc
->charset
) {
343 psenc
->ch
[psenc
->chlen
++] = 'z';
344 psenc
->ch
[psenc
->chlen
++] = 'W';
345 psenc
->charset
= GB2312
;
351 ch
= (wc
>> 8) & 0xFF;
352 if (ch
< 0x21 || ch
> 0x7E)
354 psenc
->ch
[psenc
->chlen
++] = ch
;
356 if (ch
< 0x21 || ch
> 0x7E)
358 psenc
->ch
[psenc
->chlen
++] = ch
;
365 *nresult
= (size_t)-1;
368 memcpy(s
, psenc
->ch
, psenc
->chlen
);
369 *nresult
= psenc
->chlen
;
377 _citrus_ZW_put_state_reset(_ZWEncodingInfo
* __restrict ei
,
378 char * __restrict s
, size_t n
,
379 _ZWState
* __restrict psenc
, size_t * __restrict nresult
)
381 /* ei may be unused */
382 _DIAGASSERT(s
!= NULL
);
383 _DIAGASSERT(psenc
!= NULL
);
384 _DIAGASSERT(nresult
!= NULL
);
386 if (psenc
->chlen
!= 0)
388 switch (psenc
->charset
) {
392 psenc
->ch
[psenc
->chlen
++] = '\n';
393 psenc
->charset
= NONE
;
396 *nresult
= psenc
->chlen
;
397 if (psenc
->chlen
> 0) {
398 memcpy(s
, psenc
->ch
, psenc
->chlen
);
411 _citrus_ZW_stdenc_get_state_desc_generic(_ZWEncodingInfo
* __restrict ei
,
412 _ZWState
* __restrict psenc
, int * __restrict rstate
)
414 /* ei may be unused */
415 _DIAGASSERT(psenc
!= NULL
);
416 _DIAGASSERT(rstate
!= NULL
);
418 switch (psenc
->charset
) {
420 if (psenc
->chlen
!= 0)
422 *rstate
= _STDENC_SDGEN_INITIAL
;
425 if (psenc
->chlen
!= 0)
427 *rstate
= _STDENC_SDGEN_INCOMPLETE_SHIFT
;
431 switch (psenc
->chlen
) {
433 *rstate
= _STDENC_SDGEN_STABLE
;
436 *rstate
= (psenc
->ch
[0] == '#')
437 ? _STDENC_SDGEN_INCOMPLETE_SHIFT
438 : _STDENC_SDGEN_INCOMPLETE_CHAR
;
452 _citrus_ZW_stdenc_wctocs(_ZWEncodingInfo
* __restrict ei
,
453 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
455 /* ei seems to be unused */
456 _DIAGASSERT(csid
!= NULL
);
457 _DIAGASSERT(idx
!= NULL
);
459 *csid
= (_csid_t
)(wc
<= 0x7FU
) ? 0 : 1;
467 _citrus_ZW_stdenc_cstowc(_ZWEncodingInfo
* __restrict ei
,
468 wchar_t * __restrict wc
, _csid_t csid
, _index_t idx
)
470 /* ei seems to be unused */
471 _DIAGASSERT(wc
!= NULL
);
486 _citrus_ZW_encoding_module_uninit(_ZWEncodingInfo
*ei
)
492 _citrus_ZW_encoding_module_init(_ZWEncodingInfo
* __restrict ei
,
493 const void *__restrict var
, size_t lenvar
)
498 /* ----------------------------------------------------------------------
499 * public interface for ctype
502 _CITRUS_CTYPE_DECLS(ZW
);
503 _CITRUS_CTYPE_DEF_OPS(ZW
);
505 #include "citrus_ctype_template.h"
507 /* ----------------------------------------------------------------------
508 * public interface for stdenc
511 _CITRUS_STDENC_DECLS(ZW
);
512 _CITRUS_STDENC_DEF_OPS(ZW
);
514 #include "citrus_stdenc_template.h"