2
* "streamable kanji code filter and converter"
3
* Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
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.
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.
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
21
* The author of this file:
25
* The source code included in this files was separated from mbfilter.c
26
* by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
35
#include "mbfilter_utf7.h"
37
static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter);
39
static const unsigned char mbfl_base64_table[] = {
40
/* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
41
0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
42
/* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
43
0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
44
/* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
45
0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
46
/* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
47
0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
48
/* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' */
49
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f,0x00
52
static const char *mbfl_encoding_utf7_aliases[] = {"utf7", NULL};
54
const mbfl_encoding mbfl_encoding_utf7 = {
55
mbfl_no_encoding_utf7,
58
(const char *(*)[])&mbfl_encoding_utf7_aliases,
60
MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
63
const struct mbfl_identify_vtbl vtbl_identify_utf7 = {
64
mbfl_no_encoding_utf7,
65
mbfl_filt_ident_common_ctor,
66
mbfl_filt_ident_common_dtor,
70
const struct mbfl_convert_vtbl vtbl_utf7_wchar = {
71
mbfl_no_encoding_utf7,
72
mbfl_no_encoding_wchar,
73
mbfl_filt_conv_common_ctor,
74
mbfl_filt_conv_common_dtor,
75
mbfl_filt_conv_utf7_wchar,
76
mbfl_filt_conv_common_flush
79
const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
80
mbfl_no_encoding_wchar,
81
mbfl_no_encoding_utf7,
82
mbfl_filt_conv_common_ctor,
83
mbfl_filt_conv_common_dtor,
84
mbfl_filt_conv_wchar_utf7,
85
mbfl_filt_conv_wchar_utf7_flush
89
#define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
94
int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
99
if (filter->status != 0) { /* Modified Base64 */
100
if (c >= 0x41 && c <= 0x5a) { /* A - Z */
102
} else if (c >= 0x61 && c <= 0x7a) { /* a - z */
104
} else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
106
} else if (c == 0x2b) { /* '+' */
108
} else if (c == 0x2f) { /* '/' */
111
if (n < 0 || n > 63) {
113
if (filter->status == 1) { /* "+-" -> "+" */
114
CK((*filter->output_function)(0x2b, filter->data));
116
} else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
117
CK((*filter->output_function)(c, filter->data));
118
} else { /* illegal character */
119
s = c & MBFL_WCSGROUP_MASK;
120
s |= MBFL_WCSGROUP_THROUGH;
121
CK((*filter->output_function)(s, filter->data));
129
switch (filter->status) {
130
/* directly encoded characters */
132
if (c == 0x2b) { /* '+' shift character */
134
} else if (c >= 0 && c < 0x80) { /* ASCII */
135
CK((*filter->output_function)(c, filter->data));
136
} else { /* illegal character */
137
s = c & MBFL_WCSGROUP_MASK;
138
s |= MBFL_WCSGROUP_THROUGH;
139
CK((*filter->output_function)(s, filter->data));
143
/* decode Modified Base64 */
146
filter->cache |= n << 10;
150
filter->cache |= n << 4;
154
s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
157
if (s >= 0xd800 && s < 0xdc00) {
158
s = (((s & 0x3ff) << 16) + 0x400000) | n;
160
} else if (s >= 0xdc00 && s < 0xe000) {
162
s |= (filter->cache & 0xfff0000) >> 6;
164
if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
165
CK((*filter->output_function)(s, filter->data));
166
} else { /* illegal character */
167
s &= MBFL_WCSGROUP_MASK;
168
s |= MBFL_WCSGROUP_THROUGH;
169
CK((*filter->output_function)(s, filter->data));
173
CK((*filter->output_function)(s, filter->data));
178
filter->cache |= n << 8;
182
filter->cache |= n << 2;
186
s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
189
if (s >= 0xd800 && s < 0xdc00) {
190
s = (((s & 0x3ff) << 16) + 0x400000) | n;
192
} else if (s >= 0xdc00 && s < 0xe000) {
194
s |= (filter->cache & 0xfff0000) >> 6;
196
if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
197
CK((*filter->output_function)(s, filter->data));
198
} else { /* illegal character */
199
s &= MBFL_WCSGROUP_MASK;
200
s |= MBFL_WCSGROUP_THROUGH;
201
CK((*filter->output_function)(s, filter->data));
205
CK((*filter->output_function)(s, filter->data));
210
filter->cache |= n << 6;
214
s = n | (filter->cache & 0xffff);
216
if (s >= 0xd800 && s < 0xdc00) {
217
s = (((s & 0x3ff) << 16) + 0x400000);
219
} else if (s >= 0xdc00 && s < 0xe000) {
221
s |= (filter->cache & 0xfff0000) >> 6;
223
if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
224
CK((*filter->output_function)(s, filter->data));
225
} else { /* illegal character */
226
s &= MBFL_WCSGROUP_MASK;
227
s |= MBFL_WCSGROUP_THROUGH;
228
CK((*filter->output_function)(s, filter->data));
232
CK((*filter->output_function)(s, filter->data));
247
int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
252
if (c >= 0 && c < 0x80) { /* ASCII */
253
if (c >= 0x41 && c <= 0x5a) { /* A - Z */
255
} else if (c >= 0x61 && c <= 0x7a) { /* a - z */
257
} else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
259
} else if (c == '\0') { /* '\0' */
261
} else if (c == 0x2f) { /* '/' */
263
} else if (c == 0x2d) { /* '-' */
265
} else if (c == 0x20) { /* SPACE */
267
} else if (c == 0x09) { /* HTAB */
269
} else if (c == 0x0d) { /* CR */
271
} else if (c == 0x0a) { /* LF */
273
} else if (c == 0x27) { /* "'" */
275
} else if (c == 0x28) { /* '(' */
277
} else if (c == 0x29) { /* ')' */
279
} else if (c == 0x2c) { /* ',' */
281
} else if (c == 0x2e) { /* '.' */
283
} else if (c == 0x3a) { /* ':' */
285
} else if (c == 0x3f) { /* '?' */
288
} else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
290
} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
291
s = ((c >> 10) - 0x40) | 0xd800;
292
CK((*filter->filter_function)(s, filter));
293
s = (c & 0x3ff) | 0xdc00;
294
CK((*filter->filter_function)(s, filter));
297
if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
298
CK(mbfl_filt_conv_illegal_output(c, filter));
303
switch (filter->status) {
305
if (n != 0) { /* directly encode characters */
306
CK((*filter->output_function)(c, filter->data));
307
} else { /* Modified Base64 */
308
CK((*filter->output_function)(0x2b, filter->data)); /* '+' */
314
/* encode Modified Base64 */
317
CK((*filter->output_function)(mbfl_base64_table[(s >> 10) & 0x3f], filter->data));
318
CK((*filter->output_function)(mbfl_base64_table[(s >> 4) & 0x3f], filter->data));
320
CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
322
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
324
CK((*filter->output_function)(c, filter->data));
328
filter->cache = ((s & 0xf) << 16) | c;
334
CK((*filter->output_function)(mbfl_base64_table[(s >> 14) & 0x3f], filter->data));
335
CK((*filter->output_function)(mbfl_base64_table[(s >> 8) & 0x3f], filter->data));
336
CK((*filter->output_function)(mbfl_base64_table[(s >> 2) & 0x3f], filter->data));
338
CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
340
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
342
CK((*filter->output_function)(c, filter->data));
346
filter->cache = ((s & 0x3) << 16) | c;
352
CK((*filter->output_function)(mbfl_base64_table[(s >> 12) & 0x3f], filter->data));
353
CK((*filter->output_function)(mbfl_base64_table[(s >> 6) & 0x3f], filter->data));
354
CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
357
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
359
CK((*filter->output_function)(c, filter->data));
376
int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
380
status = filter->status;
381
cache = filter->cache;
384
/* flush fragments */
387
CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
388
CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
389
CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
390
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
394
CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
395
CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
396
CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
397
CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
398
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
402
CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
403
CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
404
CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
405
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
411
static int mbfl_filt_ident_utf7(int c, mbfl_identify_filter *filter)
415
switch (filter->status) {
416
/* directly encoded characters */
418
if (c == 0x2b) { /* '+' shift character */
420
} else if (c == 0x5c || c == 0x7e || c < 0 || c > 0x7f) { /* illegal character */
421
filter->flag = 1; /* bad */
425
/* Modified Base64 */
429
if (c >= 0x41 && c <= 0x5a) { /* A - Z */
431
} else if (c >= 0x61 && c <= 0x7a) { /* a - z */
433
} else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
435
} else if (c == 0x2b) { /* '+' */
437
} else if (c == 0x2f) { /* '/' */
441
if (filter->status == 1 && c != 0x2d) {
442
filter->flag = 1; /* bad */
443
} else if (c < 0 || c > 0x7f) {
444
filter->flag = 1; /* bad */