1 /* $NetBSD: citrus_iso2022.c,v 1.18 2007/11/21 14:19:32 tnozaki Exp $ */
4 * Copyright (c)1999, 2002 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
28 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $
31 #include <sys/types.h>
42 #include "citrus_namespace.h"
43 #include "citrus_types.h"
44 #include "citrus_module.h"
45 #include "citrus_ctype.h"
46 #include "citrus_stdenc.h"
47 #include "citrus_iso2022.h"
50 /* ----------------------------------------------------------------------
51 * private stuffs used by templates
57 * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx
58 * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx
59 * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx
60 * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx
61 * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx
62 * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx
63 * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx
64 * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx
65 * 94x94 charset (ESC & V ESC $ ( F)
66 * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx
67 * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx
68 * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx
69 * reserved for UCS4 co-existence (UCS4 is 31bit encoding thanks to mohta bit)
70 * 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
77 #define CS94MULTI (2U)
78 #define CS96MULTI (3U)
85 static const _ISO2022Charset ascii = { CS94, 'B', '\0', '\0' };
86 static const _ISO2022Charset iso88591 = { CS96, 'A', '\0', '\0' };
90 /* need 3 bits to hold -1, 0, ..., 3 */
95 char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */
98 #define _ISO2022STATE_FLAG_INITIALIZED 1
102 _ISO2022Charset *recommend[4];
103 size_t recommendsize[4];
104 _ISO2022Charset initg[4];
107 #define F_8BIT 0x0001
108 #define F_NOOLD 0x0002
109 #define F_SI 0x0010 /*0F*/
110 #define F_SO 0x0020 /*0E*/
111 #define F_LS0 0x0010 /*0F*/
112 #define F_LS1 0x0020 /*0E*/
113 #define F_LS2 0x0040 /*ESC n*/
114 #define F_LS3 0x0080 /*ESC o*/
115 #define F_LS1R 0x0100 /*ESC ~*/
116 #define F_LS2R 0x0200 /*ESC }*/
117 #define F_LS3R 0x0400 /*ESC |*/
118 #define F_SS2 0x0800 /*ESC N*/
119 #define F_SS3 0x1000 /*ESC O*/
120 #define F_SS2R 0x2000 /*8E*/
121 #define F_SS3R 0x4000 /*8F*/
122 } _ISO2022EncodingInfo;
124 _ISO2022EncodingInfo ei;
126 /* for future multi-locale facility */
127 _ISO2022State s_mblen;
128 _ISO2022State s_mbrlen;
129 _ISO2022State s_mbrtowc;
130 _ISO2022State s_mbtowc;
131 _ISO2022State s_mbsrtowcs;
132 _ISO2022State s_wcrtomb;
133 _ISO2022State s_wcsrtombs;
134 _ISO2022State s_wctomb;
138 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
139 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
141 #define _FUNCNAME(m) _citrus_ISO2022_##m
142 #define _ENCODING_INFO _ISO2022EncodingInfo
143 #define _CTYPE_INFO _ISO2022CTypeInfo
144 #define _ENCODING_STATE _ISO2022State
145 #define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX
146 #define _ENCODING_IS_STATE_DEPENDENT 1
147 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) \
148 (!((_ps_)->flags & _ISO2022STATE_FLAG_INITIALIZED))
151 #define _ISO2022INVALID (wchar_t)-1
153 static __inline int isc0(__uint8_t x) { return ((x & 0x1f) == x); }
154 static __inline int isc1(__uint8_t x) { return (0x80 <= x && x <= 0x9f); }
155 static __inline int iscntl(__uint8_t x) { return (isc0(x) || isc1(x) || x == 0x7f); }
156 static __inline int is94(__uint8_t x) { return (0x21 <= x && x <= 0x7e); }
157 static __inline int is96(__uint8_t x) { return (0x20 <= x && x <= 0x7f); }
158 static __inline int isecma(__uint8_t x) { return (0x30 <= x && x <= 0x7f); }
159 static __inline int isinterm(__uint8_t x) { return (0x20 <= x && x <= 0x2f); }
160 static __inline int isthree(__uint8_t x) { return (0x60 <= x && x <= 0x6f); }
163 getcs(const char * __restrict p, _ISO2022Charset * __restrict cs)
166 _DIAGASSERT(p != NULL);
167 _DIAGASSERT(cs != NULL);
169 if (!strncmp(p, "94$", 3) && p[3] && !p[4]) {
170 cs->final = (u_char)(p[3] & 0xff);
173 cs->type = CS94MULTI;
174 } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) {
175 cs->final = (u_char)(p[3] & 0xff);
178 cs->type = CS96MULTI;
179 } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) {
180 cs->final = (u_char)(p[2] & 0xff);
184 } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) {
185 cs->final = (u_char )(p[2] & 0xff);
202 get_recommend(_ISO2022EncodingInfo * __restrict ei,
203 const char * __restrict token)
206 _ISO2022Charset cs, *p;
208 if (!strchr("0123", token[0]) || token[1] != '=')
211 if (getcs(&token[2], &cs) == 0)
213 else if (!strcmp(&token[2], "94")) {
214 cs.final = (u_char)(token[4]);
218 } else if (!strcmp(&token[2], "96")) {
219 cs.final = (u_char)(token[4]);
223 } else if (!strcmp(&token[2], "94$")) {
224 cs.final = (u_char)(token[5]);
228 } else if (!strcmp(&token[2], "96$")) {
229 cs.final = (u_char)(token[5]);
238 if (!ei->recommend[i]) {
239 ei->recommend[i] = malloc(sizeof(_ISO2022Charset));
241 p = realloc(ei->recommend[i],
242 sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1));
245 ei->recommend[i] = p;
247 if (!ei->recommend[i])
249 ei->recommendsize[i]++;
251 (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final;
252 (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm;
253 (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers;
254 (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type;
260 get_initg(_ISO2022EncodingInfo * __restrict ei,
261 const char * __restrict token)
265 if (strncmp("INIT", &token[0], 4) ||
266 !strchr("0123", token[4]) ||
270 if (getcs(&token[6], &cs) != 0)
273 ei->initg[token[4] - '0'].type = cs.type;
274 ei->initg[token[4] - '0'].final = cs.final;
275 ei->initg[token[4] - '0'].interm = cs.interm;
276 ei->initg[token[4] - '0'].vers = cs.vers;
282 get_max(_ISO2022EncodingInfo * __restrict ei,
283 const char * __restrict token)
285 if (!strcmp(token, "MAX1")) {
287 } else if (!strcmp(token, "MAX2")) {
289 } else if (!strcmp(token, "MAX3")) {
299 get_flags(_ISO2022EncodingInfo * __restrict ei,
300 const char * __restrict token)
309 { "NOOLD", F_NOOLD },
326 for (i = 0; tags[i].tag; i++) {
327 if (!strcmp(token, tags[i].tag)) {
328 ei->flags |= tags[i].flag;
338 _citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei,
339 const void * __restrict var, size_t lenvar)
345 _DIAGASSERT(ei != NULL);
349 * parse VARIABLE section.
355 v = (const char *) var;
357 /* initialize structure */
359 for (i = 0; i < 4; i++) {
360 ei->recommend[i] = NULL;
361 ei->recommendsize[i] = 0;
366 while (*v == ' ' || *v == '\t')
371 while (*e && *e != ' ' && *e != '\t')
377 if (len>=sizeof(buf))
379 snprintf(buf, sizeof(buf), "%.*s", len, v);
381 if ((ret = get_recommend(ei, buf)) != _NOTMATCH)
383 else if ((ret = get_initg(ei, buf)) != _NOTMATCH)
385 else if ((ret = get_max(ei, buf)) != _NOTMATCH)
387 else if ((ret = get_flags(ei, buf)) != _NOTMATCH)
400 free(ei->recommend[0]);
401 free(ei->recommend[1]);
402 free(ei->recommend[2]);
403 free(ei->recommend[3]);
410 _citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei,
411 _ISO2022State * __restrict s)
415 memset(s, 0, sizeof(*s));
417 s->gr = (ei->flags & F_8BIT) ? 1 : -1;
419 for (i = 0; i < 4; i++) {
420 if (ei->initg[i].final) {
421 s->g[i].type = ei->initg[i].type;
422 s->g[i].final = ei->initg[i].final;
423 s->g[i].interm = ei->initg[i].interm;
426 s->singlegl = s->singlegr = -1;
427 s->flags |= _ISO2022STATE_FLAG_INITIALIZED;
432 _citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei,
433 void * __restrict pspriv,
434 const _ISO2022State * __restrict s)
436 memcpy(pspriv, (const void *)s, sizeof(*s));
441 _citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei,
442 _ISO2022State * __restrict s,
443 const void * __restrict pspriv)
445 memcpy((void *)s, pspriv, sizeof(*s));
450 _citrus_ISO2022_encoding_module_init(_ISO2022EncodingInfo * __restrict ei,
451 const void * __restrict var,
455 _DIAGASSERT(ei != NULL);
457 return _citrus_ISO2022_parse_variable(ei, var, lenvar);
462 _citrus_ISO2022_encoding_module_uninit(_ISO2022EncodingInfo *ei)
471 static const struct seqtable {
480 /* G0 94MULTI special */
481 { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, },
482 /* G0 94MULTI special with version identification */
483 { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, },
485 { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, },
486 /* G? 94 with 2nd intermediate char */
487 { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, },
489 { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, },
490 /* G? 96 with 2nd intermediate char */
491 { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, },
493 { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, },
495 { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, },
496 /* G? 94MULTI with version specification */
497 { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, },
499 { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, },
500 { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, },
502 { -1, -1, -1, -1, -1, 2, { ESC, '~', }, },
503 { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, },
504 { -1, -1, -1, -1, -1, 2, { ESC, '|', }, },
506 { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, },
507 { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, },
513 seqmatch(const char * __restrict s, size_t n,
514 const struct seqtable * __restrict sp)
518 _DIAGASSERT(s != NULL);
519 _DIAGASSERT(sp != NULL);
522 while (p - sp->chars < n && p - sp->chars < sp->len) {
529 if (*s && strchr("@AB", *s))
538 if (*s && strchr("()*+", *s))
543 if (*s && strchr(",-./", *s))
558 return p - sp->chars;
562 _ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei,
563 const char * __restrict string, size_t n,
564 const char ** __restrict result,
565 _ISO2022State * __restrict psenc)
569 const struct seqtable *sp;
573 _DIAGASSERT(ei != NULL);
574 _DIAGASSERT(string != NULL);
575 /* result may be NULL */
579 if (1 <= n && string[0] == '\017') {
585 if (1 <= n && string[0] == '\016') {
593 if (1 <= n && string[0] && strchr("\217\216", string[0])) {
594 psenc->singlegl = psenc->singlegr =
595 (string[0] - '\216') + 2;
601 /* eat the letter if this is not ESC */
602 if (1 <= n && string[0] != '\033')
605 /* look for a perfect match from escape sequences */
606 for (sp = &seqtable[0]; sp->len; sp++) {
607 nmatch = seqmatch(string, n, sp);
608 if (sp->len == nmatch && n >= sp->len)
615 if (sp->type != -1) {
622 i = string[sp->csoff] - '(';
626 i = string[sp->csoff] - ',';
629 return (_ISO2022INVALID);
632 psenc->g[i].type = sp->type;
633 psenc->g[i].final = '\0';
634 psenc->g[i].interm = '\0';
635 psenc->g[i].vers = '\0';
636 /* sp->finaloff must not be -1 */
637 if (sp->finaloff != -1)
638 psenc->g[i].final = string[sp->finaloff];
639 if (sp->intermoff != -1)
640 psenc->g[i].interm = string[sp->intermoff];
641 if (sp->versoff != -1)
642 psenc->g[i].vers = string[sp->versoff];
650 if (2 <= n && string[0] == '\033'
651 && string[1] && strchr("no", string[1])) {
652 psenc->gl = string[1] - 'n' + 2;
659 /* XXX: { for vi showmatch */
660 if (2 <= n && string[0] == '\033'
661 && string[1] && strchr("~}|", string[1])) {
662 psenc->gr = 3 - (string[1] - '|');
669 if (2 <= n && string[0] == '\033'
670 && string[1] && strchr("NO", string[1])) {
671 psenc->singlegl = (string[1] - 'N') + 2;
679 * if we've got an unknown escape sequence, eat the ESC at the
680 * head. otherwise, wait till full escape sequence comes.
682 for (sp = &seqtable[0]; sp->len; sp++) {
683 nmatch = seqmatch(string, n, sp);
688 * if we are in the middle of escape sequence,
689 * we still need to wait for more characters to come
695 return (_ISO2022INVALID);
698 if (nmatch == sp->len) {
699 /* this case should not happen */
709 /* no letter to eat */
713 return (_ISO2022INVALID);
716 /* normal chars. always eat C0/C1 as is. */
717 if (iscntl(*string & 0xff))
719 else if (*string & 0x80) {
720 cur = (psenc->singlegr == -1)
721 ? psenc->gr : psenc->singlegr;
723 cur = (psenc->singlegl == -1)
724 ? psenc->gl : psenc->singlegl;
729 wchar = *string++ & 0xff;
732 /* reset single shift state */
733 psenc->singlegr = psenc->singlegl = -1;
737 /* length error check */
738 switch (psenc->g[cur].type) {
741 if (!isthree(psenc->g[cur].final)) {
743 && (string[0] & 0x80) == (string[1] & 0x80))
747 && (string[0] & 0x80) == (string[1] & 0x80)
748 && (string[0] & 0x80) == (string[2] & 0x80))
752 /* we still need to wait for more characters to come */
755 return (_ISO2022INVALID);
762 /* we still need to wait for more characters to come */
765 return (_ISO2022INVALID);
769 switch (psenc->g[cur].type) {
771 if (!(is94(string[0] & 0x7f)))
774 if (!(is96(string[0] & 0x7f)))
778 if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f)))
782 if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f)))
787 /* extract the character. */
788 switch (psenc->g[cur].type) {
790 /* special case for ASCII. */
791 if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) {
796 wchar = psenc->g[cur].final;
797 wchar = (wchar << 8);
798 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
799 wchar = (wchar << 8);
800 wchar = (wchar << 8) | (*string++ & 0x7f);
803 /* special case for ISO-8859-1. */
804 if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) {
810 wchar = psenc->g[cur].final;
811 wchar = (wchar << 8);
812 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
813 wchar = (wchar << 8);
814 wchar = (wchar << 8) | (*string++ & 0x7f);
819 wchar = psenc->g[cur].final;
820 wchar = (wchar << 8);
821 if (isthree(psenc->g[cur].final))
822 wchar |= (*string++ & 0x7f);
823 wchar = (wchar << 8) | (*string++ & 0x7f);
824 wchar = (wchar << 8) | (*string++ & 0x7f);
825 if (psenc->g[cur].type == CS96MULTI)
832 /* reset single shift state */
833 psenc->singlegr = psenc->singlegl = -1;
840 _citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei,
841 wchar_t * __restrict pwc,
842 const char ** __restrict s,
843 size_t n, _ISO2022State * __restrict psenc,
844 size_t * __restrict nresult)
847 const char *s0, *p, *result;
851 _DIAGASSERT(nresult != NULL);
852 _DIAGASSERT(ei != NULL);
853 _DIAGASSERT(psenc != NULL);
854 _DIAGASSERT(s != NULL);
857 _citrus_ISO2022_init_state(ei, psenc);
858 *nresult = _ENCODING_IS_STATE_DEPENDENT;
863 chlenbak = psenc->chlen;
866 * if we have something in buffer, use that.
867 * otherwise, skip here
869 if (psenc->chlen < 0 || psenc->chlen > sizeof(psenc->ch)) {
871 _citrus_ISO2022_init_state(ei, psenc);
874 if (psenc->chlen == 0)
877 /* buffer is not empty */
879 while (psenc->chlen < sizeof(psenc->ch) && n >= 0) {
881 psenc->ch[psenc->chlen++] = *s0++;
885 wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch),
888 if (wchar != _ISO2022INVALID) {
889 if (psenc->chlen > c)
890 memmove(psenc->ch, result, psenc->chlen - c);
891 if (psenc->chlen < c)
899 if ((result - p) == psenc->chlen)
900 /* complete shift sequence. */
908 /* escape sequence too long? */
912 wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc);
913 if (wchar != _ISO2022INVALID) {
925 /* complete shift sequence. */
929 if (n < sizeof(psenc->ch)) {
930 memcpy(psenc->ch, s0 - c, n);
936 /* escape sequence too long? */
940 *nresult = (size_t)-1;
951 *nresult = c - chlenbak;
957 *nresult = (size_t)-2;
963 recommendation(_ISO2022EncodingInfo * __restrict ei,
964 _ISO2022Charset * __restrict cs)
967 _ISO2022Charset *recommend;
969 _DIAGASSERT(ei != NULL);
970 _DIAGASSERT(cs != NULL);
972 /* first, try a exact match. */
973 for (i = 0; i < 4; i++) {
974 recommend = ei->recommend[i];
975 for (j = 0; j < ei->recommendsize[i]; j++) {
976 if (cs->type != recommend[j].type)
978 if (cs->final != recommend[j].final)
980 if (cs->interm != recommend[j].interm)
987 /* then, try a wildcard match over final char. */
988 for (i = 0; i < 4; i++) {
989 recommend = ei->recommend[i];
990 for (j = 0; j < ei->recommendsize[i]; j++) {
991 if (cs->type != recommend[j].type)
993 if (cs->final && (cs->final != recommend[j].final))
995 if (cs->interm && (cs->interm != recommend[j].interm))
1002 /* there's no recommendation. make a guess. */
1003 if (ei->maxcharset == 0) {
1019 _ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t wc,
1020 char * __restrict string, size_t n,
1021 char ** __restrict result,
1022 _ISO2022State * __restrict psenc,
1023 size_t * __restrict nresult)
1029 char tmp[MB_LEN_MAX];
1034 _DIAGASSERT(ei != NULL);
1035 _DIAGASSERT(string != NULL);
1036 /* result may be NULL */
1037 _DIAGASSERT(psenc != NULL);
1038 _DIAGASSERT(nresult != NULL);
1040 if (isc0(wc & 0xff)) {
1041 /* go back to INIT0 or ASCII on control chars */
1042 cs = ei->initg[0].final ? ei->initg[0] : ascii;
1043 } else if (isc1(wc & 0xff)) {
1044 /* go back to INIT1 or ISO-8859-1 on control chars */
1045 cs = ei->initg[1].final ? ei->initg[1] : iso88591;
1046 } else if (!(wc & ~0xff)) {
1048 /* special treatment for ISO-8859-1 */
1051 /* special treatment for ASCII */
1055 cs.final = (wc >> 24) & 0x7f;
1056 if ((wc >> 16) & 0x80)
1057 cs.interm = (wc >> 16) & 0x7f;
1061 cs.type = (wc & 0x00007f00) ? CS96MULTI : CS96;
1063 cs.type = (wc & 0x00007f00) ? CS94MULTI : CS94;
1065 target = recommendation(ei, &cs);
1067 bit8 = ei->flags & F_8BIT;
1069 /* designate the charset onto the target plane(G0/1/2/3). */
1070 if (psenc->g[target].type == cs.type
1071 && psenc->g[target].final == cs.final
1072 && psenc->g[target].interm == cs.interm)
1076 if (cs.type == CS94MULTI || cs.type == CS96MULTI)
1078 if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final)
1079 && !cs.interm && !(ei->flags & F_NOOLD))
1081 else if (cs.type == CS94 || cs.type == CS94MULTI)
1082 *p++ = "()*+"[target];
1084 *p++ = ",-./"[target];
1089 psenc->g[target].type = cs.type;
1090 psenc->g[target].final = cs.final;
1091 psenc->g[target].interm = cs.interm;
1094 /* invoke the plane onto GL or GR. */
1095 if (psenc->gl == target)
1097 if (bit8 && psenc->gr == target)
1100 if (target == 0 && (ei->flags & F_LS0)) {
1103 } else if (target == 1 && (ei->flags & F_LS1)) {
1106 } else if (target == 2 && (ei->flags & F_LS2)) {
1110 } else if (target == 3 && (ei->flags & F_LS3)) {
1114 } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) {
1118 } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) {
1123 } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) {
1127 } else if (target == 2 && (ei->flags & F_SS2)) {
1130 psenc->singlegl = 2;
1131 } else if (target == 3 && (ei->flags & F_SS3)) {
1134 psenc->singlegl = 3;
1135 } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) {
1138 psenc->singlegl = psenc->singlegr = 2;
1139 } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) {
1142 psenc->singlegl = psenc->singlegr = 3;
1147 if (psenc->singlegl == target)
1149 else if (psenc->singlegr == target)
1151 else if (psenc->gl == target)
1153 else if ((ei->flags & F_8BIT) && psenc->gr == target)
1165 i = !iscntl(wc & 0xff) ?
1166 (isthree(cs.final) ? 3 : 2) : 1;
1170 *p++ = ((wc >> (i << 3)) & 0x7f) | mask;
1172 /* reset single shift state */
1173 psenc->singlegl = psenc->singlegr = -1;
1175 len = (size_t)(p - tmp);
1179 *nresult = (size_t)-1;
1183 *result = string + len;
1184 memcpy(string, tmp, len);
1190 *nresult = (size_t)-1;
1195 _citrus_ISO2022_put_state_reset(_ISO2022EncodingInfo * __restrict ei,
1196 char * __restrict s, size_t n,
1197 _ISO2022State * __restrict psenc,
1198 size_t * __restrict nresult)
1200 char buf[MB_LEN_MAX];
1205 _DIAGASSERT(ei != NULL);
1206 _DIAGASSERT(nresult != NULL);
1207 _DIAGASSERT(s != NULL);
1209 /* XXX state will be modified after this operation... */
1210 ret = _ISO2022_sputwchar(ei, L'\0', buf, sizeof(buf), &result, psenc,
1217 if (sizeof(buf) < len || n < len-1) {
1218 /* XXX should recover state? */
1219 *nresult = (size_t)-1;
1223 memcpy(s, buf, len-1);
1229 _citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei,
1230 char * __restrict s, size_t n, wchar_t wc,
1231 _ISO2022State * __restrict psenc,
1232 size_t * __restrict nresult)
1234 char buf[MB_LEN_MAX];
1239 _DIAGASSERT(ei != NULL);
1240 _DIAGASSERT(s != NULL);
1241 _DIAGASSERT(psenc != NULL);
1242 _DIAGASSERT(nresult != NULL);
1244 /* XXX state will be modified after this operation... */
1245 ret = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc,
1252 if (sizeof(buf) < len || n < len) {
1253 /* XXX should recover state? */
1254 *nresult = (size_t)-1;
1258 memcpy(s, buf, len);
1265 _citrus_ISO2022_stdenc_wctocs(_ISO2022EncodingInfo * __restrict ei,
1266 _csid_t * __restrict csid,
1267 _index_t * __restrict idx, wchar_t wc)
1271 _DIAGASSERT(csid != NULL && idx != NULL);
1273 m = wc & 0x7FFF8080;
1274 nm = wc & 0x007F7F7F;
1275 if (m & 0x00800000) {
1280 if (nm & 0x007F0000) {
1283 } else if (nm & 0x00007F00) {
1288 *idx = (_index_t)nm;
1295 _citrus_ISO2022_stdenc_cstowc(_ISO2022EncodingInfo * __restrict ei,
1296 wchar_t * __restrict wc,
1297 _csid_t csid, _index_t idx)
1300 _DIAGASSERT(ei != NULL && wc != NULL);
1302 *wc = (wchar_t)(csid & 0x7F808080) | (wchar_t)idx;
1309 _citrus_ISO2022_stdenc_get_state_desc_generic(_ISO2022EncodingInfo * __restrict ei,
1310 _ISO2022State * __restrict psenc,
1311 int * __restrict rstate)
1314 if (psenc->chlen == 0) {
1315 /* XXX: it should distinguish initial and stable. */
1316 *rstate = _STDENC_SDGEN_STABLE;
1318 if (psenc->ch[0] == '\033')
1319 *rstate = _STDENC_SDGEN_INCOMPLETE_SHIFT;
1321 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
1327 /* ----------------------------------------------------------------------
1328 * public interface for ctype
1331 _CITRUS_CTYPE_DECLS(ISO2022);
1332 _CITRUS_CTYPE_DEF_OPS(ISO2022);
1334 #include "citrus_ctype_template.h"
1336 /* ----------------------------------------------------------------------
1337 * public interface for stdenc
1340 _CITRUS_STDENC_DECLS(ISO2022);
1341 _CITRUS_STDENC_DEF_OPS(ISO2022);
1343 #include "citrus_stdenc_template.h"