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_ucs2.h"
32 
33 static int mbfl_filt_conv_ucs2_wchar_flush(mbfl_convert_filter *filter);
34 static size_t mb_ucs2_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
35 static size_t mb_ucs2be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
36 static void mb_wchar_to_ucs2be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
37 static size_t mb_ucs2le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state);
38 static void mb_wchar_to_ucs2le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end);
39 
40 static const char *mbfl_encoding_ucs2_aliases[] = {"ISO-10646-UCS-2", "UCS2" , "UNICODE", NULL};
41 
42 /* This library historically had encodings called 'byte2be' and 'byte2le'
43  * which were almost identical to UCS-2, except that they would truncate
44  * Unicode codepoints higher than 0xFFFF quietly
45  * Maintain minimal support by aliasing to UCS-2 */
46 static const char *mbfl_encoding_ucs2be_aliases[] = {"byte2be", NULL};
47 static const char *mbfl_encoding_ucs2le_aliases[] = {"byte2le", NULL};
48 
49 const mbfl_encoding mbfl_encoding_ucs2 = {
50 	mbfl_no_encoding_ucs2,
51 	"UCS-2",
52 	"UCS-2",
53 	mbfl_encoding_ucs2_aliases,
54 	NULL,
55 	MBFL_ENCTYPE_WCS2,
56 	&vtbl_ucs2_wchar,
57 	&vtbl_wchar_ucs2,
58 	mb_ucs2_to_wchar,
59 	mb_wchar_to_ucs2be,
60 	NULL,
61 	NULL,
62 };
63 
64 const mbfl_encoding mbfl_encoding_ucs2be = {
65 	mbfl_no_encoding_ucs2be,
66 	"UCS-2BE",
67 	"UCS-2BE",
68 	mbfl_encoding_ucs2be_aliases,
69 	NULL,
70 	MBFL_ENCTYPE_WCS2,
71 	&vtbl_ucs2be_wchar,
72 	&vtbl_wchar_ucs2be,
73 	mb_ucs2be_to_wchar,
74 	mb_wchar_to_ucs2be,
75 	NULL,
76 	NULL,
77 };
78 
79 const mbfl_encoding mbfl_encoding_ucs2le = {
80 	mbfl_no_encoding_ucs2le,
81 	"UCS-2LE",
82 	"UCS-2LE",
83 	mbfl_encoding_ucs2le_aliases,
84 	NULL,
85 	MBFL_ENCTYPE_WCS2,
86 	&vtbl_ucs2le_wchar,
87 	&vtbl_wchar_ucs2le,
88 	mb_ucs2le_to_wchar,
89 	mb_wchar_to_ucs2le,
90 	NULL,
91 	NULL,
92 };
93 
94 const struct mbfl_convert_vtbl vtbl_ucs2_wchar = {
95 	mbfl_no_encoding_ucs2,
96 	mbfl_no_encoding_wchar,
97 	mbfl_filt_conv_common_ctor,
98 	NULL,
99 	mbfl_filt_conv_ucs2_wchar,
100 	mbfl_filt_conv_ucs2_wchar_flush,
101 	NULL,
102 };
103 
104 const struct mbfl_convert_vtbl vtbl_wchar_ucs2 = {
105 	mbfl_no_encoding_wchar,
106 	mbfl_no_encoding_ucs2,
107 	mbfl_filt_conv_common_ctor,
108 	NULL,
109 	mbfl_filt_conv_wchar_ucs2be,
110 	mbfl_filt_conv_common_flush,
111 	NULL,
112 };
113 
114 const struct mbfl_convert_vtbl vtbl_ucs2be_wchar = {
115 	mbfl_no_encoding_ucs2be,
116 	mbfl_no_encoding_wchar,
117 	mbfl_filt_conv_common_ctor,
118 	NULL,
119 	mbfl_filt_conv_ucs2be_wchar,
120 	mbfl_filt_conv_ucs2_wchar_flush,
121 	NULL,
122 };
123 
124 const struct mbfl_convert_vtbl vtbl_wchar_ucs2be = {
125 	mbfl_no_encoding_wchar,
126 	mbfl_no_encoding_ucs2be,
127 	mbfl_filt_conv_common_ctor,
128 	NULL,
129 	mbfl_filt_conv_wchar_ucs2be,
130 	mbfl_filt_conv_common_flush,
131 	NULL,
132 };
133 
134 const struct mbfl_convert_vtbl vtbl_ucs2le_wchar = {
135 	mbfl_no_encoding_ucs2le,
136 	mbfl_no_encoding_wchar,
137 	mbfl_filt_conv_common_ctor,
138 	NULL,
139 	mbfl_filt_conv_ucs2le_wchar,
140 	mbfl_filt_conv_ucs2_wchar_flush,
141 	NULL,
142 };
143 
144 const struct mbfl_convert_vtbl vtbl_wchar_ucs2le = {
145 	mbfl_no_encoding_wchar,
146 	mbfl_no_encoding_ucs2le,
147 	mbfl_filt_conv_common_ctor,
148 	NULL,
149 	mbfl_filt_conv_wchar_ucs2le,
150 	mbfl_filt_conv_common_flush,
151 	NULL,
152 };
153 
154 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
155 
mbfl_filt_conv_ucs2_wchar(int c,mbfl_convert_filter * filter)156 int mbfl_filt_conv_ucs2_wchar(int c, mbfl_convert_filter *filter)
157 {
158 	if (filter->status == 0) {
159 		filter->status = 1;
160 		filter->cache = c & 0xFF;
161 	} else {
162 		filter->status = 0;
163 		int n = (filter->cache << 8) | (c & 0xFF);
164 		if (n == 0xFFFE) {
165 			/* Found little-endian byte order mark */
166 			filter->filter_function = mbfl_filt_conv_ucs2le_wchar;
167 		} else {
168 			filter->filter_function = mbfl_filt_conv_ucs2be_wchar;
169 			if (n != 0xFEFF) {
170 				CK((*filter->output_function)(n, filter->data));
171 			}
172 		}
173 	}
174 	return 0;
175 }
176 
mbfl_filt_conv_ucs2be_wchar(int c,mbfl_convert_filter * filter)177 int mbfl_filt_conv_ucs2be_wchar(int c, mbfl_convert_filter *filter)
178 {
179 	if (filter->status == 0) {
180 		filter->status = 1;
181 		filter->cache = (c & 0xFF) << 8;
182 	} else {
183 		filter->status = 0;
184 		CK((*filter->output_function)((c & 0xFF) | filter->cache, filter->data));
185 	}
186 	return 0;
187 }
188 
mbfl_filt_conv_wchar_ucs2be(int c,mbfl_convert_filter * filter)189 int mbfl_filt_conv_wchar_ucs2be(int c, mbfl_convert_filter *filter)
190 {
191 	if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
192 		CK((*filter->output_function)((c >> 8) & 0xFF, filter->data));
193 		CK((*filter->output_function)(c & 0xFF, filter->data));
194 	} else {
195 		CK(mbfl_filt_conv_illegal_output(c, filter));
196 	}
197 	return 0;
198 }
199 
mbfl_filt_conv_ucs2le_wchar(int c,mbfl_convert_filter * filter)200 int mbfl_filt_conv_ucs2le_wchar(int c, mbfl_convert_filter *filter)
201 {
202 	if (filter->status == 0) {
203 		filter->status = 1;
204 		filter->cache = c & 0xFF;
205 	} else {
206 		filter->status = 0;
207 		CK((*filter->output_function)(((c & 0xFF) << 8) | filter->cache, filter->data));
208 	}
209 	return 0;
210 }
211 
mbfl_filt_conv_wchar_ucs2le(int c,mbfl_convert_filter * filter)212 int mbfl_filt_conv_wchar_ucs2le(int c, mbfl_convert_filter *filter)
213 {
214 	if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
215 		CK((*filter->output_function)(c & 0xFF, filter->data));
216 		CK((*filter->output_function)((c >> 8) & 0xFF, filter->data));
217 	} else {
218 		CK(mbfl_filt_conv_illegal_output(c, filter));
219 	}
220 	return 0;
221 }
222 
mbfl_filt_conv_ucs2_wchar_flush(mbfl_convert_filter * filter)223 static int mbfl_filt_conv_ucs2_wchar_flush(mbfl_convert_filter *filter)
224 {
225 	if (filter->status) {
226 		/* Input string was truncated */
227 		filter->status = 0;
228 		CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
229 	}
230 
231 	if (filter->flush_function) {
232 		(*filter->flush_function)(filter->data);
233 	}
234 
235 	return 0;
236 }
237 
238 #define DETECTED_BE 1
239 #define DETECTED_LE 2
240 
mb_ucs2_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)241 static size_t mb_ucs2_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
242 {
243 	if (*state == DETECTED_BE) {
244 		return mb_ucs2be_to_wchar(in, in_len, buf, bufsize, NULL);
245 	} else if (*state == DETECTED_LE) {
246 		return mb_ucs2le_to_wchar(in, in_len, buf, bufsize, NULL);
247 	} else if (*in_len >= 2) {
248 		unsigned char *p = *in;
249 		unsigned char c1 = *p++;
250 		unsigned char c2 = *p++;
251 		uint32_t w = (c1 << 8) | c2;
252 
253 		if (w == 0xFFFE) {
254 			/* Little-endian BOM */
255 			*in = p;
256 			*in_len -= 2;
257 			*state = DETECTED_LE;
258 			return mb_ucs2le_to_wchar(in, in_len, buf, bufsize, NULL);
259 		} else if (w == 0xFEFF) {
260 			/* Big-endian BOM; don't send it to output */
261 			*in = p;
262 			*in_len -= 2;
263 		}
264 	}
265 
266 	*state = DETECTED_BE;
267 	return mb_ucs2be_to_wchar(in, in_len, buf, bufsize, NULL);
268 }
269 
mb_ucs2be_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)270 static size_t mb_ucs2be_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
271 {
272 	unsigned char *p = *in, *e = p + (*in_len & ~1);
273 	uint32_t *out = buf, *limit = buf + bufsize;
274 
275 	while (p < e && out < limit) {
276 		unsigned char c1 = *p++;
277 		unsigned char c2 = *p++;
278 		uint32_t w = (c1 << 8) | c2;
279 		*out++ = w;
280 	}
281 
282 	if (p == e && (*in_len & 0x1) && out < limit) {
283 		/* There is 1 trailing byte, which shouldn't be there */
284 		*out++ = MBFL_BAD_INPUT;
285 		p++;
286 	}
287 
288 	*in_len -= (p - *in);
289 	*in = p;
290 	return out - buf;
291 }
292 
mb_wchar_to_ucs2be(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)293 static void mb_wchar_to_ucs2be(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
294 {
295 	unsigned char *out, *limit;
296 	MB_CONVERT_BUF_LOAD(buf, out, limit);
297 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
298 
299 	while (len--) {
300 		uint32_t w = *in++;
301 		if (w < MBFL_WCSPLANE_UCS2MAX) {
302 			out = mb_convert_buf_add2(out, (w >> 8) & 0xFF, w & 0xFF);
303 		} else {
304 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs2be);
305 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
306 		}
307 	}
308 
309 	MB_CONVERT_BUF_STORE(buf, out, limit);
310 }
311 
mb_ucs2le_to_wchar(unsigned char ** in,size_t * in_len,uint32_t * buf,size_t bufsize,unsigned int * state)312 static size_t mb_ucs2le_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
313 {
314 	unsigned char *p = *in, *e = p + (*in_len & ~1);
315 	uint32_t *out = buf, *limit = buf + bufsize;
316 
317 	while (p < e && out < limit) {
318 		unsigned char c1 = *p++;
319 		unsigned char c2 = *p++;
320 		uint32_t w = (c2 << 8) | c1;
321 		*out++ = w;
322 	}
323 
324 	if (p == e && (*in_len & 0x1) && out < limit) {
325 		/* There is 1 trailing byte, which shouldn't be there */
326 		*out++ = MBFL_BAD_INPUT;
327 		p++;
328 	}
329 
330 	*in_len -= (p - *in);
331 	*in = p;
332 	return out - buf;
333 }
334 
mb_wchar_to_ucs2le(uint32_t * in,size_t len,mb_convert_buf * buf,bool end)335 static void mb_wchar_to_ucs2le(uint32_t *in, size_t len, mb_convert_buf *buf, bool end)
336 {
337 	unsigned char *out, *limit;
338 	MB_CONVERT_BUF_LOAD(buf, out, limit);
339 	MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
340 
341 	while (len--) {
342 		uint32_t w = *in++;
343 		if (w < MBFL_WCSPLANE_UCS2MAX) {
344 			out = mb_convert_buf_add2(out, w & 0xFF, (w >> 8) & 0xFF);
345 		} else {
346 			MB_CONVERT_ERROR(buf, out, limit, w, mb_wchar_to_ucs2le);
347 			MB_CONVERT_BUF_ENSURE(buf, out, limit, len * 2);
348 		}
349 	}
350 
351 	MB_CONVERT_BUF_STORE(buf, out, limit);
352 }
353