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_sjis.h"
36 :
37 : #include "unicode_table_cp932_ext.h"
38 : #include "unicode_table_jis.h"
39 :
40 : static int mbfl_filt_ident_sjis(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_aliases[] = {"x-sjis", "SHIFT-JIS", NULL};
62 :
63 : const mbfl_encoding mbfl_encoding_sjis = {
64 : mbfl_no_encoding_sjis,
65 : "SJIS",
66 : "Shift_JIS",
67 : (const char *(*)[])&mbfl_encoding_sjis_aliases,
68 : mblen_table_sjis,
69 : MBFL_ENCTYPE_MBCS
70 : };
71 :
72 : const struct mbfl_identify_vtbl vtbl_identify_sjis = {
73 : mbfl_no_encoding_sjis,
74 : mbfl_filt_ident_common_ctor,
75 : mbfl_filt_ident_common_dtor,
76 : mbfl_filt_ident_sjis
77 : };
78 :
79 : const struct mbfl_convert_vtbl vtbl_sjis_wchar = {
80 : mbfl_no_encoding_sjis,
81 : mbfl_no_encoding_wchar,
82 : mbfl_filt_conv_common_ctor,
83 : mbfl_filt_conv_common_dtor,
84 : mbfl_filt_conv_sjis_wchar,
85 : mbfl_filt_conv_common_flush
86 : };
87 :
88 : const struct mbfl_convert_vtbl vtbl_wchar_sjis = {
89 : mbfl_no_encoding_wchar,
90 : mbfl_no_encoding_sjis,
91 : mbfl_filt_conv_common_ctor,
92 : mbfl_filt_conv_common_dtor,
93 : mbfl_filt_conv_wchar_sjis,
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 => wchar
145 : */
146 : int
147 : mbfl_filt_conv_sjis_wchar(int c, mbfl_convert_filter *filter)
148 3129 : {
149 : int c1, s1, s2, w;
150 :
151 3129 : switch (filter->status) {
152 : case 0:
153 1938 : if (c >= 0 && c < 0x80) { /* latin */
154 249 : CK((*filter->output_function)(c, filter->data));
155 1440 : } else if (c > 0xa0 && c < 0xe0) { /* kana */
156 0 : CK((*filter->output_function)(0xfec0 + c, filter->data));
157 2880 : } else if (c > 0x80 && c < 0xfd && c != 0xa0) { /* kanji first char */
158 1440 : filter->status = 1;
159 1440 : 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 1689 : break;
166 :
167 : case 1: /* kanji second char */
168 1440 : filter->status = 0;
169 1440 : c1 = filter->cache;
170 2812 : if (c >= 0x40 && c <= 0xfc && c != 0x7f) {
171 1372 : SJIS_DECODE(c1, c, s1, s2);
172 1372 : w = (s1 - 0x21)*94 + s2 - 0x21;
173 2744 : if (w >= 0 && w < jisx0208_ucs_table_size) {
174 1372 : w = jisx0208_ucs_table[w];
175 : } else {
176 0 : w = 0;
177 : }
178 1372 : if (w <= 0) {
179 82 : if (s1 < 0x7f && s2 < 0x7f) {
180 41 : w = (s1 << 8) | s2;
181 41 : w &= MBFL_WCSPLANE_MASK;
182 41 : w |= MBFL_WCSPLANE_JIS0208;
183 : } else {
184 0 : w = (c1 << 8) | c;
185 0 : w &= MBFL_WCSGROUP_MASK;
186 0 : w |= MBFL_WCSGROUP_THROUGH;
187 : }
188 : }
189 1372 : CK((*filter->output_function)(w, filter->data));
190 102 : } else if ((c >= 0 && c < 0x21) || c == 0x7f) { /* CTLs */
191 34 : CK((*filter->output_function)(c, filter->data));
192 : } else {
193 34 : w = (c1 << 8) | c;
194 34 : w &= MBFL_WCSGROUP_MASK;
195 34 : w |= MBFL_WCSGROUP_THROUGH;
196 34 : CK((*filter->output_function)(w, filter->data));
197 : }
198 1440 : break;
199 :
200 : default:
201 0 : filter->status = 0;
202 : break;
203 : }
204 :
205 3129 : return c;
206 : }
207 :
208 : /*
209 : * wchar => SJIS
210 : */
211 : int
212 : mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
213 1853 : {
214 : int c1, c2, s1, s2;
215 :
216 1853 : s1 = 0;
217 2428 : if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
218 575 : s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
219 2287 : } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
220 1009 : s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
221 349 : } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
222 80 : s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
223 189 : } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
224 114 : s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
225 : }
226 1853 : if (s1 <= 0) {
227 76 : c1 = c & ~MBFL_WCSPLANE_MASK;
228 76 : if (c1 == MBFL_WCSPLANE_JIS0208) {
229 41 : s1 = c & MBFL_WCSPLANE_MASK;
230 35 : } else if (c == 0xa5) { /* YEN SIGN */
231 0 : s1 = 0x216f; /* FULLWIDTH YEN SIGN */
232 35 : } else if (c == 0x203e) { /* OVER LINE */
233 0 : s1 = 0x2131; /* FULLWIDTH MACRON */
234 35 : } else if (c == 0xff3c) { /* FULLWIDTH REVERSE SOLIDUS */
235 0 : s1 = 0x2140;
236 35 : } else if (c == 0xff5e) { /* FULLWIDTH TILDE */
237 0 : s1 = 0x2141;
238 35 : } else if (c == 0x2225) { /* PARALLEL TO */
239 0 : s1 = 0x2142;
240 35 : } else if (c == 0xff0d) { /* FULLWIDTH HYPHEN-MINUS */
241 0 : s1 = 0x215d;
242 35 : } else if (c == 0xffe0) { /* FULLWIDTH CENT SIGN */
243 0 : s1 = 0x2171;
244 35 : } else if (c == 0xffe1) { /* FULLWIDTH POUND SIGN */
245 0 : s1 = 0x2172;
246 35 : } else if (c == 0xffe2) { /* FULLWIDTH NOT SIGN */
247 0 : s1 = 0x224c;
248 : }
249 76 : if (c == 0) {
250 1 : s1 = 0;
251 75 : } else if (s1 <= 0) {
252 34 : s1 = -1;
253 : }
254 1777 : } else if (s1 >= 0x8080) {
255 0 : s1 = -1;
256 : }
257 1853 : if (s1 >= 0) {
258 1819 : if (s1 < 0x100) { /* latin or kana */
259 564 : CK((*filter->output_function)(s1, filter->data));
260 : } else { /* kanji */
261 1255 : c1 = (s1 >> 8) & 0xff;
262 1255 : c2 = s1 & 0xff;
263 1255 : SJIS_ENCODE(c1, c2, s1, s2);
264 1255 : CK((*filter->output_function)(s1, filter->data));
265 1255 : CK((*filter->output_function)(s2, filter->data));
266 : }
267 : } else {
268 34 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
269 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
270 : }
271 : }
272 :
273 1853 : return c;
274 : }
275 :
276 : static int mbfl_filt_ident_sjis(int c, mbfl_identify_filter *filter)
277 281 : {
278 281 : if (filter->status) { /* kanji second char */
279 0 : if (c < 0x40 || c > 0xfc || c == 0x7f) { /* bad */
280 0 : filter->flag = 1;
281 : }
282 0 : filter->status = 0;
283 281 : } else if (c >= 0 && c < 0x80) { /* latin ok */
284 : ;
285 34 : } else if (c > 0xa0 && c < 0xe0) { /* kana ok */
286 : ;
287 23 : } else if (c > 0x80 && c < 0xf0 && c != 0xa0) { /* kanji first char */
288 4 : filter->status = 1;
289 : } else { /* bad */
290 15 : filter->flag = 1;
291 : }
292 :
293 281 : return c;
294 : }
295 :
|