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_cp51932.h"
32 
33 #include "unicode_table_cp932_ext.h"
34 #include "unicode_table_jis.h"
35 #include "cp932_table.h"
36 
37 static int mbfl_filt_conv_cp51932_wchar_flush(mbfl_convert_filter *filter);
38 
39 static const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
40   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
43   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
45   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
46   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
47   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
48   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1,
49   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50   1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
53   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
54   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
55   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
56 };
57 
58 static const char *mbfl_encoding_cp51932_aliases[] = {"cp51932", NULL};
59 
60 const mbfl_encoding mbfl_encoding_cp51932 = {
61 	mbfl_no_encoding_cp51932,
62 	"CP51932",
63 	"CP51932",
64 	mbfl_encoding_cp51932_aliases,
65 	mblen_table_eucjp,
66 	0,
67 	&vtbl_cp51932_wchar,
68 	&vtbl_wchar_cp51932,
69 	NULL
70 };
71 
72 const struct mbfl_convert_vtbl vtbl_cp51932_wchar = {
73 	mbfl_no_encoding_cp51932,
74 	mbfl_no_encoding_wchar,
75 	mbfl_filt_conv_common_ctor,
76 	NULL,
77 	mbfl_filt_conv_cp51932_wchar,
78 	mbfl_filt_conv_cp51932_wchar_flush,
79 	NULL,
80 };
81 
82 const struct mbfl_convert_vtbl vtbl_wchar_cp51932 = {
83 	mbfl_no_encoding_wchar,
84 	mbfl_no_encoding_cp51932,
85 	mbfl_filt_conv_common_ctor,
86 	NULL,
87 	mbfl_filt_conv_wchar_cp51932,
88 	mbfl_filt_conv_common_flush,
89 	NULL,
90 };
91 
92 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
93 
94 /*
95  * cp51932 => wchar
96  */
97 int
mbfl_filt_conv_cp51932_wchar(int c,mbfl_convert_filter * filter)98 mbfl_filt_conv_cp51932_wchar(int c, mbfl_convert_filter *filter)
99 {
100 	int c1, s, w;
101 
102 	switch (filter->status) {
103 	case 0:
104 		if (c >= 0 && c < 0x80) { /* latin */
105 			CK((*filter->output_function)(c, filter->data));
106 		} else if (c >= 0xA1 && c <= 0xFE) { /* CP932, first byte */
107 			filter->status = 1;
108 			filter->cache = c;
109 		} else if (c == 0x8e) { /* kana first char */
110 			filter->status = 2;
111 		} else {
112 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
113 		}
114 		break;
115 
116 	case 1:	/* got first half */
117 		filter->status = 0;
118 		c1 = filter->cache;
119 		if (c > 0xa0 && c < 0xff) {
120 			w = 0;
121 			s = (c1 - 0xa1)*94 + c - 0xa1;
122 			if (s <= 137) {
123 				if (s == 31) {
124 					w = 0xff3c;			/* FULLWIDTH REVERSE SOLIDUS */
125 				} else if (s == 32) {
126 					w = 0xff5e;			/* FULLWIDTH TILDE */
127 				} else if (s == 33) {
128 					w = 0x2225;			/* PARALLEL TO */
129 				} else if (s == 60) {
130 					w = 0xff0d;			/* FULLWIDTH HYPHEN-MINUS */
131 				} else if (s == 80) {
132 					w = 0xffe0;			/* FULLWIDTH CENT SIGN */
133 				} else if (s == 81) {
134 					w = 0xffe1;			/* FULLWIDTH POUND SIGN */
135 				} else if (s == 137) {
136 					w = 0xffe2;			/* FULLWIDTH NOT SIGN */
137 				}
138 			}
139 			if (w == 0) {
140 				if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {		/* vendor ext1 (13ku) */
141 					w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
142 				} else if (s >= 0 && s < jisx0208_ucs_table_size) {		/* X 0208 */
143 					w = jisx0208_ucs_table[s];
144 				} else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) {		/* vendor ext2 (89ku - 92ku) */
145 					w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
146 				}
147 			}
148 			if (w <= 0) {
149 				w = MBFL_BAD_INPUT;
150 			}
151 			CK((*filter->output_function)(w, filter->data));
152 		} else {
153 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
154 		}
155 		break;
156 
157 	case 2:	/* got 0x8e, X0201 kana */
158 		filter->status = 0;
159 		if (c > 0xa0 && c < 0xe0) {
160 			w = 0xfec0 + c;
161 			CK((*filter->output_function)(w, filter->data));
162 		} else {
163 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
164 		}
165 		break;
166 
167 	default:
168 		filter->status = 0;
169 		break;
170 	}
171 
172 	return 0;
173 }
174 
mbfl_filt_conv_cp51932_wchar_flush(mbfl_convert_filter * filter)175 static int mbfl_filt_conv_cp51932_wchar_flush(mbfl_convert_filter *filter)
176 {
177 	if (filter->status) {
178 		/* Input string was truncated */
179 		(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
180 		filter->status = 0;
181 	}
182 
183 	if (filter->flush_function) {
184 		(*filter->flush_function)(filter->data);
185 	}
186 
187 	return 0;
188 }
189 
190 /*
191  * wchar => cp51932
192  */
193 int
mbfl_filt_conv_wchar_cp51932(int c,mbfl_convert_filter * filter)194 mbfl_filt_conv_wchar_cp51932(int c, mbfl_convert_filter *filter)
195 {
196 	int c1, c2, s1;
197 
198 	s1 = 0;
199 	if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
200 		s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
201 	} else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
202 		s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
203 	} else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
204 		s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
205 	} else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
206 		s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
207 	}
208 	if (s1 >= 0x8080) s1 = -1; /* we don't support JIS X0213 */
209 	if (s1 <= 0) {
210 		if (c == 0xa5) { /* YEN SIGN */
211 			s1 = 0x216F; /* FULLWIDTH YEN SIGN */
212 		} else if (c == 0xff3c) {	/* FULLWIDTH REVERSE SOLIDUS */
213 			s1 = 0x2140;
214 		} else if (c == 0x2225) {	/* PARALLEL TO */
215 			s1 = 0x2142;
216 		} else if (c == 0xff0d) {	/* FULLWIDTH HYPHEN-MINUS */
217 			s1 = 0x215d;
218 		} else if (c == 0xffe0) {	/* FULLWIDTH CENT SIGN */
219 			s1 = 0x2171;
220 		} else if (c == 0xffe1) {	/* FULLWIDTH POUND SIGN */
221 			s1 = 0x2172;
222 		} else if (c == 0xffe2) {	/* FULLWIDTH NOT SIGN */
223 			s1 = 0x224c;
224 		} else {
225 			s1 = -1;
226 			c1 = 0;
227 			c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
228 			while (c1 < c2) {		/* CP932 vendor ext1 (13ku) */
229 				if (c == cp932ext1_ucs_table[c1]) {
230 					s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
231 					break;
232 				}
233 				c1++;
234 			}
235 			if (s1 < 0) {
236 				c1 = 0;
237 				c2 = cp932ext2_ucs_table_max - cp932ext2_ucs_table_min;
238 				while (c1 < c2) {		/* CP932 vendor ext3 (115ku - 119ku) */
239 					if (c == cp932ext2_ucs_table[c1]) {
240 					  s1 = ((c1/94 + 0x79) << 8) +(c1%94 + 0x21);
241 					  break;
242 					}
243 					c1++;
244 				}
245 			}
246 		}
247 		if (c == 0) {
248 			s1 = 0;
249 		} else if (s1 <= 0) {
250 			s1 = -1;
251 		}
252 	}
253 
254 	if (s1 >= 0) {
255 		if (s1 < 0x80) {	/* latin */
256 			CK((*filter->output_function)(s1, filter->data));
257 		} else if (s1 < 0x100) {	/* kana */
258 			CK((*filter->output_function)(0x8e, filter->data));
259 			CK((*filter->output_function)(s1, filter->data));
260 		} else if (s1 < 0x8080)  {	/* X 0208 */
261 			CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
262 			CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
263 		} else {
264 		    CK(mbfl_filt_conv_illegal_output(c, filter));
265 		}
266 	} else {
267 		CK(mbfl_filt_conv_illegal_output(c, filter));
268 	}
269 
270 	return 0;
271 }
272