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_utf16.h"
32 
33 static const char *mbfl_encoding_utf16_aliases[] = {"utf16", NULL};
34 
35 const mbfl_encoding mbfl_encoding_utf16 = {
36 	mbfl_no_encoding_utf16,
37 	"UTF-16",
38 	"UTF-16",
39 	(const char *(*)[])&mbfl_encoding_utf16_aliases,
40 	NULL,
41 	MBFL_ENCTYPE_MWC2BE,
42 	&vtbl_utf16_wchar,
43 	&vtbl_wchar_utf16
44 };
45 
46 const mbfl_encoding mbfl_encoding_utf16be = {
47 	mbfl_no_encoding_utf16be,
48 	"UTF-16BE",
49 	"UTF-16BE",
50 	NULL,
51 	NULL,
52 	MBFL_ENCTYPE_MWC2BE,
53 	&vtbl_utf16be_wchar,
54 	&vtbl_wchar_utf16be
55 };
56 
57 const mbfl_encoding mbfl_encoding_utf16le = {
58 	mbfl_no_encoding_utf16le,
59 	"UTF-16LE",
60 	"UTF-16LE",
61 	NULL,
62 	NULL,
63 	MBFL_ENCTYPE_MWC2LE,
64 	&vtbl_utf16le_wchar,
65 	&vtbl_wchar_utf16le
66 };
67 
68 const struct mbfl_convert_vtbl vtbl_utf16_wchar = {
69 	mbfl_no_encoding_utf16,
70 	mbfl_no_encoding_wchar,
71 	mbfl_filt_conv_common_ctor,
72 	NULL,
73 	mbfl_filt_conv_utf16_wchar,
74 	mbfl_filt_conv_common_flush,
75 	NULL,
76 };
77 
78 const struct mbfl_convert_vtbl vtbl_wchar_utf16 = {
79 	mbfl_no_encoding_wchar,
80 	mbfl_no_encoding_utf16,
81 	mbfl_filt_conv_common_ctor,
82 	NULL,
83 	mbfl_filt_conv_wchar_utf16be,
84 	mbfl_filt_conv_common_flush,
85 	NULL,
86 };
87 
88 const struct mbfl_convert_vtbl vtbl_utf16be_wchar = {
89 	mbfl_no_encoding_utf16be,
90 	mbfl_no_encoding_wchar,
91 	mbfl_filt_conv_common_ctor,
92 	NULL,
93 	mbfl_filt_conv_utf16be_wchar,
94 	mbfl_filt_conv_common_flush,
95 	NULL,
96 };
97 
98 const struct mbfl_convert_vtbl vtbl_wchar_utf16be = {
99 	mbfl_no_encoding_wchar,
100 	mbfl_no_encoding_utf16be,
101 	mbfl_filt_conv_common_ctor,
102 	NULL,
103 	mbfl_filt_conv_wchar_utf16be,
104 	mbfl_filt_conv_common_flush,
105 	NULL,
106 };
107 
108 const struct mbfl_convert_vtbl vtbl_utf16le_wchar = {
109 	mbfl_no_encoding_utf16le,
110 	mbfl_no_encoding_wchar,
111 	mbfl_filt_conv_common_ctor,
112 	NULL,
113 	mbfl_filt_conv_utf16le_wchar,
114 	mbfl_filt_conv_common_flush,
115 	NULL,
116 };
117 
118 const struct mbfl_convert_vtbl vtbl_wchar_utf16le = {
119 	mbfl_no_encoding_wchar,
120 	mbfl_no_encoding_utf16le,
121 	mbfl_filt_conv_common_ctor,
122 	NULL,
123 	mbfl_filt_conv_wchar_utf16le,
124 	mbfl_filt_conv_common_flush,
125 	NULL,
126 };
127 
128 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
129 
130 /*
131  * UTF-16 => wchar
132  */
mbfl_filt_conv_utf16_wchar(int c,mbfl_convert_filter * filter)133 int mbfl_filt_conv_utf16_wchar(int c, mbfl_convert_filter *filter)
134 {
135 	int n, endian;
136 
137 	endian = filter->status & 0xff00;
138 	switch (filter->status & 0x0f) {
139 	case 0:
140 		if (endian) {
141 			n = c & 0xff;
142 		} else {
143 			n = (c & 0xff) << 8;
144 		}
145 		filter->cache |= n;
146 		filter->status++;
147 		break;
148 	default:
149 		if (endian) {
150 			n = (c & 0xff) << 8;
151 		} else {
152 			n = c & 0xff;
153 		}
154 		n |= filter->cache & 0xffff;
155 		filter->status &= ~0x0f;
156 		if (n >= 0xd800 && n < 0xdc00) {
157 			filter->cache = ((n & 0x3ff) << 16) + 0x400000;
158 		} else if (n >= 0xdc00 && n < 0xe000) {
159 			n &= 0x3ff;
160 			n |= (filter->cache & 0xfff0000) >> 6;
161 			filter->cache = 0;
162 			if (n >= MBFL_WCSPLANE_SUPMIN && n < MBFL_WCSPLANE_SUPMAX) {
163 				CK((*filter->output_function)(n, filter->data));
164 			} else {		/* illegal character */
165 				n &= MBFL_WCSGROUP_MASK;
166 				n |= MBFL_WCSGROUP_THROUGH;
167 				CK((*filter->output_function)(n, filter->data));
168 			}
169 		} else {
170 			int is_first = filter->status & 0x10;
171 			filter->cache = 0;
172 			filter->status |= 0x10;
173 			if (!is_first) {
174 				if (n == 0xfffe) {
175 					if (endian) {
176 						filter->status &= ~0x100;		/* big-endian */
177 					} else {
178 						filter->status |= 0x100;		/* little-endian */
179 					}
180 					break;
181 				} else if (n == 0xfeff) {
182 					break;
183 				}
184 			}
185 			CK((*filter->output_function)(n, filter->data));
186 		}
187 		break;
188 	}
189 
190 	return c;
191 }
192 
193 /*
194  * UTF-16BE => wchar
195  */
mbfl_filt_conv_utf16be_wchar(int c,mbfl_convert_filter * filter)196 int mbfl_filt_conv_utf16be_wchar(int c, mbfl_convert_filter *filter)
197 {
198 	int n;
199 
200 	switch (filter->status) {
201 	case 0:
202 		filter->status = 1;
203 		n = (c & 0xff) << 8;
204 		filter->cache |= n;
205 		break;
206 	default:
207 		filter->status = 0;
208 		n = (filter->cache & 0xff00) | (c & 0xff);
209 		if (n >= 0xd800 && n < 0xdc00) {
210 			filter->cache = ((n & 0x3ff) << 16) + 0x400000;
211 		} else if (n >= 0xdc00 && n < 0xe000) {
212 			n &= 0x3ff;
213 			n |= (filter->cache & 0xfff0000) >> 6;
214 			filter->cache = 0;
215 			if (n >= MBFL_WCSPLANE_SUPMIN && n < MBFL_WCSPLANE_SUPMAX) {
216 				CK((*filter->output_function)(n, filter->data));
217 			} else {		/* illegal character */
218 				n &= MBFL_WCSGROUP_MASK;
219 				n |= MBFL_WCSGROUP_THROUGH;
220 				CK((*filter->output_function)(n, filter->data));
221 			}
222 		} else {
223 			filter->cache = 0;
224 			CK((*filter->output_function)(n, filter->data));
225 		}
226 		break;
227 	}
228 
229 	return c;
230 }
231 
232 /*
233  * wchar => UTF-16BE
234  */
mbfl_filt_conv_wchar_utf16be(int c,mbfl_convert_filter * filter)235 int mbfl_filt_conv_wchar_utf16be(int c, mbfl_convert_filter *filter)
236 {
237 	int n;
238 
239 	if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
240 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
241 		CK((*filter->output_function)(c & 0xff, filter->data));
242 	} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
243 		n = ((c >> 10) - 0x40) | 0xd800;
244 		CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
245 		CK((*filter->output_function)(n & 0xff, filter->data));
246 		n = (c & 0x3ff) | 0xdc00;
247 		CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
248 		CK((*filter->output_function)(n & 0xff, filter->data));
249 	} else {
250 		CK(mbfl_filt_conv_illegal_output(c, filter));
251 	}
252 
253 	return c;
254 }
255 
256 /*
257  * UTF-16LE => wchar
258  */
mbfl_filt_conv_utf16le_wchar(int c,mbfl_convert_filter * filter)259 int mbfl_filt_conv_utf16le_wchar(int c, mbfl_convert_filter *filter)
260 {
261 	int n;
262 
263 	switch (filter->status) {
264 	case 0:
265 		filter->status = 1;
266 		n = c & 0xff;
267 		filter->cache |= n;
268 		break;
269 	default:
270 		filter->status = 0;
271 		n = (filter->cache & 0xff) | ((c & 0xff) << 8);
272 		if (n >= 0xd800 && n < 0xdc00) {
273 			filter->cache = ((n & 0x3ff) << 16) + 0x400000;
274 		} else if (n >= 0xdc00 && n < 0xe000) {
275 			n &= 0x3ff;
276 			n |= (filter->cache & 0xfff0000) >> 6;
277 			filter->cache = 0;
278 			if (n >= MBFL_WCSPLANE_SUPMIN && n < MBFL_WCSPLANE_SUPMAX) {
279 				CK((*filter->output_function)(n, filter->data));
280 			} else {		/* illegal character */
281 				n &= MBFL_WCSGROUP_MASK;
282 				n |= MBFL_WCSGROUP_THROUGH;
283 				CK((*filter->output_function)(n, filter->data));
284 			}
285 		} else {
286 			filter->cache = 0;
287 			CK((*filter->output_function)(n, filter->data));
288 		}
289 		break;
290 	}
291 
292 	return c;
293 }
294 
295 /*
296  * wchar => UTF-16LE
297  */
mbfl_filt_conv_wchar_utf16le(int c,mbfl_convert_filter * filter)298 int mbfl_filt_conv_wchar_utf16le(int c, mbfl_convert_filter *filter)
299 {
300 	int n;
301 
302 	if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
303 		CK((*filter->output_function)(c & 0xff, filter->data));
304 		CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
305 	} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
306 		n = ((c >> 10) - 0x40) | 0xd800;
307 		CK((*filter->output_function)(n & 0xff, filter->data));
308 		CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
309 		n = (c & 0x3ff) | 0xdc00;
310 		CK((*filter->output_function)(n & 0xff, filter->data));
311 		CK((*filter->output_function)((n >> 8) & 0xff, filter->data));
312 	} else {
313 		CK(mbfl_filt_conv_illegal_output(c, filter));
314 	}
315 
316 	return c;
317 }
318