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.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 191 211 90.5 %
Date: 2014-11-22 Functions: 17 17 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             :   | Author: Sterling Hughes <sterling@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             : #if HAVE_BZ2
      29             : 
      30             : /* PHP Includes */
      31             : #include "ext/standard/file.h"
      32             : #include "ext/standard/info.h"
      33             : #include "ext/standard/php_string.h"
      34             : #include "main/php_network.h"
      35             : 
      36             : /* for fileno() */
      37             : #include <stdio.h>
      38             : 
      39             : /* Internal error constants */
      40             : #define PHP_BZ_ERRNO   0
      41             : #define PHP_BZ_ERRSTR  1
      42             : #define PHP_BZ_ERRBOTH 2
      43             : 
      44             : static PHP_MINIT_FUNCTION(bz2);
      45             : static PHP_MSHUTDOWN_FUNCTION(bz2);
      46             : static PHP_MINFO_FUNCTION(bz2);
      47             : static PHP_FUNCTION(bzopen);
      48             : static PHP_FUNCTION(bzread);
      49             : static PHP_FUNCTION(bzerrno);
      50             : static PHP_FUNCTION(bzerrstr);
      51             : static PHP_FUNCTION(bzerror);
      52             : static PHP_FUNCTION(bzcompress);
      53             : static PHP_FUNCTION(bzdecompress);
      54             : 
      55             : /* {{{ arginfo */
      56             : ZEND_BEGIN_ARG_INFO_EX(arginfo_bzread, 0, 0, 1)
      57             :         ZEND_ARG_INFO(0, bz)
      58             :         ZEND_ARG_INFO(0, length)
      59             : ZEND_END_ARG_INFO()
      60             : 
      61             : ZEND_BEGIN_ARG_INFO(arginfo_bzopen, 0)
      62             :         ZEND_ARG_INFO(0, file)
      63             :         ZEND_ARG_INFO(0, mode)
      64             : ZEND_END_ARG_INFO()
      65             : 
      66             : ZEND_BEGIN_ARG_INFO(arginfo_bzerrno, 0)
      67             :         ZEND_ARG_INFO(0, bz)
      68             : ZEND_END_ARG_INFO()
      69             : 
      70             : ZEND_BEGIN_ARG_INFO(arginfo_bzerrstr, 0)
      71             :         ZEND_ARG_INFO(0, bz)
      72             : ZEND_END_ARG_INFO()
      73             : 
      74             : ZEND_BEGIN_ARG_INFO(arginfo_bzerror, 0)
      75             :         ZEND_ARG_INFO(0, bz)
      76             : ZEND_END_ARG_INFO()
      77             : 
      78             : ZEND_BEGIN_ARG_INFO_EX(arginfo_bzcompress, 0, 0, 2)
      79             :         ZEND_ARG_INFO(0, source)
      80             :         ZEND_ARG_INFO(0, blocksize)
      81             :         ZEND_ARG_INFO(0, workfactor)
      82             : ZEND_END_ARG_INFO()
      83             : 
      84             : ZEND_BEGIN_ARG_INFO_EX(arginfo_bzdecompress, 0, 0, 1)
      85             :         ZEND_ARG_INFO(0, source)
      86             :         ZEND_ARG_INFO(0, small)
      87             : ZEND_END_ARG_INFO()
      88             : 
      89             : ZEND_BEGIN_ARG_INFO_EX(arginfo_bzwrite, 0, 0, 2)
      90             :         ZEND_ARG_INFO(0, fp)
      91             :         ZEND_ARG_INFO(0, str)
      92             :         ZEND_ARG_INFO(0, length)
      93             : ZEND_END_ARG_INFO()
      94             : 
      95             : ZEND_BEGIN_ARG_INFO(arginfo_bzflush, 0)
      96             :         ZEND_ARG_INFO(0, fp)
      97             : ZEND_END_ARG_INFO()
      98             : /* }}} */
      99             : 
     100             : static const zend_function_entry bz2_functions[] = {
     101             :         PHP_FE(bzopen,       arginfo_bzopen)
     102             :         PHP_FE(bzread,       arginfo_bzread)
     103             :         PHP_FALIAS(bzwrite,   fwrite,           arginfo_bzwrite)
     104             :         PHP_FALIAS(bzflush,   fflush,           arginfo_bzflush)
     105             :         PHP_FALIAS(bzclose,   fclose,           arginfo_bzflush)
     106             :         PHP_FE(bzerrno,      arginfo_bzerrno)
     107             :         PHP_FE(bzerrstr,     arginfo_bzerrstr)
     108             :         PHP_FE(bzerror,      arginfo_bzerror)
     109             :         PHP_FE(bzcompress,   arginfo_bzcompress)
     110             :         PHP_FE(bzdecompress, arginfo_bzdecompress)
     111             :         PHP_FE_END
     112             : };
     113             : 
     114             : zend_module_entry bz2_module_entry = {
     115             :         STANDARD_MODULE_HEADER,
     116             :         "bz2",
     117             :         bz2_functions,
     118             :         PHP_MINIT(bz2),
     119             :         PHP_MSHUTDOWN(bz2),
     120             :         NULL,
     121             :         NULL,
     122             :         PHP_MINFO(bz2),
     123             :         NO_VERSION_YET,
     124             :         STANDARD_MODULE_PROPERTIES
     125             : };
     126             : 
     127             : #ifdef COMPILE_DL_BZ2
     128             : ZEND_GET_MODULE(bz2)
     129             : #endif
     130             : 
     131             : struct php_bz2_stream_data_t {
     132             :         BZFILE *bz_file;
     133             :         php_stream *stream;
     134             : };
     135             : 
     136             : /* {{{ BZip2 stream implementation */
     137             : 
     138          10 : static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
     139             : {
     140          10 :         struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
     141          10 :         size_t ret = 0;
     142             :         
     143             :         do {
     144             :                 int just_read;
     145          13 :                 size_t remain = count - ret;
     146          13 :                 int to_read = (int)(remain <= INT_MAX ? remain : INT_MAX);
     147             : 
     148          13 :                 just_read = BZ2_bzread(self->bz_file, buf, to_read);
     149             : 
     150          13 :                 if (just_read < 1) {
     151          10 :                         stream->eof = 0 == just_read;
     152          10 :                         break;
     153             :                 }
     154             : 
     155           3 :                 ret += just_read;
     156           3 :         } while (ret < count);
     157             : 
     158          10 :         return ret;
     159             : }
     160             : 
     161          15 : static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
     162             : {
     163          15 :         size_t wrote = 0;
     164          15 :         struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
     165             : 
     166             : 
     167             :         do {
     168             :                 int just_wrote;
     169          15 :                 size_t remain = count - wrote;
     170          15 :                 int to_write = (int)(remain <= INT_MAX ? remain : INT_MAX);
     171             : 
     172          15 :                 just_wrote = BZ2_bzwrite(self->bz_file, (char*)buf, to_write);
     173             : 
     174          15 :                 if (just_wrote < 1) {
     175           0 :                         break;
     176             :                 }
     177             : 
     178          15 :                 wrote += just_wrote;
     179             : 
     180          15 :         } while (wrote < count);
     181             : 
     182          15 :         return wrote;
     183             : }
     184             : 
     185          16 : static int php_bz2iop_close(php_stream *stream, int close_handle TSRMLS_DC)
     186             : {
     187          16 :         struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
     188          16 :         int ret = EOF;
     189             :         
     190          16 :         if (close_handle) {
     191          16 :                 BZ2_bzclose(self->bz_file);
     192             :         }
     193             : 
     194          16 :         if (self->stream) {
     195           7 :                 php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
     196             :         }
     197             : 
     198          16 :         efree(self);
     199             : 
     200          16 :         return ret;
     201             : }
     202             : 
     203          16 : static int php_bz2iop_flush(php_stream *stream TSRMLS_DC)
     204             : {
     205          16 :         struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
     206          16 :         return BZ2_bzflush(self->bz_file);
     207             : }
     208             : /* }}} */
     209             : 
     210             : php_stream_ops php_stream_bz2io_ops = {
     211             :         php_bz2iop_write, php_bz2iop_read,
     212             :         php_bz2iop_close, php_bz2iop_flush,
     213             :         "BZip2",
     214             :         NULL, /* seek */
     215             :         NULL, /* cast */
     216             :         NULL, /* stat */
     217             :         NULL  /* set_option */
     218             : };
     219             : 
     220             : /* {{{ Bzip2 stream openers */
     221          16 : PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, 
     222             :                                                                                                                 const char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC)
     223             : {
     224             :         struct php_bz2_stream_data_t *self;
     225             :         
     226          16 :         self = emalloc(sizeof(*self));
     227             : 
     228          16 :         self->stream = innerstream;
     229          16 :         self->bz_file = bz;
     230             : 
     231          16 :         return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
     232             : }
     233             : 
     234          10 : PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
     235             :                                                                                         const char *path,
     236             :                                                                                         const char *mode,
     237             :                                                                                         int options,
     238             :                                                                                         char **opened_path,
     239             :                                                                                         php_stream_context *context STREAMS_DC TSRMLS_DC)
     240             : {
     241          10 :         php_stream *retstream = NULL, *stream = NULL;
     242          10 :         char *path_copy = NULL;
     243          10 :         BZFILE *bz_file = NULL;
     244             : 
     245          10 :         if (strncasecmp("compress.bzip2://", path, 17) == 0) {
     246           0 :                 path += 17;
     247             :         }
     248          10 :         if (mode[0] == '\0' || (mode[0] != 'w' && mode[0] != 'r' && mode[1] != '\0')) {
     249           0 :                 return NULL;
     250             :         }
     251             : 
     252             : #ifdef VIRTUAL_DIR
     253             :         virtual_filepath_ex(path, &path_copy, NULL TSRMLS_CC);
     254             : #else
     255          10 :         path_copy = path;
     256             : #endif
     257             : 
     258          10 :         if (php_check_open_basedir(path_copy TSRMLS_CC)) {
     259             : #ifdef VIRTUAL_DIR
     260             :                 efree(path_copy);
     261             : #endif
     262           0 :                 return NULL;
     263             :         }
     264             :         
     265             :         /* try and open it directly first */
     266          10 :         bz_file = BZ2_bzopen(path_copy, mode);
     267             : 
     268          10 :         if (opened_path && bz_file) {
     269             : #ifdef VIRTUAL_DIR
     270             :                 *opened_path = path_copy;
     271             :                 path_copy = NULL;
     272             : #else
     273           0 :                 *opened_path = estrdup(path_copy);
     274             : #endif
     275             :         }
     276             : 
     277             : #ifdef VIRTUAL_DIR
     278             :         if (path_copy) {
     279             :                 efree(path_copy);
     280             :         }
     281             : #endif
     282          10 :         path_copy = NULL;
     283             :         
     284          10 :         if (bz_file == NULL) {
     285             :                 /* that didn't work, so try and get something from the network/wrapper */
     286           1 :                 stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST, opened_path);
     287             :         
     288           1 :                 if (stream) {
     289             :                         php_socket_t fd;
     290           0 :                         if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
     291           0 :                                 bz_file = BZ2_bzdopen((int)fd, mode);
     292             :                         }
     293             :                 }
     294             : 
     295             :                 /* remove the file created by php_stream_open_wrapper(), it is not needed since BZ2 functions
     296             :                  * failed.
     297             :                  */
     298           1 :                 if (opened_path && !bz_file && mode[0] == 'w') {
     299           0 :                         VCWD_UNLINK(*opened_path);
     300             :                 }
     301             :         }
     302             :         
     303          10 :         if (bz_file) {
     304           9 :                 retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC TSRMLS_CC);
     305           9 :                 if (retstream) {
     306           9 :                         return retstream;
     307             :                 }
     308             : 
     309           0 :                 BZ2_bzclose(bz_file);
     310             :         }
     311             : 
     312           1 :         if (stream) {
     313           0 :                 php_stream_close(stream);
     314             :         }
     315             : 
     316           1 :         return NULL;
     317             : }
     318             : 
     319             : /* }}} */
     320             : 
     321             : static php_stream_wrapper_ops bzip2_stream_wops = {
     322             :         _php_stream_bz2open,
     323             :         NULL, /* close */
     324             :         NULL, /* fstat */
     325             :         NULL, /* stat */
     326             :         NULL, /* opendir */
     327             :         "BZip2",
     328             :         NULL, /* unlink */
     329             :         NULL, /* rename */
     330             :         NULL, /* mkdir */
     331             :         NULL  /* rmdir */
     332             : };
     333             : 
     334             : static php_stream_wrapper php_stream_bzip2_wrapper = {
     335             :         &bzip2_stream_wops,
     336             :         NULL,
     337             :         0 /* is_url */
     338             : };
     339             : 
     340             : static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
     341             : 
     342       20507 : static PHP_MINIT_FUNCTION(bz2)
     343             : {
     344       20507 :         php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper TSRMLS_CC);
     345       20507 :         php_stream_filter_register_factory("bzip2.*", &php_bz2_filter_factory TSRMLS_CC);
     346       20507 :         return SUCCESS;
     347             : }
     348             : 
     349       20541 : static PHP_MSHUTDOWN_FUNCTION(bz2)
     350             : {
     351       20541 :         php_unregister_url_stream_wrapper("compress.bzip2" TSRMLS_CC);
     352       20541 :         php_stream_filter_unregister_factory("bzip2.*" TSRMLS_CC);
     353             : 
     354       20541 :         return SUCCESS;
     355             : }
     356             : 
     357         143 : static PHP_MINFO_FUNCTION(bz2)
     358             : {
     359         143 :         php_info_print_table_start();
     360         143 :         php_info_print_table_row(2, "BZip2 Support", "Enabled");
     361         143 :         php_info_print_table_row(2, "Stream Wrapper support", "compress.bzip2://");
     362         143 :         php_info_print_table_row(2, "Stream Filter support", "bzip2.decompress, bzip2.compress");
     363         143 :         php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
     364         143 :         php_info_print_table_end();
     365         143 : }
     366             : 
     367             : /* {{{ proto string bzread(resource bz[, int length])
     368             :    Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */
     369          16 : static PHP_FUNCTION(bzread)
     370             : {
     371             :         zval *bz;
     372          16 :         zend_long len = 1024;
     373             :         php_stream *stream;
     374             :         zend_string *data;
     375             : 
     376          16 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &bz, &len)) {
     377           2 :                 RETURN_FALSE;
     378             :         }
     379             : 
     380          14 :         php_stream_from_zval(stream, bz);
     381             : 
     382          13 :         if ((len + 1) < 1) {
     383           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "length may not be negative");
     384           1 :                 RETURN_FALSE;
     385             :         }
     386          24 :         data = zend_string_alloc(len, 0);
     387          12 :         data->len = php_stream_read(stream, data->val, data->len);
     388          12 :         data->val[data->len] = '\0';
     389             : 
     390          12 :         RETURN_STR(data);
     391             : }
     392             : /* }}} */
     393             : 
     394             : /* {{{ proto resource bzopen(string|int file|fp, string mode)
     395             :    Opens a new BZip2 stream */
     396          37 : static PHP_FUNCTION(bzopen)
     397             : {
     398             :         zval     *file;   /* The file to open */
     399             :         char     *mode;   /* The mode to open the stream with */
     400             :         size_t      mode_len;
     401             : 
     402             :         BZFILE   *bz;     /* The compressed file stream */
     403          37 :         php_stream *stream = NULL;
     404             :         
     405          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &file, &mode, &mode_len) == FAILURE) {
     406           1 :                 return;
     407             :         }
     408             : 
     409          36 :         if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) {
     410           3 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode);
     411           3 :                 RETURN_FALSE;
     412             :         }
     413             : 
     414             :         /* If it's not a resource its a string containing the filename to open */
     415          66 :         if (Z_TYPE_P(file) == IS_STRING) {
     416          12 :                 if (Z_STRLEN_P(file) == 0) {
     417           2 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "filename cannot be empty");
     418           2 :                         RETURN_FALSE;
     419             :                 }
     420             :                 
     421          10 :                 if (CHECK_ZVAL_NULL_PATH(file)) {
     422           0 :                         RETURN_FALSE;
     423             :                 }
     424             : 
     425          10 :                 stream = php_stream_bz2open(NULL, Z_STRVAL_P(file), mode, REPORT_ERRORS, NULL);
     426          42 :         } else if (Z_TYPE_P(file) == IS_RESOURCE) {
     427             :                 /* If it is a resource, than its a stream resource */
     428             :                 php_socket_t fd;
     429             :                 size_t stream_mode_len;
     430             : 
     431          19 :                 php_stream_from_zval(stream, file);
     432          19 :                 stream_mode_len = strlen(stream->mode);
     433             :                 
     434          19 :                 if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
     435           8 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
     436           8 :                         RETURN_FALSE;
     437          11 :                 } else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
     438           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
     439           0 :                         RETURN_FALSE;
     440             :                 }
     441             : 
     442          11 :                 switch(mode[0]) {
     443             :                         case 'r':
     444             :                                 /* only "r" and "rb" are supported */
     445           6 :                                 if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])) {
     446           3 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot read from a stream opened in write only mode");
     447           3 :                                         RETURN_FALSE;
     448             :                                 }
     449           3 :                                 break;
     450             :                         case 'w':
     451             :                                 /* support only "w"(b), "a"(b), "x"(b) */
     452          10 :                                 if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])
     453           3 :                                         && stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
     454           2 :                                         && stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
     455           1 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write to a stream opened in read only mode");
     456           1 :                                         RETURN_FALSE;
     457             :                                 }
     458             :                                 break;
     459             :                         default:
     460             :                                 /* not reachable */
     461             :                                 break;
     462             :                 }
     463             : 
     464           7 :                 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
     465           0 :                         RETURN_FALSE;
     466             :                 }
     467             :                 
     468           7 :                 bz = BZ2_bzdopen((int)fd, mode);
     469             : 
     470           7 :                 stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
     471             :         } else {
     472           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "first parameter has to be string or file-resource");
     473           2 :                 RETURN_FALSE;
     474             :         }
     475             : 
     476          17 :         if (stream) {
     477          16 :                 php_stream_to_zval(stream, return_value);
     478             :         } else {
     479           1 :                 RETURN_FALSE;
     480             :         }
     481             : }
     482             : /* }}} */
     483             : 
     484             : /* {{{ proto int bzerrno(resource bz)
     485             :    Returns the error number */
     486           7 : static PHP_FUNCTION(bzerrno)
     487             : {
     488           7 :         php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
     489           7 : }
     490             : /* }}} */
     491             : 
     492             : /* {{{ proto string bzerrstr(resource bz)
     493             :    Returns the error string */
     494           7 : static PHP_FUNCTION(bzerrstr)
     495             : {
     496           7 :         php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
     497           7 : }
     498             : /* }}} */
     499             : 
     500             : /* {{{ proto array bzerror(resource bz)
     501             :    Returns the error number and error string in an associative array */
     502           7 : static PHP_FUNCTION(bzerror)
     503             : {
     504           7 :         php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
     505           7 : }
     506             : /* }}} */
     507             : 
     508             : /* {{{ proto string bzcompress(string source [, int blocksize100k [, int workfactor]])
     509             :    Compresses a string into BZip2 encoded data */
     510           9 : static PHP_FUNCTION(bzcompress)
     511             : {
     512             :         char             *source;          /* Source data to compress */
     513           9 :         zend_long              zblock_size = 0; /* Optional block size to use */
     514           9 :         zend_long              zwork_factor = 0;/* Optional work factor to use */
     515           9 :         zend_string      *dest = NULL;     /* Destination to place the compressed data into */
     516             :         int               error,           /* Error Container */
     517           9 :                                           block_size  = 4, /* Block size for compression algorithm */
     518           9 :                                           work_factor = 0, /* Work factor for compression algorithm */
     519             :                                           argc;            /* Argument count */
     520             :         size_t               source_len;      /* Length of the source data */
     521             :         unsigned int      dest_len;        /* Length of the destination buffer */ 
     522             : 
     523           9 :         argc = ZEND_NUM_ARGS();
     524             : 
     525           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &source, &source_len, &zblock_size, &zwork_factor) == FAILURE) {
     526           1 :                 return;
     527             :         }
     528             : 
     529             :         /* Assign them to easy to use variables, dest_len is initially the length of the data
     530             :            + .01 x length of data + 600 which is the largest size the results of the compression 
     531             :            could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net 
     532             :            for pointing this out).  */
     533           8 :         dest_len = (unsigned int) (source_len + (0.01 * source_len) + 600);
     534             :         
     535             :         /* Allocate the destination buffer */
     536          16 :         dest = zend_string_alloc(dest_len, 0);
     537             :         
     538             :         /* Handle the optional arguments */
     539           8 :         if (argc > 1) {
     540           6 :                 block_size = zblock_size;
     541             :         }
     542             :         
     543           8 :         if (argc > 2) {
     544           5 :                 work_factor = zwork_factor;
     545             :         }
     546             : 
     547           8 :         error = BZ2_bzBuffToBuffCompress(dest->val, &dest_len, source, source_len, block_size, 0, work_factor);
     548           8 :         if (error != BZ_OK) {
     549             :                 zend_string_free(dest);
     550           4 :                 RETURN_LONG(error);
     551             :         } else {
     552             :                 /* Copy the buffer, we have perhaps allocate a lot more than we need,
     553             :                    so we erealloc() the buffer to the proper size */
     554           4 :                 dest->len = dest_len;
     555           4 :                 dest->val[dest->len] = '\0';
     556           4 :                 RETURN_STR(dest);
     557             :         }
     558             : }
     559             : /* }}} */
     560             : 
     561             : /* {{{ proto string bzdecompress(string source [, int small])
     562             :    Decompresses BZip2 compressed data */
     563          11 : static PHP_FUNCTION(bzdecompress)
     564             : {
     565             :         char *source, *dest;
     566             :         size_t source_len;
     567             :         int error;
     568          11 :         zend_long small = 0;
     569             : #if defined(PHP_WIN32)
     570             :         unsigned __int64 size = 0;
     571             : #else
     572          11 :         unsigned long long size = 0;
     573             : #endif
     574             :         bz_stream bzs;
     575             : 
     576          11 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &source, &source_len, &small)) {
     577           2 :                 RETURN_FALSE;
     578             :         }
     579             : 
     580           9 :         bzs.bzalloc = NULL;
     581           9 :         bzs.bzfree = NULL;
     582             : 
     583           9 :         if (BZ2_bzDecompressInit(&bzs, 0, (int)small) != BZ_OK) {
     584           2 :                 RETURN_FALSE;
     585             :         }
     586             : 
     587           7 :         bzs.next_in = source;
     588           7 :         bzs.avail_in = source_len;
     589             : 
     590             :         /* in most cases bz2 offers at least 2:1 compression, so we use that as our base */
     591           7 :         bzs.avail_out = source_len * 2;
     592           7 :         bzs.next_out = dest = emalloc(bzs.avail_out + 1);
     593             :         
     594          14 :         while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) {
     595             :                 /* compression is better then 2:1, need to allocate more memory */
     596           0 :                 bzs.avail_out = source_len;
     597           0 :                 size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
     598           0 :                 dest = safe_erealloc(dest, 1, bzs.avail_out+1, (size_t) size );
     599           0 :                 bzs.next_out = dest + size;
     600             :         }
     601             : 
     602          11 :         if (error == BZ_STREAM_END || error == BZ_OK) {
     603           4 :                 size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
     604           4 :                 dest = safe_erealloc(dest, 1, (size_t) size, 1);
     605           4 :                 dest[size] = '\0';
     606           8 :                 RETVAL_STRINGL(dest, (int) size);
     607           4 :                 efree(dest);
     608             :         } else { /* real error */
     609           3 :                 efree(dest);
     610           3 :                 RETVAL_LONG(error);
     611             :         }
     612             : 
     613           7 :         BZ2_bzDecompressEnd(&bzs);
     614             : }
     615             : /* }}} */
     616             : 
     617             : /* {{{ php_bz2_error()
     618             :    The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */
     619          21 : static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
     620             : { 
     621             :         zval         *bzp;     /* BZip2 Resource Pointer */
     622             :         php_stream   *stream;
     623             :         const char   *errstr;  /* Error string */
     624             :         int           errnum;  /* Error number */
     625             :         struct php_bz2_stream_data_t *self;
     626             :         
     627          21 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &bzp) == FAILURE) {
     628           0 :                 return;
     629             :         }
     630             : 
     631          21 :         php_stream_from_zval(stream, bzp);
     632             : 
     633          18 :         if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
     634           0 :                 RETURN_FALSE;
     635             :         }
     636             : 
     637          18 :         self = (struct php_bz2_stream_data_t *) stream->abstract;
     638             :         
     639             :         /* Fetch the error information */
     640          18 :         errstr = BZ2_bzerror(self->bz_file, &errnum);
     641             :         
     642             :         /* Determine what to return */
     643          18 :         switch (opt) {
     644             :                 case PHP_BZ_ERRNO:
     645           6 :                         RETURN_LONG(errnum);
     646             :                         break;
     647             :                 case PHP_BZ_ERRSTR:
     648          12 :                         RETURN_STRING((char*)errstr);
     649             :                         break;
     650             :                 case PHP_BZ_ERRBOTH:
     651           6 :                         array_init(return_value);
     652             :                 
     653           6 :                         add_assoc_long  (return_value, "errno",  errnum);
     654           6 :                         add_assoc_string(return_value, "errstr", (char*)errstr);
     655             :                         break;
     656             :         }
     657             : }
     658             : /* }}} */
     659             : 
     660             : #endif
     661             : 
     662             : /*
     663             :  * Local variables:
     664             :  * tab-width: 4
     665             :  * c-basic-offset: 4
     666             :  * End:
     667             :  * vim600: fdm=marker
     668             :  * vim: noet sw=4 ts=4
     669             :  */

Generated by: LCOV version 1.10

Generated at Sat, 22 Nov 2014 23:01:13 +0000 (3 days ago)

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