1 /*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter_ja.c
26 * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
27 *
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "mbfilter.h"
35 #include "mbfilter_cp51932.h"
36
37 #include "unicode_table_cp932_ext.h"
38 #include "unicode_table_jis.h"
39 #include "cp932_table.h"
40
41 static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter);
42
43 static const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
44 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,
53 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
56 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
57 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
58 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
59 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
60 };
61
62
63 static const char *mbfl_encoding_cp51932_aliases[] = {"cp51932", NULL};
64
65 const struct mbfl_identify_vtbl vtbl_identify_cp51932 = {
66 mbfl_no_encoding_cp51932,
67 mbfl_filt_ident_common_ctor,
68 mbfl_filt_ident_common_dtor,
69 mbfl_filt_ident_cp51932
70 };
71
72 const mbfl_encoding mbfl_encoding_cp51932 = {
73 mbfl_no_encoding_cp51932,
74 "CP51932",
75 "CP51932",
76 (const char *(*)[])&mbfl_encoding_cp51932_aliases,
77 mblen_table_eucjp,
78 MBFL_ENCTYPE_MBCS
79 };
80
81 const struct mbfl_convert_vtbl vtbl_cp51932_wchar = {
82 mbfl_no_encoding_cp51932,
83 mbfl_no_encoding_wchar,
84 mbfl_filt_conv_common_ctor,
85 mbfl_filt_conv_common_dtor,
86 mbfl_filt_conv_cp51932_wchar,
87 mbfl_filt_conv_common_flush
88 };
89
90 const struct mbfl_convert_vtbl vtbl_wchar_cp51932 = {
91 mbfl_no_encoding_wchar,
92 mbfl_no_encoding_cp51932,
93 mbfl_filt_conv_common_ctor,
94 mbfl_filt_conv_common_dtor,
95 mbfl_filt_conv_wchar_cp51932,
96 mbfl_filt_conv_common_flush
97 };
98
99 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
100
101 #define sjistoidx(c1, c2) \
102 (((c1) > 0x9f) \
103 ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
104 : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
105 #define idxtoeuc1(c) (((c) / 94) + 0xa1)
106 #define idxtoeuc2(c) (((c) % 94) + 0xa1)
107
108 /*
109 * cp51932 => wchar
110 */
111 int
mbfl_filt_conv_cp51932_wchar(int c,mbfl_convert_filter * filter)112 mbfl_filt_conv_cp51932_wchar(int c, mbfl_convert_filter *filter)
113 {
114 int c1, s, w;
115
116 switch (filter->status) {
117 case 0:
118 if (c >= 0 && c < 0x80) { /* latin */
119 CK((*filter->output_function)(c, filter->data));
120 } else if (c > 0xa0 && c < 0xff) { /* CP932 first char */
121 filter->status = 1;
122 filter->cache = c;
123 } else if (c == 0x8e) { /* kana first char */
124 filter->status = 2;
125 } else {
126 w = c & MBFL_WCSGROUP_MASK;
127 w |= MBFL_WCSGROUP_THROUGH;
128 CK((*filter->output_function)(w, filter->data));
129 }
130 break;
131
132 case 1: /* got first half */
133 filter->status = 0;
134 c1 = filter->cache;
135 if (c > 0xa0 && c < 0xff) {
136 w = 0;
137 s = (c1 - 0xa1)*94 + c - 0xa1;
138 if (s <= 137) {
139 if (s == 31) {
140 w = 0xff3c; /* FULLWIDTH REVERSE SOLIDUS */
141 } else if (s == 32) {
142 w = 0xff5e; /* FULLWIDTH TILDE */
143 } else if (s == 33) {
144 w = 0x2225; /* PARALLEL TO */
145 } else if (s == 60) {
146 w = 0xff0d; /* FULLWIDTH HYPHEN-MINUS */
147 } else if (s == 80) {
148 w = 0xffe0; /* FULLWIDTH CENT SIGN */
149 } else if (s == 81) {
150 w = 0xffe1; /* FULLWIDTH POUND SIGN */
151 } else if (s == 137) {
152 w = 0xffe2; /* FULLWIDTH NOT SIGN */
153 }
154 }
155 if (w == 0) {
156 if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) { /* vendor ext1 (13ku) */
157 w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
158 } else if (s >= 0 && s < jisx0208_ucs_table_size) { /* X 0208 */
159 w = jisx0208_ucs_table[s];
160 } else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) { /* vendor ext2 (89ku - 92ku) */
161 w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
162 }
163 }
164 if (w <= 0) {
165 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
166 w &= MBFL_WCSPLANE_MASK;
167 w |= MBFL_WCSPLANE_WINCP932;
168 }
169 CK((*filter->output_function)(w, filter->data));
170 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
171 CK((*filter->output_function)(c, filter->data));
172 } else {
173 w = (c1 << 8) | c;
174 w &= MBFL_WCSGROUP_MASK;
175 w |= MBFL_WCSGROUP_THROUGH;
176 CK((*filter->output_function)(w, filter->data));
177 }
178 break;
179
180 case 2: /* got 0x8e, X0201 kana */
181 filter->status = 0;
182 if (c > 0xa0 && c < 0xe0) {
183 w = 0xfec0 + c;
184 CK((*filter->output_function)(w, filter->data));
185 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
186 CK((*filter->output_function)(c, filter->data));
187 } else {
188 w = 0x8e00 | c;
189 w &= MBFL_WCSGROUP_MASK;
190 w |= MBFL_WCSGROUP_THROUGH;
191 CK((*filter->output_function)(w, filter->data));
192 }
193 break;
194
195 default:
196 filter->status = 0;
197 break;
198 }
199
200 return c;
201 }
202
203 /*
204 * wchar => cp51932
205 */
206 int
mbfl_filt_conv_wchar_cp51932(int c,mbfl_convert_filter * filter)207 mbfl_filt_conv_wchar_cp51932(int c, mbfl_convert_filter *filter)
208 {
209 int c1, c2, s1;
210
211 s1 = 0;
212 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
213 s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
214 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
215 s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
216 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
217 s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
218 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
219 s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
220 }
221 if (s1 >= 0x8080) s1 = -1; /* we don't support JIS X0213 */
222 if (s1 <= 0) {
223 c1 = c & ~MBFL_WCSPLANE_MASK;
224 if (c1 == MBFL_WCSPLANE_WINCP932) {
225 s1 = c & MBFL_WCSPLANE_MASK;
226 if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 120ku */
227 s1 = -1;
228 }
229 } else if (c1 == MBFL_WCSPLANE_JIS0208) {
230 s1 = c & MBFL_WCSPLANE_MASK;
231 if ((s1 >= ((85 + 0x20) << 8) && /* 85ku - 94ku */
232 s1 <= ((88 + 0x20) << 8)) ||/* IBM extension */
233 (s1 >= ((93 + 0x20) << 8) && /* 89ku - 92ku */
234 s1 <= ((94 + 0x20) << 8))) {
235 s1 = -1;
236 }
237 } else if (c == 0xa5) { /* YEN SIGN */
238 s1 = 0x005c; /* YEN SIGN */
239 } else if (c == 0x203e) { /* OVER LINE */
240 s1 = 0x007e; /* FULLWIDTH MACRON */
241 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
242 s1 = 0x2140;
243 } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
244 s1 = 0x2141;
245 } else if (c == 0x2225) { /* PARALLEL TO */
246 s1 = 0x2142;
247 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
248 s1 = 0x215d;
249 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
250 s1 = 0x2171;
251 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
252 s1 = 0x2172;
253 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
254 s1 = 0x224c;
255 } else {
256 s1 = -1;
257 c1 = 0;
258 c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
259 while (c1 < c2) { /* CP932 vendor ext1 (13ku) */
260 if (c == cp932ext1_ucs_table[c1]) {
261 s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
262 break;
263 }
264 c1++;
265 }
266 if (s1 < 0) {
267 c1 = 0;
268 c2 = cp932ext2_ucs_table_max - cp932ext2_ucs_table_min;
269 while (c1 < c2) { /* CP932 vendor ext3 (115ku - 119ku) */
270 if (c == cp932ext2_ucs_table[c1]) {
271 s1 = ((c1/94 + 0x79) << 8) +(c1%94 + 0x21);
272 break;
273 }
274 c1++;
275 }
276 }
277 }
278 if (c == 0) {
279 s1 = 0;
280 } else if (s1 <= 0) {
281 s1 = -1;
282 }
283 }
284
285 if (s1 >= 0) {
286 if (s1 < 0x80) { /* latin */
287 CK((*filter->output_function)(s1, filter->data));
288 } else if (s1 < 0x100) { /* kana */
289 CK((*filter->output_function)(0x8e, filter->data));
290 CK((*filter->output_function)(s1, filter->data));
291 } else if (s1 < 0x8080) { /* X 0208 */
292 CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
293 CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
294 } else {
295 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
296 CK(mbfl_filt_conv_illegal_output(c, filter));
297 }
298 }
299 } else {
300 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
301 CK(mbfl_filt_conv_illegal_output(c, filter));
302 }
303 }
304
305 return c;
306 }
307
mbfl_filt_ident_cp51932(int c,mbfl_identify_filter * filter)308 static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter)
309 {
310 switch (filter->status) {
311 case 0: /* latin */
312 if (c >= 0 && c < 0x80) { /* ok */
313 ;
314 } else if (c > 0xa0 && c < 0xff) { /* kanji first char */
315 filter->status = 1;
316 } else if (c == 0x8e) { /* kana first char */
317 filter->status = 2;
318 } else { /* bad */
319 filter->flag = 1;
320 }
321 break;
322
323 case 1: /* got first half */
324 if (c < 0xa1 || c > 0xfe) { /* bad */
325 filter->flag = 1;
326 }
327 filter->status = 0;
328 break;
329
330 case 2: /* got 0x8e */
331 if (c < 0xa1 || c > 0xdf) { /* bad */
332 filter->flag = 1;
333 }
334 filter->status = 0;
335 break;
336
337 default:
338 filter->status = 0;
339 break;
340 }
341
342 return c;
343 }
344
345
346