]> granicus.if.org Git - php/commitdiff
Refactoring php_stream_copy_to_mem to return zend_string
authorXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 10:12:30 +0000 (18:12 +0800)
committerXinchen Hui <laruence@gmail.com>
Mon, 24 Feb 2014 10:12:30 +0000 (18:12 +0800)
ext/phar/phar.c
ext/standard/exec.c
ext/standard/file.c
ext/standard/streamsfuncs.c
main/php_streams.h
main/streams/streams.c

index 867c664171b2890582fa1ef477255e2b5dd3fbca..a23c1b89c23f4f666686bed2990964bf446c4867 100644 (file)
@@ -2604,6 +2604,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert,
        }
 
        if (user_stub) {
+               zend_string *suser_stub;
                if (len < 0) {
                        /* resource passed in */
                        if (!(php_stream_from_zval_no_verify(stubfile, (zval **)user_stub))) {
@@ -2623,7 +2624,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert,
                        }
                        user_stub = 0;
 
-                       if (!(len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)) || !user_stub) {
+                       if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) {
                                if (closeoldfile) {
                                        php_stream_close(oldfile);
                                }
@@ -2634,6 +2635,8 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert,
                                return EOF;
                        }
                        free_user_stub = 1;
+                       user_stub = suser_stub->val;
+                       len = suser_stub->len;
                } else {
                        free_user_stub = 0;
                }
@@ -2648,7 +2651,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert,
                                spprintf(error, 0, "illegal stub for phar \"%s\"", phar->fname);
                        }
                        if (free_user_stub) {
-                               efree(user_stub);
+                               STR_FREE(suser_stub);
                        }
                        return EOF;
                }
@@ -2665,13 +2668,13 @@ int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert,
                                spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
                        }
                        if (free_user_stub) {
-                               efree(user_stub);
+                               STR_FREE(suser_stub);
                        }
                        return EOF;
                }
                phar->halt_offset = len + 5;
                if (free_user_stub) {
-                       efree(user_stub);
+                       STR_FREE(suser_stub);
                }
        } else {
                size_t written;
index 8bb8fefba5f533493f4f836617bcda886bc053f2..7c440bf79192cfaece98a20accfa0e5e9d166aa0 100644 (file)
@@ -438,10 +438,9 @@ PHP_FUNCTION(escapeshellarg)
 PHP_FUNCTION(shell_exec)
 {
        FILE *in;
-       size_t total_readbytes;
        char *command;
        int command_len;
-       char *ret;
+       zend_string *ret;
        php_stream *stream;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) {
@@ -458,12 +457,11 @@ PHP_FUNCTION(shell_exec)
        }
 
        stream = php_stream_fopen_from_pipe(in, "rb");
-       total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0);
+       ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
        php_stream_close(stream);
 
-       if (total_readbytes > 0) {
-//???          RETVAL_STRINGL(ret, total_readbytes, 0);
-               RETVAL_STRINGL(ret, total_readbytes);
+       if (ret->len > 0) {
+               RETVAL_STR(ret);
        }
 }
 /* }}} */
index 5004b119c6c5faae044471af5ffea4bf2719219d..c4327fab4e67f5b5fecddda78615a9d375f99aeb 100644 (file)
@@ -520,14 +520,13 @@ PHP_FUNCTION(file_get_contents)
 {
        char *filename;
        int filename_len;
-       char *contents;
        zend_bool use_include_path = 0;
        php_stream *stream;
-       int len;
        long offset = -1;
        long maxlen = PHP_STREAM_COPY_ALL;
        zval *zcontext = NULL;
        php_stream_context *context = NULL;
+       zend_string *contents;
 
        /* Parse arguments */
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|br!ll", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen) == FAILURE) {
@@ -554,12 +553,8 @@ PHP_FUNCTION(file_get_contents)
                RETURN_FALSE;
        }
 
