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/zlib - zlib_filter.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 164 200 82.0 %
Date: 2014-11-10 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             : #include "php.h"
      22             : #include "php_zlib.h"
      23             : 
      24             : /* {{{ data structure */
      25             : 
      26             : /* Passed as opaque in malloc callbacks */
      27             : typedef struct _php_zlib_filter_data {
      28             :         z_stream strm;
      29             :         char *inbuf;
      30             :         size_t inbuf_len;
      31             :         char *outbuf;
      32             :         size_t outbuf_len;
      33             :         int persistent;
      34             :         zend_bool finished;
      35             : } php_zlib_filter_data;
      36             : 
      37             : /* }}} */
      38             : 
      39             : /* {{{ Memory management wrappers */
      40             : 
      41         302 : static voidpf php_zlib_alloc(voidpf opaque, uInt items, uInt size)
      42             : {
      43         302 :         return (voidpf)safe_pemalloc(items, size, 0, ((php_zlib_filter_data*)opaque)->persistent);
      44             : }
      45             : 
      46         302 : static void php_zlib_free(voidpf opaque, voidpf address)
      47             : {
      48         302 :         pefree((void*)address, ((php_zlib_filter_data*)opaque)->persistent);
      49         302 : }
      50             : /* }}} */
      51             : 
      52             : /* {{{ zlib.inflate filter implementation */
      53             : 
      54         139 : static php_stream_filter_status_t php_zlib_inflate_filter(
      55             :         php_stream *stream,
      56             :         php_stream_filter *thisfilter,
      57             :         php_stream_bucket_brigade *buckets_in,
      58             :         php_stream_bucket_brigade *buckets_out,
      59             :         size_t *bytes_consumed,
      60             :         int flags
      61             :         TSRMLS_DC)
      62             : {
      63             :         php_zlib_filter_data *data;
      64             :         php_stream_bucket *bucket;
      65         139 :         size_t consumed = 0;
      66             :         int status;
      67         139 :         php_stream_filter_status_t exit_status = PSFS_FEED_ME;
      68             : 
      69         139 :         if (!thisfilter || !Z_PTR(thisfilter->abstract)) {
      70             :                 /* Should never happen */
      71           0 :                 return PSFS_ERR_FATAL;
      72             :         }
      73             : 
      74         139 :         data = (php_zlib_filter_data *)(Z_PTR(thisfilter->abstract));
      75             : 
      76         331 :         while (buckets_in->head) {
      77          59 :                 size_t bin = 0, desired;
      78             : 
      79          59 :                 bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
      80             : 
      81         958 :                 while (bin < (unsigned int) bucket->buflen) {
      82             : 
      83         852 :                         if (data->finished) {
      84           6 :                                 consumed += bucket->buflen;
      85           6 :                                 break;
      86             :                         }
      87             : 
      88         846 :                         desired = bucket->buflen - bin;
      89         846 :                         if (desired > data->inbuf_len) {
      90           0 :                                 desired = data->inbuf_len;
      91             :                         }
      92         846 :                         memcpy(data->strm.next_in, bucket->buf + bin, desired);
      93         846 :                         data->strm.avail_in = desired;
      94             : 
      95         846 :                         status = inflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH);
      96         846 :                         if (status == Z_STREAM_END) {
      97          53 :                                 inflateEnd(&(data->strm));
      98          53 :                                 data->finished = '\1';
      99         793 :                         } else if (status != Z_OK) {
     100             :                                 /* Something bad happened */
     101           5 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     102             :                                 /* reset these because despite the error the filter may be used again */
     103           5 :                                 data->strm.next_in = data->inbuf;
     104           5 :                                 data->strm.avail_in = 0;
     105           5 :                                 return PSFS_ERR_FATAL;
     106             :                         }
     107         841 :                         desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
     108         841 :                         data->strm.next_in = data->inbuf;
     109         841 :                         data->strm.avail_in = 0;
     110         841 :                         bin += desired;
     111             : 
     112         841 :                         if (data->strm.avail_out < data->outbuf_len) {
     113             :                                 php_stream_bucket *out_bucket;
     114         839 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     115         839 :                                 out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     116         839 :                                 php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
     117         839 :                                 data->strm.avail_out = data->outbuf_len;
     118         839 :                                 data->strm.next_out = data->outbuf;
     119         839 :                                 exit_status = PSFS_PASS_ON;
     120           2 :                         } else if (status == Z_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
     121             :                                 /* no more data to decompress, and nothing was spat out */
     122           1 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     123           1 :                                 return PSFS_PASS_ON;
     124             :                         }
     125             : 
     126             :                 }
     127          53 :                 consumed += bucket->buflen;
     128          53 :                 php_stream_bucket_delref(bucket TSRMLS_CC);
     129             :         }
     130             : 
     131         133 :         if (!data->finished && flags & PSFS_FLAG_FLUSH_CLOSE) {
     132             :                 /* Spit it out! */
     133           3 :                 status = Z_OK;
     134           9 :                 while (status == Z_OK) {
     135           3 :                         status = inflate(&(data->strm), Z_FINISH);
     136           3 :                         if (data->strm.avail_out < data->outbuf_len) {
     137           0 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     138             : 
     139           0 :                                 bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     140           0 :                                 php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
     141           0 :                                 data->strm.avail_out = data->outbuf_len;
     142           0 :                                 data->strm.next_out = data->outbuf;
     143           0 :                                 exit_status = PSFS_PASS_ON;
     144             :                         }
     145             :                 }
     146             :         }
     147             : 
     148         133 :         if (bytes_consumed) {
     149          69 :                 *bytes_consumed = consumed;
     150             :         }
     151             : 
     152         133 :         return exit_status;
     153             : }
     154             : 
     155          60 : static void php_zlib_inflate_dtor(php_stream_filter *thisfilter TSRMLS_DC)
     156             : {
     157          60 :         if (thisfilter && Z_PTR(thisfilter->abstract)) {
     158          60 :                 php_zlib_filter_data *data = Z_PTR(thisfilter->abstract);
     159          60 :                 if (!data->finished) {
     160           7 :                         inflateEnd(&(data->strm));
     161             :                 }
     162          60 :                 pefree(data->inbuf, data->persistent);
     163          60 :                 pefree(data->outbuf, data->persistent);
     164          60 :                 pefree(data, data->persistent);
     165             :         }
     166          60 : }
     167             : 
     168             : static php_stream_filter_ops php_zlib_inflate_ops = {
     169             :         php_zlib_inflate_filter,
     170             :         php_zlib_inflate_dtor,
     171             :         "zlib.inflate"
     172             : };
     173             : /* }}} */
     174             : 
     175             : /* {{{ zlib.inflate filter implementation */
     176             : 
     177         128 : static php_stream_filter_status_t php_zlib_deflate_filter(
     178             :         php_stream *stream,
     179             :         php_stream_filter *thisfilter,
     180             :         php_stream_bucket_brigade *buckets_in,
     181             :         php_stream_bucket_brigade *buckets_out,
     182             :         size_t *bytes_consumed,
     183             :         int flags
     184             :         TSRMLS_DC)
     185             : {
     186             :         php_zlib_filter_data *data;
     187             :         php_stream_bucket *bucket;
     188         128 :         size_t consumed = 0;
     189             :         int status;
     190         128 :         php_stream_filter_status_t exit_status = PSFS_FEED_ME;
     191             : 
     192         128 :         if (!thisfilter || !Z_PTR(thisfilter->abstract)) {
     193             :                 /* Should never happen */
     194           0 :                 return PSFS_ERR_FATAL;
     195             :         }
     196             : 
     197         128 :         data = (php_zlib_filter_data *)(Z_PTR(thisfilter->abstract));
     198             : 
     199         303 :         while (buckets_in->head) {
     200          47 :                 size_t bin = 0, desired;
     201             : 
     202          47 :                 bucket = buckets_in->head;
     203             : 
     204          47 :                 bucket = php_stream_bucket_make_writeable(bucket TSRMLS_CC);
     205             : 
     206         141 :                 while (bin < (unsigned int) bucket->buflen) {
     207          47 :                         desired = bucket->buflen - bin;
     208          47 :                         if (desired > data->inbuf_len) {
     209           0 :                                 desired = data->inbuf_len;
     210             :                         }
     211          47 :                         memcpy(data->strm.next_in, bucket->buf + bin, desired);
     212          47 :                         data->strm.avail_in = desired;
     213             : 
     214          47 :                         status = deflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FULL_FLUSH : (flags & PSFS_FLAG_FLUSH_INC ? Z_SYNC_FLUSH : Z_NO_FLUSH));
     215          47 :                         if (status != Z_OK) {
     216             :                                 /* Something bad happened */
     217           0 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     218           0 :                                 return PSFS_ERR_FATAL;
     219             :                         }
     220          47 :                         desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
     221          47 :                         data->strm.next_in = data->inbuf;
     222          47 :                         data->strm.avail_in = 0;
     223          47 :                         bin += desired;
     224             : 
     225          47 :                         if (data->strm.avail_out < data->outbuf_len) {
     226             :                                 php_stream_bucket *out_bucket;
     227          13 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     228             : 
     229          13 :                                 out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     230          13 :                                 php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
     231          13 :                                 data->strm.avail_out = data->outbuf_len;
     232          13 :                                 data->strm.next_out = data->outbuf;
     233          13 :                                 exit_status = PSFS_PASS_ON;
     234             :                         }
     235             :                 }
     236          47 :                 consumed += bucket->buflen;
     237          47 :                 php_stream_bucket_delref(bucket TSRMLS_CC);
     238             :         }
     239             : 
     240         128 :         if (flags & PSFS_FLAG_FLUSH_CLOSE) {
     241             :                 /* Spit it out! */
     242          48 :                 status = Z_OK;
     243         144 :                 while (status == Z_OK) {
     244          48 :                         status = deflate(&(data->strm), Z_FINISH);
     245          48 :                         if (data->strm.avail_out < data->outbuf_len) {
     246          48 :                                 size_t bucketlen = data->outbuf_len - data->strm.avail_out;
     247             : 
     248          48 :                                 bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
     249          48 :                                 php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
     250          48 :                                 data->strm.avail_out = data->outbuf_len;
     251          48 :                                 data->strm.next_out = data->outbuf;
     252          48 :                                 exit_status = PSFS_PASS_ON;
     253             :                         }
     254             :                 }
     255             :         }
     256             : 
     257         128 :         if (bytes_consumed) {
     258          82 :                 *bytes_consumed = consumed;
     259             :         }
     260             : 
     261         128 :         return exit_status;
     262             : }
     263             : 
     264          48 : static void php_zlib_deflate_dtor(php_stream_filter *thisfilter TSRMLS_DC)
     265             : {
     266          48 :         if (thisfilter && Z_PTR(thisfilter->abstract)) {
     267          48 :                 php_zlib_filter_data *data = Z_PTR(thisfilter->abstract);
     268          48 :                 deflateEnd(&(data->strm));
     269          48 :                 pefree(data->inbuf, data->persistent);
     270          48 :                 pefree(data->outbuf, data->persistent);
     271          48 :                 pefree(data, data->persistent);
     272             :         }
     273          48 : }
     274             : 
     275             : static php_stream_filter_ops php_zlib_deflate_ops = {
     276             :         php_zlib_deflate_filter,
     277             :         php_zlib_deflate_dtor,
     278             :         "zlib.deflate"
     279             : };
     280             : 
     281             : /* }}} */
     282             : 
     283             : /* {{{ zlib.* common factory */
     284             : 
     285         108 : static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
     286             : {
     287         108 :         php_stream_filter_ops *fops = NULL;
     288             :         php_zlib_filter_data *data;
     289             :         int status;
     290             : 
     291             :         /* Create this filter */
     292         108 :         data = pecalloc(1, sizeof(php_zlib_filter_data), persistent);
     293         108 :         if (!data) {
     294           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes", sizeof(php_zlib_filter_data));
     295           0 :                 return NULL;
     296             :         }
     297             : 
     298             :         /* Circular reference */
     299         108 :         data->strm.opaque = (voidpf) data;
     300             : 
     301         108 :         data->strm.zalloc = (alloc_func) php_zlib_alloc;
     302         108 :         data->strm.zfree = (free_func) php_zlib_free;
     303         108 :         data->strm.avail_out = data->outbuf_len = data->inbuf_len = 0x8000;
     304         216 :         data->strm.next_in = data->inbuf = (Bytef *) pemalloc(data->inbuf_len, persistent);
     305         108 :         if (!data->inbuf) {
     306           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes", data->inbuf_len);
     307           0 :                 pefree(data, persistent);
     308           0 :                 return NULL;
     309             :         }
     310         108 :         data->strm.avail_in = 0;
     311         216 :         data->strm.next_out = data->outbuf = (Bytef *) pemalloc(data->outbuf_len, persistent);
     312         108 :         if (!data->outbuf) {
     313           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes", data->outbuf_len);
     314           0 :                 pefree(data->inbuf, persistent);
     315           0 :                 pefree(data, persistent);
     316           0 :                 return NULL;
     317             :         }
     318             :                 
     319         108 :         data->strm.data_type = Z_ASCII;
     320             : 
     321         108 :         if (strcasecmp(filtername, "zlib.inflate") == 0) {
     322          60 :                 int windowBits = -MAX_WBITS;
     323             : 
     324          60 :                 if (filterparams) {
     325             :                         zval *tmpzval;
     326             : 
     327          38 :                         if ((Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) &&
     328          19 :                                 (tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
     329             :                                 zval tmp;
     330             : 
     331             :                                 /* log-2 base of history window (9 - 15) */
     332          19 :                                 ZVAL_DUP(&tmp, tmpzval);
     333          19 :                                 convert_to_long(&tmp);
     334          19 :                                 if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 32) {
     335           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for window size. (%pd)", Z_LVAL(tmp));
     336             :                                 } else {
     337          19 :                                         windowBits = Z_LVAL(tmp);
     338             :                                 }
     339             :                         }
     340             :                 }
     341             : 
     342             :                 /* RFC 1951 Inflate */
     343          60 :                 data->finished = '\0';
     344          60 :                 status = inflateInit2(&(data->strm), windowBits);
     345          60 :                 fops = &php_zlib_inflate_ops;
     346          48 :         } else if (strcasecmp(filtername, "zlib.deflate") == 0) {
     347             :                 /* RFC 1951 Deflate */
     348          48 :                 int level = Z_DEFAULT_COMPRESSION;
     349          48 :                 int windowBits = -MAX_WBITS;
     350          48 :                 int memLevel = MAX_MEM_LEVEL;
     351             : 
     352             : 
     353          48 :                 if (filterparams) {
     354             :                         zval *tmpzval, tmp;
     355             : 
     356             :                         /* filterparams can either be a scalar value to indicate compression level (shortcut method)
     357             :                Or can be a hash containing one or more of 'window', 'memory', and/or 'level' members. */
     358             : 
     359          14 :                         switch (Z_TYPE_P(filterparams)) {
     360             :                                 case IS_ARRAY:
     361             :                                 case IS_OBJECT:
     362          14 :                                         if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "memory", sizeof("memory") -1))) {
     363           0 :                                                 ZVAL_DUP(&tmp, tmpzval);
     364           0 :                                                 convert_to_long(&tmp);
     365             : 
     366             :                                                 /* Memory Level (1 - 9) */
     367           0 :                                                 if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > MAX_MEM_LEVEL) {
     368           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for memory level. (%pd)", Z_LVAL(tmp));
     369             :                                                 } else {
     370           0 :                                                         memLevel = Z_LVAL(tmp);
     371             :                                                 }
     372             :                                         }
     373             : 
     374          14 :                                         if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
     375          13 :                                                 ZVAL_DUP(&tmp, tmpzval);
     376          13 :                                                 convert_to_long(&tmp);
     377             : 
     378             :                                                 /* log-2 base of history window (9 - 15) */
     379          13 :                                                 if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 16) {
     380           0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for window size. (%pd)", Z_LVAL(tmp));
     381             :                                                 } else {
     382          13 :                                                         windowBits = Z_LVAL(tmp);
     383             :                                                 }
     384             :                                         }
     385             : 
     386          14 :                                         if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "level", sizeof("level") - 1))) {
     387           1 :                                                 ZVAL_COPY_VALUE(&tmp, tmpzval);
     388             : 
     389             :                                                 /* Psuedo pass through to catch level validating code */
     390           1 :                                                 goto factory_setlevel;
     391             :                                         }
     392          13 :                                         break;
     393             :                                 case IS_STRING:
     394             :                                 case IS_DOUBLE:
     395             :                                 case IS_LONG:
     396           0 :                                         ZVAL_COPY_VALUE(&tmp, filterparams);
     397             : factory_setlevel:
     398             :                                         zval_copy_ctor(&tmp);
     399           1 :                                         convert_to_long(&tmp);
     400             : 
     401             :                                         /* Set compression level within reason (-1 == default, 0 == none, 1-9 == least to most compression */
     402           1 :                                         if (Z_LVAL(tmp) < -1 || Z_LVAL(tmp) > 9) {
     403           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid compression level specified. (%pd)", Z_LVAL(tmp));
     404             :                                         } else {
     405           1 :                                                 level = Z_LVAL(tmp);
     406             :                                         }
     407           1 :                                         break;
     408             :                                 default:
     409           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filter parameter, ignored");
     410             :                         }
     411             :                 }
     412          48 :                 status = deflateInit2(&(data->strm), level, Z_DEFLATED, windowBits, memLevel, 0);
     413          48 :                 fops = &php_zlib_deflate_ops;
     414             :         } else {
     415           0 :                 status = Z_DATA_ERROR;
     416             :         }
     417             : 
     418         108 :         if (status != Z_OK) {
     419             :                 /* Unspecified (probably strm) error, let stream-filter error do its own whining */
     420           0 :                 pefree(data->strm.next_in, persistent);
     421           0 :                 pefree(data->strm.next_out, persistent);
     422           0 :                 pefree(data, persistent);
     423           0 :                 return NULL;
     424             :         }
     425             : 
     426         108 :         return php_stream_filter_alloc(fops, data, persistent);
     427             : }
     428             : 
     429             : php_stream_filter_factory php_zlib_filter_factory = {
     430             :         php_zlib_filter_create
     431             : };
     432             : /* }}} */
     433             : 
     434             : /*
     435             :  * Local variables:
     436             :  * tab-width: 4
     437             :  * c-basic-offset: 4
     438             :  * End:
     439             :  * vim600: sw=4 ts=4 fdm=marker
     440             :  * vim<600: sw=4 ts=4
     441             :  */

Generated by: LCOV version 1.10

Generated at Mon, 10 Nov 2014 22:46:51 +0000 (11 days ago)

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