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