-       if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {
-//???  RETVAL_STRINGL(contents, len, 0);
-               RETVAL_STRINGL(contents, len);
-               efree(contents);
-       } else if (len == 0) {
-               RETVAL_EMPTY_STRING();
+       if ((contents = php_stream_copy_to_mem(stream, maxlen, 0)) != NULL) {
+               RETVAL_STR(contents);
        } else {
                RETVAL_FALSE;
        }
@@ -715,9 +710,8 @@ PHP_FUNCTION(file)
 {
        char *filename;
        int filename_len;
-       char *target_buf=NULL, *p, *s, *e;
+       char *p, *s, *e;
        register int i = 0;
-       int target_len;
        char eol_marker = '\n';
        long flags = 0;
        zend_bool use_include_path;
@@ -726,6 +720,7 @@ PHP_FUNCTION(file)
        php_stream *stream;
        zval *zcontext = NULL;
        php_stream_context *context = NULL;
+       zend_string *target_buf;
 
        /* Parse arguments */
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|lr!", &filename, &filename_len, &flags, &zcontext) == FAILURE) {
@@ -750,11 +745,11 @@ PHP_FUNCTION(file)
        /* Initialize return array */
        array_init(return_value);
 
-       if ((target_len = php_stream_copy_to_mem(stream, &target_buf, PHP_STREAM_COPY_ALL, 0))) {
-               s = target_buf;
-               e = target_buf + target_len;
+       if ((target_buf = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) != NULL) {
+               s = target_buf->val;
+               e = target_buf->val + target_buf->len;
 
-               if (!(p = (char*)php_stream_locate_eol(stream, target_buf, target_len TSRMLS_CC))) {
+               if (!(p = (char*)php_stream_locate_eol(stream, target_buf TSRMLS_CC))) {
                        p = e;
                        goto parse_eol;
                }
@@ -775,7 +770,7 @@ parse_eol:
                } else {
                        do {
                                int windows_eol = 0;
-                               if (p != target_buf && eol_marker == '\n' && *(p - 1) == '\r') {
+                               if (p != target_buf->val && eol_marker == '\n' && *(p - 1) == '\r') {
                                        windows_eol++;
                                }
                                if (skip_blank_lines && !(p-s-windows_eol)) {
@@ -795,7 +790,7 @@ parse_eol:
        }
 
        if (target_buf) {
-               efree(target_buf);
+               STR_FREE(target_buf);
        }
        php_stream_close(stream);
 }
index b5608f450d2e21911e849348299aea70248b4513..32f24c4df0bf66fc74bff4806b315e537def3a72 100644 (file)
@@ -412,8 +412,7 @@ PHP_FUNCTION(stream_get_contents)
        zval            *zsrc;
        long            maxlen          = PHP_STREAM_COPY_ALL,
                                desiredpos      = -1L;
-       int                     len;
-       char            *contents       = NULL;
+       zend_string *contents;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zsrc, &maxlen, &desiredpos) == FAILURE) {
                RETURN_FALSE;
@@ -441,14 +440,9 @@ PHP_FUNCTION(stream_get_contents)
                }
        }
 
-       len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);
+       contents = php_stream_copy_to_mem(stream, maxlen, 0);
 
-       if (contents) {
-//???          RETVAL_STRINGL(contents, len, 0);
-               RETVAL_STRINGL(contents, len);
-       } else {
-               RETVAL_EMPTY_STRING();
-       }
+       RETURN_STR(contents);
 }
 /* }}} */
 
index f434e6db75ca1f7e0cfda9a1a2e2d1753f0c1272..ad16d4b74f7e7b288d5784a0afae13402a2a21ce 100644 (file)
@@ -61,7 +61,7 @@ END_EXTERN_C()
  * the ultimate ancestor, which is useful, because there can be several layers of calls */
 #define php_stream_alloc_rel(ops, thisptr, persistent, mode) _php_stream_alloc((ops), (thisptr), (persistent), (mode) STREAMS_REL_CC TSRMLS_CC)
 
-#define php_stream_copy_to_mem_rel(src, buf, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_REL_CC TSRMLS_CC)
+#define php_stream_copy_to_mem_rel(src, maxlen, persistent) _php_stream_copy_to_mem((src), (buf), (maxlen), (persistent) STREAMS_REL_CC TSRMLS_CC)
 
 #define php_stream_fopen_rel(filename, mode, opened, options) _php_stream_fopen((filename), (mode), (opened), (options) STREAMS_REL_CC TSRMLS_CC)
 
@@ -440,9 +440,8 @@ PHPAPI int _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size
 
 /* read all data from stream and put into a buffer. Caller must free buffer
  * when done. */
-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)
+PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC);
+#define php_stream_copy_to_mem(src, maxlen, persistent) _php_stream_copy_to_mem((src), (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);
@@ -546,7 +545,7 @@ PHPAPI int php_register_url_stream_wrapper_volatile(const char *protocol, php_st
 PHPAPI int php_unregister_url_stream_wrapper_volatile(const char *protocol TSRMLS_DC);
 PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
 PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options TSRMLS_DC);
