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 : static int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter);
38 :
39 : static 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 : 2, 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 : 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
55 : 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 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_common_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 : /*
97 : * UTF-8 => wchar
98 : */
99 : int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter)
100 32325 : {
101 : int s;
102 :
103 32325 : if (c < 0x80) {
104 9793 : if (c >= 0) {
105 9793 : CK((*filter->output_function)(c, filter->data));
106 : }
107 9790 : filter->status = 0;
108 22532 : } else if (c < 0xc0) {
109 12120 : switch (filter->status & 0xff) {
110 : case 0x10: /* 2byte code 2nd char */
111 : case 0x21: /* 3byte code 3rd char */
112 : case 0x32: /* 4byte code 4th char */
113 : case 0x43: /* 5byte code 5th char */
114 : case 0x54: /* 6byte code 6th char */
115 10411 : filter->status = 0;
116 10411 : s = filter->cache | (c & 0x3f);
117 10411 : if (s >= 0x80) {
118 10411 : CK((*filter->output_function)(s, filter->data));
119 : }
120 10411 : break;
121 : case 0x20: /* 3byte code 2nd char */
122 : case 0x31: /* 4byte code 3rd char */
123 : case 0x42: /* 5byte code 4th char */
124 : case 0x53: /* 6byte code 5th char */
125 1709 : filter->cache |= ((c & 0x3f) << 6);
126 1709 : filter->status++;
127 1709 : break;
128 : case 0x30: /* 4byte code 2nd char */
129 : case 0x41: /* 5byte code 3rd char */
130 : case 0x52: /* 6byte code 4th char */
131 0 : filter->cache |= ((c & 0x3f) << 12);
132 0 : filter->status++;
133 0 : break;
134 : case 0x40: /* 5byte code 2nd char */
135 : case 0x51: /* 6byte code 3rd char */
136 0 : filter->cache |= ((c & 0x3f) << 18);
137 0 : filter->status++;
138 0 : break;
139 : case 0x50: /* 6byte code 2nd char */
140 0 : filter->cache |= ((c & 0x3f) << 24);
141 0 : filter->status++;
142 0 : break;
143 : default:
144 0 : filter->status = 0;
145 : break;
146 : }
147 10412 : } else if (c < 0xe0) { /* 2byte code first char */
148 8702 : filter->status = 0x10;
149 8702 : filter->cache = (c & 0x1f) << 6;
150 1710 : } else if (c < 0xf0) { /* 3byte code first char */
151 1709 : filter->status = 0x20;
152 1709 : filter->cache = (c & 0xf) << 12;
153 1 : } else if (c < 0xf8) { /* 4byte code first char */
154 0 : filter->status = 0x30;
155 0 : filter->cache = (c & 0x7) << 18;
156 1 : } else if (c < 0xfc) { /* 5byte code first char */
157 0 : filter->status = 0x40;
158 0 : filter->cache = (c & 0x3) << 24;
159 1 : } else if (c < 0xfe) { /* 6 byte code first char */
160 1 : filter->status = 0x50;
161 1 : filter->cache = (c & 0x1) << 30;
162 : } else {
163 0 : filter->status = 0;
164 0 : filter->cache = 0;
165 : }
166 :
167 32322 : return c;
168 : }
169 :
170 : /*
171 : * wchar => UTF-8
172 : */
173 : int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filter *filter)
174 33364 : {
175 66728 : if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) {
176 33364 : if (c < 0x80) {
177 15133 : CK((*filter->output_function)(c, filter->data));
178 18231 : } else if (c < 0x800) {
179 14313 : CK((*filter->output_function)(((c >> 6) & 0x1f) | 0xc0, filter->data));
180 14313 : CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
181 3918 : } else if (c < 0x10000) {
182 3915 : CK((*filter->output_function)(((c >> 12) & 0x0f) | 0xe0, filter->data));
183 3915 : CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
184 3915 : CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
185 3 : } else if (c < 0x200000) {
186 3 : CK((*filter->output_function)(((c >> 18) & 0x07) | 0xf0, filter->data));
187 3 : CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
188 3 : CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
189 3 : CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
190 0 : } else if (c < 0x4000000) {
191 0 : CK((*filter->output_function)(((c >> 24) & 0x03) | 0xf8, filter->data));
192 0 : CK((*filter->output_function)(((c >> 18) & 0x3f) | 0x80, filter->data));
193 0 : CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
194 0 : CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
195 0 : CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
196 : } else {
197 0 : CK((*filter->output_function)(((c >> 30) & 0x01) | 0xfc, filter->data));
198 0 : CK((*filter->output_function)(((c >> 24) & 0x3f) | 0x80, filter->data));
199 0 : CK((*filter->output_function)(((c >> 18) & 0x3f) | 0x80, filter->data));
200 0 : CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
201 0 : CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
202 0 : CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
203 : }
204 : } else {
205 0 : if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
206 0 : CK(mbfl_filt_conv_illegal_output(c, filter));
207 : }
208 : }
209 :
210 33364 : return c;
211 : }
212 :
213 : static int mbfl_filt_ident_utf8(int c, mbfl_identify_filter *filter)
214 298 : {
215 298 : if (c < 0x80) {
216 262 : if (c < 0) {
217 0 : filter->flag = 1; /* bad */
218 262 : } else if (filter->status) {
219 1 : filter->flag = 1; /* bad */
220 : }
221 262 : filter->status = 0;
222 36 : } else if (c < 0xc0) {
223 2 : switch (filter->status) {
224 : case 0x20: /* 3 byte code 2nd char */
225 : case 0x30: /* 4 byte code 2nd char */
226 : case 0x31: /* 4 byte code 3rd char */
227 : case 0x40: /* 5 byte code 2nd char */
228 : case 0x41: /* 5 byte code 3rd char */
229 : case 0x42: /* 5 byte code 4th char */
230 : case 0x50: /* 6 byte code 2nd char */
231 : case 0x51: /* 6 byte code 3rd char */
232 : case 0x52: /* 6 byte code 4th char */
233 : case 0x53: /* 6 byte code 5th char */
234 0 : filter->status++;
235 0 : break;
236 : case 0x10: /* 2 byte code 2nd char */
237 : case 0x21: /* 3 byte code 3rd char */
238 : case 0x32: /* 4 byte code 4th char */
239 : case 0x43: /* 5 byte code 5th char */
240 : case 0x54: /* 6 byte code 6th char */
241 0 : filter->status = 0;
242 0 : break;
243 : default:
244 2 : filter->flag = 1; /* bad */
245 2 : filter->status = 0;
246 : break;
247 : }
248 : } else {
249 34 : if (filter->status) {
250 16 : filter->flag = 1; /* bad */
251 : }
252 34 : filter->status = 0;
253 34 : if (c < 0xe0) { /* 2 byte code first char */
254 18 : filter->status = 0x10;
255 16 : } else if (c < 0xf0) { /* 3 byte code 1st char */
256 0 : filter->status = 0x20;
257 16 : } else if (c < 0xf8) { /* 4 byte code 1st char */
258 0 : filter->status = 0x30;
259 16 : } else if (c < 0xfc) { /* 5 byte code 1st char */
260 0 : filter->status = 0x40;
261 16 : } else if (c < 0xfe) { /* 6 byte code 1st char */
262 16 : filter->status = 0x50;
263 : } else {
264 0 : filter->flag = 1; /* bad */
265 : }
266 : }
267 :
268 298 : return c;
269 : }
|