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 const char *mbfl_encoding_ucs2_aliases[] = {"ISO-10646-UCS-2", "UCS2" , "UNICODE", NULL};
34
35 const mbfl_encoding mbfl_encoding_ucs2 = {
36 mbfl_no_encoding_ucs2,
37 "UCS-2",
38 "UCS-2",
39 (const char *(*)[])&mbfl_encoding_ucs2_aliases,
40 NULL,
41 MBFL_ENCTYPE_WCS2BE,
42 &vtbl_ucs2_wchar,
43 &vtbl_wchar_ucs2
44 };
45
46 const mbfl_encoding mbfl_encoding_ucs2be = {
47 mbfl_no_encoding_ucs2be,
48 "UCS-2BE",
49 "UCS-2BE",
50 NULL,
51 NULL,
52 MBFL_ENCTYPE_WCS2BE,
53 &vtbl_ucs2be_wchar,
54 &vtbl_wchar_ucs2be
55 };
56
57 const mbfl_encoding mbfl_encoding_ucs2le = {
58 mbfl_no_encoding_ucs2le,
59 "UCS-2LE",
60 "UCS-2LE",
61 NULL,
62 NULL,
63 MBFL_ENCTYPE_WCS2LE,
64 &vtbl_ucs2le_wchar,
65 &vtbl_wchar_ucs2le
66 };
67
68 const struct mbfl_convert_vtbl vtbl_ucs2_wchar = {
69 mbfl_no_encoding_ucs2,
70 mbfl_no_encoding_wchar,
71 mbfl_filt_conv_common_ctor,
72 NULL,
73 mbfl_filt_conv_ucs2_wchar,
74 mbfl_filt_conv_common_flush,
75 NULL,
76 };
77
78 const struct mbfl_convert_vtbl vtbl_wchar_ucs2 = {
79 mbfl_no_encoding_wchar,
80 mbfl_no_encoding_ucs2,
81 mbfl_filt_conv_common_ctor,
82 NULL,
83 mbfl_filt_conv_wchar_ucs2be,
84 mbfl_filt_conv_common_flush,
85 NULL,
86 };
87
88 const struct mbfl_convert_vtbl vtbl_ucs2be_wchar = {
89 mbfl_no_encoding_ucs2be,
90 mbfl_no_encoding_wchar,
91 mbfl_filt_conv_common_ctor,
92 NULL,
93 mbfl_filt_conv_ucs2be_wchar,
94 mbfl_filt_conv_common_flush,
95 NULL,
96 };
97
98 const struct mbfl_convert_vtbl vtbl_wchar_ucs2be = {
99 mbfl_no_encoding_wchar,
100 mbfl_no_encoding_ucs2be,
101 mbfl_filt_conv_common_ctor,
102 NULL,
103 mbfl_filt_conv_wchar_ucs2be,
104 mbfl_filt_conv_common_flush,
105 NULL,
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 NULL,
113 mbfl_filt_conv_ucs2le_wchar,
114 mbfl_filt_conv_common_flush,
115 NULL,
116 };
117
118 const struct mbfl_convert_vtbl vtbl_wchar_ucs2le = {
119 mbfl_no_encoding_wchar,
120 mbfl_no_encoding_ucs2le,
121 mbfl_filt_conv_common_ctor,
122 NULL,
123 mbfl_filt_conv_wchar_ucs2le,
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 * UCS-2 => wchar
132 */
mbfl_filt_conv_ucs2_wchar(int c,mbfl_convert_filter * filter)133 int mbfl_filt_conv_ucs2_wchar(int c, mbfl_convert_filter *filter)
134 {
135 int n, endian;
136
137 endian = filter->status & 0xff00;
138 switch (filter->status & 0xff) {
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;
155 if (n == 0xfffe) {
156 if (endian) {
157 filter->status = 0; /* big-endian */
158 } else {
159 filter->status = 0x100; /* little-endian */
160 }
161 CK((*filter->output_function)(0xfeff, filter->data));
162 } else {
163 filter->status &= ~0xff;
164 CK((*filter->output_function)(n, filter->data));
165 }
166 break;
167 }
168
169 return c;
170 }
171
172 /*
173 * UCS-2BE => wchar
174 */
mbfl_filt_conv_ucs2be_wchar(int c,mbfl_convert_filter * filter)175 int mbfl_filt_conv_ucs2be_wchar(int c, mbfl_convert_filter *filter)
176 {
177 int n;
178
179 if (filter->status == 0) {
180 filter->status = 1;
181 n = (c & 0xff) << 8;
182 filter->cache = n;
183 } else {
184 filter->status = 0;
185 n = (c & 0xff) | filter->cache;
186 CK((*filter->output_function)(n, filter->data));
187 }
188 return c;
189 }
190
191 /*
192 * wchar => UCS-2BE
193 */
mbfl_filt_conv_wchar_ucs2be(int c,mbfl_convert_filter * filter)194 int mbfl_filt_conv_wchar_ucs2be(int c, mbfl_convert_filter *filter)
195 {
196 if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
197 CK((*filter->output_function)((c >> 8) & 0xff, filter->data));
198 CK((*filter->output_function)(c & 0xff, filter->data));
199 } else {
200 CK(mbfl_filt_conv_illegal_output(c, filter));
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 CK(mbfl_filt_conv_illegal_output(c, filter));
236 }
237
238 return c;
239 }
240