-PHPAPI const char *php_stream_locate_eol(php_stream *stream, const char *buf, size_t buf_len TSRMLS_DC);
+PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf TSRMLS_DC);
 
 #define php_stream_open_wrapper(path, mode, options, opened)   _php_stream_open_wrapper_ex((path), (mode), (options), (opened), NULL STREAMS_CC TSRMLS_CC)
 #define php_stream_open_wrapper_ex(path, mode, options, opened, context)       _php_stream_open_wrapper_ex((path), (mode), (options), (opened), (context) STREAMS_CC TSRMLS_CC)
index fe592c5e9b01255922293a135346d0e8ddac93b9..e80a87c860e6d22f2686957275afce313a047a29 100644 (file)
@@ -833,7 +833,7 @@ PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_D
        return (stream->ops->stat)(stream, ssb TSRMLS_CC);
 }
 
-PHPAPI const char *php_stream_locate_eol(php_stream *stream, const char *buf, size_t buf_len TSRMLS_DC)
+PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf TSRMLS_DC)
 {
        size_t avail;
        const char *cr, *lf, *eol = NULL;
@@ -843,8 +843,8 @@ PHPAPI const char *php_stream_locate_eol(php_stream *stream, const char *buf, si
                readptr = (char*)stream->readbuf + stream->readpos;
                avail = stream->writepos - stream->readpos;
        } else {
-               readptr = buf;
-               avail = buf_len;
+               readptr = buf->val;
+               avail = buf->len;
        }
 
        /* Look for EOL */
@@ -913,7 +913,7 @@ PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
                        int done = 0;
 
                        readptr = (char*)stream->readbuf + stream->readpos;
-                       eol = php_stream_locate_eol(stream, NULL, 0 TSRMLS_CC);
+                       eol = php_stream_locate_eol(stream, NULL TSRMLS_CC);
 
                        if (eol) {
                                cpysz = eol - readptr + 1;
@@ -1411,7 +1411,7 @@ PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC TSRMLS_DC)
 }
 
 
-PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC)
+PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC)
 {
        size_t ret = 0;
        char *ptr;
@@ -1419,9 +1419,10 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen
        int step = CHUNK_SIZE;
        int min_room = CHUNK_SIZE / 4;
        php_stream_statbuf ssbuf;
+       zend_string *result;
 
        if (maxlen == 0) {
-               return 0;
+               return STR_EMPTY_ALLOC();
        }
 
        if (maxlen == PHP_STREAM_COPY_ALL) {
@@ -1429,7 +1430,8 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen
        }
 
        if (maxlen > 0) {
-               ptr = *buf = pemalloc_rel_orig(maxlen + 1, persistent);
+               result = STR_ALLOC(maxlen, persistent);
+               ptr = result->val;
                while ((len < maxlen) && !php_stream_eof(src)) {
                        ret = php_stream_read(src, ptr, maxlen - len);
                        if (!ret) {
@@ -1440,11 +1442,12 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen
                }
                if (len) {
                        *ptr = '\0';
+                       result->len = len;
                } else {
-                       pefree(*buf, persistent);
-                       *buf = NULL;
+                       STR_FREE(result);
+                       result = NULL;
                }
-               return len;
+               return result;
        }
 
        /* avoid many reallocs by allocating a good sized chunk to begin with, if
@@ -1459,26 +1462,28 @@ PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen
                max_len = step;
        }
 
-       ptr = *buf = pemalloc_rel_orig(max_len, persistent);
+       result = STR_ALLOC(max_len, persistent);
+       ptr = result->val;
 
-       while((ret = php_stream_read(src, ptr, max_len - len))) {
+       while ((ret = php_stream_read(src, ptr, max_len - len)))        {
                len += ret;
                if (len + min_room >= max_len) {
-                       *buf = perealloc_rel_orig(*buf, max_len + step, persistent);
+                       result = STR_REALLOC(result, max_len + step, persistent);
                        max_len += step;
-                       ptr = *buf + len;
+                       ptr = result->val + len;
                } else {
                        ptr += ret;
                }
        }
        if (len) {
-               *buf = perealloc_rel_orig(*buf, len + 1, persistent);
-               (*buf)[len] = '\0';
+               result = STR_REALLOC(result, len, persistent);
+               result->val[len] = '\0';
        } else {
-               pefree(*buf, persistent);
-               *buf = NULL;
+               STR_FREE(result);
+               result = NULL;
        }
-       return len;
+
+       return result;
 }
 
 /* Returns SUCCESS/FAILURE and sets *len to the number of bytes moved */