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 file 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_ucs4.h"
32 
33 static size_t mb_ucs4_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
34 static size_t mb_ucs4be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
35 static void mb_wchar_to_ucs4be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
36 static size_t mb_ucs4le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
37 static void mb_wchar_to_ucs4le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
38 
39 static const char *mbfl_encoding_ucs4_aliases[] = {"ISO-10646-UCS-4", "UCS4", NULL};
40 
41 /* This library historically had encodings called 'byte4be' and 'byte4le'
42  * which were almost identical to UCS-4
43  * Maintain minimal support by aliasing to UCS-4 */
44 static const char *mbfl_encoding_ucs4be_aliases[] = {"byte4be", NULL};
45 static const char *mbfl_encoding_ucs4le_aliases[] = {"byte4le", NULL};
46 
47 static int mbfl_filt_conv_ucs4_wchar_flush(mbfl_convert_filter *filter);
48 
49 const mbfl_encoding mbfl_encoding_ucs4 = {
50 	mbfl_no_encoding_ucs4,
51 	"UCS-4",
52 	"UCS-4",
53 	mbfl_encoding_ucs4_aliases,
54 	NULL,
55 	MBFL_ENCTYPE_WCS4,
56 	&vtbl_ucs4_wchar,
57 	&vtbl_wchar_ucs4,
58 	mb_ucs4_to_wchar,
59 	mb_wchar_to_ucs4be,
60 	NULL,
61 	NULL,
62 };
63 
64 const mbfl_encoding mbfl_encoding_ucs4be = {
65 	mbfl_no_encoding_ucs4be,
66 	"UCS-4BE",
67 	"UCS-4BE",
68 	mbfl_encoding_ucs4be_aliases,
69 	NULL,
70 	MBFL_ENCTYPE_WCS4,
71 	&vtbl_ucs4be_wchar,
72 	&vtbl_wchar_ucs4be,
73 	mb_ucs4be_to_wchar,
74 	mb_wchar_to_ucs4be,
75 	NULL,
76 	NULL,
77 };
78 
79 const mbfl_encoding mbfl_encoding_ucs4le = {
80 	mbfl_no_encoding_ucs4le,
81 	"UCS-4LE",
82 	"UCS-4LE",
83 	mbfl_encoding_ucs4le_aliases,
84 	NULL,
85 	MBFL_ENCTYPE_WCS4,
86 	&vtbl_ucs4le_wchar,
87 	&vtbl_wchar_ucs4le,
88 	mb_ucs4le_to_wchar,
89 	mb_wchar_to_ucs4le,
90 	NULL,
91 	NULL,
92 };
93 
94 const struct mbfl_convert_vtbl vtbl_ucs4_wchar = {
95 	mbfl_no_encoding_ucs4,
96 	mbfl_no_encoding_wchar,
97 	mbfl_filt_conv_common_ctor,
98 	NULL,
99 	mbfl_filt_conv_ucs4_wchar,
100 	mbfl_filt_conv_ucs4_wchar_flush,
101 	NULL,
102 };
103 
104 const struct mbfl_convert_vtbl vtbl_wchar_ucs4 = {
105 	mbfl_no_encoding_wchar,
106 	mbfl_no_encoding_ucs4,
107 	mbfl_filt_conv_common_ctor,
108 	NULL,
109 	mbfl_filt_conv_wchar_ucs4be,
110 	mbfl_filt_conv_common_flush,
111 	NULL,
112 };
113 
114 const struct mbfl_convert_vtbl vtbl_ucs4be_wchar = {
115 	mbfl_no_encoding_ucs4be,
116 	mbfl_no_encoding_wchar,
117 	mbfl_filt_conv_common_ctor,
118 	NULL,
119 	mbfl_filt_conv_ucs4be_wchar,
120 	mbfl_filt_conv_ucs4_wchar_flush,
121 	NULL,
122 };
123 
124 const struct mbfl_convert_vtbl vtbl_wchar_ucs4be = {
125 	mbfl_no_encoding_wchar,
126 	mbfl_no_encoding_ucs4be,
127 	mbfl_filt_conv_common_ctor,
128 	NULL,
129 	mbfl_filt_conv_wchar_ucs4be,
130 	mbfl_filt_conv_common_flush,
131 	NULL,
132 };
133 
134 const struct mbfl_convert_vtbl vtbl_ucs4le_wchar = {
135 	mbfl_no_encoding_ucs4le,
136 	mbfl_no_encoding_wchar,
137 	mbfl_filt_conv_common_ctor,
138 	NULL,
139 	mbfl_filt_conv_ucs4le_wchar,
140 	mbfl_filt_conv_ucs4_wchar_flush,
141 	NULL,
142 };
143 
144 const struct mbfl_convert_vtbl vtbl_wchar_ucs4le = {
145 	mbfl_no_encoding_wchar,
146 	mbfl_no_encoding_ucs4le,
147 	mbfl_filt_conv_common_ctor,
148 	NULL,
149 	mbfl_filt_conv_wchar_ucs4le,
150 	mbfl_filt_conv_common_flush,
151 	NULL,
152 };
153 
154 
155 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
156 
157 /*
158  * UCS-4 => wchar
159  */
mbfl_filt_conv_ucs4_wchar(int c,mbfl_convert_filter * filter)160 int mbfl_filt_conv_ucs4_wchar(int c, mbfl_convert_filter *filter)
161 {
162 	int n, endian;
163 
164 	endian = filter->status & 0xff00;
165 	switch (filter->status & 0xff) {
166 	case 0:
167 		if (endian) {
168 			n = c & 0xff;
169 		} else {
170 			n = (c & 0xffu) << 24;
171 		}
172 		filter->cache = n;
173 		filter->status++;
174 		break;
175 	case 1:
176 		if (endian) {
177 			n = (c & 0xff) << 8;
178 		} else {
179 			n = (c & 0xff) << 16;
180 		}
181 		filter->cache |= n;
182 		filter->status++;
183 		break;
184 	case 2:
185 		if (endian) {
186 			n = (c & 0xff) << 16;
187 		} else {
188 			n = (c & 0xff) << 8;
189 		}
190 		filter->cache |= n;
191 		filter->status++;
192 		break;
193 	default:
194 		if (endian) {
195 			n = (c & 0xffu) << 24;
196 		} else {
197 			n = c & 0xff;
198 		}
199 		n |= filter->cache;
200 		filter->status &= ~0xff;
201 		if ((n & 0xffff) == 0 && ((n >> 16) & 0xffff) == 0xfffe) {
202 			if (endian) {
203 				filter->status = 0;		/* big-endian */
204 			} else {
205 				filter->status = 0x100;		/* little-endian */
206 			}
207 		} else if (n != 0xfeff) {
208 			CK((*filter->output_function)(n, filter->data));
209 		}
210 		break;
211 	}
212 
213 	return 0;
214 }
215 
216 /*
217  * UCS-4BE => wchar
218  */
mbfl_filt_conv_ucs4be_wchar(int c,mbfl_convert_filter * filter)219 int mbfl_filt_conv_ucs4be_wchar(int c, mbfl_convert_filter *filter)
220 {
221 	int n;
222 
223 	if (filter->status == 0) {
224 		filter->status = 1;
225 		n = (c & 0xffu) << 24;
226 		filter->cache = n;
227 	} else if (filter->status == 1) {
228 		filter->status = 2;
229 		n = (c & 0xff) << 16;
230 		filter->cache |= n;
231 	} else if (filter->status == 2) {
232 		filter->status = 3;
233 		n = (c & 0xff) << 8;
234 		filter->cache |= n;
235 	} else {
236 		filter->status = 0;
237 		n = (c & 0xff) | filter->cache;
238 		CK((*filter->output_function)(n, filter->data));
239 	}
240 	return 0;
241 }
242 
243 /*
244  * wchar => UCS-4BE
245  */
mbfl_filt_conv_wchar_ucs4be(int c,mbfl_convert_filter * filter)246 int mbfl_filt_conv_wchar_ucs4be(int c, mbfl_convert_filter *filter)
247 {
248 	if (c != MBFL_BAD_INPUT) {
249 		CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
250 		CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
251 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
252 		CK((*filter->output_function)(c & 0xff, filter->data));
253 	} else {
254 		CK(mbfl_filt_conv_illegal_output(c, filter));
255 	}
256 
257 	return 0;
258 }
259 
260 /*
261  * UCS-4LE => wchar
262  */
mbfl_filt_conv_ucs4le_wchar(int c,mbfl_convert_filter * filter)263 int mbfl_filt_conv_ucs4le_wchar(int c, mbfl_convert_filter *filter)
264 {
265 	int n;
266 
267 	if (filter->status == 0) {
268 		filter->status = 1;
269 		n = (c & 0xff);
270 		filter->cache = n;
271 	} else if (filter->status == 1) {
272 		filter->status = 2;
273 		n = (c & 0xff) << 8;
274 		filter->cache |= n;
275 	} else if (filter->status == 2) {
276 		filter->status = 3;
277 		n = (c & 0xff) << 16;
278 		filter->cache |= n;
279 	} else {
280 		filter->status = 0;
281 		n = ((c & 0xffu) << 24) | filter->cache;
282 		CK((*filter->output_function)(n, filter->data));
283 	}
284 	return 0;
285 }
286 
287 /*
288  * wchar => UCS-4LE
289  */
mbfl_filt_conv_wchar_ucs4le(int c,mbfl_convert_filter * filter)290 int mbfl_filt_conv_wchar_ucs4le(int c, mbfl_convert_filter *filter)
291 {
292 	if (c != MBFL_BAD_INPUT) {
293 		CK((*filter->output_function)(c & 0xff, filter->data));
294 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
295 		CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
296 		CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
297 	} else {
298 		CK(mbfl_filt_conv_illegal_output(c, filter));
299 	}
300 
301 	return 0;
302 }
303 
mbfl_filt_conv_ucs4_wchar_flush(mbfl_convert_filter * filter)304 static int mbfl_filt_conv_ucs4_wchar_flush(mbfl_convert_filter *filter)
305 {
306 	if (filter->status & 0xF) {
307 		/* Input string was truncated */
308 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
309 	}
310 	filter->status = 0;
311 
312 	if (filter->flush_function) {
313 		(*filter->flush_function)(filter->data);
314 	}
315 
316 	return 0;
317 }
318 
319 #define DETECTED_BE 1
320 #define DETECTED_LE 2
321 
mb_ucs4_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)322 static size_t mb_ucs4_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
323 {
324 	if (*state == DETECTED_BE) {
325 		return mb_ucs4be_to_wchar(in, in_len, buf, bufsize, NULL);
326 	} else if (*state == DETECTED_LE) {
327 		return mb_ucs4le_to_wchar(in, in_len, buf, bufsize, NULL);
328 	} else if (*in_len >= 4) {
329 		unsigned char *p = *in;
330 		uint32_t c1 = *p++;
331 		uint32_t c2 = *p++;
332 		uint32_t c3 = *p++;
333 		uint32_t c4 = *p++;
334 		uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
335 
336 		if (w == 0xFFFE0000) {
337 			/* Little-endian BOM */
338 			*in = p;
339 			*in_len -= 4;
340 			*state = DETECTED_LE;
341 			return mb_ucs4le_to_wchar(in, in_len, buf, bufsize, NULL);
342 		} else if (w == 0xFEFF) {
343 			/* Big-endian BOM; don't send it to output */
344 			*in = p;
345 			*in_len -= 4;
346 		}
347 	}
348 
349 	*state = DETECTED_BE;
350 	return mb_ucs4be_to_wchar(in, in_len, buf, bufsize, NULL);
351 }
352 
mb_ucs4be_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)353 static size_t mb_ucs4be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
354 {
355 	unsigned char *p = *in, *e = p + (*in_len & ~3);
356 	uint32_t *out = buf, *limit = buf + bufsize;
357 
358 	while (p < e && out < limit) {
359 		uint32_t c1 = *p++;
360 		uint32_t c2 = *p++;
361 		uint32_t c3 = *p++;
362 		uint32_t c4 = *p++;
363 		uint32_t w = (c1 << 24) | (c2 << 16) | (c3 << 8) | c4;
364 		*out++ = w;
365 	}
366 
367 	if (p == e && (*in_len & 0x3) && out < limit) {
368 		/* There are 1-3 trailing bytes, which shouldn't be there */
369 		*out++ = MBFL_BAD_INPUT;
370 		p = *in + *in_len;
371 	}
372 
373 	*in_len -= (p - *in);
374 	*in = p;
375 	return out - buf;
376 }
377 
mb_wchar_to_ucs4be(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)378 static void mb_wchar_to_ucs4be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
379 {
380 	unsigned char *out, *limit;
381 	MB_CONVERT_BUF_LOAD(buf, out, limit);
382 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
383 
384 	while (len--) {
385 		uint32_t w = *in++;
386 		if (w != MBFL_BAD_INPUT) {
387 			out = mb_convert_buf_add4(out, (w >> 24) & 0xFF, (w >> 16) & 0xFF, (w >> 8) & 0xFF, w & 0xFF);
388 		} else {
389 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs4be);
390 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
391 		}
392 	}
393 
394 	MB_CONVERT_BUF_STORE(buf, out, limit);
395 }
396 
mb_ucs4le_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)397 static size_t mb_ucs4le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
398 {
399 	unsigned char *p = *in, *e = p + (*in_len & ~3);
400 	uint32_t *out = buf, *limit = buf + bufsize;
401 
402 	while (p < e && out < limit) {
403 		uint32_t c1 = *p++;
404 		uint32_t c2 = *p++;
405 		uint32_t c3 = *p++;
406 		uint32_t c4 = *p++;
407 		uint32_t w = (c4 << 24) | (c3 << 16) | (c2 << 8) | c1;
408 		*out++ = w;
409 	}
410 
411 	if (p == e && (*in_len & 0x3) && out < limit) {
412 		/* There are 1-3 trailing bytes, which shouldn't be there */
413 		*out++ = MBFL_BAD_INPUT;
414 		p = *in + *in_len;
415 	}
416 
417 	*in_len -= (p - *in);
418 	*in = p;
419 	return out - buf;
420 }
421 
mb_wchar_to_ucs4le(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)422 static void mb_wchar_to_ucs4le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
423 {
424 	unsigned char *out, *limit;
425 	MB_CONVERT_BUF_LOAD(buf, out, limit);
426 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
427 
428 	while (len--) {
429 		uint32_t w = *in++;
430 		if (w != MBFL_BAD_INPUT) {
431 			out = mb_convert_buf_add4(out, w & 0xFF, (w >> 8) & 0xFF, (w >> 16) & 0xFF, (w >> 24) & 0xFF);
432 		} else {
433 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs4le);
434 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 4);
435 		}
436 	}
437 
438 	MB_CONVERT_BUF_STORE(buf, out, limit);
439 }
440