php-src/ext/standard/uuencode.c

243 lines
6.7 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
+----------------------------------------------------------------------+
2018-01-02 04:57:58 +00:00
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Ilia Alshanetsky <ilia@php.net> |
+----------------------------------------------------------------------+
*/
/*
* Portions of this code are based on Berkeley's uuencode/uudecode
* implementation.
*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <math.h>
#include "php.h"
#include "php_uuencode.h"
#define PHP_UU_ENC(c) ((c) ? ((c) & 077) + ' ' : '`')
#define PHP_UU_ENC_C2(c) PHP_UU_ENC(((*(c) << 4) & 060) | ((*((c) + 1) >> 4) & 017))
#define PHP_UU_ENC_C3(c) PHP_UU_ENC(((*(c + 1) << 2) & 074) | ((*((c) + 2) >> 6) & 03))
#define PHP_UU_DEC(c) (((c) - ' ') & 077)
2014-08-25 18:22:49 +00:00
PHPAPI zend_string *php_uuencode(char *src, size_t src_len) /* {{{ */
{
2014-08-25 18:22:49 +00:00
size_t len = 45;
char *p, *s, *e, *ee;
zend_string *dest;
Merge branch 'PHP-5.6' into PHP-7.0 * PHP-5.6: (24 commits) Update NEWS BLock test with memory leak fix tests Fix TSRM build Fix bug #72850 - integer overflow in uuencode Fixed bug #72849 - integer overflow in urlencode Fix bug #72848 - integer overflow in quoted_printable_encode caused heap corruption Fix bug #72838 - Integer overflow lead to heap corruption in sql_regcase Fix bug #72837 - integer overflow in bzdecompress caused heap corruption Fix bug #72836 - integer overflow in base64_decode caused heap corruption Fix for bug #72807 - do not produce strings with negative length Fix for bug #72790 and bug #72799 Fix bug #72730 - imagegammacorrect allows arbitrary write access Fix bug#72697 - select_colors write out-of-bounds Fixed bug #72627: Memory Leakage In exif_process_IFD_in_TIFF Fix bug #72750: wddx_deserialize null dereference Fix bug #72771: ftps:// opendir wrapper is vulnerable to protocol downgrade attack Improve fix for #72663 Fix bug #70436: Use After Free Vulnerability in unserialize() Fix bug #72749: wddx_deserialize allows illegal memory access ... Conflicts: Zend/zend_API.h ext/bz2/bz2.c ext/curl/interface.c ext/ereg/ereg.c ext/exif/exif.c ext/gd/gd.c ext/gd/tests/imagetruecolortopalette_error3.phpt ext/gd/tests/imagetruecolortopalette_error4.phpt ext/session/session.c ext/snmp/snmp.c ext/standard/base64.c ext/standard/ftp_fopen_wrapper.c ext/standard/quot_print.c ext/standard/url.c ext/standard/uuencode.c ext/standard/var.c ext/standard/var_unserializer.c ext/standard/var_unserializer.re ext/wddx/tests/bug72790.phpt ext/wddx/tests/bug72799.phpt ext/wddx/wddx.c sapi/cli/generate_mime_type_map.php
2016-08-17 07:23:51 +00:00
/* encoded length is ~ 38% greater than the original
Use 1.5 for easier calculation.
*/
dest = zend_string_safe_alloc(src_len/2, 3, 46, 0);
p = ZSTR_VAL(dest);
s = src;
e = src + src_len;
while ((s + 3) < e) {
ee = s + len;
if (ee > e) {
ee = e;
len = ee - s;
if (len % 3) {
2014-10-22 07:42:11 +00:00
ee = s + (int) (floor((double)len / 3) * 3);
}
}
*p++ = PHP_UU_ENC(len);
while (s < ee) {
*p++ = PHP_UU_ENC(*s >> 2);
*p++ = PHP_UU_ENC_C2(s);
*p++ = PHP_UU_ENC_C3(s);
*p++ = PHP_UU_ENC(*(s + 2) & 077);
s += 3;
}
if (len == 45) {
*p++ = '\n';
}
}
if (s < e) {
if (len == 45) {
*p++ = PHP_UU_ENC(e - s);
len = 0;
}
*p++ = PHP_UU_ENC(*s >> 2);
*p++ = PHP_UU_ENC_C2(s);
*p++ = ((e - s) > 1) ? PHP_UU_ENC_C3(s) : PHP_UU_ENC('\0');
*p++ = ((e - s) > 2) ? PHP_UU_ENC(*(s + 2) & 077) : PHP_UU_ENC('\0');
}
if (len < 45) {
*p++ = '\n';
}
*p++ = PHP_UU_ENC('\0');
*p++ = '\n';
*p = '\0';
dest = zend_string_truncate(dest, p - ZSTR_VAL(dest), 0);
return dest;
}
2007-03-12 20:55:15 +00:00
/* }}} */
2014-08-25 18:22:49 +00:00
PHPAPI zend_string *php_uudecode(char *src, size_t src_len) /* {{{ */
{
2014-08-25 18:22:49 +00:00
size_t len, total_len=0;
char *s, *e, *p, *ee;
zend_string *dest;
2014-08-25 17:24:55 +00:00
dest = zend_string_alloc((size_t) ceil(src_len * 0.75), 0);
p = ZSTR_VAL(dest);
s = src;
e = src + src_len;
while (s < e) {
if ((len = PHP_UU_DEC(*s++)) == 0) {
break;
}
/* sanity check */
if (len > src_len) {
goto err;
}
total_len += len;
ee = s + (len == 45 ? 60 : (int) floor(len * 1.33));
/* sanity check */
if (ee > e) {
goto err;
}
while (s < ee) {
if(s+4 > e) {
goto err;
2015-01-03 09:22:58 +00:00
}
*p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
*p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
*p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
s += 4;
}
if (len < 45) {
break;
}
/* skip \n */
s++;
}
assert(p >= ZSTR_VAL(dest));
if ((len = total_len) > (size_t)(p - ZSTR_VAL(dest))) {
*p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
if (len > 1) {
*p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
if (len > 2) {
*p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
}
}
}
ZSTR_LEN(dest) = total_len;
ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
return dest;
err:
zend_string_efree(dest);
return NULL;
}
2007-03-12 20:55:15 +00:00
/* }}} */
2015-01-03 09:22:58 +00:00
/* {{{ proto string convert_uuencode(string data)
uuencode a string */
PHP_FUNCTION(convert_uuencode)
{
2014-08-18 14:50:54 +00:00
zend_string *src;
2016-12-31 01:09:35 +00:00
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(src)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
RETURN_STR(php_uuencode(ZSTR_VAL(src), ZSTR_LEN(src)));
}
/* }}} */
2006-06-26 18:48:56 +00:00
/* {{{ proto string convert_uudecode(string data)
decode a uuencoded string */
PHP_FUNCTION(convert_uudecode)
{
2014-08-18 14:50:54 +00:00
zend_string *src;
zend_string *dest;
2016-12-31 01:09:35 +00:00
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(src)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
if ((dest = php_uudecode(ZSTR_VAL(src), ZSTR_LEN(src))) == NULL) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "The given parameter is not a valid uuencoded string");
RETURN_FALSE;
}
RETURN_STR(dest);
}
/* }}} */
2007-03-12 20:55:15 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/