aboutsummaryrefslogblamecommitdiffstats
path: root/libwc/iso2022.c
blob: 59f35de81e4f7b1c202f997c1e6221be75124da4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066






















































































































































































































































































































































































































                                                                                

                                                                




























                                                                  

                                                                 

                       

                                                                


                        

                                                                 

                        

                                                                 





                                 

                                                                





                                                                      

                                                                




















































































































































































































































































































































































































































































































































































































                                                                                   
#include "wc.h"
#include "iso2022.h"
#include "jis.h"
#include "big5.h"
#include "johab.h"
#include "wtf.h"
#ifdef USE_UNICODE
#include "ucs.h"
#endif

#define C0  WC_ISO_MAP_C0
#define C1  WC_ISO_MAP_C1
#define GL  WC_ISO_MAP_GL
#define GR  WC_ISO_MAP_GR
#define GL2 WC_ISO_MAP_GL96
#define GR2 WC_ISO_MAP_GR96
#define SO  WC_ISO_MAP_SO
#define SI  WC_ISO_MAP_SI
#define ESC WC_ISO_MAP_ESC
#define SS2 WC_ISO_MAP_SS2
#define SS3 WC_ISO_MAP_SS3

wc_uint8 WC_ISO_MAP[ 0x100 ] = {
   C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, SO, SI,
   C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, C0, ESC,C0, C0, C0, C0,
   GL2,GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
   GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
   GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
   GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
   GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL,
   GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL, GL2,

   C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, SS2,SS3,
   C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
   GR2,GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR,
   GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR,
   GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR,
   GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR,
   GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR,
   GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR, GR2,
};

static wc_uchar cs94_gmap[ 0x80 - WC_F_ISO_BASE ];
static wc_uchar cs94w_gmap[ 0x80 - WC_F_ISO_BASE ];
static wc_uchar cs96_gmap[ 0x80 - WC_F_ISO_BASE ];
static wc_uchar cs96w_gmap[ 0x80 - WC_F_ISO_BASE ];
static wc_uchar cs942_gmap[ 0x80 - WC_F_ISO_BASE ];

static void
wtf_push_iso2022(Str os, wc_ccs ccs, wc_uint32 code)
{
    switch (ccs) {
    case WC_CCS_JIS_C_6226:
    case WC_CCS_JIS_X_0208:
    case WC_CCS_JIS_X_0213_1:
	ccs = wc_jisx0208_or_jisx02131(code);
	break;
    case WC_CCS_JIS_X_0212:
    case WC_CCS_JIS_X_0213_2:
	ccs = wc_jisx0212_or_jisx02132(code);
	break;
    case WC_CCS_JIS_X_0201:
    case WC_CCS_GB_1988:
	ccs = WC_CCS_US_ASCII;
	break;
    }
    wtf_push(os, ccs, code);
}

