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 const unsigned char mblen_table_utf8[] = {
34 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
49 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
50 };
51
52 static const char *mbfl_encoding_utf8_aliases[] = {"utf8", NULL};
53
54 const mbfl_encoding mbfl_encoding_utf8 = {
55 mbfl_no_encoding_utf8,
56 "UTF-8",
57 "UTF-8",
58 mbfl_encoding_utf8_aliases,
59 mblen_table_utf8,
60 0,
61 &vtbl_utf8_wchar,
62 &vtbl_wchar_utf8
63 };
64
65 const struct mbfl_convert_vtbl vtbl_utf8_wchar = {
66 mbfl_no_encoding_utf8,
67 mbfl_no_encoding_wchar,
68 mbfl_filt_conv_common_ctor,
69 NULL,
70 mbfl_filt_conv_utf8_wchar,
71 mbfl_filt_conv_utf8_wchar_flush,
72 NULL,
73 };
74
75 const struct mbfl_convert_vtbl vtbl_wchar_utf8 = {
76 mbfl_no_encoding_wchar,
77 mbfl_no_encoding_utf8,
78 mbfl_filt_conv_common_ctor,
79 NULL,
80 mbfl_filt_conv_wchar_utf8,
81 mbfl_filt_conv_common_flush,
82 NULL,
83 };
84
85 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
86
mbfl_filt_put_invalid_char(mbfl_convert_filter * filter)87 int mbfl_filt_put_invalid_char(mbfl_convert_filter *filter)
88 {
89 filter->status = filter->cache = 0;
90 CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
91 return 0;
92 }
93
mbfl_filt_conv_utf8_wchar(int c,mbfl_convert_filter * filter)94 int mbfl_filt_conv_utf8_wchar(int c, mbfl_convert_filter *filter)
95 {
96 int s, c1;
97
98 retry:
99 switch (filter->status) {
100 case 0x00:
101 if (c < 0x80) {
102 CK((*filter->output_function)(c, filter->data));
103 } else if (c >= 0xc2 && c <= 0xdf) { /* 2byte code first char: 0xc2-0xdf */
104 filter->status = 0x10;
105 filter->cache = c & 0x1f;
106 } else if (c >= 0xe0 && c <= 0xef) { /* 3byte code first char: 0xe0-0xef */
107 filter->status = 0x20;
108 filter->cache = c & 0xf;
109 } else if (c >= 0xf0 && c <= 0xf4) { /* 3byte code first char: 0xf0-0xf4 */
110 filter->status = 0x30;
111 filter->cache = c & 0x7;
112 } else {
113 CK(mbfl_filt_put_invalid_char(filter));
114 }
115 break;
116 case 0x10: /* 2byte code 2nd char: 0x80-0xbf */
117 case 0x21: /* 3byte code 3rd char: 0x80-0xbf */
118 case 0x32: /* 4byte code 4th char: 0x80-0xbf */
119 if (c >= 0x80 && c <= 0xbf) {
120 s = (filter->cache<<6) | (c & 0x3f);
121 filter->status = filter->cache = 0;
122 CK((*filter->output_function)(s, filter->data));
123 } else {
124 CK(mbfl_filt_put_invalid_char(filter));
125 if (c < 0x80 || (c >= 0xc2 && c <= 0xf4)) {
126 goto retry;
127 }
128 }
129 break;
130 case 0x20: /* 3byte code 2nd char: 0:0xa0-0xbf,D:0x80-9F,1-C,E-F:0x80-0x9f */
131 s = (filter->cache<<6) | (c & 0x3f);
132 c1 = filter->cache & 0xf;
133
134 if ((c >= 0x80 && c <= 0xbf) &&
135 ((c1 == 0x0 && c >= 0xa0) ||
136 (c1 == 0xd && c < 0xa0) ||
137 (c1 > 0x0 && c1 != 0xd))) {
138 filter->cache = s;
139 filter->status++;
140 } else {
141 CK(mbfl_filt_put_invalid_char(filter));
142 if (c < 0x80 || (c >= 0xc2 && c <= 0xf4)) {
143 goto retry;
144 }
145 }
146 break;
147 case 0x30: /* 4byte code 2nd char: 0:0x90-0xbf,1-3:0x80-0xbf,4:0x80-0x8f */
148 s = (filter->cache<<6) | (c & 0x3f);
149 c1 = filter->cache & 0x7;
150
151 if ((c >= 0x80 && c <= 0xbf) &&
152 ((c1 == 0x0 && c >= 0x90) ||
153 (c1 == 0x4 && c < 0x90) ||
154 (c1 > 0x0 && c1 != 0x4))) {
155 filter->cache = s;
156 filter->status++;
157 } else {
158 CK(mbfl_filt_put_invalid_char(filter));
159 if (c < 0x80 || (c >= 0xc2 && c <= 0xf4)) {
160 goto retry;
161 }
162 }
163 break;
164 case 0x31: /* 4byte code 3rd char: 0x80-0xbf */
165 if (c >= 0x80 && c <= 0xbf) {
166 filter->cache = (filter->cache<<6) | (c & 0x3f);
167 filter->status++;
168 } else {
169 CK(mbfl_filt_put_invalid_char(filter));
170 if (c < 0x80 || (c >= 0xc2 && c <= 0xf4)) {
171 goto retry;
172 }
173 }
174 break;
175 default:
176 filter->status = 0;
177 break;
178 }
179
180 return 0;
181 }
182
mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter * filter)183 int mbfl_filt_conv_utf8_wchar_flush(mbfl_convert_filter *filter)
184 {
185 if (filter->status) {
186 (*filter->output_function)(MBFL_BAD_INPUT, filter->data);
187 }
188
189 if (filter->flush_function) {
190 (*filter->flush_function)(filter->data);
191 }
192
193 return 0;
194 }
195
mbfl_filt_conv_wchar_utf8(int c,mbfl_convert_filter * filter)196 int mbfl_filt_conv_wchar_utf8(int c, mbfl_convert_filter *filter)
197 {
198 if (c >= 0 && c < 0x110000) {
199 if (c < 0x80) {
200 CK((*filter->output_function)(c, filter->data));
201 } else if (c < 0x800) {
202 CK((*filter->output_function)(((c >> 6) & 0x1f) | 0xc0, filter->data));
203 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
204 } else if (c < 0x10000) {
205 CK((*filter->output_function)(((c >> 12) & 0x0f) | 0xe0, filter->data));
206 CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
207 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
208 } else {
209 CK((*filter->output_function)(((c >> 18) & 0x07) | 0xf0, filter->data));
210 CK((*filter->output_function)(((c >> 12) & 0x3f) | 0x80, filter->data));
211 CK((*filter->output_function)(((c >> 6) & 0x3f) | 0x80, filter->data));
212 CK((*filter->output_function)((c & 0x3f) | 0x80, filter->data));
213 }
214 } else {
215 CK(mbfl_filt_conv_illegal_output(c, filter));
216 }
217
218 return 0;
219 }
220