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