Str
wc_conv_from_iso2022(Str is, wc_ces ces)
{
    Str os;
    wc_uchar *sp = (wc_uchar *)is->ptr;
    wc_uchar *ep = sp + is->length;
    wc_uchar *p, *q = NULL;
    int state = WC_ISO_NOSTATE;
    wc_status st;
    wc_ccs gl_ccs, gr_ccs;

    for (p = sp; p < ep && !(WC_ISO_MAP[*p] & WC_ISO_MAP_DETECT); p++)
	;
    if (p == ep)
	return is;
    os = Strnew_size(is->length);
    if (p > sp)
	Strcat_charp_n(os, is->ptr, (int)(p - sp));

    wc_input_init(ces, &st);
    gl_ccs = st.design[st.gl];
    gr_ccs = st.design[st.gr];

    for (; p < ep; p++) {
	switch (state) {
	case WC_ISO_NOSTATE:
	    switch (WC_ISO_MAP[*p]) {
	    case GL2:
		gl_ccs = st.ss ? st.design[st.ss]
			: st.design[st.gl];
		if (!(WC_CCS_TYPE(gl_ccs) & WC_CCS_A_CS96)) {
		    Strcat_char(os, (char)*p);
		    break;
		}
	    case GL:
		gl_ccs = st.ss ? st.design[st.ss]
			: st.design[st.gl];
		if (WC_CCS_IS_WIDE(gl_ccs)) {
		    q = p;
		    state = WC_ISO_MBYTE1;
		    continue;
		} else if (gl_ccs == WC_CES_US_ASCII)
		    Strcat_char(os, (char)*p);
		else
		    wtf_push_iso2022(os, gl_ccs, (wc_uint32)*p);
		break;
	    case GR2:
		gr_ccs = st.ss ? st.design[st.ss]
			: st.design[st.gr];
		if (!(WC_CCS_TYPE(gr_ccs) & WC_CCS_A_CS96)) {
		    wtf_push_unknown(os, p, 1);
		    break;
		}
	    case GR:
		gr_ccs = st.ss ? st.design[st.ss]
			: st.design[st.gr];
		if (WC_CCS_IS_WIDE(gr_ccs)) {
		    q = p;
		    state = WC_EUC_MBYTE1;
		    continue;
		} else if (gr_ccs)
		    wtf_push_iso2022(os, gr_ccs, (wc_uint32)*p);
		else
		    wtf_push_unknown(os, p, 1);
		break;
	    case C0:
		Strcat_char(os, (char)*p);
		break;
	    case C1:
		wtf_push(os, WC_CCS_C1, (wc_uint32)*p);
		break;
	    case ESC:
		st.ss = 0;
		if (wc_parse_iso2022_esc(&p, &st))
		    state = st.state;
		else
		    Strcat_char(os, (char)*p);
		continue;
	    case SI:
		st.gl = 0;
		break;
	    case SO:
		st.gl = 1;
		break;
	    case SS2:
		if (! st.design[2]) {
		    wtf_push_unknown(os, p, 1);
		    break;
		}
		st.ss = 2;
		continue;
	    case SS3:
		if (! st.design[3]) {
		    wtf_push_unknown(os, p, 1);
		    break;
		}
		st.ss = 3;
		continue;
	    }
	    break;
	case WC_ISO_MBYTE1:
	    switch (WC_ISO_MAP[*p]) {
	    case GL2:
		if (!(WC_CCS_TYPE(gl_ccs) & WC_CCS_A_CS96)) {
		    Strcat_char(os, (char)*q);
		    Strcat_char(os, (char)*p);
		    break;
		}
	    case GL:
		wtf_push_iso2022(os, gl_ccs, ((wc_uint32)*q << 8) | *p);
		break;
	    default:
		wtf_push_unknown(os, q, 2);
		break;
	    }
	    break;
	case WC_EUC_MBYTE1:
	    switch (WC_ISO_MAP[*p]) {
	    case GR2:
		if (!(WC_CCS_TYPE(gr_ccs) & WC_CCS_A_CS96)) {
		    wtf_push_unknown(os, q, 2);
		    break;
		}
	    case GR:
		if (gr_ccs == WC_CCS_CNS_11643_X) {
		    state = WC_EUC_TW_MBYTE2;
		    continue;
		}
		wtf_push_iso2022(os, gr_ccs, ((wc_uint32)*q << 8) | *p);
		break;
	    default:
		wtf_push_unknown(os, q, 2);
		break;
	    }
	    break;
	case WC_EUC_TW_MBYTE2:
	    if (WC_ISO_MAP[*p] == GR) {
		if (0xa1 <= *q && *q <= 0xa7) {
		    wtf_push_iso2022(os, WC_CCS_CNS_11643_1 + (*q - 0xa1),
			((wc_uint32)*(q+1) << 8) | *p);
		    break;
		}
		if (0xa8 <= *q && *q <= 0xb0) {
		    wtf_push_iso2022(os, WC_CCS_CNS_11643_8 + (*q - 0xa8),
			((wc_uint32)*(q+1) << 8) | *p);
		    break;
		}
	    }
	    wtf_push_unknown(os, q, 3);
	    break;
	case WC_ISO_CSWSR:
	    if (*p == WC_C_ESC && *(p+1) == WC_C_CSWSR) {
		if (*(p+2) == WC_F_ISO_BASE) {
		    state = st.state = WC_ISO_NOSTATE;
		    p += 2;
		    continue;
		} else if (*(p+2) > WC_F_ISO_BASE && *(p+2) <= 0x7e) {
		    p += 2;
		    continue;
		}
	    }
	    wtf_push_unknown(os, p, 1);
	    continue;
	case WC_ISO_CSWOSR:
	    wtf_push_unknown(os, p, ep - p);
	    return os;
	    break;
	}
	st.ss = 0;
	state = WC_ISO_NOSTATE;
    }
    switch (state) {
    case WC_ISO_MBYTE1:
    case WC_EUC_MBYTE1:
	wtf_push_unknown(os, p-1, 1);
	break;
    case WC_EUC_TW_MBYTE1:
	wtf_push_unknown(os, p-2, 2);
	break;
    }
    return os;
}

