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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "mbfilter.h"
35 #include "mbfilter_ucs2.h"
36
37 static const char *mbfl_encoding_ucs2_aliases[] = {"ISO-10646-UCS-2", "UCS2" , "UNICODE", NULL};
38
39 const mbfl_encoding mbfl_encoding_ucs2 = {
40 mbfl_no_encoding_ucs2,
41 "UCS-2",
42 "UCS-2",
43 (const char *(*)[])&mbfl_encoding_ucs2_aliases,
44 NULL,
45 MBFL_ENCTYPE_WCS2BE,
46 &vtbl_ucs2_wchar,
47 &vtbl_wchar_ucs2
48 };
49
50 const mbfl_encoding mbfl_encoding_ucs2be = {
51 mbfl_no_encoding_ucs2be,
52 "UCS-2BE",
53 "UCS-2BE",
54 NULL,
55 NULL,
56 MBFL_ENCTYPE_WCS2BE,
57 &vtbl_ucs2be_wchar,
58 &vtbl_wchar_ucs2be
59 };
60
61 const mbfl_encoding mbfl_encoding_ucs2le = {
62 mbfl_no_encoding_ucs2le,
63 "UCS-2LE",
64 "UCS-2LE",
65 NULL,
66 NULL,
67 MBFL_ENCTYPE_WCS2LE,
68 &vtbl_ucs2le_wchar,
69 &vtbl_wchar_ucs2le
70 };
71
72 const struct mbfl_convert_vtbl vtbl_ucs2_wchar = {
73 mbfl_no_encoding_ucs2,
74 mbfl_no_encoding_wchar,
75 mbfl_filt_conv_common_ctor,
76 mbfl_filt_conv_common_dtor,
77 mbfl_filt_conv_ucs2_wchar,
78 mbfl_filt_conv_common_flush
79 };
80
81 const struct mbfl_convert_vtbl vtbl_wchar_ucs2 = {
82 mbfl_no_encoding_wchar,
83 mbfl_no_encoding_ucs2,
84 mbfl_filt_conv_common_ctor,
85 mbfl_filt_conv_common_dtor,
86 mbfl_filt_conv_wchar_ucs2be,
87 mbfl_filt_conv_common_flush
88 };
89
90 const struct mbfl_convert_vtbl vtbl_ucs2be_wchar = {
91 mbfl_no_encoding_ucs2be,
92 mbfl_no_encoding_wchar,
93 mbfl_filt_conv_common_ctor,
94 mbfl_filt_conv_common_dtor,
95 mbfl_filt_conv_ucs2be_wchar,
96 mbfl_filt_conv_common_flush
97 };
98
99 const struct mbfl_convert_vtbl vtbl_wchar_ucs2be = {
100 mbfl_no_encoding_wchar,
101 mbfl_no_encoding_ucs2be,
102 mbfl_filt_conv_common_ctor,
103 mbfl_filt_conv_common_dtor,
104 mbfl_filt_conv_wchar_ucs2be,
105 mbfl_filt_conv_common_flush
106 };
107
108 const struct mbfl_convert_vtbl vtbl_ucs2le_wchar = {
109 mbfl_no_encoding_ucs2le,
110 mbfl_no_encoding_wchar,
111 mbfl_filt_conv_common_ctor,
112 mbfl_filt_conv_common_dtor,
113 mbfl_filt_conv_ucs2le_wchar,
114 mbfl_filt_conv_common_flush
115 };
116
117 const struct mbfl_convert_vtbl vtbl_wchar_ucs2le = {
118 mbfl_no_encoding_wchar,
119 mbfl_no_encoding_ucs2le,
120 mbfl_filt_conv_common_ctor,
121 mbfl_filt_conv_common_dtor,
122 mbfl_filt_conv_wchar_ucs2le,
123 mbfl_filt_conv_common_flush
124 };
125
126 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
127
128 /*
129 * UCS-2 => wchar
130 */
mbfl_filt_conv_ucs2_wchar(int c,mbfl_convert_filter * filter)131 int mbfl_filt_conv_ucs2_wchar(int c, mbfl_convert_filter *filter)
132 {
133 int n, endian;
134
135 endian = filter->status & 0xff00;
136 switch (filter->status & 0xff) {
137 case 0:
138 if (endian) {
139 n = c & 0xff;
140 } else {
141 n = (c & 0xff) << 8;
142 }
143 filter->cache = n;
144 filter->status++;
145 break;
146 default:
147 if (endian) {
148 n = (c & 0xff) << 8;
149 } else {
150 n = c & 0xff;
151 }
152 n |= filter->cache;
153 if (n == 0xfffe) {
154 if (endian) {
155 filter->status = 0; /* big-endian */
156 } else {
157 filter->status = 0x100; /* little-endian */
158 }
159 CK((*filter->output_function)(0xfeff, filter->data));
160 } else {
161 filter->status &= ~0xff;
162 CK((*filter->output_function)(n, filter->data));
163 }
164 break;
165 }
166
167 return c;
168 }
169
170 /*
171 * UCS-2BE => wchar
172 */
mbfl_filt_conv_ucs2be_wchar(int c,mbfl_convert_filter * filter)173 int mbfl_filt_conv_ucs2be_wchar(int c, mbfl_convert_filter *filter)
174 {
175 int n;
176
177 if (filter->status == 0) {
178 filter->status = 1;
179 n = (c & 0xff) << 8;
180 filter->cache = n;
181 } else {
182 filter->status = 0;
183 n = (c & 0xff) | filter->cache;
184 CK((*filter->output_function)(n, filter->data));
185 }
186 return c;
187 }
188
189 /*
190 * wchar => UCS-2BE
191 */
mbfl_filt_conv_wchar_ucs2be(int c,mbfl_convert_filter * filter)192 int mbfl_filt_conv_wchar_ucs2be(int c, mbfl_convert_filter *filter)
193 {
194 if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
195 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
196 CK((*filter->output_function)(c & 0xff, filter->data));
197 } else {
198 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
199 CK(mbfl_filt_conv_illegal_output(c, filter));
200 }
201 }
202
203 return c;
204 }
205
206 /*
207 * UCS-2LE => wchar
208 */
mbfl_filt_conv_ucs2le_wchar(int c,mbfl_convert_filter * filter)209 int mbfl_filt_conv_ucs2le_wchar(int c, mbfl_convert_filter *filter)
210 {
211 int n;
212
213 if (filter->status == 0) {
214 filter->status = 1;
215 n = c & 0xff;
216 filter->cache = n;
217 } else {
218 filter->status = 0;
219 n = ((c & 0xff) << 8) | filter->cache;
220 CK((*filter->output_function)(n, filter->data));
221 }
222 return c;
223 }
224
225
226 /*
227 * wchar => UCS-2LE
228 */
mbfl_filt_conv_wchar_ucs2le(int c,mbfl_convert_filter * filter)229 int mbfl_filt_conv_wchar_ucs2le(int c, mbfl_convert_filter *filter)
230 {
231 if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
232 CK((*filter->output_function)(c & 0xff, filter->data));
233 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
234 } else {
235 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
236 CK(mbfl_filt_conv_illegal_output(c, filter));
237 }
238 }
239
240 return c;
241 }
242