2 /* $NetBSD: citrus_hz.c,v 1.2 2008/06/14 16:01:07 tnozaki Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2004, 2006 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_bcs.h"
49 #include "citrus_module.h"
50 #include "citrus_stdenc.h"
52 #include "citrus_hz.h"
53 #include "citrus_prop.h"
58 * CTRL/ASCII 00000000 00000000 00000000 gxxxxxxx
59 * GB2312 00000000 00000000 0xxxxxxx gxxxxxxx
60 * 94/96*n (~M) 0mmmmmmm 0xxxxxxx 0xxxxxxx gxxxxxxx
63 #define ESCAPE_CHAR '~'
66 CTRL = 0, ASCII = 1, GB2312 = 2, CS94 = 3, CS96 = 4
75 static const range_t ranges[] = {
76 #define RANGE(start, end) { start, end, (end - start) + 1 }
77 /* CTRL */ RANGE(0x00, 0x1F),
78 /* ASCII */ RANGE(0x20, 0x7F),
79 /* GB2312 */ RANGE(0x21, 0x7E),
80 /* CS94 */ RANGE(0x21, 0x7E),
81 /* CS96 */ RANGE(0x20, 0x7F),
85 typedef struct escape_t escape_t;
93 typedef TAILQ_HEAD(escape_list, escape_t) escape_list;
95 TAILQ_ENTRY(escape_t) entry;
102 #define GL(escape) ((escape)->left)
103 #define GR(escape) ((escape)->right)
104 #define SET(escape) ((escape)->set)
105 #define ESC(escape) ((escape)->ch)
106 #define INIT(escape) (TAILQ_FIRST(SET(escape)))
108 static __inline escape_t *
109 find_escape(escape_list *set, int ch)
113 TAILQ_FOREACH(escape, set, entry) {
114 if (ESC(escape) == ch)
128 #define E0SET(ei) (&(ei)->e0)
129 #define E1SET(ei) (&(ei)->e1)
130 #define INIT0(ei) (TAILQ_FIRST(E0SET(ei)))
131 #define INIT1(ei) (TAILQ_FIRST(E1SET(ei)))
139 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
140 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
142 #define _FUNCNAME(m) _citrus_HZ_##m
143 #define _ENCODING_INFO _HZEncodingInfo
144 #define _ENCODING_STATE _HZState
145 #define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX
146 #define _ENCODING_IS_STATE_DEPENDENT 1
147 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) ((_ps_)->inuse == NULL)
150 _citrus_HZ_init_state(_HZEncodingInfo * __restrict ei,
151 _HZState * __restrict psenc)
155 psenc->inuse = INIT0(ei);
161 _citrus_HZ_pack_state(_HZEncodingInfo * __restrict ei __unused,
162 void *__restrict pspriv, const _HZState * __restrict psenc)
165 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
170 _citrus_HZ_unpack_state(_HZEncodingInfo * __restrict ei __unused,
171 _HZState * __restrict psenc, const void * __restrict pspriv)
174 memcpy((void *)psenc, pspriv, sizeof(*psenc));
179 _citrus_HZ_mbrtowc_priv(_HZEncodingInfo * __restrict ei,
180 wchar_t * __restrict pwc, char ** __restrict s, size_t n,
181 _HZState * __restrict psenc, size_t * __restrict nresult)
183 escape_t *candidate, *init;
185 const range_t *range;
188 int bit, ch, head, len, tail;
191 _citrus_HZ_init_state(ei, psenc);
196 if (psenc->chlen < 0 || psenc->inuse == NULL)
200 bit = head = tail = 0;
202 for (len = 0; len <= MB_LEN_MAX;) {
203 if (psenc->chlen == tail) {
206 *nresult = (size_t)-2;
209 psenc->ch[psenc->chlen++] = *s0++;
212 ch = (unsigned char)psenc->ch[tail++];
214 if ((ch & ~0x80) <= 0x1F) {
215 if (psenc->inuse != INIT0(ei))
221 graphic = GR(psenc->inuse);
225 graphic = GL(psenc->inuse);
226 if (ch == ESCAPE_CHAR)
232 } else if (tail == 2 && psenc->ch[0] == ESCAPE_CHAR) {
233 if (tail < psenc->chlen)
235 if (ch == ESCAPE_CHAR) {
237 } else if (ch == '\n') {
238 if (psenc->inuse != INIT0(ei))
240 tail = psenc->chlen = 0;
245 if (psenc->inuse == init) {
247 } else if (INIT(psenc->inuse) == init) {
252 if (candidate == NULL) {
253 candidate = find_escape(
254 SET(psenc->inuse), ch);
255 if (candidate == NULL) {
262 psenc->inuse = candidate;
263 tail = psenc->chlen = 0;
266 } else if (ch & 0x80) {
267 if (graphic != GR(psenc->inuse))
271 if (graphic != GL(psenc->inuse))
274 range = &ranges[(size_t)graphic->charset];
275 if (range->start > ch || range->end < ch)
279 if (graphic->length == (tail - head)) {
280 if (graphic->charset > GB2312)
281 bit |= ESC(psenc->inuse) << 24;
286 *nresult = (size_t)-1;
289 if (tail < psenc->chlen)
295 *nresult = (wc == 0) ? 0 : len;
301 _citrus_HZ_wcrtomb_priv(_HZEncodingInfo * __restrict ei,
302 char * __restrict s, size_t n, wchar_t wc,
303 _HZState * __restrict psenc, size_t * __restrict nresult)
305 escape_t *candidate, *init;
307 const range_t *range;
311 if (psenc->chlen != 0 || psenc->inuse == NULL)
319 if ((uint32_t)wc <= 0x1F) {
320 candidate = INIT0(ei);
321 graphic = (bit == 0) ? candidate->left : candidate->right;
324 range = &ranges[(size_t)CTRL];
326 } else if ((uint32_t)wc <= 0x7F) {
330 candidate = graphic->escape;
331 range = &ranges[(size_t)graphic->charset];
332 len = graphic->length;
333 } else if ((uint32_t)wc <= 0x7F7F) {
334 graphic = ei->gb2312;
337 candidate = graphic->escape;
338 range = &ranges[(size_t)graphic->charset];
339 len = graphic->length;
341 ch = (wc >> 24) & 0xFF;
342 candidate = find_escape(E0SET(ei), ch);
343 if (candidate == NULL) {
344 candidate = find_escape(E1SET(ei), ch);
345 if (candidate == NULL)
349 graphic = (bit == 0) ? candidate->left : candidate->right;
352 range = &ranges[(size_t)graphic->charset];
353 len = graphic->length;
355 if (psenc->inuse != candidate) {
357 if (SET(psenc->inuse) == SET(candidate)) {
358 if (INIT(psenc->inuse) != init ||
359 psenc->inuse == init || candidate == init)
361 } else if (candidate == (init = INIT(candidate))) {
368 psenc->ch[psenc->chlen++] = ESCAPE_CHAR;
369 psenc->ch[psenc->chlen++] = ESC(init);
374 psenc->ch[psenc->chlen++] = ESCAPE_CHAR;
375 psenc->ch[psenc->chlen++] = ESC(candidate);
376 psenc->inuse = candidate;
381 ch = (wc >> (len * 8)) & 0xFF;
382 if (range->start > ch || range->end < ch)
384 psenc->ch[psenc->chlen++] = ch | bit;
386 memcpy(s, psenc->ch, psenc->chlen);
387 *nresult = psenc->chlen;
393 *nresult = (size_t)-1;
398 _citrus_HZ_put_state_reset(_HZEncodingInfo * __restrict ei,
399 char * __restrict s, size_t n, _HZState * __restrict psenc,
400 size_t * __restrict nresult)
404 if (psenc->chlen != 0 || psenc->inuse == NULL)
406 candidate = INIT0(ei);
407 if (psenc->inuse != candidate) {
411 psenc->ch[psenc->chlen++] = ESCAPE_CHAR;
412 psenc->ch[psenc->chlen++] = ESC(candidate);
416 if (psenc->chlen > 0)
417 memcpy(s, psenc->ch, psenc->chlen);
418 *nresult = psenc->chlen;
419 _citrus_HZ_init_state(ei, psenc);
425 _citrus_HZ_stdenc_get_state_desc_generic(_HZEncodingInfo * __restrict ei,
426 _HZState * __restrict psenc, int * __restrict rstate)
429 if (psenc->chlen < 0 || psenc->inuse == NULL)
431 *rstate = (psenc->chlen == 0)
432 ? ((psenc->inuse == INIT0(ei))
433 ? _STDENC_SDGEN_INITIAL
434 : _STDENC_SDGEN_STABLE)
435 : ((psenc->ch[0] == ESCAPE_CHAR)
436 ? _STDENC_SDGEN_INCOMPLETE_SHIFT
437 : _STDENC_SDGEN_INCOMPLETE_CHAR);
444 _citrus_HZ_stdenc_wctocs(_HZEncodingInfo * __restrict ei __unused,
445 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
454 if ((uint32_t)wc <= 0x7F) {
455 *csid = (_csid_t)bit;
457 } else if ((uint32_t)wc <= 0x7F7F) {
458 *csid = (_csid_t)(bit | 0x8000);
461 *csid = (_index_t)(wc & ~0x00FFFF7F);
462 *idx = (_csid_t)(wc & 0x00FFFF7F);
470 _citrus_HZ_stdenc_cstowc(_HZEncodingInfo * __restrict ei __unused,
471 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
478 *wc |= (wchar_t)0x80;
484 *wc |= (wchar_t)csid;
491 _citrus_HZ_encoding_module_uninit(_HZEncodingInfo *ei)
495 while ((escape = TAILQ_FIRST(E0SET(ei))) != NULL) {
496 TAILQ_REMOVE(E0SET(ei), escape, entry);
501 while ((escape = TAILQ_FIRST(E1SET(ei))) != NULL) {
502 TAILQ_REMOVE(E1SET(ei), escape, entry);
510 _citrus_HZ_parse_char(void *context, const char *name __unused, const char *s)
515 p = (void **)context;
516 escape = (escape_t *)p[0];
517 if (escape->ch != '\0')
520 if (escape->ch == ESCAPE_CHAR || *s != '\0')
527 _citrus_HZ_parse_graphic(void *context, const char *name, const char *s)
534 p = (void **)context;
535 escape = (escape_t *)p[0];
536 ei = (_HZEncodingInfo *)p[1];
537 graphic = calloc(1, sizeof(*graphic));
540 if (strcmp("GL", name) == 0) {
541 if (GL(escape) != NULL)
543 GL(escape) = graphic;
544 } else if (strcmp("GR", name) == 0) {
545 if (GR(escape) != NULL)
547 GR(escape) = graphic;
553 graphic->escape = escape;
554 if (_bcs_strncasecmp("ASCII", s, 5) == 0) {
557 graphic->charset = ASCII;
561 } else if (_bcs_strncasecmp("GB2312", s, 6) == 0) {
564 graphic->charset = GB2312;
566 ei->gb2312 = graphic;
568 } else if (strncmp("94*", s, 3) == 0)
569 graphic->charset = CS94;
570 else if (strncmp("96*", s, 3) == 0)
571 graphic->charset = CS96;
576 case '1': case '2': case '3':
577 graphic->length = (size_t)(*s - '0');
587 static const _citrus_prop_hint_t escape_hints[] = {
588 _CITRUS_PROP_HINT_STR("CH", &_citrus_HZ_parse_char),
589 _CITRUS_PROP_HINT_STR("GL", &_citrus_HZ_parse_graphic),
590 _CITRUS_PROP_HINT_STR("GR", &_citrus_HZ_parse_graphic),
591 _CITRUS_PROP_HINT_END
595 _citrus_HZ_parse_escape(void *context, const char *name, const char *s)
601 ei = (_HZEncodingInfo *)context;
602 escape = calloc(1, sizeof(*escape));
605 if (strcmp("0", name) == 0) {
606 escape->set = E0SET(ei);
607 TAILQ_INSERT_TAIL(E0SET(ei), escape, entry);
608 } else if (strcmp("1", name) == 0) {
609 escape->set = E1SET(ei);
610 TAILQ_INSERT_TAIL(E1SET(ei), escape, entry);
615 p[0] = (void *)escape;
617 return (_citrus_prop_parse_variable(
618 escape_hints, (void *)&p[0], s, strlen(s)));
621 static const _citrus_prop_hint_t root_hints[] = {
622 _CITRUS_PROP_HINT_STR("0", &_citrus_HZ_parse_escape),
623 _CITRUS_PROP_HINT_STR("1", &_citrus_HZ_parse_escape),
624 _CITRUS_PROP_HINT_END
628 _citrus_HZ_encoding_module_init(_HZEncodingInfo * __restrict ei,
629 const void * __restrict var, size_t lenvar)
633 memset(ei, 0, sizeof(*ei));
634 TAILQ_INIT(E0SET(ei));
635 TAILQ_INIT(E1SET(ei));
636 errnum = _citrus_prop_parse_variable(
637 root_hints, (void *)ei, var, lenvar);
639 _citrus_HZ_encoding_module_uninit(ei);
643 /* ----------------------------------------------------------------------
644 * public interface for stdenc
647 _CITRUS_STDENC_DECLS(HZ);
648 _CITRUS_STDENC_DEF_OPS(HZ);
650 #include "citrus_stdenc_template.h"