From 90c1b826c1bff7722f69de49ca6a5afd5f8d52b5 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 3 Mar 2025 20:22:35 +0000 Subject: [PATCH 1/3] Move definition of php_le_stream_context From ext/standard/file.h to main/streams/php_stream_context.h This reduces some dependency of main/ on ext/standard --- ext/standard/file.h | 1 - main/streams/php_stream_context.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/standard/file.h b/ext/standard/file.h index 3a9cf1435b143..f8faebd028293 100644 --- a/ext/standard/file.h +++ b/ext/standard/file.h @@ -36,7 +36,6 @@ PHPAPI PHP_FUNCTION(fpassthru); PHP_MINIT_FUNCTION(user_streams); -PHPAPI int php_le_stream_context(void); PHPAPI zend_result php_copy_file(const char *src, const char *dest); PHPAPI zend_result php_copy_file_ex(const char *src, const char *dest, int src_flags); PHPAPI zend_result php_copy_file_ctx(const char *src, const char *dest, int src_flags, php_stream_context *ctx); diff --git a/main/streams/php_stream_context.h b/main/streams/php_stream_context.h index d4ebe29bc162e..677bf14befeb2 100644 --- a/main/streams/php_stream_context.h +++ b/main/streams/php_stream_context.h @@ -53,6 +53,7 @@ struct _php_stream_context { }; BEGIN_EXTERN_C() +PHPAPI int php_le_stream_context(void); PHPAPI void php_stream_context_free(php_stream_context *context); PHPAPI php_stream_context *php_stream_context_alloc(void); PHPAPI zval *php_stream_context_get_option(php_stream_context *context, From f7ced6561d2b5dde6157ba3a0907949d17f0bc48 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 3 Mar 2025 20:23:27 +0000 Subject: [PATCH 2/3] main/streams: Add a helper macro to retrieve default context --- main/streams/php_stream_context.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main/streams/php_stream_context.h b/main/streams/php_stream_context.h index 677bf14befeb2..a5325a94642c9 100644 --- a/main/streams/php_stream_context.h +++ b/main/streams/php_stream_context.h @@ -25,14 +25,17 @@ typedef void (*php_stream_notification_func)(php_stream_context *context, #define PHP_STREAM_NOTIFIER_PROGRESS 1 +/* TODO: Remove dependence on ext/standard/file.h for the default context global */ +#define php_stream_context_get_default(without_context) \ + (without_context) ? NULL : FG(default_context) ? FG(default_context) : \ + (FG(default_context) = php_stream_context_alloc()) + /* Attempt to fetch context from the zval passed, If no context was passed, use the default context The default context has not yet been created, do it now. */ #define php_stream_context_from_zval(zcontext, nocontext) ( \ (zcontext) ? zend_fetch_resource_ex(zcontext, "Stream-Context", php_le_stream_context()) : \ - (nocontext) ? NULL : \ - FG(default_context) ? FG(default_context) : \ - (FG(default_context) = php_stream_context_alloc()) ) + php_stream_context_get_default(nocontext)) #define php_stream_context_to_zval(context, zval) { ZVAL_RES(zval, (context)->res); GC_ADDREF((context)->res); } From 6cc653ea734682b951c8a4e998b47ea43306d65d Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Mon, 17 Mar 2025 14:29:41 +0000 Subject: [PATCH 3/3] main/streams: Add a new helper function to get a php_stream from a zval without errors This is intended to replace the few manual usages of zend_fetch_resource2_ex() to fetch a php_stream from a zval. This will simplify the conversion from resource to object for streams when this actually happens. --- main/php_streams.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main/php_streams.h b/main/php_streams.h index 80cb96951ee14..adc0811c92c23 100644 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -285,6 +285,10 @@ END_EXTERN_C() #define php_stream_from_res_no_verify(xstr, pzval) (xstr) = (php_stream*)zend_fetch_resource2((res), "stream", php_file_le_stream(), php_file_le_pstream()) #define php_stream_from_zval_no_verify(xstr, pzval) (xstr) = (php_stream*)zend_fetch_resource2_ex((pzval), "stream", php_file_le_stream(), php_file_le_pstream()) +static zend_always_inline php_stream* php_stream_from_zval_no_verify_no_error(zval *zval) { + return (php_stream*)zend_fetch_resource2_ex(zval, NULL, php_file_le_stream(), php_file_le_pstream()); +} + BEGIN_EXTERN_C() static zend_always_inline bool php_stream_zend_parse_arg_into_stream(zval *arg, php_stream **destination_stream_ptr, bool check_null)