Back to index

php5  5.3.10
bz2.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   | Author: Sterling Hughes <sterling@php.net>                           |
00016   +----------------------------------------------------------------------+
00017 */
00018  
00019 /* $Id: bz2.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_bz2.h"
00027 
00028 #if HAVE_BZ2
00029 
00030 /* PHP Includes */
00031 #include "ext/standard/file.h"
00032 #include "ext/standard/info.h"
00033 #include "ext/standard/php_string.h"
00034 
00035 /* for fileno() */
00036 #include <stdio.h>
00037 
00038 /* Internal error constants */
00039 #define PHP_BZ_ERRNO   0
00040 #define PHP_BZ_ERRSTR  1
00041 #define PHP_BZ_ERRBOTH 2
00042 
00043 static PHP_MINIT_FUNCTION(bz2);
00044 static PHP_MSHUTDOWN_FUNCTION(bz2);
00045 static PHP_MINFO_FUNCTION(bz2);
00046 static PHP_FUNCTION(bzopen);
00047 static PHP_FUNCTION(bzread);
00048 static PHP_FUNCTION(bzerrno);
00049 static PHP_FUNCTION(bzerrstr);
00050 static PHP_FUNCTION(bzerror);
00051 static PHP_FUNCTION(bzcompress);
00052 static PHP_FUNCTION(bzdecompress);
00053 
00054 /* {{{ arginfo */
00055 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzread, 0, 0, 1)
00056        ZEND_ARG_INFO(0, bz)
00057        ZEND_ARG_INFO(0, length)
00058 ZEND_END_ARG_INFO()
00059 
00060 ZEND_BEGIN_ARG_INFO(arginfo_bzopen, 0)
00061        ZEND_ARG_INFO(0, file)
00062        ZEND_ARG_INFO(0, mode)
00063 ZEND_END_ARG_INFO()
00064 
00065 ZEND_BEGIN_ARG_INFO(arginfo_bzerrno, 0)
00066        ZEND_ARG_INFO(0, bz)
00067 ZEND_END_ARG_INFO()
00068 
00069 ZEND_BEGIN_ARG_INFO(arginfo_bzerrstr, 0)
00070        ZEND_ARG_INFO(0, bz)
00071 ZEND_END_ARG_INFO()
00072 
00073 ZEND_BEGIN_ARG_INFO(arginfo_bzerror, 0)
00074        ZEND_ARG_INFO(0, bz)
00075 ZEND_END_ARG_INFO()
00076 
00077 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzcompress, 0, 0, 2)
00078        ZEND_ARG_INFO(0, source)
00079        ZEND_ARG_INFO(0, blocksize)
00080        ZEND_ARG_INFO(0, workfactor)
00081 ZEND_END_ARG_INFO()
00082 
00083 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzdecompress, 0, 0, 1)
00084        ZEND_ARG_INFO(0, source)
00085        ZEND_ARG_INFO(0, small)
00086 ZEND_END_ARG_INFO()
00087 
00088 ZEND_BEGIN_ARG_INFO_EX(arginfo_bzwrite, 0, 0, 2)
00089        ZEND_ARG_INFO(0, fp)
00090        ZEND_ARG_INFO(0, str)
00091        ZEND_ARG_INFO(0, length)
00092 ZEND_END_ARG_INFO()
00093 
00094 ZEND_BEGIN_ARG_INFO(arginfo_bzflush, 0)
00095        ZEND_ARG_INFO(0, fp)
00096 ZEND_END_ARG_INFO()
00097 /* }}} */
00098 
00099 static const zend_function_entry bz2_functions[] = {
00100        PHP_FE(bzopen,       arginfo_bzopen)
00101        PHP_FE(bzread,       arginfo_bzread)
00102        PHP_FALIAS(bzwrite,   fwrite,             arginfo_bzwrite)
00103        PHP_FALIAS(bzflush,   fflush,             arginfo_bzflush)
00104        PHP_FALIAS(bzclose,   fclose,             arginfo_bzflush)
00105        PHP_FE(bzerrno,      arginfo_bzerrno)
00106        PHP_FE(bzerrstr,     arginfo_bzerrstr)
00107        PHP_FE(bzerror,      arginfo_bzerror)
00108        PHP_FE(bzcompress,   arginfo_bzcompress)
00109        PHP_FE(bzdecompress, arginfo_bzdecompress)
00110        PHP_FE_END
00111 };
00112 
00113 zend_module_entry bz2_module_entry = {
00114        STANDARD_MODULE_HEADER,
00115        "bz2",
00116        bz2_functions,
00117        PHP_MINIT(bz2),
00118        PHP_MSHUTDOWN(bz2),
00119        NULL,
00120        NULL,
00121        PHP_MINFO(bz2),
00122        NO_VERSION_YET,
00123        STANDARD_MODULE_PROPERTIES
00124 };
00125 
00126 #ifdef COMPILE_DL_BZ2
00127 ZEND_GET_MODULE(bz2)
00128 #endif
00129 
00130 struct php_bz2_stream_data_t {
00131        BZFILE *bz_file;
00132        php_stream *stream;
00133 };
00134 
00135 /* {{{ BZip2 stream implementation */
00136 
00137 static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
00138 {
00139        struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract;
00140        size_t ret;
00141        
00142        ret = BZ2_bzread(self->bz_file, buf, count);
00143 
00144        if (ret == 0) {
00145               stream->eof = 1;
00146        }
00147 
00148        return ret;
00149 }
00150 
00151 static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
00152 {
00153        struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *) stream->abstract;
00154 
00155        return BZ2_bzwrite(self->bz_file, (char*)buf, count); 
00156 }
00157 
00158 static int php_bz2iop_close(php_stream *stream, int close_handle TSRMLS_DC)
00159 {
00160        struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
00161        int ret = EOF;
00162        
00163        if (close_handle) {
00164               BZ2_bzclose(self->bz_file);
00165        }
00166 
00167        if (self->stream) {
00168               php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
00169        }
00170 
00171        efree(self);
00172 
00173        return ret;
00174 }
00175 
00176 static int php_bz2iop_flush(php_stream *stream TSRMLS_DC)
00177 {
00178        struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract;
00179        return BZ2_bzflush(self->bz_file);
00180 }
00181 /* }}} */
00182 
00183 php_stream_ops php_stream_bz2io_ops = {
00184        php_bz2iop_write, php_bz2iop_read,
00185        php_bz2iop_close, php_bz2iop_flush,
00186        "BZip2",
00187        NULL, /* seek */
00188        NULL, /* cast */
00189        NULL, /* stat */
00190        NULL  /* set_option */
00191 };
00192 
00193 /* {{{ Bzip2 stream openers */
00194 PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, 
00195                                                                                                   char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC)
00196 {
00197        struct php_bz2_stream_data_t *self;
00198        
00199        self = emalloc(sizeof(*self));
00200 
00201        self->stream = innerstream;
00202        self->bz_file = bz;
00203 
00204        return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode);
00205 }
00206 
00207 PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
00208                                                                              char *path,
00209                                                                              char *mode,
00210                                                                              int options,
00211                                                                              char **opened_path,
00212                                                                              php_stream_context *context STREAMS_DC TSRMLS_DC)
00213 {
00214        php_stream *retstream = NULL, *stream = NULL;
00215        char *path_copy = NULL;
00216        BZFILE *bz_file = NULL;
00217 
00218        if (strncasecmp("compress.bzip2://", path, 17) == 0) {
00219               path += 17;
00220        }
00221        if (mode[0] == '\0' || (mode[0] != 'w' && mode[0] != 'r' && mode[1] != '\0')) {
00222               return NULL;
00223        }
00224 
00225 #ifdef VIRTUAL_DIR
00226        virtual_filepath_ex(path, &path_copy, NULL TSRMLS_CC);
00227 #else
00228        path_copy = path;
00229 #endif  
00230 
00231        if ((PG(safe_mode) && (!php_checkuid(path_copy, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(path_copy TSRMLS_CC)) {
00232               return NULL;
00233        }
00234        
00235        /* try and open it directly first */
00236        bz_file = BZ2_bzopen(path_copy, mode);
00237 
00238        if (opened_path && bz_file) {
00239               *opened_path = estrdup(path_copy);
00240        }
00241        path_copy = NULL;
00242        
00243        if (bz_file == NULL) {
00244               /* that didn't work, so try and get something from the network/wrapper */
00245               stream = php_stream_open_wrapper(path, mode, options | STREAM_WILL_CAST | ENFORCE_SAFE_MODE, opened_path);
00246        
00247               if (stream) {
00248                      int fd;
00249                      if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
00250                             bz_file = BZ2_bzdopen(fd, mode);
00251                      }
00252               }
00253 
00254               /* remove the file created by php_stream_open_wrapper(), it is not needed since BZ2 functions
00255                * failed.
00256                */
00257               if (opened_path && !bz_file && mode[0] == 'w') {
00258                      VCWD_UNLINK(*opened_path);
00259               }
00260        }
00261        
00262        if (bz_file) {
00263               retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC TSRMLS_CC);
00264               if (retstream) {
00265                      return retstream;
00266               }
00267 
00268               BZ2_bzclose(bz_file);
00269        }
00270 
00271        if (stream) {
00272               php_stream_close(stream);
00273        }
00274 
00275        return NULL;
00276 }
00277 
00278 /* }}} */
00279 
00280 static php_stream_wrapper_ops bzip2_stream_wops = {
00281        _php_stream_bz2open,
00282        NULL, /* close */
00283        NULL, /* fstat */
00284        NULL, /* stat */
00285        NULL, /* opendir */
00286        "BZip2",
00287        NULL, /* unlink */
00288        NULL, /* rename */
00289        NULL, /* mkdir */
00290        NULL  /* rmdir */
00291 };
00292 
00293 static php_stream_wrapper php_stream_bzip2_wrapper = {
00294        &bzip2_stream_wops,
00295        NULL,
00296        0 /* is_url */
00297 };
00298 
00299 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int);
00300 
00301 static PHP_MINIT_FUNCTION(bz2)
00302 {
00303        php_register_url_stream_wrapper("compress.bzip2", &php_stream_bzip2_wrapper TSRMLS_CC);
00304        php_stream_filter_register_factory("bzip2.*", &php_bz2_filter_factory TSRMLS_CC);
00305        return SUCCESS;
00306 }
00307 
00308 static PHP_MSHUTDOWN_FUNCTION(bz2)
00309 {
00310        php_unregister_url_stream_wrapper("compress.bzip2" TSRMLS_CC);
00311        php_stream_filter_unregister_factory("bzip2.*" TSRMLS_CC);
00312 
00313        return SUCCESS;
00314 }
00315 
00316 static PHP_MINFO_FUNCTION(bz2)
00317 {
00318        php_info_print_table_start();
00319        php_info_print_table_row(2, "BZip2 Support", "Enabled");
00320        php_info_print_table_row(2, "Stream Wrapper support", "compress.bzip2://");
00321        php_info_print_table_row(2, "Stream Filter support", "bzip2.decompress, bzip2.compress");
00322        php_info_print_table_row(2, "BZip2 Version", (char *) BZ2_bzlibVersion());
00323        php_info_print_table_end();
00324 }
00325 
00326 /* {{{ proto string bzread(resource bz[, int length])
00327    Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */
00328 static PHP_FUNCTION(bzread)
00329 {
00330        zval *bz;
00331        long len = 1024;
00332        php_stream *stream;
00333 
00334        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &bz, &len)) {
00335               RETURN_FALSE;
00336        }
00337        
00338        php_stream_from_zval(stream, &bz);
00339 
00340        if ((len + 1) < 1) {
00341               php_error_docref(NULL TSRMLS_CC, E_WARNING, "length may not be negative");
00342               RETURN_FALSE;
00343        }
00344 
00345        Z_STRVAL_P(return_value) = emalloc(len + 1);
00346        Z_STRLEN_P(return_value) = php_stream_read(stream, Z_STRVAL_P(return_value), len);
00347        
00348        if (Z_STRLEN_P(return_value) < 0) {
00349               efree(Z_STRVAL_P(return_value));
00350               php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not read valid bz2 data from stream");
00351               RETURN_FALSE;        
00352        }
00353        
00354        Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
00355 
00356        if (PG(magic_quotes_runtime)) {
00357               Z_STRVAL_P(return_value) = php_addslashes(       Z_STRVAL_P(return_value),
00358                                                                                            Z_STRLEN_P(return_value),
00359                                                                                            &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
00360        }
00361 
00362        Z_TYPE_P(return_value) = IS_STRING;
00363 }
00364 /* }}} */
00365 
00366 /* {{{ proto resource bzopen(string|int file|fp, string mode)
00367    Opens a new BZip2 stream */
00368 static PHP_FUNCTION(bzopen)
00369 {
00370        zval    **file;   /* The file to open */
00371        char     *mode;   /* The mode to open the stream with */
00372        int      mode_len;
00373 
00374        BZFILE   *bz;     /* The compressed file stream */
00375        php_stream *stream = NULL;
00376        
00377        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &file, &mode, &mode_len) == FAILURE) {
00378               return;
00379        }
00380 
00381        if (mode_len != 1 || (mode[0] != 'r' && mode[0] != 'w')) {
00382               php_error_docref(NULL TSRMLS_CC, E_WARNING, "'%s' is not a valid mode for bzopen(). Only 'w' and 'r' are supported.", mode);
00383               RETURN_FALSE;
00384        }
00385 
00386        /* If it's not a resource its a string containing the filename to open */
00387        if (Z_TYPE_PP(file) == IS_STRING) {
00388               convert_to_string_ex(file);
00389 
00390               if (strlen(Z_STRVAL_PP(file)) != Z_STRLEN_PP(file)) {
00391                      RETURN_FALSE;
00392               }
00393               if (Z_STRLEN_PP(file) == 0) {
00394                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "filename cannot be empty");
00395                      RETURN_FALSE;
00396               }
00397 
00398               stream = php_stream_bz2open(NULL,
00399                                                                Z_STRVAL_PP(file), 
00400                                                                mode, 
00401                                                                ENFORCE_SAFE_MODE | REPORT_ERRORS, 
00402                                                                NULL);
00403        } else if (Z_TYPE_PP(file) == IS_RESOURCE) {
00404               /* If it is a resource, than its a stream resource */
00405               int fd;
00406               int stream_mode_len;
00407 
00408               php_stream_from_zval(stream, file);
00409               stream_mode_len = strlen(stream->mode);
00410               
00411               if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr(stream->mode, 'b', 2))) {
00412                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
00413                      RETURN_FALSE;
00414               } else if (stream_mode_len == 1 && stream->mode[0] != 'r' && stream->mode[0] != 'w' && stream->mode[0] != 'a' && stream->mode[0] != 'x') {
00415                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot use stream opened in mode '%s'", stream->mode);
00416                      RETURN_FALSE;
00417               }
00418 
00419               switch(mode[0]) {
00420                      case 'r':
00421                             /* only "r" and "rb" are supported */
00422                             if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])) {
00423                                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot read from a stream opened in write only mode");
00424                                    RETURN_FALSE;
00425                             }
00426                             break;
00427                      case 'w':
00428                             /* support only "w"(b), "a"(b), "x"(b) */
00429                             if (stream->mode[0] != mode[0] && !(stream_mode_len == 2 && stream->mode[1] != mode[0])
00430                                    && stream->mode[0] != 'a' && !(stream_mode_len == 2 && stream->mode[1] != 'a')
00431                                    && stream->mode[0] != 'x' && !(stream_mode_len == 2 && stream->mode[1] != 'x')) {
00432                                    php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot write to a stream opened in read only mode");
00433                                    RETURN_FALSE;
00434                             }
00435                             break;
00436                      default:
00437                             /* not reachable */
00438                             break;
00439               }
00440 
00441               if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void *) &fd, REPORT_ERRORS)) {
00442                      RETURN_FALSE;
00443               }
00444               
00445               bz = BZ2_bzdopen(fd, mode);
00446 
00447               stream = php_stream_bz2open_from_BZFILE(bz, mode, stream);
00448        } else {
00449               php_error_docref(NULL TSRMLS_CC, E_WARNING, "first parameter has to be string or file-resource");
00450               RETURN_FALSE;
00451        }
00452 
00453        if (stream) {
00454               php_stream_to_zval(stream, return_value);
00455        } else {
00456               RETURN_FALSE;
00457        }
00458 }
00459 /* }}} */
00460 
00461 /* {{{ proto int bzerrno(resource bz)
00462    Returns the error number */
00463 static PHP_FUNCTION(bzerrno)
00464 {
00465        php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO);
00466 }
00467 /* }}} */
00468 
00469 /* {{{ proto string bzerrstr(resource bz)
00470    Returns the error string */
00471 static PHP_FUNCTION(bzerrstr)
00472 {
00473        php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR);
00474 }
00475 /* }}} */
00476 
00477 /* {{{ proto array bzerror(resource bz)
00478    Returns the error number and error string in an associative array */
00479 static PHP_FUNCTION(bzerror)
00480 {
00481        php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH);
00482 }
00483 /* }}} */
00484 
00485 /* {{{ proto string bzcompress(string source [, int blocksize100k [, int workfactor]])
00486    Compresses a string into BZip2 encoded data */
00487 static PHP_FUNCTION(bzcompress)
00488 {
00489        char             *source;          /* Source data to compress */
00490        long              zblock_size = 0; /* Optional block size to use */
00491        long              zwork_factor = 0;/* Optional work factor to use */
00492        char             *dest = NULL;     /* Destination to place the compressed data into */
00493        int               error,           /* Error Container */
00494                                      block_size  = 4, /* Block size for compression algorithm */
00495                                      work_factor = 0, /* Work factor for compression algorithm */
00496                                      argc;            /* Argument count */
00497        int               source_len;      /* Length of the source data */
00498        unsigned int      dest_len;        /* Length of the destination buffer */ 
00499 
00500        argc = ZEND_NUM_ARGS();
00501 
00502        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &source, &source_len, &zblock_size, &zwork_factor) == FAILURE) {
00503               return;
00504        }
00505 
00506        /* Assign them to easy to use variables, dest_len is initially the length of the data
00507           + .01 x length of data + 600 which is the largest size the results of the compression 
00508           could possibly be, at least that's what the libbz2 docs say (thanks to jeremy@nirvani.net 
00509           for pointing this out).  */
00510        dest_len   = (unsigned int) (source_len + (0.01 * source_len) + 600);
00511        
00512        /* Allocate the destination buffer */
00513        dest = emalloc(dest_len + 1);
00514        
00515        /* Handle the optional arguments */
00516        if (argc > 1) {
00517               block_size = zblock_size;
00518        }
00519        
00520        if (argc > 2) {
00521               work_factor = zwork_factor;
00522        }
00523 
00524        error = BZ2_bzBuffToBuffCompress(dest, &dest_len, source, source_len, block_size, 0, work_factor);
00525        if (error != BZ_OK) {
00526               efree(dest);
00527               RETURN_LONG(error);
00528        } else {
00529               /* Copy the buffer, we have perhaps allocate alot more than we need,
00530                  so we erealloc() the buffer to the proper size */
00531               dest = erealloc(dest, dest_len + 1);
00532               dest[dest_len] = 0;
00533               RETURN_STRINGL(dest, dest_len, 0);
00534        }
00535 }
00536 /* }}} */
00537 
00538 /* {{{ proto string bzdecompress(string source [, int small])
00539    Decompresses BZip2 compressed data */
00540 static PHP_FUNCTION(bzdecompress)
00541 {
00542        char *source, *dest;
00543        int source_len, error;
00544        long small = 0;
00545 #if defined(PHP_WIN32)
00546        unsigned __int64 size = 0;
00547 #else
00548        unsigned long long size = 0;
00549 #endif
00550        bz_stream bzs;
00551 
00552        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &source, &source_len, &small)) {
00553               RETURN_FALSE;
00554        }
00555 
00556        bzs.bzalloc = NULL;
00557        bzs.bzfree = NULL;
00558 
00559        if (BZ2_bzDecompressInit(&bzs, 0, small) != BZ_OK) {
00560               RETURN_FALSE;
00561        }
00562 
00563        bzs.next_in = source;
00564        bzs.avail_in = source_len;
00565 
00566        /* in most cases bz2 offers at least 2:1 compression, so we use that as our base */
00567        bzs.avail_out = source_len * 2;
00568        bzs.next_out = dest = emalloc(bzs.avail_out + 1);
00569        
00570        while ((error = BZ2_bzDecompress(&bzs)) == BZ_OK && bzs.avail_in > 0) {
00571               /* compression is better then 2:1, need to allocate more memory */
00572               bzs.avail_out = source_len;
00573               size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
00574               dest = safe_erealloc(dest, 1, bzs.avail_out+1, (size_t) size );
00575               bzs.next_out = dest + size;
00576        }
00577 
00578        if (error == BZ_STREAM_END || error == BZ_OK) {
00579               size = (bzs.total_out_hi32 * (unsigned int) -1) + bzs.total_out_lo32;
00580               dest = safe_erealloc(dest, 1, (size_t) size, 1);
00581               dest[size] = '\0';
00582               RETVAL_STRINGL(dest, (int) size, 0);
00583        } else { /* real error */
00584               efree(dest);
00585               RETVAL_LONG(error);
00586        }
00587 
00588        BZ2_bzDecompressEnd(&bzs);
00589 }
00590 /* }}} */
00591 
00592 /* {{{ php_bz2_error()
00593    The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */
00594 static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt)
00595 { 
00596        zval         *bzp;     /* BZip2 Resource Pointer */
00597        php_stream   *stream;
00598        const char   *errstr;  /* Error string */
00599        int           errnum;  /* Error number */
00600        struct php_bz2_stream_data_t *self;
00601        
00602        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &bzp) == FAILURE) {
00603               return;
00604        }
00605 
00606        php_stream_from_zval(stream, &bzp);
00607 
00608        if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) {
00609               RETURN_FALSE;
00610        }
00611 
00612        self = (struct php_bz2_stream_data_t *) stream->abstract;
00613        
00614        /* Fetch the error information */
00615        errstr = BZ2_bzerror(self->bz_file, &errnum);
00616        
00617        /* Determine what to return */
00618        switch (opt) {
00619               case PHP_BZ_ERRNO:
00620                      RETURN_LONG(errnum);
00621                      break;
00622               case PHP_BZ_ERRSTR:
00623                      RETURN_STRING((char*)errstr, 1);
00624                      break;
00625               case PHP_BZ_ERRBOTH:
00626                      array_init(return_value);
00627               
00628                      add_assoc_long  (return_value, "errno",  errnum);
00629                      add_assoc_string(return_value, "errstr", (char*)errstr, 1);
00630                      break;
00631        }
00632 }
00633 /* }}} */
00634 
00635 #endif
00636 
00637 /*
00638  * Local variables:
00639  * tab-width: 4
00640  * c-basic-offset: 4
00641  * End:
00642  * vim600: fdm=marker
00643  * vim: noet sw=4 ts=4
00644  */