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.h"
32
33 #define UNICODE_TABLE_CP932_DEF
34 #define UNICODE_TABLE_JIS_DEF
35
36 #include "unicode_table_cp932_ext.h"
37 #include "unicode_table_jis.h"
38
39 static int mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter *filter);
40 static size_t mb_sjis_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
41 static void mb_wchar_to_sjis(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
42
43 const unsigned char mblen_table_sjis[] = { /* 0x81-0x9F,0xE0-0xEF */
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, 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 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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
58 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
59 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
60 };
61
62 static const char *mbfl_encoding_sjis_aliases[] = {"x-sjis", "SHIFT-JIS", NULL};
63
64 const mbfl_encoding mbfl_encoding_sjis = {
65 mbfl_no_encoding_sjis,
66 "SJIS",
67 "Shift_JIS",
68 mbfl_encoding_sjis_aliases,
69 mblen_table_sjis,
70 MBFL_ENCTYPE_GL_UNSAFE,
71 &vtbl_sjis_wchar,
72 &vtbl_wchar_sjis,
73 mb_sjis_to_wchar,
74 mb_wchar_to_sjis,
75 NULL
76 };
77
78 const struct mbfl_convert_vtbl vtbl_sjis_wchar = {
79 mbfl_no_encoding_sjis,
80 mbfl_no_encoding_wchar,
81 mbfl_filt_conv_common_ctor,
82 NULL,
83 mbfl_filt_conv_sjis_wchar,
84 mbfl_filt_conv_sjis_wchar_flush,
85 NULL
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 NULL,
93 mbfl_filt_conv_wchar_sjis,
94 mbfl_filt_conv_common_flush,
95 NULL
96 };
97
98 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
99
100 #define SJIS_ENCODE(c1,c2,s1,s2) \
101 do { \
102 s1 = c1; \
103 s1--; \
104 s1 >>= 1; \
105 if ((c1) < 0x5f) { \
106 s1 += 0x71; \
107 } else { \
108 s1 += 0xb1; \
109 } \
110 s2 = c2; \
111 if ((c1) & 1) { \
112 if ((c2) < 0x60) { \
113 s2--; \
114 } \
115 s2 += 0x20; \
116 } else { \
117 s2 += 0x7e; \
118 } \
119 } while (0)
120
121 #define SJIS_DECODE(c1,c2,s1,s2) \
122 do { \
123 s1 = c1; \
124 if (s1 < 0xa0) { \
125 s1 -= 0x81; \
126 } else { \
127 s1 -= 0xc1; \
128 } \
129 s1 <<= 1; \
130 s1 += 0x21; \
131 s2 = c2; \
132 if (s2 < 0x9f) { \
133 if (s2 < 0x7f) { \
134 s2++; \
135 } \
136 s2 -= 0x20; \
137 } else { \
138 s1++; \
139 s2 -= 0x7e; \
140 } \
141 } while (0)
142
mbfl_filt_conv_sjis_wchar(int c,mbfl_convert_filter * filter)143 int mbfl_filt_conv_sjis_wchar(int c, mbfl_convert_filter *filter)
144 {
145 int s1, s2, w;
146
147 switch (filter->status) {
148 case 0:
149 if (c >= 0 && c < 0x80) { /* ASCII */
150 CK((*filter->output_function)(c, filter->data));
151 } else if (c > 0xA0 && c < 0xE0) { /* Kana */
152 CK((*filter->output_function)(0xFEC0 + c, filter->data));
153 } else if (c > 0x80 && c < 0xF0 && c != 0xA0) { /* Kanji, first byte */
154 filter->status = 1;
155 filter->cache = c;
156 } else {
157 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
158 }
159 break;
160
161 case 1: /* Kanji, second byte */
162 filter->status = 0;
163 int c1 = filter->cache;
164 if (c >= 0x40 && c <= 0xFC && c != 0x7F) {
165 SJIS_DECODE(c1, c, s1, s2);
166 w = (s1 - 0x21)*94 + s2 - 0x21;
167 if (w >= 0 && w < jisx0208_ucs_table_size) {
168 w = jisx0208_ucs_table[w];
169 if (!w)
170 w = MBFL_BAD_INPUT;
171 } else {
172 w = MBFL_BAD_INPUT;
173 }
174 CK((*filter->output_function)(w, filter->data));
175 } else {
176 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
177 }
178 }
179
180 return 0;
181 }
182
mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter * filter)183 static int mbfl_filt_conv_sjis_wchar_flush(mbfl_convert_filter *filter)
184 {
185 if (filter->status) {
186 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
187 filter->status = 0;
188 }
189
190 if (filter->flush_function) {
191 (*filter->flush_function)(filter->data);
192 }
193
194 return 0;
195 }
196
mbfl_filt_conv_wchar_sjis(int c,mbfl_convert_filter * filter)197 int mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
198 {
199 int c1, c2, s1, s2;
200
201 s1 = 0;
202 if (c >= ucs_a1_jis_table_min && c < ucs_a1_jis_table_max) {
203 s1 = ucs_a1_jis_table[c - ucs_a1_jis_table_min];
204 } else if (c >= ucs_a2_jis_table_min && c < ucs_a2_jis_table_max) {
205 s1 = ucs_a2_jis_table[c - ucs_a2_jis_table_min];
206 } else if (c >= ucs_i_jis_table_min && c < ucs_i_jis_table_max) {
207 s1 = ucs_i_jis_table[c - ucs_i_jis_table_min];
208 } else if (c >= ucs_r_jis_table_min && c < ucs_r_jis_table_max) {
209 s1 = ucs_r_jis_table[c - ucs_r_jis_table_min];
210 }
211 if (s1 <= 0) {
212 if (c == 0xA5) { /* YEN SIGN */
213 s1 = 0x216F; /* FULLWIDTH YEN SIGN */
214 } else if (c == 0xAF || c == 0x203E) { /* U+00AF is MACRON, U+203E is OVERLINE */
215 s1 = 0x2131; /* FULLWIDTH MACRON */
216 } else if (c == 0xFF3C) { /* FULLWIDTH REVERSE SOLIDUS */
217 s1 = 0x2140;
218 } else if (c == 0x2225) { /* PARALLEL TO */
219 s1 = 0x2142;
220 } else if (c == 0xFF0D) { /* FULLWIDTH HYPHEN-MINUS */
221 s1 = 0x215D;
222 } else if (c == 0xFFE0) { /* FULLWIDTH CENT SIGN */
223 s1 = 0x2171;
224 } else if (c == 0xFFE1) { /* FULLWIDTH POUND SIGN */
225 s1 = 0x2172;
226 } else if (c == 0xFFE2) { /* FULLWIDTH NOT SIGN */
227 s1 = 0x224C;
228 } else if (c == 0) {
229 s1 = 0;
230 } else {
231 s1 = -1;
232 }
233 } else if (s1 >= 0x8080) { /* JIS X 0212; not supported */
234 s1 = -1;
235 }
236
237 if (s1 >= 0) {
238 if (s1 < 0x100) { /* Latin/Kana */
239 CK((*filter->output_function)(s1, filter->data));
240 } else { /* Kanji */
241 c1 = (s1 >> 8) & 0xFF;
242 c2 = s1 & 0xFF;
243 SJIS_ENCODE(c1, c2, s1, s2);
244 CK((*filter->output_function)(s1, filter->data));
245 CK((*filter->output_function)(s2, filter->data));
246 }
247 } else {
248 CK(mbfl_filt_conv_illegal_output(c, filter));
249 }
250
251 return 0;
252 }
253
mb_sjis_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)254 static size_t mb_sjis_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
255 {
256 unsigned char *p = *in, *e = p + *in_len;
257 uint32_t *out = buf, *limit = buf + bufsize;
258
259 while (p < e && out < limit) {
260 unsigned char c = *p++;
261
262 if (c <= 0x7F) {
263 *out++ = c;
264 } else if (c >= 0xA1 && c <= 0xDF) { /* Kana */
265 *out++ = 0xFEC0 + c;
266 } else if (c > 0x80 && c <= 0xEF && c != 0xA0 && p < e) {
267 unsigned char c2 = *p++;
268 if (c2 >= 0x40 && c2 <= 0xFC && c2 != 0x7F) {
269 unsigned int s1, s2;
270 SJIS_DECODE(c, c2, s1, s2);
271 uint32_t w = (s1 - 0x21)*94 + s2 - 0x21;
272 if (w < jisx0208_ucs_table_size) {
273 w = jisx0208_ucs_table[w];
274 if (!w)
275 w = MBFL_BAD_INPUT;
276 *out++ = w;
277 } else {
278 *out++ = MBFL_BAD_INPUT;
279 }
280 } else {
281 *out++ = MBFL_BAD_INPUT;
282 }
283 } else {
284 *out++ = MBFL_BAD_INPUT;
285 }
286 }
287
288 *in_len = e - p;
289 *in = p;
290 return out - buf;
291 }
292
mb_wchar_to_sjis(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)293 static void mb_wchar_to_sjis(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
294 {
295 unsigned char *out, *limit;
296 MB_CONVERT_BUF_LOAD(buf, out, limit);
297 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
298
299 while (len--) {
300 uint32_t w = *in++;
301 unsigned int s = 0;
302
303 if (w >= ucs_a1_jis_table_min && w < ucs_a1_jis_table_max) {
304 s = ucs_a1_jis_table[w - ucs_a1_jis_table_min];
305 } else if (w >= ucs_a2_jis_table_min && w < ucs_a2_jis_table_max) {
306 s = ucs_a2_jis_table[w - ucs_a2_jis_table_min];
307 } else if (w >= ucs_i_jis_table_min && w < ucs_i_jis_table_max) {
308 s = ucs_i_jis_table[w - ucs_i_jis_table_min];
309 } else if (w >= ucs_r_jis_table_min && w < ucs_r_jis_table_max) {
310 s = ucs_r_jis_table[w - ucs_r_jis_table_min];
311 }
312
313 if (s == 0) {
314 if (w == 0xA5) { /* YEN SIGN */
315 s = 0x216F; /* FULLWIDTH YEN SIGN */
316 } else if (w == 0xAF || w == 0x203E) {
317 s = 0x2131; /* FULLWIDTH MACRON */
318 } else if (w == 0xFF3C) { /* FULLWIDTH REVERSE SOLIDUS */
319 s = 0x2140;
320 } else if (w == 0x2225) { /* PARALLEL TO */
321 s = 0x2142;
322 } else if (w == 0xFF0D) { /* FULLWIDTH HYPHEN-MINUS */
323 s = 0x215D;
324 } else if (w == 0xFFE0) { /* FULLWIDTH CENT SIGN */
325 s = 0x2171;
326 } else if (w == 0xFFE1) { /* FULLWIDTH POUND SIGN */
327 s = 0x2172;
328 } else if (w == 0xFFE2) { /* FULLWIDTH NOT SIGN */
329 s = 0x224C;
330 } else if (w != 0) {
331 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_sjis);
332 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
333 continue;
334 }
335 } else if (s >= 0x8080) { /* JIS X 0212; not supported */
336 MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_sjis);
337 MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
338 continue;
339 }
340
341 if (s <= 0xFF) {
342 /* Latin/Kana */
343 out = mb_convert_buf_add(out, s);
344 } else {
345 /* Kanji */
346 unsigned int c1 = (s >> 8) & 0xFF, c2 = s & 0xFF, s2;
347 SJIS_ENCODE(c1, c2, s, s2);
348 out = mb_convert_buf_add2(out, s, s2);
349 }
350 }
351
352 MB_CONVERT_BUF_STORE(buf, out, limit);
353 }
354