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_cp51932.h"
36 :
37 : #include "unicode_table_cp932_ext.h"
38 : #include "unicode_table_jis.h"
39 : #include "cp932_table.h"
40 :
41 : static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter);
42 :
43 : static const unsigned char mblen_table_eucjp[] = { /* 0xA1-0xFE */
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 : 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, 2, 1,
53 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54 : 1, 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, 2,
56 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1
60 : };
61 :
62 :
63 : static const char *mbfl_encoding_cp51932_aliases[] = {"cp51932", NULL};
64 :
65 : const struct mbfl_identify_vtbl vtbl_identify_cp51932 = {
66 : mbfl_no_encoding_cp51932,
67 : mbfl_filt_ident_common_ctor,
68 : mbfl_filt_ident_common_dtor,
69 : mbfl_filt_ident_cp51932
70 : };
71 :
72 : const mbfl_encoding mbfl_encoding_cp51932 = {
73 : mbfl_no_encoding_cp51932,
74 : "CP51932",
75 : "CP51932",
76 : (const char *(*)[])&mbfl_encoding_cp51932_aliases,
77 : mblen_table_eucjp,
78 : MBFL_ENCTYPE_MBCS
79 : };
80 :
81 : const struct mbfl_convert_vtbl vtbl_cp51932_wchar = {
82 : mbfl_no_encoding_cp51932,
83 : mbfl_no_encoding_wchar,
84 : mbfl_filt_conv_common_ctor,
85 : mbfl_filt_conv_common_dtor,
86 : mbfl_filt_conv_cp51932_wchar,
87 : mbfl_filt_conv_common_flush
88 : };
89 :
90 : const struct mbfl_convert_vtbl vtbl_wchar_cp51932 = {
91 : mbfl_no_encoding_wchar,
92 : mbfl_no_encoding_cp51932,
93 : mbfl_filt_conv_common_ctor,
94 : mbfl_filt_conv_common_dtor,
95 : mbfl_filt_conv_wchar_cp51932,
96 : mbfl_filt_conv_common_flush
97 : };
98 :
99 : #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
100 :
101 : #define sjistoidx(c1, c2) \
102 : (((c1) > 0x9f) \
103 : ? (((c1) - 0xc1) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)) \
104 : : (((c1) - 0x81) * 188 + (c2) - (((c2) > 0x7e) ? 0x41 : 0x40)))
105 : #define idxtoeuc1(c) (((c) / 94) + 0xa1)
106 : #define idxtoeuc2(c) (((c) % 94) + 0xa1)
107 :
108 : /*
109 : * cp51932 => wchar
110 : */
111 : int
112 : mbfl_filt_conv_cp51932_wchar(int c, mbfl_convert_filter *filter)
113 0 : {
114 : int c1, s, w;
115 :
116 0 : switch (filter->status) {
117 : case 0:
118 0 : if (c >= 0 && c < 0x80) { /* latin */
119 0 : CK((*filter->output_function)(c, filter->data));
120 0 : } else if (c > 0xa0 && c < 0xff) { /* CP932 first char */
121 0 : filter->status = 1;
122 0 : filter->cache = c;
123 0 : } else if (c == 0x8e) { /* kana first char */
124 0 : filter->status = 2;
125 : } else {
126 0 : w = c & MBFL_WCSGROUP_MASK;
127 0 : w |= MBFL_WCSGROUP_THROUGH;
128 0 : CK((*filter->output_function)(w, filter->data));
129 : }
130 0 : break;
131 :
132 : case 1: /* got first half */
133 0 : filter->status = 0;
134 0 : c1 = filter->cache;
135 0 : if (c > 0xa0 && c < 0xff) {
136 0 : w = 0;
137 0 : s = (c1 - 0xa1)*94 + c - 0xa1;
138 0 : if (s <= 137) {
139 0 : if (s == 31) {
140 0 : w = 0xff3c; /* FULLWIDTH REVERSE SOLIDUS */
141 0 : } else if (s == 32) {
142 0 : w = 0xff5e; /* FULLWIDTH TILDE */
143 0 : } else if (s == 33) {
144 0 : w = 0x2225; /* PARALLEL TO */
145 0 : } else if (s == 60) {
146 0 : w = 0xff0d; /* FULLWIDTH HYPHEN-MINUS */
147 0 : } else if (s == 80) {
148 0 : w = 0xffe0; /* FULLWIDTH CENT SIGN */
149 0 : } else if (s == 81) {
150 0 : w = 0xffe1; /* FULLWIDTH POUND SIGN */
151 0 : } else if (s == 137) {
152 0 : w = 0xffe2; /* FULLWIDTH NOT SIGN */
153 : }
154 : }
155 0 : if (w == 0) {
156 0 : if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) { /* vendor ext1 (13ku) */
157 0 : w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
158 0 : } else if (s >= 0 && s < jisx0208_ucs_table_size) { /* X 0208 */
159 0 : w = jisx0208_ucs_table[s];
160 0 : } else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) { /* vendor ext2 (89ku - 92ku) */
161 0 : w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
162 : }
163 : }
164 0 : if (w <= 0) {
165 0 : w = ((c1 & 0x7f) << 8) | (c & 0x7f);
166 0 : w &= MBFL_WCSPLANE_MASK;
167 0 : w |= MBFL_WCSPLANE_WINCP932;
168 : }
169 0 : CK((*filter->output_function)(w, filter->data));
170 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
171 0 : CK((*filter->output_function)(c, filter->data));
172 : } else {
173 0 : w = (c1 << 8) | c;
174 0 : w &= MBFL_WCSGROUP_MASK;
175 0 : w |= MBFL_WCSGROUP_THROUGH;
176 0 : CK((*filter->output_function)(w, filter->data));
177 : }
178 0 : break;
179 :
180 : case 2: /* got 0x8e, X0201 kana */
181 0 : filter->status = 0;
182 0 : if (c > 0xa0 && c < 0xe0) {
183 0 : w = 0xfec0 + c;
184 0 : CK((*filter->output_function)(w, filter->data));
185 0 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
186 0 : CK((*filter->output_function)(c, filter->data));
187 : } else {
188 0 : w = 0x8e00 | c;
189 0 : w &= MBFL_WCSGROUP_MASK;
190 0 : w |= MBFL_WCSGROUP_THROUGH;
191 0 : CK((*filter->output_function)(w, filter->data));
192 : }
193 0 : break;
194 :
195 : default:
196 0 : filter->status = 0;
197 : break;
198 : }
199 :
200 0 : return c;
201 : }
202 :
203 : int
204 : cp932ext3_to_cp51932(int c)
205 0 : {
206 : int idx;
207 :
208 0 : idx = sjistoidx(c >> 8, c & 0xff);
209 0 : if (idx >= sjistoidx(0xfa, 0x5c))
210 0 : idx -= sjistoidx(0xfa, 0x5c) - sjistoidx(0xed, 0x40);
211 0 : else if (idx >= sjistoidx(0xfa, 0x55))
212 0 : idx -= sjistoidx(0xfa, 0x55) - sjistoidx(0xee, 0xfa);
213 0 : else if (idx >= sjistoidx(0xfa, 0x40))
214 0 : idx -= sjistoidx(0xfa, 0x40) - sjistoidx(0xee, 0xef);
215 0 : return idxtoeuc1(idx) << 8 | idxtoeuc2(idx);
216 : }
217 :
218 : /*
219 : * wchar => cp51932
220 : */
221 : int
222 : mbfl_filt_conv_wchar_cp51932(int c, mbfl_convert_filter *filter)
223 0 : {
224 : int c1, c2, s1;
225 :
226 0 : s1 = 0;
227 0 : if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
228 0 : s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
229 0 : } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
230 0 : s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
231 0 : } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
232 0 : s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
233 0 : } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
234 0 : s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
235 : }
236 0 : if (s1 >= 0x8080) s1 = -1; /* we don't support JIS X0213 */
237 0 : if (s1 <= 0) {
238 0 : c1 = c & ~MBFL_WCSPLANE_MASK;
239 0 : if (c1 == MBFL_WCSPLANE_WINCP932) {
240 0 : s1 = c & MBFL_WCSPLANE_MASK;
241 0 : if (s1 >= ((85 + 0x20) << 8)) { /* 85ku - 120ku */
242 0 : s1 = -1;
243 : }
244 0 : } else if (c1 == MBFL_WCSPLANE_JIS0208) {
245 0 : s1 = c & MBFL_WCSPLANE_MASK;
246 0 : if ((s1 >= ((85 + 0x20) << 8) && /* 85ku - 94ku */
247 : s1 <= ((88 + 0x20) << 8)) ||/* IBM extension */
248 : (s1 >= ((93 + 0x20) << 8) && /* 89ku - 92ku */
249 : s1 <= ((94 + 0x20) << 8))) {
250 0 : s1 = -1;
251 : }
252 0 : } else if (c == 0xa5) { /* YEN SIGN */
253 0 : s1 = 0x216f; /* FULLWIDTH YEN SIGN */
254 0 : } else if (c == 0x203e) { /* OVER LINE */
255 0 : s1 = 0x2131; /* FULLWIDTH MACRON */
256 0 : } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
257 0 : s1 = 0x2140;
258 0 : } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
259 0 : s1 = 0x2141;
260 0 : } else if (c == 0x2225) { /* PARALLEL TO */
261 0 : s1 = 0x2142;
262 0 : } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
263 0 : s1 = 0x215d;
264 0 : } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
265 0 : s1 = 0x2171;
266 0 : } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
267 0 : s1 = 0x2172;
268 0 : } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
269 0 : s1 = 0x224c;
270 : } else {
271 0 : s1 = -1;
272 0 : c1 = 0;
273 0 : c2 = cp932ext1_ucs_table_max - cp932ext1_ucs_table_min;
274 0 : while (c1 < c2) { /* CP932 vendor ext1 (13ku) */
275 0 : if (c == cp932ext1_ucs_table[c1]) {
276 0 : s1 = ((c1/94 + 0x2d) << 8) + (c1%94 + 0x21);
277 0 : break;
278 : }
279 0 : c1++;
280 : }
281 0 : if (s1 < 0) {
282 0 : c1 = 0;
283 0 : c2 = cp932ext2_ucs_table_max - cp932ext2_ucs_table_min;
284 0 : while (c1 < c2) { /* CP932 vendor ext3 (115ku - 119ku) */
285 0 : if (c == cp932ext2_ucs_table[c1]) {
286 0 : s1 = ((c1/94 + 0x79) << 8) +(c1%94 + 0x21);
287 0 : break;
288 : }
289 0 : c1++;
290 : }
291 : }
292 : }
293 0 : if (c == 0) {
294 0 : s1 = 0;
295 0 : } else if (s1 <= 0) {
296 0 : s1 = -1;
297 : }
298 : }
299 :
300 0 : if (s1 >= 0) {
301 0 : if (s1 < 0x80) { /* latin */
302 0 : CK((*filter->output_function)(s1, filter->data));
303 0 : } else if (s1 < 0x100) { /* kana */
304 0 : CK((*filter->output_function)(0x8e, filter->data));
305 0 : CK((*filter->output_function)(s1, filter->data));
306 0 : } else if (s1 < 0x8080) { /* X 0208 */
307 0 : CK((*filter->output_function)(((s1 >> 8) & 0xff) | 0x80, filter->data));
308 0 : CK((*filter->output_function)((s1 & 0xff) | 0x80, filter->data));
309 : } else {
310 0 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
311 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
312 : }
313 : }
314 : } else {
315 0 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
316 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
317 : }
318 : }
319 :
320 0 : return c;
321 : }
322 :
323 : static int mbfl_filt_ident_cp51932(int c, mbfl_identify_filter *filter)
324 0 : {
325 0 : switch (filter->status) {
326 : case 0: /* latin */
327 0 : if (c >= 0 && c < 0x80) { /* ok */
328 : ;
329 0 : } else if (c > 0xa0 && c < 0xff) { /* kanji first char */
330 0 : filter->status = 1;
331 0 : } else if (c == 0x8e) { /* kana first char */
332 0 : filter->status = 2;
333 : } else { /* bad */
334 0 : filter->flag = 1;
335 : }
336 0 : break;
337 :
338 : case 1: /* got first half */
339 0 : if (c < 0xa1 || c > 0xfe) { /* bad */
340 0 : filter->flag = 1;
341 : }
342 0 : filter->status = 0;
343 0 : break;
344 :
345 : case 2: /* got 0x8e */
346 0 : if (c < 0xa1 || c > 0xdf) { /* bad */
347 0 : filter->flag = 1;
348 : }
349 0 : filter->status = 0;
350 0 : break;
351 :
352 : default:
353 0 : filter->status = 0;
354 : break;
355 : }
356 :
357 0 : return c;
358 : }
359 :
360 :
|