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