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
|
52
|
31 unsigned int codeconv_euc_to_jis(unsigned int euc)
|
0
|
32 {
|
52
|
33 unsigned int hi, low;
|
0
|
34
|
52
|
35 hi = (euc >> 8) & 0xff;
|
|
36 low = euc & 0xff;
|
0
|
37
|
52
|
38 if (hi < 0x81) {
|
|
39 hi = 0;
|
|
40 } else if (low == 0x8e)
|
|
41 hi = 0;
|
|
42 else {
|
|
43 hi -= 0x80;
|
|
44 low -= 0x80;
|
|
45 }
|
0
|
46
|
52
|
47 return (hi << 8) | low;
|
0
|
48 }
|
|
49
|
52
|
50 static unsigned int codeconv_jis_to_unicode(unsigned int jis)
|
0
|
51 {
|
52
|
52 int k0, k1;
|
|
53
|
|
54 if (jis < 0x80) return jis; // ASCII
|
|
55
|
|
56 k0 = (jis >> 8) - 0x20;
|
0
|
57
|
52
|
58 if (k0 < 1 || k0 > 92)
|
|
59 return 0;
|
0
|
60
|
52
|
61 k1 = (jis % 0x100) - 0x20;
|
|
62 if (k1 < 1 || k1 > 94)
|
|
63 return 0;
|
0
|
64
|
52
|
65 return unicode_tbl[k0 - 1][k1 - 1];
|
0
|
66 }
|
|
67
|
52
|
68 unsigned int codeconv_euc_to_unicode(unsigned int euc)
|
0
|
69 {
|
52
|
70 unsigned int jis, unicode;
|
0
|
71
|
52
|
72 jis = codeconv_euc_to_jis(euc);
|
|
73 unicode = codeconv_jis_to_unicode(jis);
|
0
|
74
|
52
|
75 return unicode;
|
0
|
76 }
|
|
77
|
52
|
78 static unsigned int codeconv_unicode_to_jis(unsigned int unicode)
|
0
|
79 {
|
52
|
80 int k0, k1;
|
|
81 unsigned int jis;
|
0
|
82
|
52
|
83 k0 = (unicode >> 8) & 0xff;
|
|
84 k1 = unicode & 0xff;
|
|
85 jis = unicode_rev_table[k0][k1];
|
|
86
|
|
87 return jis;
|
0
|
88 }
|
|
89
|
52
|
90 static unsigned int codeconv_jis_to_euc(unsigned int jis)
|
0
|
91 {
|
52
|
92 unsigned int hi, low;
|
0
|
93
|
52
|
94 hi = (jis >> 8) & 0x7f | 0x80;
|
|
95 low = jis & 0x7f | 0x80;
|
0
|
96
|
52
|
97 return (hi << 8) | low;
|
0
|
98 }
|
|
99
|
52
|
100 unsigned int codeconv_unicode_to_euc(unsigned int unicode)
|
0
|
101 {
|
52
|
102 unsigned int jis, euc;
|
|
103
|
|
104 if (unicode >= 0xff61 && unicode <= 0xff9f)
|
|
105 return unicode - 0xff61 + 0x8ea1;
|
0
|
106
|
52
|
107 jis = codeconv_unicode_to_jis(unicode);
|
|
108 if (jis == 0)
|
|
109 return 0x7878;
|
|
110 euc = codeconv_jis_to_euc(jis);
|
0
|
111
|
52
|
112 return euc;
|
0
|
113 }
|
|
114
|
52
|
115 static unsigned int codeconv_jis_to_sjis(unsigned int jis)
|
0
|
116 {
|
52
|
117 unsigned int hi, low;
|
0
|
118
|
52
|
119 hi = (jis >> 8) & 0xff;
|
|
120 low = jis & 0xff;
|
0
|
121
|
52
|
122 low += (hi & 0x01) ? 0x1f : 0x7d;
|
|
123 if (low >= 0x7f)
|
|
124 low++;
|
|
125 hi = ((hi - 0x21) >> 1) + 0x81;
|
|
126 if (hi > 0x9f)
|
|
127 hi += 0x40;
|
0
|
128
|
52
|
129 return (hi << 8) | low;
|
0
|
130 }
|
|
131
|
52
|
132 unsigned int codeconv_euc_to_sjis(unsigned int euc)
|
0
|
133 {
|
52
|
134 unsigned int jis, sjis;
|
0
|
135
|
52
|
136 jis = codeconv_euc_to_jis(euc);
|
|
137 sjis = codeconv_jis_to_sjis(jis);
|
0
|
138
|
52
|
139 return sjis;
|
0
|
140 }
|
|
141
|
52
|
142 static unsigned int codeconv_sjis_to_jis(unsigned int sjis)
|
0
|
143 {
|
52
|
144 unsigned int hi, low;
|
0
|
145
|
52
|
146 hi = (sjis >> 8) & 0xff;
|
|
147 low = sjis & 0xff;
|
0
|
148
|
52
|
149 hi -= (hi <= 0x9f) ? 0x71 : 0xb1;
|
|
150 hi = (hi << 1) + 1;
|
|
151 if (low > 0x7f)
|
|
152 low--;
|
|
153 if (low >= 0x9e) {
|
|
154 low -= 0x7d;
|
|
155 hi++;
|
|
156 } else low -= 0x1f;
|
|
157
|
|
158 return (hi << 8) | low;
|
0
|
159 }
|
|
160
|
52
|
161 unsigned int codeconv_sjis_to_euc(unsigned int sjis)
|
0
|
162 {
|
52
|
163 unsigned int jis, euc;
|
0
|
164
|
52
|
165 jis = codeconv_sjis_to_jis(sjis);
|
|
166 euc = codeconv_jis_to_euc(jis);
|
0
|
167
|
52
|
168 return euc;
|
0
|
169 }
|
|
170
|
52
|
171 unsigned int codeconv_euc_to_latin1(unsigned int euc)
|
0
|
172 {
|
|
173 int high = (euc>>8) & 0xff;
|
|
174 if (high) return 0;
|
|
175 return euc & 0xff;
|
|
176 }
|