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_utf7.h"
32
33 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter);
34
35 static const unsigned char mbfl_base64_table[] = {
36 /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
37 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
38 /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
39 0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
40 /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
41 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
42 /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
43 0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
44 /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
45 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
46 };
47
48 static const char *mbfl_encoding_utf7_aliases[] = {"utf7", NULL};
49
50 const mbfl_encoding mbfl_encoding_utf7 = {
51 mbfl_no_encoding_utf7,
52 "UTF-7",
53 "UTF-7",
54 (const char *(*)[])&mbfl_encoding_utf7_aliases,
55 NULL,
56 MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE,
57 &vtbl_utf7_wchar,
58 &vtbl_wchar_utf7
59 };
60
61 const struct mbfl_identify_vtbl vtbl_identify_utf7 = {
62 mbfl_no_encoding_utf7,
63 mbfl_filt_ident_common_ctor,
64 mbfl_filt_ident_utf7
65 };
66
67 const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
68 mbfl_no_encoding_utf7,
69 mbfl_no_encoding_wchar,
70 mbfl_filt_conv_common_ctor,
71 NULL,
72 mbfl_filt_conv_utf7_wchar,
73 mbfl_filt_conv_common_flush,
74 NULL,
75 };
76
77 const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
78 mbfl_no_encoding_wchar,
79 mbfl_no_encoding_utf7,
80 mbfl_filt_conv_common_ctor,
81 NULL,
82 mbfl_filt_conv_wchar_utf7,
83 mbfl_filt_conv_wchar_utf7_flush,
84 NULL,
85 };
86
87
88 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
89
90 /*
91 * UTF-7 => wchar
92 */
mbfl_filt_conv_utf7_wchar(int c,mbfl_convert_filter * filter)93 int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
94 {
95 int s, n;
96
97 n = -1;
98 if (filter->status != 0) { /* Modified Base64 */
99 if (c >= 0x41 && c <= 0x5a) { /* A - Z */
100 n = c - 65;
101 } else if (c >= 0x61 && c <= 0x7a) { /* a - z */
102 n = c - 71;
103 } else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
104 n = c + 4;
105 } else if (c == 0x2b) { /* '+' */
106 n = 62;
107 } else if (c == 0x2f) { /* '/' */
108 n = 63;
109 }
110 if (n < 0 || n > 63) {
111 if (c == 0x2d) {
112 if (filter->status == 1) { /* "+-" -> "+" */
113 CK((*filter->output_function)(0x2b, filter->data));
114 }
115 } else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
116 CK((*filter->output_function)(c, filter->data));
117 } else { /* illegal character */
118 s = c & MBFL_WCSGROUP_MASK;
119 s |= MBFL_WCSGROUP_THROUGH;
120 CK((*filter->output_function)(s, filter->data));
121 }
122 filter->cache = 0;
123 filter->status = 0;
124 return c;
125 }
126 }
127
128 switch (filter->status) {
129 /* directly encoded characters */
130 case 0:
131 if (c == 0x2b) { /* '+' shift character */
132 filter->status = 1;
133 } else if (c >= 0 && c < 0x80) { /* ASCII */
134 CK((*filter->output_function)(c, filter->data));
135 } else { /* illegal character */
136 s = c & MBFL_WCSGROUP_MASK;
137 s |= MBFL_WCSGROUP_THROUGH;
138 CK((*filter->output_function)(s, filter->data));
139 }
140 break;
141
142 /* decode Modified Base64 */
143 case 1:
144 case 2:
145 filter->cache |= n << 10;
146 filter->status = 3;
147 break;
148 case 3:
149 filter->cache |= n << 4;
150 filter->status = 4;
151 break;
152 case 4:
153 s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
154 n = (n & 0x3) << 14;
155 filter->status = 5;
156 if (s >= 0xd800 && s < 0xdc00) {
157 s = (((s & 0x3ff) << 16) + 0x400000) | n;
158 filter->cache = s;
159 } else if (s >= 0xdc00 && s < 0xe000) {
160 s &= 0x3ff;
161 s |= (filter->cache & 0xfff0000) >> 6;
162 filter->cache = n;
163 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
164 CK((*filter->output_function)(s, filter->data));
165 } else { /* illegal character */
166 s &= MBFL_WCSGROUP_MASK;
167 s |= MBFL_WCSGROUP_THROUGH;
168 CK((*filter->output_function)(s, filter->data));
169 }
170 } else {
171 filter->cache = n;
172 CK((*filter->output_function)(s, filter->data));
173 }
174 break;
175
176 case 5:
177 filter->cache |= n << 8;
178 filter->status = 6;
179 break;
180 case 6:
181 filter->cache |= n << 2;
182 filter->status = 7;
183 break;
184 case 7:
185 s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
186 n = (n & 0xf) << 12;
187 filter->status = 8;
188 if (s >= 0xd800 && s < 0xdc00) {
189 s = (((s & 0x3ff) << 16) + 0x400000) | n;
190 filter->cache = s;
191 } else if (s >= 0xdc00 && s < 0xe000) {
192 s &= 0x3ff;
193 s |= (filter->cache & 0xfff0000) >> 6;
194 filter->cache = n;
195 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
196 CK((*filter->output_function)(s, filter->data));
197 } else { /* illegal character */
198 s &= MBFL_WCSGROUP_MASK;
199 s |= MBFL_WCSGROUP_THROUGH;
200 CK((*filter->output_function)(s, filter->data));
201 }
202 } else {
203 filter->cache = n;
204 CK((*filter->output_function)(s, filter->data));
205 }
206 break;
207
208 case 8:
209 filter->cache |= n << 6;
210 filter->status = 9;
211 break;
212 case 9:
213 s = n | (filter->cache & 0xffff);
214 filter->status = 2;
215 if (s >= 0xd800 && s < 0xdc00) {
216 s = (((s & 0x3ff) << 16) + 0x400000);
217 filter->cache = s;
218 } else if (s >= 0xdc00 && s < 0xe000) {
219 s &= 0x3ff;
220 s |= (filter->cache & 0xfff0000) >> 6;
221 filter->cache = 0;
222 if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
223 CK((*filter->output_function)(s, filter->data));
224 } else { /* illegal character */
225 s &= MBFL_WCSGROUP_MASK;
226 s |= MBFL_WCSGROUP_THROUGH;
227 CK((*filter->output_function)(s, filter->data));
228 }
229 } else {
230 filter->cache = 0;
231 CK((*filter->output_function)(s, filter->data));
232 }
233 break;
234
235 default:
236 filter->status = 0;
237 break;
238 }
239
240 return c;
241 }
242
243 /*
244 * wchar => UTF-7
245 */
mbfl_filt_conv_wchar_utf7(int c,mbfl_convert_filter * filter)246 int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
247 {
248 int s, n;
249
250 n = 0;
251 if (c >= 0 && c < 0x80) { /* ASCII */
252 if (c >= 0x41 && c <= 0x5a) { /* A - Z */
253 n = 1;
254 } else if (c >= 0x61 && c <= 0x7a) { /* a - z */
255 n = 1;
256 } else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
257 n = 1;
258 } else if (c == '\0') { /* '\0' */
259 n = 1;
260 } else if (c == 0x2f) { /* '/' */
261 n = 1;
262 } else if (c == 0x2d) { /* '-' */
263 n = 1;
264 } else if (c == 0x20) { /* SPACE */
265 n = 2;
266 } else if (c == 0x09) { /* HTAB */
267 n = 2;
268 } else if (c == 0x0d) { /* CR */
269 n = 2;
270 } else if (c == 0x0a) { /* LF */
271 n = 2;
272 } else if (c == 0x27) { /* "'" */
273 n = 2;
274 } else if (c == 0x28) { /* '(' */
275 n = 2;
276 } else if (c == 0x29) { /* ')' */
277 n = 2;
278 } else if (c == 0x2c) { /* ',' */
279 n = 2;
280 } else if (c == 0x2e) { /* '.' */
281 n = 2;
282 } else if (c == 0x3a) { /* ':' */
283 n = 2;
284 } else if (c == 0x3f) { /* '?' */
285 n = 2;
286 }
287 } else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
288 ;
289 } else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
290 s = ((c >> 10) - 0x40) | 0xd800;
291 CK((*filter->filter_function)(s, filter));
292 s = (c & 0x3ff) | 0xdc00;
293 CK((*filter->filter_function)(s, filter));
294 return c;
295 } else {
296 CK(mbfl_filt_conv_illegal_output(c, filter));
297 return c;
298 }
299
300 switch (filter->status) {
301 case 0:
302 if (n != 0) { /* directly encode characters */
303 CK((*filter->output_function)(c, filter->data));
304 } else { /* Modified Base64 */
305 CK((*filter->output_function)(0x2b, filter->data)); /* '+' */
306 filter->status++;
307 filter->cache = c;
308 }
309 break;
310
311 /* encode Modified Base64 */
312 case 1:
313 s = filter->cache;
314 CK((*filter->output_function)(mbfl_base64_table[(s >> 10) & 0x3f], filter->data));
315 CK((*filter->output_function)(mbfl_base64_table[(s >> 4) & 0x3f], filter->data));
316 if (n != 0) {
317 CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
318 if (n == 1) {
319 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
320 }
321 CK((*filter->output_function)(c, filter->data));
322 filter->status = 0;
323 } else {
324 filter->status++;
325 filter->cache = ((s & 0xf) << 16) | c;
326 }
327 break;
328
329 case 2:
330 s = filter->cache;
331 CK((*filter->output_function)(mbfl_base64_table[(s >> 14) & 0x3f], filter->data));
332 CK((*filter->output_function)(mbfl_base64_table[(s >> 8) & 0x3f], filter->data));
333 CK((*filter->output_function)(mbfl_base64_table[(s >> 2) & 0x3f], filter->data));
334 if (n != 0) {
335 CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
336 if (n == 1) {
337 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
338 }
339 CK((*filter->output_function)(c, filter->data));
340 filter->status = 0;
341 } else {
342 filter->status++;
343 filter->cache = ((s & 0x3) << 16) | c;
344 }
345 break;
346
347 case 3:
348 s = filter->cache;
349 CK((*filter->output_function)(mbfl_base64_table[(s >> 12) & 0x3f], filter->data));
350 CK((*filter->output_function)(mbfl_base64_table[(s >> 6) & 0x3f], filter->data));
351 CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
352 if (n != 0) {
353 if (n == 1) {
354 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
355 }
356 CK((*filter->output_function)(c, filter->data));
357 filter->status = 0;
358 } else {
359 filter->status = 1;
360 filter->cache = c;
361 }
362 break;
363
364 default:
365 filter->status = 0;
366 break;
367 }
368
369 return c;
370
371 }
372
mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter * filter)373 int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
374 {
375 int status, cache;
376
377 status = filter->status;
378 cache = filter->cache;
379 filter->status = 0;
380 filter->cache = 0;
381 /* flush fragments */
382 switch (status) {
383 case 1:
384 CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
385 CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
386 CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
387 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
388 break;
389
390 case 2:
391 CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
392 CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
393 CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
394 CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
395 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
396 break;
397
398 case 3:
399 CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
400 CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
401 CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
402 CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
403 break;
404 }
405
406 if (filter->flush_function != NULL) {
407 (*filter->flush_function)(filter->data);
408 }
409
410 return 0;
411 }
412
mbfl_filt_ident_utf7(int c,mbfl_identify_filter * filter)413 static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter)
414 {
415 int n;
416
417 switch (filter->status) {
418 /* directly encoded characters */
419 case 0:
420 if (c == 0x2b) { /* '+' shift character */
421 filter->status++;
422 } else if (c == 0x5c || c == 0x7e || c < 0 || c > 0x7f) { /* illegal character */
423 filter->flag = 1; /* bad */
424 }
425 break;
426
427 /* Modified Base64 */
428 case 1:
429 case 2:
430 n = 0;
431 if (c >= 0x41 && c <= 0x5a) { /* A - Z */
432 n = 1;
433 } else if (c >= 0x61 && c <= 0x7a) { /* a - z */
434 n = 1;
435 } else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
436 n = 1;
437 } else if (c == 0x2b) { /* '+' */
438 n = 1;
439 } else if (c == 0x2f) { /* '/' */
440 n = 1;
441 }
442 if (n <= 0) {
443 if (filter->status == 1 && c != 0x2d) {
444 filter->flag = 1; /* bad */
445 } else if (c < 0 || c > 0x7f) {
446 filter->flag = 1; /* bad */
447 }
448 filter->status = 0;
449 } else {
450 filter->status = 2;
451 }
452 break;
453
454 default:
455 filter->status = 0;
456 break;
457 }
458
459 return c;
460 }
461