1 /* $Header: /home/yav/catty/fkiss/RCS/jiscode.c,v 1.3 2000/08/24 02:22:37 yav Exp $
2  * JIS code <-> Shift-JIS code
3  * written by yav <yav@bigfoot.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 char id_jiscode[] = "$Id: jiscode.c,v 1.3 2000/08/24 02:22:37 yav Exp $";
21 
is_sjis_1st(c)22 int is_sjis_1st(c)
23      int c;
24 {
25   return ((c)>=0x81 && (c)<=0x9f)||((c)>=0xe0 && (c)<=0xfc);
26 }
27 
is_sjis_2nd(c)28 int is_sjis_2nd(c)
29      int c;
30 {
31   return ((c)>=0x40 && (c)<=0x7e)||((c)>=0x80 && (c)<=0xfc);
32 }
33 
ysjis2jis(c)34 unsigned short ysjis2jis(c)
35      unsigned short c;
36 {
37   unsigned char h, l;
38   unsigned short dd;
39 
40   h = c >> 8;
41   l = c & 0xff;
42   if (!is_sjis_1st(h) || !is_sjis_2nd(l))
43     return 0;
44   if (h >= 0xe0)
45     h -= 0x40;
46   if (l < 0x9f) {
47     if (l >= 0x80)
48       l -= 1;
49     dd = 0xe11f;
50   } else {
51     dd = 0xe07e;
52   }
53   return (h << 9) + l - dd;
54 }
55 
56 
yjis2sjis(c)57 unsigned short yjis2sjis(c)
58      unsigned short c;
59 {
60   unsigned short h, l;
61 
62   l = c & 0xff;
63   h = c >> 8;
64   if ((h < 0x21)||(h > 0x7e)||(l < 0x21)||(l > 0x7e))
65     return 0;
66   if ((h & 1) != 0) {
67     l += 0x1f;
68     if (l >= 0x7f)
69       l++;
70   } else {
71     l += 0x7e;
72   }
73   h += 0xe1;
74   h >>= 1;
75   if (h >= 0xa0)
76     h += 0x40;
77   return (h << 8) + l;
78 }
79 
80 /* End of file */
81