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.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_utf8.h"
36
37 int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter);
38
39 const unsigned char mblen_table_utf8[] = {
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, 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, 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 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
55 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
56 };
57
58 static const char *mbfl_encoding_utf8_aliases[] = {"utf8", NULL};
59
60 const mbfl_encoding mbfl_encoding_utf8 = {
61 mbfl_no_encoding_utf8,
62 "UTF-8",
63 "UTF-8",
64 (const char *(*)[])&mbfl_encoding_utf8_aliases,
65 mblen_table_utf8,
66 MBFL_ENCTYPE_MBCS,
67 &vtbl_utf8_wchar,
68 &vtbl_wchar_utf8
69 };
70
71 const struct mbfl_identify_vtbl vtbl_identify_utf8 = {
72 mbfl_no_encoding_utf8,
73 mbfl_filt_ident_common_ctor,
74 mbfl_filt_ident_common_dtor,
75 mbfl_filt_ident_utf8
76 };
77
78 const struct mbfl_convert_vtbl vtbl_utf8_wchar = {
79 mbfl_no_encoding_utf8,
80 mbfl_no_encoding_wchar,
81 mbfl_filt_conv_common_ctor,
82 mbfl_filt_conv_common_dtor,
83 mbfl_filt_conv_utf8_wchar,
84 mbfl_filt_conv_utf8_wchar_flush
85 };
86
87 const struct mbfl_convert_vtbl vtbl_wchar_utf8 = {
88 mbfl_no_encoding_wchar,
89 mbfl_no_encoding_utf8,
90 mbfl_filt_conv_common_ctor,
91 mbfl_filt_conv_common_dtor,
92 mbfl_filt_conv_wchar_utf8,
93 mbfl_filt_conv_common_flush
94 };
95
96 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
97
mbfl_filt_put_invalid_char(int c,mbfl_convert_filter * filter)98 int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter)
99 {
100 int w;
101 w = c & MBFL_WCSGROUP_MASK;
102 w |= MBFL_WCSGROUP_THROUGH;
103 filter->status = 0;
104 filter->cache = 0;
105 CK((*filter->output_function)(w, filter->data));
106 return 0;
107 }
108
109
110 /*
111 * UTF-8 => wchar
112 */
mbfl_filt_conv_utf8_wchar(int c,mbfl_convert_filter * filter)113 int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter)
114 {
115 int s, c1;
116
117 retry:
118 switch (filter->status & 0xff) {
119 case 0x00:
120 if (c < 0x80) {
121 CK((*filter->output_function)(c, filter->data));
122 } else if (c >= 0xc2 && c <= 0xdf) { /* 2byte code first char: 0xc2-0xdf */
123 filter->status = 0x10;
124 filter->cache = c & 0x1f;
125 } else if (c >= 0xe0 && c <= 0xef) { /* 3byte code first char: 0xe0-0xef */
126 filter->status = 0x20;
127 filter->cache = c & 0xf;
128 } else if (c >= 0xf0 && c <= 0xf4) { /* 3byte code first char: 0xf0-0xf4 */
129 filter->status = 0x30;
130 filter->cache = c & 0x7;
131 } else {
132 CK(mbfl_filt_put_invalid_char(c, filter));
133 }
134 break;
135 case 0x10: /* 2byte code 2nd char: 0x80-0xbf */
136 case 0x21: /* 3byte code 3rd char: 0x80-0xbf */
137 case 0x32: /* 4byte code 4th char: 0x80-0xbf */
138 filter->status = 0;
139 if (c >= 0x80 && c <= 0xbf) {
140 s = (filter->cache<<6) | (c & 0x3f);
141 filter->cache = 0;
142 CK((*filter->output_function)(s, filter->data));
143 } else {
144 CK(mbfl_filt_put_invalid_char(filter->cache, filter));
145 goto retry;
146 }
147 break;
148 case 0x20: /* 3byte code 2nd char: 0:0xa0-0xbf,D:0x80-9F,1-C,E-F:0x80-0x9f */
149 s = (filter->cache<<6) | (c & 0x3f);
150 c1 = filter->cache & 0xf;
151
152 if ((c >= 0x80 && c <= 0xbf) &&
153 ((c1 == 0x0 && c >= 0xa0) ||
154 (c1 == 0xd && c < 0xa0) ||
155 (c1 > 0x0 && c1 != 0xd))) {
156 filter->cache = s;
157 filter->status++;
158 } else {
159 CK(mbfl_filt_put_invalid_char(filter->cache, filter));
160 goto retry;
161 }
162 break;
163 case 0x30: /* 4byte code 2nd char: 0:0x90-0xbf,1-3:0x80-0xbf,4:0x80-0x8f */
164 s = (filter->cache<<6) | (c & 0x3f);
165 c1 = filter->cache & 0x7;
166
167 if ((c >= 0x80 && c <= 0xbf) &&
168 ((c1 == 0x0 && c >= 0x90) ||
169 (c1 == 0x4 && c < 0x90) ||
170 (c1 > 0x0 && c1 != 0x4))) {
171 filter->cache = s;
172 filter->status++;
173 } else {
174 CK(mbfl_filt_put_invalid_char(filter->cache, filter));
175 goto retry;
176 }
177 break;
178 case 0x31: /* 4byte code 3rd char: 0x80-0xbf */
179 if (c >= 0x80 && c <= 0xbf) {
180 filter->cache = (filter->cache<<6) | (c & 0x3f);
181 filter->status++;
182 } else {
183 CK(mbfl_filt_put_invalid_char(filter->cache, filter));
184 goto retry;
185 }
186 break;
187 default:
188 filter->status = 0;
189 break;
190 }
191
192 return c;
193 }
194
mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter * filter)195 int mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter *filter)
196 {
197 int status, cache;
198
199 status = filter->status;
200 cache = filter->cache;
201
202 filter->status = 0;
203 filter->cache = 0;
204
205 if (status != 0) {
206 CK(mbfl_filt_put_invalid_char(cache, filter));
207 }
208
209 if (filter->flush_function != NULL) {
210 (*filter->flush_function)(filter->data);
211 }
212 return 0;
213 }
214
215 /*
216 * wchar => UTF-8
217 */
mbfl_filt_conv_wchar_utf8(int c,mbfl_convert_filter * filter)218 int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filter *filter)
219 {
220 if (c >= 0 && c < 0x110000) {
221 if (c < 0x80) {
222 CK((*filter->output_function)(c, filter->data));
223 } else if (c < 0x800) {
224 CK((*filter->output_function)(((c >> 6) & 0x1f) | 0xc0, filter->data));
225 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
226 } else if (c < 0x10000) {
227 CK((*filter->output_function)(((c >> 12) & 0x0f) | 0xe0, filter->data));
228 CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
229 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
230 } else {
231 CK((*filter->output_function)(((c >> 18) & 0x07) | 0xf0, filter->data));
232 CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
233 CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
234 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
235 }
236 } else {
237 CK(mbfl_filt_conv_illegal_output(c, filter));
238 }
239
240 return c;
241 }
242
mbfl_filt_ident_utf8(int c,mbfl_identify_filter * filter)243 int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter)
244 {
245 int c1;
246
247 c1 = (filter->status >> 8) & 0xff;
248 filter->status &= 0xff;
249
250 if (c < 0x80) {
251 if (c < 0) {
252 filter->flag = 1; /* bad */
253 } else if (filter->status) {
254 filter->flag = 1; /* bad */
255 }
256 filter->status = 0;
257 } else if (c < 0xc0) {
258 switch (filter->status) {
259 case 0x20: /* 3 byte code 2nd char */
260 if ((c1 == 0x0 && c >= 0xa0) ||
261 (c1 == 0xd && c < 0xa0) ||
262 (c1 > 0x0 && c1 != 0xd)) {
263 filter->status++;
264 } else {
265 filter->flag = 1; /* bad */
266 filter->status = 0;
267 }
268 break;
269 case 0x30: /* 4 byte code 2nd char */
270 if ((c1 == 0x0 && c >= 0x90) ||
271 (c1 > 0x0 && c1 < 0x4) ||
272 (c1 == 0x4 && c < 0x90)) {
273 filter->status++;
274 } else {
275 filter->flag = 1; /* bad */
276 filter->status = 0;
277 }
278 break;
279 case 0x31: /* 4 byte code 3rd char */
280 filter->status++;
281 break;
282 case 0x10: /* 2 byte code 2nd char */
283 case 0x21: /* 3 byte code 3rd char */
284 case 0x32: /* 4 byte code 4th char */
285 filter->status = 0;
286 break;
287 default:
288 filter->flag = 1; /* bad */
289 filter->status = 0;
290 break;
291 }
292 } else if (c < 0xc2) { /* 0xc0,0xc1 */
293 filter->flag = 1; /* bad */
294 filter->status = 0;
295 } else {
296 if (filter->status) {
297 filter->flag = 1; /* bad */
298 }
299 filter->status = 0;
300 if (c < 0xe0) { /* 2 byte code first char */
301 filter->status = 0x10;
302 } else if (c < 0xf0) { /* 3 byte code 1st char */
303 filter->status = 0x20;
304 filter->status |= (c & 0xf) << 8;
305 } else if (c < 0xf5) { /* 4 byte code 1st char */
306 filter->status = 0x30;
307 filter->status |= (c & 0x7) << 8;
308 } else {
309 filter->flag = 1; /* bad */
310 }
311 }
312
313 return c;
314 }
315