int
wc_parse_iso2022_esc(wc_uchar **ptr, wc_status *st)
{
    wc_uchar *p = *ptr, state, f = 0, g = 0, cs = 0;

    if (*p != WC_C_ESC)
	return 0;
    state = *p;
    for (p++; *p && state; p++) {
	switch (state) {
	case WC_C_ESC:		/* ESC */
	    switch (*p) {
	    case WC_C_MBCS:	/* ESC '$' */
		state = *p;
		continue;
	    case WC_C_G0_CS94:	/* ESC '(' */
	    case WC_C_G1_CS94:	/* ESC ')' */
	    case WC_C_G2_CS94:	/* ESC '*' */
	    case WC_C_G3_CS94:	/* ESC '+' */
		state = cs = WC_C_G0_CS94;
		g = *p & 0x03;
		continue;
	    case WC_C_G0_CS96:	/* ESC ',' */ /* ISO 2022 does not permit */
	    case WC_C_G1_CS96:	/* ESC '-' */
	    case WC_C_G2_CS96:	/* ESC '.' */
	    case WC_C_G3_CS96:	/* ESC '/' */
		state = cs = WC_C_G0_CS96;
		g = *p & 0x03;
		continue;
	    case WC_C_C0:	/* ESC '!' */ /* not suported */
	    case WC_C_C1:	/* ESC '"' */ /* not suported */
	    case WC_C_REP:	/* ESC '&' */ /* not suported */
		state = cs = WC_C_C0;
		continue;
	    case WC_C_CSWSR:	/* ESC '%' */ /* not suported */
		state = cs = WC_C_CSWSR;
		continue;
	    case WC_C_SS2:	/* ESC 'N' */
		st->ss = 2; *ptr = p; return 1;
	    case WC_C_SS3:	/* ESC 'O' */
		st->ss = 3; *ptr = p; return 1;
	    case WC_C_LS2:	/* ESC 'n' */
		st->gl = 2; *ptr = p; return 1;
	    case WC_C_LS3:	/* ESC 'o' */
		st->gl = 3; *ptr = p; return 1;
	    case WC_C_LS1R:	/* ESC '~' */
		st->gr = 1; *ptr = p; return 1;
	    case WC_C_LS2R:	/* ESC '}' */
		st->gr = 2; *ptr = p; return 1;
	    case WC_C_LS3R:	/* ESC '|' */
		st->gr = 3; *ptr = p; return 1;
	    default:
		return 0;
	    }
	    break;
	case WC_C_MBCS:		/* ESC '$' */
	    switch (*p) {
	    case WC_F_JIS_C_6226:	/* ESC '$' @ */
	    case WC_F_JIS_X_0208:	/* ESC '$' B */
	    case WC_F_GB_2312:	/* ESC '$' A */
		state = 0;
		cs = WC_C_G0_CS94 | 0x80;
		g = 0;
		f = *p;
		break;
	    case WC_C_G0_CS94:	/* ESC '$' '(' */
	    case WC_C_G1_CS94:	/* ESC '$' ')' */
	    case WC_C_G2_CS94:	/* ESC '$' '*' */
	    case WC_C_G3_CS94:	/* ESC '$' '+' */
		state = cs = WC_C_G0_CS94 | 0x80;
		g = *p & 0x03;
		continue;
	    case WC_C_G0_CS96:	/* ESC '$' ',' */ /* ISO 2022 does not permit */
	    case WC_C_G1_CS96:	/* ESC '$' '-' */
	    case WC_C_G2_CS96:	/* ESC '$' '.' */
	    case WC_C_G3_CS96:	/* ESC '$' '/' */
		state = cs = WC_C_G0_CS96 | 0x80;
		g = *p & 0x03;
		continue;
	    default:
		return 0;
	    }
	    break;
	case WC_C_G0_CS94:	/* ESC [()*+] F */
	    if (*p == WC_C_CS942) {	/* ESC [()*+] '!' */
		state = cs = WC_C_CS942 | 0x80;
		g = *p & 0x03;
		continue;
	    }
	case WC_C_G0_CS96:	/* ESC [,-./] F */
	case WC_C_G0_CS94 | 0x80:	/* ESC '$' [()*+] F */
	case WC_C_G0_CS96 | 0x80:	/* ESC '$' [,-./] F */
	case WC_C_CS942 | 0x80:	/* ESC [()*+] '!' F */
	case WC_C_C0:		/* ESC [!"&] F */
	case WC_C_CSWSR | 0x80:	/* ESC '%' '/' F */
	    state = 0;
	    f = *p;
	    break;
	case WC_C_CSWSR:	/* ESC '%' F */
	    if (*p == WC_C_CSWOSR) {	/* ESC '%' '/' */
		state = cs = WC_C_CSWSR | 0x80;
		continue;
	    }
	    state = 0;
	    f = *p;
	    break;
	default:
	    return 0;
	}
    }
    if (f < WC_F_ISO_BASE || f > 0x7e)
	return 0;
    switch (cs) {
    case WC_C_G0_CS94:
	st->design[g] = WC_CCS_SET_CS94(f);
	break;
    case WC_C_G0_CS94 | 0x80:
	st->design[g] = WC_CCS_SET_CS94W(f);
	break;
    case WC_C_G0_CS96:
	st->design[g] = WC_CCS_SET_CS96(f);
	break;
    case WC_C_G0_CS96 | 0x80:
	st->design[g] = WC_CCS_SET_CS96W(f);
	break;
    case WC_C_CS942 | 0x80:
	st->design[g] = WC_CCS_SET_CS942(f);
	break;
    case WC_C_CSWSR:
	if (f == WC_F_ISO_BASE)
	    st->state = WC_ISO_NOSTATE;
	else
	    st->state = WC_ISO_CSWSR;
	break;
    case WC_C_CSWOSR:
	st->state = WC_ISO_CSWOSR;
	break;
    }
    *ptr = p - 1;
    return 1;
}

