2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* lcCT.c 1.1 - Fujitsu source for CDEnext 95/11/06 20:32:33 */
24 /* $XConsortium: _fallcCT.c /main/1 1996/04/08 15:15:33 cde-fuj $ */
26 * Copyright 1992, 1993 by TOSHIBA Corp.
28 * Permission to use, copy, modify, and distribute this software and its
29 * documentation for any purpose and without fee is hereby granted, provided
30 * that the above copyright notice appear in all copies and that both that
31 * copyright notice and this permission notice appear in supporting
32 * documentation, and that the name of TOSHIBA not be used in advertising
33 * or publicity pertaining to distribution of the software without specific,
34 * written prior permission. TOSHIBA make no representations about the
35 * suitability of this software for any purpose. It is provided "as is"
36 * without express or implied warranty.
38 * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46 * Author: Katsuhisa Yano TOSHIBA Corp.
47 * mopi@osa.ilab.toshiba.co.jp
50 #include "_fallibint.h"
51 #include "_fallcPubI.h"
55 typedef struct _StateRec {
57 XlcCharSet GL_charset;
58 XlcCharSet GR_charset;
59 XlcCharSet ext_seg_charset;
63 typedef struct _CTDataRec {
65 char *encoding; /* Compound Text encoding */
68 typedef struct _CTInfoRec {
71 char *encoding; /* Compound Text encoding */
73 char *ext_segment; /* extended segment */
74 struct _CTInfoRec *next;
77 static CTDataRec default_ct_data[] =
79 { "ISO8859-1:GL", "\033(B" },
80 { "ISO8859-1:GR", "\033-A" },
81 { "ISO8859-2:GR", "\033-B" },
82 { "ISO8859-3:GR", "\033-C" },
83 { "ISO8859-4:GR", "\033-D" },
84 { "ISO8859-7:GR", "\033-F" },
85 { "ISO8859-6:GR", "\033-G" },
86 { "ISO8859-8:GR", "\033-H" },
87 { "ISO8859-5:GR", "\033-L" },
88 { "ISO8859-9:GR", "\033-M" },
89 { "JISX0201.1976-0:GL", "\033(J" },
90 { "JISX0201.1976-0:GR", "\033)I" },
92 { "GB2312.1980-0:GL", "\033$(A" },
93 { "GB2312.1980-0:GR", "\033$)A" },
94 { "JISX0208.1983-0:GL", "\033$(B" },
95 { "JISX0208.1983-0:GR", "\033$)B" },
96 { "KSC5601.1987-0:GL", "\033$(C" },
97 { "KSC5601.1987-0:GR", "\033$)C" },
99 { "JISX0212.1990-0:GL", "\033$(D" },
100 { "JISX0212.1990-0:GR", "\033$)D" },
101 { "CNS11643.1986-1:GL", "\033$(G" },
102 { "CNS11643.1986-1:GR", "\033$)G" },
103 { "CNS11643.1986-2:GL", "\033$(H" },
104 { "CNS11643.1986-2:GR", "\033$)H" },
106 /* Non-Standard Character Set Encodings */
107 { "TIS620.2533-1:GR", "\033-T"},
114 #define XctESC 0x001b
117 #define XctCSI 0x009b
120 #define XctCntrlFunc 0x0023
122 #define XctOtherCoding 0x0025
123 #define XctGL94 0x0028
124 #define XctGR94 0x0029
125 #define XctGR96 0x002d
126 #define XctNonStandard 0x002f
127 #define XctIgnoreExt 0x0030
128 #define XctNotIgnoreExt 0x0031
129 #define XctLeftToRight 0x0031
130 #define XctRightToLeft 0x0032
131 #define XctDirection 0x005d
132 #define XctDirectionEnd 0x005d
134 #define XctGL94MB 0x2428
135 #define XctGR94MB 0x2429
136 #define XctExtSeg 0x252f
137 #define XctOtherSeg 0x2f00
139 #define XctESCSeq 0x1b00
140 #define XctCSISeq 0x9b00
142 #define SKIP_I(str) while (*(str) >= 0x20 && *(str) <= 0x2f) (str)++;
143 #define SKIP_P(str) while (*(str) >= 0x30 && *(str) <= 0x3f) (str)++;
152 } CTParseRec, *CTParse;
154 static CTInfo ct_list = NULL;
157 _XlcGetCTInfoFromEncoding(encoding, length)
158 register char *encoding;
161 register CTInfo ct_info;
163 for (ct_info = ct_list; ct_info; ct_info = ct_info->next) {
164 if (length >= ct_info->encoding_len) {
165 if (ct_info->ext_segment) {
166 if (!strncmp(ct_info->encoding, encoding, 4) &&
167 !strncmp(ct_info->ext_segment, encoding + 6,
168 ct_info->ext_segment_len))
170 } else if (!strncmp(ct_info->encoding, encoding,
171 ct_info->encoding_len)) {
177 return (CTInfo) NULL;
181 _XlcParseCT(parse, text, length)
182 register CTParse parse;
186 unsigned int ret = 0;
188 register unsigned char *str = (unsigned char *) *text;
190 bzero((char *) parse, sizeof(CTParseRec));
192 switch (ch = *str++) {
194 if (*str == XctOtherCoding && *(str + 1) == XctNonStandard
195 && *(str + 2) >= 0x30 && *(str + 2) <= 0x3f && *length >= 6) {
197 /* non-standard encodings */
198 parse->side = XlcGLGR;
202 parse->char_size = *str - 0x30;
203 if (parse->char_size == 0) parse->char_size = 1;
205 parse->ct_info = _XlcGetCTInfoFromEncoding(*text, *length);
209 parse->ext_seg_length = (*str - 128) * 128 + *(str + 1) - 128;
213 } else if (*str == XctCntrlFunc && *length >= 4 &&
214 *(str + 1) >= 0x20 && *(str + 1) <= 0x2f &&
215 (*(str + 2) == XctIgnoreExt ||
216 *(str + 2) == XctNotIgnoreExt)) {
218 /* ignore extension or not */
220 parse->version = *str++ - 0x20;
226 if (*str == XctMB) { /* multiple-byte sets */
227 parse->char_size = 2;
230 parse->char_size = 1;
235 parse->set_size = 94;
236 ret = (parse->char_size == 1) ? XctGL94 : XctGL94MB;
240 parse->set_size = 94;
241 ret = (parse->char_size == 1) ? XctGR94 : XctGR94MB;
244 if (parse->char_size == 1) {
246 parse->set_size = 96;
253 if (*str >= 0x24 && *str <= 0x2f) { /* non-standard */
261 if (ret && *str < 0x40) /* non-standard */
264 if (*str < 0x30 || *str > 0x7e || (char *) str - *text >= *length)
270 if (parse->char_size == 2) {
272 parse->char_size = 4;
273 else if (*str >= 0x60)
274 parse->char_size = 3;
276 parse->ct_info = _XlcGetCTInfoFromEncoding(*text, *length);
282 if (*str == XctLeftToRight && *(str + 1) == XctDirection) {
283 ret = XctLeftToRight;
286 } else if (*str == XctRightToLeft && *(str + 1) == XctDirection) {
287 ret = XctRightToLeft;
290 } else if (*str == XctDirectionEnd) {
291 ret = XctDirectionEnd;
299 if (*str < 0x40 && *str > 0x7e)
313 if (ch == XctHT || ch == XctNL)
324 *length -= (char *) str - *text;
325 *text = (char *) str;
331 _fallcAddCT(name, encoding)
338 char *ct_ptr = encoding;
342 length = strlen(encoding);
344 switch (type = _XlcParseCT(&parse, &ct_ptr, &length)) {
351 if (parse.ct_info) /* existed */
352 return parse.ct_info->charset;
355 return (XlcCharSet) NULL;
358 charset = _fallcCreateDefaultCharSet(name, encoding);
360 return (XlcCharSet) NULL;
361 _fallcAddCharSet(charset);
363 ct_info = (CTInfo) Xmalloc(sizeof(CTInfoRec));
365 return (XlcCharSet) NULL;
367 ct_info->charset = charset;
368 ct_info->encoding = charset->ct_sequence;
369 ct_info->encoding_len = strlen(ct_info->encoding);
370 if (type == XctExtSeg) {
371 ct_info->ext_segment = ct_info->encoding + 6;
372 ct_info->ext_segment_len = strlen(ct_info->ext_segment);
374 ct_info->ext_segment = NULL;
375 ct_info->ext_segment_len = 0;
377 ct_info->next = ct_list;
384 _XlcGetCTInfoFromCharSet(charset)
385 register XlcCharSet charset;
387 register CTInfo ct_info;
389 for (ct_info = ct_list; ct_info; ct_info = ct_info->next)
390 if (ct_info->charset == charset)
393 return (CTInfo) NULL;
397 _fallcParseCharSet(charset)
401 char *ptr, buf[BUFSIZ];
405 if (charset->ct_sequence == NULL)
408 ptr = charset->ct_sequence;
409 length = strlen(ptr);
411 type = _XlcParseCT(&parse, &ptr, &length);
414 charset->xrm_name = falrmStringToQuark(charset->name);
416 strcpy(buf, charset->name);
417 if (ptr = strchr(buf, ':'))
419 charset->xrm_encoding_name = falrmStringToQuark(buf);
420 charset->encoding_name = falrmQuarkToString(charset->xrm_encoding_name);
422 charset->xrm_name = 0;
423 charset->encoding_name = NULL;
424 charset->xrm_encoding_name = 0;
427 charset->side = parse.side;
428 charset->char_size = parse.char_size;
429 charset->set_size = parse.set_size;
434 static void init_converter();
439 register XlcCharSet charset;
440 register CTData ct_data;
443 if (ct_list == NULL) {
444 num = sizeof(default_ct_data) / sizeof(CTDataRec);
445 for (ct_data = default_ct_data; num-- > 0; ct_data++) {
446 charset = _fallcAddCT(ct_data->name, ct_data->encoding);
458 _XlcCheckCTSequence(state, ctext, ctext_len)
468 _XlcParseCT(&parse, ctext, ctext_len);
470 ct_info = parse.ct_info;
471 if (parse.ext_seg_length > 0) { /* XctExtSeg or XctOtherSeg */
473 length = ct_info->ext_segment_len;
475 *ctext_len -= length;
476 state->ext_seg_left = parse.ext_seg_length - length;
477 state->ext_seg_charset = ct_info->charset;
479 state->ext_seg_left = parse.ext_seg_length;
480 state->ext_seg_charset = NULL;
482 } else if (ct_info) {
483 if (charset = ct_info->charset) {
484 if (charset->side == XlcGL)
485 state->GL_charset = charset;
486 else if (charset->side == XlcGR)
487 state->GR_charset = charset;
499 State state = (State) conv->state;
500 static XlcCharSet GL_charset = NULL;
501 static XlcCharSet GR_charset = NULL;
503 if (GL_charset == NULL) {
504 GL_charset = _fallcGetCharSet("ISO8859-1:GL");
505 GR_charset = _fallcGetCharSet("ISO8859-1:GR");
508 state->GL_charset = state->charset = GL_charset;
509 state->GR_charset = GR_charset;
510 state->ext_seg_charset = NULL;
511 state->ext_seg_left = 0;
515 cttocs(conv, from, from_left, to, to_left, args, num_args)
524 register State state = (State) conv->state;
525 register unsigned char ch;
527 XlcCharSet charset = NULL;
528 char *ctptr, *bufptr;
529 int ctext_len, buf_len;
531 ctptr = *((char **) from);
532 bufptr = *((char **) to);
533 ctext_len = *from_left;
536 while (ctext_len > 0 && buf_len > 0) {
537 if (state->ext_seg_left > 0) {
538 length = min(state->ext_seg_left, ctext_len);
539 length = min(length, buf_len);
542 state->ext_seg_left -= length;
544 if (state->ext_seg_charset) {
545 charset = state->ext_seg_charset;
547 if (charset->side == XlcGL) {
549 *bufptr++ = *ctptr++ & 0x7f;
550 } else if (charset->side == XlcGR) {
552 *bufptr++ = *ctptr++ | 0x80;
555 *bufptr++ = *ctptr++;
558 if (state->ext_seg_left < 1)
559 state->ext_seg_charset = NULL;
563 ch = *((unsigned char *) ctptr);
564 if (ch == 0x1b || ch == 0x9b) {
565 length = _XlcCheckCTSequence(state, &ctptr, &ctext_len);
568 if (state->ext_seg_left > 0 && charset)
572 if (charset != (ch & 0x80 ? state->GR_charset :
576 charset = ch & 0x80 ? state->GR_charset : state->GL_charset;
578 if ((ch < 0x20 && ch != '\0' && ch != '\n' && ch != '\t') ||
579 (ch >= 0x80 && ch < 0xa0))
582 *bufptr++ = *ctptr++;
589 state->charset = charset;
591 *((XlcCharSet *) args[0]) = state->charset;
593 *from_left -= ctptr - *((char **) from);
594 *from = (XPointer) ctptr;
596 *to_left -= bufptr - *((char **) to);
597 *to = (XPointer) bufptr;
603 cstoct(conv, from, from_left, to, to_left, args, num_args)
612 State state = (State) conv->state;
614 unsigned char min_ch, max_ch;
615 register unsigned char ch;
620 int csstr_len, ct_len;
625 csptr = *((char **) from);
626 ctptr = *((char **) to);
627 csstr_len = *from_left;
630 charset = (XlcCharSet) args[0];
632 ct_info = _XlcGetCTInfoFromCharSet(charset);
636 side = charset->side;
638 if (ct_info->ext_segment) {
639 if (charset != state->ext_seg_charset && state->ext_seg_left < 1) {
640 length = ct_info->encoding_len;
643 strcpy(ctptr, ct_info->encoding);
644 ctptr[4] = ((ct_info->ext_segment_len + csstr_len) / 128) | 0x80;
645 ctptr[5] = ((ct_info->ext_segment_len + csstr_len) % 128) | 0x80;
648 state->ext_seg_left = csstr_len;
650 length = min(state->ext_seg_left, csstr_len);
651 state->ext_seg_left -= length;
655 *ctptr++ = *csptr++ & 0x7f;
656 } else if (side == XlcGR) {
658 *ctptr++ = *csptr++ | 0x80;
663 state->ext_seg_charset = (state->ext_seg_left > 0) ? charset : NULL;
665 if ((side == XlcGR && charset != state->GR_charset) ||
666 (side == XlcGL && charset != state->GL_charset)) {
668 ct_len -= ct_info->encoding_len;
671 strcpy(ctptr, ct_info->encoding);
672 ctptr += ct_info->encoding_len;
678 if (charset->set_size == 94) {
680 if (charset->char_size > 1 || side == XlcGR)
684 while (csstr_len > 0 && ct_len > 0) {
685 ch = *((unsigned char *) csptr++) & 0x7f;
686 if (ch < min_ch || ch > max_ch)
687 if (ch != 0x00 && ch != 0x09 && ch != 0x0a && ch != 0x1b)
690 *ctptr++ = ch & 0x7f;
691 else if (side == XlcGR)
692 *ctptr++ = ch | 0x80;
699 state->GR_charset = charset;
700 else if (side == XlcGL)
701 state->GL_charset = charset;
704 *from_left -= csptr - *((char **) from);
705 *from = (XPointer) csptr;
707 *to_left -= ctptr - *((char **) to);
708 *to = (XPointer) ctptr;
714 strtocs(conv, from, from_left, to, to_left, args, num_args)
723 State state = (State) conv->state;
724 register char *src, *dst;
728 src = (char *) *from;
731 length = min(*from_left, *to_left);
732 side = *((unsigned char *) src) & 0x80;
734 while (side == (*((unsigned char *) src) & 0x80) && length-- > 0)
737 *from_left -= src - (char *) *from;
738 *from = (XPointer) src;
739 *to_left -= dst - (char *) *to;
740 *to = (XPointer) dst;
743 *((XlcCharSet *)args[0]) = side ? state->GR_charset : state->GL_charset;
749 cstostr(conv, from, from_left, to, to_left, args, num_args)
758 State state = (State) conv->state;
759 char *csptr, *string_ptr;
760 int csstr_len, str_len;
764 if (num_args < 1 || (state->GL_charset != (XlcCharSet) args[0] &&
765 state->GR_charset != (XlcCharSet) args[0]))
768 csptr = *((char **) from);
769 string_ptr = *((char **) to);
770 csstr_len = *from_left;
773 while (csstr_len-- > 0 && str_len > 0) {
774 ch = *((unsigned char *) csptr++);
775 if ((ch < 0x20 && ch != 0x00 && ch != 0x09 && ch != 0x0a) ||
776 ch == 0x7f || ((ch & 0x80) && ch < 0xa0)) {
780 *((unsigned char *) string_ptr++) = ch;
784 *from_left -= csptr - *((char **) from);
785 *from = (XPointer) csptr;
787 *to_left -= string_ptr - *((char **) to);
788 *to = (XPointer) string_ptr;
795 close_converter(conv)
799 Xfree((char *) conv->state);
801 Xfree((char *) conv);
806 XlcConvMethods methods;
808 register XlcConv conv;
810 conv = (XlcConv) Xmalloc(sizeof(XlcConvRec));
812 return (XlcConv) NULL;
814 conv->state = (XPointer) Xmalloc(sizeof(StateRec));
815 if (conv->state == NULL)
818 conv->methods = methods;
825 close_converter(conv);
827 return (XlcConv) NULL;
830 static XlcConvMethodsRec cttocs_methods = {
837 open_cttocs(from_lcd, from_type, to_lcd, to_type)
843 return create_conv(&cttocs_methods);
846 static XlcConvMethodsRec cstoct_methods = {
853 open_cstoct(from_lcd, from_type, to_lcd, to_type)
859 return create_conv(&cstoct_methods);
862 static XlcConvMethodsRec strtocs_methods = {
869 open_strtocs(from_lcd, from_type, to_lcd, to_type)
875 return create_conv(&strtocs_methods);
878 static XlcConvMethodsRec cstostr_methods = {
885 open_cstostr(from_lcd, from_type, to_lcd, to_type)
891 return create_conv(&cstostr_methods);
897 _fallcSetConverter((XLCd) NULL, XlcNCompoundText, (XLCd) NULL, XlcNCharSet,
899 _fallcSetConverter((XLCd) NULL, XlcNString, (XLCd) NULL, XlcNCharSet,
902 _fallcSetConverter((XLCd) NULL, XlcNCharSet, (XLCd) NULL, XlcNCompoundText,
904 _fallcSetConverter((XLCd) NULL, XlcNCharSet, (XLCd) NULL, XlcNString,