1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 : /* $Id: quot_print.c 276986 2009-03-10 23:40:06Z helly $ */
20 :
21 : #include <stdlib.h>
22 :
23 : #ifdef HAVE_UNISTD_H
24 : #include <unistd.h>
25 : #endif
26 : #include <string.h>
27 : #include <errno.h>
28 :
29 : #include "php.h"
30 : #include "quot_print.h"
31 :
32 : #include <stdio.h>
33 :
34 : /*
35 : * Converting HEX char to INT value
36 : */
37 : static char php_hex2int(int c) /* {{{ */
38 3256 : {
39 3256 : if (isdigit(c)) {
40 1722 : return c - '0';
41 : }
42 1534 : else if (c >= 'A' && c <= 'F') {
43 1531 : return c - 'A' + 10;
44 : }
45 3 : else if (c >= 'a' && c <= 'f') {
46 3 : return c - 'a' + 10;
47 : }
48 : else {
49 0 : return -1;
50 : }
51 : }
52 : /* }}} */
53 :
54 : PHPAPI unsigned char *php_quot_print_decode(const unsigned char *str, size_t length, size_t *ret_length, int replace_us_by_ws) /* {{{ */
55 35 : {
56 : register unsigned int i;
57 : register unsigned const char *p1;
58 : register unsigned char *p2;
59 : register unsigned int h_nbl, l_nbl;
60 :
61 : size_t decoded_len, buf_size;
62 : unsigned char *retval;
63 :
64 : static unsigned int hexval_tbl[256] = {
65 : 64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 16, 64, 64, 16, 64, 64,
66 : 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
67 : 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
68 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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 : 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
80 : 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
81 : };
82 :
83 35 : if (replace_us_by_ws) {
84 35 : replace_us_by_ws = '_';
85 : }
86 :
87 35 : i = length, p1 = str; buf_size = length;
88 :
89 384 : while (i > 1 && *p1 != '\0') {
90 314 : if (*p1 == '=') {
91 70 : buf_size -= 2;
92 70 : p1++;
93 70 : i--;
94 : }
95 314 : p1++;
96 314 : i--;
97 : }
98 :
99 35 : retval = emalloc(buf_size + 1);
100 35 : i = length; p1 = str; p2 = retval;
101 35 : decoded_len = 0;
102 :
103 349 : while (i > 0 && *p1 != '\0') {
104 279 : if (*p1 == '=') {
105 70 : i--, p1++;
106 70 : if (i == 0 || *p1 == '\0') {
107 : break;
108 : }
109 70 : h_nbl = hexval_tbl[*p1];
110 70 : if (h_nbl < 16) {
111 : /* next char should be a hexadecimal digit */
112 70 : if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
113 0 : efree(retval);
114 0 : return NULL;
115 : }
116 70 : *(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
117 70 : i--, p1++;
118 0 : } else if (h_nbl < 64) {
119 : /* soft line break */
120 0 : while (h_nbl == 32) {
121 0 : if (--i == 0 || (h_nbl = hexval_tbl[*(++p1)]) == 64) {
122 0 : efree(retval);
123 0 : return NULL;
124 : }
125 : }
126 0 : if (p1[0] == '\r' && i >= 2 && p1[1] == '\n') {
127 0 : i--, p1++;
128 : }
129 0 : i--, p1++;
130 : } else {
131 0 : efree(retval);
132 0 : return NULL;
133 : }
134 : } else {
135 209 : *(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
136 209 : i--, p1++, decoded_len++;
137 : }
138 : }
139 :
140 35 : *p2 = '\0';
141 35 : *ret_length = decoded_len;
142 35 : return retval;
143 : }
144 : /* }}} */
145 :
146 : #define PHP_QPRINT_MAXL 75
147 :
148 : PHPAPI unsigned char *php_quot_print_encode(const unsigned char *str, size_t length, size_t *ret_length) /* {{{ */
149 5 : {
150 5 : unsigned long lp = 0;
151 : unsigned char c, *ret, *d;
152 5 : char *hex = "0123456789ABCDEF";
153 :
154 5 : ret = safe_emalloc(1, 3 * length + 3 * (((3 * length)/PHP_QPRINT_MAXL) + 1), 0);
155 5 : d = ret;
156 :
157 1728 : while (length--) {
158 1718 : if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
159 0 : *d++ = '\015';
160 0 : *d++ = *str++;
161 0 : length--;
162 0 : lp = 0;
163 : } else {
164 3318 : if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
165 1600 : if ((lp += 3) > PHP_QPRINT_MAXL) {
166 65 : *d++ = '=';
167 65 : *d++ = '\015';
168 65 : *d++ = '\012';
169 65 : lp = 3;
170 : }
171 1600 : *d++ = '=';
172 1600 : *d++ = hex[c >> 4];
173 1600 : *d++ = hex[c & 0xf];
174 : } else {
175 118 : if ((++lp) > PHP_QPRINT_MAXL) {
176 0 : *d++ = '=';
177 0 : *d++ = '\015';
178 0 : *d++ = '\012';
179 0 : lp = 1;
180 : }
181 118 : *d++ = c;
182 : }
183 : }
184 : }
185 5 : *d = '\0';
186 5 : *ret_length = d - ret;
187 :
188 5 : ret = erealloc(ret, *ret_length + 1);
189 5 : return ret;
190 : }
191 : /* }}} */
192 :
193 : /*
194 : *
195 : * Decoding Quoted-printable string.
196 : *
197 : */
198 : /* {{{ proto binary quoted_printable_decode(string str) U
199 : Convert a quoted-printable string to an 8 bit string */
200 : PHP_FUNCTION(quoted_printable_decode)
201 31 : {
202 : char *str_in, *str_out;
203 : int str_in_len;
204 31 : int i = 0, j = 0, k;
205 :
206 31 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&", &str_in, &str_in_len, UG(ascii_conv)) == FAILURE) {
207 8 : return;
208 : }
209 :
210 23 : if (str_in_len == 0) {
211 : /* shortcut */
212 8 : RETURN_EMPTY_STRING();
213 : }
214 :
215 15 : str_out = emalloc(str_in_len + 1);
216 1894 : while (str_in[i]) {
217 1864 : switch (str_in[i]) {
218 : case '=':
219 3325 : if (str_in[i + 1] && str_in[i + 2] &&
220 : isxdigit((int) str_in[i + 1]) &&
221 : isxdigit((int) str_in[i + 2]))
222 : {
223 1628 : str_out[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
224 : + php_hex2int((int) str_in[i + 2]);
225 1628 : i += 3;
226 : } else /* check for soft line break according to RFC 2045*/ {
227 69 : k = 1;
228 151 : while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
229 : /* Possibly, skip spaces/tabs at the end of line */
230 13 : k++;
231 : }
232 69 : if (!str_in[i + k]) {
233 : /* End of line reached */
234 0 : i += k;
235 : }
236 134 : else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
237 : /* CRLF */
238 65 : i += k + 2;
239 : }
240 8 : else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
241 : /* CR or LF */
242 4 : i += k + 1;
243 : }
244 : else {
245 0 : str_out[j++] = str_in[i++];
246 : }
247 : }
248 1697 : break;
249 : default:
250 167 : str_out[j++] = str_in[i++];
251 : }
252 : }
253 15 : str_out[j] = '\0';
254 :
255 15 : RETVAL_STRINGL(str_out, j, 0);
256 : }
257 : /* }}} */
258 :
259 : /* {{{ proto string quoted_printable_encode(string str) U */
260 : PHP_FUNCTION(quoted_printable_encode)
261 11 : {
262 : char *str, *new_str;
263 : int str_len;
264 : size_t new_str_len;
265 :
266 11 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) != SUCCESS) {
267 3 : return;
268 : }
269 :
270 8 : if (!str_len) {
271 3 : RETURN_EMPTY_STRING();
272 : }
273 :
274 5 : new_str = (char *)php_quot_print_encode((unsigned char *)str, (size_t)str_len, &new_str_len);
275 5 : RETURN_STRINGL(new_str, new_str_len, 0);
276 : }
277 : /* }}} */
278 :
279 : /*
280 : * Local variables:
281 : * tab-width: 4
282 : * c-basic-offset: 4
283 : * End:
284 : * vim600: sw=4 ts=4 fdm=marker
285 : * vim<600: sw=4 ts=4
286 : */
|