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 int mbfl_filt_conv_utf32_wchar_flush(mbfl_convert_filter *filter);
34 static size_t mb_utf32_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
35 static size_t mb_utf32be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
36 static void mb_wchar_to_utf32be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
37 static size_t mb_utf32le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
38 static void mb_wchar_to_utf32le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
39 
40 static const char *mbfl_encoding_utf32_aliases[] = {"utf32", NULL};
41 
42 const mbfl_encoding mbfl_encoding_utf32 = {
43 	mbfl_no_encoding_utf32,
44 	"UTF-32",
45 	"UTF-32",
46 	mbfl_encoding_utf32_aliases,
47 	NULL,
48 	MBFL_ENCTYPE_WCS4,
49 	&vtbl_utf32_wchar,
50 	&vtbl_wchar_utf32,
51 	mb_utf32_to_wchar,
52 	mb_wchar_to_utf32be,
53 	NULL
54 };
55 
56 const mbfl_encoding mbfl_encoding_utf32be = {
57 	mbfl_no_encoding_utf32be,
58 	"UTF-32BE",
59 	"UTF-32BE",
60 	NULL,
61 	NULL,
62 	MBFL_ENCTYPE_WCS4,
63 	&vtbl_utf32be_wchar,
64 	&vtbl_wchar_utf32be,
65 	mb_utf32be_to_wchar,
66 	mb_wchar_to_utf32be,
67 	NULL
68 };
69 
70 const mbfl_encoding mbfl_encoding_utf32le = {
71 	mbfl_no_encoding_utf32le,
72 	"UTF-32LE",
73 	"UTF-32LE",
74 	NULL,
75 	NULL,
76 	MBFL_ENCTYPE_WCS4,
77 	&vtbl_utf32le_wchar,
78 	&vtbl_wchar_utf32le,
79 	mb_utf32le_to_wchar,
80 	mb_wchar_to_utf32le,
81 	NULL
82 };
83 
84 const struct mbfl_convert_vtbl vtbl_utf32_wchar = {
85 	mbfl_no_encoding_utf32,
86 	mbfl_no_encoding_wchar,
87 	mbfl_filt_conv_common_ctor,
88 	NULL,
89 	mbfl_filt_conv_utf32_wchar,
90 	mbfl_filt_conv_utf32_wchar_flush,
91 	NULL,
92 };
93 
94 const struct mbfl_convert_vtbl vtbl_wchar_utf32 = {
95 	mbfl_no_encoding_wchar,
96 	mbfl_no_encoding_utf32,
97 	mbfl_filt_conv_common_ctor,
98 	NULL,
99 	mbfl_filt_conv_wchar_utf32be,
100 	mbfl_filt_conv_common_flush,
101 	NULL,
102 };
103 
104 const struct mbfl_convert_vtbl vtbl_utf32be_wchar = {
105 	mbfl_no_encoding_utf32be,
106 	mbfl_no_encoding_wchar,
107 	mbfl_filt_conv_common_ctor,
108 	NULL,
109 	mbfl_filt_conv_utf32be_wchar,
110 	mbfl_filt_conv_utf32_wchar_flush,
111 	NULL,
112 };
113 
114 const struct mbfl_convert_vtbl vtbl_wchar_utf32be = {
115 	mbfl_no_encoding_wchar,
116 	mbfl_no_encoding_utf32be,
117 	mbfl_filt_conv_common_ctor,
118 	NULL,
119 	mbfl_filt_conv_wchar_utf32be,
120 	mbfl_filt_conv_common_flush,
121 	NULL,
122 };
123 
124 const struct mbfl_convert_vtbl vtbl_utf32le_wchar = {
125 	mbfl_no_encoding_utf32le,
126 	mbfl_no_encoding_wchar,
127 	mbfl_filt_conv_common_ctor,
128 	NULL,
129 	mbfl_filt_conv_utf32le_wchar,
130 	mbfl_filt_conv_utf32_wchar_flush,
131 	NULL,
132 };
133 
134 const struct mbfl_convert_vtbl vtbl_wchar_utf32le = {
135 	mbfl_no_encoding_wchar,
136 	mbfl_no_encoding_utf32le,
137 	mbfl_filt_conv_common_ctor,
138 	NULL,
139 	mbfl_filt_conv_wchar_utf32le,
140 	mbfl_filt_conv_common_flush,
141 	NULL,
142 };
143 
144 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
145 
emit_char_if_valid(int n,mbfl_convert_filter * filter)146 static int emit_char_if_valid(int n, mbfl_convert_filter *filter)
147 {
148 	if (n >= 0 && n < MBFL_WCSPLANE_UTF32MAX && (n < 0xD800 || n > 0xDFFF)) {
149 		CK((*filter->output_function)(n, filter->data));
150 	} else {
151 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
152 	}
153 	return 0;
154 }
155 
mbfl_filt_conv_utf32_wchar(int c,mbfl_convert_filter * filter)156 int mbfl_filt_conv_utf32_wchar(int c, mbfl_convert_filter *filter)
157 {
158 	if (filter->status < 3) {
159 		filter->cache = (filter->cache << 8) | (c & 0xFF);
160 		filter->status++;
161 	} else {
162 		int n = ((unsigned int)filter->cache << 8) | (c & 0xFF);
163 		filter->cache = filter->status = 0;
164 
165 		if (n == 0xFFFE0000) {
166 			/* Found a little-endian byte order mark */
167 			filter->filter_function = mbfl_filt_conv_utf32le_wchar;
168 		} else {
169 			filter->filter_function = mbfl_filt_conv_utf32be_wchar;
170 			if (n != 0xFEFF) {
171 				CK(emit_char_if_valid(n, filter));
172 			}
173 		}
174 	}
175 
176 	return 0;
177 }
178 
mbfl_filt_conv_utf32be_wchar(int c,mbfl_convert_filter * filter)179 int mbfl_filt_conv_utf32be_wchar(int c, mbfl_convert_filter *filter)
180 {
181 	if (filter->status < 3) {
182 		filter->cache = (filter->cache << 8) | (c & 0xFF);
183 		filter->status++;
184 	} else {
185 		int n = ((unsigned int)filter->cache << 8) | (c & 0xFF);
186 		filter->cache = filter->status = 0;
187 		CK(emit_char_if_valid(n, filter));
188 	}
189 	return 0;
190 }
191 
mbfl_filt_conv_wchar_utf32be(int c,mbfl_convert_filter * filter)192 int mbfl_filt_conv_wchar_utf32be(int c, mbfl_convert_filter *filter)
193 {
194 	if (c >= 0 && c < MBFL_WCSPLANE_UTF32MAX) {
195 		CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
196 		CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
197 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
198 		CK((*filter->output_function)(c & 0xff, filter->data));
199 	} else {
200 		CK(mbfl_filt_conv_illegal_output(c, filter));
201 	}
202 
203 	return 0;
204 }
205 
mbfl_filt_conv_utf32le_wchar(int c,mbfl_convert_filter * filter)206 int mbfl_filt_conv_utf32le_wchar(int c, mbfl_convert_filter *filter)
207 {
208 	if (filter->status < 3) {
209 		filter->cache |= ((c & 0xFFU) << (8 * filter->status));
210 		filter->status++;
211 	} else {
212 		int n = ((c & 0xFFU) << 24) | filter->cache;
213 		filter->cache = filter->status = 0;
214 		CK(emit_char_if_valid(n, filter));
215 	}
216 	return 0;
217 }
218 
mbfl_filt_conv_wchar_utf32le(int c,mbfl_convert_filter * filter)219 int mbfl_filt_conv_wchar_utf32le(int c, mbfl_convert_filter *filter)
220 {
221 	if (c >= 0 && c < MBFL_WCSPLANE_UTF32MAX) {
222 		CK((*filter->output_function)(c & 0xff, filter->data));
223 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
224 		CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
225 		CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
226 	} else {
227 		CK(mbfl_filt_conv_illegal_output(c, filter));
228 	}
229 
230 	return 0;
231 }
232 
mbfl_filt_conv_utf32_wchar_flush(mbfl_convert_filter * filter)233 static int mbfl_filt_conv_utf32_wchar_flush(mbfl_convert_filter *filter)
234 {
235 	if (filter->status) {
236 		/* Input string was truncated */
237 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
238 	}
239 	filter->cache = filter->status = 0;
240 
241 	if (filter->flush_function) {
242 		(*filter->flush_function)(filter->data);
243 	}
244 
245 	return 0;
246 }
247 
248 #define DETECTED_BE 1
249 #define DETECTED_LE 2
250 
mb_utf32_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)251 static size_t mb_utf32_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
252 {
253 	if (*state == DETECTED_BE) {
254 		return mb_utf32be_to_wchar(in, in_len, buf, bufsize, NULL);
255 	} else if (*state == DETECTED_LE) {
256 		return mb_utf32le_to_wchar(in, in_len, buf, bufsize, NULL);
257 	} else if (*in_len >= 4) {
258 		unsigned char *p = *in;
259 		uint32_t c1 = *p++;
260 		uint32_t c2 = *p++;
261 		uint32_t c3 = *p++;
262 		uint32_t c4 = *p++;
263 		uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
264 
265 		if (w == 0xFFFE0000) {
266 			/* Little-endian BOM */
267 			*in = p;
268 			*in_len -= 4;
269 			*state = DETECTED_LE;
270 			return mb_utf32le_to_wchar(in, in_len, buf, bufsize, NULL);
271 		} else if (w == 0xFEFF) {
272 			/* Big-endian BOM; don't send it to output */
273 			*in = p;
274 			*in_len -= 4;
275 		}
276 	}
277 
278 	*state = DETECTED_BE;
279 	return mb_utf32be_to_wchar(in, in_len, buf, bufsize, NULL);
280 }
281 
mb_utf32be_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)282 static size_t mb_utf32be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
283 {
284 	unsigned char *p = *in, *e = p + (*in_len & ~3);
285 	uint32_t *out = buf, *limit = buf + bufsize;
286 
287 	while (p < e && out < limit) {
288 		uint32_t c1 = *p++;
289 		uint32_t c2 = *p++;
290 		uint32_t c3 = *p++;
291 		uint32_t c4 = *p++;
292 		uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
293 
294 		if (w < MBFL_WCSPLANE_UTF32MAX && (w < 0xD800 || w > 0xDFFF)) {
295 			*out++ = w;
296 		} else {
297 			*out++ = MBFL_BAD_INPUT;
298 		}
299 	}
300 
301 	if (p == e && (*in_len & 0x3) && out < limit) {
302 		/* There are 1-3 trailing bytes, which shouldn't be there */
303 		*out++ = MBFL_BAD_INPUT;
304 		p = *in + *in_len;
305 	}
306 
307 	*in_len -= (p - *in);
308 	*in = p;
309 	return out - buf;
310 }
311 
mb_wchar_to_utf32be(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)312 static void mb_wchar_to_utf32be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
313 {
314 	unsigned char *out, *limit;
315 	MB_CONVERT_BUF_LOAD(buf, out, limit);
316 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
317 
318 	while (len--) {
319 		uint32_t w = *in++;
320 		if (w < MBFL_WCSPLANE_UTF32MAX) {
321 			out = mb_convert_buf_add4(out, (w >> 24) & 0xFF, (w >> 16) & 0xFF, (w >> 8) & 0xFF, w & 0xFF);
322 		} else {
323 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_utf32be);
324 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
325 		}
326 	}
327 
328 	MB_CONVERT_BUF_STORE(buf, out, limit);
329 }
330 
mb_utf32le_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)331 static size_t mb_utf32le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
332 {
333 	unsigned char *p = *in, *e = p + (*in_len & ~3);
334 	uint32_t *out = buf, *limit = buf + bufsize;
335 
336 	while (p < e && out < limit) {
337 		uint32_t c1 = *p++;
338 		uint32_t c2 = *p++;
339 		uint32_t c3 = *p++;
340 		uint32_t c4 = *p++;
341 		uint32_t w = (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
342 
343 		if (w < MBFL_WCSPLANE_UTF32MAX && (w < 0xD800 || w > 0xDFFF)) {
344 			*out++ = w;
345 		} else {
346 			*out++ = MBFL_BAD_INPUT;
347 		}
348 	}
349 
350 	if (p == e && (*in_len & 0x3) && out < limit) {
351 		/* There are 1-3 trailing bytes, which shouldn't be there */
352 		*out++ = MBFL_BAD_INPUT;
353 		p = *in + *in_len;
354 	}
355 
356 	*in_len -= (p - *in);
357 	*in = p;
358 	return out - buf;
359 }
360 
mb_wchar_to_utf32le(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)361 static void mb_wchar_to_utf32le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
362 {
363 	unsigned char *out, *limit;
364 	MB_CONVERT_BUF_LOAD(buf, out, limit);
365 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
366 
367 	while (len--) {
368 		uint32_t w = *in++;
369 		if (w < MBFL_WCSPLANE_UTF32MAX) {
370 			out = mb_convert_buf_add4(out, w & 0xFF, (w >> 8) & 0xFF, (w >> 16) & 0xFF, (w >> 24) & 0xFF);
371 		} else {
372 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_utf32le);
373 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
374 		}
375 	}
376 
377 	MB_CONVERT_BUF_STORE(buf, out, limit);
378 }
379