From a662f012bba5a6fdc50533673f3fff47bf9af219 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 21 Mar 2002 01:11:52 +0000 Subject: [PATCH] Convert the gzfile related functions into aliases for their equivalents in ext/standard/file.c, so a gzopen()ed file pointer can be used in fread, fseek etc. Improved behaviour of zlib stream. Moved passthru code into streams.c # I'm not happy about BG(mmap_file) Nuked gzgetss_state as no longer needed. --- ext/standard/file.c | 53 +---- ext/zlib/php_zlib.h | 13 -- ext/zlib/zlib.c | 419 +++------------------------------- ext/zlib/zlib_fopen_wrapper.c | 12 +- main/php_streams.h | 5 + main/streams.c | 50 +++- 6 files changed, 97 insertions(+), 455 deletions(-) diff --git a/ext/standard/file.c b/ext/standard/file.c index 0a6d1716a9..801970b9fe 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -1326,55 +1326,6 @@ PHP_FUNCTION(rmdir) } /* }}} */ -/* {{{ php_passthru_stream */ -static size_t php_passthru_stream(php_stream *stream TSRMLS_DC) -{ - size_t bcount = 0; - int ready = 0; - char buf[8192]; -#ifdef HAVE_MMAP - int fd; -#endif - -#ifdef HAVE_MMAP - if (!php_stream_is(stream, PHP_STREAM_IS_SOCKET) - && php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 0)) - { - struct stat sbuf; - off_t off; - void *p; - size_t len; - - fstat(fd, &sbuf); - - if (sbuf.st_size > sizeof(buf)) { - off = php_stream_tell(stream); - len = sbuf.st_size - off; - p = mmap(0, len, PROT_READ, MAP_SHARED, fd, off); - if (p != (void *) MAP_FAILED) { - BG(mmap_file) = p; - BG(mmap_len) = len; - PHPWRITE(p, len); - BG(mmap_file) = NULL; - munmap(p, len); - bcount += len; - ready = 1; - } - } - } -#endif - if(!ready) { - int b; - - while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) { - PHPWRITE(buf, b); - bcount += b; - } - } - return bcount; -} -/* }}} */ - /* {{{ proto int readfile(string filename [, int use_include_path]) Output a file or a URL */ PHP_FUNCTION(readfile) @@ -1407,7 +1358,7 @@ PHP_FUNCTION(readfile) use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); if (stream) { - size = php_passthru_stream(stream TSRMLS_CC); + size = php_stream_passthru(stream); php_stream_close(stream); RETURN_LONG(size); } @@ -1460,7 +1411,7 @@ PHP_FUNCTION(fpassthru) what = zend_fetch_resource(arg1 TSRMLS_CC,-1, "File-Handle", &type, 1, le_stream); ZEND_VERIFY_RESOURCE(what); - size = php_passthru_stream((php_stream*)what TSRMLS_CC); + size = php_stream_passthru((php_stream*)what); RETURN_LONG(size); } /* }}} */ diff --git a/ext/zlib/php_zlib.h b/ext/zlib/php_zlib.h index f3da984d2d..8907838f55 100644 --- a/ext/zlib/php_zlib.h +++ b/ext/zlib/php_zlib.h @@ -25,8 +25,6 @@ #include ZEND_BEGIN_MODULE_GLOBALS(zlib) - int gzgetss_state; - /* variables for transparent gzip encoding */ int compression_coding; z_stream stream; @@ -44,17 +42,6 @@ PHP_MSHUTDOWN_FUNCTION(zlib); PHP_RINIT_FUNCTION(zlib); PHP_MINFO_FUNCTION(zlib); PHP_FUNCTION(gzopen); -PHP_FUNCTION(gzclose); -PHP_FUNCTION(gzeof); -PHP_FUNCTION(gzread); -PHP_FUNCTION(gzgetc); -PHP_FUNCTION(gzgets); -PHP_FUNCTION(gzgetss); -PHP_FUNCTION(gzwrite); -PHP_FUNCTION(gzrewind); -PHP_FUNCTION(gztell); -PHP_FUNCTION(gzseek); -PHP_FUNCTION(gzpassthru); PHP_FUNCTION(readgzfile); PHP_FUNCTION(gzfile); PHP_FUNCTION(gzcompress); diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index ce5b67ffdf..a006eb8bc4 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -86,27 +86,27 @@ static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ /* {{{ php_zlib_functions[] */ function_entry php_zlib_functions[] = { - PHP_FE(readgzfile, NULL) - PHP_FE(gzrewind, NULL) - PHP_FE(gzclose, NULL) - PHP_FE(gzeof, NULL) - PHP_FE(gzgetc, NULL) - PHP_FE(gzgets, NULL) - PHP_FE(gzgetss, NULL) - PHP_FE(gzread, NULL) - PHP_FE(gzopen, NULL) - PHP_FE(gzpassthru, NULL) - PHP_FE(gzseek, NULL) - PHP_FE(gztell, NULL) - PHP_FE(gzwrite, NULL) - PHP_FALIAS(gzputs, gzwrite, NULL) - PHP_FE(gzfile, NULL) - PHP_FE(gzcompress, NULL) - PHP_FE(gzuncompress, NULL) - PHP_FE(gzdeflate, NULL) - PHP_FE(gzinflate, NULL) - PHP_FE(gzencode, NULL) - PHP_FE(ob_gzhandler, NULL) + PHP_FE(readgzfile, NULL) + PHP_NAMED_FE(gzrewind, PHP_FN(rewind), NULL) + PHP_NAMED_FE(gzclose, PHP_FN(fclose), NULL) + PHP_NAMED_FE(gzeof, PHP_FN(feof), NULL) + PHP_NAMED_FE(gzgetc, PHP_FN(fgetc), NULL) + PHP_NAMED_FE(gzgets, PHP_FN(fgets), NULL) + PHP_NAMED_FE(gzgetss, PHP_FN(fgetss), NULL) + PHP_NAMED_FE(gzread, PHP_FN(fread), NULL) + PHP_FE(gzopen, NULL) + PHP_NAMED_FE(gzpassthru, PHP_FN(fpassthru), NULL) + PHP_NAMED_FE(gzseek, PHP_FN(fseek), NULL) + PHP_NAMED_FE(gztell, PHP_FN(ftell), NULL) + PHP_NAMED_FE(gzwrite, PHP_FN(fwrite), NULL) + PHP_NAMED_FE(gzputs, PHP_FN(fwrite), NULL) + PHP_FE(gzfile, NULL) + PHP_FE(gzcompress, NULL) + PHP_FE(gzuncompress, NULL) + PHP_FE(gzdeflate, NULL) + PHP_FE(gzinflate, NULL) + PHP_FE(gzencode, NULL) + PHP_FE(ob_gzhandler, NULL) {NULL, NULL, NULL} }; /* }}} */ @@ -246,25 +246,6 @@ PHP_MINFO_FUNCTION(zlib) } /* }}} */ -/* {{{ php_gzopen_wrapper - */ -static gzFile php_gzopen_wrapper(char *path, char *mode, int options TSRMLS_DC) -{ - php_stream *stream = NULL; - int fd; - - stream = php_stream_open_wrapper(path, mode, options | REPORT_ERRORS, NULL); - if (stream) { - if (SUCCESS == php_stream_cast(stream, PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD|PHP_STREAM_CAST_TRY_HARD, (void**)&fd, 1)) - { - return gzdopen(fd, mode); - } - php_stream_close(stream); - } - return NULL; -} -/* }}} */ - /* {{{ proto array gzfile(string filename [, int use_include_path]) Read und uncompress entire .gz-file into an array */ PHP_FUNCTION(gzfile) @@ -327,7 +308,7 @@ PHP_FUNCTION(gzfile) PHP_FUNCTION(gzopen) { pval **arg1, **arg2, **arg3; - gzFile *zp; + php_stream *stream; char *p; int use_include_path = 0; @@ -350,282 +331,16 @@ PHP_FUNCTION(gzopen) convert_to_string_ex(arg1); convert_to_string_ex(arg2); p = estrndup(Z_STRVAL_PP(arg2),Z_STRLEN_PP(arg2)); - - /* - * We need a better way of returning error messages from - * php_gzopen_wrapper(). - */ - zp = php_gzopen_wrapper(Z_STRVAL_PP(arg1), p, use_include_path|ENFORCE_SAFE_MODE TSRMLS_CC); - if (!zp) { - php_error(E_WARNING,"gzopen(\"%s\",\"%s\") - %s", - Z_STRVAL_PP(arg1), p, strerror(errno)); - efree(p); - RETURN_FALSE; - } - ZLIBG(gzgetss_state)=0; - efree(p); - ZEND_REGISTER_RESOURCE(return_value, zp, le_zp); -} -/* }}} */ - -/* {{{ proto int gzclose(int zp) - Close an open .gz-file pointer */ -PHP_FUNCTION(gzclose) -{ - pval **arg1; - gzFile *zp; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - zend_list_delete(Z_LVAL_PP(arg1)); - RETURN_TRUE; -} -/* }}} */ - -/* {{{ proto int gzeof(int zp) - Test for end-of-file on a .gz-file pointer */ -PHP_FUNCTION(gzeof) -{ - pval **arg1; - gzFile *zp; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - if ((gzeof(zp))) { - RETURN_TRUE; - } else { - RETURN_FALSE; - } -} -/* }}} */ - -/* {{{ proto string gzgets(int zp, int length) - Get a line from .gz-file pointer */ -PHP_FUNCTION(gzgets) -{ - pval **arg1, **arg2; - gzFile *zp; - int len; - char *buf; - - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_long_ex(arg2); - len = Z_LVAL_PP(arg2); - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - buf = emalloc(sizeof(char) * (len + 1)); - /* needed because recv doesnt put a null at the end*/ - memset(buf,0,len+1); - if (!(gzgets(zp, buf, len) != NULL)) { - efree(buf); - RETVAL_FALSE; - } else { - if (PG(magic_quotes_runtime)) { - Z_STRVAL_P(return_value) = php_addslashes(buf,0,&Z_STRLEN_P(return_value),1 TSRMLS_CC); - } else { - Z_STRVAL_P(return_value) = buf; - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - } - Z_TYPE_P(return_value) = IS_STRING; - } - return; -} -/* }}} */ - -/* {{{ proto string gzgetc(int zp) - Get a character from .gz-file pointer */ -PHP_FUNCTION(gzgetc) -{ - pval **arg1; - gzFile *zp; - int c; - char *buf; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - buf = emalloc(sizeof(char) * 2); - if ((c=gzgetc(zp)) == (-1)) { - efree(buf); - RETVAL_FALSE; - } else { - buf[0]=(char)c; - buf[1]='\0'; - Z_STRVAL_P(return_value) = buf; - Z_STRLEN_P(return_value) = 1; - Z_TYPE_P(return_value) = IS_STRING; - } - return; -} -/* }}} */ - -/* Strip any HTML tags while reading */ -/* {{{ proto string gzgetss(int zp, int length [, string allowable_tags]) - Get a line from file pointer and strip HTML tags */ -PHP_FUNCTION(gzgetss) -{ - pval **fd, **bytes, **allow=NULL; - gzFile *zp; - int len; - char *buf; - char *allowed_tags=NULL; - int allowed_tags_len=0; - switch(ZEND_NUM_ARGS()) { - case 2: - if(zend_get_parameters_ex(2, &fd, &bytes) == FAILURE) { - RETURN_FALSE; - } - break; - case 3: - if(zend_get_parameters_ex(3, &fd, &bytes, &allow) == FAILURE) { - RETURN_FALSE; - } - convert_to_string_ex(allow); - allowed_tags = Z_STRVAL_PP(allow); - allowed_tags_len = Z_STRLEN_PP(allow); - break; - default: - WRONG_PARAM_COUNT; - /* NOTREACHED */ - break; - } - - convert_to_long_ex(bytes); - - len = Z_LVAL_PP(bytes); - - ZEND_FETCH_RESOURCE(zp, gzFile *, fd, -1, "Zlib file", le_zp); - - buf = emalloc(sizeof(char) * (len + 1)); - /*needed because recv doesnt set null char at end*/ - memset(buf,0,len+1); - if (!(gzgets(zp, buf, len) != NULL)) { - efree(buf); + stream = php_stream_gzopen(Z_STRVAL_PP(arg1), p, use_include_path|ENFORCE_SAFE_MODE, NULL, NULL STREAMS_CC TSRMLS_CC); + if (!stream) { RETURN_FALSE; } - - /* strlen() can be used here since we are doing it on the return of an fgets() anyway */ - php_strip_tags(buf, strlen(buf), &ZLIBG(gzgetss_state), allowed_tags, allowed_tags_len); - RETURN_STRING(buf, 0); - -} -/* }}} */ - -/* {{{ proto int gzwrite(int zp, string str [, int length]) - Binary-safe .gz-file write */ -PHP_FUNCTION(gzwrite) -{ - pval **arg1, **arg2, **arg3=NULL; - gzFile *zp; - int ret; - int num_bytes; - - switch (ZEND_NUM_ARGS()) { - case 2: - if (zend_get_parameters_ex(2, &arg1, &arg2)==FAILURE) { - RETURN_FALSE; - } - convert_to_string_ex(arg2); - num_bytes = Z_STRLEN_PP(arg2); - break; - case 3: - if (zend_get_parameters_ex(3, &arg1, &arg2, &arg3)==FAILURE) { - RETURN_FALSE; - } - convert_to_string_ex(arg2); - convert_to_long_ex(arg3); - num_bytes = MIN(Z_LVAL_PP(arg3), Z_STRLEN_PP(arg2)); - break; - default: - WRONG_PARAM_COUNT; - /* NOTREACHED */ - break; - } - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - /* strip slashes only if the length wasn't specified explicitly */ - if (!arg3 && PG(magic_quotes_runtime)) { - php_stripslashes(Z_STRVAL_PP(arg2), &num_bytes TSRMLS_CC); - } - - ret = gzwrite(zp, Z_STRVAL_PP(arg2),num_bytes); - RETURN_LONG(ret); + efree(p); + php_stream_to_zval(stream, return_value); } /* }}} */ -/* {{{ proto int gzputs(int zp, string str [, int length]) - An alias for gzwrite */ -/* }}} */ - -/* {{{ proto int gzrewind(int zp) - Rewind the position of a .gz-file pointer */ -PHP_FUNCTION(gzrewind) -{ - pval **arg1; - gzFile *zp; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - gzrewind(zp); - RETURN_TRUE; -} -/* }}} */ - -/* {{{ proto int gztell(int zp) - Get .gz-file pointer's read/write position */ -PHP_FUNCTION(gztell) -{ - pval **arg1; - long pos; - gzFile *zp; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - pos = gztell(zp); - RETURN_LONG(pos); -} -/* }}} */ - -/* {{{ proto int gzseek(int zp, int offset) - Seek on a file pointer */ -PHP_FUNCTION(gzseek) -{ - pval **arg1, **arg2; - int ret; - gzFile *zp; - - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_long_ex(arg2); - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - ret = gzseek(zp, Z_LVAL_PP(arg2), SEEK_SET); - RETURN_LONG(ret); -} -/* }}} */ - /* * Read a file and write the ouput to stdout */ @@ -634,9 +349,8 @@ PHP_FUNCTION(gzseek) PHP_FUNCTION(readgzfile) { pval **arg1, **arg2; - char buf[8192]; - gzFile *zp; - int b, size; + php_stream * stream; + int size; int use_include_path = 0; @@ -659,85 +373,16 @@ PHP_FUNCTION(readgzfile) } convert_to_string_ex(arg1); - /* - * We need a better way of returning error messages from - * php_gzopen_wrapper(). - */ - zp = php_gzopen_wrapper(Z_STRVAL_PP(arg1),"r", use_include_path|ENFORCE_SAFE_MODE TSRMLS_CC); - if (!zp){ - php_error(E_WARNING,"ReadGzFile(\"%s\") - %s",Z_STRVAL_PP(arg1),strerror(errno)); + stream = php_stream_gzopen(Z_STRVAL_PP(arg1), "rb", use_include_path|ENFORCE_SAFE_MODE, NULL, NULL STREAMS_CC TSRMLS_CC); + if (!stream) { RETURN_FALSE; } - size= 0; - while((b = gzread(zp, buf, sizeof(buf))) > 0) { - PHPWRITE(buf,b); - size += b ; - } - gzclose(zp); - RETURN_LONG(size); -} -/* }}} */ - -/* - * Read to EOF on a file descriptor and write the output to stdout. - */ -/* {{{ proto int gzpassthru(int zp) - Output all remaining data from a .gz-file pointer */ -PHP_FUNCTION(gzpassthru) -{ - pval **arg1; - gzFile *zp; - char buf[8192]; - int size, b; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - size = 0; - while((b = gzread(zp, buf, sizeof(buf))) > 0) { - PHPWRITE(buf,b); - size += b ; - } -/* gzclose(zp); */ - zend_list_delete(Z_LVAL_PP(arg1)); + php_stream_passthru(stream); + php_stream_close(stream); RETURN_LONG(size); } /* }}} */ -/* {{{ proto string gzread(int zp, int length) - Binary-safe file read */ -PHP_FUNCTION(gzread) -{ - pval **arg1, **arg2; - gzFile *zp; - int len; - - if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &arg1, &arg2) == FAILURE) { - WRONG_PARAM_COUNT; - } - convert_to_long_ex(arg2); - len = Z_LVAL_PP(arg2); - - ZEND_FETCH_RESOURCE(zp, gzFile *, arg1, -1, "Zlib file", le_zp); - - Z_STRVAL_P(return_value) = emalloc(sizeof(char) * (len + 1)); - /* needed because recv doesnt put a null at the end*/ - - Z_STRLEN_P(return_value) = gzread(zp, Z_STRVAL_P(return_value), len); - Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0; - - if (PG(magic_quotes_runtime)) { - Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value),&Z_STRLEN_P(return_value),1 TSRMLS_CC); - } - Z_TYPE_P(return_value) = IS_STRING; -} - -/* }}} */ - - /* {{{ proto string gzcompress(string data [, int level]) Gzip-compress a string */ PHP_FUNCTION(gzcompress) diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c index df2961c4be..d2acfa0e27 100644 --- a/ext/zlib/zlib_fopen_wrapper.c +++ b/ext/zlib/zlib_fopen_wrapper.c @@ -12,7 +12,8 @@ | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ - | Author: Hartmut Holzgraefe | + | Author: Wez Furlong , based on work by: | + | Hartmut Holzgraefe | +----------------------------------------------------------------------+ */ /* $Id$ */ @@ -57,6 +58,11 @@ static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count static int php_gziop_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC) { struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract; + + assert(self != NULL); + if (offset == 0 && whence == SEEK_CUR) + return gztell(self->gz_file); + return gzseek(self->gz_file, offset, whence); } @@ -90,14 +96,14 @@ php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened if (strncmp("zlib:", path, 5) == 0) path += 5; - self->stream = php_stream_open_wrapper(path, mode, options, opened_path); + self->stream = php_stream_open_wrapper(path, mode, STREAM_MUST_SEEK|options, opened_path); if (self->stream) { int fd; if (SUCCESS == php_stream_cast(self->stream, PHP_STREAM_AS_FD, (void**)&fd, REPORT_ERRORS)) { self->gz_file = gzdopen(fd, mode); if (self->gz_file) { - stream = php_stream_alloc(&php_stream_gzio_ops, self, 0, mode); + stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode); if (stream) return stream; gzclose(self->gz_file); diff --git a/main/php_streams.h b/main/php_streams.h index efce5adcef..33fd4ee625 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -193,6 +193,7 @@ PHPAPI int _php_stream_flush(php_stream *stream TSRMLS_DC); PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC); #define php_stream_gets(stream, buf, maxlen) _php_stream_gets((stream), (buf), (maxlen) TSRMLS_CC) +/* CAREFUL! this is equivalent to puts NOT fputs! */ PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC); #define php_stream_puts(stream, buf) _php_stream_puts((stream), (buf) TSRMLS_CC) @@ -208,6 +209,10 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen int persistent STREAMS_DC TSRMLS_DC); #define php_stream_copy_to_mem(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_CC TSRMLS_CC) +/* output all data from a stream */ +PHPAPI size_t _php_stream_passthru(php_stream * src STREAMS_DC TSRMLS_DC); +#define php_stream_passthru(stream) _php_stream_passthru((stream) STREAMS_CC TSRMLS_CC) + /* maybe implement someday */ #define php_stream_error(stream) (0) diff --git a/main/streams.c b/main/streams.c index 419745b9d4..a02e4c7f7e 100755 --- a/main/streams.c +++ b/main/streams.c @@ -26,7 +26,7 @@ #include "php_network.h" #include "php_open_temporary_file.h" #include "ext/standard/file.h" - +#include "ext/standard/basic_functions.h" /* for BG(mmap_file) (not strictly required) */ #ifdef HAVE_SYS_MMAN_H #include #endif @@ -286,6 +286,54 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_ return -1; } +PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC TSRMLS_DC) +{ + size_t bcount = 0; + int ready = 0; + char buf[8192]; +#ifdef HAVE_MMAP + int fd; +#endif + +#ifdef HAVE_MMAP + if (!php_stream_is(stream, PHP_STREAM_IS_SOCKET) + && php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 0)) + { + struct stat sbuf; + off_t off; + void *p; + size_t len; + + fstat(fd, &sbuf); + + if (sbuf.st_size > sizeof(buf)) { + off = php_stream_tell(stream); + len = sbuf.st_size - off; + p = mmap(0, len, PROT_READ, MAP_SHARED, fd, off); + if (p != (void *) MAP_FAILED) { + BG(mmap_file) = p; + BG(mmap_len) = len; + PHPWRITE(p, len); + BG(mmap_file) = NULL; + munmap(p, len); + bcount += len; + ready = 1; + } + } + } +#endif + if(!ready) { + int b; + + while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) { + PHPWRITE(buf, b); + bcount += b; + } + } + return bcount; +} + + PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC) { size_t ret = 0; -- 2.50.1