xref: /php-src/ext/standard/quot_print.c (revision 312f789e)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Kirill Maximov <kir@actimind.com>                            |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include <stdlib.h>
18 
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <string.h>
23 #include <errno.h>
24 
25 #include "php.h"
26 #include "quot_print.h"
27 
28 #include <stdio.h>
29 
30 /*
31 *  Converting HEX char to INT value
32 */
php_hex2int(int c)33 static char php_hex2int(int c) /* {{{ */
34 {
35 	if (isdigit(c)) {
36 		return c - '0';
37 	} else if (c >= 'A' && c <= 'F') {
38 		return c - 'A' + 10;
39 	} else {
40 		ZEND_ASSERT(c >= 'a' && c <= 'f');
41 		return c - 'a' + 10;
42 	}
43 }
44 /* }}} */
45 
php_quot_print_decode(const unsigned char * str,size_t length,int replace_us_by_ws)46 PHPAPI zend_string *php_quot_print_decode(const unsigned char *str, size_t length, int replace_us_by_ws) /* {{{ */
47 {
48 	size_t i;
49 	unsigned const char *p1;
50 	unsigned char *p2;
51 	unsigned int h_nbl, l_nbl;
52 
53 	size_t decoded_len, buf_size;
54 	zend_string *retval;
55 
56 	static unsigned int hexval_tbl[256] = {
57 		64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 16, 64, 64, 16, 64, 64,
58 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
59 		32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
60 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 64, 64, 64, 64, 64, 64,
61 		64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64,
62 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
63 		64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
65 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
66 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
67 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
68 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
69 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
70 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
71 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
72 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
73 	};
74 
75 	if (replace_us_by_ws) {
76 		replace_us_by_ws = '_';
77 	}
78 
79 	i = length, p1 = str; buf_size = length;
80 
81 	while (i > 1 && *p1 != '\0') {
82 		if (*p1 == '=') {
83 			buf_size -= 2;
84 			p1++;
85 			i--;
86 		}
87 		p1++;
88 		i--;
89 	}
90 
91 	retval = zend_string_alloc(buf_size, 0);
92 	i = length; p1 = str; p2 = (unsigned char*)ZSTR_VAL(retval);
93 	decoded_len = 0;
94 
95 	while (i > 0 && *p1 != '\0') {
96 		if (*p1 == '=') {
97 			i--, p1++;
98 			if (i == 0 || *p1 == '\0') {
99 				break;
100 			}
101 			h_nbl = hexval_tbl[*p1];
102 			if (h_nbl < 16) {
103 				/* next char should be a hexadecimal digit */
104 				if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
105 					efree(retval);
106 					return NULL;
107 				}
108 				*(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
109 				i--, p1++;
110 			} else if (h_nbl < 64) {
111 				/* soft line break */
112 				while (h_nbl == 32) {
113 					if (--i == 0 || (h_nbl = hexval_tbl[*(++p1)]) == 64) {
114 						efree(retval);
115 						return NULL;
116 					}
117 				}
118 				if (p1[0] == '\r' && i >= 2 && p1[1] == '\n') {
119 					i--, p1++;
120 				}
121 				i--, p1++;
122 			} else {
123 				efree(retval);
124 				return NULL;
125 			}
126 		} else {
127 			*(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
128 			i--, p1++, decoded_len++;
129 		}
130 	}
131 
132 	*p2 = '\0';
133 	ZSTR_LEN(retval) = decoded_len;
134 	return retval;
135 }
136 /* }}} */
137 
138 #define PHP_QPRINT_MAXL 75
139 
php_quot_print_encode(const unsigned char * str,size_t length)140 PHPAPI zend_string *php_quot_print_encode(const unsigned char *str, size_t length) /* {{{ */
141 {
142 	zend_ulong lp = 0;
143 	unsigned char c, *d;
144 	const char *hex = "0123456789ABCDEF";
145 	zend_string *ret;
146 
147 	ret = zend_string_safe_alloc(3, (length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1)), 0, 0);
148 	d = (unsigned char*)ZSTR_VAL(ret);
149 
150 	while (length--) {
151 		if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
152 			*d++ = '\015';
153 			*d++ = *str++;
154 			length--;
155 			lp = 0;
156 		} else {
157 			if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
158 				if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0x7f))
159 				    || ((c > 0x7f) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL))
160 				    || ((c > 0xdf) && (c <= 0xef) && ((lp + 6) > PHP_QPRINT_MAXL))
161 				    || ((c > 0xef) && (c <= 0xf4) && ((lp + 9) > PHP_QPRINT_MAXL))) {
162 					*d++ = '=';
163 					*d++ = '\015';
164 					*d++ = '\012';
165 					lp = 3;
166 				}
167 				*d++ = '=';
168 				*d++ = hex[c >> 4];
169 				*d++ = hex[c & 0xf];
170 			} else {
171 				if ((++lp) > PHP_QPRINT_MAXL) {
172 					*d++ = '=';
173 					*d++ = '\015';
174 					*d++ = '\012';
175 					lp = 1;
176 				}
177 				*d++ = c;
178 			}
179 		}
180 	}
181 	*d = '\0';
182 	ret = zend_string_truncate(ret, d - (unsigned char*)ZSTR_VAL(ret), 0);
183 	return ret;
184 }
185 /* }}} */
186 
187 /*
188 *
189 * Decoding  Quoted-printable string.
190 *
191 */
192 /* {{{ Convert a quoted-printable string to an 8 bit string */
PHP_FUNCTION(quoted_printable_decode)193 PHP_FUNCTION(quoted_printable_decode)
194 {
195 	zend_string *arg1;
196 	char *str_in;
197 	zend_string *str_out;
198 	size_t i = 0, j = 0, k;
199 
200 	ZEND_PARSE_PARAMETERS_START(1, 1)
201 		Z_PARAM_STR(arg1)
202 	ZEND_PARSE_PARAMETERS_END();
203 
204 	if (ZSTR_LEN(arg1) == 0) {
205 		/* shortcut */
206 		RETURN_EMPTY_STRING();
207 	}
208 
209 	str_in = ZSTR_VAL(arg1);
210 	str_out = zend_string_alloc(ZSTR_LEN(arg1), 0);
211 	while (str_in[i]) {
212 		switch (str_in[i]) {
213 		case '=':
214 			if (str_in[i + 1] && str_in[i + 2] &&
215 				isxdigit((int) str_in[i + 1]) &&
216 				isxdigit((int) str_in[i + 2]))
217 			{
218 				ZSTR_VAL(str_out)[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
219 						+ php_hex2int((int) str_in[i + 2]);
220 				i += 3;
221 			} else  /* check for soft line break according to RFC 2045*/ {
222 				k = 1;
223 				while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
224 					/* Possibly, skip spaces/tabs at the end of line */
225 					k++;
226 				}
227 				if (!str_in[i + k]) {
228 					/* End of line reached */
229 					i += k;
230 				}
231 				else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
232 					/* CRLF */
233 					i += k + 2;
234 				}
235 				else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
236 					/* CR or LF */
237 					i += k + 1;
238 				}
239 				else {
240 					ZSTR_VAL(str_out)[j++] = str_in[i++];
241 				}
242 			}
243 			break;
244 		default:
245 			ZSTR_VAL(str_out)[j++] = str_in[i++];
246 		}
247 	}
248 	ZSTR_VAL(str_out)[j] = '\0';
249 	ZSTR_LEN(str_out) = j;
250 
251 	RETVAL_NEW_STR(str_out);
252 }
253 /* }}} */
254 
255 /* {{{ */
PHP_FUNCTION(quoted_printable_encode)256 PHP_FUNCTION(quoted_printable_encode)
257 {
258 	zend_string *str;
259 	zend_string *new_str;
260 
261 	ZEND_PARSE_PARAMETERS_START(1, 1)
262 		Z_PARAM_STR(str)
263 	ZEND_PARSE_PARAMETERS_END();
264 
265 	if (!ZSTR_LEN(str)) {
266 		RETURN_EMPTY_STRING();
267 	}
268 
269 	new_str = php_quot_print_encode((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str));
270 	RETURN_STR(new_str);
271 }
272 /* }}} */
273