Back to index

php5  5.3.10
zlib_filter.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Sara Golemon (pollita@php.net)                              |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: zlib_filter.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 #include "php_zlib.h"
00023 
00024 /* {{{ data structure */
00025 
00026 /* Passed as opaque in malloc callbacks */
00027 typedef struct _php_zlib_filter_data {
00028        int persistent;
00029        z_stream strm;
00030        char *inbuf;
00031        size_t inbuf_len;
00032        char *outbuf;
00033        size_t outbuf_len;
00034        zend_bool finished;
00035 } php_zlib_filter_data;
00036 
00037 /* }}} */
00038 
00039 /* {{{ Memory management wrappers */
00040 
00041 static voidpf php_zlib_alloc(voidpf opaque, uInt items, uInt size)
00042 {
00043        return (voidpf)safe_pemalloc(items, size, 0, ((php_zlib_filter_data*)opaque)->persistent);
00044 }
00045 
00046 static void php_zlib_free(voidpf opaque, voidpf address)
00047 {
00048        pefree((void*)address, ((php_zlib_filter_data*)opaque)->persistent);
00049 }
00050 /* }}} */
00051 
00052 /* {{{ zlib.inflate filter implementation */
00053 
00054 static php_stream_filter_status_t php_zlib_inflate_filter(
00055        php_stream *stream,
00056        php_stream_filter *thisfilter,
00057        php_stream_bucket_brigade *buckets_in,
00058        php_stream_bucket_brigade *buckets_out,
00059        size_t *bytes_consumed,
00060        int flags
00061        TSRMLS_DC)
00062 {
00063        php_zlib_filter_data *data;
00064        php_stream_bucket *bucket;
00065        size_t consumed = 0, original_out, original_in;
00066        int status;
00067        php_stream_filter_status_t exit_status = PSFS_FEED_ME;
00068        z_stream *streamp;
00069 
00070        if (!thisfilter || !thisfilter->abstract) {
00071               /* Should never happen */
00072               return PSFS_ERR_FATAL;
00073        }
00074 
00075        data = (php_zlib_filter_data *)(thisfilter->abstract);
00076        streamp = &(data->strm);
00077        original_in = data->strm.total_in;
00078        original_out = data->strm.total_out;
00079 
00080        while (buckets_in->head) {
00081               size_t bin = 0, desired;
00082 
00083               bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
00084               while (bin < bucket->buflen) {
00085 
00086                      if (data->finished) {
00087                             consumed += bucket->buflen;
00088                             break;
00089                      }
00090 
00091                      desired = bucket->buflen - bin;
00092                      if (desired > data->inbuf_len) {
00093                             desired = data->inbuf_len;
00094                      }
00095                      memcpy(data->strm.next_in, bucket->buf + bin, desired);
00096                      data->strm.avail_in = desired;
00097 
00098                      status = inflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH);
00099                      if (status == Z_STREAM_END) {
00100                             inflateEnd(&(data->strm));
00101                             data->finished = '\1';
00102                      } else if (status != Z_OK) {
00103                             /* Something bad happened */
00104                             php_stream_bucket_delref(bucket TSRMLS_CC);
00105                             /* reset these because despite the error the filter may be used again */
00106                             data->strm.next_in = data->inbuf;
00107                             data->strm.avail_in = 0;
00108                             return PSFS_ERR_FATAL;
00109                      }
00110                      desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
00111                      data->strm.next_in = data->inbuf;
00112                      data->strm.avail_in = 0;
00113                      consumed += desired;
00114                      bin += desired;
00115 
00116                      if (data->strm.avail_out < data->outbuf_len) {
00117                             php_stream_bucket *out_bucket;
00118                             size_t bucketlen = data->outbuf_len - data->strm.avail_out;
00119                             out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
00120                             php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
00121                             data->strm.avail_out = data->outbuf_len;
00122                             data->strm.next_out = data->outbuf;
00123                             exit_status = PSFS_PASS_ON;
00124                      } else if (status == Z_STREAM_END && data->strm.avail_out >= data->outbuf_len) {
00125                             /* no more data to decompress, and nothing was spat out */
00126                             php_stream_bucket_delref(bucket TSRMLS_CC);
00127                             return PSFS_PASS_ON;
00128                      }
00129               }
00130               php_stream_bucket_delref(bucket TSRMLS_CC);
00131        }
00132 
00133        if (!data->finished && flags & PSFS_FLAG_FLUSH_CLOSE) {
00134               /* Spit it out! */
00135               status = Z_OK;
00136               while (status == Z_OK) {
00137                      status = inflate(&(data->strm), Z_FINISH);
00138                      if (data->strm.avail_out < data->outbuf_len) {
00139                             size_t bucketlen = data->outbuf_len - data->strm.avail_out;
00140 
00141                             bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
00142                             php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
00143                             data->strm.avail_out = data->outbuf_len;
00144                             data->strm.next_out = data->outbuf;
00145                             exit_status = PSFS_PASS_ON;
00146                      }
00147               }
00148        }
00149 
00150        if (bytes_consumed) {
00151               *bytes_consumed = consumed;
00152        }
00153 
00154        return exit_status;
00155 }
00156 
00157 static void php_zlib_inflate_dtor(php_stream_filter *thisfilter TSRMLS_DC)
00158 {
00159        if (thisfilter && thisfilter->abstract) {
00160               php_zlib_filter_data *data = thisfilter->abstract;
00161               if (!data->finished) {
00162                      inflateEnd(&(data->strm));
00163               }
00164               pefree(data->inbuf, data->persistent);
00165               pefree(data->outbuf, data->persistent);
00166               pefree(data, data->persistent);
00167        }
00168 }
00169 
00170 static php_stream_filter_ops php_zlib_inflate_ops = {
00171        php_zlib_inflate_filter,
00172        php_zlib_inflate_dtor,
00173        "zlib.inflate"
00174 };
00175 /* }}} */
00176 
00177 /* {{{ zlib.inflate filter implementation */
00178 
00179 static php_stream_filter_status_t php_zlib_deflate_filter(
00180        php_stream *stream,
00181        php_stream_filter *thisfilter,
00182        php_stream_bucket_brigade *buckets_in,
00183        php_stream_bucket_brigade *buckets_out,
00184        size_t *bytes_consumed,
00185        int flags
00186        TSRMLS_DC)
00187 {
00188        php_zlib_filter_data *data;
00189        php_stream_bucket *bucket;
00190        size_t consumed = 0, original_out, original_in;
00191        int status;
00192        php_stream_filter_status_t exit_status = PSFS_FEED_ME;
00193        z_stream *streamp;
00194 
00195        if (!thisfilter || !thisfilter->abstract) {
00196               /* Should never happen */
00197               return PSFS_ERR_FATAL;
00198        }
00199 
00200        data = (php_zlib_filter_data *)(thisfilter->abstract);
00201        streamp = &(data->strm);
00202        original_in = data->strm.total_in;
00203        original_out = data->strm.total_out;
00204 
00205        while (buckets_in->head) {
00206               size_t bin = 0, desired;
00207 
00208               bucket = php_stream_bucket_make_writeable(buckets_in->head TSRMLS_CC);
00209 
00210               while (bin < bucket->buflen) {
00211                      desired = bucket->buflen - bin;
00212                      if (desired > data->inbuf_len) {
00213                             desired = data->inbuf_len;
00214                      }
00215                      memcpy(data->strm.next_in, bucket->buf + bin, desired);
00216                      data->strm.avail_in = desired;
00217 
00218                      status = deflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FULL_FLUSH : (flags & PSFS_FLAG_FLUSH_INC ? Z_SYNC_FLUSH : Z_NO_FLUSH));
00219                      if (status != Z_OK) {
00220                             /* Something bad happened */
00221                             php_stream_bucket_delref(bucket TSRMLS_CC);
00222                             return PSFS_ERR_FATAL;
00223                      }
00224                      desired -= data->strm.avail_in; /* desired becomes what we consumed this round through */
00225                      data->strm.next_in = data->inbuf;
00226                      data->strm.avail_in = 0;
00227                      consumed += desired;
00228                      bin += desired;
00229 
00230                      if (data->strm.avail_out < data->outbuf_len) {
00231                             php_stream_bucket *out_bucket;
00232                             size_t bucketlen = data->outbuf_len - data->strm.avail_out;
00233 
00234                             out_bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
00235                             php_stream_bucket_append(buckets_out, out_bucket TSRMLS_CC);
00236                             data->strm.avail_out = data->outbuf_len;
00237                             data->strm.next_out = data->outbuf;
00238                             exit_status = PSFS_PASS_ON;
00239                      }
00240               }
00241               php_stream_bucket_delref(bucket TSRMLS_CC);
00242        }
00243 
00244        if (flags & PSFS_FLAG_FLUSH_CLOSE) {
00245               /* Spit it out! */
00246               status = Z_OK;
00247               while (status == Z_OK) {
00248                      status = deflate(&(data->strm), Z_FINISH);
00249                      if (data->strm.avail_out < data->outbuf_len) {
00250                             size_t bucketlen = data->outbuf_len - data->strm.avail_out;
00251 
00252                             bucket = php_stream_bucket_new(stream, estrndup(data->outbuf, bucketlen), bucketlen, 1, 0 TSRMLS_CC);
00253                             php_stream_bucket_append(buckets_out, bucket TSRMLS_CC);
00254                             data->strm.avail_out = data->outbuf_len;
00255                             data->strm.next_out = data->outbuf;
00256                             exit_status = PSFS_PASS_ON;
00257                      }
00258               }
00259        }
00260 
00261        if (bytes_consumed) {
00262               *bytes_consumed = consumed;
00263        }
00264        return exit_status;
00265 }
00266 
00267 static void php_zlib_deflate_dtor(php_stream_filter *thisfilter TSRMLS_DC)
00268 {
00269        if (thisfilter && thisfilter->abstract) {
00270               php_zlib_filter_data *data = thisfilter->abstract;
00271               deflateEnd(&(data->strm));
00272               pefree(data->inbuf, data->persistent);
00273               pefree(data->outbuf, data->persistent);
00274               pefree(data, data->persistent);
00275        }
00276 }
00277 
00278 static php_stream_filter_ops php_zlib_deflate_ops = {
00279        php_zlib_deflate_filter,
00280        php_zlib_deflate_dtor,
00281        "zlib.deflate"
00282 };
00283 
00284 /* }}} */
00285 
00286 /* {{{ zlib.* common factory */
00287 
00288 static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *filterparams, int persistent TSRMLS_DC)
00289 {
00290        php_stream_filter_ops *fops = NULL;
00291        php_zlib_filter_data *data;
00292        int status;
00293 
00294        /* Create this filter */
00295        data = pecalloc(1, sizeof(php_zlib_filter_data), persistent);
00296        if (!data) {
00297               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes.", sizeof(php_zlib_filter_data));
00298               return NULL;
00299        }
00300 
00301        /* Circular reference */
00302        data->strm.opaque = (voidpf) data;
00303 
00304        data->strm.zalloc = (alloc_func) php_zlib_alloc;
00305        data->strm.zfree = (free_func) php_zlib_free;
00306        data->strm.avail_out = data->outbuf_len = data->inbuf_len = 2048;
00307        data->strm.next_in = data->inbuf = (Bytef *) pemalloc(data->inbuf_len, persistent);
00308        if (!data->inbuf) {
00309               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes.", data->inbuf_len);
00310               pefree(data, persistent);
00311               return NULL;
00312        }
00313        data->strm.avail_in = 0;
00314        data->strm.next_out = data->outbuf = (Bytef *) pemalloc(data->outbuf_len, persistent);
00315        if (!data->outbuf) {
00316               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed allocating %zd bytes.", data->outbuf_len);
00317               pefree(data->inbuf, persistent);
00318               pefree(data, persistent);
00319               return NULL;
00320        }
00321               
00322        data->strm.data_type = Z_ASCII;
00323 
00324        if (strcasecmp(filtername, "zlib.inflate") == 0) {
00325               int windowBits = -MAX_WBITS;
00326 
00327               if (filterparams) {
00328                      zval **tmpzval;
00329 
00330                      if ((Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) &&
00331                             zend_hash_find(HASH_OF(filterparams), "window", sizeof("window"), (void **) &tmpzval) == SUCCESS) {
00332                             zval tmp;
00333 
00334                             /* log-2 base of history window (9 - 15) */
00335                             tmp = **tmpzval;
00336                             zval_copy_ctor(&tmp);
00337                             convert_to_long(&tmp);
00338                             if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 32) {
00339                                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for window size. (%ld)", Z_LVAL(tmp));
00340                             } else {
00341                                    windowBits = Z_LVAL(tmp);
00342                             }
00343                      }
00344               }
00345 
00346               /* RFC 1951 Inflate */
00347               data->finished = '\0';
00348               status = inflateInit2(&(data->strm), windowBits);
00349               fops = &php_zlib_inflate_ops;
00350        } else if (strcasecmp(filtername, "zlib.deflate") == 0) {
00351               /* RFC 1951 Deflate */
00352               int level = Z_DEFAULT_COMPRESSION;
00353               int windowBits = -MAX_WBITS;
00354               int memLevel = MAX_MEM_LEVEL;
00355 
00356 
00357               if (filterparams) {
00358                      zval **tmpzval, tmp;
00359 
00360                      /* filterparams can either be a scalar value to indicate compression level (shortcut method)
00361                Or can be a hash containing one or more of 'window', 'memory', and/or 'level' members. */
00362 
00363                      switch (Z_TYPE_P(filterparams)) {
00364                             case IS_ARRAY:
00365                             case IS_OBJECT:
00366                                    if (zend_hash_find(HASH_OF(filterparams), "memory", sizeof("memory"), (void**) &tmpzval) == SUCCESS) {
00367                                           tmp = **tmpzval;
00368                                           zval_copy_ctor(&tmp);
00369                                           convert_to_long(&tmp);
00370 
00371                                           /* Memory Level (1 - 9) */
00372                                           if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > MAX_MEM_LEVEL) {
00373                                                  php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for memory level. (%ld)", Z_LVAL(tmp));
00374                                           } else {
00375                                                  memLevel = Z_LVAL(tmp);
00376                                           }
00377                                    }
00378 
00379                                    if (zend_hash_find(HASH_OF(filterparams), "window", sizeof("window"), (void**) &tmpzval) == SUCCESS) {
00380                                           tmp = **tmpzval;
00381                                           zval_copy_ctor(&tmp);
00382                                           convert_to_long(&tmp);
00383 
00384                                           /* log-2 base of history window (9 - 15) */
00385                                           if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 16) {
00386                                                  php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for window size. (%ld)", Z_LVAL(tmp));
00387                                           } else {
00388                                                  windowBits = Z_LVAL(tmp);
00389                                           }
00390                                    }
00391 
00392                                    if (zend_hash_find(HASH_OF(filterparams), "level", sizeof("level"), (void**) &tmpzval) == SUCCESS) {
00393                                           tmp = **tmpzval;
00394 
00395                                           /* Psuedo pass through to catch level validating code */
00396                                           goto factory_setlevel;
00397                                    }
00398                                    break;
00399                             case IS_STRING:
00400                             case IS_DOUBLE:
00401                             case IS_LONG:
00402                                    tmp = *filterparams;
00403 factory_setlevel:
00404                                    zval_copy_ctor(&tmp);
00405                                    convert_to_long(&tmp);
00406 
00407                                    /* Set compression level within reason (-1 == default, 0 == none, 1-9 == least to most compression */
00408                                    if (Z_LVAL(tmp) < -1 || Z_LVAL(tmp) > 9) {
00409                                           php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid compression level specified. (%ld)", Z_LVAL(tmp));
00410                                    } else {
00411                                           level = Z_LVAL(tmp);
00412                                    }
00413                                    break;
00414                             default:
00415                                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filter parameter, ignored.");
00416                      }
00417               }
00418               status = deflateInit2(&(data->strm), level, Z_DEFLATED, windowBits, memLevel, 0);
00419               fops = &php_zlib_deflate_ops;
00420        } else {
00421               status = Z_DATA_ERROR;
00422        }
00423 
00424        if (status != Z_OK) {
00425               /* Unspecified (probably strm) error, let stream-filter error do its own whining */
00426               pefree(data->strm.next_in, persistent);
00427               pefree(data->strm.next_out, persistent);
00428               pefree(data, persistent);
00429               return NULL;
00430        }
00431 
00432        return php_stream_filter_alloc(fops, data, persistent);
00433 }
00434 
00435 php_stream_filter_factory php_zlib_filter_factory = {
00436        php_zlib_filter_create
00437 };
00438 /* }}} */
00439 
00440 /*
00441  * Local variables:
00442  * tab-width: 4
00443  * c-basic-offset: 4
00444  * End:
00445  * vim600: sw=4 ts=4 fdm=marker
00446  * vim<600: sw=4 ts=4
00447  */