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