File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / bz2 / bz2.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Sun Jun 15 20:03:41 2014 UTC (10 years, 1 month ago) by misho
Branches: php, MAIN
CVS tags: v5_4_29, HEAD
php 5.4.29

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>