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 	(const char *(*)[])&mbfl_encoding_qprint_aliases,
41 	NULL,
42 	MBFL_ENCTYPE_GL_UNSAFE,
43 	NULL,
44 	NULL
45 };
46 
47 const struct mbfl_convert_vtbl vtbl_8bit_qprint = {
48 	mbfl_no_encoding_8bit,
49 	mbfl_no_encoding_qprint,
50 	mbfl_filt_conv_common_ctor,
51 	NULL,
52 	mbfl_filt_conv_qprintenc,
53 	mbfl_filt_conv_qprintenc_flush,
54 	NULL,
55 };
56 
57 const struct mbfl_convert_vtbl vtbl_qprint_8bit = {
58 	mbfl_no_encoding_qprint,
59 	mbfl_no_encoding_8bit,
60 	mbfl_filt_conv_common_ctor,
61 	NULL,
62 	mbfl_filt_conv_qprintdec,
63 	mbfl_filt_conv_qprintdec_flush,
64 	NULL,
65 };
66 
67 
68 #define CK(statement)	do { if ((statement) < 0) return (-1); } while (0)
69 
70 /*
71  * any => Quoted-Printable
72  */
73 
mbfl_filt_conv_qprintenc(int c,mbfl_convert_filter * filter)74 int mbfl_filt_conv_qprintenc(int c, mbfl_convert_filter *filter)
75 {
76 	int s, n;
77 
78 	switch (filter->status & 0xff) {
79 	case 0:
80 		filter->cache = c;
81 		filter->status++;
82 		break;
83 	default:
84 		s = filter->cache;
85 		filter->cache = c;
86 		n = (filter->status & 0xff00) >> 8;
87 
88 		if (s == 0) {		/* null */
89 			CK((*filter->output_function)(s, filter->data));
90 			filter->status &= ~0xff00;
91 			break;
92 		}
93 
94 		if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0) {
95 			if (s == 0x0a || (s == 0x0d && c != 0x0a)) {	/* line feed */
96 				CK((*filter->output_function)(0x0d, filter->data));		/* CR */
97 				CK((*filter->output_function)(0x0a, filter->data));		/* LF */
98 				filter->status &= ~0xff00;
99 				break;
100 			} else if (s == 0x0d) {
101 				break;
102 			}
103 		}
104 
105 		if ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) == 0  && n >= 72) {	/* soft line feed */
106 			CK((*filter->output_function)(0x3d, filter->data));		/* '=' */
107 			CK((*filter->output_function)(0x0d, filter->data));		/* CR */
108 			CK((*filter->output_function)(0x0a, filter->data));		/* LF */
109 			filter->status &= ~0xff00;
110 		}
111 
112 		if (s <= 0 || s >= 0x80 || s == 0x3d		/* not ASCII or '=' */
113 		   || ((filter->status & MBFL_QPRINT_STS_MIME_HEADER) != 0 &&
114 		       (mbfl_charprop_table[s] & MBFL_CHP_MMHQENC) != 0)) {
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 c;
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 c;
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