void
wc_push_to_iso2022(Str os, wc_wchar_t cc, wc_status *st)
{
    wc_uchar g = 0;
    wc_bool is_wide = WC_FALSE, retry = WC_FALSE;
    wc_wchar_t cc2;

  while (1) {
    switch (WC_CCS_TYPE(cc.ccs)) {
    case WC_CCS_A_CS94:
	if (cc.ccs == WC_CCS_US_ASCII)
	    cc.ccs = st->g0_ccs;
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs94_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	break;
    case WC_CCS_A_CS94W:
	is_wide = 1;
	switch (cc.ccs) {
#ifdef USE_UNICODE
	case WC_CCS_JIS_X_0212:
	    if (!WcOption.use_jisx0212 && WcOption.use_jisx0213 &&
		WcOption.ucs_conv) {
		cc2 = wc_jisx0212_to_jisx0213(cc);
		if (cc2.ccs == WC_CCS_JIS_X_0213_1 ||
		    cc2.ccs == WC_CCS_JIS_X_0213_2) {
		    cc = cc2;
		    continue;
		}
	    }
	    break;
	case WC_CCS_JIS_X_0213_1:
	case WC_CCS_JIS_X_0213_2:
	    if (!WcOption.use_jisx0213 && WcOption.use_jisx0212 &&
		WcOption.ucs_conv) {
		cc2 = wc_jisx0213_to_jisx0212(cc);
		if (cc2.ccs == WC_CCS_JIS_X_0212) {
		    cc = cc2;
		    continue;
		}
	    }
	    break;
#endif
	}
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs94w_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	break;
    case WC_CCS_A_CS96:
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs96_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	break;
    case WC_CCS_A_CS96W:
	is_wide = 1;
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs96w_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	break;
    case WC_CCS_A_CS942:
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs942_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	break;
    case WC_CCS_A_UNKNOWN_W:
	if (WcOption.no_replace)
	    return;
	is_wide = 1;
	cc.ccs = WC_CCS_US_ASCII;
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs94_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	cc.code = ((wc_uint32)WC_REPLACE_W[0] << 8) | WC_REPLACE_W[1];
	break;
    case WC_CCS_A_UNKNOWN:
	if (WcOption.no_replace)
	    return;
	cc.ccs = WC_CCS_US_ASCII;
	if (WC_CCS_INDEX(cc.ccs) >= WC_F_ISO_BASE)
	    g = cs94_gmap[WC_CCS_INDEX(cc.ccs) - WC_F_ISO_BASE];
	cc.code = (wc_uint32)WC_REPLACE[0];
	break;
    default:
	if ((cc.ccs == WC_CCS_JOHAB || WC_CCS_JOHAB_1 ||
		cc.ccs == WC_CCS_JOHAB_2 || cc.ccs == WC_CCS_JOHAB_3) &&
		cs94w_gmap[WC_F_KS_X_1001 - WC_F_ISO_BASE]) {
	    wc_wchar_t cc2 = wc_johab_to_ksx1001(cc);
	    if (cc2.ccs == WC_CCS_KS_X_1001) {
		cc = cc2;
		continue;
	    }
	}
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_iso2022(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
    if (! g) {
#ifdef USE_UNICODE
	if (WcOption.ucs_conv && ! retry)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	retry = WC_TRUE;
	continue;
    }

    wc_push_iso2022_esc(os, cc.ccs, g, 1, st);
    if (is_wide)
	Strcat_char(os, (char)((cc.code >> 8) & 0x7f));
    Strcat_char(os, (char)(cc.code & 0x7f));
    return;
  }
}

void
wc_push_to_iso2022_end(Str os, wc_status *st)
{
    if (st->design[1] != 0 && st->design[1] != st->g1_ccs)
	wc_push_iso2022_esc(os, st->g1_ccs, WC_C_G1_CS94, 0, st);
    wc_push_iso2022_esc(os, st->g0_ccs, WC_C_G0_CS94, 1, st);
}

void
wc_push_iso2022_esc(Str os, wc_ccs ccs, wc_uchar g, wc_uint8 invoke, wc_status *st)
{
    wc_uint8 g_invoke = g & 0x03;

    if (st->design[g_invoke] != ccs) {
	Strcat_char(os, WC_C_ESC);
	if (WC_CCS_IS_WIDE(ccs)) {
	    Strcat_char(os, WC_C_MBCS);
	    if (g_invoke != 0 ||
		(ccs != WC_CCS_JIS_C_6226 &&
		 ccs != WC_CCS_JIS_X_0208 &&
		 ccs != WC_CCS_GB_2312))
		Strcat_char(os, (char)g);
	} else {
	    Strcat_char(os, (char)g);
	    if ((ccs & WC_CCS_A_ISO_2022) == WC_CCS_A_CS942)
		Strcat_char(os, WC_C_CS942);
	}
	Strcat_char(os, (char)WC_CCS_GET_F(ccs));
	st->design[g_invoke] = ccs;
    }
    if (! invoke)
	return;

    switch (g_invoke) {
    case 0:
	if (st->gl != 0) {
	    Strcat_char(os, WC_C_SI);
	    st->gl = 0;
	}
	break;
    case 1:
	if (st->gl != 1) {
	    Strcat_char(os, WC_C_SO);
	    st->gl = 1;
	}
	break;
    case 2:
	Strcat_char(os, WC_C_ESC);
	Strcat_char(os, WC_C_SS2);
	break;
    case 3:
	Strcat_char(os, WC_C_ESC);
	Strcat_char(os, WC_C_SS3);
	break;
    }
}

void
wc_push_to_euc(Str os, wc_wchar_t cc, wc_status *st)
{
    wc_ccs g1_ccs = st->ces_info->gset[1].ccs;

  while (1) {
    if (cc.ccs == g1_ccs) {
	Strcat_char(os, (char)((cc.code >> 8) | 0x80));
	Strcat_char(os, (char)((cc.code & 0xff) | 0x80));
	return;
    }
    switch (cc.ccs) {
    case WC_CCS_US_ASCII:
	Strcat_char(os, (char)cc.code);
	return;
    case WC_CCS_C1:
	Strcat_char(os, (char)(cc.code | 0x80));
	return;
    case WC_CCS_UNKNOWN_W:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE_W);
	return;
    case WC_CCS_UNKNOWN:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE);
	return;
    case WC_CCS_JOHAB:
    case WC_CCS_JOHAB_1:
    case WC_CCS_JOHAB_2:
    case WC_CCS_JOHAB_3:
	if (st->ces_info->id == WC_CES_EUC_KR) {
	    cc = wc_johab_to_ksx1001(cc);
	    continue;
	}
    default:
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
  }
}

void
wc_push_to_eucjp(Str os, wc_wchar_t cc, wc_status *st)
{
  while (1) {
    switch (cc.ccs) {
    case WC_CCS_US_ASCII:
	Strcat_char(os, (char)cc.code);
	return;
    case WC_CCS_JIS_X_0201K:
	if (WcOption.use_jisx0201k) {
	    Strcat_char(os, WC_C_SS2R);
	    Strcat_char(os, (char)(cc.code | 0x80));
	    return;
	} else if (WcOption.fix_width_conv)
	    cc.ccs = WC_CCS_UNKNOWN;
	else
	    cc = wc_jisx0201k_to_jisx0208(cc);
	continue;
    case WC_CCS_JIS_X_0208:
	break;
    case WC_CCS_JIS_X_0213_1:
	if (WcOption.use_jisx0213)
	    break;
#ifdef USE_UNICODE
	else if (WcOption.ucs_conv && WcOption.use_jisx0212)
	    cc = wc_jisx0213_to_jisx0212(cc);
#endif
	else
	    cc.ccs = WC_CCS_UNKNOWN_W;
	continue;
    case WC_CCS_JIS_X_0212:
	if (WcOption.use_jisx0212) {
	    Strcat_char(os, WC_C_SS3R);
	    break;
	}
#ifdef USE_UNICODE
	else if (WcOption.ucs_conv && WcOption.use_jisx0213)
	    cc = wc_jisx0212_to_jisx0213(cc);
#endif
	else
	    cc.ccs = WC_CCS_UNKNOWN_W;
	continue;
    case WC_CCS_JIS_X_0213_2:
	if (WcOption.use_jisx0213) {
	    Strcat_char(os, WC_C_SS3R);
	    break;
	}
#ifdef USE_UNICODE
	else if (WcOption.ucs_conv && WcOption.use_jisx0212)
	    cc = wc_jisx0213_to_jisx0212(cc);
#endif
	else
	    cc.ccs = WC_CCS_UNKNOWN_W;
	continue;
    case WC_CCS_C1:
	Strcat_char(os, (char)(cc.code | 0x80));
	return;
    case WC_CCS_UNKNOWN_W:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE_W);
	return;
    case WC_CCS_UNKNOWN:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE);
	return;
    default:
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
    Strcat_char(os, (char)((cc.code >> 8) | 0x80));
    Strcat_char(os, (char)((cc.code & 0xff) | 0x80));
    return;
  }
}

