xref: /php-src/ext/standard/quot_print.c (revision aff36587)
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 	}
38 	else if (c >= 'A' && c <= 'F') {
39 		return c - 'A' + 10;
40 	}
41 	else if (c >= 'a' && c <= 'f') {
42 		return c - 'a' + 10;
43 	}
44 	else {
45 		return -1;
46 	}
47 }
48 /* }}} */
49 
php_quot_print_decode(const unsigned char * str,size_t length,int replace_us_by_ws)50 PHPAPI zend_string *php_quot_print_decode(const unsigned char *str, size_t length, int replace_us_by_ws) /* {{{ */
51 {
52 	size_t i;
53 	unsigned const char *p1;
54 	unsigned char *p2;
55 	unsigned int h_nbl, l_nbl;
56 
57 	size_t decoded_len, buf_size;
58 	zend_string *retval;
59 
60 	static unsigned int hexval_tbl[256] = {
61 		64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 16, 64, 64, 16, 64, 64,
62 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
63 		32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64 		 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 64, 64, 64, 64, 64, 64,
65 		64, 10, 11, 12, 13, 14, 15, 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, 10, 11, 12, 13, 14, 15, 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 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
74 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
75 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
76 		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
77 	};
78 
79 	if (replace_us_by_ws) {
80 		replace_us_by_ws = '_';
81 	}
82 
83 	i = length, p1 = str; buf_size = length;
84 
85 	while (i > 1 && *p1 != '\0') {
86 		if (*p1 == '=') {
87 			buf_size -= 2;
88 			p1++;
89 			i--;
90 		}
91 		p1++;
92 		i--;
93 	}
94 
95 	retval = zend_string_alloc(buf_size, 0);
96 	i = length; p1 = str; p2 = (unsigned char*)ZSTR_VAL(retval);
97 	decoded_len = 0;
98 
99 	while (i > 0 && *p1 != '\0') {
100 		if (*p1 == '=') {
101 			i--, p1++;
102 			if (i == 0 || *p1 == '\0') {
103 				break;
104 			}
105 			h_nbl = hexval_tbl[*p1];
106 			if (h_nbl < 16) {
107 				/* next char should be a hexadecimal digit */
108 				if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
109 					efree(retval);
110 					return NULL;
111 				}
112 				*(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
113 				i--, p1++;
114 			} else if (h_nbl < 64) {
115 				/* soft line break */
116 				while (h_nbl == 32) {
117 					if (--i == 0 || (h_nbl = hexval_tbl[*(++p1)]) == 64) {
118 						efree(retval);
119 						return NULL;
120 					}
121 				}
122 				if (p1[0] == '\r' && i >= 2 && p1[1] == '\n') {
123 					i--, p1++;
124 				}
125 				i--, p1++;
126 			} else {
127 				efree(retval);
128 				return NULL;
129 			}
130 		} else {
131 			*(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
132 			i--, p1++, decoded_len++;
133 		}
134 	}
135 
136 	*p2 = '\0';
137 	ZSTR_LEN(retval) = decoded_len;
138 	return retval;
139 }
140 /* }}} */
141 
142 #define PHP_QPRINT_MAXL 75
143 
php_quot_print_encode(const unsigned char * str,size_t length)144 PHPAPI zend_string *php_quot_print_encode(const unsigned char *str, size_t length) /* {{{ */
145 {
146 	zend_ulong lp = 0;
147 	unsigned char c, *d;
148 	char *hex = "0123456789ABCDEF";
149 	zend_string *ret;
150 
151 	ret = zend_string_safe_alloc(3, (length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1)), 0, 0);
152 	d = (unsigned char*)ZSTR_VAL(ret);
153 
154 	while (length--) {
155 		if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
156 			*d++ = '\015';
157 			*d++ = *str++;
158 			length--;
159 			lp = 0;
160 		} else {
161 			if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
162 				if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0x7f))
163 				    || ((c > 0x7f) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL))
164 				    || ((c > 0xdf) && (c <= 0xef) && ((lp + 6) > PHP_QPRINT_MAXL))
165 				    || ((c > 0xef) && (c <= 0xf4) && ((lp + 9) > PHP_QPRINT_MAXL))) {
166 					*d++ = '=';
167 					*d++ = '\015';
168 					*d++ = '\012';
169 					lp = 3;
170 				}
171 				*d++ = '=';
172 				*d++ = hex[c >> 4];
173 				*d++ = hex[c & 0xf];
174 			} else {
175 				if ((++lp) > PHP_QPRINT_MAXL) {
176 					*d++ = '=';
177 					*d++ = '\015';
178 					*d++ = '\012';
179 					lp = 1;
180 				}
181 				*d++ = c;
182 			}
183 		}
184 	}
185 	*d = '\0';
186 	ret = zend_string_truncate(ret, d - (unsigned char*)ZSTR_VAL(ret), 0);
187 	return ret;
188 }
189 /* }}} */
190 
191 /*
192 *
193 * Decoding  Quoted-printable string.
194 *
195 */
196 /* {{{ Convert a quoted-printable string to an 8 bit string */
PHP_FUNCTION(quoted_printable_decode)197 PHP_FUNCTION(quoted_printable_decode)
198 {
199 	zend_string *arg1;
200 	char *str_in;
201 	zend_string *str_out;
202 	size_t i = 0, j = 0, k;
203 
204 	ZEND_PARSE_PARAMETERS_START(1, 1)
205 		Z_PARAM_STR(arg1)
206 	ZEND_PARSE_PARAMETERS_END();
207 
208 	if (ZSTR_LEN(arg1) == 0) {
209 		/* shortcut */
210 		RETURN_EMPTY_STRING();
211 	}
212 
213 	str_in = ZSTR_VAL(arg1);
214 	str_out = zend_string_alloc(ZSTR_LEN(arg1), 0);
215 	while (str_in[i]) {
216 		switch (str_in[i]) {
217 		case '=':
218 			if (str_in[i + 1] && str_in[i + 2] &&
219 				isxdigit((int) str_in[i + 1]) &&
220 				isxdigit((int) str_in[i + 2]))
221 			{
222 				ZSTR_VAL(str_out)[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
223 						+ php_hex2int((int) str_in[i + 2]);
224 				i += 3;
225 			} else  /* check for soft line break according to RFC 2045*/ {
226 				k = 1;
227 				while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
228 					/* Possibly, skip spaces/tabs at the end of line */
229 					k++;
230 				}
231 				if (!str_in[i + k]) {
232 					/* End of line reached */
233 					i += k;
234 				}
235 				else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
236 					/* CRLF */
237 					i += k + 2;
238 				}
239 				else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
240 					/* CR or LF */
241 					i += k + 1;
242 				}
243 				else {
244 					ZSTR_VAL(str_out)[j++] = str_in[i++];
245 				}
246 			}
247 			break;
248 		default:
249 			ZSTR_VAL(str_out)[j++] = str_in[i++];
250 		}
251 	}
252 	ZSTR_VAL(str_out)[j] = '\0';
253 	ZSTR_LEN(str_out) = j;
254 
255 	RETVAL_NEW_STR(str_out);
256 }
257 /* }}} */
258 
259 /* {{{ */
PHP_FUNCTION(quoted_printable_encode)260 PHP_FUNCTION(quoted_printable_encode)
261 {
262 	zend_string *str;
263 	zend_string *new_str;
264 
265 	ZEND_PARSE_PARAMETERS_START(1, 1)
266 		Z_PARAM_STR(str)
267 	ZEND_PARSE_PARAMETERS_END();
268 
269 	if (!ZSTR_LEN(str)) {
270 		RETURN_EMPTY_STRING();
271 	}
272 
273 	new_str = php_quot_print_encode((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str));
274 	RETURN_STR(new_str);
275 }
276 /* }}} */
277