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