cvs: php4 /ext/bz2 bz2.c php_bz2.h /ext/zlib zlib_fopen_wrapper.c
| From: | Wez Furlong | Date: | Thu, 21 Mar 2002 02:27:42 +0000 |
| Subject: | cvs: php4 /ext/bz2 bz2.c php_bz2.h /ext/zlib zlib_fopen_wrapper.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-10507@lists.php.net to get a copy of this message | ||
wez Wed Mar 20 21:27:42 2002 EDT
Modified files:
/php4/ext/bz2 bz2.c php_bz2.h
/php4/ext/zlib zlib_fopen_wrapper.c
Log:
Implement bz2 streams and bz2: wrapper.
You can now do this:
copy("zlib:src.gz", "bz2:dest.bz2");
As with zlib, most of the functions with counterparts
in file.c are now aliases to those functions.
Index: php4/ext/bz2/bz2.c diff -u php4/ext/bz2/bz2.c:1.37 php4/ext/bz2/bz2.c:1.38 --- php4/ext/bz2/bz2.c:1.37 Fri Mar 15 16:02:55 2002 +++ php4/ext/bz2/bz2.c Wed Mar 20 21:27:41 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: bz2.c,v 1.37 2002/03/15 21:02:55 wez Exp $ */ +/* $Id: bz2.c,v 1.38 2002/03/21 02:27:41 wez Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -34,9 +34,6 @@ /* for fileno() */ #include <stdio.h> -/* Bzip2 includes */ -#include <bzlib.h> - /* Internal error constants */ #define PHP_BZ_ERRNO 0 #define PHP_BZ_ERRSTR 1 @@ -47,10 +44,10 @@ function_entry bz2_functions[] = { PHP_FE(bzopen, NULL) - PHP_FE(bzread, NULL) - PHP_FE(bzwrite, NULL) - PHP_FE(bzflush, NULL) - PHP_FE(bzclose, NULL) + PHP_NAMED_FE(bzread, PHP_FN(fread), NULL) + PHP_NAMED_FE(bzwrite, PHP_FN(fwrite), NULL) + PHP_NAMED_FE(bzflush, PHP_FN(fflush), NULL) + PHP_NAMED_FE(bzclose, PHP_FN(fclose), NULL) PHP_FE(bzerrno, NULL) PHP_FE(bzerrstr, NULL) PHP_FE(bzerror, NULL) @@ -64,7 +61,7 @@ "bz2", bz2_functions, PHP_MINIT(bz2), - NULL, + PHP_MSHUTDOWN(bz2), NULL, NULL, PHP_MINFO(bz2), @@ -76,17 +73,138 @@ ZEND_GET_MODULE(bz2) #endif -static int le_bz2; +struct php_bz2_stream_data_t { + BZFILE *bz_file; + php_stream *stream; +}; + +static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + + if (buf == NULL && count == 0) { + /* we have no way of knowing, so let them try to read some more; + * then they will get an error and can stop */ + return 0; + } + + return BZ2_bzread(self->bz_file, buf, count); +} + +static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + return BZ2_bzwrite(self->bz_file, (char*)buf, count); +} + +static int php_bz2iop_close(php_stream *stream, int close_handle TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + int ret = EOF; + + if (close_handle) + BZ2_bzclose(self->bz_file); + if (self->stream) + php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0)); + efree(self); + + return ret; +} + +static int php_bz2iop_flush(php_stream *stream TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + return BZ2_bzflush(self->bz_file); +} + +php_stream_ops php_stream_bz2io_ops = { + php_bz2iop_write, php_bz2iop_read, + php_bz2iop_close, php_bz2iop_flush, + NULL, NULL, + NULL, "BZip2" +}; + +PHPAPI php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self; + + self = emalloc(sizeof(*self)); + + self->stream = innerstream; + self->bz_file = bz; + + return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode); +} + +PHPAPI php_stream *_php_stream_bz2open(char *path, char *mode, int options, char **opened_path, void *wrappercontext STREAMS_DC TSRMLS_DC) +{ + php_stream *retstream = NULL, *stream = NULL; + char *path_copy; + BZFILE *bz_file; + + if (strncmp("bz2:", path, 4) == 0) + path += 4; + +#ifdef VIRTUAL_DIR + virtual_filepath(path, &path_copy TSRMLS_CC); +#else + path_copy = estrdup(path); +#endif + + /* try and open it directly first */ + bz_file = BZ2_bzopen(path_copy, mode); + + if (opened_path == NULL) { + efree(path_copy); + } else if (bz_file) { + *opened_path = path_copy; + } + path_copy = NULL; + + if (bz_file == NULL) { + /* that didn't work, so try and get something from the network/wrapper */ + stream = php_stream_open_wrapper(path, mode, options, opened_path); + + if (stream) { + int fd; + if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void**)&fd, REPORT_ERRORS)) + bz_file = BZ2_bzdopen(fd, mode); + } + } + + if (bz_file) { + retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC TSRMLS_CC); + if (retstream) + return retstream; + BZ2_bzclose(bz_file); + } + if (stream) + php_stream_close(stream); + + return NULL; +} + +php_stream_wrapper php_stream_bzip2_wrapper = { + _php_stream_bz2open, + NULL, + NULL +}; -static void php_bz2_close(zend_rsrc_list_entry *rsrc TSRMLS_DC); static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int); PHP_MINIT_FUNCTION(bz2) { - /* Register the resource, with destructor (arg 1) and text description (arg 3), the - other arguments are just standard placeholders */ - le_bz2 = zend_register_list_destructors_ex(php_bz2_close, NULL, "BZip2", module_number); + if(PG(allow_url_fopen)) { + php_register_url_stream_wrapper("bz2", &php_stream_bzip2_wrapper TSRMLS_CC); + } + + return SUCCESS; +} +PHP_MSHUTDOWN_FUNCTION(bz2) +{ + if (PG(allow_url_fopen)) + php_unregister_url_stream_wrapper("bz2" TSRMLS_CC); return SUCCESS; } @@ -105,7 +223,7 @@ zval **file, /* The file to open */ **mode; /* The mode to open the stream with */ BZFILE *bz; /* The compressed file stream */ - FILE *fp; /* If filepointer is given, its placed in here */ + php_stream *stream = NULL; char *path; if (ZEND_NUM_ARGS() != 2 || @@ -117,138 +235,27 @@ /* If it's not a resource its a string containing the filename to open */ if (Z_TYPE_PP(file) != IS_RESOURCE) { convert_to_string_ex(file); - -#ifdef VIRTUAL_DIR - virtual_filepath(Z_STRVAL_PP(file), &path TSRMLS_CC); -#else - path = Z_STRVAL_PP(file); -#endif - - bz = BZ2_bzopen(path, Z_STRVAL_PP(mode)); - } - /* If it is a resource, than its a 'FILE *' resource */ - else { + stream = php_stream_bz2open(path, Z_STRVAL_PP(mode), ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL, NULL); + } else { + /* If it is a resource, than its a stream resource */ int type; int fd; - fp = (FILE*) zend_fetch_resource(file TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream()); - ZEND_VERIFY_RESOURCE(fp); - if (!php_stream_cast((php_stream*)fp, PHP_STREAM_AS_FD, (void*)&fd, REPORT_ERRORS)) + stream = (php_stream *)zend_fetch_resource(file TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream()); + ZEND_VERIFY_RESOURCE(stream); + if (!php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, REPORT_ERRORS)) RETURN_FALSE; bz = BZ2_bzdopen(fd, Z_STRVAL_PP(mode)); - } - - if (bz == NULL) { - php_error(E_WARNING, "bzopen(): Unable to open file"); - RETURN_FALSE; - } - - ZEND_REGISTER_RESOURCE(return_value, bz, le_bz2); -} -/* }}} */ - -/* {{{ proto string bzread(resource bz [, int len]) - Reads len bytes from the BZip2 stream given by bz */ -PHP_FUNCTION(bzread) -{ - zval **bzp, /* BZip2 Resource Pointer */ - **zlen; /* The (optional) length to read */ - BZFILE *bz; /* BZip2 File pointer */ - void *buf; /* Buffer to read data into */ - int len = 1024, /* Length to read, passed to the BZ2_bzread function */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - - if (argc < 1 || argc > 2 || - zend_get_parameters_ex(argc, &bzp, &zlen) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - /* Optional second argument, the length to read, if not given, it - defaults to 1024 bytes (why not? :) */ - if (argc > 1) { - convert_to_long_ex(zlen); - len = Z_LVAL_PP(zlen); - } - - /* Allocate the buffer and read data into it */ - buf = emalloc(len + 1); - BZ2_bzread(bz, buf, len); - - RETURN_STRINGL(buf, len, 0); -} -/* }}} */ - -/* {{{ proto int bzwrite(resource bz, string data [, int len]) - Writes data to the BZip2 stream given by bz */ -PHP_FUNCTION(bzwrite) -{ - zval **bzp, /* Bzip2 Resource Pointer */ - **data, /* The data to write */ - **zlen; /* The (optional) length of the data to write */ - BZFILE *bz; /* BZip2 File pointer */ - int error, /* Error container */ - len, /* Length to read, passed to the BZ2_bzwrite function */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - - if (argc < 2 || argc > 3 || - zend_get_parameters_ex(argc, &bzp, &data, &zlen) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(data); - - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - /* If the length of the data is given, use that, otherwise, just use the - data's string length */ - if (argc > 2) { - convert_to_long_ex(zlen); - len = Z_LVAL_PP(zlen); - } - else { - len = Z_STRLEN_PP(data); - } - - /* Write the data and return the error */ - error = BZ2_bzwrite(bz, (void *) Z_STRVAL_PP(data), len); - RETURN_LONG(error); -} -/* }}} */ - -/* {{{ proto int bzflush(resource bz) - Flushes a BZip2 stream */ -PHP_FUNCTION(bzflush) -{ - zval **bzp; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ - int error; /* Error container */ - if (ZEND_NUM_ARGS() != 1 || - zend_get_parameters_ex(1, &bzp) == FAILURE) { - WRONG_PARAM_COUNT; + stream = php_stream_bz2open_from_BZFILE(bz, Z_STRVAL_PP(mode), stream); } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - error = BZ2_bzflush(bz); - RETURN_LONG(error); -} -/* }}} */ -/* {{{ proto int bzclose(resource bz) - Closes a BZip2 stream */ -PHP_FUNCTION(bzclose) -{ - zval **bzp=NULL; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ - - if (ZEND_NUM_ARGS() != 1 || - zend_get_parameters_ex(1, &bzp) == FAILURE) { - WRONG_PARAM_COUNT; + if (stream) { + php_stream_to_zval(stream, return_value); + } else { + RETURN_FALSE; } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - zend_list_delete(Z_RESVAL_PP(bzp)); } /* }}} */ @@ -333,7 +340,8 @@ } else { /* Copy the buffer, we have perhaps allocate alot more than we need, so we erealloc() the buffer to the proper size */ - dest = erealloc(dest, size); + dest = erealloc(dest, size + 1); + dest[size] = '\0'; RETURN_STRINGL(dest, size, 0); } } @@ -398,39 +406,37 @@ } else { /* we might have allocated a little to much, so erealloc the buffer down to size, before returning it */ - dest = erealloc(dest, size); + dest = erealloc(dest, size + 1); + dest[size] = '\0'; RETURN_STRINGL(dest, size, 0); } } /* }}} */ -/* {{{ php_bz2_close() - Closes a BZip2 file pointer */ -static void php_bz2_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) -{ - BZFILE *bz = (BZFILE *) rsrc->ptr; /* The BZip2 File pointer */ - - BZ2_bzclose(bz); -} -/* }}} */ - /* {{{ php_bz2_error() The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */ static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt) { zval **bzp; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ + php_stream *stream; const char *errstr; /* Error string */ int errnum; /* Error number */ + struct php_bz2_stream_data_t *self; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &bzp) == FAILURE) { WRONG_PARAM_COUNT; } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File pointer", le_bz2); + ZEND_FETCH_RESOURCE(stream, php_stream *, bzp, -1, "File-Handle", php_file_le_stream()); + + if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) { + RETURN_FALSE; + } + + self = (struct php_bz2_stream_data_t *)stream->abstract; /* Fetch the error information */ - errstr = BZ2_bzerror(bz, &errnum); + errstr = BZ2_bzerror(self->bz_file, &errnum); /* Determine what to return */ switch (opt) { Index: php4/ext/bz2/php_bz2.h diff -u php4/ext/bz2/php_bz2.h:1.7 php4/ext/bz2/php_bz2.h:1.8 --- php4/ext/bz2/php_bz2.h:1.7 Thu Feb 28 03:25:43 2002 +++ php4/ext/bz2/php_bz2.h Wed Mar 20 21:27:41 2002 @@ -30,13 +30,13 @@ #define PHP_BZ2_API #endif +/* Bzip2 includes */ +#include <bzlib.h> + PHP_MINIT_FUNCTION(bz2); +PHP_MSHUTDOWN_FUNCTION(bz2); PHP_MINFO_FUNCTION(bz2); PHP_FUNCTION(bzopen); -PHP_FUNCTION(bzread); -PHP_FUNCTION(bzwrite); -PHP_FUNCTION(bzflush); -PHP_FUNCTION(bzclose); PHP_FUNCTION(bzerrno); PHP_FUNCTION(bzerrstr); PHP_FUNCTION(bzerror); @@ -46,6 +46,15 @@ #else #define phpext_bz2_ptr NULL #endif + +PHPAPI php_stream *_php_stream_bz2open(char *path, char *mode, int options, char **opened_path, void *wrappercontext STREAMS_DC TSRMLS_DC); +PHPAPI php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC); + +#define php_stream_bz2open_from_BZFILE(bz, mode, innerstream) _php_stream_bz2open_from_BZFILE((bz), (mode), (innerstream) STREAMS_CC TSRMLS_CC) +#define php_stream_bz2open(path, mode, options, opened_path, wrappercontext) _php_stream_bz2open((path), (mode), (options), (opened_path), (wrappercontext) STREAMS_CC TSRMLS_CC) + +php_stream_ops php_stream_bz2io_ops; +#define PHP_STREAM_IS_BZIP2 &php_stream_bz2io_ops #endif Index: php4/ext/zlib/zlib_fopen_wrapper.c diff -u php4/ext/zlib/zlib_fopen_wrapper.c:1.18 php4/ext/zlib/zlib_fopen_wrapper.c:1.19 --- php4/ext/zlib/zlib_fopen_wrapper.c:1.18 Wed Mar 20 20:11:52 2002 +++ php4/ext/zlib/zlib_fopen_wrapper.c Wed Mar 20 21:27:41 2002 @@ -16,7 +16,7 @@ | Hartmut Holzgraefe <hartmut@six.de> | +----------------------------------------------------------------------+ */ -/* $Id: zlib_fopen_wrapper.c,v 1.18 2002/03/21 01:11:52 wez Exp $ */ +/* $Id: zlib_fopen_wrapper.c,v 1.19 2002/03/21 02:27:41 wez Exp $ */ #define IS_EXT_MODULE #define _GNU_SOURCE @@ -78,10 +78,16 @@ return ret; } +static int php_gziop_flush(php_stream *stream TSRMLS_DC) +{ + struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract; + return gzflush(self->gz_file, Z_SYNC_FLUSH); +} + php_stream_ops php_stream_gzio_ops = { php_gziop_write, php_gziop_read, - php_gziop_close, NULL, + php_gziop_close, php_gziop_flush, php_gziop_seek, php_gziop_gets, NULL, "ZLIB" }; @@ -122,6 +128,7 @@ php_stream_wrapper php_stream_gzip_wrapper = { php_stream_gzopen, + NULL, NULL };
Index: php4/ext/bz2/bz2.c diff -u php4/ext/bz2/bz2.c:1.37 php4/ext/bz2/bz2.c:1.38 --- php4/ext/bz2/bz2.c:1.37 Fri Mar 15 16:02:55 2002 +++ php4/ext/bz2/bz2.c Wed Mar 20 21:27:41 2002 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: bz2.c,v 1.37 2002/03/15 21:02:55 wez Exp $ */ +/* $Id: bz2.c,v 1.38 2002/03/21 02:27:41 wez Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -34,9 +34,6 @@ /* for fileno() */ #include <stdio.h> -/* Bzip2 includes */ -#include <bzlib.h> - /* Internal error constants */ #define PHP_BZ_ERRNO 0 #define PHP_BZ_ERRSTR 1 @@ -47,10 +44,10 @@ function_entry bz2_functions[] = { PHP_FE(bzopen, NULL) - PHP_FE(bzread, NULL) - PHP_FE(bzwrite, NULL) - PHP_FE(bzflush, NULL) - PHP_FE(bzclose, NULL) + PHP_NAMED_FE(bzread, PHP_FN(fread), NULL) + PHP_NAMED_FE(bzwrite, PHP_FN(fwrite), NULL) + PHP_NAMED_FE(bzflush, PHP_FN(fflush), NULL) + PHP_NAMED_FE(bzclose, PHP_FN(fclose), NULL) PHP_FE(bzerrno, NULL) PHP_FE(bzerrstr, NULL) PHP_FE(bzerror, NULL) @@ -64,7 +61,7 @@ "bz2", bz2_functions, PHP_MINIT(bz2), - NULL, + PHP_MSHUTDOWN(bz2), NULL, NULL, PHP_MINFO(bz2), @@ -76,17 +73,138 @@ ZEND_GET_MODULE(bz2) #endif -static int le_bz2; +struct php_bz2_stream_data_t { + BZFILE *bz_file; + php_stream *stream; +}; + +static size_t php_bz2iop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + + if (buf == NULL && count == 0) { + /* we have no way of knowing, so let them try to read some more; + * then they will get an error and can stop */ + return 0; + } + + return BZ2_bzread(self->bz_file, buf, count); +} + +static size_t php_bz2iop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + return BZ2_bzwrite(self->bz_file, (char*)buf, count); +} + +static int php_bz2iop_close(php_stream *stream, int close_handle TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + int ret = EOF; + + if (close_handle) + BZ2_bzclose(self->bz_file); + if (self->stream) + php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0)); + efree(self); + + return ret; +} + +static int php_bz2iop_flush(php_stream *stream TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self = (struct php_bz2_stream_data_t *)stream->abstract; + return BZ2_bzflush(self->bz_file); +} + +php_stream_ops php_stream_bz2io_ops = { + php_bz2iop_write, php_bz2iop_read, + php_bz2iop_close, php_bz2iop_flush, + NULL, NULL, + NULL, "BZip2" +}; + +PHPAPI php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC) +{ + struct php_bz2_stream_data_t *self; + + self = emalloc(sizeof(*self)); + + self->stream = innerstream; + self->bz_file = bz; + + return php_stream_alloc_rel(&php_stream_bz2io_ops, self, 0, mode); +} + +PHPAPI php_stream *_php_stream_bz2open(char *path, char *mode, int options, char **opened_path, void *wrappercontext STREAMS_DC TSRMLS_DC) +{ + php_stream *retstream = NULL, *stream = NULL; + char *path_copy; + BZFILE *bz_file; + + if (strncmp("bz2:", path, 4) == 0) + path += 4; + +#ifdef VIRTUAL_DIR + virtual_filepath(path, &path_copy TSRMLS_CC); +#else + path_copy = estrdup(path); +#endif + + /* try and open it directly first */ + bz_file = BZ2_bzopen(path_copy, mode); + + if (opened_path == NULL) { + efree(path_copy); + } else if (bz_file) { + *opened_path = path_copy; + } + path_copy = NULL; + + if (bz_file == NULL) { + /* that didn't work, so try and get something from the network/wrapper */ + stream = php_stream_open_wrapper(path, mode, options, opened_path); + + if (stream) { + int fd; + if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void**)&fd, REPORT_ERRORS)) + bz_file = BZ2_bzdopen(fd, mode); + } + } + + if (bz_file) { + retstream = _php_stream_bz2open_from_BZFILE(bz_file, mode, stream STREAMS_REL_CC TSRMLS_CC); + if (retstream) + return retstream; + BZ2_bzclose(bz_file); + } + if (stream) + php_stream_close(stream); + + return NULL; +} + +php_stream_wrapper php_stream_bzip2_wrapper = { + _php_stream_bz2open, + NULL, + NULL +}; -static void php_bz2_close(zend_rsrc_list_entry *rsrc TSRMLS_DC); static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int); PHP_MINIT_FUNCTION(bz2) { - /* Register the resource, with destructor (arg 1) and text description (arg 3), the - other arguments are just standard placeholders */ - le_bz2 = zend_register_list_destructors_ex(php_bz2_close, NULL, "BZip2", module_number); + if(PG(allow_url_fopen)) { + php_register_url_stream_wrapper("bz2", &php_stream_bzip2_wrapper TSRMLS_CC); + } + + return SUCCESS; +} +PHP_MSHUTDOWN_FUNCTION(bz2) +{ + if (PG(allow_url_fopen)) + php_unregister_url_stream_wrapper("bz2" TSRMLS_CC); return SUCCESS; } @@ -105,7 +223,7 @@ zval **file, /* The file to open */ **mode; /* The mode to open the stream with */ BZFILE *bz; /* The compressed file stream */ - FILE *fp; /* If filepointer is given, its placed in here */ + php_stream *stream = NULL; char *path; if (ZEND_NUM_ARGS() != 2 || @@ -117,138 +235,27 @@ /* If it's not a resource its a string containing the filename to open */ if (Z_TYPE_PP(file) != IS_RESOURCE) { convert_to_string_ex(file); - -#ifdef VIRTUAL_DIR - virtual_filepath(Z_STRVAL_PP(file), &path TSRMLS_CC); -#else - path = Z_STRVAL_PP(file); -#endif - - bz = BZ2_bzopen(path, Z_STRVAL_PP(mode)); - } - /* If it is a resource, than its a 'FILE *' resource */ - else { + stream = php_stream_bz2open(path, Z_STRVAL_PP(mode), ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL, NULL); + } else { + /* If it is a resource, than its a stream resource */ int type; int fd; - fp = (FILE*) zend_fetch_resource(file TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream()); - ZEND_VERIFY_RESOURCE(fp); - if (!php_stream_cast((php_stream*)fp, PHP_STREAM_AS_FD, (void*)&fd, REPORT_ERRORS)) + stream = (php_stream *)zend_fetch_resource(file TSRMLS_CC, -1, "File-Handle", &type, 1, php_file_le_stream()); + ZEND_VERIFY_RESOURCE(stream); + if (!php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, REPORT_ERRORS)) RETURN_FALSE; bz = BZ2_bzdopen(fd, Z_STRVAL_PP(mode)); - } - - if (bz == NULL) { - php_error(E_WARNING, "bzopen(): Unable to open file"); - RETURN_FALSE; - } - - ZEND_REGISTER_RESOURCE(return_value, bz, le_bz2); -} -/* }}} */ - -/* {{{ proto string bzread(resource bz [, int len]) - Reads len bytes from the BZip2 stream given by bz */ -PHP_FUNCTION(bzread) -{ - zval **bzp, /* BZip2 Resource Pointer */ - **zlen; /* The (optional) length to read */ - BZFILE *bz; /* BZip2 File pointer */ - void *buf; /* Buffer to read data into */ - int len = 1024, /* Length to read, passed to the BZ2_bzread function */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - - if (argc < 1 || argc > 2 || - zend_get_parameters_ex(argc, &bzp, &zlen) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - /* Optional second argument, the length to read, if not given, it - defaults to 1024 bytes (why not? :) */ - if (argc > 1) { - convert_to_long_ex(zlen); - len = Z_LVAL_PP(zlen); - } - - /* Allocate the buffer and read data into it */ - buf = emalloc(len + 1); - BZ2_bzread(bz, buf, len); - - RETURN_STRINGL(buf, len, 0); -} -/* }}} */ - -/* {{{ proto int bzwrite(resource bz, string data [, int len]) - Writes data to the BZip2 stream given by bz */ -PHP_FUNCTION(bzwrite) -{ - zval **bzp, /* Bzip2 Resource Pointer */ - **data, /* The data to write */ - **zlen; /* The (optional) length of the data to write */ - BZFILE *bz; /* BZip2 File pointer */ - int error, /* Error container */ - len, /* Length to read, passed to the BZ2_bzwrite function */ - argc = ZEND_NUM_ARGS(); /* Argument count */ - - if (argc < 2 || argc > 3 || - zend_get_parameters_ex(argc, &bzp, &data, &zlen) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_string_ex(data); - - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - /* If the length of the data is given, use that, otherwise, just use the - data's string length */ - if (argc > 2) { - convert_to_long_ex(zlen); - len = Z_LVAL_PP(zlen); - } - else { - len = Z_STRLEN_PP(data); - } - - /* Write the data and return the error */ - error = BZ2_bzwrite(bz, (void *) Z_STRVAL_PP(data), len); - RETURN_LONG(error); -} -/* }}} */ - -/* {{{ proto int bzflush(resource bz) - Flushes a BZip2 stream */ -PHP_FUNCTION(bzflush) -{ - zval **bzp; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ - int error; /* Error container */ - if (ZEND_NUM_ARGS() != 1 || - zend_get_parameters_ex(1, &bzp) == FAILURE) { - WRONG_PARAM_COUNT; + stream = php_stream_bz2open_from_BZFILE(bz, Z_STRVAL_PP(mode), stream); } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - - error = BZ2_bzflush(bz); - RETURN_LONG(error); -} -/* }}} */ -/* {{{ proto int bzclose(resource bz) - Closes a BZip2 stream */ -PHP_FUNCTION(bzclose) -{ - zval **bzp=NULL; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ - - if (ZEND_NUM_ARGS() != 1 || - zend_get_parameters_ex(1, &bzp) == FAILURE) { - WRONG_PARAM_COUNT; + if (stream) { + php_stream_to_zval(stream, return_value); + } else { + RETURN_FALSE; } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File Handle", le_bz2); - zend_list_delete(Z_RESVAL_PP(bzp)); } /* }}} */ @@ -333,7 +340,8 @@ } else { /* Copy the buffer, we have perhaps allocate alot more than we need, so we erealloc() the buffer to the proper size */ - dest = erealloc(dest, size); + dest = erealloc(dest, size + 1); + dest[size] = '\0'; RETURN_STRINGL(dest, size, 0); } } @@ -398,39 +406,37 @@ } else { /* we might have allocated a little to much, so erealloc the buffer down to size, before returning it */ - dest = erealloc(dest, size); + dest = erealloc(dest, size + 1); + dest[size] = '\0'; RETURN_STRINGL(dest, size, 0); } } /* }}} */ -/* {{{ php_bz2_close() - Closes a BZip2 file pointer */ -static void php_bz2_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) -{ - BZFILE *bz = (BZFILE *) rsrc->ptr; /* The BZip2 File pointer */ - - BZ2_bzclose(bz); -} -/* }}} */ - /* {{{ php_bz2_error() The central error handling interface, does the work for bzerrno, bzerrstr and bzerror */ static void php_bz2_error(INTERNAL_FUNCTION_PARAMETERS, int opt) { zval **bzp; /* BZip2 Resource Pointer */ - BZFILE *bz; /* BZip2 File pointer */ + php_stream *stream; const char *errstr; /* Error string */ int errnum; /* Error number */ + struct php_bz2_stream_data_t *self; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &bzp) == FAILURE) { WRONG_PARAM_COUNT; } - ZEND_FETCH_RESOURCE(bz, BZFILE *, bzp, -1, "BZip2 File pointer", le_bz2); + ZEND_FETCH_RESOURCE(stream, php_stream *, bzp, -1, "File-Handle", php_file_le_stream()); + + if (!php_stream_is(stream, PHP_STREAM_IS_BZIP2)) { + RETURN_FALSE; + } + + self = (struct php_bz2_stream_data_t *)stream->abstract; /* Fetch the error information */ - errstr = BZ2_bzerror(bz, &errnum); + errstr = BZ2_bzerror(self->bz_file, &errnum); /* Determine what to return */ switch (opt) { Index: php4/ext/bz2/php_bz2.h diff -u php4/ext/bz2/php_bz2.h:1.7 php4/ext/bz2/php_bz2.h:1.8 --- php4/ext/bz2/php_bz2.h:1.7 Thu Feb 28 03:25:43 2002 +++ php4/ext/bz2/php_bz2.h Wed Mar 20 21:27:41 2002 @@ -30,13 +30,13 @@ #define PHP_BZ2_API #endif +/* Bzip2 includes */ +#include <bzlib.h> + PHP_MINIT_FUNCTION(bz2); +PHP_MSHUTDOWN_FUNCTION(bz2); PHP_MINFO_FUNCTION(bz2); PHP_FUNCTION(bzopen); -PHP_FUNCTION(bzread); -PHP_FUNCTION(bzwrite); -PHP_FUNCTION(bzflush); -PHP_FUNCTION(bzclose); PHP_FUNCTION(bzerrno); PHP_FUNCTION(bzerrstr); PHP_FUNCTION(bzerror); @@ -46,6 +46,15 @@ #else #define phpext_bz2_ptr NULL #endif + +PHPAPI php_stream *_php_stream_bz2open(char *path, char *mode, int options, char **opened_path, void *wrappercontext STREAMS_DC TSRMLS_DC); +PHPAPI php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz, char *mode, php_stream *innerstream STREAMS_DC TSRMLS_DC); + +#define php_stream_bz2open_from_BZFILE(bz, mode, innerstream) _php_stream_bz2open_from_BZFILE((bz), (mode), (innerstream) STREAMS_CC TSRMLS_CC) +#define php_stream_bz2open(path, mode, options, opened_path, wrappercontext) _php_stream_bz2open((path), (mode), (options), (opened_path), (wrappercontext) STREAMS_CC TSRMLS_CC) + +php_stream_ops php_stream_bz2io_ops; +#define PHP_STREAM_IS_BZIP2 &php_stream_bz2io_ops #endif Index: php4/ext/zlib/zlib_fopen_wrapper.c diff -u php4/ext/zlib/zlib_fopen_wrapper.c:1.18 php4/ext/zlib/zlib_fopen_wrapper.c:1.19 --- php4/ext/zlib/zlib_fopen_wrapper.c:1.18 Wed Mar 20 20:11:52 2002 +++ php4/ext/zlib/zlib_fopen_wrapper.c Wed Mar 20 21:27:41 2002 @@ -16,7 +16,7 @@ | Hartmut Holzgraefe <hartmut@six.de> | +----------------------------------------------------------------------+ */ -/* $Id: zlib_fopen_wrapper.c,v 1.18 2002/03/21 01:11:52 wez Exp $ */ +/* $Id: zlib_fopen_wrapper.c,v 1.19 2002/03/21 02:27:41 wez Exp $ */ #define IS_EXT_MODULE #define _GNU_SOURCE @@ -78,10 +78,16 @@ return ret; } +static int php_gziop_flush(php_stream *stream TSRMLS_DC) +{ + struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract; + return gzflush(self->gz_file, Z_SYNC_FLUSH); +} + php_stream_ops php_stream_gzio_ops = { php_gziop_write, php_gziop_read, - php_gziop_close, NULL, + php_gziop_close, php_gziop_flush, php_gziop_seek, php_gziop_gets, NULL, "ZLIB" }; @@ -122,6 +128,7 @@ php_stream_wrapper php_stream_gzip_wrapper = { php_stream_gzopen, + NULL, NULL };