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 /* Modified UTF-7 used for 'international mailbox names' in the IMAP protocol
31  * Also known as mUTF-7
32  * Defined in RFC 3501 5.1.3 (https://tools.ietf.org/html/rfc3501)
33  *
34  * Quoting from the RFC:
35  *
36  ***********************************************************************
37  * In modified UTF-7, printable US-ASCII characters, except for "&",
38  * represent themselves; that is, characters with octet values 0x20-0x25
39  * and 0x27-0x7e. The character "&" (0x26) is represented by the
40  * two-octet sequence "&-".
41  *
42  * All other characters (octet values 0x00-0x1f and 0x7f-0xff) are
43  * represented in modified BASE64, with a further modification from
44  * UTF-7 that "," is used instead of "/". Modified BASE64 MUST NOT be
45  * used to represent any printing US-ASCII character which can represent
46  * itself.
47  *
48  * "&" is used to shift to modified BASE64 and "-" to shift back to
49  * US-ASCII. There is no implicit shift from BASE64 to US-ASCII, and
50  * null shifts ("-&" while in BASE64; note that "&-" while in US-ASCII
51  * means "&") are not permitted.  However, all names start in US-ASCII,
52  * and MUST end in US-ASCII; that is, a name that ends with a non-ASCII
53  * ISO-10646 character MUST end with a "-").
54  ***********************************************************************
55  *
56  * The purpose of all this is: 1) to keep all parts of IMAP messages 7-bit clean,
57  * 2) to avoid giving special treatment to +, /, \, and ~, since these are
58  * commonly used in mailbox names, and 3) to ensure there is only one
59  * representation of any mailbox name (vanilla UTF-7 does allow multiple
60  * representations of the same string, by Base64-encoding characters which
61  * could have been included as ASCII literals.)
62  *
63  * RFC 2152 also applies, since it defines vanilla UTF-7 (minus IMAP modifications)
64  * The following paragraph is notable:
65  *
66  ***********************************************************************
67  * Unicode is encoded using Modified Base64 by first converting Unicode
68  * 16-bit quantities to an octet stream (with the most significant octet first).
69  * Surrogate pairs (UTF-16) are converted by treating each half of the pair as
70  * a separate 16 bit quantity (i.e., no special treatment). Text with an odd
71  * number of octets is ill-formed. ISO 10646 characters outside the range
72  * addressable via surrogate pairs cannot be encoded.
73  ***********************************************************************
74  *
75  * So after reversing the modified Base64 encoding on an encoded section,
76  * the contents are interpreted as UTF-16BE. */
77 
78 #include "mbfilter.h"
79 #include "mbfilter_utf7imap.h"
80 
81 static int mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter *filter);
82 static int mbfl_filt_conv_utf7imap_wchar_flush(mbfl_convert_filter *filter);
83 
84 static const char *mbfl_encoding_utf7imap_aliases[] = {"mUTF-7", NULL};
85 
86 const mbfl_encoding mbfl_encoding_utf7imap = {
87 	mbfl_no_encoding_utf7imap,
88 	"UTF7-IMAP",
89 	NULL,
90 	mbfl_encoding_utf7imap_aliases,
91 	NULL,
92 	0,
93 	&vtbl_utf7imap_wchar,
94 	&vtbl_wchar_utf7imap
95 };
96 
97 const struct mbfl_convert_vtbl vtbl_utf7imap_wchar = {
98 	mbfl_no_encoding_utf7imap,
99 	mbfl_no_encoding_wchar,
100 	mbfl_filt_conv_common_ctor,
101 	NULL,
102 	mbfl_filt_conv_utf7imap_wchar,
103 	mbfl_filt_conv_utf7imap_wchar_flush,
104 	NULL,
105 };
106 
107 const struct mbfl_convert_vtbl vtbl_wchar_utf7imap = {
108 	mbfl_no_encoding_wchar,
109 	mbfl_no_encoding_utf7imap,
110 	mbfl_filt_conv_common_ctor,
111 	NULL,
112 	mbfl_filt_conv_wchar_utf7imap,
113 	mbfl_filt_conv_wchar_utf7imap_flush,
114 	NULL,
115 };
116 
117 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
118 
mbfl_filt_conv_utf7imap_wchar(int c,mbfl_convert_filter * filter)119 int mbfl_filt_conv_utf7imap_wchar(int c, mbfl_convert_filter *filter)
120 {
121 	int s, n = -1;
122 
123 	if (filter->status != 0) { /* Modified Base64 */
124 		if (c >= 'A' && c <= 'Z') {
125 			n = c - 65;
126 		} else if (c >= 'a' && c <= 'z') {
127 			n = c - 71;
128 		} else if (c >= '0' && c <= '9') {
129 			n = c + 4;
130 		} else if (c == '+') {
131 			n = 62;
132 		} else if (c == ',') {
133 			n = 63;
134 		}
135 
136 		if (n < 0 || n > 63) {
137 			if (c == '-') {
138 				if (filter->status == 1) { /* "&-" -> "&" */
139 					CK((*filter->output_function)('&', filter->data));
140 				} else if (filter->cache) {
141 					/* Base64-encoded section ended abruptly, with partially encoded characters,
142 					 * or it could be that it ended on the first half of a surrogate pair */
143 					CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
144 				}
145 			} else { /* illegal character */
146 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
147 			}
148 			filter->cache = filter->status = 0;
149 			return 0;
150 		}
151 	}
152 
153 	switch (filter->status) {
154 	/* directly encoded characters */
155 	case 0:
156 		if (c == '&') { /* shift character */
157 			filter->status++;
158 		} else if (c >= 0x20 && c <= 0x7E) { /* ASCII */
159 			CK((*filter->output_function)(c, filter->data));
160 		} else { /* illegal character */
161 			CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
162 		}
163 		break;
164 
165 	/* decode Modified Base64 */
166 	case 1:
167 	case 2:
168 		filter->cache |= n << 10;
169 		filter->status = 3;
170 		break;
171 	case 3:
172 		filter->cache |= n << 4;
173 		filter->status = 4;
174 		break;
175 	case 4:
176 		s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
177 		n = (n & 0x3) << 14;
178 		filter->status = 5;
179 		if (s >= 0xd800 && s < 0xdc00) {
180 			/* 1st part of surrogate pair */
181 			s = (((s & 0x3ff) << 16) + 0x400000) | n;
182 			filter->cache = s;
183 		} else if (s >= 0xdc00 && s < 0xe000) {
184 			/* 2nd part of surrogate pair */
185 			if (filter->cache & 0xfff0000) {
186 				s &= 0x3ff;
187 				s |= (filter->cache & 0xfff0000) >> 6;
188 				filter->cache = n;
189 				CK((*filter->output_function)(s, filter->data));
190 			} else { /* illegal character */
191 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
192 			}
193 		} else {
194 			filter->cache = n;
195 			/* Characters which can be expressed as literal, ASCII characters
196 			 * should not be Base64-encoded */
197 			if (s < 0x20 || s > 0x7E || s == '&') {
198 				CK((*filter->output_function)(s, filter->data));
199 			} else {
200 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
201 			}
202 		}
203 		break;
204 
205 	case 5:
206 		filter->cache |= n << 8;
207 		filter->status = 6;
208 		break;
209 	case 6:
210 		filter->cache |= n << 2;
211 		filter->status = 7;
212 		break;
213 	case 7:
214 		s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
215 		n = (n & 0xf) << 12;
216 		filter->status = 8;
217 		if (s >= 0xd800 && s < 0xdc00) {
218 			s = (((s & 0x3ff) << 16) + 0x400000) | n;
219 			filter->cache = s;
220 		} else if (s >= 0xdc00 && s < 0xe000) {
221 			if (filter->cache & 0xfff0000) {
222 				s &= 0x3ff;
223 				s |= (filter->cache & 0xfff0000) >> 6;
224 				filter->cache = n;
225 				CK((*filter->output_function)(s, filter->data));
226 			} else { /* illegal character */
227 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
228 			}
229 		} else {
230 			filter->cache = n;
231 			/* Characters which can be expressed as literal, ASCII characters
232 			 * should not be Base64-encoded */
233 			if (s < 0x20 || s > 0x7E || s == '&') {
234 				CK((*filter->output_function)(s, filter->data));
235 			} else {
236 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
237 			}
238 		}
239 		break;
240 
241 	case 8:
242 		filter->cache |= n << 6;
243 		filter->status = 9;
244 		break;
245 	case 9:
246 		s = n | (filter->cache & 0xffff);
247 		filter->status = 2;
248 		if (s >= 0xd800 && s < 0xdc00) {
249 			s = (((s & 0x3ff) << 16) + 0x400000);
250 			filter->cache = s;
251 		} else if (s >= 0xdc00 && s < 0xe000) {
252 			if (filter->cache & 0xfff0000) {
253 				s &= 0x3ff;
254 				s |= (filter->cache & 0xfff0000) >> 6;
255 				filter->cache = 0;
256 				CK((*filter->output_function)(s, filter->data));
257 			} else { /* illegal character */
258 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
259 			}
260 		} else {
261 			filter->cache = 0;
262 			/* Characters which can be expressed as literal, ASCII characters
263 			 * should not be Base64-encoded */
264 			if (s < 0x20 || s > 0x7E || s == '&') {
265 				CK((*filter->output_function)(s, filter->data));
266 			} else {
267 				CK((*filter->output_function)(MBFL_BAD_INPUT, filter->data));
268 			}
269 		}
270 		break;
271 
272 	default:
273 		filter->status = 0;
274 		break;
275 	}
276 
277 	return 0;
278 }
279 
mbfl_filt_conv_utf7imap_wchar_flush(mbfl_convert_filter * filter)280 static int mbfl_filt_conv_utf7imap_wchar_flush(mbfl_convert_filter *filter)
281 {
282 	if (filter->status) {
283 		/* It is illegal for a UTF-7 IMAP string to end in a Base-64 encoded
284 		 * section. It should always change back to ASCII before the end. */
285 		(*filter->output_function)(MBFL_BAD_INPUT, filter->data);
286 	}
287 
288 	if (filter->flush_function) {
289 		(*filter->flush_function)(filter->data);
290 	}
291 
292 	return 0;
293 }
294 
295 static const unsigned char mbfl_utf7imap_base64_table[] =
296 {
297  /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
298    0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
299  /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
300    0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
301  /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
302    0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
303  /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
304    0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
305  /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ',', '\0' */
306    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2c,0x00
307 };
308 
mbfl_filt_conv_wchar_utf7imap(int c,mbfl_convert_filter * filter)309 int mbfl_filt_conv_wchar_utf7imap(int c, mbfl_convert_filter *filter)
310 {
311 	int n = 0, s;
312 
313 	if (c == '&') {
314 		n = 1;
315 	} else if ((c >= 0x20 && c <= 0x7e) || c == 0) {
316 		n = 2;
317 	} else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
318 		;
319 	} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
320 		s = ((c >> 10) - 0x40) | 0xd800;
321 		CK((*filter->filter_function)(s, filter));
322 		s = (c & 0x3ff) | 0xdc00;
323 		CK((*filter->filter_function)(s, filter));
324 		return 0;
325 	} else {
326 		CK(mbfl_filt_conv_illegal_output(c, filter));
327 		return 0;
328 	}
329 
330 	switch (filter->status) {
331 	case 0:
332 		if (n != 0) {	/* directly encode characters */
333 			CK((*filter->output_function)(c, filter->data));
334 			if (n == 1) {
335 				CK((*filter->output_function)(0x2d, filter->data));		/* '-' */
336 			}
337 		} else {	/* Modified Base64 */
338 			CK((*filter->output_function)(0x26, filter->data));		/* '&' */
339 			filter->status = 1;
340 			filter->cache = c;
341 		}
342 		break;
343 
344 	/* encode Modified Base64 */
345 	case 1:
346 		s = filter->cache;
347 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 10) & 0x3f], filter->data));
348 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 4) & 0x3f], filter->data));
349 		if (n != 0) {
350 			CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 2) & 0x3c], filter->data));
351 			CK((*filter->output_function)('-', filter->data));
352 			CK((*filter->output_function)(c, filter->data));
353 			if (n == 1) {
354 				CK((*filter->output_function)('-', filter->data));
355 			}
356 			filter->status = 0;
357 		} else {
358 			filter->status = 2;
359 			filter->cache = ((s & 0xf) << 16) | c;
360 		}
361 		break;
362 
363 	case 2:
364 		s = filter->cache;
365 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 14) & 0x3f], filter->data));
366 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 8) & 0x3f], filter->data));
367 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 2) & 0x3f], filter->data));
368 		if (n != 0) {
369 			CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 4) & 0x30], filter->data));
370 			CK((*filter->output_function)('-', filter->data));
371 			CK((*filter->output_function)(c, filter->data));
372 			if (n == 1) {
373 				CK((*filter->output_function)('-', filter->data));
374 			}
375 			filter->status = 0;
376 		} else {
377 			filter->status = 3;
378 			filter->cache = ((s & 0x3) << 16) | c;
379 		}
380 		break;
381 
382 	case 3:
383 		s = filter->cache;
384 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 12) & 0x3f], filter->data));
385 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 6) & 0x3f], filter->data));
386 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[s & 0x3f], filter->data));
387 		if (n != 0) {
388 			CK((*filter->output_function)('-', filter->data));
389 			CK((*filter->output_function)(c, filter->data));
390 			if (n == 1) {
391 				CK((*filter->output_function)('-', filter->data));
392 			}
393 			filter->status = 0;
394 		} else {
395 			filter->status = 1;
396 			filter->cache = c;
397 		}
398 		break;
399 
400 	default:
401 		filter->status = 0;
402 		break;
403 	}
404 
405 	return 0;
406 }
407 
mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter * filter)408 static int mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter *filter)
409 {
410 	int status = filter->status, cache = filter->cache;
411 	filter->status = filter->cache = 0;
412 
413 	/* flush fragments */
414 	switch (status) {
415 	case 1:
416 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 10) & 0x3f], filter->data));
417 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 4) & 0x3f], filter->data));
418 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 2) & 0x3c], filter->data));
419 		CK((*filter->output_function)('-', filter->data));
420 		break;
421 
422 	case 2:
423 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 14) & 0x3f], filter->data));
424 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 8) & 0x3f], filter->data));
425 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 2) & 0x3f], filter->data));
426 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 4) & 0x30], filter->data));
427 		CK((*filter->output_function)('-', filter->data));
428 		break;
429 
430 	case 3:
431 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 12) & 0x3f], filter->data));
432 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 6) & 0x3f], filter->data));
433 		CK((*filter->output_function)(mbfl_utf7imap_base64_table[cache & 0x3f], filter->data));
434 		CK((*filter->output_function)('-', filter->data));
435 		break;
436 	}
437 	return 0;
438 }
439