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

LTP GCOV extension - code coverage report
Current view: directory - bz2 - bz2_filter.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 194
Code covered: 62.4 % Executed lines: 121
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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                 :    | Authors: Sara Golemon (pollita@php.net)                              |
      16                 :    +----------------------------------------------------------------------+
      17                 : */
      18                 : 
      19                 : /* $Id: bz2_filter.c 275383 2009-02-09 03:44:59Z cellog $ */
      20                 : 
      21                 : #ifdef HAVE_CONFIG_H
      22                 : #include "config.h"
      23                 : #endif
      24                 : 
      25                 : #include "php.h"
      26                 : #include "php_bz2.h"
      27                 : 
      28                 : /* {{{ data structure */
      29                 : 
      30                 : typedef struct _php_bz2_filter_data {
      31                 :         int persistent;
      32                 :         bz_stream strm;
      33                 :         char *inbuf;
      34                 :         size_t inbuf_len;
      35                 :         char *outbuf;
      36                 :         size_t outbuf_len;
      37                 :         zend_bool finished;
      38                 : } php_bz2_filter_data;
      39                 : 
      40                 : /* }}} */
      41                 : 
      42                 : /* {{{ Memory management wrappers */
      43                 : 
      44                 : static void *php_bz2_alloc(void *opaque, int items, int size)
      45               6 : {
      46               6 :         return (void *)safe_pemalloc(items, size, 0, ((php_bz2_filter_data*)opaque)->persistent);
      47                 : }
      48                 : 
      49                 : static void php_bz2_free(void *opaque, void *address)
      50               6 : {
      51               6 :         pefree((void *)address, ((php_bz2_filter_data*)opaque)->persistent);
      52               6 : }
      53                 : /* }}} */
      54                 : 
      55                 : /* {{{ bzip2.decompress filter implementation */
      56                 : 
      57                 : static php_stream_filter_status_t php_bz2_decompress_filter(
      58                 :         php_stream *stream,
      59                 :         php_stream_filter *thisfilter,
      60                 :         php_stream_bucket_brigade *buckets_in,
      61                 :         php_stream_bucket_brigade *buckets_out,
      62                 :         size_t *bytes_consumed,
      63                 :         int flags
      64                 :         TSRMLS_DC)
      65               2 : {
      66                 :         php_bz2_filter_data *data;
      67                 :         php_stream_bucket *bucket;
      68               2 :         size_t consumed = 0;
      69                 :         int status;
      70               2 :         php_stream_filter_status_t exit_status = PSFS_FEED_ME;
      71                 :         bz_stream *streamp;
      72                 : 
      73               2 :         if (!thisfilter || !thisfilter->abstract) {
      74                 :                 /* Should never happen */
      75               0 :                 return PSFS_ERR_FATAL;
      76                 :         }
      77                 : 
      78               2 :         data = (php_bz2_filter_data *)(thisfilter->abstract);
      79               2 :         streamp = &(data->strm);
      80                 : 
      81               5 :         while (buckets_in->head) {
      82               1 :                 size_t bin = 0, desired;
      83                 : 
      84               1 :                 bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
      85               3 :                 while (bin < bucket->buflen) {
      86               1 :                         if (data->finished) {
      87               0 :                                 consumed += bucket->buflen;
      88               0 :                                 break;
      89                 :                         }
      90                 : 
      91               1 :                         desired = bucket->buflen - bin;
      92               1 :                         if (desired > data->inbuf_len) {
      93               0 :                                 desired = data->inbuf_len;
      94                 :                         }
      95               1 :                         memcpy(data->strm.next_in, bucket->buf + bin, desired);
      96               1 :                         data->strm.avail_in = desired;
      97                 : 
      98               1 :                         status = BZ2_bzDecompress(&(data->strm));
      99                 : 
     100               1 :                         if (status == BZ_STREAM_END) {
     101               1 :                                 BZ2_bzDecompressEnd(&(data->strm));
     102               1 :                                 data->finished = '\1';
     103               0 :                         } else if (status != BZ_OK) {
     104                 :                                 /* Something bad happened */
     105               0 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     106               0 :                                 return PSFS_ERR_FATAL;
     107                 :                         }
     108               1 :                         desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
     109               1 :                         data->strm.next_in = data->inbuf;
     110               1 :                         data->strm.avail_in = 0;
     111               1 :                         consumed += desired;
     112               1 :                         bin += desired;
     113                 : 
     114               1 :                         if (data->strm.avail_out < data->outbuf_len) {
     115                 :                                 php_stream_bucket *out_bucket;
     116               1 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     117               1 :                                 out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     118               1 :                                 php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
     119               1 :                                 data->strm.avail_out = data->outbuf_len;
     120               1 :                                 data->strm.next_out = data->outbuf;
     121               1 :                                 exit_status = PSFS_PASS_ON;
     122               0 :                         } else if (status == BZ_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
     123                 :                                 /* no more data to decompress, and nothing was spat out */
     124               0 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     125               0 :                                 return PSFS_PASS_ON;
     126                 :                         }
     127                 :                 }
     128                 : 
     129               1 :                 php_stream_bucket_delref(bucket TSRMLS_CC);
     130                 :         }
     131                 : 
     132               2 :         if (!data->finished && (flags & PSFS_FLAG_FLUSH_CLOSE)) {
     133                 :                 /* Spit it out! */
     134               0 :                 status = BZ_OK;
     135               0 :                 while (status == BZ_OK) {
     136               0 :                         status = BZ2_bzDecompress(&(data->strm));
     137               0 :                         if (data->strm.avail_out < data->outbuf_len) {
     138               0 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     139                 : 
     140               0 :                                 bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     141               0 :                                 php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
     142               0 :                                 data->strm.avail_out = data->outbuf_len;
     143               0 :                                 data->strm.next_out = data->outbuf;
     144               0 :                                 exit_status = PSFS_PASS_ON;
     145               0 :                         } else if (status == BZ_OK) {
     146               0 :                                 break;
     147                 :                         }
     148                 :                 }
     149                 :         }
     150                 : 
     151               2 :         if (bytes_consumed) {
     152               0 :                 *bytes_consumed = consumed;
     153                 :         }
     154                 : 
     155               2 :         return exit_status;
     156                 : }
     157                 : 
     158                 : static void php_bz2_decompress_dtor(php_stream_filter *thisfilter TSRMLS_DC)
     159               1 : {
     160               1 :         if (thisfilter && thisfilter->abstract) {
     161               1 :                 php_bz2_filter_data *data = thisfilter->abstract;
     162               1 :                 if (!data->finished) {
     163               0 :                         BZ2_bzDecompressEnd(&(data->strm));
     164                 :                 }
     165               1 :                 pefree(data->inbuf, data->persistent);
     166               1 :                 pefree(data->outbuf, data->persistent);
     167               1 :                 pefree(data, data->persistent);
     168                 :         }
     169               1 : }
     170                 : 
     171                 : static php_stream_filter_ops php_bz2_decompress_ops = {
     172                 :         php_bz2_decompress_filter,
     173                 :         php_bz2_decompress_dtor,
     174                 :         "bzip2.decompress"
     175                 : };
     176                 : /* }}} */
     177                 : 
     178                 : /* {{{ bzip2.compress filter implementation */
     179                 : 
     180                 : static php_stream_filter_status_t php_bz2_compress_filter(
     181                 :         php_stream *stream,
     182                 :         php_stream_filter *thisfilter,
     183                 :         php_stream_bucket_brigade *buckets_in,
     184                 :         php_stream_bucket_brigade *buckets_out,
     185                 :         size_t *bytes_consumed,
     186                 :         int flags
     187                 :         TSRMLS_DC)
     188               2 : {
     189                 :         php_bz2_filter_data *data;
     190                 :         php_stream_bucket *bucket;
     191               2 :         size_t consumed = 0;
     192                 :         int status;
     193               2 :         php_stream_filter_status_t exit_status = PSFS_FEED_ME;
     194                 :         bz_stream *streamp;
     195                 : 
     196               2 :         if (!thisfilter || !thisfilter->abstract) {
     197                 :                 /* Should never happen */
     198               0 :                 return PSFS_ERR_FATAL;
     199                 :         }
     200                 : 
     201               2 :         data = (php_bz2_filter_data *)(thisfilter->abstract);
     202               2 :         streamp = &(data->strm);
     203                 : 
     204               5 :         while (buckets_in->head) {
     205               1 :                 size_t bin = 0, desired;
     206                 : 
     207               1 :                 bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
     208                 : 
     209               3 :                 while (bin < bucket->buflen) {
     210               1 :                         desired = bucket->buflen - bin;
     211               1 :                         if (desired > data->inbuf_len) {
     212               0 :                                 desired = data->inbuf_len;
     213                 :                         }
     214               1 :                         memcpy(data->strm.next_in, bucket->buf + bin, desired);
     215               1 :                         data->strm.avail_in = desired;
     216                 : 
     217               1 :                         status = BZ2_bzCompress(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? BZ_FINISH : (flags & PSFS_FLAG_FLUSH_INC ? BZ_FLUSH : BZ_RUN));
     218               1 :                         if (status != BZ_RUN_OK && status != BZ_FLUSH_OK && status != BZ_FINISH_OK) {
     219                 :                                 /* Something bad happened */
     220               0 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     221               0 :                                 return PSFS_ERR_FATAL;
     222                 :                         }
     223               1 :                         desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
     224               1 :                         data->strm.next_in = data->inbuf;
     225               1 :                         data->strm.avail_in = 0;
     226               1 :                         consumed += desired;
     227               1 :                         bin += desired;
     228                 : 
     229               1 :                         if (data->strm.avail_out < data->outbuf_len) {
     230                 :                                 php_stream_bucket *out_bucket;
     231               0 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     232                 : 
     233               0 :                                 out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     234               0 :                                 php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
     235               0 :                                 data->strm.avail_out = data->outbuf_len;
     236               0 :                                 data->strm.next_out = data->outbuf;
     237               0 :                                 exit_status = PSFS_PASS_ON;
     238                 :                         }
     239                 :                 }
     240               1 :                 php_stream_bucket_delref(bucket TSRMLS_CC);
     241                 :         }
     242                 : 
     243               2 :         if (flags & PSFS_FLAG_FLUSH_CLOSE) {
     244                 :                 /* Spit it out! */
     245               1 :                 status = BZ_FINISH_OK;
     246               3 :                 while (status == BZ_FINISH_OK) {
     247               1 :                         status = BZ2_bzCompress(&(data->strm), BZ_FINISH);
     248               1 :                         if (data->strm.avail_out < data->outbuf_len) {
     249               1 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     250                 : 
     251               1 :                                 bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     252               1 :                                 php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
     253               1 :                                 data->strm.avail_out = data->outbuf_len;
     254               1 :                                 data->strm.next_out = data->outbuf;
     255               1 :                                 exit_status = PSFS_PASS_ON;
     256                 :                         }
     257                 :                 }
     258                 :         }
     259                 : 
     260               2 :         if (bytes_consumed) {
     261               2 :                 *bytes_consumed = consumed;
     262                 :         }
     263               2 :         return exit_status;
     264                 : }
     265                 : 
     266                 : static void php_bz2_compress_dtor(php_stream_filter *thisfilter TSRMLS_DC)
     267               1 : {
     268               1 :         if (thisfilter && thisfilter->abstract) {
     269               1 :                 php_bz2_filter_data *data = thisfilter->abstract;
     270               1 :                 BZ2_bzCompressEnd(&(data->strm));
     271               1 :                 pefree(data->inbuf, data->persistent);
     272               1 :                 pefree(data->outbuf, data->persistent);
     273               1 :                 pefree(data, data->persistent);
     274                 :         }
     275               1 : }
     276                 : 
     277                 : static php_stream_filter_ops php_bz2_compress_ops = {
     278                 :         php_bz2_compress_filter,
     279                 :         php_bz2_compress_dtor,
     280                 :         "bzip2.compress"
     281                 : };
     282                 : 
     283                 : /* }}} */
     284                 : 
     285                 : /* {{{ bzip2.* common factory */
     286                 : 
     287                 : static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
     288               2 : {
     289               2 :         php_stream_filter_ops *fops = NULL;
     290                 :         php_bz2_filter_data *data;
     291                 :         int status;
     292                 : 
     293                 :         /* Create this filter */
     294               2 :         data = pecalloc(1, sizeof(php_bz2_filter_data), persistent);
     295               2 :         if (!data) {
     296               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", sizeof(php_bz2_filter_data));
     297               0 :                 return NULL;
     298                 :         }
     299                 : 
     300                 :         /* Circular reference */
     301               2 :         data->strm.opaque = (void *) data;
     302                 : 
     303               2 :         data->strm.bzalloc = php_bz2_alloc;
     304               2 :         data->strm.bzfree = php_bz2_free;
     305               2 :         data->persistent = persistent;
     306               2 :         data->strm.avail_out = data->outbuf_len = data->inbuf_len = 2048;
     307               2 :         data->strm.next_in = data->inbuf = (char *) pemalloc(data->inbuf_len, persistent);
     308               2 :         if (!data->inbuf) {
     309               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", data->inbuf_len);
     310               0 :                 pefree(data, persistent);
     311               0 :                 return NULL;
     312                 :         }
     313               2 :         data->strm.avail_in = 0;
     314               2 :         data->strm.next_out = data->outbuf = (char *) pemalloc(data->outbuf_len, persistent);
     315               2 :         if (!data->outbuf) {
     316               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zu bytes.", data->outbuf_len);
     317               0 :                 pefree(data->inbuf, persistent);
     318               0 :                 pefree(data, persistent);
     319               0 :                 return NULL;
     320                 :         }
     321                 : 
     322               2 :         if (strcasecmp(filtername, "bzip2.decompress") == 0) {
     323               1 :                 int smallFootprint = 0;
     324                 : 
     325               1 :                 if (filterparams) {
     326               0 :                         zval **tmpzval = NULL;
     327                 : 
     328               0 :                         if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
     329               0 :                                 zend_hash_find(HASH_OF(filterparams), "small", sizeof("small"), (void **) &tmpzval);
     330                 :                         } else {
     331               0 :                                 tmpzval = &filterparams;
     332                 :                         }
     333                 : 
     334               0 :                         if (tmpzval) {
     335                 :                                 zval tmp, *tmp2;
     336                 : 
     337               0 :                                 tmp = **tmpzval;
     338               0 :                                 zval_copy_ctor(&tmp);
     339               0 :                                 tmp2 = &tmp;
     340               0 :                                 convert_to_boolean_ex(&tmp2);
     341               0 :                                 smallFootprint = Z_LVAL(tmp);
     342                 :                         }
     343                 :                 }
     344                 : 
     345               1 :                 status = BZ2_bzDecompressInit(&(data->strm), 0, smallFootprint);
     346               1 :                 data->finished = '\0';
     347               1 :                 fops = &php_bz2_decompress_ops;
     348               1 :         } else if (strcasecmp(filtername, "bzip2.compress") == 0) {
     349               1 :                 int blockSize100k = PHP_BZ2_FILTER_DEFAULT_BLOCKSIZE;
     350               1 :                 int workFactor = PHP_BZ2_FILTER_DEFAULT_WORKFACTOR;
     351                 : 
     352               1 :                 if (filterparams) {
     353                 :                         zval **tmpzval;
     354                 : 
     355               0 :                         if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
     356               0 :                                 if (zend_hash_find(HASH_OF(filterparams), "blocks", sizeof("blocks"), (void**) &tmpzval) == SUCCESS) {
     357                 :                                         /* How much memory to allocate (1 - 9) x 100kb */
     358                 :                                         zval tmp;
     359                 :         
     360               0 :                                         tmp = **tmpzval;
     361               0 :                                         zval_copy_ctor(&tmp);
     362               0 :                                         convert_to_long(&tmp);
     363               0 :                                         if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > 9) {
     364               0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter given for number of blocks to allocate. (%ld)", Z_LVAL_PP(tmpzval));
     365                 :                                         } else {
     366               0 :                                                 blockSize100k = Z_LVAL(tmp);
     367                 :                                         }
     368                 :                                 }
     369                 : 
     370               0 :                                 if (zend_hash_find(HASH_OF(filterparams), "work", sizeof("work"), (void**) &tmpzval) == SUCCESS) {
     371                 :                                         /* Work Factor (0 - 250) */
     372                 :                                         zval tmp;
     373                 :         
     374               0 :                                         tmp = **tmpzval;
     375               0 :                                         zval_copy_ctor(&tmp);
     376               0 :                                         convert_to_long(&tmp);
     377                 : 
     378               0 :                                         if (Z_LVAL(tmp) < 0 || Z_LVAL(tmp) > 250) {
     379               0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter given for work factor. (%ld)", Z_LVAL(tmp));
     380                 :                                         } else {
     381               0 :                                                 workFactor = Z_LVAL(tmp);
     382                 :                                         }
     383                 :                                 }
     384                 :                         }
     385                 :                 }
     386                 : 
     387               1 :                 status = BZ2_bzCompressInit(&(data->strm), blockSize100k, 0, workFactor);
     388               1 :                 fops = &php_bz2_compress_ops;
     389                 :         } else {
     390               0 :                 status = BZ_DATA_ERROR;
     391                 :         }
     392                 : 
     393               2 :         if (status != BZ_OK) {
     394                 :                 /* Unspecified (probably strm) error, let stream-filter error do its own whining */
     395               0 :                 pefree(data->strm.next_in, persistent);
     396               0 :                 pefree(data->strm.next_out, persistent);
     397               0 :                 pefree(data, persistent);
     398               0 :                 return NULL;
     399                 :         }
     400                 : 
     401               2 :         return php_stream_filter_alloc(fops, data, persistent);
     402                 : }
     403                 : 
     404                 : php_stream_filter_factory php_bz2_filter_factory = {
     405                 :         php_bz2_filter_create
     406                 : };
     407                 : /* }}} */
     408                 : 
     409                 : /*
     410                 :  * Local variables:
     411                 :  * tab-width: 4
     412                 :  * c-basic-offset: 4
     413                 :  * End:
     414                 :  * vim600: sw=4 ts=4 fdm=marker
     415                 :  * vim<600: sw=4 ts=4
     416                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:05 +0000 (5 days ago)

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