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

Generated by: LCOV version 1.10

Generated at Tue, 02 Sep 2014 09:43:10 +0000 (11 hours ago)

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