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 #include "mbfilter.h"
31 #include "mbfilter_jis.h"
32
33 #include "unicode_table_cp932_ext.h"
34 #include "unicode_table_jis.h"
35
36 static int mbfl_filt_conv_jis_wchar_flush(mbfl_convert_filter *filter);
37
38 const mbfl_encoding mbfl_encoding_jis = {
39 mbfl_no_encoding_jis,
40 "JIS",
41 "ISO-2022-JP",
42 NULL,
43 NULL,
44 MBFL_ENCTYPE_GL_UNSAFE,
45 &vtbl_jis_wchar,
46 &vtbl_wchar_jis
47 };
48
49 const mbfl_encoding mbfl_encoding_2022jp = {
50 mbfl_no_encoding_2022jp,
51 "ISO-2022-JP",
52 "ISO-2022-JP",
53 NULL,
54 NULL,
55 MBFL_ENCTYPE_GL_UNSAFE,
56 &vtbl_2022jp_wchar,
57 &vtbl_wchar_2022jp
58 };
59
60 const struct mbfl_convert_vtbl vtbl_jis_wchar = {
61 mbfl_no_encoding_jis,
62 mbfl_no_encoding_wchar,
63 mbfl_filt_conv_common_ctor,
64 NULL,
65 mbfl_filt_conv_jis_wchar,
66 mbfl_filt_conv_jis_wchar_flush,
67 NULL,
68 };
69
70 const struct mbfl_convert_vtbl vtbl_wchar_jis = {
71 mbfl_no_encoding_wchar,
72 mbfl_no_encoding_jis,
73 mbfl_filt_conv_common_ctor,
74 NULL,
75 mbfl_filt_conv_wchar_jis,
76 mbfl_filt_conv_any_jis_flush,
77 NULL,
78 };
79
80 const struct mbfl_convert_vtbl vtbl_2022jp_wchar = {
81 mbfl_no_encoding_2022jp,
82 mbfl_no_encoding_wchar,
83 mbfl_filt_conv_common_ctor,
84 NULL,
85 mbfl_filt_conv_jis_wchar,
86 mbfl_filt_conv_jis_wchar_flush,
87 NULL,
88 };
89
90 const struct mbfl_convert_vtbl vtbl_wchar_2022jp = {
91 mbfl_no_encoding_wchar,
92 mbfl_no_encoding_2022jp,
93 mbfl_filt_conv_common_ctor,
94 NULL,
95 mbfl_filt_conv_wchar_2022jp,
96 mbfl_filt_conv_any_jis_flush,
97 NULL,
98 };
99
100 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
101
102 /*
103 * JIS => wchar
104 */
105 int
mbfl_filt_conv_jis_wchar(int c,mbfl_convert_filter * filter)106 mbfl_filt_conv_jis_wchar(int c, mbfl_convert_filter *filter)
107 {
108 int c1, s, w;
109
110 retry:
111 switch (filter->status & 0xf) {
112 /* case 0x00: ASCII */
113 /* case 0x10: X 0201 latin */
114 /* case 0x20: X 0201 kana */
115 /* case 0x80: X 0208 */
116 /* case 0x90: X 0212 */
117 case 0:
118 if (c == 0x1b) {
119 filter->status += 2;
120 } else if (c == 0x0e) { /* "kana in" */
121 filter->status = 0x20;
122 } else if (c == 0x0f) { /* "kana out" */
123 filter->status = 0;
124 } else if (filter->status == 0x10 && c == 0x5c) { /* YEN SIGN */
125 CK((*filter->output_function)(0xa5, filter->data));
126 } else if (filter->status == 0x10 && c == 0x7e) { /* OVER LINE */
127 CK((*filter->output_function)(0x203e, filter->data));
128 } else if (filter->status == 0x20 && c > 0x20 && c < 0x60) { /* kana */
129 CK((*filter->output_function)(0xff40 + c, filter->data));
130 } else if ((filter->status == 0x80 || filter->status == 0x90) && c > 0x20 && c < 0x7f) { /* kanji first char */
131 filter->cache = c;
132 filter->status += 1;
133 } else if (c >= 0 && c < 0x80) { /* latin, CTLs */
134 CK((*filter->output_function)(c, filter->data));
135 } else if (c > 0xa0 && c < 0xe0) { /* GR kana */
136 CK((*filter->output_function)(0xfec0 + c, filter->data));
137 } else {
138 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
139 }
140 break;
141
142 /* case 0x81: X 0208 second char */
143 /* case 0x91: X 0212 second char */
144 case 1:
145 filter->status &= ~0xf;
146 c1 = filter->cache;
147 if (c > 0x20 && c < 0x7f) {
148 s = (c1 - 0x21)*94 + c - 0x21;
149 if (filter->status == 0x80) {
150 if (s >= 0 && s < jisx0208_ucs_table_size) {
151 w = jisx0208_ucs_table[s];
152 } else {
153 w = 0;
154 }
155
156 if (w <= 0) {
157 w = MBFL_BAD_INPUT;
158 }
159 } else {
160 if (s >= 0 && s < jisx0212_ucs_table_size) {
161 w = jisx0212_ucs_table[s];
162 } else {
163 w = 0;
164 }
165
166 if (w <= 0) {
167 w = MBFL_BAD_INPUT;
168 }
169 }
170 CK((*filter->output_function)(w, filter->data));
171 } else {
172 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
173 }
174 break;
175
176 /* ESC */
177 /* case 0x02: */
178 /* case 0x12: */
179 /* case 0x22: */
180 /* case 0x82: */
181 /* case 0x92: */
182 case 2:
183 if (c == 0x24) { /* '$' */
184 filter->status++;
185 } else if (c == 0x28) { /* '(' */
186 filter->status += 3;
187 } else {
188 filter->status &= ~0xf;
189 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
190 goto retry;
191 }
192 break;
193
194 /* ESC $ */
195 /* case 0x03: */
196 /* case 0x13: */
197 /* case 0x23: */
198 /* case 0x83: */
199 /* case 0x93: */
200 case 3:
201 if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
202 filter->status = 0x80;
203 } else if (c == 0x28) { /* '(' */
204 filter->status++;
205 } else {
206 filter->status &= ~0xf;
207 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
208 CK((*filter->output_function)(0x24, filter->data));
209 goto retry;
210 }
211 break;
212
213 /* ESC $ ( */
214 /* case 0x04: */
215 /* case 0x14: */
216 /* case 0x24: */
217 /* case 0x84: */
218 /* case 0x94: */
219 case 4:
220 if (c == 0x40 || c == 0x42) { /* '@' or 'B' */
221 filter->status = 0x80;
222 } else if (c == 0x44) { /* 'D' */
223 filter->status = 0x90;
224 } else {
225 filter->status &= ~0xf;
226 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
227 CK((*filter->output_function)(0x24, filter->data));
228 CK((*filter->output_function)(0x28, filter->data));
229 goto retry;
230 }
231 break;
232
233 /* ESC ( */
234 /* case 0x05: */
235 /* case 0x15: */
236 /* case 0x25: */
237 /* case 0x85: */
238 /* case 0x95: */
239 case 5:
240 if (c == 0x42 || c == 0x48) { /* 'B' or 'H' */
241 filter->status = 0;
242 } else if (c == 0x4a) { /* 'J' */
243 filter->status = 0x10;
244 } else if (c == 0x49) { /* 'I' */
245 filter->status = 0x20;
246 } else {
247 filter->status &= ~0xf;
248 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
249 CK((*filter->output_function)(0x28, filter->data));
250 goto retry;
251 }
252 break;
253
254 default:
255 filter->status = 0;
256 break;
257 }
258
259 return 0;
260 }
261
mbfl_filt_conv_jis_wchar_flush(mbfl_convert_filter * filter)262 static int mbfl_filt_conv_jis_wchar_flush(mbfl_convert_filter *filter)
263 {
264 if ((filter->status & 0xF) == 1) {
265 /* 2-byte (JIS X 0208 or 0212) character was truncated */
266 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
267 }
268 return 0;
269 }
270
271 /*
272 * wchar => JIS
273 */
274 int
mbfl_filt_conv_wchar_jis(int c,mbfl_convert_filter * filter)275 mbfl_filt_conv_wchar_jis(int c, mbfl_convert_filter *filter)
276 {
277 int s = 0;
278
279 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
280 s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
281 } else if (c == 0x203E) { /* OVERLINE */
282 s = 0x1007E; /* Convert to JISX 0201 OVERLINE */
283 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
284 s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
285 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
286 s = ucs_i_jis_table[c - ucs_i_jis_table_min];
287 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
288 s = ucs_r_jis_table[c - ucs_r_jis_table_min];
289 }
290 if (s <= 0) {
291 if (c == 0xa5) { /* YEN SIGN */
292 s = 0x1005c;
293 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
294 s = 0x2140;
295 } else if (c == 0x2225) { /* PARALLEL TO */
296 s = 0x2142;
297 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
298 s = 0x215d;
299 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
300 s = 0x2171;
301 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
302 s = 0x2172;
303 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
304 s = 0x224c;
305 }
306 if (c == 0) {
307 s = 0;
308 } else if (s <= 0) {
309 s = -1;
310 }
311 }
312 if (s >= 0) {
313 if (s < 0x80) { /* ASCII */
314 if ((filter->status & 0xff00) != 0) {
315 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
316 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
317 CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
318 }
319 filter->status = 0;
320 CK((*filter->output_function)(s, filter->data));
321 } else if (s < 0x100) { /* kana */
322 if ((filter->status & 0xff00) != 0x100) {
323 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
324 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
325 CK((*filter->output_function)(0x49, filter->data)); /* 'I' */
326 }
327 filter->status = 0x100;
328 CK((*filter->output_function)(s & 0x7f, filter->data));
329 } else if (s < 0x8080) { /* X 0208 */
330 if ((filter->status & 0xff00) != 0x200) {
331 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
332 CK((*filter->output_function)(0x24, filter->data)); /* '$' */
333 CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
334 }
335 filter->status = 0x200;
336 CK((*filter->output_function)((s >> 8) & 0x7f, filter->data));
337 CK((*filter->output_function)(s & 0x7f, filter->data));
338 } else if (s < 0x10000) { /* X 0212 */
339 if ((filter->status & 0xff00) != 0x300) {
340 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
341 CK((*filter->output_function)(0x24, filter->data)); /* '$' */
342 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
343 CK((*filter->output_function)(0x44, filter->data)); /* 'D' */
344 }
345 filter->status = 0x300;
346 CK((*filter->output_function)((s >> 8) & 0x7f, filter->data));
347 CK((*filter->output_function)(s & 0x7f, filter->data));
348 } else { /* X 0201 latin */
349 if ((filter->status & 0xff00) != 0x400) {
350 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
351 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
352 CK((*filter->output_function)(0x4a, filter->data)); /* 'J' */
353 }
354 filter->status = 0x400;
355 CK((*filter->output_function)(s & 0x7f, filter->data));
356 }
357 } else {
358 CK(mbfl_filt_conv_illegal_output(c, filter));
359 }
360
361 return 0;
362 }
363
364
365 /*
366 * wchar => ISO-2022-JP
367 */
368 int
mbfl_filt_conv_wchar_2022jp(int c,mbfl_convert_filter * filter)369 mbfl_filt_conv_wchar_2022jp(int c, mbfl_convert_filter *filter)
370 {
371 int s;
372
373 s = 0;
374 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
375 s = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
376 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
377 s = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
378 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
379 s = ucs_i_jis_table[c - ucs_i_jis_table_min];
380 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
381 s = ucs_r_jis_table[c - ucs_r_jis_table_min];
382 }
383 if (s <= 0) {
384 if (c == 0xa5) { /* YEN SIGN */
385 s = 0x1005c;
386 } else if (c == 0x203e) { /* OVER LINE */
387 s = 0x1007e;
388 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
389 s = 0x2140;
390 } else if (c == 0x2225) { /* PARALLEL TO */
391 s = 0x2142;
392 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
393 s = 0x215d;
394 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
395 s = 0x2171;
396 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
397 s = 0x2172;
398 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
399 s = 0x224c;
400 }
401 if (c == 0) {
402 s = 0;
403 } else if (s <= 0) {
404 s = -1;
405 }
406 } else if ((s >= 0x80 && s < 0x2121) || (s > 0x8080)) {
407 s = -1;
408 }
409 if (s >= 0) {
410 if (s < 0x80) { /* ASCII */
411 if ((filter->status & 0xff00) != 0) {
412 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
413 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
414 CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
415 }
416 filter->status = 0;
417 CK((*filter->output_function)(s, filter->data));
418 } else if (s < 0x10000) { /* X 0208 */
419 if ((filter->status & 0xff00) != 0x200) {
420 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
421 CK((*filter->output_function)(0x24, filter->data)); /* '$' */
422 CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
423 }
424 filter->status = 0x200;
425 CK((*filter->output_function)((s >> 8) & 0x7f, filter->data));
426 CK((*filter->output_function)(s & 0x7f, filter->data));
427 } else { /* X 0201 latin */
428 if ((filter->status & 0xff00) != 0x400) {
429 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
430 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
431 CK((*filter->output_function)(0x4a, filter->data)); /* 'J' */
432 }
433 filter->status = 0x400;
434 CK((*filter->output_function)(s & 0x7f, filter->data));
435 }
436 } else {
437 CK(mbfl_filt_conv_illegal_output(c, filter));
438 }
439
440 return 0;
441 }
442
443 int
mbfl_filt_conv_any_jis_flush(mbfl_convert_filter * filter)444 mbfl_filt_conv_any_jis_flush(mbfl_convert_filter *filter)
445 {
446 /* back to latin */
447 if ((filter->status & 0xff00) != 0) {
448 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
449 CK((*filter->output_function)(0x28, filter->data)); /* '(' */
450 CK((*filter->output_function)(0x42, filter->data)); /* 'B' */
451 }
452 filter->status &= 0xff;
453
454 if (filter->flush_function != NULL) {
455 return (*filter->flush_function)(filter->data);
456 }
457
458 return 0;
459 }
460