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_iso2022_jp_ms.c
26  * by Rui Hirokawa <hirokawa@php.net> on 25 July 2011.
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "mbfilter.h"
35 #include "mbfilter_iso2022jp_mobile.h"
36 #include "mbfilter_sjis_mobile.h"
37 
38 #include "unicode_table_cp932_ext.h"
39 #include "unicode_table_jis.h"
40 #include "cp932_table.h"
41 
42 extern int mbfl_filt_conv_any_jis_flush(mbfl_convert_filter *filter);
43 extern int mbfl_filt_ident_2022jpms(int c, mbfl_identify_filter *filter);
44 
45 static const char *mbfl_encoding_2022jp_kddi_aliases[] = {"ISO-2022-JP-KDDI", NULL};
46 
47 const mbfl_encoding mbfl_encoding_2022jp_kddi = {
48 	mbfl_no_encoding_2022jp_kddi,
49 	"ISO-2022-JP-MOBILE#KDDI",
50 	"ISO-2022-JP",
51 	&mbfl_encoding_2022jp_kddi_aliases,
52 	NULL,
53 	MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE | MBFL_ENCTYPE_GL_UNSAFE,
54 	&vtbl_2022jp_kddi_wchar,
55 	&vtbl_wchar_2022jp_kddi
56 };
57 
58 const struct mbfl_identify_vtbl vtbl_identify_2022jp_kddi = {
59 	mbfl_no_encoding_2022jp_kddi,
60 	mbfl_filt_ident_common_ctor,
61 	mbfl_filt_ident_common_dtor,
62 	mbfl_filt_ident_2022jpms
63 };
64 
65 const struct mbfl_convert_vtbl vtbl_2022jp_kddi_wchar = {
66 	mbfl_no_encoding_2022jp_kddi,
67 	mbfl_no_encoding_wchar,
68 	mbfl_filt_conv_common_ctor,
69 	mbfl_filt_conv_common_dtor,
70 	mbfl_filt_conv_2022jp_mobile_wchar,
71 	mbfl_filt_conv_common_flush
72 };
73 
74 const struct mbfl_convert_vtbl vtbl_wchar_2022jp_kddi = {
75 	mbfl_no_encoding_wchar,
76 	mbfl_no_encoding_2022jp_kddi,
77 	mbfl_filt_conv_common_ctor,
78 	mbfl_filt_conv_common_dtor,
79 	mbfl_filt_conv_wchar_2022jp_mobile,
80 	mbfl_filt_conv_any_jis_flush
81 };
82 
83 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
84 
85 #define sjistoidx(c1, c2) \
86         (((c1) > 0x9f) \
87         ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
88         : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
89 #define idxtojis1(c) (((c) / 94) + 0x21)
90 #define idxtojis2(c) (((c) % 94) + 0x21)
91 
92 #define SJIS_ENCODE(c1,c2,s1,s2)	\
93 		do {						\
94 			s1 = c1;				\
95 			s1--;					\
96 			s1 >>= 1;				\
97 			if ((c1) < 0x5f) {		\
98 				s1 += 0x71;			\
99 			} else {				\
100 				s1 += 0xb1;			\
101 			}						\
102 			s2 = c2;				\
103 			if ((c1) & 1) {			\
104 				if ((c2) < 0x60) {	\
105 					s2--;			\
106 				}					\
107 				s2 += 0x20;			\
108 			} else {				\
109 				s2 += 0x7e;			\
110 			}						\
111 		} while (0)
112 
113 #define SJIS_DECODE(c1,c2,s1,s2)	\
114 		do {						\
115 			s1 = c1;				\
116 			if (s1 < 0xa0) {		\
117 				s1 -= 0x81;			\
118 			} else {				\
119 				s1 -= 0xc1;			\
120 			}						\
121 			s1 <<= 1;				\
122 			s1 += 0x21;				\
123 			s2 = c2;				\
124 			if (s2 < 0x9f) {		\
125 				if (s2 < 0x7f) {	\
126 					s2++;			\
127 				}					\
128 				s2 -= 0x20;			\
129 			} else {				\
130 				s1++;				\
131 				s2 -= 0x7e;			\
132 			}						\
133 		} while (0)
134 
135 #define CODE2JIS(c1,c2,s1,s2)       \
136 	c1 = (s1)/94+0x21;				\
137 	c2 = (s1)-94*((c1)-0x21)+0x21;	\
138 	s1 = ((c1) << 8) | (c2);		\
139 	s2 = 1
140 
141 /*
142  * ISO-2022-JP-Mobile => wchar
143  */
144 int
mbfl_filt_conv_2022jp_mobile_wchar(int c,mbfl_convert_filter * filter)145 mbfl_filt_conv_2022jp_mobile_wchar(int c, mbfl_convert_filter *filter)
146 {
147 	int c1, s, w, snd = 0;
148 
149 retry:
150 	switch (filter->status & 0xf) {
151 /*	case 0x00:	 ASCII */
152 /*	case 0x10:	 X 0201 latin */
153 /*	case 0x20:	 X 0201 kana */
154 /*	case 0x80:	 X 0208 */
155 	case 0:
156 		if (c == 0x1b) {
157 			filter->status += 2;
158 		} else if (filter->status == 0x20 && c > 0x20 && c < 0x60) {		/* kana */
159 			CK((*filter->output_function)(0xff40 + c, filter->data));
160 		} else if (filter->status == 0x80 && c > 0x20 && c < 0x80) {		/* kanji first char */
161 			filter->cache = c;
162 			filter->status += 1;
163 		} else if (c >= 0 && c < 0x80) {		/* latin, CTLs */
164 			CK((*filter->output_function)(c, filter->data));
165 		} else if (c > 0xa0 && c < 0xe0) {	/* GR kana */
166 			CK((*filter->output_function)(0xfec0 + c, filter->data));
167 		} else {
168 			w = c & MBFL_WCSGROUP_MASK;
169 			w |= MBFL_WCSGROUP_THROUGH;
170 			CK((*filter->output_function)(w, filter->data));
171 		}
172 		break;
173 
174 /*	case 0x81:	 X 0208 second char */
175 	case 1:
176 		w = 0;
177 		filter->status &= ~0xf;
178 		c1 = filter->cache;
179 		if (c > 0x20 && c < 0x7f) {
180 			s = (c1 - 0x21)*94 + c - 0x21;
181 
182 			if (s <= 137) {
183 				if (s == 31) {
184 					w = 0xff3c;			/* FULLWIDTH REVERSE SOLIDUS */
185 				} else if (s == 32) {
186 					w = 0xff5e;			/* FULLWIDTH TILDE */
187 				} else if (s == 33) {
188 					w = 0x2225;			/* PARALLEL TO */
189 				} else if (s == 60) {
190 					w = 0xff0d;			/* FULLWIDTH HYPHEN-MINUS */
191 				} else if (s == 80) {
192 					w = 0xffe0;			/* FULLWIDTH CENT SIGN */
193 				} else if (s == 81) {
194 					w = 0xffe1;			/* FULLWIDTH POUND SIGN */
195 				} else if (s == 137) {
196 					w = 0xffe2;			/* FULLWIDTH NOT SIGN */
197 				}
198 			}
199 
200 			if (w == 0) {
201 				if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {		/* vendor ext1 (13ku) */
202 					w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
203 				} else if (s >= 0 && s < jisx0208_ucs_table_size) {
204 					w = jisx0208_ucs_table[s];
205 				} else {
206 					w = 0;
207 				}
208 			}
209 
210 			if (s >= (84*94) && s < 91*94) {
211 				s += 22*94;
212 				if (filter->from->no_encoding == mbfl_no_encoding_2022jp_kddi) {
213 					w = mbfilter_sjis_emoji_kddi2unicode(s, &snd);
214 				}
215 				if (w > 0  && snd > 0) {
216 					CK((*filter->output_function)(snd, filter->data));
217 				}
218 			}
219 
220 			if (w <= 0) {
221 				w = (c1 << 8) | c;
222 				w &= MBFL_WCSPLANE_MASK;
223 				w |= MBFL_WCSPLANE_JIS0208;
224 				}
225 			CK((*filter->output_function)(w, filter->data));
226 		} else if (c == 0x1b) {
227 			filter->status += 2;
228 		} else if ((c >= 0 && c < 0x21) || c == 0x7f) {		/* CTLs */
229 			CK((*filter->output_function)(c, filter->data));
230 		} else {
231 			w = (c1 << 8) | c;
232 			w &= MBFL_WCSGROUP_MASK;
233 			w |= MBFL_WCSGROUP_THROUGH;
234 			CK((*filter->output_function)(w, filter->data));
235 		}
236 		break;
237 
238 	/* ESC */
239 /*	case 0x02:	*/
240 /*	case 0x12:	*/
241 /*	case 0x22:	*/
242 /*	case 0x82:	*/
243 	case 2:
244 		if (c == 0x24) {		/* '$' */
245 			filter->status++;
246 		} else if (c == 0x28) {		/* '(' */
247 			filter->status += 3;
248 		} else {
249 			filter->status &= ~0xf;
250 			CK((*filter->output_function)(0x1b, filter->data));
251 			goto retry;
252 		}
253 		break;
254 
255 	/* ESC $ */
256 /*	case 0x03:	*/
257 /*	case 0x13:	*/
258 /*	case 0x23:	*/
259 /*	case 0x83:	*/
260 	case 3:
261 		if (c == 0x40 || c == 0x42) {	/* '@' or 'B' */
262 			filter->status = 0x80;
263 		} else if (c == 0x28) {     /* '(' */
264 			filter->status++;
265 		} else {
266 			filter->status &= ~0xf;
267 			CK((*filter->output_function)(0x1b, filter->data));
268 			CK((*filter->output_function)(0x24, filter->data));
269 			goto retry;
270 		}
271 		break;
272 
273 	/* ESC $ ( */
274 /*	case 0x04:	*/
275 /*	case 0x14:	*/
276 /*	case 0x24:	*/
277 /*	case 0x84:	*/
278 	case 4:
279 		if (c == 0x40 || c == 0x42) {	/* '@' or 'B' */
280 			filter->status = 0x80;
281 		} else {
282 			filter->status &= ~0xf;
283 			CK((*filter->output_function)(0x1b, filter->data));
284 			CK((*filter->output_function)(0x24, filter->data));
285 			CK((*filter->output_function)(0x28, filter->data));
286 			goto retry;
287 		}
288 		break;
289 
290 	/* ESC ( */
291 /*	case 0x05:	*/
292 /*	case 0x15:	*/
293 /*	case 0x25:	*/
294 /*	case 0x85:	*/
295 	case 5:
296 		if (c == 0x42) {		/* 'B' */
297 			filter->status = 0;
298 		} else if (c == 0x4a) {		/* 'J' */
299 			filter->status = 0;
300 		} else if (c == 0x49) {		/* 'I' */
301 			filter->status = 0x20;
302 		} else {
303 			filter->status &= ~0xf;
304 			CK((*filter->output_function)(0x1b, filter->data));
305 			CK((*filter->output_function)(0x28, filter->data));
306 			goto retry;
307 		}
308 		break;
309 
310 	default:
311 		filter->status = 0;
312 		break;
313 	}
314 
315 	return c;
316 }
317 
318 /*
319  * wchar => ISO-2022-JP-Mobile
320  */
321 int
mbfl_filt_conv_wchar_2022jp_mobile(int c,mbfl_convert_filter * filter)322 mbfl_filt_conv_wchar_2022jp_mobile(int c, mbfl_convert_filter *filter)
323 {
324 	int c1, c2, s1, s2;
325 
326 	s1 = 0;
327 	s2 = 0;
328 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
329 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
330 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
331 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
332 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
333 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
334 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
335 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
336 	} else if (c >= 0xe000 && c < (0xe000 + 20*94)) {	/* user  (95ku - 114ku) */
337 		s1 = c - 0xe000;
338 		c1 = s1/94 + 0x7f;
339 		c2 = s1%94 + 0x21;
340 		s1 = (c1 << 8) | c2;
341 	}
342 	if (s1 <= 0) {
343 		c1 = c & ~MBFL_WCSPLANE_MASK;
344 		if (c1 == MBFL_WCSPLANE_WINCP932) {
345 			s1 = c & MBFL_WCSPLANE_MASK;
346 			s2 = 1;
347 		} else if (c1 == MBFL_WCSPLANE_JIS0208) {
348 			s1 = c & MBFL_WCSPLANE_MASK;
349 		} else if (c1 == MBFL_WCSPLANE_JIS0212) {
350 			s1 = c & MBFL_WCSPLANE_MASK;
351 			s1 |= 0x8080;
352 		} else if (c == 0xa5) {		/* YEN SIGN */
353 			s1 = 0x216f;	            /* FULLWIDTH YEN SIGN */
354 		} else if (c == 0x203e) {	/* OVER LINE */
355 			s1 = 0x2131;	/* FULLWIDTH MACRON */
356 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
357 			s1 = 0x2140;
358 		} else if (c == 0xff5e) {	/* FULLWIDTH TILDE */
359 			s1 = 0x2141;
360 		} else if (c == 0x2225) {	/* PARALLEL TO */
361 			s1 = 0x2142;
362 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
363 			s1 = 0x215d;
364 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
365 			s1 = 0x2171;
366 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
367 			s1 = 0x2172;
368 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
369 			s1 = 0x224c;
370 		}
371 	}
372 
373 	if ((s1 <= 0) || (s1 >= 0xa1a1 && s2 == 0)) { /* not found or X 0212 */
374 		s1 = -1;
375 		c1 = 0;
376 		c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
377 		while (c1 < c2) {		/* CP932 vendor ext1 (13ku) */
378 			if (c == cp932ext1_ucs_table[c1]) {
379 				s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
380 				break;
381 			}
382 			c1++;
383 		}
384 		if (c == 0) {
385 			s1 = 0;
386 		} else if (s1 <= 0) {
387 			s1 = -1;
388 		}
389 	}
390 
391  	if (filter->to->no_encoding == mbfl_no_encoding_2022jp_kddi &&
392 		mbfilter_unicode2sjis_emoji_kddi(c, &s1, filter) > 0) {
393 		CODE2JIS(c1,c2,s1,s2);
394 		s1 -= 0x1600;
395  	}
396 
397 	if (filter->status == 1 && filter->cache > 0) {
398 		return c;
399 	}
400 
401 	if (s1 >= 0) {
402 		if (s1 < 0x80) { /* latin */
403 			if ((filter->status & 0xff00) != 0) {
404 				CK((*filter->output_function)(0x1b, filter->data));		/* ESC */
405 				CK((*filter->output_function)(0x28, filter->data));		/* '(' */
406 				CK((*filter->output_function)(0x42, filter->data));		/* 'B' */
407 			}
408 			CK((*filter->output_function)(s1, filter->data));
409 			filter->status = 0;
410 		} else if (s1 > 0xa0 && s1 < 0xe0) { /* kana */
411 			if ((filter->status & 0xff00) != 0x100) {
412 				CK((*filter->output_function)(0x1b, filter->data));		/* ESC */
413 				CK((*filter->output_function)(0x28, filter->data));		/* '(' */
414 				CK((*filter->output_function)(0x49, filter->data));		/* 'I' */
415 			}
416 			filter->status = 0x100;
417 			CK((*filter->output_function)(s1 & 0x7f, filter->data));
418 		} else if (s1 < 0x7e7f) { /* 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)((s1 >> 8) & 0xff, filter->data));
426 			CK((*filter->output_function)(s1 & 0x7f, filter->data));
427 		}
428 	} else {
429 		CK(mbfl_filt_conv_illegal_output(c, filter));
430 	}
431 
432 	return c;
433 }
434