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_euc_jp_win.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_eucjp_win(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, 3,
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_eucjp_win_aliases[] = {"eucJP-open",
64 "eucJP-ms", NULL};
65
66 const struct mbfl_identify_vtbl vtbl_identify_eucjpwin = {
67 mbfl_no_encoding_eucjp_win,
68 mbfl_filt_ident_common_ctor,
69 mbfl_filt_ident_common_dtor,
70 mbfl_filt_ident_eucjp_win
71 };
72
73 const mbfl_encoding mbfl_encoding_eucjp_win = {
74 mbfl_no_encoding_eucjp_win,
75 "eucJP-win",
76 "EUC-JP",
77 (const char *(*)[])&mbfl_encoding_eucjp_win_aliases,
78 mblen_table_eucjp,
79 MBFL_ENCTYPE_MBCS,
80 &vtbl_eucjpwin_wchar,
81 &vtbl_wchar_eucjpwin
82 };
83
84 const struct mbfl_convert_vtbl vtbl_eucjpwin_wchar = {
85 mbfl_no_encoding_eucjp_win,
86 mbfl_no_encoding_wchar,
87 mbfl_filt_conv_common_ctor,
88 mbfl_filt_conv_common_dtor,
89 mbfl_filt_conv_eucjpwin_wchar,
90 mbfl_filt_conv_common_flush
91 };
92
93 const struct mbfl_convert_vtbl vtbl_wchar_eucjpwin = {
94 mbfl_no_encoding_wchar,
95 mbfl_no_encoding_eucjp_win,
96 mbfl_filt_conv_common_ctor,
97 mbfl_filt_conv_common_dtor,
98 mbfl_filt_conv_wchar_eucjpwin,
99 mbfl_filt_conv_common_flush
100 };
101
102 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
103
104 /*
105 * eucJP-win => wchar
106 */
107 int
mbfl_filt_conv_eucjpwin_wchar(int c,mbfl_convert_filter * filter)108 mbfl_filt_conv_eucjpwin_wchar(int c, mbfl_convert_filter *filter)
109 {
110 int c1, s, w, n;
111
112 switch (filter->status) {
113 case 0:
114 if (c >= 0 && c < 0x80) { /* latin */
115 CK((*filter->output_function)(c, filter->data));
116 } else if (c > 0xa0 && c < 0xff) { /* CP932 first char */
117 filter->status = 1;
118 filter->cache = c;
119 } else if (c == 0x8e) { /* kana first char */
120 filter->status = 2;
121 } else if (c == 0x8f) { /* X 0212 first char */
122 filter->status = 3;
123 } else {
124 w = c & MBFL_WCSGROUP_MASK;
125 w |= MBFL_WCSGROUP_THROUGH;
126 CK((*filter->output_function)(w, filter->data));
127 }
128 break;
129
130 case 1: /* got first half */
131 filter->status = 0;
132 c1 = filter->cache;
133 if (c > 0xa0 && c < 0xff) {
134 w = 0;
135 s = (c1 - 0xa1)*94 + c - 0xa1;
136 if (s <= 137) {
137 if (s == 31) {
138 w = 0xff3c; /* FULLWIDTH REVERSE SOLIDUS */
139 } else if (s == 32) {
140 w = 0xff5e; /* FULLWIDTH TILDE */
141 } else if (s == 33) {
142 w = 0x2225; /* PARALLEL TO */
143 } else if (s == 60) {
144 w = 0xff0d; /* FULLWIDTH HYPHEN-MINUS */
145 } else if (s == 80) {
146 w = 0xffe0; /* FULLWIDTH CENT SIGN */
147 } else if (s == 81) {
148 w = 0xffe1; /* FULLWIDTH POUND SIGN */
149 } else if (s == 137) {
150 w = 0xffe2; /* FULLWIDTH NOT SIGN */
151 }
152 }
153 if (w == 0) {
154 if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) { /* vendor ext1 (13ku) */
155 w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
156 } else if (s >= 0 && s < jisx0208_ucs_table_size) { /* X 0208 */
157 w = jisx0208_ucs_table[s];
158 } else if (s >= (84 * 94)) { /* user (85ku - 94ku) */
159 w = s - (84 * 94) + 0xe000;
160 }
161 }
162 if (w <= 0) {
163 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
164 w &= MBFL_WCSPLANE_MASK;
165 w |= MBFL_WCSPLANE_WINCP932;
166 }
167 CK((*filter->output_function)(w, filter->data));
168 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
169 CK((*filter->output_function)(c, filter->data));
170 } else {
171 w = (c1 << 8) | c;
172 w &= MBFL_WCSGROUP_MASK;
173 w |= MBFL_WCSGROUP_THROUGH;
174 CK((*filter->output_function)(w, filter->data));
175 }
176 break;
177
178 case 2: /* got 0x8e, X0201 kana */
179 filter->status = 0;
180 if (c > 0xa0 && c < 0xe0) {
181 w = 0xfec0 + c;
182 CK((*filter->output_function)(w, filter->data));
183 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
184 CK((*filter->output_function)(c, filter->data));
185 } else {
186 w = 0x8e00 | c;
187 w &= MBFL_WCSGROUP_MASK;
188 w |= MBFL_WCSGROUP_THROUGH;
189 CK((*filter->output_function)(w, filter->data));
190 }
191 break;
192
193 case 3: /* got 0x8f, X 0212 first char */
194 if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
195 CK((*filter->output_function)(c, filter->data));
196 filter->status = 0;
197 } else {
198 filter->status++;
199 filter->cache = c;
200 }
201 break;
202 case 4: /* got 0x8f, X 0212 second char */
203 filter->status = 0;
204 c1 = filter->cache;
205 if (c1 > 0xa0 && c1 < 0xff && c > 0xa0 && c < 0xff) {
206 s = (c1 - 0xa1)*94 + c - 0xa1;
207 if (s >= 0 && s < jisx0212_ucs_table_size) {
208 w = jisx0212_ucs_table[s];
209 if (w == 0x007e) {
210 w = 0xff5e; /* FULLWIDTH TILDE */
211 }
212 } else if (s >= (82*94) && s < (84*94)) { /* vender ext3 (83ku - 84ku) <-> CP932 (115ku -120ku) */
213 s = (c1<< 8) | c;
214 w = 0;
215 n = 0;
216 while (n < cp932ext3_eucjp_table_size) {
217 if (s == cp932ext3_eucjp_table[n]) {
218 if (n < (cp932ext3_ucs_table_max - cp932ext3_ucs_table_min)) {
219 w = cp932ext3_ucs_table[n];
220 }
221 break;
222 }
223 n++;
224 }
225 } else if (s >= (84*94)) { /* user (85ku - 94ku) */
226 w = s - (84*94) + (0xe000 + (94*10));
227 } else {
228 w = 0;
229 }
230 if (w == 0x00A6) {
231 w = 0xFFE4; /* FULLWIDTH BROKEN BAR */
232 }
233 if (w <= 0) {
234 w = ((c1 & 0x7f) << 8) | (c & 0x7f);
235 w &= MBFL_WCSPLANE_MASK;
236 w |= MBFL_WCSPLANE_JIS0212;
237 }
238 CK((*filter->output_function)(w, filter->data));
239 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
240 CK((*filter->output_function)(c, filter->data));
241 } else {
242 w = (c1 << 8) | c | 0x8f0000;
243 w &= MBFL_WCSGROUP_MASK;
244 w |= MBFL_WCSGROUP_THROUGH;
245 CK((*filter->output_function)(w, filter->data));
246 }
247 break;
248
249 default:
250 filter->status = 0;
251 break;
252 }
253
254 return c;
255 }
256
257 /*
258 * wchar => eucJP-win
259 */
260 int
mbfl_filt_conv_wchar_eucjpwin(int c,mbfl_convert_filter * filter)261 mbfl_filt_conv_wchar_eucjpwin(int c, mbfl_convert_filter *filter)
262 {
263 int c1, c2, s1;
264
265 s1 = 0;
266 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
267 s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
268 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
269 s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
270 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
271 s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
272 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
273 s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
274 } else if (c >= 0xe000 && c < (0xe000 + 10*94)) { /* user (X0208 85ku - 94ku) */
275 s1 = c - 0xe000;
276 c1 = s1/94 + 0x75;
277 c2 = s1%94 + 0x21;
278 s1 = (c1 << 8) | c2;
279 } else if (c >= (0xe000 + 10*94) && c < (0xe000 + 20*94)) { /* user (X0212 85ku - 94ku) */
280 s1 = c - (0xe000 + 10*94);
281 c1 = s1/94 + 0xf5;
282 c2 = s1%94 + 0xa1;
283 s1 = (c1 << 8) | c2;
284 }
285 if (s1 == 0xa2f1) {
286 s1 = 0x2d62; /* NUMERO SIGN */
287 }
288 if (s1 <= 0) {
289 c1 = c & ~MBFL_WCSPLANE_MASK;
290 if (c1 == MBFL_WCSPLANE_WINCP932) {
291 s1 = c & MBFL_WCSPLANE_MASK;
292 if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 120ku */
293 s1 = -1;
294 }
295 } else if (c1 == MBFL_WCSPLANE_JIS0208) {
296 s1 = c & MBFL_WCSPLANE_MASK;
297 if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 94ku */
298 s1 = -1;
299 }
300 } else if (c1 == MBFL_WCSPLANE_JIS0212) {
301 s1 = c & MBFL_WCSPLANE_MASK;
302 if (s1 >= ((83 + 0x20) << 8)) { /* 83ku - 94ku */
303 s1 = -1;
304 } else {
305 s1 |= 0x8080;
306 }
307 } else if (c == 0xa5) { /* YEN SIGN */
308 s1 = 0x216f; /* FULLWIDTH YEN SIGN */
309 } else if (c == 0x203e) { /* OVER LINE */
310 s1 = 0x2131; /* FULLWIDTH MACRON */
311 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
312 s1 = 0x2140;
313 } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
314 s1 = 0x2141;
315 } else if (c == 0x2225) { /* PARALLEL TO */
316 s1 = 0x2142;
317 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
318 s1 = 0x215d;
319 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
320 s1 = 0x2171;
321 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
322 s1 = 0x2172;
323 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
324 s1 = 0x224c;
325 } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
326 s1 = 0x2141;
327 } else {
328 s1 = -1;
329 c1 = 0;
330 c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
331 while (c1 < c2) { /* CP932 vendor ext1 (13ku) */
332 const int oh = cp932ext1_ucs_table_min / 94;
333
334 if (c == cp932ext1_ucs_table[c1]) {
335 s1 = ((c1 / 94 + oh + 0x21) << 8) + (c1 % 94 + 0x21);
336 break;
337 }
338 c1++;
339 }
340 if (s1 < 0) {
341 c1 = 0;
342 c2 = cp932ext3_ucs_table_max - cp932ext3_ucs_table_min;
343 while (c1 < c2) { /* CP932 vendor ext3 (115ku - 119ku) */
344 if (c == cp932ext3_ucs_table[c1]) {
345 if (c1 < cp932ext3_eucjp_table_size) {
346 s1 = cp932ext3_eucjp_table[c1];
347 }
348 break;
349 }
350 c1++;
351 }
352 }
353 }
354 if (c == 0) {
355 s1 = 0;
356 } else if (s1 <= 0) {
357 s1 = -1;
358 }
359 }
360
361 if (s1 >= 0) {
362 if (s1 < 0x80) { /* latin */
363 CK((*filter->output_function)(s1, filter->data));
364 } else if (s1 < 0x100) { /* kana */
365 CK((*filter->output_function)(0x8e, filter->data));
366 CK((*filter->output_function)(s1, filter->data));
367 } else if (s1 < 0x8080) { /* X 0208 */
368 CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
369 CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
370 } else { /* X 0212 */
371 CK((*filter->output_function)(0x8f, filter->data));
372 CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
373 CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
374 }
375 } else {
376 CK(mbfl_filt_conv_illegal_output(c, filter));
377 }
378
379 return c;
380 }
381
mbfl_filt_ident_eucjp_win(int c,mbfl_identify_filter * filter)382 static int mbfl_filt_ident_eucjp_win(int c, mbfl_identify_filter *filter)
383 {
384 switch (filter->status) {
385 case 0: /* latin */
386 if (c >= 0 && c < 0x80) { /* ok */
387 ;
388 } else if (c > 0xa0 && c < 0xff) { /* kanji first char */
389 filter->status = 1;
390 } else if (c == 0x8e) { /* kana first char */
391 filter->status = 2;
392 } else if (c == 0x8f) { /* X 0212 first char */
393 filter->status = 3;
394 } else { /* bad */
395 filter->flag = 1;
396 }
397 break;
398
399 case 1: /* got first half */
400 if (c < 0xa1 || c > 0xfe) { /* bad */
401 filter->flag = 1;
402 }
403 filter->status = 0;
404 break;
405
406 case 2: /* got 0x8e */
407 if (c < 0xa1 || c > 0xdf) { /* bad */
408 filter->flag = 1;
409 }
410 filter->status = 0;
411 break;
412
413 case 3: /* got 0x8f */
414 if (c < 0xa1 || c > 0xfe) { /* bad */
415 filter->flag = 1;
416 }
417 filter->status++;
418 break;
419 case 4: /* got 0x8f */
420 if (c < 0xa1 || c > 0xfe) { /* bad */
421 filter->flag = 1;
422 }
423 filter->status = 0;
424 break;
425
426 default:
427 filter->status = 0;
428 break;
429 }
430
431 return c;
432 }
433