void
wc_push_to_euctw(Str os, wc_wchar_t cc, wc_status *st)
{
  while (1) {
    switch (cc.ccs) {
    case WC_CCS_US_ASCII:
	Strcat_char(os, (char)cc.code);
	return;
    case WC_CCS_CNS_11643_1:
	break;
    case WC_CCS_CNS_11643_2:
    case WC_CCS_CNS_11643_3:
    case WC_CCS_CNS_11643_4:
    case WC_CCS_CNS_11643_5:
    case WC_CCS_CNS_11643_6:
    case WC_CCS_CNS_11643_7:
	Strcat_char(os, WC_C_SS2R);
	Strcat_char(os, (char)(0xA1 + (cc.ccs - WC_CCS_CNS_11643_1)));
	break;
    case WC_CCS_CNS_11643_8:
    case WC_CCS_CNS_11643_9:
    case WC_CCS_CNS_11643_10:
    case WC_CCS_CNS_11643_11:
    case WC_CCS_CNS_11643_12:
    case WC_CCS_CNS_11643_13:
    case WC_CCS_CNS_11643_14:
    case WC_CCS_CNS_11643_15:
    case WC_CCS_CNS_11643_16:
	Strcat_char(os, WC_C_SS2R);
	Strcat_char(os, (char)(0xA8 + (cc.ccs - WC_CCS_CNS_11643_8)));
	break;
    case WC_CCS_C1:
	Strcat_char(os, (char)(cc.code | 0x80));
	return;
    case WC_CCS_UNKNOWN_W:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE_W);
	return;
    case WC_CCS_UNKNOWN:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE);
	return;
    default:
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
    Strcat_char(os, (char)((cc.code >> 8) | 0x80));
    Strcat_char(os, (char)((cc.code & 0xff) | 0x80));
    return;
  }
}

