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