0
|
1 /*
|
|
2 * Copyright (c) 2000 Yuki Sawada
|
|
3 * All rights reserved.
|
|
4 *
|
|
5 * Redistribution and use in source and binary forms, with or without
|
|
6 * modification, are permitted provided that the following conditions
|
|
7 * are met:
|
|
8 * 1. Redistributions of source code must retain the above copyright
|
|
9 * notice, this list of conditions and the following disclaimer.
|
|
10 * 2. Redistributions in binary form must reproduce the above copyright
|
|
11 * notice, this list of conditions and the following disclaimer in the
|
|
12 * documentation and/or other materials provided with the distribution.
|
|
13 * 3. The name of the author may not be used to endorse or promote products
|
|
14 * derived from this software without specific prior written permission.
|
|
15 *
|
|
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26 */
|
|
27
|
|
28 #include "codeconv.h"
|
|
29 #include "codeconv_tbl.h"
|
|
30
|
|
31 static unsigned int
|
|
32 codeconv_euc_to_jis(unsigned int euc)
|
|
33 {
|
|
34 unsigned int hi, low;
|
|
35
|
|
36 hi = (euc >> 8) & 0xff;
|
|
37 low = euc & 0xff;
|
|
38
|
|
39 if (hi < 0x81) {
|
|
40 hi = 0;
|
|
41 } else if (low == 0x8e)
|
|
42 hi = 0;
|
|
43 else {
|
|
44 hi -= 0x80;
|
|
45 low -= 0x80;
|
|
46 }
|
|
47
|
|
48 return (hi << 8) | low;
|
|
49 }
|
|
50
|
|
51 static unsigned int
|
|
52 codeconv_jis_to_unicode(unsigned int jis)
|
|
53 {
|
|
54 int k0, k1;
|
|
55
|
|
56 if (jis < 0x80) return jis; // ASCII
|
|
57 k0 = (jis >> 8) - 0x20;
|
|
58 if (k0 < 1 || k0 > 92)
|
|
59 return 0;
|
|
60
|
|
61 k1 = (jis % 0x100) - 0x20;
|
|
62 if (k1 < 1 || k1 > 94)
|
|
63 return 0;
|
|
64
|
|
65 return unicode_tbl[k0 - 1][k1 - 1];
|
|
66 }
|
|
67
|
|
68 unsigned int
|
|
69 codeconv_euc_to_unicode(unsigned int euc)
|
|
70 {
|
|
71 unsigned int jis, unicode;
|
|
72
|
|
73 jis = codeconv_euc_to_jis(euc);
|
|
74 unicode = codeconv_jis_to_unicode(jis);
|
|
75
|
|
76 return unicode;
|
|
77 }
|
|
78
|
|
79 static unsigned int
|
|
80 codeconv_unicode_to_jis(unsigned int unicode)
|
|
81 {
|
|
82 int k0, k1;
|
|
83 unsigned int jis;
|
|
84
|
|
85 k0 = (unicode >> 8) & 0xff;
|
|
86 k1 = unicode & 0xff;
|
|
87 jis = unicode_rev_table[k0][k1];
|
|
88
|
|
89 return jis;
|
|
90 }
|
|
91
|
|
92 static unsigned int
|
|
93 codeconv_jis_to_euc(unsigned int jis)
|
|
94 {
|
|
95 unsigned int hi, low;
|
|
96
|
|
97 hi = (jis >> 8) & 0x7f | 0x80;
|
|
98 low = jis & 0x7f | 0x80;
|
|
99
|
|
100 return (hi << 8) | low;
|
|
101 }
|
|
102
|
|
103 unsigned int
|
|
104 codeconv_unicode_to_euc(unsigned int unicode)
|
|
105 {
|
|
106 unsigned int jis, euc;
|
|
107
|
|
108 if (unicode >= 0xff61 && unicode <= 0xff9f)
|
|
109 return unicode - 0xff61 + 0x8ea1;
|
|
110
|
|
111 jis = codeconv_unicode_to_jis(unicode);
|
|
112 if (jis == 0)
|
|
113 return 0x7878;
|
|
114 euc = codeconv_jis_to_euc(jis);
|
|
115
|
|
116 return euc;
|
|
117 }
|
|
118
|
|
119 static unsigned int
|
|
120 codeconv_jis_to_sjis(unsigned int jis)
|
|
121 {
|
|
122 unsigned int hi, low;
|
|
123
|
|
124 hi = (jis >> 8) & 0xff;
|
|
125 low = jis & 0xff;
|
|
126
|
|
127 low += (hi & 0x01) ? 0x1f : 0x7d;
|
|
128 if (low >= 0x7f)
|
|
129 low++;
|
|
130 hi = ((hi - 0x21) >> 1) + 0x81;
|
|
131 if (hi > 0x9f)
|
|
132 hi += 0x40;
|
|
133
|
|
134 return (hi << 8) | low;
|
|
135 }
|
|
136
|
|
137 unsigned int
|
|
138 codeconv_euc_to_sjis(unsigned int euc)
|
|
139 {
|
|
140 unsigned int jis, sjis;
|
|
141
|
|
142 jis = codeconv_euc_to_jis(euc);
|
|
143 sjis = codeconv_jis_to_sjis(jis);
|
|
144
|
|
145 return sjis;
|
|
146 }
|
|
147
|
|
148 static unsigned int
|
|
149 codeconv_sjis_to_jis(unsigned int sjis)
|
|
150 {
|
|
151 unsigned int hi, low;
|
|
152
|
|
153 hi = (sjis >> 8) & 0xff;
|
|
154 low = sjis & 0xff;
|
|
155
|
|
156 hi -= (hi <= 0x9f) ? 0x71 : 0xb1;
|
|
157 hi = (hi << 1) + 1;
|
|
158 if (low > 0x7f)
|
|
159 low--;
|
|
160 if (low >= 0x9e) {
|
|
161 low -= 0x7d;
|
|
162 hi++;
|
|
163 } else
|
|
164 low -= 0x1f;
|
|
165
|
|
166 return (hi << 8) | low;
|
|
167 }
|
|
168
|
|
169 unsigned int
|
|
170 codeconv_sjis_to_euc(unsigned int sjis)
|
|
171 {
|
|
172 unsigned int jis, euc;
|
|
173
|
|
174 jis = codeconv_sjis_to_jis(sjis);
|
|
175 euc = codeconv_jis_to_euc(jis);
|
|
176
|
|
177 return euc;
|
|
178 }
|
|
179
|
|
180 unsigned int
|
|
181 codeconv_euc_to_latin1(unsigned int euc)
|
|
182 {
|
|
183 int high = (euc>>8) & 0xff;
|
|
184 if (high) return 0;
|
|
185 return euc & 0xff;
|
|
186 }
|