LTP GCOV extension - code coverage report
Current view: directory - ext/standard - base64.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 78
Code covered: 0.0 % Executed lines: 0
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2007 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: Jim Winstead <jimw@php.net>                                  |
      16                 :    +----------------------------------------------------------------------+
      17                 :  */
      18                 : /* $Id: base64.c,v 1.43.2.2.2.2 2007/01/01 09:36:08 sebastian Exp $ */
      19                 : 
      20                 : #include <string.h>
      21                 : 
      22                 : #include "php.h"
      23                 : #include "base64.h"
      24                 : 
      25                 : /* {{{ */
      26                 : static const char base64_table[] =
      27                 :         { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
      28                 :           'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
      29                 :           'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      30                 :           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
      31                 :           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
      32                 :         };
      33                 : 
      34                 : static const char base64_pad = '=';
      35                 : 
      36                 : static const short base64_reverse_table[256] = {
      37                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
      38                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      39                 :         -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
      40                 :         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
      41                 :         -2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
      42                 :         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
      43                 :         -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
      44                 :         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
      45                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      46                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      47                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      48                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      49                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      50                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      51                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
      52                 :         -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
      53                 : };
      54                 : /* }}} */
      55                 : 
      56                 : /* {{{ php_base64_encode */
      57                 : PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length)
      58               0 : {
      59               0 :         const unsigned char *current = str;
      60                 :         unsigned char *p;
      61                 :         unsigned char *result;
      62                 : 
      63               0 :         if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
      64               0 :                 if (ret_length != NULL) {
      65               0 :                         *ret_length = 0;
      66                 :                 }
      67               0 :                 return NULL;
      68                 :         }
      69                 : 
      70               0 :         result = (unsigned char *)safe_emalloc(((length + 2) / 3) * 4, sizeof(char), 1);
      71               0 :         p = result;
      72                 : 
      73               0 :         while (length > 2) { /* keep going until we have less than 24 bits */
      74               0 :                 *p++ = base64_table[current[0] >> 2];
      75               0 :                 *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
      76               0 :                 *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
      77               0 :                 *p++ = base64_table[current[2] & 0x3f];
      78                 : 
      79               0 :                 current += 3;
      80               0 :                 length -= 3; /* we just handle 3 octets of data */
      81                 :         }
      82                 : 
      83                 :         /* now deal with the tail end of things */
      84               0 :         if (length != 0) {
      85               0 :                 *p++ = base64_table[current[0] >> 2];
      86               0 :                 if (length > 1) {
      87               0 :                         *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
      88               0 :                         *p++ = base64_table[(current[1] & 0x0f) << 2];
      89               0 :                         *p++ = base64_pad;
      90                 :                 } else {
      91               0 :                         *p++ = base64_table[(current[0] & 0x03) << 4];
      92               0 :                         *p++ = base64_pad;
      93               0 :                         *p++ = base64_pad;
      94                 :                 }
      95                 :         }
      96               0 :         if (ret_length != NULL) {
      97               0 :                 *ret_length = (int)(p - result);
      98                 :         }
      99               0 :         *p = '\0';
     100               0 :         return result;
     101                 : }
     102                 : /* }}} */
     103                 : 
     104                 : /* {{{ */
     105                 : /* generate reverse table (do not set index 0 to 64)
     106                 : static unsigned short base64_reverse_table[256];
     107                 : #define rt base64_reverse_table
     108                 : void php_base64_init()
     109                 : {
     110                 :         char *s = emalloc(10240), *sp;
     111                 :         char *chp;
     112                 :         short idx;
     113                 : 
     114                 :         for(ch = 0; ch < 256; ch++) {
     115                 :                 chp = strchr(base64_table, ch);
     116                 :                 if(ch && chp) {
     117                 :                         idx = chp - base64_table;
     118                 :                         if (idx >= 64) idx = -1;
     119                 :                         rt[ch] = idx;
     120                 :                 } else {
     121                 :                         rt[ch] = -1;
     122                 :                 }
     123                 :         }
     124                 :         sp = s;
     125                 :         sprintf(sp, "static const short base64_reverse_table[256] = {\n");
     126                 :         for(ch =0; ch < 256;) {
     127                 :                 sp = s+strlen(s);
     128                 :                 sprintf(sp, "\t% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,\n", rt[ch+0], rt[ch+1], rt[ch+2], rt[ch+3], rt[ch+4], rt[ch+5], rt[ch+6], rt[ch+7], rt[ch+8], rt[ch+9], rt[ch+10], rt[ch+11], rt[ch+12], rt[ch+13], rt[ch+14], rt[ch+15]);
     129                 :                 ch += 16;
     130                 :         }
     131                 :         sprintf(sp, "};");
     132                 :         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Reverse_table:\n%s", s);
     133                 :         efree(s);
     134                 : }
     135                 : */
     136                 : /* }}} */
     137                 : 
     138                 : PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length)
     139               0 : {
     140               0 :         return php_base64_decode_ex(str, length, ret_length, 0);
     141                 : }
     142                 : 
     143                 : /* {{{ php_base64_decode */
     144                 : /* as above, but backwards. :) */
     145                 : PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict)
     146               0 : {
     147               0 :         const unsigned char *current = str;
     148               0 :         int ch, i = 0, j = 0, k;
     149                 :         /* this sucks for threaded environments */
     150                 :         unsigned char *result;
     151                 :         
     152               0 :         result = (unsigned char *)emalloc(length + 1);
     153                 : 
     154                 :         /* run through the whole string, converting as we go */
     155               0 :         while ((ch = *current++) != '\0' && length-- > 0) {
     156               0 :                 if (ch == base64_pad) break;
     157                 : 
     158               0 :                 ch = base64_reverse_table[ch];
     159               0 :                 if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
     160                 :                         continue;
     161               0 :                 } else if (ch == -2) {
     162               0 :                         efree(result);
     163               0 :                         return NULL;
     164                 :                 }
     165                 : 
     166               0 :                 switch(i % 4) {
     167                 :                 case 0:
     168               0 :                         result[j] = ch << 2;
     169               0 :                         break;
     170                 :                 case 1:
     171               0 :                         result[j++] |= ch >> 4;
     172               0 :                         result[j] = (ch & 0x0f) << 4;
     173               0 :                         break;
     174                 :                 case 2:
     175               0 :                         result[j++] |= ch >>2;
     176               0 :                         result[j] = (ch & 0x03) << 6;
     177               0 :                         break;
     178                 :                 case 3:
     179               0 :                         result[j++] |= ch;
     180                 :                         break;
     181                 :                 }
     182               0 :                 i++;
     183                 :         }
     184                 : 
     185               0 :         k = j;
     186                 :         /* mop things up if we ended on a boundary */
     187               0 :         if (ch == base64_pad) {
     188               0 :                 switch(i % 4) {
     189                 :                 case 1:
     190               0 :                         efree(result);
     191               0 :                         return NULL;
     192                 :                 case 2:
     193               0 :                         k++;
     194                 :                 case 3:
     195               0 :                         result[k++] = 0;
     196                 :                 }
     197                 :         }
     198               0 :         if(ret_length) {
     199               0 :                 *ret_length = j;
     200                 :         }
     201               0 :         result[j] = '\0';
     202               0 :         return result;
     203                 : }
     204                 : /* }}} */
     205                 : 
     206                 : /* {{{ proto string base64_encode(string str)
     207                 :    Encodes string using MIME base64 algorithm */
     208                 : PHP_FUNCTION(base64_encode)
     209               0 : {
     210                 :         char *str;
     211                 :         unsigned char *result;
     212                 :         int str_len, ret_length;
     213                 : 
     214               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
     215               0 :                 return;
     216                 :         }
     217               0 :         result = php_base64_encode(str, str_len, &ret_length);
     218               0 :         if (result != NULL) {
     219               0 :                 RETVAL_STRINGL(result, ret_length, 0);
     220                 :         } else {
     221               0 :                 RETURN_FALSE;
     222                 :         }
     223                 : }
     224                 : /* }}} */
     225                 : 
     226                 : 
     227                 : /* {{{ proto string base64_decode(string str[, bool strict])
     228                 :    Decodes string using MIME base64 algorithm */
     229                 : PHP_FUNCTION(base64_decode)
     230               0 : {
     231                 :         char *str;
     232                 :         unsigned char *result;
     233               0 :         zend_bool strict = 0;
     234                 :         int str_len, ret_length;
     235                 : 
     236               0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
     237               0 :                 return;
     238                 :         }
     239               0 :         result = php_base64_decode_ex(str, str_len, &ret_length, strict);
     240               0 :         if (result != NULL) {
     241               0 :                 RETVAL_STRINGL(result, ret_length, 0);
     242                 :         } else {
     243               0 :                 RETURN_FALSE;
     244                 :         }
     245                 : }
     246                 : /* }}} */
     247                 : 
     248                 : 
     249                 : /*
     250                 :  * Local variables:
     251                 :  * tab-width: 4
     252                 :  * c-basic-offset: 4
     253                 :  * End:
     254                 :  * vim600: sw=4 ts=4 fdm=marker
     255                 :  * vim<600: sw=4 ts=4
     256                 :  */

Generated by: LTP GCOV extension version 1.5