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_iso2022_jp_ms.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_2022jpms(int c, mbfl_identify_filter *filter);
42 :
43 : static const char *mbfl_encoding_2022jpms_aliases[] = {"ISO2022JPMS", NULL};
44 :
45 : const mbfl_encoding mbfl_encoding_2022jpms = {
46 : mbfl_no_encoding_2022jpms,
47 : "ISO-2022-JP-MS",
48 : "ISO-2022-JP",
49 : (const char *(*)[])&mbfl_encoding_2022jpms_aliases,
50 : NULL,
51 : MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
52 : };
53 :
54 : const struct mbfl_identify_vtbl vtbl_identify_2022jpms = {
55 : mbfl_no_encoding_2022jpms,
56 : mbfl_filt_ident_common_ctor,
57 : mbfl_filt_ident_common_dtor,
58 : mbfl_filt_ident_2022jpms
59 : };
60 :
61 : const struct mbfl_convert_vtbl vtbl_2022jpms_wchar = {
62 : mbfl_no_encoding_2022jpms,
63 : mbfl_no_encoding_wchar,
64 : mbfl_filt_conv_common_ctor,
65 : mbfl_filt_conv_common_dtor,
66 : mbfl_filt_conv_2022jpms_wchar,
67 : mbfl_filt_conv_common_flush
68 : };
69 :
70 : const struct mbfl_convert_vtbl vtbl_wchar_2022jpms = {
71 : mbfl_no_encoding_wchar,
72 : mbfl_no_encoding_2022jpms,
73 : mbfl_filt_conv_common_ctor,
74 : mbfl_filt_conv_common_dtor,
75 : mbfl_filt_conv_wchar_2022jpms,
76 : mbfl_filt_conv_any_2022jpms_flush
77 : };
78 :
79 : #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
80 :
81 : #define sjistoidx(c1, c2) \
82 : (((c1) > 0x9f) \
83 : ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
84 : : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
85 : #define idxtojis1(c) (((c) / 94) + 0x21)
86 : #define idxtojis2(c) (((c) % 94) + 0x21)
87 :
88 : /*
89 : * ISO-2022-JP-MS => wchar
90 : */
91 : int
92 : mbfl_filt_conv_2022jpms_wchar(int c, mbfl_convert_filter *filter)
93 0 : {
94 : int c1, s, w;
95 :
96 0 : retry:
97 0 : switch (filter->status & 0xf) {
98 : /* case 0x00: ASCII */
99 : /* case 0x10: X 0201 latin */
100 : /* case 0x20: X 0201 kana */
101 : /* case 0x80: X 0208 */
102 : /* case 0xa0: UDC */
103 : case 0:
104 0 : if (c == 0x1b) {
105 0 : filter->status += 2;
106 0 : } else if (filter->status == 0x20 && c > 0x20 && c < 0x60) { /* kana */
107 0 : CK((*filter->output_function)(0xff40 + c, filter->data));
108 0 : } else if ((filter->status == 0x80 || filter->status == 0xa0) && c > 0x20 && c < 0x80) { /* kanji first char */
109 0 : filter->cache = c;
110 0 : filter->status += 1;
111 0 : } else if (c >= 0 && c < 0x80) { /* latin, CTLs */
112 0 : CK((*filter->output_function)(c, filter->data));
113 0 : } else if (c > 0xa0 && c < 0xe0) { /* GR kana */
114 0 : CK((*filter->output_function)(0xfec0 + c, filter->data));
115 : } else {
116 0 : w = c & MBFL_WCSGROUP_MASK;
117 0 : w |= MBFL_WCSGROUP_THROUGH;
118 0 : CK((*filter->output_function)(w, filter->data));
119 : }
120 0 : break;
121 :
122 : /* case 0x81: X 0208 second char */
123 : /* case 0xa1: UDC second char */
124 : case 1:
125 0 : w = 0;
126 0 : filter->status &= ~0xf;
127 0 : c1 = filter->cache;
128 0 : if (c > 0x20 && c < 0x7f) {
129 0 : s = (c1 - 0x21)*94 + c - 0x21;
130 0 : if (filter->status == 0x80) {
131 0 : if (s <= 137) {
132 0 : if (s == 31) {
133 0 : w = 0xff3c; /* FULLWIDTH REVERSE SOLIDUS */
134 0 : } else if (s == 32) {
135 0 : w = 0xff5e; /* FULLWIDTH TILDE */
136 0 : } else if (s == 33) {
137 0 : w = 0x2225; /* PARALLEL TO */
138 0 : } else if (s == 60) {
139 0 : w = 0xff0d; /* FULLWIDTH HYPHEN-MINUS */
140 0 : } else if (s == 80) {
141 0 : w = 0xffe0; /* FULLWIDTH CENT SIGN */
142 0 : } else if (s == 81) {
143 0 : w = 0xffe1; /* FULLWIDTH POUND SIGN */
144 0 : } else if (s == 137) {
145 0 : w = 0xffe2; /* FULLWIDTH NOT SIGN */
146 : }
147 : }
148 0 : if (w == 0) {
149 0 : if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) { /* vendor ext1 (13ku) */
150 0 : w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
151 0 : } else if (s >= 0 && s < jisx0208_ucs_table_size) {
152 0 : w = jisx0208_ucs_table[s];
153 0 : } else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) { /* vendor ext2 (89ku - 92ku) */
154 0 : w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
155 : } else {
156 0 : w = 0;
157 : }
158 : }
159 0 : if (w <= 0) {
160 0 : w = (c1 << 8) | c;
161 0 : w &= MBFL_WCSPLANE_MASK;
162 0 : w |= MBFL_WCSPLANE_JIS0208;
163 : }
164 0 : CK((*filter->output_function)(w, filter->data));
165 : } else {
166 0 : if (c1 > 0x20 && c1 < 0x35) {
167 0 : w = 0xe000 + (c1 - 0x21)*94 + c - 0x21;
168 : }
169 0 : if (w <= 0) {
170 0 : w = (((c1 - 0x21) + 0x7f) << 8) | c;
171 0 : w &= MBFL_WCSPLANE_MASK;
172 0 : w |= MBFL_WCSPLANE_JIS0208;
173 : }
174 0 : CK((*filter->output_function)(w, filter->data));
175 : }
176 0 : } else if (c == 0x1b) {
177 0 : filter->status += 2;
178 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
179 0 : CK((*filter->output_function)(c, filter->data));
180 : } else {
181 0 : w = (c1 << 8) | c;
182 0 : w &= MBFL_WCSGROUP_MASK;
183 0 : w |= MBFL_WCSGROUP_THROUGH;
184 0 : CK((*filter->output_function)(w, filter->data));
185 : }
186 0 : break;
187 :
188 : /* ESC */
189 : /* case 0x02: */
190 : /* case 0x12: */
191 : /* case 0x22: */
192 : /* case 0x82: */
193 : /* case 0xa2: */
194 : case 2:
195 0 : if (c == 0x24) { /* '$' */
196 0 : filter->status++;
197 0 : } else if (c == 0x28) { /* '(' */
198 0 : filter->status += 3;
199 : } else {
200 0 : filter->status &= ~0xf;
201 0 : CK((*filter->output_function)(0x1b, filter->data));
202 0 : goto retry;
203 : }
204 0 : break;
205 :
206 : /* ESC $ */
207 : /* case 0x03: */
208 : /* case 0x13: */
209 : /* case 0x23: */
210 : /* case 0x83: */
211 : /* case 0xa3: */
212 : case 3:
213 0 : if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
214 0 : filter->status = 0x80;
215 0 : } else if (c == 0x28) { /* '(' */
216 0 : filter->status++;
217 : } else {
218 0 : filter->status &= ~0xf;
219 0 : CK((*filter->output_function)(0x1b, filter->data));
220 0 : CK((*filter->output_function)(0x24, filter->data));
221 0 : goto retry;
222 : }
223 0 : break;
224 :
225 : /* ESC $ ( */
226 : /* case 0x04: */
227 : /* case 0x14: */
228 : /* case 0x24: */
229 : /* case 0x84: */
230 : /* case 0xa4: */
231 : case 4:
232 0 : if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
233 0 : filter->status = 0x80;
234 0 : } else if (c == 0x3f) { /* '?' */
235 0 : filter->status = 0xa0;
236 : } else {
237 0 : filter->status &= ~0xf;
238 0 : CK((*filter->output_function)(0x1b, filter->data));
239 0 : CK((*filter->output_function)(0x24, filter->data));
240 0 : CK((*filter->output_function)(0x28, filter->data));
241 0 : goto retry;
242 : }
243 0 : break;
244 :
245 : /* ESC ( */
246 : /* case 0x05: */
247 : /* case 0x15: */
248 : /* case 0x25: */
249 : /* case 0x85: */
250 : /* case 0xa5: */
251 : case 5:
252 0 : if (c == 0x42) { /* 'B' */
253 0 : filter->status = 0;
254 0 : } else if (c == 0x4a) { /* 'J' */
255 0 : filter->status = 0;
256 0 : } else if (c == 0x49) { /* 'I' */
257 0 : filter->status = 0x20;
258 : } else {
259 0 : filter->status &= ~0xf;
260 0 : CK((*filter->output_function)(0x1b, filter->data));
261 0 : CK((*filter->output_function)(0x28, filter->data));
262 0 : goto retry;
263 : }
264 0 : break;
265 :
266 : default:
267 0 : filter->status = 0;
268 : break;
269 : }
270 :
271 0 : return c;
272 : }
273 :
274 : static int
275 : cp932ext3_cp932ext2_jis(int c)
276 0 : {
277 : int idx;
278 :
279 0 : idx = sjistoidx(0xfa, 0x40) + c;
280 0 : if (idx >= sjistoidx(0xfa, 0x5c))
281 0 : idx -= sjistoidx(0xfa, 0x5c) - sjistoidx(0xed, 0x40);
282 0 : else if (idx >= sjistoidx(0xfa, 0x55))
283 0 : idx -= sjistoidx(0xfa, 0x55) - sjistoidx(0xee, 0xfa);
284 0 : else if (idx >= sjistoidx(0xfa, 0x40))
285 0 : idx -= sjistoidx(0xfa, 0x40) - sjistoidx(0xee, 0xef);
286 0 : return idxtojis1(idx) << 8 | idxtojis2(idx);
287 : }
288 :
289 : /*
290 : * wchar => ISO-2022-JP-MS
291 : */
292 : int
293 : mbfl_filt_conv_wchar_2022jpms(int c, mbfl_convert_filter *filter)
294 0 : {
295 : int c1, c2, s1, s2;
296 :
297 0 : s1 = 0;
298 0 : s2 = 0;
299 0 : if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
300 0 : s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
301 0 : } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
302 0 : s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
303 0 : } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
304 0 : s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
305 0 : } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
306 0 : s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
307 0 : } else if (c >= 0xe000 && c < (0xe000 + 20*94)) { /* user (95ku - 114ku) */
308 0 : s1 = c - 0xe000;
309 0 : c1 = s1/94 + 0x7f;
310 0 : c2 = s1%94 + 0x21;
311 0 : s1 = (c1 << 8) | c2;
312 : }
313 0 : if (s1 <= 0) {
314 0 : c1 = c & ~MBFL_WCSPLANE_MASK;
315 0 : if (c1 == MBFL_WCSPLANE_WINCP932) {
316 0 : s1 = c & MBFL_WCSPLANE_MASK;
317 0 : s2 = 1;
318 0 : } else if (c1 == MBFL_WCSPLANE_JIS0208) {
319 0 : s1 = c & MBFL_WCSPLANE_MASK;
320 0 : } else if (c1 == MBFL_WCSPLANE_JIS0212) {
321 0 : s1 = c & MBFL_WCSPLANE_MASK;
322 0 : s1 |= 0x8080;
323 0 : } else if (c == 0xa5) { /* YEN SIGN */
324 0 : s1 = 0x216f; /* FULLWIDTH YEN SIGN */
325 0 : } else if (c == 0x203e) { /* OVER LINE */
326 0 : s1 = 0x2131; /* FULLWIDTH MACRON */
327 0 : } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
328 0 : s1 = 0x2140;
329 0 : } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
330 0 : s1 = 0x2141;
331 0 : } else if (c == 0x2225) { /* PARALLEL TO */
332 0 : s1 = 0x2142;
333 0 : } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
334 0 : s1 = 0x215d;
335 0 : } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
336 0 : s1 = 0x2171;
337 0 : } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
338 0 : s1 = 0x2172;
339 0 : } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
340 0 : s1 = 0x224c;
341 : }
342 : }
343 0 : if ((s1 <= 0) || (s1 >= 0xa1a1 && s2 == 0)) { /* not found or X 0212 */
344 0 : s1 = -1;
345 0 : c1 = 0;
346 0 : c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
347 0 : while (c1 < c2) { /* CP932 vendor ext1 (13ku) */
348 0 : if (c == cp932ext1_ucs_table[c1]) {
349 0 : s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
350 0 : break;
351 : }
352 0 : c1++;
353 : }
354 0 : if (s1 <= 0) {
355 0 : c1 = 0;
356 0 : c2 = cp932ext3_ucs_table_max - cp932ext3_ucs_table_min;
357 0 : while (c1 < c2) { /* CP932 vendor ext3 (115ku - 119ku) */
358 0 : if (c == cp932ext3_ucs_table[c1]) {
359 0 : s1 = cp932ext3_cp932ext2_jis(c1);
360 0 : break;
361 : }
362 0 : c1++;
363 : }
364 : }
365 0 : if (c == 0) {
366 0 : s1 = 0;
367 0 : } else if (s1 <= 0) {
368 0 : s1 = -1;
369 : }
370 : }
371 0 : if (s1 >= 0) {
372 0 : if (s1 < 0x80) { /* latin */
373 0 : if ((filter->status & 0xff00) != 0) {
374 0 : CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
375 0 : CK((*filter->output_function)(0x28, filter->data)); /* '(' */
376 0 : CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
377 : }
378 0 : CK((*filter->output_function)(s1, filter->data));
379 0 : filter->status = 0;
380 0 : } else if (s1 > 0xa0 && s1 < 0xe0) { /* kana */
381 0 : if ((filter->status & 0xff00) != 0x100) {
382 0 : CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
383 0 : CK((*filter->output_function)(0x28, filter->data)); /* '(' */
384 0 : CK((*filter->output_function)(0x49, filter->data)); /* 'I' */
385 : }
386 0 : filter->status = 0x100;
387 0 : CK((*filter->output_function)(s1 & 0x7f, filter->data));
388 0 : } else if (s1 < 0x7e7f) { /* X 0208 */
389 0 : if ((filter->status & 0xff00) != 0x200) {
390 0 : CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
391 0 : CK((*filter->output_function)(0x24, filter->data)); /* '$' */
392 0 : CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
393 : }
394 0 : filter->status = 0x200;
395 0 : CK((*filter->output_function)((s1 >> 8) & 0xff, filter->data));
396 0 : CK((*filter->output_function)(s1 & 0x7f, filter->data));
397 0 : } else if (s1 < 0x927f) { /* UDC */
398 0 : if ((filter->status & 0xff00) != 0x800) {
399 0 : CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
400 0 : CK((*filter->output_function)(0x24, filter->data)); /* '$' */
401 0 : CK((*filter->output_function)(0x28, filter->data)); /* '(' */
402 0 : CK((*filter->output_function)(0x3f, filter->data)); /* '?' */
403 : }
404 0 : filter->status = 0x800;
405 0 : CK((*filter->output_function)(((s1 >> 8) - 0x5e) & 0x7f, filter->data));
406 0 : CK((*filter->output_function)(s1 & 0x7f, filter->data));
407 : }
408 : } else {
409 0 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
410 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
411 : }
412 : }
413 :
414 0 : return c;
415 : }
416 :
417 : int
418 : mbfl_filt_conv_any_2022jpms_flush(mbfl_convert_filter *filter)
419 0 : {
420 : /* back to latin */
421 0 : if ((filter->status & 0xff00) != 0) {
422 0 : CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
423 0 : CK((*filter->output_function)(0x28, filter->data)); /* '(' */
424 0 : CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
425 : }
426 0 : filter->status &= 0xff;
427 0 : return 0;
428 : }
429 :
430 : static int mbfl_filt_ident_2022jpms(int c, mbfl_identify_filter *filter)
431 0 : {
432 0 : retry:
433 0 : switch (filter->status & 0xf) {
434 : /* case 0x00: ASCII */
435 : /* case 0x10: X 0201 latin */
436 : /* case 0x20: X 0201 kana */
437 : /* case 0x80: X 0208 */
438 : /* case 0xa0: X UDC */
439 : case 0:
440 0 : if (c == 0x1b) {
441 0 : filter->status += 2;
442 0 : } else if ((filter->status == 0x80 || filter->status == 0xa0) && c > 0x20 && c < 0x80) { /* kanji first char */
443 0 : filter->status += 1;
444 0 : } else if (c >= 0 && c < 0x80) { /* latin, CTLs */
445 : ;
446 : } else {
447 0 : filter->flag = 1; /* bad */
448 : }
449 0 : break;
450 :
451 : /* case 0x81: X 0208 second char */
452 : /* case 0xa1: UDC second char */
453 : case 1:
454 0 : filter->status &= ~0xf;
455 0 : if (c == 0x1b) {
456 0 : goto retry;
457 0 : } else if (c < 0x21 || c > 0x7e) { /* bad */
458 0 : filter->flag = 1;
459 : }
460 0 : break;
461 :
462 : /* ESC */
463 : case 2:
464 0 : if (c == 0x24) { /* '$' */
465 0 : filter->status++;
466 0 : } else if (c == 0x28) { /* '(' */
467 0 : filter->status += 3;
468 : } else {
469 0 : filter->flag = 1; /* bad */
470 0 : filter->status &= ~0xf;
471 0 : goto retry;
472 : }
473 0 : break;
474 :
475 : /* ESC $ */
476 : case 3:
477 0 : if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
478 0 : filter->status = 0x80;
479 0 : } else if (c == 0x28) { /* '(' */
480 0 : filter->status++;
481 : } else {
482 0 : filter->flag = 1; /* bad */
483 0 : filter->status &= ~0xf;
484 0 : goto retry;
485 : }
486 0 : break;
487 :
488 : /* ESC $ ( */
489 : case 4:
490 0 : if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
491 0 : filter->status = 0x80;
492 0 : } else if (c == 0x3f) { /* '?' */
493 0 : filter->status = 0xa0;
494 : } else {
495 0 : filter->flag = 1; /* bad */
496 0 : filter->status &= ~0xf;
497 0 : goto retry;
498 : }
499 0 : break;
500 :
501 : /* ESC ( */
502 : case 5:
503 0 : if (c == 0x42) { /* 'B' */
504 0 : filter->status = 0;
505 0 : } else if (c == 0x4a) { /* 'J' */
506 0 : filter->status = 0;
507 0 : } else if (c == 0x49) { /* 'I' */
508 0 : filter->status = 0x20;
509 : } else {
510 0 : filter->flag = 1; /* bad */
511 0 : filter->status &= ~0xf;
512 0 : goto retry;
513 : }
514 0 : break;
515 :
516 : default:
517 0 : filter->status = 0;
518 : break;
519 : }
520 :
521 0 : return c;
522 : }
|