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_sjis_open.h"
32
33 #include "unicode_table_cp932_ext.h"
34 #include "unicode_table_jis.h"
35
36 static int mbfl_filt_ident_sjis_open(int c, mbfl_identify_filter *filter);
37
38 static const unsigned char mblen_table_sjis[] = { /* 0x80-0x9f,0xE0-0xFF */
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
50 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
52 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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 };
56
57 static const char *mbfl_encoding_sjis_open_aliases[] = {"SJIS-open", "SJIS-ms", NULL};
58
59 const mbfl_encoding mbfl_encoding_sjis_open = {
60 mbfl_no_encoding_sjis_open,
61 "SJIS-win",
62 "Shift_JIS",
63 (const char *(*)[])&mbfl_encoding_sjis_open_aliases,
64 mblen_table_sjis,
65 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
66 &vtbl_sjis_open_wchar,
67 &vtbl_wchar_sjis_open
68 };
69
70 const struct mbfl_identify_vtbl vtbl_identify_sjis_open = {
71 mbfl_no_encoding_sjis_open,
72 mbfl_filt_ident_common_ctor,
73 mbfl_filt_ident_sjis_open
74 };
75
76 const struct mbfl_convert_vtbl vtbl_sjis_open_wchar = {
77 mbfl_no_encoding_sjis_open,
78 mbfl_no_encoding_wchar,
79 mbfl_filt_conv_common_ctor,
80 NULL,
81 mbfl_filt_conv_sjis_open_wchar,
82 mbfl_filt_conv_common_flush,
83 NULL,
84 };
85
86 const struct mbfl_convert_vtbl vtbl_wchar_sjis_open = {
87 mbfl_no_encoding_wchar,
88 mbfl_no_encoding_sjis_open,
89 mbfl_filt_conv_common_ctor,
90 NULL,
91 mbfl_filt_conv_wchar_sjis_open,
92 mbfl_filt_conv_common_flush,
93 NULL,
94 };
95
96 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
97
98 #define SJIS_ENCODE(c1,c2,s1,s2) \
99 do { \
100 s1 = c1; \
101 s1--; \
102 s1 >>= 1; \
103 if ((c1) < 0x5f) { \
104 s1 += 0x71; \
105 } else { \
106 s1 += 0xb1; \
107 } \
108 s2 = c2; \
109 if ((c1) & 1) { \
110 if ((c2) < 0x60) { \
111 s2--; \
112 } \
113 s2 += 0x20; \
114 } else { \
115 s2 += 0x7e; \
116 } \
117 } while (0)
118
119 #define SJIS_DECODE(c1,c2,s1,s2) \
120 do { \
121 s1 = c1; \
122 if (s1 < 0xa0) { \
123 s1 -= 0x81; \
124 } else { \
125 s1 -= 0xc1; \
126 } \
127 s1 <<= 1; \
128 s1 += 0x21; \
129 s2 = c2; \
130 if (s2 < 0x9f) { \
131 if (s2 < 0x7f) { \
132 s2++; \
133 } \
134 s2 -= 0x20; \
135 } else { \
136 s1++; \
137 s2 -= 0x7e; \
138 } \
139 } while (0)
140
141 /*
142 * SJIS-win => wchar
143 */
144 int
mbfl_filt_conv_sjis_open_wchar(int c,mbfl_convert_filter * filter)145 mbfl_filt_conv_sjis_open_wchar(int c, mbfl_convert_filter *filter)
146 {
147 int c1, s, s1, s2, w;
148
149 switch (filter->status) {
150 case 0:
151 if (c >= 0 && c < 0x80) { /* latin */
152 CK((*filter->output_function)(c, filter->data));
153 } else if (c > 0xa0 && c < 0xe0) { /* kana */
154 CK((*filter->output_function)(0xfec0 + c, filter->data));
155 } else if (c > 0x80 && c < 0xfd && c != 0xa0) { /* kanji first char */
156 filter->status = 1;
157 filter->cache = c;
158 } else {
159 w = c & MBFL_WCSGROUP_MASK;
160 w |= MBFL_WCSGROUP_THROUGH;
161 CK((*filter->output_function)(w, filter->data));
162 }
163 break;
164
165 case 1: /* kanji second char */
166 filter->status = 0;
167 c1 = filter->cache;
168 if (c >= 0x40 && c <= 0xfc && c != 0x7f) {
169 w = 0;
170 SJIS_DECODE(c1, c, s1, s2);
171 s = (s1 - 0x21)*94 + s2 - 0x21;
172 if (s <= 137) {
173 if (s == 31) {
174 w = 0xff3c; /* FULLWIDTH REVERSE SOLIDUS */
175 } else if (s == 32) {
176 w = 0xff5e; /* FULLWIDTH TILDE */
177 } else if (s == 33) {
178 w = 0x2225; /* PARALLEL TO */
179 } else if (s == 60) {
180 w = 0xff0d; /* FULLWIDTH HYPHEN-MINUS */
181 } else if (s == 80) {
182 w = 0xffe0; /* FULLWIDTH CENT SIGN */
183 } else if (s == 81) {
184 w = 0xffe1; /* FULLWIDTH POUND SIGN */
185 } else if (s == 137) {
186 w = 0xffe2; /* FULLWIDTH NOT SIGN */
187 }
188 }
189 if (w == 0) {
190 if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) { /* vendor ext1 (13ku) */
191 w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
192 } else if (s >= 0 && s < jisx0208_ucs_table_size) { /* X 0208 */
193 w = jisx0208_ucs_table[s];
194 } else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) { /* vendor ext2 (89ku - 92ku) */
195 w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
196 } else if (s >= cp932ext3_ucs_table_min && s < cp932ext3_ucs_table_max) { /* vendor ext3 (115ku - 119ku) */
197 w = cp932ext3_ucs_table[s - cp932ext3_ucs_table_min];
198 } else if (s >= (94*94) && s < (114*94)) { /* user (95ku - 114ku) */
199 w = s - (94*94) + 0xe000;
200 }
201 }
202 if (w <= 0) {
203 w = (s1 << 8) | s2;
204 w &= MBFL_WCSPLANE_MASK;
205 w |= MBFL_WCSPLANE_WINCP932;
206 }
207 CK((*filter->output_function)(w, filter->data));
208 } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
209 CK((*filter->output_function)(c, filter->data));
210 } else {
211 w = (c1 << 8) | c;
212 w &= MBFL_WCSGROUP_MASK;
213 w |= MBFL_WCSGROUP_THROUGH;
214 CK((*filter->output_function)(w, filter->data));
215 }
216 break;
217
218 default:
219 filter->status = 0;
220 break;
221 }
222
223 return c;
224 }
225
226 /*
227 * wchar => SJIS-win
228 */
229 int
mbfl_filt_conv_wchar_sjis_open(int c,mbfl_convert_filter * filter)230 mbfl_filt_conv_wchar_sjis_open(int c, mbfl_convert_filter *filter)
231 {
232 int c1, c2, s1, s2;
233
234 s1 = 0;
235 s2 = 0;
236 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
237 s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
238 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
239 s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
240 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
241 s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
242 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
243 s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
244 } else if (c >= 0xe000 && c < (0xe000 + 20*94)) { /* user (95ku - 114ku) */
245 s1 = c - 0xe000;
246 c1 = s1/94 + 0x7f;
247 c2 = s1%94 + 0x21;
248 s1 = (c1 << 8) | c2;
249 s2 = 1;
250 }
251 if (s1 <= 0) {
252 c1 = c & ~MBFL_WCSPLANE_MASK;
253 if (c1 == MBFL_WCSPLANE_WINCP932) {
254 s1 = c & MBFL_WCSPLANE_MASK;
255 s2 = 1;
256 } else if (c1 == MBFL_WCSPLANE_JIS0208) {
257 s1 = c & MBFL_WCSPLANE_MASK;
258 } else if (c1 == MBFL_WCSPLANE_JIS0212) {
259 s1 = c & MBFL_WCSPLANE_MASK;
260 s1 |= 0x8080;
261 } else if (c == 0xa5) { /* YEN SIGN */
262 s1 = 0x216f; /* FULLWIDTH YEN SIGN */
263 } else if (c == 0x203e) { /* OVER LINE */
264 s1 = 0x2131; /* FULLWIDTH MACRON */
265 } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
266 s1 = 0x2140;
267 } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
268 s1 = 0x2141;
269 } else if (c == 0x2225) { /* PARALLEL TO */
270 s1 = 0x2142;
271 } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
272 s1 = 0x215d;
273 } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
274 s1 = 0x2171;
275 } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
276 s1 = 0x2172;
277 } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
278 s1 = 0x224c;
279 }
280 }
281 if ((s1 <= 0) || (s1 >= 0x8080 && s2 == 0)) { /* not found or X 0212 */
282 s1 = -1;
283 c1 = 0;
284 c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
285 while (c1 < c2) { /* CP932 vendor ext1 (13ku) */
286 if (c == cp932ext1_ucs_table[c1]) {
287 s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
288 break;
289 }
290 c1++;
291 }
292 if (s1 <= 0) {
293 c1 = 0;
294 c2 = cp932ext3_ucs_table_max - cp932ext3_ucs_table_min;
295 while (c1 < c2) { /* CP932 vendor ext3 (115ku - 119ku) */
296 if (c == cp932ext3_ucs_table[c1]) {
297 s1 = ((c1/94 + 0x93) << 8) + (c1%94 + 0x21);
298 break;
299 }
300 c1++;
301 }
302 }
303 if (c == 0) {
304 s1 = 0;
305 } else if (s1 <= 0) {
306 s1 = -1;
307 }
308 }
309
310 if (s1 >= 0) {
311 if (s1 < 0x100) { /* latin or kana */
312 CK((*filter->output_function)(s1, filter->data));
313 } else { /* kanji */
314 c1 = (s1 >> 8) & 0xff;
315 c2 = s1 & 0xff;
316 SJIS_ENCODE(c1, c2, s1, s2);
317 CK((*filter->output_function)(s1, filter->data));
318 CK((*filter->output_function)(s2, filter->data));
319 }
320 } else {
321 CK(mbfl_filt_conv_illegal_output(c, filter));
322 }
323
324 return c;
325 }
326
mbfl_filt_ident_sjis_open(int c,mbfl_identify_filter * filter)327 static int mbfl_filt_ident_sjis_open(int c, mbfl_identify_filter *filter)
328 {
329 if (filter->status) { /* kanji second char */
330 if (c < 0x40 || c > 0xfc || c == 0x7f) { /* bad */
331 filter->flag = 1;
332 }
333 filter->status = 0;
334 } else if (c >= 0 && c < 0x80) { /* latin ok */
335 ;
336 } else if (c > 0xa0 && c < 0xe0) { /* kana ok */
337 ;
338 } else if (c > 0x80 && c < 0xfd && c != 0xa0) { /* kanji first char */
339 filter->status = 1;
340 } else { /* bad */
341 filter->flag = 1;
342 }
343
344 return c;
345 }
346