void
wc_push_to_iso8859(Str os, wc_wchar_t cc, wc_status *st)
{
    wc_ccs g1_ccs = st->ces_info->gset[1].ccs;

  while (1) {
    if (cc.ccs == g1_ccs) {
	Strcat_char(os, (char)(cc.code | 0x80));
	return;
    }
    switch (cc.ccs) {
    case WC_CCS_US_ASCII:
	Strcat_char(os, (char)cc.code);
	return;
    case WC_CCS_C1:
	Strcat_char(os, (char)(cc.code | 0x80));
	return;
    case WC_CCS_UNKNOWN_W:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE_W);
	return;
    case WC_CCS_UNKNOWN:
	if (!WcOption.no_replace)
	    Strcat_charp(os, WC_REPLACE);
	return;
    default:
#ifdef USE_UNICODE
	if (WcOption.ucs_conv)
	    cc = wc_any_to_any_ces(cc, st);
	else
#endif
	    cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
	continue;
    }
  }
}

void
wc_create_gmap(wc_status *st)
{
    wc_gset *gset = st->ces_info->gset;
    wc_uchar *gset_ext = st->ces_info->gset_ext;
    int i, f;

    if (WcOption.strict_iso2022) {
	for (i = 0; i < WC_F_ISO_BASE; i++) {
	    cs94_gmap[i] = 0;
	    cs96_gmap[i] = 0;
	    cs94w_gmap[i] = 0;
	    cs96w_gmap[i] = 0;
	    cs942_gmap[i] = 0;
	}
    } else {
	for (i = 0; i < WC_F_ISO_BASE; i++) {
	    cs94_gmap[i] = gset_ext[0];
	    cs96_gmap[i] = gset_ext[1];
	    cs94w_gmap[i] = gset_ext[2];
	    cs96w_gmap[i] = gset_ext[3];
	    cs942_gmap[i] = gset_ext[0];
	}
    }
    for (i = 0; gset[i].ccs; i++) {
	f = WC_CCS_GET_F(gset[i].ccs) - WC_F_ISO_BASE;
	switch (WC_CCS_TYPE(gset[i].ccs)) {
	case WC_CCS_A_CS94:
	    switch (gset[i].ccs) {
	    case WC_CCS_JIS_X_0201K:
		if (!WcOption.use_jisx0201k)
		    continue;
		break;
	    }
	    cs94_gmap[f] = gset[i].g;
	    break;
	case WC_CCS_A_CS94W:
	    switch (gset[i].ccs) {
	    case WC_CCS_JIS_X_0212:
		if (!WcOption.use_jisx0212)
		    continue;
		break;
	    case WC_CCS_JIS_X_0213_1:
	    case WC_CCS_JIS_X_0213_2:
		if (!WcOption.use_jisx0213)
		    continue;
		break;
	    }
	    cs94w_gmap[f] = gset[i].g;
	    break;
	case WC_CCS_A_CS96:
	    cs96_gmap[f] = gset[i].g;
	    break;
	case WC_CCS_A_CS96W:
	    cs96w_gmap[f] = gset[i].g;
	    break;
	case WC_CCS_A_CS942:
	    cs942_gmap[f] = gset[i].g;
	    break;
	}
    }
}

