PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - main/streams - streams.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 932 1034 90.1 %
Date: 2014-08-04 Functions: 69 72 95.8 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
      16             :    | Borrowed code from:                                                  |
      17             :    |          Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
      18             :    |          Jim Winstead <jimw@php.net>                                 |
      19             :    +----------------------------------------------------------------------+
      20             :  */
      21             : 
      22             : /* $Id$ */
      23             : 
      24             : #define _GNU_SOURCE
      25             : #include "php.h"
      26             : #include "php_globals.h"
      27             : #include "php_network.h"
      28             : #include "php_open_temporary_file.h"
      29             : #include "ext/standard/file.h"
      30             : #include "ext/standard/basic_functions.h" /* for BG(mmap_file) (not strictly required) */
      31             : #include "ext/standard/php_string.h" /* for php_memnstr, used by php_stream_get_record() */
      32             : #include <stddef.h>
      33             : #include <fcntl.h>
      34             : #include "php_streams_int.h"
      35             : 
      36             : /* {{{ resource and registration code */
      37             : /* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */
      38             : static HashTable url_stream_wrappers_hash;
      39             : static int le_stream = FAILURE; /* true global */
      40             : static int le_pstream = FAILURE; /* true global */
      41             : static int le_stream_filter = FAILURE; /* true global */
      42             : 
      43     8611084 : PHPAPI int php_file_le_stream(void)
      44             : {
      45     8611084 :         return le_stream;
      46             : }
      47             : 
      48     8583714 : PHPAPI int php_file_le_pstream(void)
      49             : {
      50     8583714 :         return le_pstream;
      51             : }
      52             : 
      53         109 : PHPAPI int php_file_le_stream_filter(void)
      54             : {
      55         109 :         return le_stream_filter;
      56             : }
      57             : 
      58         143 : PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D)
      59             : {
      60         143 :         return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
      61             : }
      62             : 
      63           1 : PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void)
      64             : {
      65           1 :         return &url_stream_wrappers_hash;
      66             : }
      67             : 
      68           4 : static int _php_stream_release_context(zend_rsrc_list_entry *le, void *pContext TSRMLS_DC)
      69             : {
      70           4 :         if (le->ptr == pContext) {
      71           1 :                 return --le->refcount == 0;
      72             :         }
      73           3 :         return 0;
      74             : }
      75             : 
      76         625 : static int forget_persistent_resource_id_numbers(zend_rsrc_list_entry *rsrc TSRMLS_DC)
      77             : {
      78             :         php_stream *stream;
      79             : 
      80         625 :         if (Z_TYPE_P(rsrc) != le_pstream) {
      81         618 :                 return 0;
      82             :         }
      83             : 
      84           7 :         stream = (php_stream*)rsrc->ptr;
      85             : 
      86             : #if STREAM_DEBUG
      87             : fprintf(stderr, "forget_persistent: %s:%p\n", stream->ops->label, stream);
      88             : #endif
      89             : 
      90           7 :         stream->rsrc_id = FAILURE;
      91             : 
      92           7 :         if (stream->context) {
      93           1 :                 zend_hash_apply_with_argument(&EG(regular_list),
      94             :                                 (apply_func_arg_t) _php_stream_release_context,
      95           1 :                                 stream->context TSRMLS_CC);
      96           1 :                 stream->context = NULL;
      97             :         }
      98             : 
      99           7 :         return 0;
     100             : }
     101             : 
     102       21257 : PHP_RSHUTDOWN_FUNCTION(streams)
     103             : {
     104       21257 :         zend_hash_apply(&EG(persistent_list), (apply_func_t)forget_persistent_resource_id_numbers TSRMLS_CC);
     105       21257 :         return SUCCESS;
     106             : }
     107             : 
     108         154 : PHPAPI php_stream *php_stream_encloses(php_stream *enclosing, php_stream *enclosed)
     109             : {
     110         154 :         php_stream *orig = enclosed->enclosing_stream;
     111             : 
     112             :         php_stream_auto_cleanup(enclosed);
     113         154 :         enclosed->enclosing_stream = enclosing;
     114         154 :         return orig;
     115             : }
     116             : 
     117        1159 : PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream TSRMLS_DC)
     118             : {
     119             :         zend_rsrc_list_entry *le;
     120             : 
     121        1159 :         if (zend_hash_find(&EG(persistent_list), (char*)persistent_id, strlen(persistent_id)+1, (void*) &le) == SUCCESS) {
     122           1 :                 if (Z_TYPE_P(le) == le_pstream) {
     123           1 :                         if (stream) {
     124             :                                 HashPosition pos;
     125             :                                 zend_rsrc_list_entry *regentry;
     126           1 :                                 ulong index = -1; /* intentional */
     127             : 
     128             :                                 /* see if this persistent resource already has been loaded to the
     129             :                                  * regular list; allowing the same resource in several entries in the
     130             :                                  * regular list causes trouble (see bug #54623) */
     131           1 :                                 zend_hash_internal_pointer_reset_ex(&EG(regular_list), &pos);
     132           5 :                                 while (zend_hash_get_current_data_ex(&EG(regular_list),
     133             :                                                 (void **)&regentry, &pos) == SUCCESS) {
     134           4 :                                         if (regentry->ptr == le->ptr) {
     135           1 :                                                 zend_hash_get_current_key_ex(&EG(regular_list), NULL, NULL,
     136             :                                                         &index, 0, &pos);
     137           1 :                                                 break;
     138             :                                         }
     139           3 :                                         zend_hash_move_forward_ex(&EG(regular_list), &pos);
     140             :                                 }
     141             :                                 
     142           1 :                                 *stream = (php_stream*)le->ptr;
     143           1 :                                 if (index == -1) { /* not found in regular list */
     144           0 :                                         le->refcount++;
     145           0 :                                         (*stream)->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, *stream, le_pstream);
     146             :                                 } else {
     147           1 :                                         regentry->refcount++;
     148           1 :                                         (*stream)->rsrc_id = index;
     149             :                                 }
     150             :                         }
     151           1 :                         return PHP_STREAM_PERSISTENT_SUCCESS;
     152             :                 }
     153           0 :                 return PHP_STREAM_PERSISTENT_FAILURE;
     154             :         }
     155        1158 :         return PHP_STREAM_PERSISTENT_NOT_EXIST;
     156             : }
     157             : 
     158             : /* }}} */
     159             : 
     160         485 : static zend_llist *php_get_wrapper_errors_list(php_stream_wrapper *wrapper TSRMLS_DC)
     161             : {
     162         485 :     zend_llist *list = NULL;
     163         485 :     if (!FG(wrapper_errors)) {
     164         375 :         return NULL;
     165             :     } else {
     166         110 :         zend_hash_find(FG(wrapper_errors), (const char*)&wrapper,
     167             :             sizeof wrapper, (void**)&list);
     168         110 :         return list;
     169             :     }
     170             : }
     171             : 
     172             : /* {{{ wrapper error reporting */
     173         505 : void php_stream_display_wrapper_errors(php_stream_wrapper *wrapper, const char *path, const char *caption TSRMLS_DC)
     174             : {
     175         505 :         char *tmp = estrdup(path);
     176             :         char *msg;
     177         505 :         int free_msg = 0;
     178             : 
     179         505 :         if (wrapper) {
     180         485 :                 zend_llist *err_list = php_get_wrapper_errors_list(wrapper TSRMLS_CC);
     181         485 :                 if (err_list) {
     182         109 :                         size_t l = 0;
     183             :                         int brlen;
     184             :                         int i;
     185         109 :                         int count = zend_llist_count(err_list);
     186             :                         const char *br;
     187             :                         const char **err_buf_p;
     188             :                         zend_llist_position pos;
     189             : 
     190         109 :                         if (PG(html_errors)) {
     191           0 :                                 brlen = 7;
     192           0 :                                 br = "<br />\n";
     193             :                         } else {
     194         109 :                                 brlen = 1;
     195         109 :                                 br = "\n";
     196             :                         }
     197             : 
     198         333 :                         for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
     199             :                                         err_buf_p;
     200         115 :                                         err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
     201         115 :                                 l += strlen(*err_buf_p);
     202         115 :                                 if (i < count - 1) {
     203           6 :                                         l += brlen;
     204             :                                 }
     205             :                         }
     206         109 :                         msg = emalloc(l + 1);
     207         109 :                         msg[0] = '\0';
     208         333 :                         for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
     209             :                                         err_buf_p;
     210         115 :                                         err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
     211         115 :                                 strcat(msg, *err_buf_p);
     212         115 :                                 if (i < count - 1) {
     213           6 :                                         strcat(msg, br);
     214             :                                 }
     215             :                         }
     216             : 
     217         109 :                         free_msg = 1;
     218             :                 } else {
     219         376 :                         if (wrapper == &php_plain_files_wrapper) {
     220         371 :                                 msg = strerror(errno); /* TODO: not ts on linux */
     221             :                         } else {
     222           5 :                                 msg = "operation failed";
     223             :                         }
     224             :                 }
     225             :         } else {
     226          20 :                 msg = "no suitable wrapper could be found";
     227             :         }
     228             : 
     229         505 :         php_strip_url_passwd(tmp);
     230         505 :         php_error_docref1(NULL TSRMLS_CC, tmp, E_WARNING, "%s: %s", caption, msg);
     231         505 :         efree(tmp);
     232         505 :         if (free_msg) {
     233         109 :                 efree(msg);
     234             :         }
     235         505 : }
     236             : 
     237      136249 : void php_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper TSRMLS_DC)
     238             : {
     239      136249 :         if (wrapper && FG(wrapper_errors)) {
     240         178 :                 zend_hash_del(FG(wrapper_errors), (const char*)&wrapper, sizeof wrapper);
     241             :         }
     242      136249 : }
     243             : 
     244         115 : static void wrapper_error_dtor(void *error)
     245             : {
     246         115 :         efree(*(char**)error);
     247         115 : }
     248             : 
     249         149 : PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options TSRMLS_DC, const char *fmt, ...)
     250             : {
     251             :         va_list args;
     252         149 :         char *buffer = NULL;
     253             : 
     254         149 :         va_start(args, fmt);
     255         149 :         vspprintf(&buffer, 0, fmt, args);
     256         149 :         va_end(args);
     257             : 
     258         183 :         if (options & REPORT_ERRORS || wrapper == NULL) {
     259          34 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", buffer);
     260          34 :                 efree(buffer);
     261             :         } else {
     262         115 :                 zend_llist *list = NULL;
     263         115 :                 if (!FG(wrapper_errors)) {
     264          68 :                         ALLOC_HASHTABLE(FG(wrapper_errors));
     265          68 :                         zend_hash_init(FG(wrapper_errors), 8, NULL,
     266             :                                         (dtor_func_t)zend_llist_destroy, 0);
     267             :                 } else {
     268          47 :                         zend_hash_find(FG(wrapper_errors), (const char*)&wrapper,
     269             :                                 sizeof wrapper, (void**)&list);
     270             :                 }
     271             : 
     272         115 :                 if (!list) {
     273             :                         zend_llist new_list;
     274         109 :                         zend_llist_init(&new_list, sizeof buffer, wrapper_error_dtor, 0);
     275         109 :                         zend_hash_update(FG(wrapper_errors), (const char*)&wrapper,
     276             :                                 sizeof wrapper, &new_list, sizeof new_list, (void**)&list);
     277             :                 }
     278             : 
     279             :                 /* append to linked list */
     280         115 :                 zend_llist_add_element(list, &buffer);
     281             :         }
     282         149 : }
     283             : 
     284             : 
     285             : /* }}} */
     286             : 
     287             : /* allocate a new stream for a particular ops */
     288      226382 : PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract, const char *persistent_id, const char *mode STREAMS_DC TSRMLS_DC) /* {{{ */
     289             : {
     290             :         php_stream *ret;
     291             : 
     292      226382 :         ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent_id ? 1 : 0);
     293             : 
     294      226382 :         memset(ret, 0, sizeof(php_stream));
     295             : 
     296      226382 :         ret->readfilters.stream = ret;
     297      226382 :         ret->writefilters.stream = ret;
     298             : 
     299             : #if STREAM_DEBUG
     300             : fprintf(stderr, "stream_alloc: %s:%p persistent=%s\n", ops->label, ret, persistent_id);
     301             : #endif
     302             : 
     303      226382 :         ret->ops = ops;
     304      226382 :         ret->abstract = abstract;
     305      226382 :         ret->is_persistent = persistent_id ? 1 : 0;
     306      226382 :         ret->chunk_size = FG(def_chunk_size);
     307             : 
     308             : #if ZEND_DEBUG
     309             :         ret->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
     310             :         ret->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
     311             : #endif
     312             : 
     313      226382 :         if (FG(auto_detect_line_endings)) {
     314           0 :                 ret->flags |= PHP_STREAM_FLAG_DETECT_EOL;
     315             :         }
     316             : 
     317      226382 :         if (persistent_id) {
     318             :                 zend_rsrc_list_entry le;
     319             : 
     320        1158 :                 Z_TYPE(le) = le_pstream;
     321        1158 :                 le.ptr = ret;
     322        1158 :                 le.refcount = 0;
     323             : 
     324        1158 :                 if (FAILURE == zend_hash_update(&EG(persistent_list), (char *)persistent_id,
     325             :                                         strlen(persistent_id) + 1,
     326             :                                         (void *)&le, sizeof(le), NULL)) {
     327             : 
     328           0 :                         pefree(ret, 1);
     329           0 :                         return NULL;
     330             :                 }
     331             :         }
     332             : 
     333      226382 :         ret->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ret, persistent_id ? le_pstream : le_stream);
     334      226382 :         strlcpy(ret->mode, mode, sizeof(ret->mode));
     335             : 
     336      226382 :         ret->wrapper          = NULL;
     337      226382 :         ret->wrapperthis      = NULL;
     338      226382 :         ret->wrapperdata      = NULL;
     339      226382 :         ret->stdiocast        = NULL;
     340      226382 :         ret->orig_path        = NULL;
     341      226382 :         ret->context          = NULL;
     342      226382 :         ret->readbuf          = NULL;
     343      226382 :         ret->enclosing_stream = NULL;
     344             : 
     345      226382 :         return ret;
     346             : }
     347             : /* }}} */
     348             : 
     349         154 : PHPAPI int _php_stream_free_enclosed(php_stream *stream_enclosed, int close_options TSRMLS_DC) /* {{{ */
     350             : {
     351         154 :         return _php_stream_free(stream_enclosed,
     352             :                 close_options | PHP_STREAM_FREE_IGNORE_ENCLOSING TSRMLS_CC);
     353             : }
     354             : /* }}} */
     355             : 
     356             : #if STREAM_DEBUG
     357             : static const char *_php_stream_pretty_free_options(int close_options, char *out)
     358             : {
     359             :         if (close_options & PHP_STREAM_FREE_CALL_DTOR)
     360             :                 strcat(out, "CALL_DTOR, ");
     361             :         if (close_options & PHP_STREAM_FREE_RELEASE_STREAM)
     362             :                 strcat(out, "RELEASE_STREAM, ");
     363             :         if (close_options & PHP_STREAM_FREE_PRESERVE_HANDLE)
     364             :                 strcat(out, "PREVERSE_HANDLE, ");
     365             :         if (close_options & PHP_STREAM_FREE_RSRC_DTOR)
     366             :                 strcat(out, "RSRC_DTOR, ");
     367             :         if (close_options & PHP_STREAM_FREE_PERSISTENT)
     368             :                 strcat(out, "PERSISTENT, ");
     369             :         if (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING)
     370             :                 strcat(out, "IGNORE_ENCLOSING, ");
     371             :         if (out[0] != '\0')
     372             :                 out[strlen(out) - 2] = '\0';
     373             :         return out;
     374             : }
     375             : #endif
     376             : 
     377          45 : static int _php_stream_free_persistent(zend_rsrc_list_entry *le, void *pStream TSRMLS_DC)
     378             : {
     379          45 :         return le->ptr == pStream;
     380             : }
     381             : 
     382             : 
     383      346450 : PHPAPI int _php_stream_free(php_stream *stream, int close_options TSRMLS_DC) /* {{{ */
     384             : {
     385      346450 :         int ret = 1;
     386      346450 :         int preserve_handle = close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 1 : 0;
     387      346450 :         int release_cast = 1;
     388      346450 :         php_stream_context *context = NULL;
     389             : 
     390             :         /* on an resource list destruction, the context, another resource, may have
     391             :          * already been freed (if it was created after the stream resource), so
     392             :          * don't reference it */
     393      346450 :         if (EG(active)) {
     394      346059 :                 context = stream->context;
     395             :         }
     396             : 
     397      346450 :         if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
     398           0 :                 preserve_handle = 1;
     399             :         }
     400             : 
     401             : #if STREAM_DEBUG
     402             :         {
     403             :                 char out[200] = "";
     404             :                 fprintf(stderr, "stream_free: %s:%p[%s] in_free=%d opts=%s\n",
     405             :                         stream->ops->label, stream, stream->orig_path, stream->in_free, _php_stream_pretty_free_options(close_options, out));
     406             :         }
     407             :         
     408             : #endif
     409             : 
     410      346450 :         if (stream->in_free) {
     411             :                 /* hopefully called recursively from the enclosing stream; the pointer was NULLed below */
     412      119967 :                 if ((stream->in_free == 1) && (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) && (stream->enclosing_stream == NULL)) {
     413          44 :                         close_options |= PHP_STREAM_FREE_RSRC_DTOR; /* restore flag */
     414             :                 } else {
     415      119879 :                         return 1; /* recursion protection */
     416             :                 }
     417             :         }
     418             : 
     419      226571 :         stream->in_free++;
     420             : 
     421             :         /* force correct order on enclosing/enclosed stream destruction (only from resource
     422             :          * destructor as in when reverse destroying the resource list) */
     423      549760 :         if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) &&
     424      107759 :                         !(close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) &&
     425      107715 :                         (close_options & (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)) && /* always? */
     426      107715 :                         (stream->enclosing_stream != NULL)) {
     427          44 :                 php_stream *enclosing_stream = stream->enclosing_stream;
     428          44 :                 stream->enclosing_stream = NULL;
     429             :                 /* we force PHP_STREAM_CALL_DTOR because that's from where the
     430             :                  * enclosing stream can free this stream. We remove rsrc_dtor because
     431             :                  * we want the enclosing stream to be deleted from the resource list */
     432          44 :                 return _php_stream_free(enclosing_stream,
     433             :                         (close_options | PHP_STREAM_FREE_CALL_DTOR) & ~PHP_STREAM_FREE_RSRC_DTOR TSRMLS_CC);
     434             :         }
     435             : 
     436             :         /* if we are releasing the stream only (and preserving the underlying handle),
     437             :          * we need to do things a little differently.
     438             :          * We are only ever called like this when the stream is cast to a FILE*
     439             :          * for include (or other similar) purposes.
     440             :          * */
     441      226527 :         if (preserve_handle) {
     442           0 :                 if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
     443             :                         /* If the stream was fopencookied, we must NOT touch anything
     444             :                          * here, as the cookied stream relies on it all.
     445             :                          * Instead, mark the stream as OK to auto-clean */
     446             :                         php_stream_auto_cleanup(stream);
     447           0 :                         stream->in_free--;
     448           0 :                         return 0;
     449             :                 }
     450             :                 /* otherwise, make sure that we don't close the FILE* from a cast */
     451           0 :                 release_cast = 0;
     452             :         }
     453             : 
     454             : #if STREAM_DEBUG
     455             : fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remove_rsrc=%d\n",
     456             :                 stream->ops->label, stream, stream->orig_path, preserve_handle, release_cast,
     457             :                 (close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0);
     458             : #endif
     459             : 
     460             :         /* make sure everything is saved */
     461      226527 :         _php_stream_flush(stream, 1 TSRMLS_CC);
     462             : 
     463             :         /* If not called from the resource dtor, remove the stream from the resource list. */
     464      226527 :         if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0) {
     465             :                 /* zend_list_delete actually only decreases the refcount; if we're
     466             :                  * releasing the stream, we want to actually delete the resource from
     467             :                  * the resource list, otherwise the resource will point to invalid memory.
     468             :                  * In any case, let's always completely delete it from the resource list,
     469             :                  * not only when PHP_STREAM_FREE_RELEASE_STREAM is set */
     470      236918 :                 while (zend_list_delete(stream->rsrc_id) == SUCCESS) {}
     471             :         }
     472             : 
     473      226527 :         if (close_options & PHP_STREAM_FREE_CALL_DTOR) {
     474      226527 :                 if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
     475             :                         /* calling fclose on an fopencookied stream will ultimately
     476             :                                 call this very same function.  If we were called via fclose,
     477             :                                 the cookie_closer unsets the fclose_stdiocast flags, so
     478             :                                 we can be sure that we only reach here when PHP code calls
     479             :                                 php_stream_free.
     480             :                                 Lets let the cookie code clean it all up.
     481             :                          */
     482           1 :                         stream->in_free = 0;
     483           1 :                         return fclose(stream->stdiocast);
     484             :                 }
     485             : 
     486      226526 :                 ret = stream->ops->close(stream, preserve_handle ? 0 : 1 TSRMLS_CC);
     487      226526 :                 stream->abstract = NULL;
     488             : 
     489             :                 /* tidy up any FILE* that might have been fdopened */
     490      226526 :                 if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
     491           0 :                         fclose(stream->stdiocast);
     492           0 :                         stream->stdiocast = NULL;
     493           0 :                         stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
     494             :                 }
     495             :         }
     496             : 
     497      226526 :         if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) {
     498      453118 :                 while (stream->readfilters.head) {
     499          66 :                         php_stream_filter_remove(stream->readfilters.head, 1 TSRMLS_CC);
     500             :                 }
     501      453101 :                 while (stream->writefilters.head) {
     502          49 :                         php_stream_filter_remove(stream->writefilters.head, 1 TSRMLS_CC);
     503             :                 }
     504             : 
     505      226526 :                 if (stream->wrapper && stream->wrapper->wops && stream->wrapper->wops->stream_closer) {
     506           0 :                         stream->wrapper->wops->stream_closer(stream->wrapper, stream TSRMLS_CC);
     507           0 :                         stream->wrapper = NULL;
     508             :                 }
     509             : 
     510      226526 :                 if (stream->wrapperdata) {
     511          94 :                         zval_ptr_dtor(&stream->wrapperdata);
     512          94 :                         stream->wrapperdata = NULL;
     513             :                 }
     514             : 
     515      226526 :                 if (stream->readbuf) {
     516       51232 :                         pefree(stream->readbuf, stream->is_persistent);
     517       51232 :                         stream->readbuf = NULL;
     518             :                 }
     519             : 
     520      226526 :                 if (stream->is_persistent && (close_options & PHP_STREAM_FREE_PERSISTENT)) {
     521             :                         /* we don't work with *stream but need its value for comparison */
     522        1108 :                         zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) _php_stream_free_persistent, stream TSRMLS_CC);
     523             :                 }
     524             : #if ZEND_DEBUG
     525             :                 if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) && (stream->__exposed == 0) && (EG(error_reporting) & E_WARNING)) {
     526             :                         /* it leaked: Lets deliberately NOT pefree it so that the memory manager shows it
     527             :                          * as leaked; it will log a warning, but lets help it out and display what kind
     528             :                          * of stream it was. */
     529             :                         char *leakinfo;
     530             :                         spprintf(&leakinfo, 0, __FILE__ "(%d) : Stream of type '%s' %p (path:%s) was not closed\n", __LINE__, stream->ops->label, stream, stream->orig_path);
     531             : 
     532             :                         if (stream->orig_path) {
     533             :                                 pefree(stream->orig_path, stream->is_persistent);
     534             :                                 stream->orig_path = NULL;
     535             :                         }
     536             : 
     537             : # if defined(PHP_WIN32)
     538             :                         OutputDebugString(leakinfo);
     539             : # else
     540             :                         fprintf(stderr, "%s", leakinfo);
     541             : # endif
     542             :                         efree(leakinfo);
     543             :                 } else {
     544             :                         if (stream->orig_path) {
     545             :                                 pefree(stream->orig_path, stream->is_persistent);
     546             :                                 stream->orig_path = NULL;
     547             :                         }
     548             : 
     549             :                         pefree(stream, stream->is_persistent);
     550             :                 }
     551             : #else
     552      226526 :                 if (stream->orig_path) {
     553      157380 :                         pefree(stream->orig_path, stream->is_persistent);
     554      157380 :                         stream->orig_path = NULL;
     555             :                 }
     556             : 
     557      226526 :                 pefree(stream, stream->is_persistent);
     558             : #endif
     559             :         }
     560             : 
     561      226526 :         if (context) {
     562        1026 :                 zend_list_delete(context->rsrc_id);
     563             :         }
     564             : 
     565      226526 :         return ret;
     566             : }
     567             : /* }}} */
     568             : 
     569             : /* {{{ generic stream operations */
     570             : 
     571     1098001 : static void php_stream_fill_read_buffer(php_stream *stream, size_t size TSRMLS_DC)
     572             : {
     573             :         /* allocate/fill the buffer */
     574             : 
     575     1098001 :         if (stream->readfilters.head) {
     576             :                 char *chunk_buf;
     577          96 :                 int err_flag = 0;
     578          96 :                 php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
     579          96 :                 php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
     580             : 
     581             :                 /* Invalidate the existing cache, otherwise reads can fail, see note in
     582             :                    main/streams/filter.c::_php_stream_filter_append */
     583          96 :                 stream->writepos = stream->readpos = 0;
     584             : 
     585             :                 /* allocate a buffer for reading chunks */
     586          96 :                 chunk_buf = emalloc(stream->chunk_size);
     587             : 
     588         253 :                 while (!stream->eof && !err_flag && (stream->writepos - stream->readpos < (off_t)size)) {
     589          77 :                         size_t justread = 0;
     590             :                         int flags;
     591             :                         php_stream_bucket *bucket;
     592          77 :                         php_stream_filter_status_t status = PSFS_ERR_FATAL;
     593             :                         php_stream_filter *filter;
     594             : 
     595             :                         /* read a chunk into a bucket */
     596          77 :                         justread = stream->ops->read(stream, chunk_buf, stream->chunk_size TSRMLS_CC);
     597         138 :                         if (justread && justread != (size_t)-1) {
     598          61 :                                 bucket = php_stream_bucket_new(stream, chunk_buf, justread, 0, 0 TSRMLS_CC);
     599             : 
     600             :                                 /* after this call, bucket is owned by the brigade */
     601          61 :                                 php_stream_bucket_append(brig_inp, bucket TSRMLS_CC);
     602             : 
     603          61 :                                 flags = PSFS_FLAG_NORMAL;
     604             :                         } else {
     605          16 :                                 flags = stream->eof ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC;
     606             :                         }
     607             : 
     608             :                         /* wind the handle... */
     609         140 :                         for (filter = stream->readfilters.head; filter; filter = filter->next) {
     610          83 :                                 status = filter->fops->filter(stream, filter, brig_inp, brig_outp, NULL, flags TSRMLS_CC);
     611             : 
     612          83 :                                 if (status != PSFS_PASS_ON) {
     613          20 :                                         break;
     614             :                                 }
     615             : 
     616             :                                 /* brig_out becomes brig_in.
     617             :                                  * brig_in will always be empty here, as the filter MUST attach any un-consumed buckets
     618             :                                  * to its own brigade */
     619          63 :                                 brig_swap = brig_inp;
     620          63 :                                 brig_inp = brig_outp;
     621          63 :                                 brig_outp = brig_swap;
     622          63 :                                 memset(brig_outp, 0, sizeof(*brig_outp));
     623             :                         }
     624             : 
     625          77 :                         switch (status) {
     626             :                                 case PSFS_PASS_ON:
     627             :                                         /* we get here when the last filter in the chain has data to pass on.
     628             :                                          * in this situation, we are passing the brig_in brigade into the
     629             :                                          * stream read buffer */
     630         159 :                                         while (brig_inp->head) {
     631          45 :                                                 bucket = brig_inp->head;
     632             :                                                 /* grow buffer to hold this bucket
     633             :                                                  * TODO: this can fail for persistent streams */
     634          45 :                                                 if (stream->readbuflen - stream->writepos < bucket->buflen) {
     635          36 :                                                         stream->readbuflen += bucket->buflen;
     636          36 :                                                         stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
     637             :                                                                         stream->is_persistent);
     638             :                                                 }
     639          45 :                                                 memcpy(stream->readbuf + stream->writepos, bucket->buf, bucket->buflen);
     640          45 :                                                 stream->writepos += bucket->buflen;
     641             : 
     642          45 :                                                 php_stream_bucket_unlink(bucket TSRMLS_CC);
     643          45 :                                                 php_stream_bucket_delref(bucket TSRMLS_CC);
     644             :                                         }
     645          57 :                                         break;
     646             : 
     647             :                                 case PSFS_FEED_ME:
     648             :                                         /* when a filter needs feeding, there is no brig_out to deal with.
     649             :                                          * we simply continue the loop; if the caller needs more data,
     650             :                                          * we will read again, otherwise out job is done here */
     651           3 :                                         if (justread == 0) {
     652             :                                                 /* there is no data */
     653           3 :                                                 err_flag = 1;
     654           3 :                                                 break;
     655             :                                         }
     656           0 :                                         continue;
     657             : 
     658             :                                 case PSFS_ERR_FATAL:
     659             :                                         /* some fatal error. Theoretically, the stream is borked, so all
     660             :                                          * further reads should fail. */
     661          17 :                                         err_flag = 1;
     662             :                                         break;
     663             :                         }
     664             : 
     665          77 :                         if (justread == 0 || justread == (size_t)-1) {
     666             :                                 break;
     667             :                         }
     668             :                 }
     669             : 
     670          96 :                 efree(chunk_buf);
     671             : 
     672             :         } else {
     673             :                 /* is there enough data in the buffer ? */
     674     1097905 :                 if (stream->writepos - stream->readpos < (off_t)size) {
     675     1097903 :                         size_t justread = 0;
     676             : 
     677             :                         /* reduce buffer memory consumption if possible, to avoid a realloc */
     678     1097903 :                         if (stream->readbuf && stream->readbuflen - stream->writepos < stream->chunk_size) {
     679      994846 :                                 memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->readbuflen - stream->readpos);
     680      994846 :                                 stream->writepos -= stream->readpos;
     681      994846 :                                 stream->readpos = 0;
     682             :                         }
     683             : 
     684             :                         /* grow the buffer if required
     685             :                          * TODO: this can fail for persistent streams */
     686     1097903 :                         if (stream->readbuflen - stream->writepos < stream->chunk_size) {
     687       53320 :                                 stream->readbuflen += stream->chunk_size;
     688       53320 :                                 stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
     689             :                                                 stream->is_persistent);
     690             :                         }
     691             : 
     692     1097903 :                         justread = stream->ops->read(stream, stream->readbuf + stream->writepos,
     693             :                                         stream->readbuflen - stream->writepos
     694             :                                         TSRMLS_CC);
     695             : 
     696     1097903 :                         if (justread != (size_t)-1) {
     697     1097595 :                                 stream->writepos += justread;
     698             :                         }
     699             :                 }
     700             :         }
     701     1098001 : }
     702             : 
     703     2136170 : PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size TSRMLS_DC)
     704             : {
     705     2136170 :         size_t toread = 0, didread = 0;
     706             : 
     707     4341876 :         while (size > 0) {
     708             : 
     709             :                 /* take from the read buffer first.
     710             :                  * It is possible that a buffered stream was switched to non-buffered, so we
     711             :                  * drain the remainder of the buffer before using the "raw" read mode for
     712             :                  * the excess */
     713     2141881 :                 if (stream->writepos > stream->readpos) {
     714             : 
     715     1134632 :                         toread = stream->writepos - stream->readpos;
     716     1134632 :                         if (toread > size) {
     717     1006667 :                                 toread = size;
     718             :                         }
     719             : 
     720     1134632 :                         memcpy(buf, stream->readbuf + stream->readpos, toread);
     721     1134632 :                         stream->readpos += toread;
     722     1134632 :                         size -= toread;
     723     1134632 :                         buf += toread;
     724     1134632 :                         didread += toread;
     725             :                 }
     726             : 
     727             :                 /* ignore eof here; the underlying state might have changed */
     728     2141881 :                 if (size == 0) {
     729     1055557 :                         break;
     730             :                 }
     731             : 
     732     1125262 :                 if (!stream->readfilters.head && (stream->flags & PHP_STREAM_FLAG_NO_BUFFER || stream->chunk_size == 1)) {
     733       45268 :                         toread = stream->ops->read(stream, buf, size TSRMLS_CC);
     734       45268 :                         if (toread == (size_t) -1) {
     735             :                                 /* e.g. underlying read(2) returned -1 */
     736        6330 :                                 break;
     737             :                         }
     738             :                 } else {
     739     1041056 :                         php_stream_fill_read_buffer(stream, size TSRMLS_CC);
     740             : 
     741     1041056 :                         toread = stream->writepos - stream->readpos;
     742     1041056 :                         if (toread > size) {
     743      137765 :                                 toread = size;
     744             :                         }
     745             : 
     746     1041056 :                         if (toread > 0) {
     747      999451 :                                 memcpy(buf, stream->readbuf + stream->readpos, toread);
     748      999451 :                                 stream->readpos += toread;
     749             :                         }
     750             :                 }
     751     1079994 :                 if (toread > 0) {
     752     1037233 :                         didread += toread;
     753     1037233 :                         buf += toread;
     754     1037233 :                         size -= toread;
     755             :                 } else {
     756             :                         /* EOF, or temporary end of data (for non-blocking mode). */
     757       42761 :                         break;
     758             :                 }
     759             : 
     760             :                 /* just break anyway, to avoid greedy read */
     761     1037233 :                 if (stream->wrapper != &php_plain_files_wrapper) {
     762      967697 :                         break;
     763             :                 }
     764             :         }
     765             : 
     766     2136170 :         if (didread > 0) {
     767     2095374 :                 stream->position += didread;
     768             :         }
     769             : 
     770     2136170 :         return didread;
     771             : }
     772             : 
     773     1197171 : PHPAPI int _php_stream_eof(php_stream *stream TSRMLS_DC)
     774             : {
     775             :         /* if there is data in the buffer, it's not EOF */
     776     1197171 :         if (stream->writepos - stream->readpos > 0) {
     777     1160021 :                 return 0;
     778             :         }
     779             : 
     780             :         /* use the configured timeout when checking eof */
     781       37150 :         if (!stream->eof && PHP_STREAM_OPTION_RETURN_ERR ==
     782             :                         php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS,
     783             :                         0, NULL)) {
     784           4 :                 stream->eof = 1;
     785             :         }
     786             : 
     787       37150 :         return stream->eof;
     788             : }
     789             : 
     790          48 : PHPAPI int _php_stream_putc(php_stream *stream, int c TSRMLS_DC)
     791             : {
     792          48 :         unsigned char buf = c;
     793             : 
     794          48 :         if (php_stream_write(stream, &buf, 1) > 0) {
     795          48 :                 return 1;
     796             :         }
     797           0 :         return EOF;
     798             : }
     799             : 
     800      538108 : PHPAPI int _php_stream_getc(php_stream *stream TSRMLS_DC)
     801             : {
     802             :         char buf;
     803             : 
     804      538108 :         if (php_stream_read(stream, &buf, 1) > 0) {
     805      536664 :                 return buf & 0xff;
     806             :         }
     807        1444 :         return EOF;
     808             : }
     809             : 
     810           0 : PHPAPI int _php_stream_puts(php_stream *stream, const char *buf TSRMLS_DC)
     811             : {
     812             :         int len;
     813           0 :         char newline[2] = "\n"; /* is this OK for Win? */
     814           0 :         len = strlen(buf);
     815             : 
     816           0 :         if (len > 0 && php_stream_write(stream, buf, len) && php_stream_write(stream, newline, 1)) {
     817           0 :                 return 1;
     818             :         }
     819           0 :         return 0;
     820             : }
     821             : 
     822       43545 : PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
     823             : {
     824       43545 :         memset(ssb, 0, sizeof(*ssb));
     825             : 
     826             :         /* if the stream was wrapped, allow the wrapper to stat it */
     827       43545 :         if (stream->wrapper && stream->wrapper->wops->stream_stat != NULL) {
     828          12 :                 return stream->wrapper->wops->stream_stat(stream->wrapper, stream, ssb TSRMLS_CC);
     829             :         }
     830             : 
     831             :         /* if the stream doesn't directly support stat-ing, return with failure.
     832             :          * We could try and emulate this by casting to a FD and fstat-ing it,
     833             :          * but since the fd might not represent the actual underlying content
     834             :          * this would give bogus results. */
     835       43533 :         if (stream->ops->stat == NULL) {
     836          44 :                 return -1;
     837             :         }
     838             : 
     839       43489 :         return (stream->ops->stat)(stream, ssb TSRMLS_CC);
     840             : }
     841             : 
     842     1766739 : PHPAPI const char *php_stream_locate_eol(php_stream *stream, const char *buf, size_t buf_len TSRMLS_DC)
     843             : {
     844             :         size_t avail;
     845     1766739 :         const char *cr, *lf, *eol = NULL;
     846             :         const char *readptr;
     847             : 
     848     1766739 :         if (!buf) {
     849     1766574 :                 readptr = stream->readbuf + stream->readpos;
     850     1766574 :                 avail = stream->writepos - stream->readpos;
     851             :         } else {
     852         165 :                 readptr = buf;
     853         165 :                 avail = buf_len;
     854             :         }
     855             : 
     856             :         /* Look for EOL */
     857     1766739 :         if (stream->flags & PHP_STREAM_FLAG_DETECT_EOL) {
     858           0 :                 cr = memchr(readptr, '\r', avail);
     859           0 :                 lf = memchr(readptr, '\n', avail);
     860             : 
     861           0 :                 if (cr && lf != cr + 1 && !(lf && lf < cr)) {
     862             :                         /* mac */
     863           0 :                         stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
     864           0 :                         stream->flags |= PHP_STREAM_FLAG_EOL_MAC;
     865           0 :                         eol = cr;
     866           0 :                 } else if ((cr && lf && cr == lf - 1) || (lf)) {
     867             :                         /* dos or unix endings */
     868           0 :                         stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
     869           0 :                         eol = lf;
     870             :                 }
     871     1766739 :         } else if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
     872           0 :                 eol = memchr(readptr, '\r', avail);
     873             :         } else {
     874             :                 /* unix (and dos) line endings */
     875     1766739 :                 eol = memchr(readptr, '\n', avail);
     876             :         }
     877             : 
     878     1766739 :         return eol;
     879             : }
     880             : 
     881             : /* If buf == NULL, the buffer will be allocated automatically and will be of an
     882             :  * appropriate length to hold the line, regardless of the line length, memory
     883             :  * permitting */
     884     1757121 : PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
     885             :                 size_t *returned_len TSRMLS_DC)
     886             : {
     887     1757121 :         size_t avail = 0;
     888     1757121 :         size_t current_buf_size = 0;
     889     1757121 :         size_t total_copied = 0;
     890     1757121 :         int grow_mode = 0;
     891     1757121 :         char *bufstart = buf;
     892             : 
     893     1757121 :         if (buf == NULL) {
     894     1181721 :                 grow_mode = 1;
     895      575400 :         } else if (maxlen == 0) {
     896           0 :                 return NULL;
     897             :         }
     898             : 
     899             :         /*
     900             :          * If the underlying stream operations block when no new data is readable,
     901             :          * we need to take extra precautions.
     902             :          *
     903             :          * If there is buffered data available, we check for a EOL. If it exists,
     904             :          * we pass the data immediately back to the caller. This saves a call
     905             :          * to the read implementation and will not block where blocking
     906             :          * is not necessary at all.
     907             :          *
     908             :          * If the stream buffer contains more data than the caller requested,
     909             :          * we can also avoid that costly step and simply return that data.
     910             :          */
     911             : 
     912             :         for (;;) {
     913     1823786 :                 avail = stream->writepos - stream->readpos;
     914             : 
     915     1823786 :                 if (avail > 0) {
     916     1766574 :                         size_t cpysz = 0;
     917             :                         char *readptr;
     918             :                         const char *eol;
     919     1766574 :                         int done = 0;
     920             : 
     921     1766574 :                         readptr = stream->readbuf + stream->readpos;
     922     1766574 :                         eol = php_stream_locate_eol(stream, NULL, 0 TSRMLS_CC);
     923             : 
     924     1766574 :                         if (eol) {
     925     1741426 :                                 cpysz = eol - readptr + 1;
     926     1741426 :                                 done = 1;
     927             :                         } else {
     928       25148 :                                 cpysz = avail;
     929             :                         }
     930             : 
     931     1766574 :                         if (grow_mode) {
     932             :                                 /* allow room for a NUL. If this realloc is really a realloc
     933             :                                  * (ie: second time around), we get an extra byte. In most
     934             :                                  * cases, with the default chunk size of 8K, we will only
     935             :                                  * incur that overhead once.  When people have lines longer
     936             :                                  * than 8K, we waste 1 byte per additional 8K or so.
     937             :                                  * That seems acceptable to me, to avoid making this code
     938             :                                  * hard to follow */
     939     1169496 :                                 bufstart = erealloc(bufstart, current_buf_size + cpysz + 1);
     940     1169496 :                                 current_buf_size += cpysz + 1;
     941     1169496 :                                 buf = bufstart + total_copied;
     942             :                         } else {
     943      597078 :                                 if (cpysz >= maxlen - 1) {
     944         581 :                                         cpysz = maxlen - 1;
     945         581 :                                         done = 1;
     946             :                                 }
     947             :                         }
     948             : 
     949     1766574 :                         memcpy(buf, readptr, cpysz);
     950             : 
     951     1766574 :                         stream->position += cpysz;
     952     1766574 :                         stream->readpos += cpysz;
     953     1766574 :                         buf += cpysz;
     954     1766574 :                         maxlen -= cpysz;
     955     1766574 :                         total_copied += cpysz;
     956             : 
     957     1766574 :                         if (done) {
     958     1741520 :                                 break;
     959             :                         }
     960       57212 :                 } else if (stream->eof) {
     961         317 :                         break;
     962             :                 } else {
     963             :                         /* XXX: Should be fine to always read chunk_size */
     964             :                         size_t toread;
     965             : 
     966       56895 :                         if (grow_mode) {
     967       30229 :                                 toread = stream->chunk_size;
     968             :                         } else {
     969       26666 :                                 toread = maxlen - 1;
     970       26666 :                                 if (toread > stream->chunk_size) {
     971           0 :                                         toread = stream->chunk_size;
     972             :                                 }
     973             :                         }
     974             : 
     975       56895 :                         php_stream_fill_read_buffer(stream, toread TSRMLS_CC);
     976             : 
     977       56895 :                         if (stream->writepos - stream->readpos == 0) {
     978       15284 :                                 break;
     979             :                         }
     980             :                 }
     981       66665 :         }
     982             : 
     983     1757121 :         if (total_copied == 0) {
     984             :                 if (grow_mode) {
     985             :                         assert(bufstart == NULL);
     986             :                 }
     987       13531 :                 return NULL;
     988             :         }
     989             : 
     990     1743590 :         buf[0] = '\0';
     991     1743590 :         if (returned_len) {
     992     1742016 :                 *returned_len = total_copied;
     993             :         }
     994             : 
     995     1743590 :         return bufstart;
     996             : }
     997             : 
     998             : #define STREAM_BUFFERED_AMOUNT(stream) \
     999             :         ((size_t)(((stream)->writepos) - (stream)->readpos))
    1000             : 
    1001          90 : static const char *_php_stream_search_delim(php_stream *stream,
    1002             :                                                                                         size_t maxlen,
    1003             :                                                                                         size_t skiplen,
    1004             :                                                                                         const char *delim, /* non-empty! */
    1005             :                                                                                         size_t delim_len TSRMLS_DC)
    1006             : {
    1007             :         size_t  seek_len;
    1008             : 
    1009             :         /* set the maximum number of bytes we're allowed to read from buffer */
    1010          90 :         seek_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
    1011          90 :         if (seek_len <= skiplen) {
    1012          35 :                 return NULL;
    1013             :         }
    1014             : 
    1015          55 :         if (delim_len == 1) {
    1016          50 :                 return memchr(&stream->readbuf[stream->readpos + skiplen],
    1017          25 :                         delim[0], seek_len - skiplen);
    1018             :         } else {
    1019          60 :                 return php_memnstr((char*)&stream->readbuf[stream->readpos + skiplen],
    1020             :                                 delim, delim_len,
    1021          30 :                                 (char*)&stream->readbuf[stream->readpos + seek_len]);
    1022             :         }
    1023             : }
    1024             : 
    1025          56 : PHPAPI char *php_stream_get_record(php_stream *stream, size_t maxlen, size_t *returned_len, const char *delim, size_t delim_len TSRMLS_DC)
    1026             : {
    1027             :         char    *ret_buf;                               /* returned buffer */
    1028          56 :         const char *found_delim = NULL;
    1029             :         size_t  buffered_len,
    1030             :                         tent_ret_len;                   /* tentative returned length */
    1031          56 :         int             has_delim        = delim_len > 0;
    1032             : 
    1033          56 :         if (maxlen == 0) {
    1034           0 :                 return NULL;
    1035             :         }
    1036             : 
    1037          56 :         if (has_delim) {
    1038          56 :                 found_delim = _php_stream_search_delim(
    1039             :                         stream, maxlen, 0, delim, delim_len TSRMLS_CC);
    1040             :         }
    1041             : 
    1042          56 :         buffered_len = STREAM_BUFFERED_AMOUNT(stream);
    1043             :         /* try to read up to maxlen length bytes while we don't find the delim */
    1044         121 :         while (!found_delim && buffered_len < maxlen) {
    1045             :                 size_t  just_read,
    1046             :                                 to_read_now;
    1047             : 
    1048          50 :                 to_read_now = MIN(maxlen - buffered_len, stream->chunk_size);
    1049             : 
    1050          50 :                 php_stream_fill_read_buffer(stream, buffered_len + to_read_now TSRMLS_CC);
    1051             : 
    1052          50 :                 just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len;
    1053             : 
    1054             :                 /* Assume the stream is temporarily or permanently out of data */
    1055          50 :                 if (just_read == 0) {
    1056          16 :                         break;
    1057             :                 }
    1058             : 
    1059          34 :                 if (has_delim) {
    1060             :                         /* search for delimiter, but skip buffered_len (the number of bytes
    1061             :                          * buffered before this loop iteration), as they have already been
    1062             :                          * searched for the delimiter.
    1063             :                          * The left part of the delimiter may still remain in the buffer,
    1064             :                          * so subtract up to <delim_len - 1> from buffered_len, which is
    1065             :                          * the ammount of data we skip on this search  as an optimization
    1066             :                          */
    1067          52 :                         found_delim = _php_stream_search_delim(
    1068             :                                 stream, maxlen,
    1069          34 :                                 buffered_len >= (delim_len - 1)
    1070          18 :                                                 ? buffered_len - (delim_len - 1)
    1071             :                                                 : 0,
    1072             :                                 delim, delim_len TSRMLS_CC);
    1073          34 :                         if (found_delim) {
    1074          25 :                                 break;
    1075             :                         }
    1076             :                 }
    1077           9 :                 buffered_len += just_read;
    1078             :         }
    1079             : 
    1080          91 :         if (has_delim && found_delim) {
    1081          35 :                 tent_ret_len = found_delim - (char*)&stream->readbuf[stream->readpos];
    1082          21 :         } else if (!has_delim && STREAM_BUFFERED_AMOUNT(stream) >= maxlen) {
    1083           0 :                 tent_ret_len = maxlen;
    1084             :         } else {
    1085             :                 /* return with error if the delimiter string (if any) was not found, we
    1086             :                  * could not completely fill the read buffer with maxlen bytes and we
    1087             :                  * don't know we've reached end of file. Added with non-blocking streams
    1088             :                  * in mind, where this situation is frequent */
    1089          21 :                 if (STREAM_BUFFERED_AMOUNT(stream) < maxlen && !stream->eof) {
    1090           6 :                         return NULL;
    1091          15 :                 } else if (STREAM_BUFFERED_AMOUNT(stream) == 0 && stream->eof) {
    1092             :                         /* refuse to return an empty string just because by accident
    1093             :                          * we knew of EOF in a read that returned no data */
    1094           4 :                         return NULL;
    1095             :                 } else {
    1096          11 :                         tent_ret_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
    1097             :                 }
    1098             :         }
    1099             : 
    1100          46 :         ret_buf = emalloc(tent_ret_len + 1);
    1101             :         /* php_stream_read will not call ops->read here because the necessary
    1102             :          * data is guaranteedly buffered */
    1103          46 :         *returned_len = php_stream_read(stream, ret_buf, tent_ret_len);
    1104             : 
    1105          46 :         if (found_delim) {
    1106          35 :                 stream->readpos += delim_len;
    1107          35 :                 stream->position += delim_len;
    1108             :         }
    1109          46 :         ret_buf[*returned_len] = '\0';
    1110          46 :         return ret_buf;
    1111             : }
    1112             : 
    1113             : /* Writes a buffer directly to a stream, using multiple of the chunk size */
    1114      247348 : static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
    1115             : {
    1116      247348 :         size_t didwrite = 0, towrite, justwrote;
    1117             : 
    1118             :         /* if we have a seekable stream we need to ensure that data is written at the
    1119             :          * current stream->position. This means invalidating the read buffer and then
    1120             :          * performing a low-level seek */
    1121      247348 :         if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) {
    1122           1 :                 stream->readpos = stream->writepos = 0;
    1123             : 
    1124           1 :                 stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC);
    1125             :         }
    1126             : 
    1127             : 
    1128      743838 :         while (count > 0) {
    1129      249217 :                 towrite = count;
    1130      249217 :                 if (towrite > stream->chunk_size)
    1131        1870 :                         towrite = stream->chunk_size;
    1132             : 
    1133      249217 :                 justwrote = stream->ops->write(stream, buf, towrite TSRMLS_CC);
    1134             : 
    1135             :                 /* convert justwrote to an integer, since normally it is unsigned */
    1136      249217 :                 if ((int)justwrote > 0) {
    1137      249142 :                         buf += justwrote;
    1138      249142 :                         count -= justwrote;
    1139      249142 :                         didwrite += justwrote;
    1140             : 
    1141             :                         /* Only screw with the buffer if we can seek, otherwise we lose data
    1142             :                          * buffered from fifos and sockets */
    1143      249142 :                         if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
    1144      209209 :                                 stream->position += justwrote;
    1145             :                         }
    1146             :                 } else {
    1147          75 :                         break;
    1148             :                 }
    1149             :         }
    1150      247348 :         return didwrite;
    1151             : 
    1152             : }
    1153             : 
    1154             : /* push some data through the write filter chain.
    1155             :  * buf may be NULL, if flags are set to indicate a flush.
    1156             :  * This may trigger a real write to the stream.
    1157             :  * Returns the number of bytes consumed from buf by the first filter in the chain.
    1158             :  * */
    1159         292 : static size_t _php_stream_write_filtered(php_stream *stream, const char *buf, size_t count, int flags TSRMLS_DC)
    1160             : {
    1161         292 :         size_t consumed = 0;
    1162             :         php_stream_bucket *bucket;
    1163         292 :         php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
    1164         292 :         php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
    1165         292 :         php_stream_filter_status_t status = PSFS_ERR_FATAL;
    1166             :         php_stream_filter *filter;
    1167             : 
    1168         292 :         if (buf) {
    1169         161 :                 bucket = php_stream_bucket_new(stream, (char *)buf, count, 0, 0 TSRMLS_CC);
    1170         161 :                 php_stream_bucket_append(&brig_in, bucket TSRMLS_CC);
    1171             :         }
    1172             : 
    1173         431 :         for (filter = stream->writefilters.head; filter; filter = filter->next) {
    1174             :                 /* for our return value, we are interested in the number of bytes consumed from
    1175             :                  * the first filter in the chain */
    1176         606 :                 status = filter->fops->filter(stream, filter, brig_inp, brig_outp,
    1177         303 :                                 filter == stream->writefilters.head ? &consumed : NULL, flags TSRMLS_CC);
    1178             : 
    1179         303 :                 if (status != PSFS_PASS_ON) {
    1180         164 :                         break;
    1181             :                 }
    1182             :                 /* brig_out becomes brig_in.
    1183             :                  * brig_in will always be empty here, as the filter MUST attach any un-consumed buckets
    1184             :                  * to its own brigade */
    1185         139 :                 brig_swap = brig_inp;
    1186         139 :                 brig_inp = brig_outp;
    1187         139 :                 brig_outp = brig_swap;
    1188         139 :                 memset(brig_outp, 0, sizeof(*brig_outp));
    1189             :         }
    1190             : 
    1191         292 :         switch (status) {
    1192             :                 case PSFS_PASS_ON:
    1193             :                         /* filter chain generated some output; push it through to the
    1194             :                          * underlying stream */
    1195         525 :                         while (brig_inp->head) {
    1196         269 :                                 bucket = brig_inp->head;
    1197         269 :                                 _php_stream_write_buffer(stream, bucket->buf, bucket->buflen TSRMLS_CC);
    1198             :                                 /* Potential error situation - eg: no space on device. Perhaps we should keep this brigade
    1199             :                                  * hanging around and try to write it later.
    1200             :                                  * At the moment, we just drop it on the floor
    1201             :                                  * */
    1202             : 
    1203         269 :                                 php_stream_bucket_unlink(bucket TSRMLS_CC);
    1204         269 :                                 php_stream_bucket_delref(bucket TSRMLS_CC);
    1205             :                         }
    1206             :                         break;
    1207             :                 case PSFS_FEED_ME:
    1208             :                         /* need more data before we can push data through to the stream */
    1209             :                         break;
    1210             : 
    1211             :                 case PSFS_ERR_FATAL:
    1212             :                         /* some fatal error.  Theoretically, the stream is borked, so all
    1213             :                          * further writes should fail. */
    1214             :                         break;
    1215             :         }
    1216             : 
    1217         292 :         return consumed;
    1218             : }
    1219             : 
    1220      228852 : PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC)
    1221             : {
    1222      228852 :         int ret = 0;
    1223             : 
    1224      228852 :         if (stream->writefilters.head) {
    1225         131 :                 _php_stream_write_filtered(stream, NULL, 0, closing ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC  TSRMLS_CC);
    1226             :         }
    1227             : 
    1228      228852 :         if (stream->ops->flush) {
    1229      228008 :                 ret = stream->ops->flush(stream TSRMLS_CC);
    1230             :         }
    1231             : 
    1232      228852 :         return ret;
    1233             : }
    1234             : 
    1235      262039 : PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
    1236             : {
    1237      262039 :         if (buf == NULL || count == 0 || stream->ops->write == NULL) {
    1238       14799 :                 return 0;
    1239             :         }
    1240             : 
    1241      247240 :         if (stream->writefilters.head) {
    1242         161 :                 return _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL TSRMLS_CC);
    1243             :         } else {
    1244      247079 :                 return _php_stream_write_buffer(stream, buf, count TSRMLS_CC);
    1245             :         }
    1246             : }
    1247             : 
    1248         153 : PHPAPI size_t _php_stream_printf(php_stream *stream TSRMLS_DC, const char *fmt, ...)
    1249             : {
    1250             :         size_t count;
    1251             :         char *buf;
    1252             :         va_list ap;
    1253             : 
    1254         153 :         va_start(ap, fmt);
    1255         153 :         count = vspprintf(&buf, 0, fmt, ap);
    1256         153 :         va_end(ap);
    1257             : 
    1258         153 :         if (!buf) {
    1259           0 :                 return 0; /* error condition */
    1260             :         }
    1261             : 
    1262         153 :         count = php_stream_write(stream, buf, count);
    1263         153 :         efree(buf);
    1264             : 
    1265         153 :         return count;
    1266             : }
    1267             : 
    1268       63110 : PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC)
    1269             : {
    1270       63110 :         return stream->position;
    1271             : }
    1272             : 
    1273       80424 : PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
    1274             : {
    1275       80424 :         if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
    1276             :                 /* flush to commit data written to the fopencookie FILE* */
    1277           1 :                 fflush(stream->stdiocast);
    1278             :         }
    1279             : 
    1280             :         /* handle the case where we are in the buffer */
    1281       80424 :         if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) {
    1282       79823 :                 switch(whence) {
    1283             :                         case SEEK_CUR:
    1284       18141 :                                 if (offset > 0 && offset <= stream->writepos - stream->readpos) {
    1285         441 :                                         stream->readpos += offset; /* if offset = ..., then readpos = writepos */
    1286         441 :                                         stream->position += offset;
    1287         441 :                                         stream->eof = 0;
    1288         441 :                                         return 0;
    1289             :                                 }
    1290       17700 :                                 break;
    1291             :                         case SEEK_SET:
    1292       61848 :                                 if (offset > stream->position &&
    1293        2506 :                                                 offset <= stream->position + stream->writepos - stream->readpos) {
    1294         831 :                                         stream->readpos += offset - stream->position;
    1295         831 :                                         stream->position = offset;
    1296         831 :                                         stream->eof = 0;
    1297         831 :                                         return 0;
    1298             :                                 }
    1299             :                                 break;
    1300             :                 }
    1301             :         }
    1302             : 
    1303             : 
    1304       79152 :         if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
    1305             :                 int ret;
    1306             : 
    1307       79149 :                 if (stream->writefilters.head) {
    1308           6 :                         _php_stream_flush(stream, 0 TSRMLS_CC);
    1309             :                 }
    1310             : 
    1311       79149 :                 switch(whence) {
    1312             :                         case SEEK_CUR:
    1313       17708 :                                 offset = stream->position + offset;
    1314       17708 :                                 whence = SEEK_SET;
    1315             :                                 break;
    1316             :                 }
    1317       79149 :                 ret = stream->ops->seek(stream, offset, whence, &stream->position TSRMLS_CC);
    1318             : 
    1319       79149 :                 if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) {
    1320       79149 :                         if (ret == 0) {
    1321       78937 :                                 stream->eof = 0;
    1322             :                         }
    1323             : 
    1324             :                         /* invalidate the buffer contents */
    1325       79149 :                         stream->readpos = stream->writepos = 0;
    1326             : 
    1327       79149 :                         return ret;
    1328             :                 }
    1329             :                 /* else the stream has decided that it can't support seeking after all;
    1330             :                  * fall through to attempt emulation */
    1331             :         }
    1332             : 
    1333             :         /* emulate forward moving seeks with reads */
    1334           3 :         if (whence == SEEK_CUR && offset >= 0) {
    1335             :                 char tmp[1024];
    1336             :                 size_t didread;
    1337           2 :                 while(offset > 0) {
    1338           0 :                         if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) == 0) {
    1339           0 :                                 return -1;
    1340             :                         }
    1341           0 :                         offset -= didread;
    1342             :                 }
    1343           1 :                 stream->eof = 0;
    1344           1 :                 return 0;
    1345             :         }
    1346             : 
    1347           2 :         php_error_docref(NULL TSRMLS_CC, E_WARNING, "stream does not support seeking");
    1348             : 
    1349           2 :         return -1;
    1350             : }
    1351             : 
    1352      124704 : PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
    1353             : {
    1354      124704 :         int ret = PHP_STREAM_OPTION_RETURN_NOTIMPL;
    1355             : 
    1356      124704 :         if (stream->ops->set_option) {
    1357      124008 :                 ret = stream->ops->set_option(stream, option, value, ptrparam TSRMLS_CC);
    1358             :         }
    1359             : 
    1360      124703 :         if (ret == PHP_STREAM_OPTION_RETURN_NOTIMPL) {
    1361       34078 :                 switch(option) {
    1362             :                         case PHP_STREAM_OPTION_SET_CHUNK_SIZE:
    1363           3 :                                 ret = stream->chunk_size;
    1364           3 :                                 stream->chunk_size = value;
    1365           3 :                                 return ret;
    1366             : 
    1367             :                         case PHP_STREAM_OPTION_READ_BUFFER:
    1368             :                                 /* try to match the buffer mode as best we can */
    1369           9 :                                 if (value == PHP_STREAM_BUFFER_NONE) {
    1370           9 :                                         stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
    1371           0 :                                 } else if (stream->flags & PHP_STREAM_FLAG_NO_BUFFER) {
    1372           0 :                                         stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER;
    1373             :                                 }
    1374           9 :                                 ret = PHP_STREAM_OPTION_RETURN_OK;
    1375             :                                 break;
    1376             : 
    1377             :                         default:
    1378             :                                 ;
    1379             :                 }
    1380             :         }
    1381             : 
    1382      124700 :         return ret;
    1383             : }
    1384             : 
    1385         407 : PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize TSRMLS_DC)
    1386             : {
    1387         407 :         return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize);
    1388             : }
    1389             : 
    1390         541 : PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC TSRMLS_DC)
    1391             : {
    1392         541 :         size_t bcount = 0;
    1393             :         char buf[8192];
    1394             :         int b;
    1395             : 
    1396         541 :         if (php_stream_mmap_possible(stream)) {
    1397             :                 char *p;
    1398             :                 size_t mapped;
    1399             : 
    1400         479 :                 p = php_stream_mmap_range(stream, php_stream_tell(stream), PHP_STREAM_MMAP_ALL, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
    1401             : 
    1402         479 :                 if (p) {
    1403             :                         do {
    1404             :                                 /* output functions return int, so pass in int max */
    1405         373 :                                 if (0 < (b = PHPWRITE(p, MIN(mapped - bcount, INT_MAX)))) {
    1406         373 :                                         bcount += b;
    1407             :                                 }
    1408         373 :                         } while (b > 0 && mapped > bcount);
    1409             : 
    1410         373 :                         php_stream_mmap_unmap_ex(stream, mapped);
    1411             : 
    1412         373 :                         return bcount;
    1413             :                 }
    1414             :         }
    1415             : 
    1416         415 :         while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) {
    1417          79 :                 PHPWRITE(buf, b);
    1418          79 :                 bcount += b;
    1419             :         }
    1420             : 
    1421         168 :         return bcount;
    1422             : }
    1423             : 
    1424             : 
    1425        9953 : PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC)
    1426             : {
    1427        9953 :         size_t ret = 0;
    1428             :         char *ptr;
    1429        9953 :         size_t len = 0, max_len;
    1430        9953 :         int step = CHUNK_SIZE;
    1431        9953 :         int min_room = CHUNK_SIZE / 4;
    1432             :         php_stream_statbuf ssbuf;
    1433             : 
    1434        9953 :         if (maxlen == 0) {
    1435          13 :                 return 0;
    1436             :         }
    1437             : 
    1438        9940 :         if (maxlen == PHP_STREAM_COPY_ALL) {
    1439        9847 :                 maxlen = 0;
    1440             :         }
    1441             : 
    1442        9940 :         if (maxlen > 0) {
    1443          93 :                 ptr = *buf = pemalloc_rel_orig(maxlen + 1, persistent);
    1444         271 :                 while ((len < maxlen) && !php_stream_eof(src)) {
    1445          92 :                         ret = php_stream_read(src, ptr, maxlen - len);
    1446          92 :                         if (!ret) {
    1447           7 :                                 break;
    1448             :                         }
    1449          85 :                         len += ret;
    1450          85 :                         ptr += ret;
    1451             :                 }
    1452          93 :                 if (len) {
    1453          85 :                         *ptr = '\0';
    1454             :                 } else {
    1455           8 :                         pefree(*buf, persistent);
    1456           8 :                         *buf = NULL;
    1457             :                 }
    1458          93 :                 return len;
    1459             :         }
    1460             : 
    1461             :         /* avoid many reallocs by allocating a good sized chunk to begin with, if
    1462             :          * we can.  Note that the stream may be filtered, in which case the stat
    1463             :          * result may be inaccurate, as the filter may inflate or deflate the
    1464             :          * number of bytes that we can read.  In order to avoid an upsize followed
    1465             :          * by a downsize of the buffer, overestimate by the step size (which is
    1466             :          * 2K).  */
    1467       12803 :         if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) {
    1468        2956 :                 max_len = ssbuf.sb.st_size + step;
    1469             :         } else {
    1470        6891 :                 max_len = step;
    1471             :         }
    1472             : 
    1473        9847 :         ptr = *buf = pemalloc_rel_orig(max_len, persistent);
    1474             : 
    1475       24705 :         while((ret = php_stream_read(src, ptr, max_len - len))) {
    1476        5011 :                 len += ret;
    1477        5011 :                 if (len + min_room >= max_len) {
    1478           7 :                         *buf = perealloc_rel_orig(*buf, max_len + step, persistent);
    1479           7 :                         max_len += step;
    1480           7 :                         ptr = *buf + len;
    1481             :                 } else {
    1482        5004 :                         ptr += ret;
    1483             :                 }
    1484             :         }
    1485        9847 :         if (len) {
    1486        3113 :                 *buf = perealloc_rel_orig(*buf, len + 1, persistent);
    1487        3113 :                 (*buf)[len] = '\0';
    1488             :         } else {
    1489        6734 :                 pefree(*buf, persistent);
    1490        6734 :                 *buf = NULL;
    1491             :         }
    1492        9847 :         return len;
    1493             : }
    1494             : 
    1495             : /* Returns SUCCESS/FAILURE and sets *len to the number of bytes moved */
    1496       33581 : PHPAPI int _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC TSRMLS_DC)
    1497             : {
    1498             :         char buf[CHUNK_SIZE];
    1499             :         size_t readchunk;
    1500       33581 :         size_t haveread = 0;
    1501             :         size_t didread, didwrite, towrite;
    1502             :         size_t dummy;
    1503             :         php_stream_statbuf ssbuf;
    1504             : 
    1505       33581 :         if (!len) {
    1506        7385 :                 len = &dummy;
    1507             :         }
    1508             : 
    1509       33581 :         if (maxlen == 0) {
    1510       10683 :                 *len = 0;
    1511       10683 :                 return SUCCESS;
    1512             :         }
    1513             : 
    1514       22898 :         if (maxlen == PHP_STREAM_COPY_ALL) {
    1515       12490 :                 maxlen = 0;
    1516             :         }
    1517             : 
    1518       22898 :         if (php_stream_stat(src, &ssbuf) == 0) {
    1519       27021 :                 if (ssbuf.sb.st_size == 0
    1520             : #ifdef S_ISREG
    1521       27021 :                         && S_ISREG(ssbuf.sb.st_mode)
    1522             : #endif
    1523             :                 ) {
    1524        4123 :                         *len = 0;
    1525        4123 :                         return SUCCESS;
    1526             :                 }
    1527             :         }
    1528             : 
    1529       18775 :         if (php_stream_mmap_possible(src)) {
    1530             :                 char *p;
    1531             :                 size_t mapped;
    1532             : 
    1533       18751 :                 p = php_stream_mmap_range(src, php_stream_tell(src), maxlen, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
    1534             : 
    1535       18751 :                 if (p) {
    1536       14188 :                         didwrite = php_stream_write(dest, p, mapped);
    1537             : 
    1538       14188 :                         php_stream_mmap_unmap_ex(src, mapped);
    1539             : 
    1540       14188 :                         *len = didwrite;
    1541             : 
    1542             :                         /* we've got at least 1 byte to read
    1543             :                          * less than 1 is an error
    1544             :                          * AND read bytes match written */
    1545       14188 :                         if (mapped > 0 && mapped == didwrite) {
    1546       14188 :                                 return SUCCESS;
    1547             :                         }
    1548           0 :                         return FAILURE;
    1549             :                 }
    1550             :         }
    1551             : 
    1552             :         while(1) {
    1553        4620 :                 readchunk = sizeof(buf);
    1554             : 
    1555        4620 :                 if (maxlen && (maxlen - haveread) < readchunk) {
    1556        4579 :                         readchunk = maxlen - haveread;
    1557             :                 }
    1558             : 
    1559        4620 :                 didread = php_stream_read(src, buf, readchunk);
    1560             : 
    1561        4620 :                 if (didread) {
    1562             :                         /* extra paranoid */
    1563             :                         char *writeptr;
    1564             : 
    1565        4609 :                         towrite = didread;
    1566        4609 :                         writeptr = buf;
    1567        4609 :                         haveread += didread;
    1568             : 
    1569       13827 :                         while(towrite) {
    1570        4609 :                                 didwrite = php_stream_write(dest, writeptr, towrite);
    1571        4609 :                                 if (didwrite == 0) {
    1572           0 :                                         *len = haveread - (didread - towrite);
    1573           0 :                                         return FAILURE;
    1574             :                                 }
    1575             : 
    1576        4609 :                                 towrite -= didwrite;
    1577        4609 :                                 writeptr += didwrite;
    1578             :                         }
    1579             :                 } else {
    1580          11 :                         break;
    1581             :                 }
    1582             : 
    1583        4609 :                 if (maxlen - haveread == 0) {
    1584        4576 :                         break;
    1585             :                 }
    1586          33 :         }
    1587             : 
    1588        4587 :         *len = haveread;
    1589             : 
    1590             :         /* we've got at least 1 byte to read.
    1591             :          * less than 1 is an error */
    1592             : 
    1593        4587 :         if (haveread > 0 || src->eof) {
    1594        4587 :                 return SUCCESS;
    1595             :         }
    1596           0 :         return FAILURE;
    1597             : }
    1598             : 
    1599             : /* Returns the number of bytes moved.
    1600             :  * Returns 1 when source len is 0.
    1601             :  * Deprecated in favor of php_stream_copy_to_stream_ex() */
    1602             : ZEND_ATTRIBUTE_DEPRECATED
    1603           0 : PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC)
    1604             : {
    1605             :         size_t len;
    1606           0 :         int ret = _php_stream_copy_to_stream_ex(src, dest, maxlen, &len STREAMS_REL_CC TSRMLS_CC);
    1607           0 :         if (ret == SUCCESS && len == 0 && maxlen != 0) {
    1608           0 :                 return 1;
    1609             :         }
    1610           0 :         return len;
    1611             : }
    1612             : /* }}} */
    1613             : 
    1614             : /* {{{ wrapper init and registration */
    1615             : 
    1616      225307 : static void stream_resource_regular_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    1617             : {
    1618      225307 :         php_stream *stream = (php_stream*)rsrc->ptr;
    1619             :         /* set the return value for pclose */
    1620      225307 :         FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
    1621      225307 : }
    1622             : 
    1623        1158 : static void stream_resource_persistent_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    1624             : {
    1625        1158 :         php_stream *stream = (php_stream*)rsrc->ptr;
    1626        1158 :         FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
    1627        1158 : }
    1628             : 
    1629       21257 : void php_shutdown_stream_hashes(TSRMLS_D)
    1630             : {
    1631       21257 :         if (FG(stream_wrappers)) {
    1632          41 :                 zend_hash_destroy(FG(stream_wrappers));
    1633          41 :                 efree(FG(stream_wrappers));
    1634          41 :                 FG(stream_wrappers) = NULL;
    1635             :         }
    1636             : 
    1637       21257 :         if (FG(stream_filters)) {
    1638           9 :                 zend_hash_destroy(FG(stream_filters));
    1639           9 :                 efree(FG(stream_filters));
    1640           9 :                 FG(stream_filters) = NULL;
    1641             :         }
    1642             :     
    1643       21257 :     if (FG(wrapper_errors)) {
    1644          68 :                 zend_hash_destroy(FG(wrapper_errors));
    1645          68 :                 efree(FG(wrapper_errors));
    1646          68 :                 FG(wrapper_errors) = NULL;
    1647             :     }
    1648       21257 : }
    1649             : 
    1650       21265 : int php_init_stream_wrappers(int module_number TSRMLS_DC)
    1651             : {
    1652       21265 :         le_stream = zend_register_list_destructors_ex(stream_resource_regular_dtor, NULL, "stream", module_number);
    1653       21265 :         le_pstream = zend_register_list_destructors_ex(NULL, stream_resource_persistent_dtor, "persistent stream", module_number);
    1654             : 
    1655             :         /* Filters are cleaned up by the streams they're attached to */
    1656       21265 :         le_stream_filter = zend_register_list_destructors_ex(NULL, NULL, "stream filter", module_number);
    1657             : 
    1658      148855 :         return (
    1659             :                         zend_hash_init(&url_stream_wrappers_hash, 0, NULL, NULL, 1) == SUCCESS
    1660       21265 :                         &&
    1661             :                         zend_hash_init(php_get_stream_filters_hash_global(), 0, NULL, NULL, 1) == SUCCESS
    1662       21265 :                         &&
    1663             :                         zend_hash_init(php_stream_xport_get_hash(), 0, NULL, NULL, 1) == SUCCESS
    1664       21265 :                         &&
    1665             :                         php_stream_xport_register("tcp", php_stream_generic_socket_factory TSRMLS_CC) == SUCCESS
    1666       21265 :                         &&
    1667             :                         php_stream_xport_register("udp", php_stream_generic_socket_factory TSRMLS_CC) == SUCCESS
    1668             : #if defined(AF_UNIX) && !(defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE))
    1669       21265 :                         &&
    1670             :                         php_stream_xport_register("unix", php_stream_generic_socket_factory TSRMLS_CC) == SUCCESS
    1671       21265 :                         &&
    1672       21265 :                         php_stream_xport_register("udg", php_stream_generic_socket_factory TSRMLS_CC) == SUCCESS
    1673             : #endif
    1674             :                 ) ? SUCCESS : FAILURE;
    1675             : }
    1676             : 
    1677       21298 : int php_shutdown_stream_wrappers(int module_number TSRMLS_DC)
    1678             : {
    1679       21298 :         zend_hash_destroy(&url_stream_wrappers_hash);
    1680       21298 :         zend_hash_destroy(php_get_stream_filters_hash_global());
    1681       21298 :         zend_hash_destroy(php_stream_xport_get_hash());
    1682       21298 :         return SUCCESS;
    1683             : }
    1684             : 
    1685             : /* Validate protocol scheme names during registration
    1686             :  * Must conform to /^[a-zA-Z0-9+.-]+$/
    1687             :  */
    1688      255230 : static inline int php_stream_wrapper_scheme_validate(const char *protocol, unsigned int protocol_len)
    1689             : {
    1690             :         unsigned int i;
    1691             : 
    1692     1637720 :         for(i = 0; i < protocol_len; i++) {
    1693     1510080 :                 if (!isalnum((int)protocol[i]) &&
    1694       42530 :                         protocol[i] != '+' &&
    1695       42530 :                         protocol[i] != '-' &&
    1696       42530 :                         protocol[i] != '.') {
    1697           0 :                         return FAILURE;
    1698             :                 }
    1699             :         }
    1700             : 
    1701      255230 :         return SUCCESS;
    1702             : }
    1703             : 
    1704             : /* API for registering GLOBAL wrappers */
    1705      255180 : PHPAPI int php_register_url_stream_wrapper(const char *protocol, php_stream_wrapper *wrapper TSRMLS_DC)
    1706             : {
    1707      255180 :         unsigned int protocol_len = strlen(protocol);
    1708             : 
    1709      255180 :         if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) {
    1710           0 :                 return FAILURE;
    1711             :         }
    1712             : 
    1713      255180 :         return zend_hash_add(&url_stream_wrappers_hash, protocol, protocol_len + 1, &wrapper, sizeof(wrapper), NULL);
    1714             : }
    1715             : 
    1716      191682 : PHPAPI int php_unregister_url_stream_wrapper(const char *protocol TSRMLS_DC)
    1717             : {
    1718      191682 :         return zend_hash_del(&url_stream_wrappers_hash, protocol, strlen(protocol) + 1);
    1719             : }
    1720             : 
    1721          41 : static void clone_wrapper_hash(TSRMLS_D)
    1722             : {
    1723             :         php_stream_wrapper *tmp;
    1724             : 
    1725          41 :         ALLOC_HASHTABLE(FG(stream_wrappers));
    1726          41 :         zend_hash_init(FG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 1);
    1727          41 :         zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL, &tmp, sizeof(tmp));
    1728          41 : }
    1729             : 
    1730             : /* API for registering VOLATILE wrappers */
    1731          50 : PHPAPI int php_register_url_stream_wrapper_volatile(const char *protocol, php_stream_wrapper *wrapper TSRMLS_DC)
    1732             : {
    1733          50 :         unsigned int protocol_len = strlen(protocol);
    1734             : 
    1735          50 :         if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) {
    1736           0 :                 return FAILURE;
    1737             :         }
    1738             : 
    1739          50 :         if (!FG(stream_wrappers)) {
    1740          40 :                 clone_wrapper_hash(TSRMLS_C);
    1741             :         }
    1742             : 
    1743          50 :         return zend_hash_add(FG(stream_wrappers), protocol, protocol_len + 1, &wrapper, sizeof(wrapper), NULL);
    1744             : }
    1745             : 
    1746           2 : PHPAPI int php_unregister_url_stream_wrapper_volatile(const char *protocol TSRMLS_DC)
    1747             : {
    1748           2 :         if (!FG(stream_wrappers)) {
    1749           1 :                 clone_wrapper_hash(TSRMLS_C);
    1750             :         }
    1751             : 
    1752           2 :         return zend_hash_del(FG(stream_wrappers), protocol, strlen(protocol) + 1);
    1753             : }
    1754             : /* }}} */
    1755             : 
    1756             : /* {{{ php_stream_locate_url_wrapper */
    1757      496989 : PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options TSRMLS_DC)
    1758             : {
    1759      496989 :         HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
    1760      496989 :         php_stream_wrapper **wrapperpp = NULL;
    1761      496989 :         const char *p, *protocol = NULL;
    1762      496989 :         int n = 0;
    1763             : 
    1764      496989 :         if (path_for_open) {
    1765      265887 :                 *path_for_open = (char*)path;
    1766             :         }
    1767             : 
    1768      496989 :         if (options & IGNORE_URL) {
    1769        5815 :                 return (options & STREAM_LOCATE_WRAPPERS_ONLY) ? NULL : &php_plain_files_wrapper;
    1770             :         }
    1771             : 
    1772      719371 :         for (p = path; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
    1773      228197 :                 n++;
    1774             :         }
    1775             : 
    1776      556303 :         if ((*p == ':') && (n > 1) && (!strncmp("//", p+1, 2) || (n == 4 && !memcmp("data:", path, 5)))) {
    1777       65129 :                 protocol = path;
    1778      426045 :         } else if (n == 5 && strncasecmp(path, "zlib:", 5) == 0) {
    1779             :                 /* BC with older php scripts and zlib wrapper */
    1780           0 :                 protocol = "compress.zlib";
    1781           0 :                 n = 13;
    1782           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Use of \"zlib:\" wrapper is deprecated; please use \"compress.zlib://\" instead");
    1783             :         }
    1784             : 
    1785      491174 :         if (protocol) {
    1786       65129 :                 char *tmp = estrndup(protocol, n);
    1787       65129 :                 if (FAILURE == zend_hash_find(wrapper_hash, (char*)tmp, n + 1, (void**)&wrapperpp)) {
    1788           3 :                         php_strtolower(tmp, n);
    1789           3 :                         if (FAILURE == zend_hash_find(wrapper_hash, (char*)tmp, n + 1, (void**)&wrapperpp)) {
    1790             :                                 char wrapper_name[32];
    1791             : 
    1792           2 :                                 if (n >= sizeof(wrapper_name)) {
    1793           0 :                                         n = sizeof(wrapper_name) - 1;
    1794             :                                 }
    1795           2 :                                 PHP_STRLCPY(wrapper_name, protocol, sizeof(wrapper_name), n);
    1796             : 
    1797           2 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find the wrapper \"%s\" - did you forget to enable it when you configured PHP?", wrapper_name);
    1798             : 
    1799           2 :                                 wrapperpp = NULL;
    1800           2 :                                 protocol = NULL;
    1801             :                         }
    1802             :                 }
    1803       65129 :                 efree(tmp);
    1804             :         }
    1805             :         /* TODO: curl based streams probably support file:// properly */
    1806      491174 :         if (!protocol || !strncasecmp(protocol, "file", n))   {
    1807             :                 /* fall back on regular file access */
    1808      426101 :                 php_stream_wrapper *plain_files_wrapper = &php_plain_files_wrapper;
    1809             : 
    1810      426101 :                 if (protocol) {
    1811          54 :                         int localhost = 0;
    1812             : 
    1813          54 :                         if (!strncasecmp(path, "file://localhost/", 17)) {
    1814           0 :                                 localhost = 1;
    1815             :                         }
    1816             : 
    1817             : #ifdef PHP_WIN32
    1818             :                         if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/' && path[n+4] != ':')        {
    1819             : #else
    1820          54 :                         if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/') {
    1821             : #endif
    1822          18 :                                 if (options & REPORT_ERRORS) {
    1823          12 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "remote host file access not supported, %s", path);
    1824             :                                 }
    1825          18 :                                 return NULL;
    1826             :                         }
    1827             : 
    1828          36 :                         if (path_for_open) {
    1829             :                                 /* skip past protocol and :/, but handle windows correctly */
    1830          35 :                                 *path_for_open = (char*)path + n + 1;
    1831          35 :                                 if (localhost == 1) {
    1832           0 :                                         (*path_for_open) += 11;
    1833             :                                 }
    1834         107 :                                 while (*(++*path_for_open)=='/');
    1835             : #ifdef PHP_WIN32
    1836             :                                 if (*(*path_for_open + 1) != ':')
    1837             : #endif
    1838          35 :                                         (*path_for_open)--;
    1839             :                         }
    1840             :                 }
    1841             : 
    1842      426083 :                 if (options & STREAM_LOCATE_WRAPPERS_ONLY) {
    1843         578 :                         return NULL;
    1844             :                 }
    1845             : 
    1846      425505 :                 if (FG(stream_wrappers)) {
    1847             :                 /* The file:// wrapper may have been disabled/overridden */
    1848             : 
    1849          15 :                         if (wrapperpp) {
    1850             :                                 /* It was found so go ahead and provide it */
    1851           3 :                                 return *wrapperpp;
    1852             :                         }
    1853             : 
    1854             :                         /* Check again, the original check might have not known the protocol name */
    1855          12 :                         if (zend_hash_find(wrapper_hash, "file", sizeof("file"), (void**)&wrapperpp) == SUCCESS) {
    1856          11 :                                 return *wrapperpp;
    1857             :                         }
    1858             : 
    1859           1 :                         if (options & REPORT_ERRORS) {
    1860           1 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "file:// wrapper is disabled in the server configuration");
    1861             :                         }
    1862           1 :                         return NULL;
    1863             :                 }
    1864             : 
    1865      425490 :                 return plain_files_wrapper;
    1866             :         }
    1867             : 
    1868       65624 :         if (wrapperpp && (*wrapperpp)->is_url &&
    1869         140 :         (options & STREAM_DISABLE_URL_PROTECTION) == 0 &&
    1870         140 :             (!PG(allow_url_fopen) ||
    1871         135 :              (((options & STREAM_OPEN_FOR_INCLUDE) ||
    1872         136 :                PG(in_user_include)) && !PG(allow_url_include)))) {
    1873          10 :                 if (options & REPORT_ERRORS) {
    1874             :                         /* protocol[n] probably isn't '\0' */
    1875           7 :                         char *protocol_dup = estrndup(protocol, n);
    1876           7 :                         if (!PG(allow_url_fopen)) {
    1877           4 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_fopen=0", protocol_dup);
    1878             :                         } else {
    1879           3 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_include=0", protocol_dup);
    1880             :                         }
    1881           7 :                         efree(protocol_dup);
    1882             :                 }
    1883          10 :                 return NULL;
    1884             :         }
    1885             : 
    1886       65063 :         return *wrapperpp;
    1887             : }
    1888             : /* }}} */
    1889             : 
    1890             : /* {{{ _php_stream_mkdir
    1891             :  */
    1892        1696 : PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context TSRMLS_DC)
    1893             : {
    1894        1696 :         php_stream_wrapper *wrapper = NULL;
    1895             : 
    1896        1696 :         wrapper = php_stream_locate_url_wrapper(path, NULL, 0 TSRMLS_CC);
    1897        1696 :         if (!wrapper || !wrapper->wops || !wrapper->wops->stream_mkdir) {
    1898           1 :                 return 0;
    1899             :         }
    1900             : 
    1901        1695 :         return wrapper->wops->stream_mkdir(wrapper, path, mode, options, context TSRMLS_CC);
    1902             : }
    1903             : /* }}} */
    1904             : 
    1905             : /* {{{ _php_stream_rmdir
    1906             :  */
    1907        1717 : PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context TSRMLS_DC)
    1908             : {
    1909        1717 :         php_stream_wrapper *wrapper = NULL;
    1910             : 
    1911        1717 :         wrapper = php_stream_locate_url_wrapper(path, NULL, 0 TSRMLS_CC);
    1912        1717 :         if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) {
    1913           1 :                 return 0;
    1914             :         }
    1915             : 
    1916        1716 :         return wrapper->wops->stream_rmdir(wrapper, path, options, context TSRMLS_CC);
    1917             : }
    1918             : /* }}} */
    1919             : 
    1920             : /* {{{ _php_stream_stat_path */
    1921       60577 : PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf *ssb, php_stream_context *context TSRMLS_DC)
    1922             : {
    1923       60577 :         php_stream_wrapper *wrapper = NULL;
    1924       60577 :         const char *path_to_open = path;
    1925             :         int ret;
    1926             : 
    1927       60577 :         if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
    1928             :                 /* Try to hit the cache first */
    1929       60403 :                 if (flags & PHP_STREAM_URL_STAT_LINK) {
    1930        2629 :                         if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
    1931           8 :                                 memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
    1932           8 :                                 return 0;
    1933             :                         }
    1934             :                 } else {
    1935       57774 :                         if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
    1936        6051 :                                 memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
    1937        6051 :                                 return 0;
    1938             :                         }
    1939             :                 }
    1940             :         }
    1941             : 
    1942       54518 :         wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0 TSRMLS_CC);
    1943       54518 :         if (wrapper && wrapper->wops->url_stat) {
    1944       54501 :                 ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context TSRMLS_CC);
    1945       54501 :                 if (ret == 0) {
    1946       52546 :                         if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
    1947             :                                 /* Drop into cache */
    1948       52535 :                                 if (flags & PHP_STREAM_URL_STAT_LINK) {
    1949        2599 :                                         if (BG(CurrentLStatFile)) {
    1950        2520 :                                                 efree(BG(CurrentLStatFile));
    1951             :                                         }
    1952        2599 :                                         BG(CurrentLStatFile) = estrdup(path);
    1953        2599 :                                         memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
    1954             :                                 } else {
    1955       49936 :                                         if (BG(CurrentStatFile)) {
    1956       28366 :                                                 efree(BG(CurrentStatFile));
    1957             :                                         }
    1958       49936 :                                         BG(CurrentStatFile) = estrdup(path);
    1959       49936 :                                         memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
    1960             :                                 }
    1961             :                         }
    1962             :                 }
    1963       54501 :                 return ret;
    1964             :         }
    1965          17 :         return -1;
    1966             : }
    1967             : /* }}} */
    1968             : 
    1969             : /* {{{ php_stream_opendir */
    1970        1409 : PHPAPI php_stream *_php_stream_opendir(const char *path, int options,
    1971             :                 php_stream_context *context STREAMS_DC TSRMLS_DC)
    1972             : {
    1973        1409 :         php_stream *stream = NULL;
    1974        1409 :         php_stream_wrapper *wrapper = NULL;
    1975             :         const char *path_to_open;
    1976             : 
    1977        1409 :         if (!path || !*path) {
    1978          17 :                 return NULL;
    1979             :         }
    1980             : 
    1981        1392 :         path_to_open = path;
    1982             : 
    1983        1392 :         wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options TSRMLS_CC);
    1984             : 
    1985        2783 :         if (wrapper && wrapper->wops->dir_opener) {
    1986        1391 :                 stream = wrapper->wops->dir_opener(wrapper,
    1987             :                                 path_to_open, "r", options ^ REPORT_ERRORS, NULL,
    1988             :                                 context STREAMS_REL_CC TSRMLS_CC);
    1989             : 
    1990        1391 :                 if (stream) {
    1991        1297 :                         stream->wrapper = wrapper;
    1992        1297 :                         stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR;
    1993             :                 }
    1994           1 :         } else if (wrapper) {
    1995           1 :                 php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS TSRMLS_CC, "not implemented");
    1996             :         }
    1997        1392 :         if (stream == NULL && (options & REPORT_ERRORS)) {
    1998          95 :                 php_stream_display_wrapper_errors(wrapper, path, "failed to open dir" TSRMLS_CC);
    1999             :         }
    2000        1392 :         php_stream_tidy_wrapper_error_log(wrapper TSRMLS_CC);
    2001             : 
    2002        1392 :         return stream;
    2003             : }
    2004             : /* }}} */
    2005             : 
    2006             : /* {{{ _php_stream_readdir */
    2007       37918 : PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent TSRMLS_DC)
    2008             : {
    2009             : 
    2010       37918 :         if (sizeof(php_stream_dirent) == php_stream_read(dirstream, (char*)ent, sizeof(php_stream_dirent))) {
    2011       36961 :                 return ent;
    2012             :         }
    2013             : 
    2014         957 :         return NULL;
    2015             : }
    2016             : /* }}} */
    2017             : 
    2018             : /* {{{ php_stream_open_wrapper_ex */
    2019      144057 : PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options,
    2020             :                 char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
    2021             : {
    2022      144057 :         php_stream *stream = NULL;
    2023      144057 :         php_stream_wrapper *wrapper = NULL;
    2024             :         const char *path_to_open;
    2025      144057 :         int persistent = options & STREAM_OPEN_PERSISTENT;
    2026      144057 :         char *resolved_path = NULL;
    2027      144057 :         char *copy_of_path = NULL;
    2028             : 
    2029      144057 :         if (opened_path) {
    2030       14429 :                 *opened_path = NULL;
    2031             :         }
    2032             : 
    2033      144057 :         if (!path || !*path) {
    2034          66 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filename cannot be empty");
    2035          66 :                 return NULL;
    2036             :         }
    2037             : 
    2038      143991 :         if (options & USE_PATH) {
    2039       10639 :                 resolved_path = zend_resolve_path(path, strlen(path) TSRMLS_CC);
    2040       10639 :                 if (resolved_path) {
    2041       10276 :                         path = resolved_path;
    2042             :                         /* we've found this file, don't re-check include_path or run realpath */
    2043       10276 :                         options |= STREAM_ASSUME_REALPATH;
    2044       10276 :                         options &= ~USE_PATH;
    2045             :                 }
    2046             :         }
    2047             : 
    2048      143991 :         path_to_open = path;
    2049             : 
    2050      143991 :         wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options TSRMLS_CC);
    2051      143991 :         if (options & STREAM_USE_URL && (!wrapper || !wrapper->is_url)) {
    2052           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function may only be used against URLs");
    2053           0 :                 if (resolved_path) {
    2054           0 :                         efree(resolved_path);
    2055             :                 }
    2056           0 :                 return NULL;
    2057             :         }
    2058             : 
    2059      143991 :         if (wrapper) {
    2060      143971 :                 if (!wrapper->wops->stream_opener) {
    2061           0 :                         php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS TSRMLS_CC,
    2062             :                                         "wrapper does not support stream open");
    2063             :                 } else {
    2064      143971 :                         stream = wrapper->wops->stream_opener(wrapper,
    2065             :                                 path_to_open, mode, options ^ REPORT_ERRORS,
    2066             :                                 opened_path, context STREAMS_REL_CC TSRMLS_CC);
    2067             :                 }
    2068             : 
    2069             :                 /* if the caller asked for a persistent stream but the wrapper did not
    2070             :                  * return one, force an error here */
    2071      143970 :                 if (stream && (options & STREAM_OPEN_PERSISTENT) && !stream->is_persistent) {
    2072           0 :                         php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS TSRMLS_CC,
    2073             :                                         "wrapper does not support persistent streams");
    2074           0 :                         php_stream_close(stream);
    2075           0 :                         stream = NULL;
    2076             :                 }
    2077             : 
    2078      143970 :                 if (stream) {
    2079      143168 :                         stream->wrapper = wrapper;
    2080             :                 }
    2081             :         }
    2082             : 
    2083      143990 :         if (stream) {
    2084      143168 :                 if (opened_path && !*opened_path && resolved_path) {
    2085           2 :                         *opened_path = resolved_path;
    2086           2 :                         resolved_path = NULL;
    2087             :                 }
    2088      143168 :                 if (stream->orig_path) {
    2089           1 :                         pefree(stream->orig_path, persistent);
    2090             :                 }
    2091      143168 :                 copy_of_path = pestrdup(path, persistent);
    2092      143168 :                 stream->orig_path = copy_of_path;
    2093             : #if ZEND_DEBUG
    2094             :                 stream->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
    2095             :                 stream->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
    2096             : #endif
    2097             :         }
    2098             : 
    2099      143990 :         if (stream != NULL && (options & STREAM_MUST_SEEK)) {
    2100             :                 php_stream *newstream;
    2101             : 
    2102        9133 :                 switch(php_stream_make_seekable_rel(stream, &newstream,
    2103             :                                         (options & STREAM_WILL_CAST)
    2104             :                                                 ? PHP_STREAM_PREFER_STDIO : PHP_STREAM_NO_PREFERENCE)) {
    2105             :                         case PHP_STREAM_UNCHANGED:
    2106        9133 :                                 if (resolved_path) {
    2107          41 :                                         efree(resolved_path);
    2108             :                                 }
    2109        9133 :                                 return stream;
    2110             :                         case PHP_STREAM_RELEASED:
    2111           0 :                                 if (newstream->orig_path) {
    2112           0 :                                         pefree(newstream->orig_path, persistent);
    2113             :                                 }
    2114           0 :                                 newstream->orig_path = pestrdup(path, persistent);
    2115           0 :                                 if (resolved_path) {
    2116           0 :                                         efree(resolved_path);
    2117             :                                 }
    2118           0 :                                 return newstream;
    2119             :                         default:
    2120           0 :                                 php_stream_close(stream);
    2121           0 :                                 stream = NULL;
    2122           0 :                                 if (options & REPORT_ERRORS) {
    2123           0 :                                         char *tmp = estrdup(path);
    2124           0 :                                         php_strip_url_passwd(tmp);
    2125           0 :                                         php_error_docref1(NULL TSRMLS_CC, tmp, E_WARNING, "could not make seekable - %s",
    2126             :                                                         tmp);
    2127           0 :                                         efree(tmp);
    2128             : 
    2129           0 :                                         options ^= REPORT_ERRORS;
    2130             :                                 }
    2131             :                 }
    2132             :         }
    2133             : 
    2134      134857 :         if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && strchr(mode, 'a') && stream->position == 0) {
    2135        1568 :                 off_t newpos = 0;
    2136             : 
    2137             :                 /* if opened for append, we need to revise our idea of the initial file position */
    2138        1568 :                 if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos TSRMLS_CC)) {
    2139        1568 :                         stream->position = newpos;
    2140             :                 }
    2141             :         }
    2142             : 
    2143      134857 :         if (stream == NULL && (options & REPORT_ERRORS)) {
    2144         410 :                 php_stream_display_wrapper_errors(wrapper, path, "failed to open stream" TSRMLS_CC);
    2145         410 :                 if (opened_path && *opened_path) {
    2146           0 :                         efree(*opened_path);
    2147           0 :                         *opened_path = NULL;
    2148             :                 }
    2149             :         }
    2150      134857 :         php_stream_tidy_wrapper_error_log(wrapper TSRMLS_CC);
    2151             : #if ZEND_DEBUG
    2152             :         if (stream == NULL && copy_of_path != NULL) {
    2153             :                 pefree(copy_of_path, persistent);
    2154             :         }
    2155             : #endif
    2156      134857 :         if (resolved_path) {
    2157       10233 :                 efree(resolved_path);
    2158             :         }
    2159      134857 :         return stream;
    2160             : }
    2161             : /* }}} */
    2162             : 
    2163             : /* {{{ context API */
    2164        3753 : PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context TSRMLS_DC)
    2165             : {
    2166        3753 :         php_stream_context *oldcontext = stream->context;
    2167             : 
    2168        3753 :         stream->context = context;
    2169             : 
    2170        3753 :         if (context) {
    2171         962 :                 zend_list_addref(context->rsrc_id);
    2172             :         }
    2173        3753 :         if (oldcontext) {
    2174          59 :                 zend_list_delete(oldcontext->rsrc_id);
    2175             :         }
    2176             : 
    2177        3753 :         return oldcontext;
    2178             : }
    2179             : 
    2180           0 : PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
    2181             :                 char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC)
    2182             : {
    2183           0 :         if (context && context->notifier)
    2184           0 :                 context->notifier->func(context, notifycode, severity, xmsg, xcode, bytes_sofar, bytes_max, ptr TSRMLS_CC);
    2185           0 : }
    2186             : 
    2187        4780 : PHPAPI void php_stream_context_free(php_stream_context *context)
    2188             : {
    2189        4780 :         if (context->options) {
    2190           0 :                 zval_ptr_dtor(&context->options);
    2191           0 :                 context->options = NULL;
    2192             :         }
    2193        4780 :         if (context->notifier) {
    2194           1 :                 php_stream_notification_free(context->notifier);
    2195           1 :                 context->notifier = NULL;
    2196             :         }
    2197        4780 :         efree(context);
    2198        4780 : }
    2199             : 
    2200        4749 : PHPAPI php_stream_context *php_stream_context_alloc(TSRMLS_D)
    2201             : {
    2202             :         php_stream_context *context;
    2203             : 
    2204        4749 :         context = ecalloc(1, sizeof(php_stream_context));
    2205        4749 :         context->notifier = NULL;
    2206        4749 :         MAKE_STD_ZVAL(context->options);
    2207        4749 :         array_init(context->options);
    2208             : 
    2209        4749 :         context->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, context, php_le_stream_context(TSRMLS_C));
    2210        4749 :         return context;
    2211             : }
    2212             : 
    2213           2 : PHPAPI php_stream_notifier *php_stream_notification_alloc(void)
    2214             : {
    2215           2 :         return ecalloc(1, sizeof(php_stream_notifier));
    2216             : }
    2217             : 
    2218           2 : PHPAPI void php_stream_notification_free(php_stream_notifier *notifier)
    2219             : {
    2220           2 :         if (notifier->dtor) {
    2221           2 :                 notifier->dtor(notifier);
    2222             :         }
    2223           2 :         efree(notifier);
    2224           2 : }
    2225             : 
    2226        4877 : PHPAPI int php_stream_context_get_option(php_stream_context *context,
    2227             :                 const char *wrappername, const char *optionname, zval ***optionvalue)
    2228             : {
    2229             :         zval **wrapperhash;
    2230             : 
    2231        4877 :         if (FAILURE == zend_hash_find(Z_ARRVAL_P(context->options), (char*)wrappername, strlen(wrappername)+1, (void**)&wrapperhash)) {
    2232        1797 :                 return FAILURE;
    2233             :         }
    2234        3080 :         return zend_hash_find(Z_ARRVAL_PP(wrapperhash), (char*)optionname, strlen(optionname)+1, (void**)optionvalue);
    2235             : }
    2236             : 
    2237        1746 : PHPAPI int php_stream_context_set_option(php_stream_context *context,
    2238             :                 const char *wrappername, const char *optionname, zval *optionvalue)
    2239             : {
    2240             :         zval **wrapperhash;
    2241             :         zval *category, *copied_val;
    2242             : 
    2243        1746 :         ALLOC_INIT_ZVAL(copied_val);
    2244        1746 :         *copied_val = *optionvalue;
    2245        1746 :         zval_copy_ctor(copied_val);
    2246        1746 :         INIT_PZVAL(copied_val);
    2247             : 
    2248        1746 :         if (FAILURE == zend_hash_find(Z_ARRVAL_P(context->options), (char*)wrappername, strlen(wrappername)+1, (void**)&wrapperhash)) {
    2249         851 :                 MAKE_STD_ZVAL(category);
    2250         851 :                 array_init(category);
    2251         851 :                 if (FAILURE == zend_hash_update(Z_ARRVAL_P(context->options), (char*)wrappername, strlen(wrappername)+1, (void**)&category, sizeof(zval *), NULL)) {
    2252           0 :                         return FAILURE;
    2253             :                 }
    2254             : 
    2255         851 :                 wrapperhash = &category;
    2256             :         }
    2257        1746 :         return zend_hash_update(Z_ARRVAL_PP(wrapperhash), (char*)optionname, strlen(optionname)+1, (void**)&copied_val, sizeof(zval *), NULL);
    2258             : }
    2259             : /* }}} */
    2260             : 
    2261             : /* {{{ php_stream_dirent_alphasort
    2262             :  */
    2263         844 : PHPAPI int php_stream_dirent_alphasort(const char **a, const char **b)
    2264             : {
    2265         844 :         return strcoll(*a, *b);
    2266             : }
    2267             : /* }}} */
    2268             : 
    2269             : /* {{{ php_stream_dirent_alphasortr
    2270             :  */
    2271          30 : PHPAPI int php_stream_dirent_alphasortr(const char **a, const char **b)
    2272             : {
    2273          30 :         return strcoll(*b, *a);
    2274             : }
    2275             : /* }}} */
    2276             : 
    2277             : /* {{{ php_stream_scandir
    2278             :  */
    2279          81 : PHPAPI int _php_stream_scandir(const char *dirname, char **namelist[], int flags, php_stream_context *context,
    2280             :                           int (*compare) (const char **a, const char **b) TSRMLS_DC)
    2281             : {
    2282             :         php_stream *stream;
    2283             :         php_stream_dirent sdp;
    2284          81 :         char **vector = NULL;
    2285          81 :         unsigned int vector_size = 0;
    2286          81 :         unsigned int nfiles = 0;
    2287             : 
    2288          81 :         if (!namelist) {
    2289           0 :                 return FAILURE;
    2290             :         }
    2291             : 
    2292          81 :         stream = php_stream_opendir(dirname, REPORT_ERRORS, context);
    2293          81 :         if (!stream) {
    2294          31 :                 return FAILURE;
    2295             :         }
    2296             : 
    2297         378 :         while (php_stream_readdir(stream, &sdp)) {
    2298         278 :                 if (nfiles == vector_size) {
    2299          55 :                         if (vector_size == 0) {
    2300          50 :                                 vector_size = 10;
    2301             :                         } else {
    2302           5 :                                 if(vector_size*2 < vector_size) {
    2303             :                                         /* overflow */
    2304           0 :                                         php_stream_closedir(stream);
    2305           0 :                                         efree(vector);
    2306           0 :                                         return FAILURE;
    2307             :                                 }
    2308           5 :                                 vector_size *= 2;
    2309             :                         }
    2310          55 :                         vector = (char **) safe_erealloc(vector, vector_size, sizeof(char *), 0);
    2311             :                 }
    2312             : 
    2313         278 :                 vector[nfiles] = estrdup(sdp.d_name);
    2314             : 
    2315         278 :                 nfiles++;
    2316         278 :                 if(vector_size < 10 || nfiles == 0) {
    2317             :                         /* overflow */
    2318           0 :                         php_stream_closedir(stream);
    2319           0 :                         efree(vector);
    2320           0 :                         return FAILURE;
    2321             :                 }
    2322             :         }
    2323          50 :         php_stream_closedir(stream);
    2324             : 
    2325          50 :         *namelist = vector;
    2326             : 
    2327          50 :         if (nfiles > 0 && compare) {
    2328          49 :                 qsort(*namelist, nfiles, sizeof(char *), (int(*)(const void *, const void *))compare);
    2329             :         }
    2330          50 :         return nfiles;
    2331             : }
    2332             : /* }}} */
    2333             : 
    2334             : /*
    2335             :  * Local variables:
    2336             :  * tab-width: 4
    2337             :  * c-basic-offset: 4
    2338             :  * End:
    2339             :  * vim600: noet sw=4 ts=4 fdm=marker
    2340             :  * vim<600: noet sw=4 ts=4
    2341             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:19 +0000 (28 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.