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