Str
wc_char_conv_from_iso2022(wc_uchar c, wc_status *st)
{
    static Str os;
    static wc_uchar buf[4];
    static size_t nbuf;
    wc_uchar *p;
    wc_ccs gl_ccs, gr_ccs;

    if (st->state == -1) {
	st->state = WC_ISO_NOSTATE;
	os = Strnew_size(8);
	nbuf = 0;
    }

    gl_ccs = st->ss ? st->design[st->ss] : st->design[st->gl];
    gr_ccs = st->ss ? st->design[st->ss] : st->design[st->gr];

    switch (st->state) {
    case WC_ISO_NOSTATE:
	switch (WC_ISO_MAP[c]) {
	case GL2:
	    if (!(WC_CCS_TYPE(gl_ccs) & WC_CCS_A_CS96)) {
		Strcat_char(os, (char)c);
		break;
	    }
	case GL:
	    if (WC_CCS_IS_WIDE(gl_ccs)) {
		buf[nbuf++] = c;
		st->state = WC_ISO_MBYTE1;
		return NULL;
	    } else if (gl_ccs == WC_CES_US_ASCII)
		Strcat_char(os, (char)c);
	    else
		wtf_push_iso2022(os, gl_ccs, (wc_uint32)c);
	    break;
	case GR2:
	    if (!(WC_CCS_TYPE(gr_ccs) & WC_CCS_A_CS96))
		break;
	case GR:
	    if (WC_CCS_IS_WIDE(gr_ccs)) {
		buf[nbuf++] = c;
		st->state = WC_EUC_MBYTE1;
		return NULL;
	    } else if (gr_ccs)
		wtf_push_iso2022(os, gr_ccs, (wc_uint32)c);
	    break;
	case C0:
	    Strcat_char(os, (char)c);
	    break;
	case C1:
	    break;
	case ESC:
	    buf[nbuf++] = c;
	    st->state = WC_C_ESC;
	    return NULL;
	case SI:
	    st->gl = 0;
	    break;
	case SO:
	    st->gl = 1;
	    break;
	case SS2:
	    if (! st->design[2])
		return os;
	    st->ss = 2;
	    return NULL;
	case SS3:
	    if (! st->design[3])
		return os;
	    st->ss = 3;
	    return NULL;
	}
	break;
    case WC_ISO_MBYTE1:
	switch (WC_ISO_MAP[c]) {
	case GL2:
	    if (!(WC_CCS_TYPE(gl_ccs) & WC_CCS_A_CS96))
		break;
	case GL:
	    buf[nbuf++] = c;
	    wtf_push_iso2022(os, gl_ccs, ((wc_uint32)buf[0] << 8) | buf[1]);
	    break;
	}
	st->state = WC_ISO_NOSTATE;
	break;
    case WC_EUC_MBYTE1:
	switch (WC_ISO_MAP[c]) {
	case GR2:
	    if (!(WC_CCS_TYPE(gr_ccs) & WC_CCS_A_CS96))
		break;
	case GR:
	    if (gr_ccs == WC_CCS_CNS_11643_X) {
		buf[nbuf++] = c;
		st->state = WC_EUC_TW_MBYTE2;
		return NULL;
	    }
	    buf[nbuf++] = c;
	    wtf_push_iso2022(os, gr_ccs, ((wc_uint32)buf[0] << 8) | buf[1]);
	    break;
	}
	st->state = WC_ISO_NOSTATE;
	break;
    case WC_EUC_TW_MBYTE2:
	if (WC_ISO_MAP[c] == GR) {
	    buf[nbuf++] = c;
	    c = buf[0];
	    if (0xa1 <= c && c <= 0xa7) {
		wtf_push_iso2022(os, WC_CCS_CNS_11643_1 + (c - 0xa1),
			((wc_uint32)buf[1] << 8) | buf[2]);
		break;
	    }
	    if (0xa8 <= c && c <= 0xb0) {
		wtf_push_iso2022(os, WC_CCS_CNS_11643_8 + (c - 0xa8),
			((wc_uint32)buf[1] << 8) | buf[2]);
		break;
	    }
	}
	st->state = WC_ISO_NOSTATE;
	break;
    case WC_C_ESC:
	switch (c) {
	case WC_C_G0_CS94:
	case WC_C_G1_CS94:
	case WC_C_G2_CS94:
	case WC_C_G3_CS94:
	    buf[nbuf++] = c;
	    st->state = WC_C_G0_CS94;
	    return NULL;
	case WC_C_G0_CS96:
	case WC_C_G1_CS96:
	case WC_C_G2_CS96:
	case WC_C_G3_CS96:
	case WC_C_C0:
	case WC_C_C1:
	case WC_C_REP:
	    buf[nbuf++] = c;
	    st->state = WC_C_G0_CS96;
	    return NULL;
	case WC_C_MBCS:
	case WC_C_CSWSR:
	    buf[nbuf++] = c;
	    st->state = c;
	    return NULL;
	case WC_C_SS2:
	    st->ss = 2;
	    st->state = WC_ISO_NOSTATE;
	    return NULL;
	case WC_C_SS3:
	    st->ss = 3;
	    st->state = WC_ISO_NOSTATE;
	    return NULL;
	case WC_C_LS2:
	    st->gl = 2;
	    break;
	case WC_C_LS3:
	    st->gl = 3;
	    break;
	case WC_C_LS2R:
	    st->gr = 2;
	    break;
	case WC_C_LS3R:
	    st->gr = 3;
	    break;
	default:
	    break;
	}
	break;
    case WC_C_MBCS:
	switch (c) {
	case WC_F_JIS_C_6226:
	case WC_F_JIS_X_0208:
	case WC_F_GB_2312:
	    buf[nbuf++] = c;
	    p = buf;
	    wc_parse_iso2022_esc(&p, st);
	    break;
	case WC_C_G0_CS94:
	case WC_C_G1_CS94:
	case WC_C_G2_CS94:
	case WC_C_G3_CS94:
	case WC_C_G0_CS96:
	case WC_C_G1_CS96:
	case WC_C_G2_CS96:
	case WC_C_G3_CS96:
	    buf[nbuf++] = c;
	    st->state = WC_C_G0_CS96;
	    return NULL;
	}
	break;
    case WC_C_CSWSR:
	switch (c) {
	case WC_C_CSWOSR:
	    buf[nbuf++] = c;
	    st->state = WC_C_G1_CS94;
	    return NULL;
	}
	buf[nbuf++] = c;
	p = buf;
	wc_parse_iso2022_esc(&p, st);
	break;
    case WC_C_G0_CS94:
	switch (c) {
	case WC_C_CS942:
	    buf[nbuf++] = c;
	    st->state = WC_C_G0_CS96;
	    return NULL;
	}
    case WC_C_G0_CS96:
	buf[nbuf++] = c;
	p = buf;
	wc_parse_iso2022_esc(&p, st);
	break;
    }
    st->ss = 0;
    st->state = -1;
    return os;
}