This source file includes following definitions.
- mbfl_filt_put_invalid_char
- mbfl_filt_conv_utf8_wchar
- mbfl_filt_conv_utf8_wchar_flush
- mbfl_filt_conv_wchar_utf8
- mbfl_filt_ident_utf8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 };
68
69 const struct mbfl_identify_vtbl vtbl_identify_utf8 = {
70 mbfl_no_encoding_utf8,
71 mbfl_filt_ident_common_ctor,
72 mbfl_filt_ident_common_dtor,
73 mbfl_filt_ident_utf8
74 };
75
76 const struct mbfl_convert_vtbl vtbl_utf8_wchar = {
77 mbfl_no_encoding_utf8,
78 mbfl_no_encoding_wchar,
79 mbfl_filt_conv_common_ctor,
80 mbfl_filt_conv_common_dtor,
81 mbfl_filt_conv_utf8_wchar,
82 mbfl_filt_conv_utf8_wchar_flush
83 };
84
85 const struct mbfl_convert_vtbl vtbl_wchar_utf8 = {
86 mbfl_no_encoding_wchar,
87 mbfl_no_encoding_utf8,
88 mbfl_filt_conv_common_ctor,
89 mbfl_filt_conv_common_dtor,
90 mbfl_filt_conv_wchar_utf8,
91 mbfl_filt_conv_common_flush
92 };
93
94 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
95
96 int mbfl_filt_put_invalid_char(int c, mbfl_convert_filter *filter)
97 {
98 int w;
99 w = c & MBFL_WCSGROUP_MASK;
100 w |= MBFL_WCSGROUP_THROUGH;
101 filter->status = 0;
102 filter->cache = 0;
103 CK((*filter->output_function)(w, filter->data));
104 }
105
106
107
108
109
110 int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter)
111 {
112 int s, c1, w = 0, flag = 0;
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) {
120 filter->status = 0x10;
121 filter->cache = c & 0x1f;
122 } else if (c >= 0xe0 && c <= 0xef) {
123 filter->status = 0x20;
124 filter->cache = c & 0xf;
125 } else if (c >= 0xf0 && c <= 0xf4) {
126 filter->status = 0x30;
127 filter->cache = c & 0x7;
128 } else {
129 mbfl_filt_put_invalid_char(c, filter);
130 }
131 break;
132 case 0x10:
133 case 0x21:
134 case 0x32:
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 mbfl_filt_put_invalid_char(filter->cache, filter);
142 goto retry;
143 }
144 break;
145 case 0x20:
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 mbfl_filt_put_invalid_char(filter->cache, filter);
157 goto retry;
158 }
159 break;
160 case 0x30:
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 mbfl_filt_put_invalid_char(filter->cache, filter);
172 goto retry;
173 }
174 break;
175 case 0x31:
176 if (c >= 0x80 && c <= 0xbf) {
177 filter->cache = (filter->cache<<6) | (c & 0x3f);
178 filter->status++;
179 } else {
180 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
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 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
214
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 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
235 CK(mbfl_filt_conv_illegal_output(c, filter));
236 }
237 }
238
239 return c;
240 }
241
242 int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter)
243 {
244 int c1;
245
246 c1 = (filter->status >> 8) & 0xff;
247 filter->status &= 0xff;
248
249 if (c < 0x80) {
250 if (c < 0) {
251 filter->flag = 1;
252 } else if (filter->status) {
253 filter->flag = 1;
254 }
255 filter->status = 0;
256 } else if (c < 0xc0) {
257 switch (filter->status) {
258 case 0x20:
259 if ((c1 == 0x0 && c >= 0xa0) ||
260 (c1 == 0xd && c < 0xa0) ||
261 (c1 > 0x0 && c1 != 0xd)) {
262 filter->status++;
263 } else {
264 filter->flag = 1;
265 filter->status = 0;
266 }
267 break;
268 case 0x30:
269 if ((c1 == 0x0 && c >= 0x90) ||
270 (c1 > 0x0 && c1 < 0x4) ||
271 (c1 == 0x4 && c < 0x90)) {
272 filter->status++;
273 } else {
274 filter->flag = 1;
275 filter->status = 0;
276 }
277 break;
278 case 0x31:
279 filter->status++;
280 break;
281 case 0x10:
282 case 0x21:
283 case 0x32:
284 filter->status = 0;
285 break;
286 default:
287 filter->flag = 1;
288 filter->status = 0;
289 break;
290 }
291 } else if (c < 0xc2) {
292 filter->flag = 1;
293 filter->status = 0;
294 } else {
295 if (filter->status) {
296 filter->flag = 1;
297 }
298 filter->status = 0;
299 if (c < 0xe0) {
300 filter->status = 0x10;
301 } else if (c < 0xf0) {
302 filter->status = 0x20;
303 filter->status |= (c & 0xf) << 8;
304 } else if (c < 0xf5) {
305 filter->status = 0x30;
306 filter->status |= (c & 0x7) << 8;
307 } else {
308 filter->flag = 1;
309 }
310 }
311
312 return c;
313 }