1 /* $FreeBSD: head/lib/libiconv_modules/ISO2022/citrus_iso2022.c 281550 2015-04-15 09:09:20Z tijl $ */
2 /* $NetBSD: citrus_iso2022.c,v 1.20 2010/12/07 22:01:45 joerg Exp $ */
5 * Copyright (c)1999, 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
29 * $Citrus: xpg4dl/FreeBSD/lib/libc/locale/iso2022.c,v 1.23 2001/06/21 01:51:44 yamt Exp $
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
45 #include "citrus_namespace.h"
46 #include "citrus_types.h"
47 #include "citrus_module.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_iso2022.h"
52 /* ----------------------------------------------------------------------
53 * private stuffs used by templates
59 * ASCII (ESC ( B) 00000000 00000000 00000000 0xxxxxxx
60 * iso-8859-1 (ESC , A) 00000000 00000000 00000000 1xxxxxxx
61 * 94 charset (ESC ( F) 0fffffff 00000000 00000000 0xxxxxxx
62 * 94 charset (ESC ( M F) 0fffffff 1mmmmmmm 00000000 0xxxxxxx
63 * 96 charset (ESC , F) 0fffffff 00000000 00000000 1xxxxxxx
64 * 96 charset (ESC , M F) 0fffffff 1mmmmmmm 00000000 1xxxxxxx
65 * 94x94 charset (ESC $ ( F) 0fffffff 00000000 0xxxxxxx 0xxxxxxx
66 * 96x96 charset (ESC $ , F) 0fffffff 00000000 0xxxxxxx 1xxxxxxx
67 * 94x94 charset (ESC & V ESC $ ( F)
68 * 0fffffff 1vvvvvvv 0xxxxxxx 0xxxxxxx
69 * 94x94x94 charset (ESC $ ( F) 0fffffff 0xxxxxxx 0xxxxxxx 0xxxxxxx
70 * 96x96x96 charset (ESC $ , F) 0fffffff 0xxxxxxx 0xxxxxxx 1xxxxxxx
71 * reserved for UCS4 co-existence (UCS4 is 31bit encoding thanks to mohta bit)
72 * 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
77 #define CS94MULTI (2U)
78 #define CS96MULTI (3U)
87 static const _ISO2022Charset ascii = { CS94, 'B', '\0', '\0' };
88 static const _ISO2022Charset iso88591 = { CS96, 'A', '\0', '\0' };
92 /* need 3 bits to hold -1, 0, ..., 3 */
97 char ch[7]; /* longest escape sequence (ESC & V ESC $ ( F) */
100 #define _ISO2022STATE_FLAG_INITIALIZED 1
104 _ISO2022Charset *recommend[4];
105 size_t recommendsize[4];
106 _ISO2022Charset initg[4];
109 #define F_8BIT 0x0001
110 #define F_NOOLD 0x0002
111 #define F_SI 0x0010 /*0F*/
112 #define F_SO 0x0020 /*0E*/
113 #define F_LS0 0x0010 /*0F*/
114 #define F_LS1 0x0020 /*0E*/
115 #define F_LS2 0x0040 /*ESC n*/
116 #define F_LS3 0x0080 /*ESC o*/
117 #define F_LS1R 0x0100 /*ESC ~*/
118 #define F_LS2R 0x0200 /*ESC }*/
119 #define F_LS3R 0x0400 /*ESC |*/
120 #define F_SS2 0x0800 /*ESC N*/
121 #define F_SS3 0x1000 /*ESC O*/
122 #define F_SS2R 0x2000 /*8E*/
123 #define F_SS3R 0x4000 /*8F*/
124 } _ISO2022EncodingInfo;
126 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
127 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
129 #define _FUNCNAME(m) _citrus_ISO2022_##m
130 #define _ENCODING_INFO _ISO2022EncodingInfo
131 #define _ENCODING_STATE _ISO2022State
132 #define _ENCODING_MB_CUR_MAX(_ei_) MB_LEN_MAX
133 #define _ENCODING_IS_STATE_DEPENDENT 1
134 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) \
135 (!((_ps_)->flags & _ISO2022STATE_FLAG_INITIALIZED))
138 #define _ISO2022INVALID (wchar_t)-1
140 static __inline bool isc0(__uint8_t x)
143 return ((x & 0x1f) == x);
146 static __inline bool isc1(__uint8_t x)
149 return (0x80 <= x && x <= 0x9f);
152 static __inline bool iscntl(__uint8_t x)
155 return (isc0(x) || isc1(x) || x == 0x7f);
158 static __inline bool is94(__uint8_t x)
161 return (0x21 <= x && x <= 0x7e);
164 static __inline bool is96(__uint8_t x)
167 return (0x20 <= x && x <= 0x7f);
170 static __inline bool isecma(__uint8_t x)
173 return (0x30 <= x && x <= 0x7f);
176 static __inline bool isinterm(__uint8_t x)
179 return (0x20 <= x && x <= 0x2f);
182 static __inline bool isthree(__uint8_t x)
185 return (0x60 <= x && x <= 0x6f);
189 getcs(const char * __restrict p, _ISO2022Charset * __restrict cs)
192 if (!strncmp(p, "94$", 3) && p[3] && !p[4]) {
193 cs->final = (unsigned char)(p[3] & 0xff);
196 cs->type = CS94MULTI;
197 } else if (!strncmp(p, "96$", 3) && p[3] && !p[4]) {
198 cs->final = (unsigned char)(p[3] & 0xff);
201 cs->type = CS96MULTI;
202 } else if (!strncmp(p, "94", 2) && p[2] && !p[3]) {
203 cs->final = (unsigned char)(p[2] & 0xff);
207 } else if (!strncmp(p, "96", 2) && p[2] && !p[3]) {
208 cs->final = (unsigned char )(p[2] & 0xff);
224 get_recommend(_ISO2022EncodingInfo * __restrict ei,
225 const char * __restrict token)
227 _ISO2022Charset cs, *p;
230 if (!strchr("0123", token[0]) || token[1] != '=')
233 if (getcs(&token[2], &cs) == 0)
235 else if (!strcmp(&token[2], "94")) {
236 cs.final = (unsigned char)(token[4]);
240 } else if (!strcmp(&token[2], "96")) {
241 cs.final = (unsigned char)(token[4]);
245 } else if (!strcmp(&token[2], "94$")) {
246 cs.final = (unsigned char)(token[5]);
250 } else if (!strcmp(&token[2], "96$")) {
251 cs.final = (unsigned char)(token[5]);
259 if (!ei->recommend[i])
260 ei->recommend[i] = malloc(sizeof(_ISO2022Charset));
262 p = realloc(ei->recommend[i],
263 sizeof(_ISO2022Charset) * (ei->recommendsize[i] + 1));
266 ei->recommend[i] = p;
268 if (!ei->recommend[i])
270 ei->recommendsize[i]++;
272 (ei->recommend[i] + (ei->recommendsize[i] - 1))->final = cs.final;
273 (ei->recommend[i] + (ei->recommendsize[i] - 1))->interm = cs.interm;
274 (ei->recommend[i] + (ei->recommendsize[i] - 1))->vers = cs.vers;
275 (ei->recommend[i] + (ei->recommendsize[i] - 1))->type = cs.type;
281 get_initg(_ISO2022EncodingInfo * __restrict ei,
282 const char * __restrict token)
286 if (strncmp("INIT", &token[0], 4) ||
287 !strchr("0123", token[4]) ||
291 if (getcs(&token[6], &cs) != 0)
294 ei->initg[token[4] - '0'].type = cs.type;
295 ei->initg[token[4] - '0'].final = cs.final;
296 ei->initg[token[4] - '0'].interm = cs.interm;
297 ei->initg[token[4] - '0'].vers = cs.vers;
303 get_max(_ISO2022EncodingInfo * __restrict ei,
304 const char * __restrict token)
306 if (!strcmp(token, "MAX1"))
308 else if (!strcmp(token, "MAX2"))
310 else if (!strcmp(token, "MAX3"))
320 get_flags(_ISO2022EncodingInfo * __restrict ei,
321 const char * __restrict token)
329 { "NOOLD", F_NOOLD },
347 for (i = 0; tags[i].tag; i++)
348 if (!strcmp(token, tags[i].tag)) {
349 ei->flags |= tags[i].flag;
358 _citrus_ISO2022_parse_variable(_ISO2022EncodingInfo * __restrict ei,
359 const void * __restrict var, size_t lenvar __unused)
367 * parse VARIABLE section.
373 v = (const char *) var;
375 /* initialize structure */
377 for (i = 0; i < 4; i++) {
378 ei->recommend[i] = NULL;
379 ei->recommendsize[i] = 0;
384 while (*v == ' ' || *v == '\t')
389 while (*e && *e != ' ' && *e != '\t')
395 if (len >= sizeof(buf))
397 snprintf(buf, sizeof(buf), "%.*s", (int)len, v);
399 if ((ret = get_recommend(ei, buf)) != _NOTMATCH)
401 else if ((ret = get_initg(ei, buf)) != _NOTMATCH)
403 else if ((ret = get_max(ei, buf)) != _NOTMATCH)
405 else if ((ret = get_flags(ei, buf)) != _NOTMATCH)
409 if (ret == _PARSEFAIL)
418 free(ei->recommend[0]);
419 free(ei->recommend[1]);
420 free(ei->recommend[2]);
421 free(ei->recommend[3]);
428 _citrus_ISO2022_init_state(_ISO2022EncodingInfo * __restrict ei,
429 _ISO2022State * __restrict s)
433 memset(s, 0, sizeof(*s));
435 s->gr = (ei->flags & F_8BIT) ? 1 : -1;
437 for (i = 0; i < 4; i++)
438 if (ei->initg[i].final) {
439 s->g[i].type = ei->initg[i].type;
440 s->g[i].final = ei->initg[i].final;
441 s->g[i].interm = ei->initg[i].interm;
443 s->singlegl = s->singlegr = -1;
444 s->flags |= _ISO2022STATE_FLAG_INITIALIZED;
450 _citrus_ISO2022_pack_state(_ISO2022EncodingInfo * __restrict ei __unused,
451 void * __restrict pspriv, const _ISO2022State * __restrict s)
454 memcpy(pspriv, (const void *)s, sizeof(*s));
459 _citrus_ISO2022_unpack_state(_ISO2022EncodingInfo * __restrict ei __unused,
460 _ISO2022State * __restrict s, const void * __restrict pspriv)
463 memcpy((void *)s, pspriv, sizeof(*s));
469 _citrus_ISO2022_encoding_module_init(_ISO2022EncodingInfo * __restrict ei,
470 const void * __restrict var, size_t lenvar)
473 return (_citrus_ISO2022_parse_variable(ei, var, lenvar));
478 _citrus_ISO2022_encoding_module_uninit(_ISO2022EncodingInfo *ei __unused)
487 static const struct seqtable {
496 /* G0 94MULTI special */
497 { CS94MULTI, -1, 2, -1, -1, 3, { ESC, '$', OECMA }, },
498 /* G0 94MULTI special with version identification */
499 { CS94MULTI, -1, 5, -1, 2, 6, { ESC, '&', ECMA, ESC, '$', OECMA }, },
501 { CS94, 1, 2, -1, -1, 3, { ESC, CS94, ECMA, }, },
502 /* G? 94 with 2nd intermediate char */
503 { CS94, 1, 3, 2, -1, 4, { ESC, CS94, INTERM, ECMA, }, },
505 { CS96, 1, 2, -1, -1, 3, { ESC, CS96, ECMA, }, },
506 /* G? 96 with 2nd intermediate char */
507 { CS96, 1, 3, 2, -1, 4, { ESC, CS96, INTERM, ECMA, }, },
509 { CS94MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS94, ECMA, }, },
511 { CS96MULTI, 2, 3, -1, -1, 4, { ESC, '$', CS96, ECMA, }, },
512 /* G? 94MULTI with version specification */
513 { CS94MULTI, 5, 6, -1, 2, 7, { ESC, '&', ECMA, ESC, '$', CS94, ECMA, }, },
515 { -1, -1, -1, -1, -1, 2, { ESC, 'n', }, },
516 { -1, -1, -1, -1, -1, 2, { ESC, 'o', }, },
518 { -1, -1, -1, -1, -1, 2, { ESC, '~', }, },
519 { -1, -1, -1, -1, -1, 2, { ESC, /*{*/ '}', }, },
520 { -1, -1, -1, -1, -1, 2, { ESC, '|', }, },
522 { -1, -1, -1, -1, -1, 2, { ESC, 'N', }, },
523 { -1, -1, -1, -1, -1, 2, { ESC, 'O', }, },
526 { 0, 0, 0, 0, 0, 0, { ESC, 0, }, }
530 seqmatch(const char * __restrict s, size_t n,
531 const struct seqtable * __restrict sp)
536 while ((size_t)(p - sp->chars) < n && p - sp->chars < sp->len) {
543 if (*s && strchr("@AB", *s))
552 if (*s && strchr("()*+", *s))
557 if (*s && strchr(",-./", *s))
572 return (p - sp->chars);
576 _ISO2022_sgetwchar(_ISO2022EncodingInfo * __restrict ei __unused,
577 char * __restrict string, size_t n, char ** __restrict result,
578 _ISO2022State * __restrict psenc)
580 const struct seqtable *sp;
586 if (1 <= n && string[0] == '\017') {
592 if (1 <= n && string[0] == '\016') {
600 if (1 <= n && string[0] && strchr("\217\216", string[0])) {
601 psenc->singlegl = psenc->singlegr =
602 (string[0] - '\216') + 2;
608 /* eat the letter if this is not ESC */
609 if (1 <= n && string[0] != '\033')
612 /* look for a perfect match from escape sequences */
613 for (sp = &seqtable[0]; sp->len; sp++) {
614 nmatch = seqmatch(string, n, sp);
615 if (sp->len == nmatch && n >= (size_t)(sp->len))
622 if (sp->type != -1) {
629 i = string[sp->csoff] - '(';
633 i = string[sp->csoff] - ',';
636 return (_ISO2022INVALID);
639 psenc->g[i].type = sp->type;
640 psenc->g[i].final = '\0';
641 psenc->g[i].interm = '\0';
642 psenc->g[i].vers = '\0';
643 /* sp->finaloff must not be -1 */
644 if (sp->finaloff != -1)
645 psenc->g[i].final = string[sp->finaloff];
646 if (sp->intermoff != -1)
647 psenc->g[i].interm = string[sp->intermoff];
648 if (sp->versoff != -1)
649 psenc->g[i].vers = string[sp->versoff];
657 if (2 <= n && string[0] == '\033' &&
658 string[1] && strchr("no", string[1])) {
659 psenc->gl = string[1] - 'n' + 2;
666 /* XXX: { for vi showmatch */
667 if (2 <= n && string[0] == '\033' &&
668 string[1] && strchr("~}|", string[1])) {
669 psenc->gr = 3 - (string[1] - '|');
676 if (2 <= n && string[0] == '\033' && string[1] &&
677 strchr("NO", string[1])) {
678 psenc->singlegl = (string[1] - 'N') + 2;
686 * if we've got an unknown escape sequence, eat the ESC at the
687 * head. otherwise, wait till full escape sequence comes.
689 for (sp = &seqtable[0]; sp->len; sp++) {
690 nmatch = seqmatch(string, n, sp);
695 * if we are in the middle of escape sequence,
696 * we still need to wait for more characters to come
698 if (n < (size_t)(sp->len)) {
699 if ((size_t)(nmatch) == n) {
702 return (_ISO2022INVALID);
705 if (nmatch == sp->len) {
706 /* this case should not happen */
716 /* no letter to eat */
720 return (_ISO2022INVALID);
723 /* normal chars. always eat C0/C1 as is. */
724 if (iscntl(*string & 0xff))
726 else if (*string & 0x80)
727 cur = (psenc->singlegr == -1) ? psenc->gr : psenc->singlegr;
729 cur = (psenc->singlegl == -1) ? psenc->gl : psenc->singlegl;
733 wchar = *string++ & 0xff;
736 /* reset single shift state */
737 psenc->singlegr = psenc->singlegl = -1;
741 /* length error check */
742 switch (psenc->g[cur].type) {
745 if (!isthree(psenc->g[cur].final)) {
747 (string[0] & 0x80) == (string[1] & 0x80))
751 (string[0] & 0x80) == (string[1] & 0x80) &&
752 (string[0] & 0x80) == (string[2] & 0x80))
756 /* we still need to wait for more characters to come */
759 return (_ISO2022INVALID);
766 /* we still need to wait for more characters to come */
769 return (_ISO2022INVALID);
773 switch (psenc->g[cur].type) {
775 if (!(is94(string[0] & 0x7f)))
778 if (!(is96(string[0] & 0x7f)))
782 if (!(is94(string[0] & 0x7f) && is94(string[1] & 0x7f)))
786 if (!(is96(string[0] & 0x7f) && is96(string[1] & 0x7f)))
791 /* extract the character. */
792 switch (psenc->g[cur].type) {
794 /* special case for ASCII. */
795 if (psenc->g[cur].final == 'B' && !psenc->g[cur].interm) {
800 wchar = psenc->g[cur].final;
801 wchar = (wchar << 8);
802 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
803 wchar = (wchar << 8);
804 wchar = (wchar << 8) | (*string++ & 0x7f);
807 /* special case for ISO-8859-1. */
808 if (psenc->g[cur].final == 'A' && !psenc->g[cur].interm) {
814 wchar = psenc->g[cur].final;
815 wchar = (wchar << 8);
816 wchar |= (psenc->g[cur].interm ? (0x80 | psenc->g[cur].interm) : 0);
817 wchar = (wchar << 8);
818 wchar = (wchar << 8) | (*string++ & 0x7f);
823 wchar = psenc->g[cur].final;
824 wchar = (wchar << 8);
825 if (isthree(psenc->g[cur].final))
826 wchar |= (*string++ & 0x7f);
827 wchar = (wchar << 8) | (*string++ & 0x7f);
828 wchar = (wchar << 8) | (*string++ & 0x7f);
829 if (psenc->g[cur].type == CS96MULTI)
836 /* reset single shift state */
837 psenc->singlegr = psenc->singlegl = -1;
844 _citrus_ISO2022_mbrtowc_priv(_ISO2022EncodingInfo * __restrict ei,
845 wchar_t * __restrict pwc, char ** __restrict s,
846 size_t n, _ISO2022State * __restrict psenc, size_t * __restrict nresult)
848 char *p, *result, *s0;
853 _citrus_ISO2022_init_state(ei, psenc);
854 *nresult = _ENCODING_IS_STATE_DEPENDENT;
859 chlenbak = psenc->chlen;
862 * if we have something in buffer, use that.
863 * otherwise, skip here
865 if (psenc->chlen > sizeof(psenc->ch)) {
867 _citrus_ISO2022_init_state(ei, psenc);
870 if (psenc->chlen == 0)
873 /* buffer is not empty */
875 while (psenc->chlen < sizeof(psenc->ch)) {
877 psenc->ch[psenc->chlen++] = *s0++;
881 wchar = _ISO2022_sgetwchar(ei, p, psenc->chlen - (p-psenc->ch),
884 if (wchar != _ISO2022INVALID) {
885 if (psenc->chlen > (size_t)c)
886 memmove(psenc->ch, result, psenc->chlen - c);
887 if (psenc->chlen < (size_t)c)
895 if ((size_t)(result - p) == psenc->chlen)
896 /* complete shift sequence. */
904 /* escape sequence too long? */
908 wchar = _ISO2022_sgetwchar(ei, s0, n, &result, psenc);
909 if (wchar != _ISO2022INVALID) {
921 /* complete shift sequence. */
925 if (n < sizeof(psenc->ch)) {
926 memcpy(psenc->ch, s0 - c, n);
932 /* escape sequence too long? */
936 *nresult = (size_t)-1;
943 *nresult = wchar ? c - chlenbak : 0;
948 *nresult = (size_t)-2;
954 recommendation(_ISO2022EncodingInfo * __restrict ei,
955 _ISO2022Charset * __restrict cs)
957 _ISO2022Charset *recommend;
961 /* first, try a exact match. */
962 for (i = 0; i < 4; i++) {
963 recommend = ei->recommend[i];
964 for (j = 0; j < ei->recommendsize[i]; j++) {
965 if (cs->type != recommend[j].type)
967 if (cs->final != recommend[j].final)
969 if (cs->interm != recommend[j].interm)
976 /* then, try a wildcard match over final char. */
977 for (i = 0; i < 4; i++) {
978 recommend = ei->recommend[i];
979 for (j = 0; j < ei->recommendsize[i]; j++) {
980 if (cs->type != recommend[j].type)
982 if (cs->final && (cs->final != recommend[j].final))
984 if (cs->interm && (cs->interm != recommend[j].interm))
991 /* there's no recommendation. make a guess. */
992 if (ei->maxcharset == 0) {
1008 _ISO2022_sputwchar(_ISO2022EncodingInfo * __restrict ei, wchar_t wc,
1009 char * __restrict string, size_t n, char ** __restrict result,
1010 _ISO2022State * __restrict psenc, size_t * __restrict nresult)
1014 char tmp[MB_LEN_MAX];
1016 int bit8, i = 0, target;
1019 if (isc0(wc & 0xff)) {
1020 /* go back to INIT0 or ASCII on control chars */
1021 cs = ei->initg[0].final ? ei->initg[0] : ascii;
1022 } else if (isc1(wc & 0xff)) {
1023 /* go back to INIT1 or ISO-8859-1 on control chars */
1024 cs = ei->initg[1].final ? ei->initg[1] : iso88591;
1025 } else if (!(wc & ~0xff)) {
1027 /* special treatment for ISO-8859-1 */
1030 /* special treatment for ASCII */
1034 cs.final = (wc >> 24) & 0x7f;
1035 if ((wc >> 16) & 0x80)
1036 cs.interm = (wc >> 16) & 0x7f;
1040 cs.type = (wc & 0x00007f00) ? CS96MULTI : CS96;
1042 cs.type = (wc & 0x00007f00) ? CS94MULTI : CS94;
1044 target = recommendation(ei, &cs);
1046 bit8 = ei->flags & F_8BIT;
1048 /* designate the charset onto the target plane(G0/1/2/3). */
1049 if (psenc->g[target].type == cs.type &&
1050 psenc->g[target].final == cs.final &&
1051 psenc->g[target].interm == cs.interm)
1055 if (cs.type == CS94MULTI || cs.type == CS96MULTI)
1057 if (target == 0 && cs.type == CS94MULTI && strchr("@AB", cs.final) &&
1058 !cs.interm && !(ei->flags & F_NOOLD))
1060 else if (cs.type == CS94 || cs.type == CS94MULTI)
1061 *p++ = "()*+"[target];
1063 *p++ = ",-./"[target];
1068 psenc->g[target].type = cs.type;
1069 psenc->g[target].final = cs.final;
1070 psenc->g[target].interm = cs.interm;
1073 /* invoke the plane onto GL or GR. */
1074 if (psenc->gl == target)
1076 if (bit8 && psenc->gr == target)
1079 if (target == 0 && (ei->flags & F_LS0)) {
1082 } else if (target == 1 && (ei->flags & F_LS1)) {
1085 } else if (target == 2 && (ei->flags & F_LS2)) {
1089 } else if (target == 3 && (ei->flags & F_LS3)) {
1093 } else if (bit8 && target == 1 && (ei->flags & F_LS1R)) {
1097 } else if (bit8 && target == 2 && (ei->flags & F_LS2R)) {
1102 } else if (bit8 && target == 3 && (ei->flags & F_LS3R)) {
1106 } else if (target == 2 && (ei->flags & F_SS2)) {
1109 psenc->singlegl = 2;
1110 } else if (target == 3 && (ei->flags & F_SS3)) {
1113 psenc->singlegl = 3;
1114 } else if (bit8 && target == 2 && (ei->flags & F_SS2R)) {
1117 psenc->singlegl = psenc->singlegr = 2;
1118 } else if (bit8 && target == 3 && (ei->flags & F_SS3R)) {
1121 psenc->singlegl = psenc->singlegr = 3;
1126 if (psenc->singlegl == target)
1128 else if (psenc->singlegr == target)
1130 else if (psenc->gl == target)
1132 else if ((ei->flags & F_8BIT) && psenc->gr == target)
1144 i = !iscntl(wc & 0xff) ?
1145 (isthree(cs.final) ? 3 : 2) : 1;
1149 *p++ = ((wc >> (i << 3)) & 0x7f) | mask;
1151 /* reset single shift state */
1152 psenc->singlegl = psenc->singlegr = -1;
1154 len = (size_t)(p - tmp);
1157 *result = (char *)0;
1158 *nresult = (size_t)-1;
1162 *result = string + len;
1163 memcpy(string, tmp, len);
1169 *nresult = (size_t)-1;
1174 _citrus_ISO2022_put_state_reset(_ISO2022EncodingInfo * __restrict ei,
1175 char * __restrict s, size_t n, _ISO2022State * __restrict psenc,
1176 size_t * __restrict nresult)
1179 char buf[MB_LEN_MAX];
1183 /* XXX state will be modified after this operation... */
1184 ret = _ISO2022_sputwchar(ei, L'\0', buf, sizeof(buf), &result, psenc,
1191 if (sizeof(buf) < len || n < len-1) {
1192 /* XXX should recover state? */
1193 *nresult = (size_t)-1;
1197 memcpy(s, buf, len - 1);
1203 _citrus_ISO2022_wcrtomb_priv(_ISO2022EncodingInfo * __restrict ei,
1204 char * __restrict s, size_t n, wchar_t wc,
1205 _ISO2022State * __restrict psenc, size_t * __restrict nresult)
1208 char buf[MB_LEN_MAX];
1212 /* XXX state will be modified after this operation... */
1213 ret = _ISO2022_sputwchar(ei, wc, buf, sizeof(buf), &result, psenc,
1220 if (sizeof(buf) < len || n < len) {
1221 /* XXX should recover state? */
1222 *nresult = (size_t)-1;
1226 memcpy(s, buf, len);
1233 _citrus_ISO2022_stdenc_wctocs(_ISO2022EncodingInfo * __restrict ei __unused,
1234 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
1238 m = wc & 0x7FFF8080;
1239 nm = wc & 0x007F7F7F;
1244 if (nm & 0x007F0000) {
1247 } else if (nm & 0x00007F00) {
1252 *idx = (_index_t)nm;
1259 _citrus_ISO2022_stdenc_cstowc(_ISO2022EncodingInfo * __restrict ei __unused,
1260 wchar_t * __restrict wc, _csid_t csid, _index_t idx)
1263 *wc = (wchar_t)(csid & 0x7F808080) | (wchar_t)idx;
1270 _citrus_ISO2022_stdenc_get_state_desc_generic(_ISO2022EncodingInfo * __restrict ei __unused,
1271 _ISO2022State * __restrict psenc, int * __restrict rstate)
1274 if (psenc->chlen == 0) {
1275 /* XXX: it should distinguish initial and stable. */
1276 *rstate = _STDENC_SDGEN_STABLE;
1278 *rstate = (psenc->ch[0] == '\033') ?
1279 _STDENC_SDGEN_INCOMPLETE_SHIFT :
1280 _STDENC_SDGEN_INCOMPLETE_CHAR;
1284 /* ----------------------------------------------------------------------
1285 * public interface for stdenc
1288 _CITRUS_STDENC_DECLS(ISO2022);
1289 _CITRUS_STDENC_DEF_OPS(ISO2022);
1291 #include "citrus_stdenc_template.h"