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_utf7imap.h"
32 
33 const mbfl_encoding mbfl_encoding_utf7imap = {
34 	mbfl_no_encoding_utf7imap,
35 	"UTF7-IMAP",
36 	NULL,
37 	NULL,
38 	NULL,
39 	MBFL_ENCTYPE_MBCS,
40 	&vtbl_utf7imap_wchar,
41 	&vtbl_wchar_utf7imap
42 };
43 
44 const struct mbfl_convert_vtbl vtbl_utf7imap_wchar = {
45 	mbfl_no_encoding_utf7imap,
46 	mbfl_no_encoding_wchar,
47 	mbfl_filt_conv_common_ctor,
48 	NULL,
49 	mbfl_filt_conv_utf7imap_wchar,
50 	mbfl_filt_conv_common_flush,
51 	NULL,
52 };
53 
54 const struct mbfl_convert_vtbl vtbl_wchar_utf7imap = {
55 	mbfl_no_encoding_wchar,
56 	mbfl_no_encoding_utf7imap,
57 	mbfl_filt_conv_common_ctor,
58 	NULL,
59 	mbfl_filt_conv_wchar_utf7imap,
60 	mbfl_filt_conv_wchar_utf7imap_flush,
61 	NULL,
62 };
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 		CK(mbfl_filt_conv_illegal_output(c, filter));
255 		return c;
256 	}
257 
258 	switch (filter->status) {
259 	case 0:
260 		if (n != 0) {	/* directly encode characters */
261 			CK((*filter->output_function)(c, filter->data));
262 			if (n == 1) {
263 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
264 			}
265 		} else {	/* Modified Base64 */
266 			CK((*filter->output_function)(0x26, filter->data));		/* '&' */
267 			filter->status = 1;
268 			filter->cache = c;
269 		}
270 		break;
271 
272 	/* encode Modified Base64 */
273 	case 1:
274 		s = filter->cache;
275 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 10) & 0x3f], filter->data));
276 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 4) & 0x3f], filter->data));
277 		if (n != 0) {
278 			CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 2) & 0x3c], filter->data));
279 			CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
280 			CK((*filter->output_function)(c, filter->data));
281 			if (n == 1) {
282 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
283 			}
284 			filter->status = 0;
285 		} else {
286 			filter->status = 2;
287 			filter->cache = ((s & 0xf) << 16) | c;
288 		}
289 		break;
290 
291 	case 2:
292 		s = filter->cache;
293 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 14) & 0x3f], filter->data));
294 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 8) & 0x3f], filter->data));
295 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 2) & 0x3f], filter->data));
296 		if (n != 0) {
297 			CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 4) & 0x30], filter->data));
298 			CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
299 			CK((*filter->output_function)(c, filter->data));
300 			if (n == 1) {
301 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
302 			}
303 			filter->status = 0;
304 		} else {
305 			filter->status = 3;
306 			filter->cache = ((s & 0x3) << 16) | c;
307 		}
308 		break;
309 
310 	case 3:
311 		s = filter->cache;
312 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 12) & 0x3f], filter->data));
313 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 6) & 0x3f], filter->data));
314 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[s & 0x3f], filter->data));
315 		if (n != 0) {
316 			CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
317 			CK((*filter->output_function)(c, filter->data));
318 			if (n == 1) {
319 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
320 			}
321 			filter->status = 0;
322 		} else {
323 			filter->status = 1;
324 			filter->cache = c;
325 		}
326 		break;
327 
328 	default:
329 		filter->status = 0;
330 		break;
331 	}
332 
333 	return c;
334 
335 }
336 
mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter * filter)337 int mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter *filter)
338 {
339 	int status, cache;
340 
341 	status = filter->status;
342 	cache = filter->cache;
343 	filter->status = 0;
344 	filter->cache = 0;
345 	/* flush fragments */
346 	switch (status) {
347 	case 1:
348 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 10) & 0x3f], filter->data));
349 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 4) & 0x3f], filter->data));
350 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 2) & 0x3c], filter->data));
351 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
352 		break;
353 
354 	case 2:
355 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 14) & 0x3f], filter->data));
356 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 8) & 0x3f], filter->data));
357 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 2) & 0x3f], filter->data));
358 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 4) & 0x30], filter->data));
359 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
360 		break;
361 
362 	case 3:
363 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 12) & 0x3f], filter->data));
364 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 6) & 0x3f], filter->data));
365 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[cache & 0x3f], filter->data));
366 		CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
367 		break;
368 	}
369 	return 0;
370 }
371