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 20 dec 2002.
27 *
28 */
29
30 #include "mbfilter.h"
31 #include "mbfilter_utf32.h"
32
33 static const char *mbfl_encoding_utf32_aliases[] = {"utf32", NULL};
34
35 const mbfl_encoding mbfl_encoding_utf32 = {
36 mbfl_no_encoding_utf32,
37 "UTF-32",
38 "UTF-32",
39 (const char *(*)[])&mbfl_encoding_utf32_aliases,
40 NULL,
41 MBFL_ENCTYPE_WCS4BE,
42 &vtbl_utf32_wchar,
43 &vtbl_wchar_utf32
44 };
45
46 const mbfl_encoding mbfl_encoding_utf32be = {
47 mbfl_no_encoding_utf32be,
48 "UTF-32BE",
49 "UTF-32BE",
50 NULL,
51 NULL,
52 MBFL_ENCTYPE_WCS4BE,
53 &vtbl_utf32be_wchar,
54 &vtbl_wchar_utf32be
55 };
56
57 const mbfl_encoding mbfl_encoding_utf32le = {
58 mbfl_no_encoding_utf32le,
59 "UTF-32LE",
60 "UTF-32LE",
61 NULL,
62 NULL,
63 MBFL_ENCTYPE_WCS4LE,
64 &vtbl_utf32le_wchar,
65 &vtbl_wchar_utf32le
66 };
67
68 const struct mbfl_convert_vtbl vtbl_utf32_wchar = {
69 mbfl_no_encoding_utf32,
70 mbfl_no_encoding_wchar,
71 mbfl_filt_conv_common_ctor,
72 NULL,
73 mbfl_filt_conv_utf32_wchar,
74 mbfl_filt_conv_common_flush,
75 NULL,
76 };
77
78 const struct mbfl_convert_vtbl vtbl_wchar_utf32 = {
79 mbfl_no_encoding_wchar,
80 mbfl_no_encoding_utf32,
81 mbfl_filt_conv_common_ctor,
82 NULL,
83 mbfl_filt_conv_wchar_utf32be,
84 mbfl_filt_conv_common_flush,
85 NULL,
86 };
87
88 const struct mbfl_convert_vtbl vtbl_utf32be_wchar = {
89 mbfl_no_encoding_utf32be,
90 mbfl_no_encoding_wchar,
91 mbfl_filt_conv_common_ctor,
92 NULL,
93 mbfl_filt_conv_utf32be_wchar,
94 mbfl_filt_conv_common_flush,
95 NULL,
96 };
97
98 const struct mbfl_convert_vtbl vtbl_wchar_utf32be = {
99 mbfl_no_encoding_wchar,
100 mbfl_no_encoding_utf32be,
101 mbfl_filt_conv_common_ctor,
102 NULL,
103 mbfl_filt_conv_wchar_utf32be,
104 mbfl_filt_conv_common_flush,
105 NULL,
106 };
107
108 const struct mbfl_convert_vtbl vtbl_utf32le_wchar = {
109 mbfl_no_encoding_utf32le,
110 mbfl_no_encoding_wchar,
111 mbfl_filt_conv_common_ctor,
112 NULL,
113 mbfl_filt_conv_utf32le_wchar,
114 mbfl_filt_conv_common_flush,
115 NULL,
116 };
117
118 const struct mbfl_convert_vtbl vtbl_wchar_utf32le = {
119 mbfl_no_encoding_wchar,
120 mbfl_no_encoding_utf32le,
121 mbfl_filt_conv_common_ctor,
122 NULL,
123 mbfl_filt_conv_wchar_utf32le,
124 mbfl_filt_conv_common_flush,
125 NULL,
126 };
127
128 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
129
130 /*
131 * UTF-32 => wchar
132 */
mbfl_filt_conv_utf32_wchar(int c,mbfl_convert_filter * filter)133 int mbfl_filt_conv_utf32_wchar(int c, mbfl_convert_filter *filter)
134 {
135 int n, endian;
136
137 endian = filter->status & 0xff00;
138 switch (filter->status & 0xff) {
139 case 0:
140 if (endian) {
141 n = c & 0xff;
142 } else {
143 n = (c & 0xffu) << 24;
144 }
145 filter->cache = n;
146 filter->status++;
147 break;
148 case 1:
149 if (endian) {
150 n = (c & 0xff) << 8;
151 } else {
152 n = (c & 0xff) << 16;
153 }
154 filter->cache |= n;
155 filter->status++;
156 break;
157 case 2:
158 if (endian) {
159 n = (c & 0xff) << 16;
160 } else {
161 n = (c & 0xff) << 8;
162 }
163 filter->cache |= n;
164 filter->status++;
165 break;
166 default:
167 if (endian) {
168 n = (c & 0xffu) << 24;
169 } else {
170 n = c & 0xff;
171 }
172 n |= filter->cache;
173 if ((n & 0xffff) == 0 && ((n >> 16) & 0xffff) == 0xfffe) {
174 if (endian) {
175 filter->status = 0; /* big-endian */
176 } else {
177 filter->status = 0x100; /* little-endian */
178 }
179 CK((*filter->output_function)(0xfeff, filter->data));
180 } else {
181 filter->status &= ~0xff;
182 if (n < MBFL_WCSPLANE_UTF32MAX && (n < 0xd800 || n > 0xdfff)) {
183 CK((*filter->output_function)(n, filter->data));
184 } else {
185 n = (n & MBFL_WCSGROUP_MASK) | MBFL_WCSGROUP_THROUGH;
186 CK((*filter->output_function)(n, filter->data));
187 }
188 }
189 break;
190 }
191
192 return c;
193 }
194
195 /*
196 * UTF-32BE => wchar
197 */
mbfl_filt_conv_utf32be_wchar(int c,mbfl_convert_filter * filter)198 int mbfl_filt_conv_utf32be_wchar(int c, mbfl_convert_filter *filter)
199 {
200 int n;
201
202 if (filter->status == 0) {
203 filter->status = 1;
204 n = (c & 0xffu) << 24;
205 filter->cache = n;
206 } else if (filter->status == 1) {
207 filter->status = 2;
208 n = (c & 0xff) << 16;
209 filter->cache |= n;
210 } else if (filter->status == 2) {
211 filter->status = 3;
212 n = (c & 0xff) << 8;
213 filter->cache |= n;
214 } else {
215 filter->status = 0;
216 n = (c & 0xff) | filter->cache;
217 if (n < MBFL_WCSPLANE_UTF32MAX && (n < 0xd800 || n > 0xdfff)) {
218 CK((*filter->output_function)(n, filter->data));
219 } else {
220 n = (n & MBFL_WCSGROUP_MASK) | MBFL_WCSGROUP_THROUGH;
221 CK((*filter->output_function)(n, filter->data));
222 }
223 }
224 return c;
225 }
226
227 /*
228 * wchar => UTF-32BE
229 */
mbfl_filt_conv_wchar_utf32be(int c,mbfl_convert_filter * filter)230 int mbfl_filt_conv_wchar_utf32be(int c, mbfl_convert_filter *filter)
231 {
232 if (c >= 0 && c < MBFL_WCSPLANE_UTF32MAX) {
233 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
234 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
235 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
236 CK((*filter->output_function)(c & 0xff, filter->data));
237 } else {
238 CK(mbfl_filt_conv_illegal_output(c, filter));
239 }
240
241 return c;
242 }
243
244 /*
245 * UTF-32LE => wchar
246 */
mbfl_filt_conv_utf32le_wchar(int c,mbfl_convert_filter * filter)247 int mbfl_filt_conv_utf32le_wchar(int c, mbfl_convert_filter *filter)
248 {
249 int n;
250
251 if (filter->status == 0) {
252 filter->status = 1;
253 n = (c & 0xff);
254 filter->cache = n;
255 } else if (filter->status == 1) {
256 filter->status = 2;
257 n = (c & 0xff) << 8;
258 filter->cache |= n;
259 } else if (filter->status == 2) {
260 filter->status = 3;
261 n = (c & 0xff) << 16;
262 filter->cache |= n;
263 } else {
264 filter->status = 0;
265 n = ((c & 0xffu) << 24) | filter->cache;
266 if (n < MBFL_WCSPLANE_UTF32MAX && (n < 0xd800 || n > 0xdfff)) {
267 CK((*filter->output_function)(n, filter->data));
268 } else {
269 n = (n & MBFL_WCSGROUP_MASK) | MBFL_WCSGROUP_THROUGH;
270 CK((*filter->output_function)(n, filter->data));
271 }
272 }
273 return c;
274 }
275
276 /*
277 * wchar => UTF-32LE
278 */
mbfl_filt_conv_wchar_utf32le(int c,mbfl_convert_filter * filter)279 int mbfl_filt_conv_wchar_utf32le(int c, mbfl_convert_filter *filter)
280 {
281 if (c >= 0 && c < MBFL_WCSPLANE_UTF32MAX) {
282 CK((*filter->output_function)(c & 0xff, filter->data));
283 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
284 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
285 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
286 } else {
287 CK(mbfl_filt_conv_illegal_output(c, filter));
288 }
289
290 return c;
291 }
292