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_ucs4.h"
36
37 static const char *mbfl_encoding_ucs4_aliases[] = {"ISO-10646-UCS-4", "UCS4", NULL};
38
39 const mbfl_encoding mbfl_encoding_ucs4 = {
40 mbfl_no_encoding_ucs4,
41 "UCS-4",
42 "UCS-4",
43 (const char *(*)[])&mbfl_encoding_ucs4_aliases,
44 NULL,
45 MBFL_ENCTYPE_WCS4BE,
46 &vtbl_ucs4_wchar,
47 &vtbl_wchar_ucs4
48 };
49
50 const mbfl_encoding mbfl_encoding_ucs4be = {
51 mbfl_no_encoding_ucs4be,
52 "UCS-4BE",
53 "UCS-4BE",
54 NULL,
55 NULL,
56 MBFL_ENCTYPE_WCS4BE,
57 &vtbl_ucs4be_wchar,
58 &vtbl_wchar_ucs4be
59 };
60
61 const mbfl_encoding mbfl_encoding_ucs4le = {
62 mbfl_no_encoding_ucs4le,
63 "UCS-4LE",
64 "UCS-4LE",
65 NULL,
66 NULL,
67 MBFL_ENCTYPE_WCS4LE,
68 &vtbl_ucs4le_wchar,
69 &vtbl_wchar_ucs4le
70 };
71
72 const struct mbfl_convert_vtbl vtbl_ucs4_wchar = {
73 mbfl_no_encoding_ucs4,
74 mbfl_no_encoding_wchar,
75 mbfl_filt_conv_common_ctor,
76 mbfl_filt_conv_common_dtor,
77 mbfl_filt_conv_ucs4_wchar,
78 mbfl_filt_conv_common_flush
79 };
80
81 const struct mbfl_convert_vtbl vtbl_wchar_ucs4 = {
82 mbfl_no_encoding_wchar,
83 mbfl_no_encoding_ucs4,
84 mbfl_filt_conv_common_ctor,
85 mbfl_filt_conv_common_dtor,
86 mbfl_filt_conv_wchar_ucs4be,
87 mbfl_filt_conv_common_flush
88 };
89
90 const struct mbfl_convert_vtbl vtbl_ucs4be_wchar = {
91 mbfl_no_encoding_ucs4be,
92 mbfl_no_encoding_wchar,
93 mbfl_filt_conv_common_ctor,
94 mbfl_filt_conv_common_dtor,
95 mbfl_filt_conv_ucs4be_wchar,
96 mbfl_filt_conv_common_flush
97 };
98
99 const struct mbfl_convert_vtbl vtbl_wchar_ucs4be = {
100 mbfl_no_encoding_wchar,
101 mbfl_no_encoding_ucs4be,
102 mbfl_filt_conv_common_ctor,
103 mbfl_filt_conv_common_dtor,
104 mbfl_filt_conv_wchar_ucs4be,
105 mbfl_filt_conv_common_flush
106 };
107
108 const struct mbfl_convert_vtbl vtbl_ucs4le_wchar = {
109 mbfl_no_encoding_ucs4le,
110 mbfl_no_encoding_wchar,
111 mbfl_filt_conv_common_ctor,
112 mbfl_filt_conv_common_dtor,
113 mbfl_filt_conv_ucs4le_wchar,
114 mbfl_filt_conv_common_flush
115 };
116
117 const struct mbfl_convert_vtbl vtbl_wchar_ucs4le = {
118 mbfl_no_encoding_wchar,
119 mbfl_no_encoding_ucs4le,
120 mbfl_filt_conv_common_ctor,
121 mbfl_filt_conv_common_dtor,
122 mbfl_filt_conv_wchar_ucs4le,
123 mbfl_filt_conv_common_flush
124 };
125
126
127 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
128
129 /*
130 * UCS-4 => wchar
131 */
mbfl_filt_conv_ucs4_wchar(int c,mbfl_convert_filter * filter)132 int mbfl_filt_conv_ucs4_wchar(int c, mbfl_convert_filter *filter)
133 {
134 int n, endian;
135
136 endian = filter->status & 0xff00;
137 switch (filter->status & 0xff) {
138 case 0:
139 if (endian) {
140 n = c & 0xff;
141 } else {
142 n = (c & 0xff) << 24;
143 }
144 filter->cache = n;
145 filter->status++;
146 break;
147 case 1:
148 if (endian) {
149 n = (c & 0xff) << 8;
150 } else {
151 n = (c & 0xff) << 16;
152 }
153 filter->cache |= n;
154 filter->status++;
155 break;
156 case 2:
157 if (endian) {
158 n = (c & 0xff) << 16;
159 } else {
160 n = (c & 0xff) << 8;
161 }
162 filter->cache |= n;
163 filter->status++;
164 break;
165 default:
166 if (endian) {
167 n = (c & 0xff) << 24;
168 } else {
169 n = c & 0xff;
170 }
171 n |= filter->cache;
172 if ((n & 0xffff) == 0 && ((n >> 16) & 0xffff) == 0xfffe) {
173 if (endian) {
174 filter->status = 0; /* big-endian */
175 } else {
176 filter->status = 0x100; /* little-endian */
177 }
178 CK((*filter->output_function)(0xfeff, filter->data));
179 } else {
180 filter->status &= ~0xff;
181 CK((*filter->output_function)(n, filter->data));
182 }
183 break;
184 }
185
186 return c;
187 }
188
189 /*
190 * UCS-4BE => wchar
191 */
mbfl_filt_conv_ucs4be_wchar(int c,mbfl_convert_filter * filter)192 int mbfl_filt_conv_ucs4be_wchar(int c, mbfl_convert_filter *filter)
193 {
194 int n;
195
196 if (filter->status == 0) {
197 filter->status = 1;
198 n = (c & 0xff) << 24;
199 filter->cache = n;
200 } else if (filter->status == 1) {
201 filter->status = 2;
202 n = (c & 0xff) << 16;
203 filter->cache |= n;
204 } else if (filter->status == 2) {
205 filter->status = 3;
206 n = (c & 0xff) << 8;
207 filter->cache |= n;
208 } else {
209 filter->status = 0;
210 n = (c & 0xff) | filter->cache;
211 CK((*filter->output_function)(n, filter->data));
212 }
213 return c;
214 }
215
216 /*
217 * wchar => UCS-4BE
218 */
mbfl_filt_conv_wchar_ucs4be(int c,mbfl_convert_filter * filter)219 int mbfl_filt_conv_wchar_ucs4be(int c, mbfl_convert_filter *filter)
220 {
221 if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) {
222 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
223 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
224 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
225 CK((*filter->output_function)(c & 0xff, filter->data));
226 } else {
227 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
228 CK(mbfl_filt_conv_illegal_output(c, filter));
229 }
230 }
231
232 return c;
233 }
234
235 /*
236 * UCS-4LE => wchar
237 */
mbfl_filt_conv_ucs4le_wchar(int c,mbfl_convert_filter * filter)238 int mbfl_filt_conv_ucs4le_wchar(int c, mbfl_convert_filter *filter)
239 {
240 int n;
241
242 if (filter->status == 0) {
243 filter->status = 1;
244 n = (c & 0xff);
245 filter->cache = n;
246 } else if (filter->status == 1) {
247 filter->status = 2;
248 n = (c & 0xff) << 8;
249 filter->cache |= n;
250 } else if (filter->status == 2) {
251 filter->status = 3;
252 n = (c & 0xff) << 16;
253 filter->cache |= n;
254 } else {
255 filter->status = 0;
256 n = ((c & 0xff) << 24) | filter->cache;
257 CK((*filter->output_function)(n, filter->data));
258 }
259 return c;
260 }
261
262 /*
263 * wchar => UCS-4LE
264 */
mbfl_filt_conv_wchar_ucs4le(int c,mbfl_convert_filter * filter)265 int mbfl_filt_conv_wchar_ucs4le(int c, mbfl_convert_filter *filter)
266 {
267 if (c >= 0 && c < MBFL_WCSGROUP_UCS4MAX) {
268 CK((*filter->output_function)(c & 0xff, filter->data));
269 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
270 CK((*filter->output_function)((c >> 16) & 0xff, filter->data));
271 CK((*filter->output_function)((c >> 24) & 0xff, filter->data));
272 } else {
273 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
274 CK(mbfl_filt_conv_illegal_output(c, filter));
275 }
276 }
277
278 return c;
279 }
280