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