1 /*
2  *  xutoj.c,v 1.7 2002/03/24 01:25:13 hiroo Exp
3  *  Canna: $Id: xutoj.c,v 1.3 2003/01/04 07:31:02 aida_s Exp $
4  */
5 
6 /*
7  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
8  * This file is part of FreeWnn.
9  *
10  * Copyright Kyoto University Research Institute for Mathematical Sciences
11  *                 1987, 1988, 1989, 1990, 1991, 1992
12  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
13  * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
14  * Copyright FreeWnn Project 1999, 2000, 2002
15  *
16  * Maintainer:  FreeWnn Project   <freewnn@tomo.gr.jp>
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36 
37 #define NEED_CR
38 
39 #if STDC_HEADERS
40 #  include <stdlib.h>
41 #  include <string.h>
42 #elif HAVE_STRINGS_H
43 #  include <strings.h>
44 #endif /* STDC_HEADERS */
45 
46 #include "commonhd.h"
47 #include "wnn_config.h"
48 #include "wnn_os.h"
49 
50 #define ECNS_IS_UCNS 1          /* The trust CNS is CNS11643 based on ISO2022,
51                                    but the CNS is binded on EUC */
52 
53 #define NON_LIMIT       0x7FFFFFFF
54 
55 #define G0      0
56 #define G1      1
57 #define G2      2
58 #define G3      3
59 #define SS      4
60 #define GL      1
61 #define GR      2
62 #define LS0     0x0f
63 #define LS1     0x0e
64 #define LS1R    0x7e
65 #define LS2     0x6e
66 #define LS2R    0x7d
67 #define LS3     0x6f
68 #define LS3R    0x7c
69 #define SS2     0x8e
70 #define SS3     0x8f
71 
72 
73 #define CS_MASK         0x8080
74 #define CS0_MASK        0x0000
75 #define CS1_MASK        0x8080
76 #define CS2_MASK        0x0080
77 #define CS3_MASK        0x8000
78 
79 #define CS1     0
80 #define CS2     1
81 #define CS3     2
82 
83 #define UJIS_CSWIDTH    "2,1,2"
84 #define UGB_CSWIDTH     "2,1,2"
85 #define UKSC_CSWIDTH    "2,1,2"
86 
87 typedef struct _CSWidthTable
88 {
89   int cs1, cs2, cs3;
90 }
91 CSWidthTable;
92 
93 typedef struct _cswidth_name_struct
94 {
95   char *lang;
96   char *name;
97   char *def_name;
98 }
99 cswidth_name_struct;
100 
101 typedef struct _DesignateTable
102 {
103   unsigned char *code;
104   unsigned int mask;
105 }
106 DesignateTable;
107 
108 static int _etc_cs[3] = { 2, 1, 2 };
109 static int _etc_cs_len[3] = { 2, 1, 2 };
110 static int cs_mask[3] = { 0x8080, 0x0080, 0x8000 };
111 
112 static int default_glr_mode[3] = { 0, G0, G1 };
113 static int save_glr_mode[2];
114 static int default_gn_len[4] = { 1, 1, 1, 2 };
115 static unsigned int default_gn_mask[4] = { 0x00, 0x80, 0x80, 0x8000 };
116 static DesignateTable default_designate[4] = {
117   {(unsigned char *) "(B", 0x0},
118   {(unsigned char *) NULL, 0x0}
119 };
120 
121 static int *glr_mode = default_glr_mode;
122 static int *gn_len = default_gn_len;
123 static unsigned int *gn_mask = default_gn_mask;
124 
125 static unsigned char save_seq[6] = { '\0' };
126 static int save_seq_len = 0;
127 static int pending_esc = 0;
128 static unsigned char pending = '\0';
129 static w_char pending_mask = (w_char) 0;
130 
131 static DesignateTable *designate = default_designate;
132 
133 #ifdef  JAPANESE
134 static DesignateTable JIS_designate[] = {
135   {(unsigned char *) "(B", 0x0},
136   {(unsigned char *) "(J", 0x0},
137   {(unsigned char *) "(I", 0x80},
138   {(unsigned char *) "$B", 0x8080},
139   {(unsigned char *) "$(B", 0x8080},
140   {(unsigned char *) ")I", 0x80},
141   {(unsigned char *) "$)B", 0x8080},
142   {(unsigned char *) "$)D", 0x8000},
143   {(unsigned char *) "$(D", 0x8000},
144   {(unsigned char *) NULL, 0x0}
145 };
146 #endif /* JAPANESE */
147 
148 #ifdef  CHINESE
149 #ifndef ECNS_IS_UCNS
150 static DesignateTable CNS_designate[] = {
151   {(unsigned char *) "(B", 0x0},
152   {(unsigned char *) "$)0", 0x8080},
153   {(unsigned char *) "$*1", 0x8000},
154   {NULL, 0x0}
155 };
156 #endif /* ECNS_IS_UCNS */
157 #endif /* CHINESE */
158 
159 #ifdef  KOREAN
160 static DesignateTable KSC_designate[] = {
161   {(unsigned char *) "(B", 0x0},
162   {(unsigned char *) "$(C", 0x8080},
163   {(unsigned char *) "$)C", 0x8080},
164   {(unsigned char *) NULL, 0x0}
165 };
166 #endif /* KOREAN */
167 
168 #if defined(JAPANESE) || defined(CHINESE) || defined(KOREAN)
169 static w_char tmp_w_buf[1000];
170 #endif
171 
172 static void
set_gn(dg)173 set_gn (dg)
174      DesignateTable *dg;
175 {
176   register char *p = (char *) dg->code;
177   register int len = 1, gn = 0;
178 
179   if (!strcmp (p, "$B"))
180     {                           /* JIS */
181       gn_len[0] = 2;
182       gn_mask[0] = dg->mask;
183       return;
184     }
185   if (*p == '$')
186     {
187       len = 2;
188       p++;
189     }
190   if (*p >= '(' && *p <= '+')
191     gn = *p - '(';
192   else if (*p >= '-' && *p <= '/')
193     gn = *p - '+';
194   else
195     return;
196   gn_len[gn] = len;
197   gn_mask[gn] = dg->mask;
198 }
199 
200 static int
check_designate(ec,eend,ret_buf)201 check_designate (ec, eend, ret_buf)
202      unsigned char *ec, *eend, **ret_buf;
203 {
204   register unsigned char *c = ec;
205   register int i, j, ok = 0;
206 
207   *ret_buf = NULL;
208   for (i = save_seq_len; c < eend; c++)
209     {
210       ok = 0;
211       save_seq[i++] = *c;
212       save_seq[i] = '\0';
213       for (j = 0; designate[j].code; j++)
214         {
215           if (!strncmp ((char *) save_seq, (char *) designate[j].code, i))
216             {
217               if (i == strlen ((char *) designate[j].code))
218                 {
219                   set_gn (&designate[j]);
220                   save_seq_len = 0;
221                   return (c - ec);
222                 }
223               ok = 1;
224               break;
225             }
226         }
227       if (ok == 0)
228         {
229           *ret_buf = save_seq;
230           save_seq_len = 0;
231           return (c - ec);
232         }
233     }
234   save_seq_len = i;
235   return (c - ec - 1);
236 }
237 
238 int
flush_designate(buf)239 flush_designate (buf)
240      w_char *buf;
241 {
242   register w_char *c = buf;
243   register int i;
244 
245   if (pending_esc)
246     {
247       *c++ = ESC;
248       pending_esc = 0;
249       return (1);
250     }
251   if (save_seq_len == 0)
252     return (0);
253   *c++ = ESC;
254   for (i = 0; i < save_seq_len; i++)
255     {
256       *c++ = save_seq[i];
257     }
258   save_seq_len = 0;
259   return ((char *) c - (char *) buf);
260 }
261 
262 int
extc_to_intc(intc,extc,esiz)263 extc_to_intc (intc, extc, esiz)
264      w_char *intc;
265      unsigned char *extc;
266      int esiz;
267 {
268   unsigned char *eend = extc + esiz;
269   register unsigned char *ec = extc;
270   register w_char *ic = intc;
271   register int LorR = 0, i;
272   w_char tmp;
273   int ret, len;
274   unsigned char *ret_buf;
275   register unsigned char *p;
276 
277   for (; ec < eend; ec++)
278     {
279       if (pending_esc)
280         {
281           pending_esc = 0;
282           goto ESC_SWITCH;
283         }
284       if (pending)
285         {
286           *ic++ = ((pending << 8 | *ec) & 0x7f7f) | pending_mask;
287           pending = '\0';
288           continue;
289         }
290       switch (*ec)
291         {
292 #ifdef JIS7
293         case LS0:
294           glr_mode[GL] = G0;
295           break;
296         case LS1:
297           glr_mode[GL] = G1;
298           break;
299 #endif /* JIS7 */
300         case SS2:
301           save_glr_mode[GL] = glr_mode[GL];
302           glr_mode[GL] = (G2 | SS);
303           break;
304         case SS3:
305           save_glr_mode[GL] = glr_mode[GL];
306           glr_mode[GL] = (G3 | SS);
307           break;
308         case ESC:
309           if (++ec == eend)
310             {
311               pending_esc = 1;
312               break;
313             }
314         ESC_SWITCH:
315           switch (*ec)
316             {
317 #ifndef CANNA /* This should be removed even if not Canna */
318             case LS1R:
319               glr_mode[GR] = G1;
320               break;
321             case LS2:
322               glr_mode[GL] = G2;
323               break;
324             case LS2R:
325               glr_mode[GR] = G2;
326               break;
327             case LS3:
328               glr_mode[GL] = G3;
329               break;
330             case LS3R:
331               glr_mode[GR] = G3;
332               break;
333 #endif /* CANNA */
334             default:
335               ret = check_designate (ec, eend, &ret_buf);
336               ec += ret;
337               if (ret_buf)
338                 {
339                   *ic++ = ESC;
340                   for (p = ret_buf; *p; p++)
341                     *ic++ = *p;
342                 }
343               break;
344             }
345           break;
346         default:
347           LorR = 0;
348           if (*ec >= 0x20 && *ec <= 0x7f)
349             {                   /* GL */
350               LorR = GL;
351             }
352           else if (*ec >= 0xa0 && *ec <= 0xff)
353             {                   /* GR */
354               LorR = GR;
355             }
356           if (LorR)
357             {
358               len = gn_len[(glr_mode[LorR] & 0x3)];
359               if ((ec + len) > eend)
360                 {
361                   pending = *ec;
362                   pending_mask = gn_mask[(glr_mode[LorR] & 0x3)];
363                 }
364               else
365                 {
366                   for (tmp = (w_char) 0, i = 0; i < len; i++)
367                     {
368                       tmp = ((tmp << 8 | *ec++) & 0x7f7f) | gn_mask[(glr_mode[LorR] & 0x3)];
369                     }
370                   if (len)
371                     ec -= 1;
372                   *ic++ = tmp;
373                   if (glr_mode[LorR] & SS)
374                     glr_mode[LorR] = save_glr_mode[LorR];
375                 }
376             }
377           else
378             {
379               *ic++ = *ec;
380             }
381         }
382     }
383   return ((char *) ic - (char *) intc);
384 }
385 
386 int
through(x,y,z)387 through (x, y, z)
388      char *x, *y;
389      int z;
390 {
391   bcopy (y, x, z);
392   return z;
393 }
394 
395 int
ibit8_to_ebit8(ebit8,ibit8,ibsiz)396 ibit8_to_ebit8 (ebit8, ibit8, ibsiz)
397      unsigned char *ebit8;
398      w_char *ibit8;
399      int ibsiz;
400 {
401   register unsigned char *eb = ebit8;
402   register w_char *ib = ibit8;
403 
404   for (; ibsiz > 0; ibsiz -= sizeof (w_char))
405     {
406       *eb++ = *ib++ & 0xff;
407     }
408   return ((char *) eb - (char *) ebit8);
409 }
410 
411 /** cswidth functions **/
412 unsigned int
create_cswidth(s)413 create_cswidth (s)
414      char *s;
415 {
416   char tmp[2];
417   int cs = 0, css = 0, i;
418 
419   if (!s || !*s)
420     return (0);
421 
422   tmp[0] = tmp[1] = '\0';
423   for (i = 2; i >= 0; i--)
424     {
425       tmp[0] = *s;
426       cs = atoi (tmp);
427       if (cs > 0 && cs < 3)
428         css = (cs << (i * 8 + 4)) | css;
429       if (!*++s)
430         {
431           if (cs > 0 && cs < 3)
432             css = (cs << (i * 8)) | css;
433           break;
434         }
435       if (*s == ':')
436         {
437           if (!*++s)
438             {
439               if (cs > 0 && cs < 3)
440                 css = (cs << (i * 8)) | css;
441               break;
442             }
443           tmp[0] = *s;
444           cs = atoi (tmp);
445           s++;
446         }
447       if (cs > 0 && cs < 3)
448         css = (cs << (i * 8)) | css;
449       if (!*s || *s != ',' || !*++s)
450         break;
451     }
452   return (css);
453 }
454 
455 void
set_cswidth(id)456 set_cswidth (id)
457      register unsigned int id;
458 {
459   _etc_cs[CS1] = (id >> 20) & 0xf;
460   _etc_cs[CS2] = (id >> 12) & 0xf;
461   _etc_cs[CS3] = (id >> 4) & 0xf;
462   _etc_cs_len[CS1] = (id >> 16) & 0xf;
463   _etc_cs_len[CS2] = (id >> 8) & 0xf;
464   _etc_cs_len[CS3] = id & 0xf;
465   return;
466 }
467 
468 static cswidth_name_struct cs_width_name[] = {
469   {WNN_J_LANG, "JCSWIDTH", "2,1,2"},
470   {WNN_C_LANG, "CCSWIDTH", "2,1,2"},
471   {WNN_K_LANG, "KCSWIDTH", "2"},
472   {WNN_T_LANG, "TCSWIDTH", "2,1,2"},
473   {NULL, NULL}
474 };
475 
476 char *
get_cswidth_name(lang)477 get_cswidth_name (lang)
478      register char *lang;
479 {
480   register cswidth_name_struct *p;
481   register char *name;
482   extern char *getenv ();
483 
484   if (!lang || !*lang)
485     {
486       return (getenv ("CSWIDTH"));
487     }
488 
489   for (p = cs_width_name; p->lang; p++)
490     {
491       if (!strncmp (lang, p->lang, strlen (lang)))
492         {
493           if ((name = getenv (p->name)) != NULL)
494             {
495               return (name);
496             }
497           else if ((name = getenv ("CSWIDTH")) != NULL)
498             {
499               return (name);
500             }
501           else
502             {
503               return (p->def_name);
504             }
505         }
506     }
507   return (NULL);
508 }
509 
510 int
get_cswidth(cs)511 get_cswidth (cs)
512      int cs;
513 {
514   return (_etc_cs[cs]);
515 }
516 
517 int
get_cswidth_by_char(c)518 get_cswidth_by_char (c)
519      register unsigned char c;
520 {
521   if (c < SS2 || (c < 0xa0 && c > SS3))
522     return (1);
523   if (c == SS2)
524     return (_etc_cs[CS2] + 1);
525   if (c == SS3)
526     return (_etc_cs[CS3] + 1);
527   return (_etc_cs[CS1]);
528 }
529 
530 int
get_cs_mask(cs)531 get_cs_mask (cs)
532      int cs;
533 {
534   return (cs_mask[cs]);
535 }
536 
537 int
columnlen(eeuc)538 columnlen (eeuc)
539      unsigned char *eeuc;
540 {
541   register int n = 0;
542   register unsigned char *c, x;
543   register int cs_id;
544 
545   for (c = eeuc; *c;)
546     {
547       x = *c;
548       if (x & 0x80)
549         {
550           cs_id = ((x == SS2) ? CS2 : ((x == SS3) ? CS3 : CS1));
551           if (cs_id == CS2 || cs_id == CS3)
552             c++;
553           n += _etc_cs_len[cs_id];
554           c += _etc_cs[cs_id];
555         }
556       else
557         {
558           n++;
559           c++;
560         }
561     }
562   return (n);
563 }
564 
565 int
columnlen_w(ieuc)566 columnlen_w (ieuc)
567      w_char *ieuc;
568 {
569   register int n = 0;
570   register w_char *c, x;
571   register int cs_id, mask;
572 
573   for (c = ieuc; *c; c++)
574     {
575       x = *c;
576       mask = x & CS_MASK;
577       if (mask == CS0_MASK)
578         {
579           n++;
580         }
581       else
582         {
583           cs_id = (mask == cs_mask[CS3]) ? CS3 : ((mask == cs_mask[CS2]) ? CS2 : CS1);
584           n += _etc_cs_len[cs_id];
585         }
586     }
587   return (n);
588 }
589 
590 int
ieuc_to_eeuc(eeuc,ieuc,iesiz)591 ieuc_to_eeuc (eeuc, ieuc, iesiz)
592      unsigned char *eeuc;
593      w_char *ieuc;
594      int iesiz;
595 {
596   register int x;
597   register w_char *ie;
598   register unsigned char *ee;
599   register int cs_id, mask, non_limit = 0;
600   ie = ieuc;
601   ee = eeuc;
602 
603   if (iesiz == -1)
604     non_limit = 1;
605   for (; (non_limit ? (*ie) : (iesiz > 0)); iesiz -= sizeof (w_char))
606     {
607       x = *ie++;
608       mask = x & CS_MASK;
609       if (mask == CS0_MASK || x == 0xffff)
610         {
611           *ee++ = x;
612         }
613       else
614         {
615           cs_id = (mask == cs_mask[CS3]) ? CS3 : ((mask == cs_mask[CS2]) ? CS2 : CS1);
616           if (_etc_cs[cs_id] <= 0)
617             continue;
618           if (cs_id == CS2)
619             *ee++ = SS2;
620           else if (cs_id == CS3)
621             *ee++ = SS3;
622           if (_etc_cs[cs_id] > 1)
623             *ee++ = (x >> 8) | 0x80;
624           if (_etc_cs[cs_id] > 0)
625             *ee++ = (x & 0xff) | 0x80;
626         }
627     }
628   return ((char *) ee - (char *) eeuc);
629 }
630 
631 
632 int
eeuc_to_ieuc(ieuc,eeuc,eesiz)633 eeuc_to_ieuc (ieuc, eeuc, eesiz)
634      w_char *ieuc;
635      unsigned char *eeuc;
636      register int eesiz;
637 {
638   register unsigned char x;
639   register w_char *ie;
640   register unsigned char *ee;
641   register int cs_id, non_limit = 0;
642   ie = ieuc;
643   ee = eeuc;
644 
645   if (eesiz == -1)
646     non_limit = 1;
647   for (; (non_limit ? (*ee) : (eesiz > 0));)
648     {
649       x = *ee++;
650       if (x > 0x9f || x == SS2 || x == SS3)
651         {
652           cs_id = ((x == SS2) ? CS2 : ((x == SS3) ? CS3 : CS1));
653           if (cs_id == CS2 || cs_id == CS3)
654             x = *ee++;
655           if (_etc_cs[cs_id] <= 0)
656             continue;
657           if (_etc_cs[cs_id] > 1)
658             {
659               *ie = (w_char) (x & 0x7f) << 8;
660               x = *ee++;
661             }
662           else
663             {
664               *ie = (w_char) 0;
665             }
666           *ie |= (x & 0x7f);
667           *ie++ |= cs_mask[cs_id];
668           eesiz -= _etc_cs[cs_id] + 1;
669         }
670       else
671         {
672           *ie++ = x;
673           eesiz--;
674         }
675     }
676   return ((char *) ie - (char *) ieuc);
677 }
678 
679 #ifdef nodef
680 void
wnn_delete_ss2(s,n)681 wnn_delete_ss2 (s, n)
682      register unsigned int *s;
683      register int n;
684 {
685   register unsigned int x;
686 
687   for (; n != 0 && (x = *s); n--, s++)
688     {
689       if ((x & 0xff00) == 0x8e00)
690         *s &= ~0xff00;
691       if (x == 0xffffffff)
692         break;
693     }
694 }
695 #endif
696 
697 void
wnn_delete_w_ss2(s,n)698 wnn_delete_w_ss2 (s, n)
699      register w_char *s;
700      register int n;
701 {
702   register w_char x;
703 
704   for (; n != 0 && (x = *s); n--, s++)
705     {
706       if ((x & 0xff00) == 0x8e00)
707         *s &= ~0xff00;
708     }
709 }
710 
711 #ifdef nodef
712 int
wnn_byte_count(in)713 wnn_byte_count (in)
714      register int in;
715 {
716   return (((in < 0xa0 && in != 0x00 && in != 0x8e) || in == 0xff) ? 1 : 2);
717 }
718 #endif
719 
720 #define ASCII           0
721 
722 #ifdef  JAPANESE
723 #define HANKAKU_JIS_IN  '\016'
724 #define HANKAKU_JIS_OUT '\017'
725 
726 #define HANKAKU_JIS     2
727 #define ZENKAKU_JIS     1
728 #define ZENKAKU_JIS_HOJYO       3
729 
730 static unsigned char *j;
731 static w_char *iu;
732 static unsigned char *eu;
733 static unsigned char *sj;
734 static unsigned char tmp_buf[2000];
735 
736 static void
putj(x)737 putj (x)
738      int x;
739 {
740   *j++ = x;
741 }
742 
743 static void
puteu(x)744 puteu (x)
745      int x;
746 {
747   *eu++ = x;
748 }
749 
750 static void
putsj(x)751 putsj (x)
752      int x;
753 {
754   *sj++ = x;
755 }
756 
757 static void
putsjw(x)758 putsjw (x)
759      int x;
760 {
761   *sj++ = x >> 8;
762   *sj++ = x;
763 }
764 
765 static int oj_mode = ASCII;     /* ���ϻ��Σ������ɤΥ⡼�� */
766 static int jtosj ();
767 extern int eujis_to_iujis ();
768 
769 /* convert JIS code to shift-JIS code */
770 static int
jtosj(high,low)771 jtosj (high, low)
772      unsigned high, low;
773 {
774   if (high & 1)
775     low += 0x1f;
776   else
777     low += 0x7d;
778   if (low >= 0x7f)
779     low++;
780   high = ((high - 0x21) >> 1) + 0x81;
781   if (high > 0x9f)
782     high += 0x40;
783   return ((high << 8) | low);
784 }
785 
786 /* convert shift-JIS to JIS code */
787 static int
sjtoj(high,low)788 sjtoj (high, low)
789      register unsigned high, low;
790 {
791   high -= (high <= 0x9f) ? 0x71 : 0xb1;
792   high = high * 2;
793   if (low > 0x7f)
794     low--;
795   if (low >= 0x9e)
796     {
797       high += 2;
798       low -= 0x7d;
799     }
800   else
801     {
802       high++;
803       low -= 0x1f;
804     }
805   return ((high << 8) | low);
806 }
807 
808 static void
jis_change_mode(mode,new_mode)809 jis_change_mode (mode, new_mode)
810      int *mode;
811      int new_mode;
812 {
813   if (*mode == new_mode)
814     return;
815   switch (*mode)
816     {
817     case ZENKAKU_JIS:
818     case ZENKAKU_JIS_HOJYO:
819       /* designate ISO-8859-1 rather than JIS X 0201 */
820       /* putj('\033'); putj('('); putj('J');break; */
821       putj ('\033');
822       putj ('(');
823       putj ('B');
824       break;
825 #ifdef  JIS7
826     case HANKAKU_JIS:
827       putj (HANKAKU_JIS_OUT);
828       break;
829 #endif /* JIS7 */
830     default:;
831     }
832   *mode = new_mode;
833   switch (new_mode)
834     {
835     case ZENKAKU_JIS:
836       putj ('\033');
837       putj ('$');
838       putj ('B');
839       break;
840     case ZENKAKU_JIS_HOJYO:
841       putj ('\033');
842       putj ('$');
843       putj ('(');
844       putj ('D');
845       break;
846 #ifdef  JIS7
847     case HANKAKU_JIS:
848       putj (HANKAKU_JIS_IN);
849       break;
850 #endif /* JIS7 */
851     default:;
852     }
853 }
854 
855 #ifdef  JIS7
856 /*      ���� U-jis �� 7bit jis �����ɤ��Ѵ����ޤ�
857         ʸ�����Ĺ�����֤��ޤ�                  */
858 extern int
859 iujis_to_jis (jis, iujis, iusiz)
860      unsigned char *jis;        /*      jis�����ɤˤʤä���Τ���buf  */
861      w_char *iujis;             /*      iujis�����ɤΤ�Τ����Ƥ���buf */
862      int iusiz;                 /*      iujis ���礭��                  */
863 {
864   int x;
865   j = jis;
866   iu = iujis;
867   for (; iusiz > 0; iusiz -= sizeof (w_char))
868     {
869       x = *iu++;
870       if (((x & 0xFF00) == 0x8E00) || ((x & 0xFF80) == 0x80))
871         {
872           jis_change_mode (&oj_mode, HANKAKU_JIS);
873           putj (x & 0x7f);
874         }
875       else if ((x & 0x8080) == 0x8080)
876         {
877           jis_change_mode (&oj_mode, ZENKAKU_JIS);
878           putj ((x >> 8) & 0x7f);
879           putj (x & 0x7f);
880         }
881       else if (x & 0x8000)
882         {
883           jis_change_mode (&oj_mode, ZENKAKU_JIS_HOJYO);
884           putj ((x >> 8) & 0x7f);
885           putj (x & 0x7f);
886         }
887       else
888         {
889           jis_change_mode (&oj_mode, ASCII);
890           putj (x);
891         }
892     }
893   jis_change_mode (&oj_mode, ASCII);
894   return (j - jis);
895 }
896 #endif /* JIS7 */
897 
898 /*      ���� U-jis �� 8bit jis �����ɤ��Ѵ����ޤ�
899         ʸ�����Ĺ�����֤��ޤ�                  */
900 extern int
901 iujis_to_jis8 (jis, iujis, iusiz)
902      unsigned char *jis;        /*      jis�����ɤˤʤä���Τ���buf  */
903      w_char *iujis;             /*      iujis�����ɤΤ�Τ����Ƥ���buf */
904      int iusiz;                 /*      iujis ���礭��                  */
905 {
906   int x;
907   j = jis;
908   iu = iujis;
909   for (; iusiz > 0; iusiz -= sizeof (w_char))
910     {
911       x = *iu++;
912       if (((x & 0xFF00) == 0x8E00) || ((x & 0xFF80) == 0x80))
913         {
914           jis_change_mode (&oj_mode, ASCII);
915           putj (x & 0xff);
916         }
917       else if ((x & 0x8080) == 0x8080)
918         {
919           jis_change_mode (&oj_mode, ZENKAKU_JIS);
920           putj ((x >> 8) & 0x7f);
921           putj (x & 0x7f);
922         }
923       else if (x & 0x8000)
924         {
925           jis_change_mode (&oj_mode, ZENKAKU_JIS_HOJYO);
926           putj ((x >> 8) & 0x7f);
927           putj (x & 0x7f);
928         }
929       else
930         {
931           jis_change_mode (&oj_mode, ASCII);
932           putj (x);
933         }
934     }
935   jis_change_mode (&oj_mode, ASCII);
936   return (j - jis);
937 }
938 
939 
940 #ifdef  JIS7
941 /*      ���� U-jis �� 7bit jis �����ɤ��Ѵ����ޤ�       */
942 extern int
943 eujis_to_jis (jis, eujis, eusiz)
944      unsigned char *jis, *eujis;
945      int eusiz;
946 {
947   static int kanji1 = 0;
948   static char kanji1_code = 0;
949   /* 0: normal
950      1: get SS2
951      2: get kanji 1 byte */
952   /*
953      int oj_mode;
954    */
955   int x;
956   j = jis;
957   eu = eujis;
958   /* ADD KURI */
959   if (kanji1 != 0)
960     {
961       if (kanji1 == 2)
962         {
963           putj (kanji1_code & 0x7f);
964         }
965       putj (*eu & 0x7f);
966       eusiz -= sizeof (char);
967       kanji1 = 0;
968     }
969   /* ADD KURI end */
970   /*
971      for(oj_mode=ASCII;eusiz>0;eusiz-=sizeof(char)){
972    */
973   for (; eusiz > 0; eusiz -= sizeof (char))
974     {
975       x = *eu++;
976       if ((x & 0xFF) == 0x8E)
977         {
978           jis_change_mode (&oj_mode, HANKAKU_JIS);
979           if (eusiz > 1)
980             {
981               putj (*eu++ & 0x7f);
982               eusiz -= sizeof (char);
983             }
984           else
985             {
986               kanji1 = 1;
987             }
988         }
989       else if (x & 0x80)
990         {
991           jis_change_mode (&oj_mode, ZENKAKU_JIS);
992           if (eusiz > 1)
993             {
994               putj (x & 0x7f);
995               putj (*eu++ & 0x7f);
996               eusiz -= sizeof (char);
997             }
998           else
999             {
1000               kanji1 = 2;
1001               kanji1_code = x & 0x7f;
1002             }
1003         }
1004       else
1005         {
1006           jis_change_mode (&oj_mode, ASCII);
1007           putj (x);
1008         }
1009     }
1010   if (kanji1 == 0)
1011     jis_change_mode (&oj_mode, ASCII);
1012   return (j - jis);
1013 }
1014 #endif /* JIS7 */
1015 
1016 /*      ���� U-jis �� 8bit jis �����ɤ��Ѵ����ޤ�       */
1017 extern int
1018 eujis_to_jis8 (jis, eujis, eusiz)
1019      unsigned char *jis, *eujis;
1020      int eusiz;
1021 {
1022   static int kanji1 = 0;
1023   static unsigned char kanji1_code = 0;
1024   /* 0: normal
1025      1: get SS2
1026      2: get kanji 1 byte */
1027   /*
1028      int oj_mode;
1029    */
1030   int x;
1031   j = jis;
1032   eu = eujis;
1033   /* ADD KURI */
1034   if (kanji1 != 0)
1035     {
1036       if (kanji1 == 2)
1037         {
1038           putj (kanji1_code & 0x7f);
1039           putj (*eu & 0x7f);
1040         }
1041       else
1042         {
1043           putj (*eu);
1044         }
1045       eusiz -= sizeof (char);
1046       kanji1 = 0;
1047       eu++;
1048     }
1049   /* ADD KURI end */
1050   /*
1051      for(oj_mode=ASCII;eusiz>0;eusiz-=sizeof(char)){
1052    */
1053   for (; eusiz > 0; eusiz -= sizeof (char))
1054     {
1055       x = *eu++;
1056       if ((x & 0xFF) == 0x8E)
1057         {
1058           jis_change_mode (&oj_mode, ASCII);
1059           if (eusiz > 1)
1060             {
1061               putj (*eu++);
1062               eusiz -= sizeof (char);
1063             }
1064           else
1065             {
1066               kanji1 = 1;
1067             }
1068         }
1069       else if (x & 0x80)
1070         {
1071           jis_change_mode (&oj_mode, ZENKAKU_JIS);
1072           if (eusiz > 1)
1073             {
1074               putj (x & 0x7f);
1075               putj (*eu++ & 0x7f);
1076               eusiz -= sizeof (char);
1077             }
1078           else
1079             {
1080               kanji1 = 2;
1081               kanji1_code = x;
1082             }
1083         }
1084       else
1085         {
1086           jis_change_mode (&oj_mode, ASCII);
1087           putj (x);
1088         }
1089     }
1090   if (kanji1 == 0)
1091     jis_change_mode (&oj_mode, ASCII);
1092   return (j - jis);
1093 }
1094 
1095 /*      ���� U-jis �� ���� U-jis �����ɤ��Ѵ����ޤ�     */
1096 extern int
1097 iujis_to_eujis (eujis, iujis, iusiz)
1098      unsigned char *eujis;
1099      w_char *iujis;
1100      int iusiz;
1101 {
1102   static int first = 0;
1103   static unsigned int cswidth_id;
1104 
1105   if (first == 0)
1106     {
1107       cswidth_id = create_cswidth (UJIS_CSWIDTH);
1108       first++;
1109     }
1110   set_cswidth (cswidth_id);
1111   return (ieuc_to_eeuc (eujis, iujis, iusiz));
1112 }
1113 
1114 int
jis_to_eujis(eujis,jis,jsiz)1115 jis_to_eujis (eujis, jis, jsiz)
1116      unsigned char *eujis, *jis;
1117      int jsiz;
1118 {
1119   int len;
1120 
1121   designate = JIS_designate;
1122   len = extc_to_intc (tmp_w_buf, jis, jsiz);
1123   return (iujis_to_eujis (eujis, tmp_w_buf, len));
1124 }
1125 
1126 /*
1127  *      Shifted JIS
1128  */
1129 
1130 /*      ���� U-jis �� S-jis �����ɤ��Ѵ����ޤ�
1131         ʸ�����Ĺ�����֤��ޤ�                  */
1132 extern int
1133 eujis_to_sjis (sjis, eujis, eusiz)
1134      unsigned char *sjis;       /*      sjis�����ɤˤʤä���Τ���buf */
1135      unsigned char *eujis;      /*      eujis�����ɤΤ�Τ����Ƥ���buf */
1136      int eusiz;                 /*      eujis ���礭��                  */
1137 {
1138   register int x;
1139   int save = 0;
1140   sj = sjis;
1141   eu = eujis;
1142   if (save && eusiz > 0)
1143     {
1144       if (save == 0x8e)
1145         {
1146           putsj (*eu++ | 0x80);
1147         }
1148       else
1149         {
1150           putsjw (jtosj (save & 0x7F, *eu++ & 0x7F));
1151         }
1152       eusiz--;
1153     }
1154   for (; eusiz > 0;)
1155     {
1156       x = *eu++;
1157       eusiz--;
1158       if (x & 0x80)
1159         {
1160           if (eusiz <= 0)
1161             {
1162               save = x;
1163               break;
1164             }
1165           if (x == 0x8e)
1166             {
1167               putsj (*eu++ | 0x80);
1168             }
1169           else
1170             {
1171               putsjw (jtosj (x & 0x7F, *eu++ & 0x7F));
1172             }
1173           eusiz--;
1174         }
1175       else
1176         {
1177           putsj (x);
1178         }
1179     }
1180   return (sj - sjis);
1181 }
1182 
1183 /*      ���� U-jis �� S-jis �����ɤ��Ѵ����ޤ�
1184         ʸ�����Ĺ�����֤��ޤ�                  */
1185 extern int
1186 iujis_to_sjis (sjis, iujis, iusiz)
1187      unsigned char *sjis;       /*      sjis�����ɤˤʤä���Τ���buf */
1188      w_char *iujis;             /*      iujis�����ɤΤ�Τ����Ƥ���buf */
1189      int iusiz;                 /*      iujis ���礭��                  */
1190 {
1191   register int x;
1192   sj = sjis;
1193   iu = iujis;
1194   for (; iusiz > 0; iusiz -= sizeof (w_char))
1195     {
1196       if ((x = *iu++) & 0xff00)
1197         {
1198           if ((x & 0xff00) == 0x8e00)
1199             {
1200               putsj ((x & 0xff) | 0x80);
1201             }
1202           else
1203             {
1204               putsjw (jtosj ((x >> 8) & 0x7f, x & 0x7f));
1205             }
1206         }
1207       else
1208         {
1209           putsj (x);
1210         }
1211     }
1212   return (sj - sjis);
1213 }
1214 
1215 int
sjis_to_iujis(iujis,sjis,ssiz)1216 sjis_to_iujis (iujis, sjis, ssiz)
1217      w_char *iujis;             /* iujis�����ɤˤʤä���Τ���buf */
1218      unsigned char *sjis;       /* sjis�����ɤΤ�Τ����Ƥ���buf */
1219      int ssiz;                  /* sjis ���礭 */
1220 {
1221   register int x;
1222   int save = 0;
1223   sj = sjis;
1224   iu = iujis;
1225   if (save && ssiz > 0)
1226     {
1227       *iu++ = (sjtoj (save, *sj++) | 0x8080);
1228       ssiz--;
1229       save = 0;
1230     }
1231   for (; ssiz > 0;)
1232     {
1233       x = *sj++;
1234       ssiz--;
1235       if (x & 0x80)
1236         {
1237           if (ssiz <= 0)
1238             {
1239               save = x;
1240               break;
1241             }
1242           *iu++ = ((sjtoj (x, *sj++)) | 0x8080);
1243           ssiz--;
1244         }
1245       else
1246         {
1247           *iu++ = (x);
1248         }
1249     }
1250   return ((char *) iu - (char *) iujis);
1251 }
1252 
1253 int
sjis_to_eujis(eujis,sjis,ssiz)1254 sjis_to_eujis (eujis, sjis, ssiz)
1255      unsigned char *eujis;      /*      eujis�����ɤˤʤä���Τ���buf        */
1256      unsigned char *sjis;       /*      sjis�����ɤΤ�Τ����Ƥ���buf */
1257      int ssiz;                  /*      sjis ���礭��                   */
1258 {
1259   register int x;
1260   unsigned char *sj;
1261   int save = 0;
1262   sj = sjis;
1263   eu = eujis;
1264   if (save && ssiz > 0)
1265     {
1266       x = (sjtoj (save, *sj++) | 0x8080);
1267       puteu (x >> 8);
1268       puteu (x);
1269       ssiz--;
1270       save = 0;
1271     }
1272   for (; ssiz > 0;)
1273     {
1274       x = *sj++;
1275       ssiz--;
1276       if (x & 0x80)
1277         {
1278           if (ssiz <= 0)
1279             {
1280               save = x;
1281               break;
1282             }
1283           x = (sjtoj (x, *sj++) | 0x8080);      /* �Ѥ��ޤ��� KUWA */
1284           puteu (x >> 8);
1285           puteu (x);
1286           ssiz--;
1287         }
1288       else
1289         {
1290           puteu (x);
1291         }
1292     }
1293   return (eu - eujis);
1294 }
1295 
1296 #ifdef  JIS7
1297 int
sjis_to_jis(jis,sjis,siz)1298 sjis_to_jis (jis, sjis, siz)
1299      unsigned char *jis, *sjis;
1300      int siz;
1301 {
1302   int len;
1303   len = sjis_to_eujis (tmp_buf, sjis, siz);
1304   return (eujis_to_jis (jis, tmp_buf, len));
1305 }
1306 #endif /* JIS7 */
1307 
1308 int
sjis_to_jis8(jis,sjis,siz)1309 sjis_to_jis8 (jis, sjis, siz)
1310      unsigned char *jis, *sjis;
1311      int siz;
1312 {
1313   int len;
1314   len = sjis_to_eujis (tmp_buf, sjis, siz);
1315   return (eujis_to_jis8 (jis, tmp_buf, len));
1316 }
1317 
1318 int
jis_to_iujis(iujis,jis,jsiz)1319 jis_to_iujis (iujis, jis, jsiz)
1320      w_char *iujis;
1321      unsigned char *jis;
1322      int jsiz;
1323 {
1324   designate = JIS_designate;
1325   return (extc_to_intc (iujis, jis, jsiz));
1326 }
1327 
1328 int
jis_to_sjis(sjis,jis,siz)1329 jis_to_sjis (sjis, jis, siz)
1330      unsigned char *sjis, *jis;
1331      int siz;
1332 {
1333   int len;
1334   len = jis_to_iujis (tmp_w_buf, jis, siz);
1335   return (iujis_to_sjis (sjis, tmp_w_buf, len));
1336 }
1337 
1338 int
eujis_to_iujis(iujis,eujis,eusiz)1339 eujis_to_iujis (iujis, eujis, eusiz)
1340      w_char *iujis;
1341      unsigned char *eujis;
1342      int eusiz;
1343 {
1344   static int first = 0;
1345   static unsigned int cswidth_id;
1346 
1347   if (first == 0)
1348     {
1349       cswidth_id = create_cswidth (UJIS_CSWIDTH);
1350       first++;
1351     }
1352   set_cswidth (cswidth_id);
1353   return (eeuc_to_ieuc (iujis, eujis, eusiz));
1354 }
1355 
1356 #endif /* JAPANESE */
1357 
1358 #ifdef  CHINESE
1359 
1360 #define CNS11643_1      2
1361 #define CNS11643_2      1
1362 
1363 /* The following facts are helpful for understanding:
1364         * _W,           means Wei in Pinyin
1365         * _Q,           means Wei in Pinyin.
1366         * 0x5e = 94,    num of wchar in one _Q at 1xxxxxxx 1xxxxxxx hand
1367         * 0x3f = 63,    Num of Wchar in one _Q at 1xxxxxxx 0xxxxxxx hand
1368         * Almost all the numbers in HEX are given for showing the _Q or _W
1369 */
1370 
1371 #define CNS_NUM_Q               0x5e    /* num of wchar in one _Q at hand */
1372 #define BIG5_NUM_11_Q           0x5e    /* num of wchar in one _Q at 1xxxxxxx 1xxxxxxx hand */
1373 #define BIG5_NUM_10_Q           0x3f    /* num of wchar in one _Q at 1xxxxxxx 0xxxxxxx hand */
1374 
1375 #define CNS_HANZI_11_START_Q    0x24    /* At 1xxxxxxx 1xxxxxxx hand */
1376 #define CNS_HANZI_11_START_W    0x01    /* At 1xxxxxxx 1xxxxxxx hand */
1377 #define CNS_HANZI_11_END_Q      0x5D    /* At 1xxxxxxx 1xxxxxxx hand */
1378 #define CNS_HANZI_11_END_W      0x2B    /* At 1xxxxxxx 1xxxxxxx hand */
1379 #define CNS_HANZI_11_START_QM   0x01    /* At 1xxxxxxx 1xxxxxxx hand */
1380 #define CNS_HANZI_11_END_QM     0x05    /* At 1xxxxxxx 1xxxxxxx hand */
1381 
1382 #define CNS_HANZI_10_START_Q    0x01    /* At 1xxxxxxx 0xxxxxxx hand */
1383 #define CNS_HANZI_10_START_W    0x01    /* At 1xxxxxxx 0xxxxxxx hand */
1384 #define CNS_HANZI_10_END_Q      0x52    /* At 1xxxxxxx 0xxxxxxx hand */
1385 #define CNS_HANZI_10_END_W      0x24    /* At 1xxxxxxx 0xxxxxxx hand */
1386 
1387 #define BIG5_HANZI_11_START_Q   0x04    /* At 1xxxxxxx 1xxxxxxx hand */
1388 #define BIG5_HANZI_11_START_W   0x01    /* At 1xxxxxxx 1xxxxxxx hand */
1389 #define BIG5_HANZI_11_END_Q     0x25    /* At 1xxxxxxx 1xxxxxxx hand */
1390 #define BIG5_HANZI_11_END_W     0x5e    /* At 1xxxxxxx 1xxxxxxx hand */
1391 #define BIG5_HANZI_11_START_QM  0x01    /* At 1xxxxxxx 1xxxxxxx hand */
1392 #define BIG5_HANZI_11_END_QM    0x03    /* At 1xxxxxxx 1xxxxxxx hand */
1393 
1394 #define BIG5_HANZI_10_START_Q   0x04    /* At 1xxxxxxx 0xxxxxxx hand */
1395 #define BIG5_HANZI_10_START_W   0x20    /* At 1xxxxxxx 0xxxxxxx hand */
1396 #define BIG5_HANZI_10_END_Q     0x26    /* At 1xxxxxxx 0xxxxxxx hand */
1397 #define BIG5_HANZI_10_END_W     0x5e    /* At 1xxxxxxx 0xxxxxxx hand */
1398 
1399 #define BIG5_HANZI_11_START_Q2  0x29    /* At 1xxxxxxx 1xxxxxxx hand */
1400 #define BIG5_HANZI_11_START_W2  0x01    /* At 1xxxxxxx 1xxxxxxx hand */
1401 #define BIG5_HANZI_11_END_Q2    0x59    /* At 1xxxxxxx 1xxxxxxx hand */
1402 #define BIG5_HANZI_11_END_W2    0x35    /* At 1xxxxxxx 1xxxxxxx hand */
1403 
1404 #define BIG5_HANZI_10_START_Q2  0x29    /* At 1xxxxxxx 0xxxxxxx hand */
1405 #define BIG5_HANZI_10_START_W2  0x20    /* At 1xxxxxxx 0xxxxxxx hand */
1406 #define BIG5_HANZI_10_END_Q2    0x59    /* At 1xxxxxxx 0xxxxxxx hand */
1407 #define BIG5_HANZI_10_END_W2    0x5e    /* At 1xxxxxxx 0xxxxxxx hand */
1408 
1409 #define CNS_SYMBOL_11_START     0
1410 #define CNS_SYMBOL_11_END       0
1411 #define BIG5_SYMBOL_10_START    0
1412 #define BIG5_SYMBOL_10_END      0
1413 #define BIG5_SYMBOL_11_START    0
1414 #define BIG5_SYMBOL_11_END      0
1415 
1416 #define BIG5_TO_CNS     0x0000  /* Flag for convert from BIG5 to CNS */
1417 #define CNS_TO_BIG5     0x0001  /* Flag for convert from CNS to BIG5 */
1418 
1419 #define BIG5_1NUMS      ((BIG5_NUM_10_Q*0x23)+(BIG5_NUM_11_Q*0x22))
1420                                 /* Numbers of level 1 */
1421 #define BIG5_1TO2_SKIP  (0x25*(BIG5_NUM_10_Q+BIG5_NUM_11_Q))
1422                                 /*Location of starting level 2 */
1423 
1424 #define CNS_SPACE       0x256D  /* for unconvert character code */
1425 
1426 /* not sequential character code on CNS level 2 */
1427 #define CNS_XK          0xa121  /* XK on Tsang Jye */
1428 #define CNS_ONLN        0xa14c  /* ONLN on Tsang Jye */
1429 #define CNS_MSOK        0xa24d  /* MSOK on Tsang Jye */
1430 #define CNS_TNKM        0xbf6a  /* TNKM on Tsang Jye */
1431 #define CNS_CYIB        0xd54b  /* CYIB on Tsang Jye */
1432 #define CNS_YIHXO       0xda28  /* YIHXO on Tsang Jye */
1433 #define CNS_MDMR        0xdd74  /* MDMR on Tsang Jye */
1434 #define CNS_COLH        0xe42f  /* COLH on Tsang Jye */
1435 #define CNS_ODC         0xe761  /* ODC on Tsang Jye */
1436 #define CNS_OKHAE       0xe934  /* OKHAE on Tsang Jye */
1437 #define CNS_HBBM        0xe64d  /* HBBM on Tsang Jye */
1438 #define CNS_CJTC        0xea4b  /* CJTC on Tsang Jye */
1439 #define CNS_LNNXU       0xf166  /* LNNXU on Tsang Jye */
1440 #define CNS_YPYBP       0xf244  /* YPYBP on Tsang Jye */
1441 #define CNS_FDDH        0xf240  /* FDDH on Tsang Jye */
1442 #define CNS_HOOMA       0xd722  /* HOOMA on Tsang Jye */
1443 
1444 /* not sequential character code on BIG5 level 2 */
1445 #define BIG5_XK         0xc940  /* XK on Tsang Jye */
1446 #define BIG5_ONLN       0xc9be  /* ONLN on Tsang Jye */
1447 #define BIG5_MSOK       0xcaf7  /* MSOK on Tsang Jye */
1448 #define BIG5_TNKM       0xd77a  /* TNKM on Tsang Jye */
1449 #define BIG5_CYIB       0xebf1  /* CYIB on Tsang Jye */
1450 #define BIG5_YIHXO      0xf0cb  /* YIHXO on Tsang Jye */
1451 #define BIG5_MDMR       0xf056  /* MDMR on Tsang Jye */
1452 #define BIG5_COLH       0xeeeb  /* COLH on Tsang Jye */
1453 #define BIG5_ODC        0xf16b  /* ODC on Tsang Jye */
1454 #define BIG5_OKHAE      0xf268  /* OKHAE on Tsang Jye */
1455 #define BIG5_HBBM       0xf4b5  /* HBBM on Tsang Jye */
1456 #define BIG5_CJTC       0xf663  /* CJTC on Tsang Jye */
1457 #define BIG5_LNNXU      0xf9c4  /* LNNXU on Tsang Jye */
1458 #define BIG5_YPYBP      0xf9d5  /* YPYBP on Tsang Jye */
1459 #define BIG5_FDDH       0xf9c6  /* FDDH on Tsang Jye */
1460 #define BIG5_HOOMA      0xecde  /* HOOMA on Tsang Jye */
1461 #define BIG5_MU         0xc94a  /* MU on Tsang Jye */
1462 #define BIG5_GRHNE      0xddfc  /* GRHNE on Tsang Jye */
1463 
1464 /* This function checks if the given code is really a Hanzi in the original
1465 code definition determined by "which".  If so, it returns 1. And otherwise
1466 it returns 0)
1467 */
1468 static int
_is_hanzi(code,which)1469 _is_hanzi (code, which)
1470      w_char code;
1471      int which;
1472 {
1473   register unsigned char high, low;
1474 
1475   if (which == CNS_TO_BIG5)
1476     {
1477       if ((code & 0x8080) == 0x8080)
1478         {                       /* 1xxxxxxx 1xxxxxxx Case */
1479           high = (code >> 8) - 0xa0;
1480           low = (code & 0xff) - 0xa0;
1481           if (((high >= CNS_HANZI_11_START_Q && high < CNS_HANZI_11_END_Q) &&
1482                (low >= CNS_HANZI_11_START_W && low <= CNS_NUM_Q)) ||
1483               ((high == CNS_HANZI_11_END_Q) &&
1484                (low >= CNS_HANZI_11_START_W && low <= CNS_HANZI_11_END_W)) || ((high >= CNS_HANZI_11_START_QM && high < CNS_HANZI_11_END_QM) && (low >= CNS_HANZI_11_START_W && low <= CNS_NUM_Q)))
1485             {
1486               return (1);
1487             }
1488           else
1489             {
1490               return (0);
1491             }
1492         }
1493       else if ((code & 0x8080) == 0x8000)
1494         {                       /* 1xxxxxxx 0xxxxxxx Case */
1495           high = (code >> 8) - 0xa0;
1496           low = (code & 0xff) - 0x20;
1497           if (((high >= CNS_HANZI_10_START_Q && high < CNS_HANZI_10_END_Q) &&
1498                (low >= CNS_HANZI_10_START_W && low <= CNS_NUM_Q)) || ((high == CNS_HANZI_10_END_Q) && (low >= CNS_HANZI_10_START_W && low <= CNS_HANZI_10_END_W)))
1499             {
1500               return (1);
1501             }
1502           else
1503             {
1504               return (0);
1505             }
1506         }
1507       else
1508         {
1509           return (0);
1510         }
1511     }
1512   else if (which == BIG5_TO_CNS)
1513     {
1514       if ((code & 0x8080) == 0x8080)
1515         {                       /* 1xxxxxxx 1xxxxxxx Case */
1516           high = (code >> 8) - 0xa0;
1517           low = (code & 0xff) - 0xa0;
1518           if (((high >= BIG5_HANZI_11_START_Q && high <= BIG5_HANZI_11_END_Q) &&
1519                (low >= BIG5_HANZI_11_START_W && low <= BIG5_HANZI_11_END_W)) ||
1520               ((high >= BIG5_HANZI_11_START_QM && high <= BIG5_HANZI_11_END_QM) && (low >= BIG5_HANZI_11_START_W && low <= BIG5_HANZI_11_END_W)))
1521             {
1522               return (1);
1523             }
1524           else if (((high >= BIG5_HANZI_11_START_Q2 && high < BIG5_HANZI_11_END_Q2) &&
1525                     (low >= BIG5_HANZI_11_START_W2 && low <= BIG5_HANZI_11_END_W)) || ((high == BIG5_HANZI_11_END_Q2) && (low >= BIG5_HANZI_11_START_W2 && low <= BIG5_HANZI_11_END_W2)))
1526             {
1527               return (1);
1528             }
1529           else
1530             {
1531               return (0);
1532             }
1533         }
1534       else if ((code & 0x8080) == 0x8000)
1535         {                       /* 1xxxxxxx 0xxxxxxx Case */
1536           high = (code >> 8) - 0xa0;
1537           low = (code & 0xff) - 0x20;
1538           if ((high >= BIG5_HANZI_10_START_Q && high <= BIG5_HANZI_10_END_Q) && (low >= BIG5_HANZI_10_START_W && low <= BIG5_HANZI_10_END_W))
1539             {
1540               return (1);
1541             }
1542           else if ((high >= BIG5_HANZI_10_START_Q2 && high <= BIG5_HANZI_10_END_Q2) && (low >= BIG5_HANZI_10_START_W2 && low <= BIG5_HANZI_10_END_W2))
1543             {
1544               return (1);
1545             }
1546           else
1547             {
1548               return (0);
1549             }
1550         }
1551       else
1552         {
1553           return (0);
1554         }
1555     }
1556   return (0);
1557 }
1558 
1559 /* convert one Hanzi from BIG5 (or CNS) to CNS (or BIG5) depend on the
1560    parameter "which".  This function works under the asumpsion that the
1561    give code is really a Hanzi under the original code definition.  Given
1562    code value can be checked by function "_is_hanzi()" in the same file.
1563    The result Hanzi code is always returned.
1564 */
1565 static unsigned int
_convert(code,which)1566 _convert (code, which)
1567      register w_char code;
1568      int which;
1569 {
1570   unsigned int qu, wei;         /* counting from   1 ------     */
1571   register unsigned int location;       /* counting from   0 ----       */
1572   unsigned int loc_wei;         /* counting from   0 ----       */
1573   int plant;
1574 
1575   if (which == CNS_TO_BIG5)
1576     {
1577       if ((code & 0x8080) == 0x8080)
1578         {                       /* 1xxxxxxx 1xxxxxxx Case */
1579           if (code <= 0xa5fe && code >= 0xa1a1)
1580             {
1581               wei = (code & 0x7f);
1582               switch (((code & 0xff00) >> 8) - 0xa0)
1583                 {
1584                 case (1):
1585                   if (wei > 0x5f)
1586                     {
1587                       return (0xa1a1 + (wei - 0x60));
1588                     }
1589                   else
1590                     {
1591                       return (0xa140 + (wei - 0x21));
1592                     }
1593                 case (2):
1594                   if (wei > 0x5f)
1595                     {
1596                       return (0xa2a1 + (wei - 0x60));
1597                     }
1598                   else
1599                     {
1600                       return (0xa240 + (wei - 0x21));
1601                     }
1602                 case (3):
1603                   if (wei > 0x40)
1604                     {
1605                       return (0xa2a1 + (wei - 0x41));
1606                     }
1607                   else
1608                     {
1609                       return (0xa25f + (wei - 0x21));
1610                     }
1611                 case (4):
1612                   if (wei > 0x70)
1613                     {
1614                       return (0xa340 + (wei - 0x71));
1615                     }
1616                   else
1617                     {
1618                       return (0xa2af + (wei - 0x21));
1619                     }
1620                 case (5):
1621                   if (wei > 0x51)
1622                     {
1623                       return (0xa3a1 + (wei - 0x52));
1624                     }
1625                   else
1626                     {
1627                       return (0xa34e + (wei - 0x21));
1628                     }
1629                 }
1630             }
1631           location = ((code >> 8) - 0xa0 - CNS_HANZI_11_START_Q) * CNS_NUM_Q + (code & 0xff) - 0xa0 - 1;
1632           if ((code > 0xebd0 && code <= 0xefdb) || (code > 0xf5c5 && code <= 0xf7c6) || (code > 0xf8ad && code <= 0xf9e1))
1633             location--;
1634           else if (code == 0xebd0)
1635             return (0xbe52);
1636           else if (code == 0xf5b5)
1637             return (0xc2cb);
1638           else if (code == 0xf8ad)
1639             return (0xc456);
1640         }
1641       else
1642         {                       /* 1xxxxxxx 0xxxxxxx Case */
1643           location = ((code >> 8) - 0xa0 - CNS_HANZI_10_START_Q) * CNS_NUM_Q + (code & 0xff) - 0x20 - 1;
1644           location += BIG5_1TO2_SKIP;
1645           if (code > CNS_YIHXO && code <= 0xdb3e)
1646             location--;
1647           else if ((code >= 0xa12b && code < CNS_ONLN) ||
1648                    (code >= 0xa17d && code < CNS_MSOK) ||
1649                    (code >= 0xa439 && code <= 0xb87d) ||
1650                    (code > CNS_TNKM && code <= 0xc423) ||
1651                    (code > CNS_CYIB && code < CNS_HOOMA) ||
1652                    (code >= 0xdc6a && code < CNS_MDMR) ||
1653                    (code >= 0xe039 && code <= 0xe242) || (code > CNS_OKHAE && code <= 0xe961) || (code > CNS_CJTC && code <= 0xec51) || (code > CNS_LNNXU && code <= 0xf233))
1654             location++;
1655           else if ((code >= 0xb87e && code < CNS_TNKM) ||
1656                    (code >= 0xc424 && code < CNS_CYIB) ||
1657                    (code >= 0xe243 && code <= 0xe336) ||
1658                    (code > CNS_COLH && code <= 0xe437) ||
1659                    (code > CNS_ODC && code < CNS_OKHAE) || (code >= 0xe962 && code < CNS_CJTC) || (code >= 0xec52 && code < CNS_LNNXU) || (code == 0xf234) || (code > CNS_FDDH && code <= CNS_YPYBP))
1660             location += 2;
1661           else if ((code >= 0xe337 && code < CNS_COLH) || (code >= 0xe438 && code <= 0xe572) || (code > CNS_HBBM && code < CNS_ODC) || (code >= 0xf235 && code < CNS_FDDH))
1662             location += 3;
1663           else if (code >= 0xe573 && code < CNS_HBBM)
1664             location += 4;
1665           else if (code == CNS_ONLN)
1666             return (BIG5_ONLN);
1667           else if (code == CNS_MSOK)
1668             return (BIG5_MSOK);
1669           else if (code == CNS_TNKM)
1670             return (BIG5_TNKM);
1671           else if (code == CNS_CYIB)
1672             return (BIG5_CYIB);
1673           else if (code == CNS_YIHXO)
1674             return (BIG5_YIHXO);
1675           else if (code == CNS_MDMR)
1676             return (BIG5_MDMR);
1677           else if (code == CNS_COLH)
1678             return (BIG5_COLH);
1679           else if (code == CNS_ODC)
1680             return (BIG5_ODC);
1681           else if (code == CNS_OKHAE)
1682             return (BIG5_OKHAE);
1683           else if (code == CNS_HBBM)
1684             return (BIG5_HBBM);
1685           else if (code == CNS_CJTC)
1686             return (BIG5_CJTC);
1687           else if (code == CNS_LNNXU)
1688             return (BIG5_LNNXU);
1689           else if (code == CNS_YPYBP)
1690             return (BIG5_YPYBP);
1691           else if (code == CNS_HOOMA)
1692             return (BIG5_HOOMA);
1693           else if (code == CNS_FDDH)
1694             return (BIG5_FDDH);
1695         }
1696       qu = location / (BIG5_NUM_10_Q + BIG5_NUM_11_Q) + BIG5_HANZI_11_START_Q;
1697       loc_wei = location % (BIG5_NUM_10_Q + BIG5_NUM_11_Q);
1698 
1699       if (loc_wei < BIG5_NUM_10_Q)
1700         {                       /* 1xxxxxxx 0xxxxxxx Case */
1701           plant = 0xa020;
1702           wei = loc_wei + BIG5_HANZI_10_START_W;
1703         }
1704       else
1705         {
1706           plant = 0xa0a0;
1707           wei = loc_wei - BIG5_NUM_10_Q + 1;    /* 1xxxxxxx 1xxxxxxx Case */
1708         }
1709       return ((qu << 8) + wei + plant);
1710     }
1711   else if (which == BIG5_TO_CNS)
1712     {
1713       if (code >= 0xa3cd && code <= 0xa140)
1714         {
1715           if (code <= 0xa1bf)
1716             {
1717               if (code & 0x80)
1718                 {
1719                   return (0xa1e0 + (code - 0xa1a1));
1720                 }
1721               else
1722                 {
1723                   return (0xa1a1 + (code - 0xa140));
1724                 }
1725             }
1726           else if (code <= 0xa25e)
1727             {
1728               if (code & 0x80)
1729                 {
1730                   return (0xa2a1 + (code - 0xa1c0));
1731                 }
1732               else
1733                 {
1734                   return (0xa2e0 + (code - 0xa240));
1735                 }
1736             }
1737           else if (code <= 0xa2ae)
1738             {
1739               if (code & 0x80)
1740                 {
1741                   return (0xa3c1 + (code - 0xa2a1));
1742                 }
1743               else
1744                 {
1745                   return (0xa3a1 + (code - 0xa25f));
1746                 }
1747             }
1748           else if (code <= 0xa34e)
1749             {
1750               if (code & 0x80)
1751                 {
1752                   return (0xa4a1 + (code - 0xa2af));
1753                 }
1754               else
1755                 {
1756                   return (0xa4f1 + (code - 0xa340));
1757                 }
1758             }
1759           else
1760             {
1761               if (code & 0x80)
1762                 {
1763                   return (0xa5d2 + (code - 0xa3a1));
1764                 }
1765               else
1766                 {
1767                   return (0xa5a1 + (code - 0xa34e));
1768                 }
1769             }
1770         }
1771       if ((code & 0x8080) == 0x8080)
1772         {                       /* 1xxxxxxx 1xxxxxxx Case */
1773           location = (((code >> 8) - 0xa0 - BIG5_HANZI_11_START_Q) * (BIG5_NUM_10_Q + BIG5_NUM_11_Q) + (code & 0xff) - 0xa0 + BIG5_NUM_10_Q - 1);
1774 
1775         }
1776       else
1777         {                       /* 1xxxxxxx 0xxxxxxx Case */
1778           location = (((code >> 8) - 0xa0 - BIG5_HANZI_10_START_Q) * (BIG5_NUM_10_Q + BIG5_NUM_11_Q) + (code & 0xff) - 0x20 - BIG5_HANZI_10_START_W);
1779         }
1780       if (location < BIG5_1NUMS)
1781         {                       /* 1xxxxxxx 0xxxxxxx  Case */
1782           plant = 0xa0a0;
1783           qu = location / BIG5_NUM_11_Q + CNS_HANZI_11_START_Q;
1784           wei = location % BIG5_NUM_11_Q + CNS_HANZI_11_START_W;
1785           if ((code >= 0xbbc8 && code < 0xbe52) || (code >= 0xc1ab && code < 0xc2cb) || (code >= 0xc361 && code < 0xc456))
1786             location++;
1787           else if (code == 0xbe52)
1788             return (0xebd0);
1789           else if (code == 0xc2cb)
1790             return (0xf5b5);
1791           else if (code == 0xc456)
1792             return (0xf8ad);
1793         }
1794       else
1795         {                       /* 1xxxxxxx 1xxxxxxx Case */
1796           location -= BIG5_1TO2_SKIP;   /* For level two */
1797           if (code >= 0xeb5b && code < BIG5_CYIB)
1798             location++;
1799           else if ((code > BIG5_MU && code <= 0xc96b) ||
1800                    (code > BIG5_ONLN && code <= 0xc9ec) ||
1801                    (code > BIG5_MSOK && code < BIG5_TNKM) ||
1802                    (code >= 0xdba7 && code < BIG5_GRHNE) ||
1803                    (code >= 0xe8a3 && code <= 0xe975) ||
1804                    (code > BIG5_HOOMA && code <= 0xeda9) ||
1805                    (code > BIG5_COLH && code < BIG5_MDMR) || (code >= 0xf466 && code < BIG5_HBBM) || (code >= 0xf4fd && code < BIG5_CJTC) || (code >= 0xf977 && code < BIG5_LNNXU))
1806             location--;
1807           else if ((code > BIG5_TNKM && code <= 0xdba6) ||
1808                    (code > BIG5_GRHNE && code <= 0xe8a2) ||
1809                    (code > BIG5_MDMR && code < BIG5_YIHXO) ||
1810                    (code >= 0xf163 && code < BIG5_ODC) ||
1811                    (code >= 0xf375 && code <= 0xf465) || (code > BIG5_HBBM && code <= 0xf4fc) || (code > BIG5_CJTC && code <= 0xf976) || (code == 0xf9c5) || (code >= 0xf9d2 && code <= BIG5_YPYBP))
1812             location -= 2;
1813           else if ((code > BIG5_YIHXO && code <= 0xf162) || (code > BIG5_ODC && code < BIG5_OKHAE) || (code >= 0xf2c3 && code <= 0xf374) || (code > BIG5_FDDH && code <= 0xf9d1))
1814             location -= 3;
1815           else if (code > BIG5_OKHAE && code <= 0xf2c2)
1816             location -= 4;
1817           else if (code == BIG5_ONLN)
1818             return (CNS_ONLN);
1819           else if (code == BIG5_MSOK)
1820             return (CNS_MSOK);
1821           else if (code == BIG5_TNKM)
1822             return (CNS_TNKM);
1823           else if (code == BIG5_CYIB)
1824             return (CNS_CYIB);
1825           else if (code == BIG5_YIHXO)
1826             return (CNS_YIHXO);
1827           else if (code == BIG5_MDMR)
1828             return (CNS_MDMR);
1829           else if (code == BIG5_COLH)
1830             return (CNS_COLH);
1831           else if (code == BIG5_ODC)
1832             return (CNS_ODC);
1833           else if (code == BIG5_OKHAE)
1834             return (CNS_OKHAE);
1835           else if (code == BIG5_HBBM)
1836             return (CNS_HBBM);
1837           else if (code == BIG5_CJTC)
1838             return (CNS_CJTC);
1839           else if (code == BIG5_LNNXU)
1840             return (CNS_LNNXU);
1841           else if (code == BIG5_FDDH)
1842             return (CNS_FDDH);
1843           else if (code == BIG5_YPYBP)
1844             return (CNS_YPYBP);
1845           else if (code == BIG5_HOOMA)
1846             return (CNS_HOOMA);
1847           else if (code == BIG5_MU)
1848             return (CNS_SPACE);
1849           else if (code == BIG5_GRHNE)
1850             return (CNS_SPACE);
1851 
1852           plant = 0xa020;
1853           qu = location / BIG5_NUM_11_Q + CNS_HANZI_10_START_Q;
1854           wei = location % BIG5_NUM_11_Q + CNS_HANZI_10_START_W;
1855         }
1856       return ((qu << 8) + wei + plant);
1857     }
1858   return (0);
1859 }
1860 
1861 #ifdef  ECNS_IS_UCNS
1862 int
ecns_to_icns(icns,ucns,siz)1863 ecns_to_icns (icns, ucns, siz)
1864      w_char *icns;
1865      unsigned char *ucns;
1866      int siz;
1867 {
1868   register w_char *i = icns;
1869   register unsigned char *u = ucns, *uend = ucns + siz, x;
1870   static w_char local_pending = (w_char) 0;
1871   static unsigned char shift_mode = '\0';
1872 
1873   if (siz <= 0)
1874     return (0);
1875   if (local_pending)
1876     {
1877       *i = local_pending | (*u++ & 0x7f);
1878       if (shift_mode == SS3)
1879         {
1880           *i++ |= 0x8000;
1881           shift_mode = '\0';
1882         }
1883       else
1884         {
1885           *i++ |= 0x8080;
1886         }
1887       local_pending = (w_char) 0;
1888     }
1889   if (shift_mode == SS2)
1890     {
1891       *i++ = *u++;
1892       shift_mode = '\0';
1893     }
1894   for (; u < uend;)
1895     {
1896       x = *u++;
1897       if (x == SS2)
1898         {
1899           if (u == uend)
1900             {
1901               shift_mode = SS2;
1902               break;
1903             }
1904           *i++ = *u++;
1905           break;
1906         }
1907       else if (x == SS3)
1908         {
1909           if (u == uend)
1910             {
1911               shift_mode = SS3;
1912               break;
1913             }
1914           *i = ((*u++ & 0x7f) << 8);
1915           if (u == uend)
1916             {
1917               local_pending = *i;
1918               break;
1919             }
1920           *i |= (*u++ & 0x7f);
1921           *i++ |= 0x8000;
1922         }
1923       else if (x > 0x7f)
1924         {
1925           *i = ((x & 0x7f) << 8);
1926           if (u == uend)
1927             {
1928               local_pending = *i;
1929               break;
1930             }
1931           *i |= (*u++ & 0x7f);
1932           *i++ |= 0x8080;
1933         }
1934       else
1935         {
1936           *i++ = x;
1937         }
1938     }
1939   return ((char *) i - (char *) icns);
1940 }
1941 
1942 int
icns_to_ecns(ucns,icns,siz)1943 icns_to_ecns (ucns, icns, siz)
1944      unsigned char *ucns;
1945      w_char *icns;
1946      int siz;
1947 {
1948   register unsigned char *u = ucns;
1949   register w_char *i = icns, w;
1950 
1951   for (; siz > 0; siz -= sizeof (w_char))
1952     {
1953       w = *i++;
1954       if (!(w & 0xff80))
1955         {                       /* CS0 */
1956           *u++ = (unsigned char) w;
1957         }
1958       else if (!(w & 0xff00))
1959         {                       /* CS2 */
1960           *u++ = SS2;
1961           *u++ = (unsigned char) w;
1962         }
1963       else if (w & 0x80)
1964         {                       /* CS1 */
1965           *u++ = (unsigned char) ((w & 0xff00) >> 8);
1966           *u++ = (unsigned char) (w & 0xff);
1967         }
1968       else
1969         {                       /* CS3 */
1970           *u++ = SS3;
1971           *u++ = (unsigned char) ((w & 0xff00) >> 8);
1972           *u++ = (unsigned char) ((w & 0xff) | 0x80);
1973         }
1974     }
1975   return (u - ucns);
1976 }
1977 #else /* ECNS_IS_UCNS */
1978 
1979 static int oc_mode = ASCII;
1980 static unsigned char *cns;
1981 
1982 static void
putcns(x)1983 putcns (x)
1984      unsigned char x;
1985 {
1986   *cns++ = x;
1987 }
1988 
1989 static void
cns_change_mode(mode,new_mode)1990 cns_change_mode (mode, new_mode)
1991      int *mode;
1992      int new_mode;
1993 {
1994   if (*mode == new_mode)
1995     return;
1996   switch (*mode = new_mode)
1997     {
1998     case CNS11643_1:
1999       putcns ('\033');
2000       putcns ('$');
2001       putcns (')');
2002       putcns ('0');
2003       break;
2004     case CNS11643_2:
2005       putcns ('\033');
2006       putcns ('$');
2007       putcns ('*');
2008       putcns ('1');
2009       break;
2010     case ASCII:
2011       /* designate ISO-8859-1 rather than JIS X 0201 */
2012       /* putcns('\033'); putcns('('); putcns('J'); break; */
2013       putcns ('\033');
2014       putcns ('(');
2015       putcns ('B');
2016       break;
2017     }
2018 }
2019 
2020 int
ecns_to_icns(icns,ecns,siz)2021 ecns_to_icns (icns, ecns, siz)
2022      w_char *icns;
2023      unsigned char *ecns;
2024      int siz;
2025 {
2026   designate = CNS_designate;
2027   return (extc_to_intc (icns, ecns, siz));
2028 }
2029 
2030 int
icns_to_ecns(ecns,icns,siz)2031 icns_to_ecns (ecns, icns, siz)
2032      unsigned char *ecns;
2033      w_char *icns;
2034      int siz;
2035 {
2036   register int i = siz;
2037   register w_char *ic, x;
2038   cns = ecns;
2039 
2040   for (; i > 0; i -= sizeof (w_char))
2041     {
2042       x = *ic++;
2043       if ((x & 0x8080) == 0x8080)
2044         {
2045           cns_change_mode (&oc_mode, CNS11643_1);
2046           putcns ((x >> 8) & 0xff);
2047           putcns (x & 0xff);
2048         }
2049       else if ((x & 0x8000) == 0x8000)
2050         {
2051           cns_change_mode (&oc_mode, CNS11643_2);
2052           putcns (((x >> 8) & 0x7f) | 0x80);
2053           putcns ((x & 0x7f) | 0x80);
2054         }
2055       else
2056         {
2057           cns_change_mode (&oc_mode, ASCII);
2058           putcns (x & 0x7f);
2059         }
2060     }
2061   cns_change_mode (&oc_mode, ASCII);
2062   return (cns - ecns);
2063 }
2064 #endif /* ECNS_IS_UCNS */
2065 
2066 int
icns_to_big5(big5,icns,siz)2067 icns_to_big5 (big5, icns, siz)
2068      unsigned char *big5;
2069      w_char *icns;
2070      int siz;
2071 {
2072   register unsigned char *d = big5;
2073   register w_char *s = icns;
2074   register int i = siz;
2075   short code_out;               /* Buffering one two-byte code  */
2076 
2077   if (d == NULL || s == NULL)
2078     return (-1);
2079 
2080   for (; i > 0; i -= sizeof (w_char))
2081     {
2082       if (!(*s & 0xff00))
2083         {                       /* Ascii */
2084           if (!(*s & 0x80))
2085             {
2086               *d++ = (unsigned char) (*s++ & 0x7f);
2087             }
2088           else
2089             {                   /* Single Shift */
2090               *d++ = SS2;
2091               *d++ = (unsigned char) (*s++ & 0xff);
2092             }
2093         }
2094       else if (_is_hanzi (*s, CNS_TO_BIG5))
2095         {
2096           code_out = _convert (*s++, CNS_TO_BIG5);
2097           *d++ = (code_out >> 8);
2098           *d++ = code_out & 0x00ff;
2099         }
2100       else
2101         {                       /* Strainge codes */
2102           *d++ = (unsigned char) ((*s & 0xff00) >> 8);
2103           *d++ = (unsigned char) (*s++ & 0xff);
2104         }
2105     }
2106   *d = '\0';
2107   return (d - big5);
2108 }
2109 
2110 int
ecns_to_big5(big5,ecns,siz)2111 ecns_to_big5 (big5, ecns, siz)
2112      unsigned char *big5, *ecns;
2113      int siz;
2114 {
2115   int len;
2116   len = ecns_to_icns (tmp_w_buf, ecns, siz);
2117   return (icns_to_big5 (big5, tmp_w_buf, len));
2118 }
2119 
2120 int
big5_to_icns(icns,big5,siz)2121 big5_to_icns (icns, big5, siz)
2122      w_char *icns;
2123      unsigned char *big5;
2124      int siz;
2125 {
2126   register w_char *d = icns;
2127   register unsigned char *s = big5;
2128   unsigned char *send = s + siz;
2129   unsigned short code_in;       /* Buffering one two-byte code  */
2130 
2131   if (d == NULL || s == NULL)
2132     return (-1);
2133 
2134   for (; s < send; s++)
2135     {
2136       if (!(*s & 0x80))
2137         {                       /* Ascii */
2138           *d++ = (w_char) * s;
2139         }
2140       else if (*s == 0x8e)
2141         {                       /* Single Shift */
2142           *d++ = (w_char) * (++s);
2143         }
2144       else
2145         {
2146           code_in = ((*s++) << 8);
2147           code_in |= *s;
2148           if (_is_hanzi (code_in, BIG5_TO_CNS))
2149             {
2150               *d++ = _convert (code_in, BIG5_TO_CNS);
2151             }
2152           else
2153             {                   /* Strainge codes */
2154               *d++ = code_in;
2155             }
2156         }
2157     }
2158   *d = (w_char) 0;
2159   return ((char *) d - (char *) icns);
2160 }
2161 
2162 int
big5_to_ecns(ecns,big5,siz)2163 big5_to_ecns (ecns, big5, siz)
2164      unsigned char *ecns, *big5;
2165      int siz;
2166 {
2167   int len;
2168   len = big5_to_icns (tmp_w_buf, big5, siz);
2169   return (icns_to_ecns (ecns, tmp_w_buf, len));
2170 }
2171 
2172 int
iugb_to_eugb(eugb,iugb,siz)2173 iugb_to_eugb (eugb, iugb, siz)
2174      unsigned char *eugb;
2175      w_char *iugb;
2176      int siz;
2177 {
2178   static int first = 0;
2179   static unsigned int cswidth_id;
2180 
2181   if (first == 0)
2182     {
2183       cswidth_id = create_cswidth (UGB_CSWIDTH);
2184       first++;
2185     }
2186   set_cswidth (cswidth_id);
2187   return (ieuc_to_eeuc (eugb, iugb, siz));
2188 }
2189 
2190 int
eugb_to_iugb(iugb,eugb,siz)2191 eugb_to_iugb (iugb, eugb, siz)
2192      w_char *iugb;
2193      unsigned char *eugb;
2194      int siz;
2195 {
2196   static int first = 0;
2197   static unsigned int cswidth_id;
2198 
2199   if (first == 0)
2200     {
2201       cswidth_id = create_cswidth (UGB_CSWIDTH);
2202       first++;
2203     }
2204   set_cswidth (cswidth_id);
2205   return (eeuc_to_ieuc (iugb, eugb, siz));
2206 }
2207 #endif /* CHINESE */
2208 
2209 
2210 #ifdef  KOREAN
2211 
2212 #define ZENKAKU_KSC     1
2213 
2214 static unsigned char *ks;
2215 static w_char *iuk;
2216 static unsigned char *euk;
2217 
2218 static void
putks(x)2219 putks (x)
2220      int x;
2221 {
2222   *ks++ = x;
2223 }
2224 
2225 static int oks_mode = ASCII;    /* ���ϻ���KSC�����ɤΥ⡼�� */
2226 extern int euksc_to_iuksc ();
2227 
2228 static void
ksc_change_mode(mode,new_mode)2229 ksc_change_mode (mode, new_mode)
2230      int *mode;
2231      int new_mode;
2232 {
2233   if (*mode == new_mode)
2234     return;
2235   switch (*mode)
2236     {
2237     case ZENKAKU_KSC:
2238       putks ('\033');
2239       putks ('(');
2240       putks ('B');
2241       break;
2242     default:;
2243     }
2244   *mode = new_mode;
2245   switch (new_mode)
2246     {
2247     case ZENKAKU_KSC:
2248       putks ('\033');
2249       putks ('$');
2250       putks ('(');
2251       putks ('C');
2252       break;
2253     default:;
2254     }
2255 }
2256 
2257 /*      ���� U-ksc �� ksc �����ɤ��Ѵ����ޤ�
2258         ʸ�����Ĺ�����֤��ޤ�                  */
2259 extern int
2260 iuksc_to_ksc (ksc, iuksc, iusiz)
2261      unsigned char *ksc;        /*      ksc�����ɤˤʤä���Τ���buf  */
2262      w_char *iuksc;             /*      iuksc�����ɤΤ�Τ����Ƥ���buf */
2263      int iusiz;                 /*      iuksc ���礭��                  */
2264 {
2265   int x;
2266   ks = ksc;
2267   iuk = iuksc;
2268   for (; iusiz > 0; iusiz -= sizeof (w_char))
2269     {
2270       x = *iuk++;
2271       if ((x & 0x8080) == 0x8080)
2272         {
2273           ksc_change_mode (&oks_mode, ZENKAKU_KSC);
2274           putks ((x >> 8) & 0x7f);
2275           putks (x & 0x7f);
2276         }
2277       else
2278         {
2279           ksc_change_mode (&oks_mode, ASCII);
2280           putks (x);
2281         }
2282     }
2283   ksc_change_mode (&oks_mode, ASCII);
2284   return (ks - ksc);
2285 }
2286 
2287 
2288 /*      ���� U-ksc �� ksc �����ɤ��Ѵ����ޤ�    */
2289 extern int
2290 euksc_to_ksc (ksc, euksc, eusiz)
2291      unsigned char *ksc, *euksc;
2292      int eusiz;
2293 {
2294   static int kanji1 = 0;
2295   static unsigned char kanji1_code = 0;
2296   /* 0: normal
2297      1: get SS2
2298      2: get kanji 1 byte */
2299   int x;
2300   ks = ksc;
2301   euk = euksc;
2302   if (kanji1 != 0)
2303     {
2304       if (kanji1 == 2)
2305         {
2306           putks (kanji1_code & 0x7f);
2307           putks (*euk & 0x7f);
2308         }
2309       else
2310         {
2311           putks (*euk);
2312         }
2313       eusiz -= sizeof (char);
2314       kanji1 = 0;
2315       euk++;
2316     }
2317   for (; eusiz > 0; eusiz -= sizeof (char))
2318     {
2319       x = *euk++;
2320       if (x & 0x80)
2321         {
2322           ksc_change_mode (&oks_mode, ZENKAKU_KSC);
2323           if (eusiz > 1)
2324             {
2325               putks (x & 0x7f);
2326               putks (*euk++ & 0x7f);
2327               eusiz -= sizeof (char);
2328             }
2329           else
2330             {
2331               kanji1 = 2;
2332               kanji1_code = x;
2333             }
2334         }
2335       else
2336         {
2337           ksc_change_mode (&oks_mode, ASCII);
2338           putks (x);
2339         }
2340     }
2341   if (kanji1 == 0)
2342     ksc_change_mode (&oks_mode, ASCII);
2343   return (ks - ksc);
2344 }
2345 
2346 /*      ���� U-ksc �� ���� U-ksc �����ɤ��Ѵ����ޤ�     */
2347 extern int
2348 iuksc_to_euksc (euksc, iuksc, iusiz)
2349      unsigned char *euksc;
2350      w_char *iuksc;
2351      int iusiz;
2352 {
2353   static int first = 0;
2354   static unsigned int cswidth_id;
2355 
2356   if (first == 0)
2357     {
2358       cswidth_id = create_cswidth (UKSC_CSWIDTH);
2359       first++;
2360     }
2361   set_cswidth (cswidth_id);
2362   return (ieuc_to_eeuc (euksc, iuksc, iusiz));
2363 }
2364 
2365 int
ksc_to_euksc(euksc,ksc,jsiz)2366 ksc_to_euksc (euksc, ksc, jsiz)
2367      unsigned char *euksc, *ksc;
2368      int jsiz;
2369 {
2370   int len;
2371 
2372   designate = KSC_designate;
2373   len = extc_to_intc (tmp_w_buf, ksc, jsiz);
2374   return (iuksc_to_euksc (euksc, tmp_w_buf, len));
2375 }
2376 
2377 int
ksc_to_iuksc(iuksc,ksc,jsiz)2378 ksc_to_iuksc (iuksc, ksc, jsiz)
2379      w_char *iuksc;
2380      unsigned char *ksc;
2381      int jsiz;
2382 {
2383   designate = KSC_designate;
2384   return (extc_to_intc (iuksc, ksc, jsiz));
2385 }
2386 
2387 int
euksc_to_iuksc(iuksc,euksc,eusiz)2388 euksc_to_iuksc (iuksc, euksc, eusiz)
2389      w_char *iuksc;
2390      unsigned char *euksc;
2391      int eusiz;
2392 {
2393   static int first = 0;
2394   static unsigned int cswidth_id;
2395 
2396   if (first == 0)
2397     {
2398       cswidth_id = create_cswidth (UKSC_CSWIDTH);
2399       first++;
2400     }
2401   set_cswidth (cswidth_id);
2402   return (eeuc_to_ieuc (iuksc, euksc, eusiz));
2403 }
2404 
2405 #endif /* KOREAN */
2406