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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:10 +0000 (7 days ago)

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