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