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_sjis.c
26 * by rui hirokawa <hirokawa@php.net> on 15 aug 2011.
27 */
28
29 /* Although the specification for Shift-JIS-2004 indicates that 0x5C and
30 * 0x7E should (respectively) represent a Yen sign and an overbar, feedback
31 * from Japanese PHP users indicates that they prefer 0x5C and 0x7E to be
32 * treated as equivalent to U+005C and U+007E. This is the historical
33 * behavior of mbstring, and promotes compatibility with other software
34 * which handles Shift-JIS and Shift-JIS-2004 text in this way. */
35
36 #include "mbfilter.h"
37 #include "mbfilter_sjis_2004.h"
38
39 #include "unicode_table_jis2004.h"
40 #include "unicode_table_jis.h"
41
42 extern const unsigned char mblen_table_sjis_mobile[];
43
44 extern int mbfl_bisec_srch(int w, const unsigned short *tbl, int n);
45 extern int mbfl_bisec_srch2(int w, const unsigned short tbl[], int n);
46
47 static const char *mbfl_encoding_sjis2004_aliases[] = {"SJIS2004","Shift_JIS-2004", NULL};
48
49 const mbfl_encoding mbfl_encoding_sjis2004 = {
50 mbfl_no_encoding_sjis2004,
51 "SJIS-2004",
52 "Shift_JIS",
53 mbfl_encoding_sjis2004_aliases,
54 mblen_table_sjis_mobile, /* Leading byte values used for SJIS-2004 are the same as mobile SJIS variants */
55 MBFL_ENCTYPE_GL_UNSAFE,
56 &vtbl_sjis2004_wchar,
57 &vtbl_wchar_sjis2004,
58 NULL
59 };
60
61 const struct mbfl_convert_vtbl vtbl_sjis2004_wchar = {
62 mbfl_no_encoding_sjis2004,
63 mbfl_no_encoding_wchar,
64 mbfl_filt_conv_common_ctor,
65 NULL,
66 mbfl_filt_conv_jis2004_wchar,
67 mbfl_filt_conv_jis2004_wchar_flush,
68 NULL,
69 };
70
71 const struct mbfl_convert_vtbl vtbl_wchar_sjis2004 = {
72 mbfl_no_encoding_wchar,
73 mbfl_no_encoding_sjis2004,
74 mbfl_filt_conv_common_ctor,
75 NULL,
76 mbfl_filt_conv_wchar_jis2004,
77 mbfl_filt_conv_wchar_jis2004_flush,
78 NULL,
79 };
80
81 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
82
83 #define SJIS_ENCODE(c1,c2,s1,s2) \
84 do { \
85 s1 = c1; \
86 s1--; \
87 s1 >>= 1; \
88 if ((c1) < 0x5f) { \
89 s1 += 0x71; \
90 } else { \
91 s1 += 0xb1; \
92 } \
93 s2 = c2; \
94 if ((c1) & 1) { \
95 if ((c2) < 0x60) { \
96 s2--; \
97 } \
98 s2 += 0x20; \
99 } else { \
100 s2 += 0x7e; \
101 } \
102 } while (0)
103
104 #define SJIS_DECODE(c1,c2,s1,s2) \
105 do { \
106 s1 = c1; \
107 if (s1 < 0xa0) { \
108 s1 -= 0x81; \
109 } else { \
110 s1 -= 0xc1; \
111 } \
112 s1 <<= 1; \
113 s1 += 0x21; \
114 s2 = c2; \
115 if (s2 < 0x9f) { \
116 if (s2 < 0x7f) { \
117 s2++; \
118 } \
119 s2 -= 0x20; \
120 } else { \
121 s1++; \
122 s2 -= 0x7e; \
123 } \
124 } while (0)
125
mbfl_filt_conv_jis2004_wchar(int c,mbfl_convert_filter * filter)126 int mbfl_filt_conv_jis2004_wchar(int c, mbfl_convert_filter *filter)
127 {
128 int k;
129 int c1, c2, s, s1 = 0, s2 = 0, w = 0, w1;
130
131 switch (filter->status & 0xf) {
132 case 0:
133 if (c >= 0 && c < 0x80) { /* latin */
134 if (filter->from->no_encoding == mbfl_no_encoding_eucjp2004) {
135 CK((*filter->output_function)(c, filter->data));
136 } else if (filter->from->no_encoding == mbfl_no_encoding_sjis2004) {
137 if (c == 0x5c) {
138 CK((*filter->output_function)(0x00a5, filter->data));
139 } else if (c == 0x7e) {
140 CK((*filter->output_function)(0x203e, filter->data));
141 } else {
142 CK((*filter->output_function)(c, filter->data));
143 }
144 } else { /* ISO-2022-JP-2004 */
145 if (c == 0x1b) {
146 filter->status += 6;
147 } else if ((filter->status == 0x80 || filter->status == 0x90 || filter->status == 0xa0)
148 && c > 0x20 && c < 0x7f) { /* kanji first char */
149 filter->cache = c;
150 if (filter->status == 0x90) {
151 filter->status += 1; /* JIS X 0213 plane 1 */
152 } else if (filter->status == 0xa0) {
153 filter->status += 4; /* JIS X 0213 plane 2 */
154 } else {
155 filter->status += 5; /* JIS X 0208 */
156 }
157 } else {
158 CK((*filter->output_function)(c, filter->data));
159 }
160 }
161 } else {
162 if (filter->from->no_encoding == mbfl_no_encoding_eucjp2004) {
163 if (c > 0xa0 && c < 0xff) { /* X 0213 plane 1 first char */
164 filter->status = 1;
165 filter->cache = c;
166 } else if (c == 0x8e) { /* kana first char */
167 filter->cache = 0x8E; /* So error will be reported if input is truncated right here */
168 filter->status = 2;
169 } else if (c == 0x8f) { /* X 0213 plane 2 first char */
170 filter->status = 3;
171 } else {
172 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
173 }
174 } else if (filter->from->no_encoding == mbfl_no_encoding_sjis2004) {
175 if (c > 0xa0 && c < 0xe0) { /* kana */
176 CK((*filter->output_function)(0xfec0 + c, filter->data));
177 } else if (c > 0x80 && c < 0xfd && c != 0xa0) { /* kanji first char */
178 filter->status = 1;
179 filter->cache = c;
180 } else {
181 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
182 }
183 } else {
184 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
185 }
186 }
187 break;
188
189 case 1: /* kanji second char */
190 filter->status &= ~0xf;
191 c1 = filter->cache;
192
193 if (filter->from->no_encoding == mbfl_no_encoding_eucjp2004) {
194 if (c > 0xa0 && c < 0xff) {
195 s1 = c1 - 0x80;
196 s2 = c - 0x80;
197 } else {
198 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
199 break;
200 }
201 } else if (filter->from->no_encoding == mbfl_no_encoding_sjis2004) {
202 if (c >= 0x40 && c <= 0xfc && c != 0x7f) {
203 SJIS_DECODE(c1, c, s1, s2);
204 } else {
205 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
206 break;
207 }
208 } else { /* ISO-2022-JP-2004 */
209 if (c >= 0x21 && c <= 0x7E) {
210 s1 = c1;
211 s2 = c;
212 } else {
213 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
214 break;
215 }
216 }
217 w1 = (s1 << 8) | s2;
218
219 if (w1 >= 0x2121) {
220 /* conversion for combining characters */
221 if ((w1 >= 0x2477 && w1 <= 0x2479) || (w1 >= 0x2479 && w1 <= 0x247B) ||
222 (w1 >= 0x2577 && w1 <= 0x257E) || w1 == 0x2678 || w1 == 0x2B44 ||
223 (w1 >= 0x2B48 && w1 <= 0x2B4F) || (w1 >= 0x2B65 && w1 <= 0x2B66)) {
224 k = mbfl_bisec_srch2(w1, jisx0213_u2_key, jisx0213_u2_tbl_len);
225 if (k >= 0) {
226 w = jisx0213_u2_tbl[2*k];
227 CK((*filter->output_function)(w, filter->data));
228 w = jisx0213_u2_tbl[2*k+1];
229 }
230 }
231
232 /* conversion for BMP */
233 if (w <= 0) {
234 w1 = (s1 - 0x21)*94 + s2 - 0x21;
235 if (w1 >= 0 && w1 < jisx0213_ucs_table_size) {
236 w = jisx0213_ucs_table[w1];
237 }
238 }
239
240 /* conversion for CJK Unified Ideographs ext.B (U+2XXXX) */
241 if (w <= 0) {
242 w1 = (s1 << 8) | s2;
243 k = mbfl_bisec_srch2(w1, jisx0213_jis_u5_key, jisx0213_u5_tbl_len);
244 if (k >= 0) {
245 w = jisx0213_jis_u5_tbl[k] + 0x20000;
246 }
247 }
248
249 if (w <= 0) {
250 w = MBFL_BAD_INPUT;
251 }
252 CK((*filter->output_function)(w, filter->data));
253 } else {
254 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
255 }
256 break;
257
258 case 2: /* got 0x8e: EUC-JP-2004 kana */
259 filter->status = 0;
260 if (c > 0xa0 && c < 0xe0) {
261 w = 0xfec0 + c;
262 CK((*filter->output_function)(w, filter->data));
263 } else {
264 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
265 }
266 break;
267
268 case 3: /* X 0213 plane 2 first char: EUC-JP-2004 (0x8f) */
269 if (c == 0xA1 || (c >= 0xA3 && c <= 0xA5) || c == 0xA8 || (c >= 0xAC && c <= 0xAF) || (c >= 0xEE && c <= 0xFE)) {
270 filter->cache = c - 0x80;
271 filter->status++;
272 } else {
273 filter->status = 0;
274 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
275 }
276 break;
277
278 case 4: /* X 0213 plane 2 second char: EUC-JP-2004, ISO-2022-JP-2004 */
279 filter->status &= ~0xF;
280 c1 = filter->cache;
281 if (filter->from->no_encoding == mbfl_no_encoding_eucjp2004) {
282 c2 = c - 0x80;
283 } else {
284 c2 = c;
285 }
286
287 if (c2 < 0x21 || c2 > 0x7E) {
288 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
289 break;
290 }
291
292 s1 = c1 - 0x21;
293 s2 = c2 - 0x21;
294
295 if (((s1 >= 0 && s1 <= 4 && s1 != 1) || s1 == 7 || (s1 >= 11 && s1 <= 14) ||
296 (s1 >= 77 && s1 < 94)) && s2 >= 0 && s2 < 94) {
297 /* calc offset from ku */
298 for (k = 0; k < jisx0213_p2_ofst_len; k++) {
299 if (s1 == jisx0213_p2_ofst[k]-1) {
300 break;
301 }
302 }
303 k -= (jisx0213_p2_ofst[k]-1);
304
305 /* check for japanese chars in BMP */
306 s = (s1 + 94 + k)*94 + s2;
307 if (s >= 0 && s < jisx0213_ucs_table_size) {
308 w = jisx0213_ucs_table[s];
309 } else {
310 w = 0;
311 }
312
313 /* check for japanese chars in CJK Unified Ideographs ext.B (U+2XXXX) */
314 if (w <= 0) {
315 w1 = ((c1 + k + 94) << 8) | c2;
316 k = mbfl_bisec_srch2(w1, jisx0213_jis_u5_key, jisx0213_u5_tbl_len);
317 if (k >= 0) {
318 w = jisx0213_jis_u5_tbl[k] + 0x20000;
319 }
320 }
321
322 if (w <= 0) {
323 w = MBFL_BAD_INPUT;
324 }
325
326 CK((*filter->output_function)(w, filter->data));
327 } else {
328 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
329 }
330 break;
331
332 case 5: /* X 0208: ISO-2022-JP-2004 */
333 filter->status &= ~0xf;
334 c1 = filter->cache;
335 if (c > 0x20 && c < 0x7f) {
336 s = (c1 - 0x21)*94 + c - 0x21;
337 if (s >= 0 && s < jisx0208_ucs_table_size) {
338 w = jisx0208_ucs_table[s];
339 }
340 }
341
342 if (w <= 0) {
343 w = MBFL_BAD_INPUT;
344 }
345
346 CK((*filter->output_function)(w, filter->data));
347 break;
348
349 /* ESC: ISO-2022-JP-2004 */
350 /* case 0x06: */
351 /* case 0x16: */
352 /* case 0x26: */
353 /* case 0x86: */
354 /* case 0x96: */
355 /* case 0xa6: */
356 case 6:
357 if (c == '$') {
358 filter->status++;
359 } else if (c == '(') {
360 filter->status += 3;
361 } else {
362 filter->status &= ~0xf;
363 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
364 }
365 break;
366
367 /* ESC $: ISO-2022-JP-2004 */
368 /* case 0x07: */
369 /* case 0x17: */
370 /* case 0x27: */
371 /* case 0x87: */
372 /* case 0x97: */
373 /* case 0xa7: */
374 case 7:
375 if (c == 'B') { /* JIS X 0208-1983 */
376 filter->status = 0x80;
377 } else if (c == '(') {
378 filter->status++;
379 } else {
380 filter->status &= ~0xf;
381 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
382 }
383 break;
384
385 /* ESC $ (: ISO-2022-JP-2004 */
386 /* case 0x08: */
387 /* case 0x18: */
388 /* case 0x28: */
389 /* case 0x88: */
390 /* case 0x98: */
391 /* case 0xa8: */
392 case 8:
393 if (c == 'Q') { /* JIS X 0213 plane 1 */
394 filter->status = 0x90;
395 } else if (c == 'P') { /* JIS X 0213 plane 2 */
396 filter->status = 0xa0;
397 } else {
398 filter->status &= ~0xf;
399 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
400 }
401 break;
402
403 /* ESC (: ISO-2022-JP-2004 */
404 /* case 0x09: */
405 /* case 0x19: */
406 /* case 0x29: */
407 /* case 0x89: */
408 /* case 0x99: */
409 case 9:
410 if (c == 'B') {
411 filter->status = 0;
412 } else {
413 filter->status &= ~0xf;
414 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
415 }
416 break;
417
418 default:
419 filter->status = 0;
420 break;
421 }
422
423 return 0;
424 }
425
mbfl_filt_conv_jis2004_wchar_flush(mbfl_convert_filter * filter)426 int mbfl_filt_conv_jis2004_wchar_flush(mbfl_convert_filter *filter)
427 {
428 if (filter->status & 0xF) {
429 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
430 }
431 filter->status = 0;
432
433 if (filter->flush_function) {
434 return (*filter->flush_function)(filter->data);
435 }
436
437 return 0;
438 }
439
mbfl_filt_conv_wchar_jis2004(int c,mbfl_convert_filter * filter)440 int mbfl_filt_conv_wchar_jis2004(int c, mbfl_convert_filter *filter)
441 {
442 int k;
443 int c1, c2, s1, s2;
444
445 retry:
446 s1 = 0;
447 /* check for 1st char of combining characters */
448 if ((filter->status & 0xf) == 0 && (
449 c == 0x00E6 ||
450 (c >= 0x0254 && c <= 0x02E9) ||
451 (c >= 0x304B && c <= 0x3053) ||
452 (c >= 0x30AB && c <= 0x30C8) ||
453 c == 0x31F7)) {
454 for (k = 0; k < jisx0213_u2_tbl_len; k++) {
455 if (c == jisx0213_u2_tbl[2*k]) {
456 filter->status++;
457 filter->cache = k;
458 return 0;
459 }
460 }
461 }
462
463 /* check for 2nd char of combining characters */
464 if ((filter->status & 0xf) == 1 && filter->cache >= 0 && filter->cache < jisx0213_u2_tbl_len) {
465 k = filter->cache;
466 filter->status &= ~0xf;
467 filter->cache = 0;
468
469 c1 = jisx0213_u2_tbl[2*k];
470 if ((c1 == 0x0254 || c1 == 0x028C || c1 == 0x0259 || c1 == 0x025A) && c == 0x0301) {
471 k++;
472 }
473 if (c == jisx0213_u2_tbl[2*k+1]) {
474 s1 = jisx0213_u2_key[k];
475 } else { /* fallback */
476 s1 = jisx0213_u2_fb_tbl[k];
477
478 if (filter->to->no_encoding == mbfl_no_encoding_sjis2004) {
479 c1 = (s1 >> 8) & 0xff;
480 c2 = s1 & 0xff;
481 SJIS_ENCODE(c1, c2, s1, s2);
482 } else if (filter->to->no_encoding == mbfl_no_encoding_eucjp2004) {
483 s2 = (s1 & 0xff) + 0x80;
484 s1 = ((s1 >> 8) & 0xff) + 0x80;
485 } else {
486 if (filter->status != 0x200) {
487 CK((*filter->output_function)(0x1b, filter->data));
488 CK((*filter->output_function)('$', filter->data));
489 CK((*filter->output_function)('(', filter->data));
490 CK((*filter->output_function)('Q', filter->data));
491 }
492 filter->status = 0x200;
493
494 s2 = s1 & 0x7f;
495 s1 = (s1 >> 8) & 0x7f;
496 }
497
498 /* Flush out cached data */
499 CK((*filter->output_function)(s1, filter->data));
500 CK((*filter->output_function)(s2, filter->data));
501 goto retry;
502 }
503 }
504
505 /* check for major japanese chars: U+4E00 - U+9FFF */
506 if (s1 <= 0) {
507 for (k = 0; k < uni2jis_tbl_len; k++) {
508 if (c >= uni2jis_tbl_range[k][0] && c <= uni2jis_tbl_range[k][1]) {
509 s1 = uni2jis_tbl[k][c-uni2jis_tbl_range[k][0]];
510 break;
511 }
512 }
513 }
514
515 /* check for japanese chars in compressed mapping area: U+1E00 - U+4DBF */
516 if (s1 <= 0 && c >= ucs_c1_jisx0213_min && c <= ucs_c1_jisx0213_max) {
517 k = mbfl_bisec_srch(c, ucs_c1_jisx0213_tbl, ucs_c1_jisx0213_tbl_len);
518 if (k >= 0) {
519 s1 = ucs_c1_jisx0213_ofst[k] + c - ucs_c1_jisx0213_tbl[2*k];
520 }
521 }
522
523 /* check for japanese chars in CJK Unified Ideographs ext.B (U+2XXXX) */
524 if (s1 <= 0 && c >= jisx0213_u5_tbl_min && c <= jisx0213_u5_tbl_max) {
525 k = mbfl_bisec_srch2(c - 0x20000, jisx0213_u5_jis_key, jisx0213_u5_tbl_len);
526 if (k >= 0) {
527 s1 = jisx0213_u5_jis_tbl[k];
528 }
529 }
530
531 if (s1 <= 0) {
532 /* CJK Compatibility Forms: U+FE30 - U+FE4F */
533 if (c == 0xfe45) {
534 s1 = 0x233e;
535 } else if (c == 0xfe46) {
536 s1 = 0x233d;
537 } else if (c >= 0xf91d && c <= 0xf9dc) {
538 /* CJK Compatibility Ideographs: U+F900 - U+F92A */
539 k = mbfl_bisec_srch2(c, ucs_r2b_jisx0213_cmap_key, ucs_r2b_jisx0213_cmap_len);
540 if (k >= 0) {
541 s1 = ucs_r2b_jisx0213_cmap_val[k];
542 }
543 }
544 }
545
546 if (s1 <= 0) {
547 if (c == 0) {
548 s1 = 0;
549 } else {
550 s1 = -1;
551 }
552 } else if (s1 >= 0x9980) {
553 s1 = -1;
554 }
555
556 if (s1 >= 0) {
557 if (s1 < 0x80) { /* ASCII */
558 if (filter->to->no_encoding == mbfl_no_encoding_2022jp_2004 && (filter->status & 0xff00)) {
559 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
560 CK((*filter->output_function)('(', filter->data));
561 CK((*filter->output_function)('B', filter->data));
562 }
563 filter->status = 0;
564 CK((*filter->output_function)(s1, filter->data));
565 } else if (s1 < 0x100) { /* latin or kana */
566 if (filter->to->no_encoding == mbfl_no_encoding_eucjp2004) {
567 CK((*filter->output_function)(0x8e, filter->data));
568 }
569 CK((*filter->output_function)(s1, filter->data));
570 } else if (s1 < 0x7f00) { /* X 0213 plane 1 */
571 if (filter->to->no_encoding == mbfl_no_encoding_sjis2004) {
572 c1 = (s1 >> 8) & 0xff;
573 c2 = s1 & 0xff;
574 SJIS_ENCODE(c1, c2, s1, s2);
575 } else if (filter->to->no_encoding == mbfl_no_encoding_eucjp2004) {
576 s2 = (s1 & 0xff) + 0x80;
577 s1 = ((s1 >> 8) & 0xff) + 0x80;
578 } else {
579 if ((filter->status & 0xff00) != 0x200) {
580 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
581 CK((*filter->output_function)('$', filter->data));
582 CK((*filter->output_function)('(', filter->data));
583 CK((*filter->output_function)('Q', filter->data));
584 }
585 filter->status = 0x200;
586 s2 = s1 & 0xff;
587 s1 = (s1 >> 8) & 0xff;
588 }
589 CK((*filter->output_function)(s1, filter->data));
590 CK((*filter->output_function)(s2, filter->data));
591 } else { /* X 0213 plane 2 */
592 if (filter->to->no_encoding == mbfl_no_encoding_sjis2004) {
593 c1 = (s1 >> 8) & 0xff;
594 c2 = s1 & 0xff;
595 SJIS_ENCODE(c1, c2, s1, s2);
596 } else {
597 s2 = s1 & 0xff;
598 k = ((s1 >> 8) & 0xff) - 0x7f;
599 if (k >= 0 && k < jisx0213_p2_ofst_len) {
600 s1 = jisx0213_p2_ofst[k] - 1 + 0x21;
601 }
602 if (filter->to->no_encoding == mbfl_no_encoding_eucjp2004) {
603 s2 |= 0x80;
604 s1 |= 0x80;
605 CK((*filter->output_function)(0x8f, filter->data));
606 } else {
607 if ((filter->status & 0xff00) != 0x200) {
608 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
609 CK((*filter->output_function)('$', filter->data));
610 CK((*filter->output_function)('(', filter->data));
611 CK((*filter->output_function)('P', filter->data));
612 }
613 filter->status = 0x200;
614 }
615 }
616
617 CK((*filter->output_function)(s1, filter->data));
618 CK((*filter->output_function)(s2, filter->data));
619 }
620 } else {
621 CK(mbfl_filt_conv_illegal_output(c, filter));
622 }
623
624 return 0;
625 }
626
mbfl_filt_conv_wchar_jis2004_flush(mbfl_convert_filter * filter)627 int mbfl_filt_conv_wchar_jis2004_flush(mbfl_convert_filter *filter)
628 {
629 int k, c1, c2, s1, s2;
630
631 k = filter->cache;
632 filter->cache = 0;
633
634 if (filter->status == 1 && k >= 0 && k <= jisx0213_u2_tbl_len) {
635 s1 = jisx0213_u2_fb_tbl[k];
636
637 if (filter->to->no_encoding == mbfl_no_encoding_sjis2004) {
638 c1 = (s1 >> 8) & 0xff;
639 c2 = s1 & 0xff;
640 SJIS_ENCODE(c1, c2, s1, s2);
641 } else if (filter->to->no_encoding == mbfl_no_encoding_eucjp2004) {
642 s2 = (s1 & 0xff) | 0x80;
643 s1 = ((s1 >> 8) & 0xff) | 0x80;
644 } else {
645 s2 = s1 & 0x7f;
646 s1 = (s1 >> 8) & 0x7f;
647 if ((filter->status & 0xff00) != 0x200) {
648 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
649 CK((*filter->output_function)('$', filter->data));
650 CK((*filter->output_function)('(', filter->data));
651 CK((*filter->output_function)('Q', filter->data));
652 }
653 filter->status = 0x200;
654 }
655
656 CK((*filter->output_function)(s1, filter->data));
657 CK((*filter->output_function)(s2, filter->data));
658 }
659
660 /* If we had switched to a different charset, go back to ASCII mode
661 * This makes it possible to concatenate arbitrary valid strings
662 * together and get a valid string */
663 if (filter->status & 0xff00) {
664 CK((*filter->output_function)(0x1b, filter->data)); /* ESC */
665 CK((*filter->output_function)('(', filter->data));
666 CK((*filter->output_function)('B', filter->data));
667 }
668
669 filter->status = 0;
670
671 if (filter->flush_function) {
672 return (*filter->flush_function)(filter->data);
673 }
674
675 return 0;
676 }
677