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 - quot_print.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 94 118 79.7 %
Date: 2014-11-22 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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$ */
      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        3372 : static char php_hex2int(int c) /* {{{ */
      38             : {
      39        3372 :         if (isdigit(c)) {
      40        1768 :                 return c - '0';
      41             :         }
      42        1604 :         else if (c >= 'A' && c <= 'F') {
      43        1598 :                 return c - 'A' + 10;
      44             :         }
      45           6 :         else if (c >= 'a' && c <= 'f') {
      46           6 :                 return c - 'a' + 10;
      47             :         }
      48             :         else {
      49           0 :                 return -1;
      50             :         }
      51             : }
      52             : /* }}} */
      53             : 
      54          44 : PHPAPI zend_string *php_quot_print_decode(const unsigned char *str, size_t length, int replace_us_by_ws) /* {{{ */
      55             : {
      56             :         register size_t 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             :         zend_string *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, 10, 11, 12, 13, 14, 15, 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          44 :         if (replace_us_by_ws) {
      84          44 :                 replace_us_by_ws = '_';
      85             :         }
      86             : 
      87          44 :         i = length, p1 = str; buf_size = length;
      88             : 
      89         500 :         while (i > 1 && *p1 != '\0') {
      90         412 :                 if (*p1 == '=') {
      91          74 :                         buf_size -= 2;
      92          74 :                         p1++;
      93          74 :                         i--;
      94             :                 }
      95         412 :                 p1++;
      96         412 :                 i--;
      97             :         }
      98             : 
      99          44 :         retval = zend_string_alloc(buf_size, 0);
     100          44 :         i = length; p1 = str; p2 = (unsigned char*)retval->val;
     101          44 :         decoded_len = 0;
     102             : 
     103         470 :         while (i > 0 && *p1 != '\0') {
     104         382 :                 if (*p1 == '=') {
     105          74 :                         i--, p1++;
     106          74 :                         if (i == 0 || *p1 == '\0') {
     107             :                                 break;
     108             :                         }
     109          74 :                         h_nbl = hexval_tbl[*p1];
     110          74 :                         if (h_nbl < 16) {
     111             :                                 /* next char should be a hexadecimal digit */
     112          74 :                                 if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
     113           0 :                                         efree(retval);
     114           0 :                                         return NULL;
     115             :                                 }
     116          74 :                                 *(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
     117          74 :                                 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         308 :                         *(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
     136         308 :                         i--, p1++, decoded_len++;
     137             :                 }
     138             :         }
     139             : 
     140          44 :         *p2 = '\0';
     141          44 :         retval->len = decoded_len;
     142          44 :         return retval;
     143             : }
     144             : /* }}} */
     145             : 
     146             : #define PHP_QPRINT_MAXL 75
     147             :  
     148           8 : PHPAPI zend_string *php_quot_print_encode(const unsigned char *str, size_t length) /* {{{ */
     149             : {
     150           8 :         zend_ulong lp = 0;
     151             :         unsigned char c, *d;
     152           8 :         char *hex = "0123456789ABCDEF";
     153             :         zend_string *ret;
     154             : 
     155          16 :         ret = zend_string_safe_alloc(3, (length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1)), 0, 0);
     156           8 :         d = (unsigned char*)ret->val;
     157             : 
     158      102888 :         while (length--) {
     159      102872 :                 if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
     160           0 :                         *d++ = '\015';
     161           0 :                         *d++ = *str++;
     162           0 :                         length--;
     163           0 :                         lp = 0;
     164             :                 } else {
     165      205626 :                         if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
     166      308055 :                                 if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0x7f)) 
     167      104301 :             || ((c > 0x7f) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL)) 
     168           0 :             || ((c > 0xdf) && (c <= 0xef) && ((lp + 6) > PHP_QPRINT_MAXL)) 
     169      101000 :             || ((c > 0xef) && (c <= 0xf4) && ((lp + 9) > PHP_QPRINT_MAXL))) {
     170        4663 :                                         *d++ = '=';
     171        4663 :                                         *d++ = '\015';
     172        4663 :                                         *d++ = '\012';
     173        4663 :                                         lp = 3;
     174             :                                 }
     175      102754 :                                 *d++ = '=';
     176      102754 :                                 *d++ = hex[c >> 4];
     177      102754 :                                 *d++ = hex[c & 0xf];
     178             :                         } else {
     179         118 :                                 if ((++lp) > PHP_QPRINT_MAXL) {
     180           0 :                                         *d++ = '=';
     181           0 :                                         *d++ = '\015';
     182           0 :                                         *d++ = '\012';
     183           0 :                                         lp = 1;
     184             :                                 }
     185         118 :                                 *d++ = c;
     186             :                         }
     187             :                 }
     188             :         }
     189           8 :         *d = '\0';
     190          16 :         ret = zend_string_realloc(ret, d - (unsigned char*)ret->val, 0);
     191           8 :         return ret;
     192             : }
     193             : /* }}} */
     194             : 
     195             : /*
     196             : *
     197             : * Decoding  Quoted-printable string.
     198             : *
     199             : */
     200             : /* {{{ proto string quoted_printable_decode(string str)
     201             :    Convert a quoted-printable string to an 8 bit string */
     202          35 : PHP_FUNCTION(quoted_printable_decode)
     203             : {
     204             :         zend_string *arg1;
     205             :         char *str_in;
     206             :         zend_string *str_out;
     207          35 :         size_t i = 0, j = 0, k;
     208             : 
     209          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &arg1) == FAILURE) {
     210           8 :                 return;
     211             :         }
     212             :     
     213          27 :         if (arg1->len == 0) {
     214             :                 /* shortcut */
     215           8 :                 RETURN_EMPTY_STRING();
     216             :         }
     217             : 
     218          19 :         str_in = arg1->val;
     219          38 :         str_out = zend_string_alloc(arg1->len, 0);
     220        1990 :         while (str_in[i]) {
     221        1952 :                 switch (str_in[i]) {
     222             :                 case '=':
     223        6902 :                         if (str_in[i + 1] && str_in[i + 2] && 
     224        1765 :                                 isxdigit((int) str_in[i + 1]) && 
     225        1686 :                                 isxdigit((int) str_in[i + 2]))
     226             :                         {
     227        3372 :                                 str_out->val[j++] = (php_hex2int((int) str_in[i + 1]) << 4) 
     228        1686 :                                                 + php_hex2int((int) str_in[i + 2]);
     229        1686 :                                 i += 3;
     230             :                         } else  /* check for soft line break according to RFC 2045*/ {
     231          79 :                                 k = 1;
     232         197 :                                 while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
     233             :                                         /* Possibly, skip spaces/tabs at the end of line */
     234          39 :                                         k++;
     235             :                                 }
     236          79 :                                 if (!str_in[i + k]) {
     237             :                                         /* End of line reached */
     238           0 :                                         i += k;
     239             :                                 }
     240         146 :                                 else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
     241             :                                         /* CRLF */
     242          67 :                                         i += k + 2;
     243             :                                 }
     244          24 :                                 else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
     245             :                                         /* CR or LF */
     246          12 :                                         i += k + 1;
     247             :                                 }
     248             :                                 else {
     249           0 :                                         str_out->val[j++] = str_in[i++];
     250             :                                 }
     251             :                         }
     252        1765 :                         break;
     253             :                 default:
     254         187 :                         str_out->val[j++] = str_in[i++];
     255             :                 }
     256             :         }
     257          19 :         str_out->val[j] = '\0';
     258          19 :         str_out->len = j;
     259             :     
     260          19 :         RETVAL_NEW_STR(str_out);
     261             : }
     262             : /* }}} */
     263             : 
     264             : /* {{{ proto string quoted_printable_encode(string str) */
     265          14 : PHP_FUNCTION(quoted_printable_encode)
     266             : {
     267             :         zend_string *str;
     268             :         zend_string *new_str;
     269             : 
     270          14 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &str) != SUCCESS) {
     271           3 :                 return;
     272             :         }
     273             : 
     274          11 :         if (!str->len) {
     275           3 :                 RETURN_EMPTY_STRING();
     276             :         }
     277             : 
     278           8 :         new_str = php_quot_print_encode((unsigned char *)str->val, (size_t)str->len);
     279           8 :         RETURN_STR(new_str);
     280             : }
     281             : /* }}} */
     282             : 
     283             : /*
     284             :  * Local variables:
     285             :  * tab-width: 4
     286             :  * c-basic-offset: 4
     287             :  * End:
     288             :  * vim600: sw=4 ts=4 fdm=marker
     289             :  * vim<600: sw=4 ts=4
     290             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:25 +0000 (4 days ago)

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