xref: /php-src/ext/standard/uuencode.c (revision 01b3fc03)
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: Ilia Alshanetsky <ilia@php.net>                              |
14    +----------------------------------------------------------------------+
15  */
16 
17 /*
18  * Portions of this code are based on Berkeley's uuencode/uudecode
19  * implementation.
20  *
21  * Copyright (c) 1983, 1993
22  *  The Regents of the University of California.  All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. All advertising materials mentioning features or use of this software
33  *    must display the following acknowledgement:
34  *  This product includes software developed by the University of
35  *  California, Berkeley and its contributors.
36  * 4. Neither the name of the University nor the names of its contributors
37  *    may be used to endorse or promote products derived from this software
38  *    without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 
53 #include <math.h>
54 
55 #include "php.h"
56 #include "php_uuencode.h"
57 
58 #define PHP_UU_ENC(c) ((c) ? ((c) & 077) + ' ' : '`')
59 #define PHP_UU_ENC_C2(c) PHP_UU_ENC(((*(c) << 4) & 060) | ((*((c) + 1) >> 4) & 017))
60 #define PHP_UU_ENC_C3(c) PHP_UU_ENC(((*(c + 1) << 2) & 074) | ((*((c) + 2) >> 6) & 03))
61 
62 #define PHP_UU_DEC(c) (((c) - ' ') & 077)
63 
php_uuencode(const char * src,size_t src_len)64 PHPAPI zend_string *php_uuencode(const char *src, size_t src_len) /* {{{ */
65 {
66 	size_t len = 45;
67 	unsigned char *p;
68 	const unsigned char *s, *e, *ee;
69 	zend_string *dest;
70 
71 	/* encoded length is ~ 38% greater than the original
72        Use 1.5 for easier calculation.
73     */
74 	dest = zend_string_safe_alloc(src_len/2, 3, 46, 0);
75 	p = (unsigned char *) ZSTR_VAL(dest);
76 	s = (unsigned char *) src;
77 	e = s + src_len;
78 
79 	while ((s + 3) < e) {
80 		ee = s + len;
81 		if (ee > e) {
82 			ee = e;
83 			len = ee - s;
84 			if (len % 3) {
85 				ee = s + (int) (floor((double)len / 3) * 3);
86 			}
87 		}
88 		*p++ = PHP_UU_ENC(len);
89 
90 		while (s < ee) {
91 			*p++ = PHP_UU_ENC(*s >> 2);
92 			*p++ = PHP_UU_ENC_C2(s);
93 			*p++ = PHP_UU_ENC_C3(s);
94 			*p++ = PHP_UU_ENC(*(s + 2) & 077);
95 
96 			s += 3;
97 		}
98 
99 		if (len == 45) {
100 			*p++ = '\n';
101 		}
102 	}
103 
104 	if (s < e) {
105 		if (len == 45) {
106 			*p++ = PHP_UU_ENC(e - s);
107 			len = 0;
108 		}
109 
110 		*p++ = PHP_UU_ENC(*s >> 2);
111 		*p++ = PHP_UU_ENC_C2(s);
112 		*p++ = ((e - s) > 1) ? PHP_UU_ENC_C3(s) : PHP_UU_ENC('\0');
113 		*p++ = ((e - s) > 2) ? PHP_UU_ENC(*(s + 2) & 077) : PHP_UU_ENC('\0');
114 	}
115 
116 	if (len < 45) {
117 		*p++ = '\n';
118 	}
119 
120 	*p++ = PHP_UU_ENC('\0');
121 	*p++ = '\n';
122 	*p = '\0';
123 
124 	dest = zend_string_truncate(dest, (char *) p - ZSTR_VAL(dest), 0);
125 	return dest;
126 }
127 /* }}} */
128 
php_uudecode(const char * src,size_t src_len)129 PHPAPI zend_string *php_uudecode(const char *src, size_t src_len) /* {{{ */
130 {
131 	size_t len, total_len=0;
132 	char *p;
133 	const char *s, *e, *ee;
134 	zend_string *dest;
135 
136 	if (src_len == 0) {
137 		return NULL;
138 	}
139 
140 	dest = zend_string_alloc((size_t) ceil(src_len * 0.75), 0);
141 	p = ZSTR_VAL(dest);
142 	s = src;
143 	e = src + src_len;
144 
145 	while (s < e) {
146 		if ((len = PHP_UU_DEC(*s++)) == 0) {
147 			break;
148 		}
149 		/* sanity check */
150 		if (len > src_len) {
151 			goto err;
152 		}
153 
154 		total_len += len;
155 
156 		ee = s + (len == 45 ? 60 : (int) floor(len * 1.33));
157 		/* sanity check */
158 		if (ee > e) {
159 			goto err;
160 		}
161 
162 		while (s < ee) {
163 			if(s+4 > e) {
164 				goto err;
165 			}
166 			*p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
167 			*p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
168 			*p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
169 			s += 4;
170 		}
171 
172 		if (len < 45) {
173 			break;
174 		}
175 
176 		/* skip \n */
177 		s++;
178 	}
179 
180 	assert(p >= ZSTR_VAL(dest));
181 	if ((len = total_len) > (size_t)(p - ZSTR_VAL(dest))) {
182 		*p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
183 		if (len > 1) {
184 			*p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
185 			if (len > 2) {
186 				*p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
187 			}
188 		}
189 	}
190 
191 	ZSTR_LEN(dest) = total_len;
192 	ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
193 
194 	return dest;
195 
196 err:
197 	zend_string_efree(dest);
198 
199 	return NULL;
200 }
201 /* }}} */
202 
203 /* {{{ uuencode a string */
PHP_FUNCTION(convert_uuencode)204 PHP_FUNCTION(convert_uuencode)
205 {
206 	zend_string *src;
207 
208 	ZEND_PARSE_PARAMETERS_START(1, 1)
209 		Z_PARAM_STR(src)
210 	ZEND_PARSE_PARAMETERS_END();
211 
212 	RETURN_STR(php_uuencode(ZSTR_VAL(src), ZSTR_LEN(src)));
213 }
214 /* }}} */
215 
216 /* {{{ decode a uuencoded string */
PHP_FUNCTION(convert_uudecode)217 PHP_FUNCTION(convert_uudecode)
218 {
219 	zend_string *src;
220 	zend_string *dest;
221 
222 	ZEND_PARSE_PARAMETERS_START(1, 1)
223 		Z_PARAM_STR(src)
224 	ZEND_PARSE_PARAMETERS_END();
225 
226 	if ((dest = php_uudecode(ZSTR_VAL(src), ZSTR_LEN(src))) == NULL) {
227 		php_error_docref(NULL, E_WARNING, "Argument #1 ($data) is not a valid uuencoded string");
228 		RETURN_FALSE;
229 	}
230 
231 	RETURN_STR(dest);
232 }
233 /* }}} */
234