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