PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - uuencode.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 77 83 92.8 %
Date: 2016-09-27 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 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: 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          34 : PHPAPI zend_string *php_uuencode(char *src, size_t src_len) /* {{{ */
      69             : {
      70          34 :         size_t len = 45;
      71             :         char *p, *s, *e, *ee;
      72             :         zend_string *dest;
      73             : 
      74             :         /* encoded length is ~ 38% greater than the original
      75             :        Use 1.5 for easier calculation.
      76             :     */
      77          68 :         dest = zend_string_safe_alloc(src_len/2, 3, 46, 0);
      78          34 :         p = ZSTR_VAL(dest);
      79          34 :         s = src;
      80          34 :         e = src + src_len;
      81             : 
      82          94 :         while ((s + 3) < e) {
      83          26 :                 ee = s + len;
      84          26 :                 if (ee > e) {
      85          20 :                         ee = e;
      86          20 :                         len = ee - s;
      87          20 :                         if (len % 3) {
      88          10 :                                 ee = s + (int) (floor((double)len / 3) * 3);
      89             :                         }
      90             :                 }
      91          26 :                 *p++ = PHP_UU_ENC(len);
      92             : 
      93         215 :                 while (s < ee) {
      94         163 :                         *p++ = PHP_UU_ENC(*s >> 2);
      95         163 :                         *p++ = PHP_UU_ENC_C2(s);
      96         163 :                         *p++ = PHP_UU_ENC_C3(s);
      97         163 :                         *p++ = PHP_UU_ENC(*(s + 2) & 077);
      98             : 
      99         163 :                         s += 3;
     100             :                 }
     101             : 
     102          26 :                 if (len == 45) {
     103           6 :                         *p++ = '\n';
     104             :                 }
     105             :         }
     106             : 
     107          34 :         if (s < e) {
     108          24 :                 if (len == 45) {
     109          14 :                         *p++ = PHP_UU_ENC(e - s);
     110          14 :                         len = 0;
     111             :                 }
     112             : 
     113          24 :                 *p++ = PHP_UU_ENC(*s >> 2);
     114          24 :                 *p++ = PHP_UU_ENC_C2(s);
     115          24 :                 *p++ = ((e - s) > 1) ? PHP_UU_ENC_C3(s) : PHP_UU_ENC('\0');
     116          24 :                 *p++ = ((e - s) > 2) ? PHP_UU_ENC(*(s + 2) & 077) : PHP_UU_ENC('\0');
     117             :         }
     118             : 
     119          34 :         if (len < 45) {
     120          34 :                 *p++ = '\n';
     121             :         }
     122             : 
     123          34 :         *p++ = PHP_UU_ENC('\0');
     124          34 :         *p++ = '\n';
     125          34 :         *p = '\0';
     126             : 
     127          68 :         dest = zend_string_truncate(dest, p - ZSTR_VAL(dest), 0);
     128          34 :         return dest;
     129             : }
     130             : /* }}} */
     131             : 
     132          27 : PHPAPI zend_string *php_uudecode(char *src, size_t src_len) /* {{{ */
     133             : {
     134          27 :         size_t len, total_len=0;
     135             :         char *s, *e, *p, *ee;
     136             :         zend_string *dest;
     137             : 
     138          54 :         dest = zend_string_alloc((size_t) ceil(src_len * 0.75), 0);
     139          27 :         p = ZSTR_VAL(dest);
     140          27 :         s = src;
     141          27 :         e = src + src_len;
     142             : 
     143          58 :         while (s < e) {
     144          31 :                 if ((len = PHP_UU_DEC(*s++)) <= 0) {
     145           0 :                         break;
     146             :                 }
     147             :                 /* sanity check */
     148          31 :                 if (len > src_len) {
     149          12 :                         goto err;
     150             :                 }
     151             : 
     152          19 :                 total_len += len;
     153             : 
     154          19 :                 ee = s + (len == 45 ? 60 : (int) floor(len * 1.33));
     155             :                 /* sanity check */
     156          19 :                 if (ee > e) {
     157           1 :                         goto err;
     158             :                 }
     159             : 
     160         143 :                 while (s < ee) {
     161         108 :                         if(s+4 > e) {
     162           1 :                                 goto err;
     163             :                         }
     164         107 :                         *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
     165         107 :                         *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
     166         107 :                         *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
     167         107 :                         s += 4;
     168             :                 }
     169             : 
     170          17 :                 if (len < 45) {
     171          13 :                         break;
     172             :                 }
     173             : 
     174             :                 /* skip \n */
     175           4 :                 s++;
     176             :         }
     177             : 
     178             :         assert(p >= ZSTR_VAL(dest));
     179          13 :         if ((len = total_len) > (size_t)(p - ZSTR_VAL(dest))) {
     180           0 :                 *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
     181           0 :                 if (len > 1) {
     182           0 :                         *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
     183           0 :                         if (len > 2) {
     184           0 :                                 *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
     185             :                         }
     186             :                 }
     187             :         }
     188             : 
     189          13 :         ZSTR_LEN(dest) = total_len;
     190          13 :         ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
     191             : 
     192          13 :         return dest;
     193             : 
     194             : err:
     195             :         zend_string_free(dest);
     196             : 
     197          14 :         return NULL;
     198             : }
     199             : /* }}} */
     200             : 
     201             : /* {{{ proto string convert_uuencode(string data)
     202             :    uuencode a string */
     203          48 : PHP_FUNCTION(convert_uuencode)
     204             : {
     205             :         zend_string *src;
     206             : 
     207          48 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &src) == FAILURE || ZSTR_LEN(src) < 1) {
     208          14 :                 RETURN_FALSE;
     209             :         }
     210             : 
     211          34 :         RETURN_STR(php_uuencode(ZSTR_VAL(src), ZSTR_LEN(src)));
     212             : }
     213             : /* }}} */
     214             : 
     215             : /* {{{ proto string convert_uudecode(string data)
     216             :    decode a uuencoded string */
     217          41 : PHP_FUNCTION(convert_uudecode)
     218             : {
     219             :         zend_string *src;
     220             :         zend_string *dest;
     221             : 
     222          41 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &src) == FAILURE || ZSTR_LEN(src) < 1) {
     223          14 :                 RETURN_FALSE;
     224             :         }
     225             : 
     226          27 :         if ((dest = php_uudecode(ZSTR_VAL(src), ZSTR_LEN(src))) == NULL) {
     227          14 :                 php_error_docref(NULL, E_WARNING, "The given parameter is not a valid uuencoded string");
     228          14 :                 RETURN_FALSE;
     229             :         }
     230             : 
     231          13 :         RETURN_STR(dest);
     232             : }
     233             : /* }}} */
     234             : 
     235             : /*
     236             :  * Local variables:
     237             :  * tab-width: 4
     238             :  * c-basic-offset: 4
     239             :  * End:
     240             :  * vim600: noet sw=4 ts=4 fdm=marker
     241             :  * vim<600: noet sw=4 ts=4
     242             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:08 +0000 (2 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.