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