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_qprint.h"
32 #include "unicode_prop.h"
33
34 static const char *mbfl_encoding_qprint_aliases[] = {"qprint", NULL};
35
36 const mbfl_encoding mbfl_encoding_qprint = {
37 mbfl_no_encoding_qprint,
38 "Quoted-Printable",
39 "Quoted-Printable",
40 mbfl_encoding_qprint_aliases,
41 NULL,
42 MBFL_ENCTYPE_GL_UNSAFE,
43 NULL,
44 NULL,
45 NULL
46 };
47
48 const struct mbfl_convert_vtbl vtbl_8bit_qprint = {
49 mbfl_no_encoding_8bit,
50 mbfl_no_encoding_qprint,
51 mbfl_filt_conv_common_ctor,
52 NULL,
53 mbfl_filt_conv_qprintenc,
54 mbfl_filt_conv_qprintenc_flush,
55 NULL,
56 };
57
58 const struct mbfl_convert_vtbl vtbl_qprint_8bit = {
59 mbfl_no_encoding_qprint,
60 mbfl_no_encoding_8bit,
61 mbfl_filt_conv_common_ctor,
62 NULL,
63 mbfl_filt_conv_qprintdec,
64 mbfl_filt_conv_qprintdec_flush,
65 NULL,
66 };
67
68
69 #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
70
71 /*
72 * any => Quoted-Printable
73 */
74
mbfl_filt_conv_qprintenc(int c,mbfl_convert_filter * filter)75 int mbfl_filt_conv_qprintenc(int c, mbfl_convert_filter *filter)
76 {
77 int s, n;
78
79 switch (filter->status & 0xff) {
80 case 0:
81 filter->cache = c;
82 filter->status++;
83 break;
84 default:
85 s = filter->cache;
86 filter->cache = c;
87 n = (filter->status & 0xff00) >> 8;
88
89 if (s == 0) { /* null */
90 CK((*filter->output_function)(s, filter->data));
91 filter->status &= ~0xff00;
92 break;
93 }
94
95 if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0) {
96 if (s == 0x0a || (s == 0x0d && c != 0x0a)) { /* line feed */
97 CK((*filter->output_function)(0x0d, filter->data)); /* CR */
98 CK((*filter->output_function)(0x0a, filter->data)); /* LF */
99 filter->status &= ~0xff00;
100 break;
101 } else if (s == 0x0d) {
102 break;
103 }
104 }
105
106 if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0 && n >= 72) { /* soft line feed */
107 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
108 CK((*filter->output_function)(0x0d, filter->data)); /* CR */
109 CK((*filter->output_function)(0x0a, filter->data)); /* LF */
110 filter->status &= ~0xff00;
111 }
112
113 if (s <= 0 || s >= 0x80 || s == 0x3d /* not ASCII or '=' */
114 || ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) && mime_char_needs_qencode[s])) {
115 /* hex-octet */
116 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
117 n = (s >> 4) & 0xf;
118 if (n < 10) {
119 n += 48; /* '0' */
120 } else {
121 n += 55; /* 'A' - 10 */
122 }
123 CK((*filter->output_function)(n, filter->data));
124 n = s & 0xf;
125 if (n < 10) {
126 n += 48;
127 } else {
128 n += 55;
129 }
130 CK((*filter->output_function)(n, filter->data));
131 if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0) {
132 filter->status += 0x300;
133 }
134 } else {
135 CK((*filter->output_function)(s, filter->data));
136 if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0) {
137 filter->status += 0x100;
138 }
139 }
140 break;
141 }
142
143 return 0;
144 }
145
mbfl_filt_conv_qprintenc_flush(mbfl_convert_filter * filter)146 int mbfl_filt_conv_qprintenc_flush(mbfl_convert_filter *filter)
147 {
148 /* flush filter cache */
149 (*filter->filter_function)('\0', filter);
150 filter->status &= ~0xffff;
151 filter->cache = 0;
152 return 0;
153 }
154
155 /*
156 * Quoted-Printable => any
157 */
mbfl_filt_conv_qprintdec(int c,mbfl_convert_filter * filter)158 int mbfl_filt_conv_qprintdec(int c, mbfl_convert_filter *filter)
159 {
160 int n, m;
161
162 static int hex2code_map[] = {
163 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
164 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
165 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
166 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
167 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
171 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
172 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
179 };
180
181 switch (filter->status) {
182 case 1:
183 if (hex2code_map[c & 0xff] >= 0) {
184 filter->cache = c;
185 filter->status = 2;
186 } else if (c == 0x0d) { /* soft line feed */
187 filter->status = 3;
188 } else if (c == 0x0a) { /* soft line feed */
189 filter->status = 0;
190 } else {
191 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
192 CK((*filter->output_function)(c, filter->data));
193 filter->status = 0;
194 }
195 break;
196 case 2:
197 m = hex2code_map[c & 0xff];
198 if (m < 0) {
199 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
200 CK((*filter->output_function)(filter->cache, filter->data));
201 n = c;
202 } else {
203 n = hex2code_map[filter->cache] << 4 | m;
204 }
205 CK((*filter->output_function)(n, filter->data));
206 filter->status = 0;
207 break;
208 case 3:
209 if (c != 0x0a) { /* LF */
210 CK((*filter->output_function)(c, filter->data));
211 }
212 filter->status = 0;
213 break;
214 default:
215 if (c == 0x3d) { /* '=' */
216 filter->status = 1;
217 } else {
218 CK((*filter->output_function)(c, filter->data));
219 }
220 break;
221 }
222
223 return 0;
224 }
225
mbfl_filt_conv_qprintdec_flush(mbfl_convert_filter * filter)226 int mbfl_filt_conv_qprintdec_flush(mbfl_convert_filter *filter)
227 {
228 int status, cache;
229
230 status = filter->status;
231 cache = filter->cache;
232 filter->status = 0;
233 filter->cache = 0;
234 /* flush fragments */
235 if (status == 1) {
236 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
237 } else if (status == 2) {
238 CK((*filter->output_function)(0x3d, filter->data)); /* '=' */
239 CK((*filter->output_function)(cache, filter->data));
240 }
241
242 return 0;
243 }
244