cvs: php4 /ext/bz2 bz2.c

From: Date: Sun, 27 Jan 2002 05:47:11 +0000
Subject: cvs: php4 /ext/bz2 bz2.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-9188@lists.php.net to get a copy of this message
sterling Sun Jan 27 00:47:11 2002 EDT Modified files: /php4/ext/bz2 bz2.c Log: Source code cleanup Index: php4/ext/bz2/bz2.c diff -u php4/ext/bz2/bz2.c:1.32 php4/ext/bz2/bz2.c:1.33 --- php4/ext/bz2/bz2.c:1.32 Sun Jan 27 00:32:12 2002 +++ php4/ext/bz2/bz2.c Sun Jan 27 00:47:11 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: bz2.c,v 1.32 2002/01/27 05:32:12 sterling Exp $ */ +/* $Id: bz2.c,v 1.33 2002/01/27 05:47:11 sterling Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -170,10 +170,7 @@ buf = emalloc(len + 1); BZ2_bzread(bz, buf, len); - RETVAL_STRINGL(buf, len, 1); - - /* We copied the buffer, so now we can free it */ - efree(buf); + RETURN_STRINGL(buf, len, 0); } /* }}} */ @@ -276,18 +273,20 @@ Compresses a string into BZip2 encoded data */ PHP_FUNCTION(bzcompress) { - zval **source, /* Source data to compress */ - **zblock_size, /* Optional block size to use */ - **zwork_factor; /* Optional work factor to use */ - char *dest = NULL; /* Destination to place the compressed data into */ - int error, /* Error Container */ - block_size = 4, /* Block size for compression algorithm */ - work_factor = 0, /* Work factor for compression algorithm */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - unsigned int size, /* The size to "realloc" if the initial buffer wasn't big enough */ - source_len, /* Length of the source data */ - dest_len; /* Length of the destination buffer */ + zval **source, /* Source data to compress */ + **zblock_size, /* Optional block size to use */ + **zwork_factor; /* Optional work factor to use */ + char *dest = NULL; /* Destination to place the compressed data into */ + int error, /* Error Container */ + block_size = 4, /* Block size for compression algorithm */ + work_factor = 0, /* Work factor for compression algorithm */ + argc; /* Argument count */ + unsigned int size, /* The size to "realloc" to */ + source_len, /* Length of the source data */ + dest_len; /* Length of the destination buffer */ + argc = ZEND_NUM_ARGS(); + if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &source, &zblock_size, &zwork_factor) == FAILURE) { WRONG_PARAM_COUNT; @@ -315,18 +314,21 @@ work_factor = Z_LVAL_PP(zwork_factor); } - error = BZ2_bzBuffToBuffCompress(dest, &size, Z_STRVAL_PP(source), source_len, block_size, 0, work_factor); + error = BZ2_bzBuffToBuffCompress(dest, + &size, + Z_STRVAL_PP(source), + source_len, + block_size, + 0, + work_factor); if (error != BZ_OK) { - RETVAL_LONG(error); + RETURN_LONG(error); } else { /* Copy the buffer, we have perhaps allocate alot more than we need, - so we want to copy the correct amount and then free the in-exactly - allocated buffer */ - RETVAL_STRINGL(dest, size, 1); + so we erealloc() the buffer to the proper size */ + dest = erealloc(dest, size); + RETURN_STRINGL(dest, size, 0); } - - /* Free the buffer */ - efree(dest); } /* }}} */ @@ -334,15 +336,15 @@ Decompresses BZip2 compressed data */ PHP_FUNCTION(bzdecompress) { - zval **source, /* Source data to decompress */ - **zsmall; /* (Optional) user specified small */ - char *dest = emalloc(PHP_BZ_DECOMPRESS_SIZE); /* Destination buffer, initially allocated */ - int error, /* Error container */ - iter = 1, /* Iteration count for the compression loop */ - size, /* Current size to realloc the dest buffer to */ - dest_len = PHP_BZ_DECOMPRESS_SIZE, /* Size of the destination length */ - small = 0, /* The actual small */ - argc = ZEND_NUM_ARGS(); /* Argument count */ + zval **source, /* Source data to decompress */ + **zsmall; /* (Optional) user specified small */ + char *dest; /* Destination buffer, initially allocated */ + int error, /* Error container */ + iter = 1, /* Iteration count for the compression loop */ + size, /* Current size to realloc the dest buffer to */ + dest_len = PHP_BZ_DECOMPRESS_SIZE, /* Size of the destination length */ + small = 0, /* The actual small */ + argc = ZEND_NUM_ARGS(); /* Argument count */ if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &source, &zsmall) == FAILURE) { @@ -355,7 +357,17 @@ convert_to_long_ex(zsmall); small = Z_LVAL_PP(zsmall); } + + /* Depending on the size of the source buffer, either allocate + the length of the source buffer or the a default decompression + size */ + dest = emalloc( + PHP_BZ_DECOMPRESS_SIZE > Z_STRLEN_PP(source) ? + PHP_BZ_DECOMPRESS_SIZE : + Z_STRLEN_PP(source) + ); + /* (de)Compression Loop */ do { /* Handle the (re)allocation of the buffer */ @@ -366,18 +378,22 @@ ++iter; /* Perform the decompression */ - error = BZ2_bzBuffToBuffDecompress(dest, &size, Z_STRVAL_PP(source), Z_STRLEN_PP(source), small, 0); + error = BZ2_bzBuffToBuffDecompress(dest, + &size, + Z_STRVAL_PP(source), + Z_STRLEN_PP(source), + small, + 0); } while (error == BZ_OUTBUFF_FULL); if (error != BZ_OK) { - RETVAL_LONG(error); + RETURN_LONG(error); } else { - /* we might have allocated a little to much, so copy the exact size and free the - in-exactly allocated buffer */ - RETVAL_STRINGL(dest, size, 1); + /* we might have allocated a little to much, so erealloc the buffer + down to size, before returning it */ + dest = erealloc(dest, size); + RETURN_STRINGL(dest, size, 0); } - - efree(dest); } /* }}} */

« previous php.cvs (#9188) next »