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