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_utf7imap.h"
36
37 const mbfl_encoding mbfl_encoding_utf7imap = {
38 mbfl_no_encoding_utf7imap,
39 "UTF7-IMAP",
40 NULL,
41 NULL,
42 NULL,
43 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE,
44 &vtbl_utf7imap_wchar,
45 &vtbl_wchar_utf7imap
46 };
47
48 const struct mbfl_convert_vtbl vtbl_utf7imap_wchar = {
49 mbfl_no_encoding_utf7imap,
50 mbfl_no_encoding_wchar,
51 mbfl_filt_conv_common_ctor,
52 mbfl_filt_conv_common_dtor,
53 mbfl_filt_conv_utf7imap_wchar,
54 mbfl_filt_conv_common_flush };
55
56 const struct mbfl_convert_vtbl vtbl_wchar_utf7imap = {
57 mbfl_no_encoding_wchar,
58 mbfl_no_encoding_utf7imap,
59 mbfl_filt_conv_common_ctor,
60 mbfl_filt_conv_common_dtor,
61 mbfl_filt_conv_wchar_utf7imap,
62 mbfl_filt_conv_wchar_utf7imap_flush };
63
64 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
65
66 /*
67 * UTF7-IMAP => wchar
68 */
mbfl_filt_conv_utf7imap_wchar(int c,mbfl_convert_filter * filter)69 int mbfl_filt_conv_utf7imap_wchar(int c, mbfl_convert_filter *filter)
70 {
71 int s, n;
72
73 n = -1;
74 if (filter->status != 0) { /* Modified Base64 */
75 if (c >= 0x41 && c <= 0x5a) { /* A - Z */
76 n = c - 65;
77 } else if (c >= 0x61 && c <= 0x7a) { /* a - z */
78 n = c - 71;
79 } else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
80 n = c + 4;
81 } else if (c == 0x2b) { /* '+' */
82 n = 62;
83 } else if (c == 0x2c) { /* ',' */
84 n = 63;
85 }
86 if (n < 0 || n > 63) {
87 if (c == 0x2d) {
88 if (filter->status == 1) { /* "&-" -> "&" */
89 CK((*filter->output_function)(0x26, filter->data));
90 }
91 } else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
92 CK((*filter->output_function)(c, filter->data));
93 } else { /* illegal character */
94 s = c & MBFL_WCSGROUP_MASK;
95 s |= MBFL_WCSGROUP_THROUGH;
96 CK((*filter->output_function)(s, filter->data));
97 }
98 filter->cache = 0;
99 filter->status = 0;
100 return c;
101 }
102 }
103
104 switch (filter->status) {
105 /* directly encoded characters */
106 case 0:
107 if (c == 0x26) { /* '&' shift character */
108 filter->status++;
109 } else if (c >= 0 && c < 0x80) { /* ASCII */
110 CK((*filter->output_function)(c, filter->data));
111 } else { /* illegal character */
112 s = c & MBFL_WCSGROUP_MASK;
113 s |= MBFL_WCSGROUP_THROUGH;
114 CK((*filter->output_function)(s, filter->data));
115 }
116 break;
117
118 /* decode Modified Base64 */
119 case 1:
120 case 2:
121 filter->cache |= n << 10;
122 filter->status = 3;
123 break;
124 case 3:
125 filter->cache |= n << 4;
126 filter->status = 4;
127 break;
128 case 4:
129 s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
130 n = (n & 0x3) << 14;
131 filter->status = 5;
132 if (s >= 0xd800 && s < 0xdc00) {
133 s = (((s & 0x3ff) << 16) + 0x400000) | n;
134 filter->cache = s;
135 } else if (s >= 0xdc00 && s < 0xe000) {
136 s &= 0x3ff;
137 s |= (filter->cache & 0xfff0000) >> 6;
138 filter->cache = n;
139 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
140 CK((*filter->output_function)(s, filter->data));
141 } else { /* illegal character */
142 s &= MBFL_WCSGROUP_MASK;
143 s |= MBFL_WCSGROUP_THROUGH;
144 CK((*filter->output_function)(s, filter->data));
145 }
146 } else {
147 filter->cache = n;
148 CK((*filter->output_function)(s, filter->data));
149 }
150 break;
151
152 case 5:
153 filter->cache |= n << 8;
154 filter->status = 6;
155 break;
156 case 6:
157 filter->cache |= n << 2;
158 filter->status = 7;
159 break;
160 case 7:
161 s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
162 n = (n & 0xf) << 12;
163 filter->status = 8;
164 if (s >= 0xd800 && s < 0xdc00) {
165 s = (((s & 0x3ff) << 16) + 0x400000) | n;
166 filter->cache = s;
167 } else if (s >= 0xdc00 && s < 0xe000) {
168 s &= 0x3ff;
169 s |= (filter->cache & 0xfff0000) >> 6;
170 filter->cache = n;
171 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
172 CK((*filter->output_function)(s, filter->data));
173 } else { /* illegal character */
174 s &= MBFL_WCSGROUP_MASK;
175 s |= MBFL_WCSGROUP_THROUGH;
176 CK((*filter->output_function)(s, filter->data));
177 }
178 } else {
179 filter->cache = n;
180 CK((*filter->output_function)(s, filter->data));
181 }
182 break;
183
184 case 8:
185 filter->cache |= n << 6;
186 filter->status = 9;
187 break;
188 case 9:
189 s = n | (filter->cache & 0xffff);
190 filter->status = 2;
191 if (s >= 0xd800 && s < 0xdc00) {
192 s = (((s & 0x3ff) << 16) + 0x400000);
193 filter->cache = s;
194 } else if (s >= 0xdc00 && s < 0xe000) {
195 s &= 0x3ff;
196 s |= (filter->cache & 0xfff0000) >> 6;
197 filter->cache = 0;
198 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
199 CK((*filter->output_function)(s, filter->data));
200 } else { /* illegal character */
201 s &= MBFL_WCSGROUP_MASK;
202 s |= MBFL_WCSGROUP_THROUGH;
203 CK((*filter->output_function)(s, filter->data));
204 }
205 } else {
206 filter->cache = 0;
207 CK((*filter->output_function)(s, filter->data));
208 }
209 break;
210
211 default:
212 filter->status = 0;
213 break;
214 }
215
216 return c;
217 }
218
219 static const unsigned char mbfl_utf7imap_base64_table[] =
220 {
221 /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
222 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
223 /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
224 0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
225 /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
226 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
227 /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
228 0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
229 /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ',', '\0' */
230 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2c,0x00
231 };
232
233 /*
234 * wchar => UTF7-IMAP
235 */
mbfl_filt_conv_wchar_utf7imap(int c,mbfl_convert_filter * filter)236 int mbfl_filt_conv_wchar_utf7imap(int c, mbfl_convert_filter *filter)
237 {
238 int n, s;
239
240 n = 0;
241 if (c == 0x26) {
242 n = 1;
243 } else if ((c >= 0x20 && c <= 0x7e) || c == 0) {
244 n = 2;
245 } else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
246 ;
247 } else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
248 s = ((c >> 10) - 0x40) | 0xd800;
249 CK((*filter->filter_function)(s, filter));
250 s = (c & 0x3ff) | 0xdc00;
251 CK((*filter->filter_function)(s, filter));
252 return c;
253 } else {
254 if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
255 CK(mbfl_filt_conv_illegal_output(c, filter));
256 }
257 return c;
258 }
259
260 switch (filter->status) {
261 case 0:
262 if (n != 0) { /* directly encode characters */
263 CK((*filter->output_function)(c, filter->data));
264 if (n == 1) {
265 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
266 }
267 } else { /* Modified Base64 */
268 CK((*filter->output_function)(0x26, filter->data)); /* '&' */
269 filter->status = 1;
270 filter->cache = c;
271 }
272 break;
273
274 /* encode Modified Base64 */
275 case 1:
276 s = filter->cache;
277 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 10) & 0x3f], filter->data));
278 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 4) & 0x3f], filter->data));
279 if (n != 0) {
280 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 2) & 0x3c], filter->data));
281 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
282 CK((*filter->output_function)(c, filter->data));
283 if (n == 1) {
284 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
285 }
286 filter->status = 0;
287 } else {
288 filter->status = 2;
289 filter->cache = ((s & 0xf) << 16) | c;
290 }
291 break;
292
293 case 2:
294 s = filter->cache;
295 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 14) & 0x3f], filter->data));
296 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 8) & 0x3f], filter->data));
297 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 2) & 0x3f], filter->data));
298 if (n != 0) {
299 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 4) & 0x30], filter->data));
300 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
301 CK((*filter->output_function)(c, filter->data));
302 if (n == 1) {
303 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
304 }
305 filter->status = 0;
306 } else {
307 filter->status = 3;
308 filter->cache = ((s & 0x3) << 16) | c;
309 }
310 break;
311
312 case 3:
313 s = filter->cache;
314 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 12) & 0x3f], filter->data));
315 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 6) & 0x3f], filter->data));
316 CK((*filter->output_function)(mbfl_utf7imap_base64_table[s & 0x3f], filter->data));
317 if (n != 0) {
318 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
319 CK((*filter->output_function)(c, filter->data));
320 if (n == 1) {
321 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
322 }
323 filter->status = 0;
324 } else {
325 filter->status = 1;
326 filter->cache = c;
327 }
328 break;
329
330 default:
331 filter->status = 0;
332 break;
333 }
334
335 return c;
336
337 }
338
mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter * filter)339 int mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter *filter)
340 {
341 int status, cache;
342
343 status = filter->status;
344 cache = filter->cache;
345 filter->status = 0;
346 filter->cache = 0;
347 /* flush fragments */
348 switch (status) {
349 case 1:
350 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 10) & 0x3f], filter->data));
351 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 4) & 0x3f], filter->data));
352 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 2) & 0x3c], filter->data));
353 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
354 break;
355
356 case 2:
357 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 14) & 0x3f], filter->data));
358 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 8) & 0x3f], filter->data));
359 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 2) & 0x3f], filter->data));
360 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 4) & 0x30], filter->data));
361 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
362 break;
363
364 case 3:
365 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 12) & 0x3f], filter->data));
366 CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 6) & 0x3f], filter->data));
367 CK((*filter->output_function)(mbfl_utf7imap_base64_table[cache & 0x3f], filter->data));
368 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
369 break;
370 }
371 return 0;
372 }
373