xref: /PHP-8.2/main/php_output.h (revision a1a8e903)
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    | Author: Michael Wallner <mike@php.net>                               |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifndef PHP_OUTPUT_H
18 #define PHP_OUTPUT_H
19 
20 #define PHP_OUTPUT_NEWAPI 1
21 
22 /* handler ops */
23 #define PHP_OUTPUT_HANDLER_WRITE	0x00	/* standard passthru */
24 #define PHP_OUTPUT_HANDLER_START	0x01	/* start */
25 #define PHP_OUTPUT_HANDLER_CLEAN	0x02	/* restart */
26 #define PHP_OUTPUT_HANDLER_FLUSH	0x04	/* pass along as much as possible */
27 #define PHP_OUTPUT_HANDLER_FINAL	0x08	/* finalize */
28 #define PHP_OUTPUT_HANDLER_CONT		PHP_OUTPUT_HANDLER_WRITE
29 #define PHP_OUTPUT_HANDLER_END		PHP_OUTPUT_HANDLER_FINAL
30 
31 /* handler types */
32 #define PHP_OUTPUT_HANDLER_INTERNAL		0x0000
33 #define PHP_OUTPUT_HANDLER_USER			0x0001
34 
35 /* handler ability flags */
36 #define PHP_OUTPUT_HANDLER_CLEANABLE	0x0010
37 #define PHP_OUTPUT_HANDLER_FLUSHABLE	0x0020
38 #define PHP_OUTPUT_HANDLER_REMOVABLE	0x0040
39 #define PHP_OUTPUT_HANDLER_STDFLAGS		0x0070
40 
41 /* handler status flags */
42 #define PHP_OUTPUT_HANDLER_STARTED		0x1000
43 #define PHP_OUTPUT_HANDLER_DISABLED		0x2000
44 #define PHP_OUTPUT_HANDLER_PROCESSED	0x4000
45 
46 /* handler op return values */
47 typedef enum _php_output_handler_status_t {
48 	PHP_OUTPUT_HANDLER_FAILURE,
49 	PHP_OUTPUT_HANDLER_SUCCESS,
50 	PHP_OUTPUT_HANDLER_NO_DATA
51 } php_output_handler_status_t;
52 
53 /* php_output_stack_pop() flags */
54 #define PHP_OUTPUT_POP_TRY			0x000
55 #define PHP_OUTPUT_POP_FORCE		0x001
56 #define PHP_OUTPUT_POP_DISCARD		0x010
57 #define PHP_OUTPUT_POP_SILENT		0x100
58 
59 /* real global flags */
60 #define PHP_OUTPUT_IMPLICITFLUSH		0x01
61 #define PHP_OUTPUT_DISABLED				0x02
62 #define PHP_OUTPUT_WRITTEN				0x04
63 #define PHP_OUTPUT_SENT					0x08
64 /* supplementary flags for php_output_get_status() */
65 #define PHP_OUTPUT_ACTIVE				0x10
66 #define PHP_OUTPUT_LOCKED				0x20
67 /* output layer is ready to use */
68 #define PHP_OUTPUT_ACTIVATED		0x100000
69 
70 /* handler hooks */
71 typedef enum _php_output_handler_hook_t {
72 	PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ,
73 	PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS,
74 	PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL,
75 	PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE,
76 	PHP_OUTPUT_HANDLER_HOOK_DISABLE,
77 	/* unused */
78 	PHP_OUTPUT_HANDLER_HOOK_LAST
79 } php_output_handler_hook_t;
80 
81 #define PHP_OUTPUT_HANDLER_INITBUF_SIZE(s) \
82 ( ((s) > 1) ? \
83 	(s) + PHP_OUTPUT_HANDLER_ALIGNTO_SIZE - ((s) % (PHP_OUTPUT_HANDLER_ALIGNTO_SIZE)) : \
84 	PHP_OUTPUT_HANDLER_DEFAULT_SIZE \
85 )
86 #define PHP_OUTPUT_HANDLER_ALIGNTO_SIZE		0x1000
87 #define PHP_OUTPUT_HANDLER_DEFAULT_SIZE		0x4000
88 
89 typedef struct _php_output_buffer {
90 	char *data;
91 	size_t size;
92 	size_t used;
93 	uint32_t free:1;
94 	uint32_t _reserved:31;
95 } php_output_buffer;
96 
97 typedef struct _php_output_context {
98 	int op;
99 	php_output_buffer in;
100 	php_output_buffer out;
101 } php_output_context;
102 
103 /* old-style, stateless callback */
104 typedef void (*php_output_handler_func_t)(char *output, size_t output_len, char **handled_output, size_t *handled_output_len, int mode);
105 /* new-style, opaque context callback */
106 typedef int (*php_output_handler_context_func_t)(void **handler_context, php_output_context *output_context);
107 /* output handler context dtor */
108 typedef void (*php_output_handler_context_dtor_t)(void *opaq);
109 /* conflict check callback */
110 typedef int (*php_output_handler_conflict_check_t)(const char *handler_name, size_t handler_name_len);
111 /* ctor for aliases */
112 typedef struct _php_output_handler *(*php_output_handler_alias_ctor_t)(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags);
113 
114 typedef struct _php_output_handler_user_func_t {
115 	zend_fcall_info fci;
116 	zend_fcall_info_cache fcc;
117 	zval zoh;
118 } php_output_handler_user_func_t;
119 
120 typedef struct _php_output_handler {
121 	zend_string *name;
122 	int flags;
123 	int level;
124 	size_t size;
125 	php_output_buffer buffer;
126 
127 	void *opaq;
128 	void (*dtor)(void *opaq);
129 
130 	union {
131 		php_output_handler_user_func_t *user;
132 		php_output_handler_context_func_t internal;
133 	} func;
134 } php_output_handler;
135 
136 ZEND_BEGIN_MODULE_GLOBALS(output)
137 	zend_stack handlers;
138 	php_output_handler *active;
139 	php_output_handler *running;
140 	zend_string *output_start_filename;
141 	int output_start_lineno;
142 	int flags;
143 ZEND_END_MODULE_GLOBALS(output)
144 
145 PHPAPI ZEND_EXTERN_MODULE_GLOBALS(output)
146 
147 /* there should not be a need to use OG() from outside of output.c */
148 #ifdef ZTS
149 # define OG(v) ZEND_TSRMG(output_globals_id, zend_output_globals *, v)
150 #else
151 # define OG(v) (output_globals.v)
152 #endif
153 
154 /* convenience macros */
155 #define PHPWRITE(str, str_len)		php_output_write((str), (str_len))
156 #define PHPWRITE_H(str, str_len)	php_output_write_unbuffered((str), (str_len))
157 
158 #define PUTC(c)						php_output_write((const char *) &(c), 1)
159 #define PUTC_H(c)					php_output_write_unbuffered((const char *) &(c), 1)
160 
161 #define PUTS(str)					do {				\
162 	const char *__str = (str);							\
163 	php_output_write(__str, strlen(__str));	\
164 } while (0)
165 #define PUTS_H(str)					do {							\
166 	const char *__str = (str);										\
167 	php_output_write_unbuffered(__str, strlen(__str));	\
168 } while (0)
169 
170 
171 BEGIN_EXTERN_C()
172 
173 extern const char php_output_default_handler_name[sizeof("default output handler")];
174 extern const char php_output_devnull_handler_name[sizeof("null output handler")];
175 
176 #define php_output_tearup() \
177 	php_output_startup(); \
178 	php_output_activate()
179 #define php_output_teardown() \
180 	php_output_end_all(); \
181 	php_output_deactivate(); \
182 	php_output_shutdown()
183 
184 /* MINIT */
185 PHPAPI void php_output_startup(void);
186 /* MSHUTDOWN */
187 PHPAPI void php_output_shutdown(void);
188 
189 PHPAPI void php_output_register_constants(void);
190 
191 /* RINIT */
192 PHPAPI int php_output_activate(void);
193 /* RSHUTDOWN */
194 PHPAPI void php_output_deactivate(void);
195 
196 PHPAPI void php_output_set_status(int status);
197 PHPAPI int php_output_get_status(void);
198 PHPAPI void php_output_set_implicit_flush(int flush);
199 PHPAPI const char *php_output_get_start_filename(void);
200 PHPAPI int php_output_get_start_lineno(void);
201 
202 PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len);
203 PHPAPI size_t php_output_write(const char *str, size_t len);
204 
205 PHPAPI int php_output_flush(void);
206 PHPAPI void php_output_flush_all(void);
207 PHPAPI int php_output_clean(void);
208 PHPAPI void php_output_clean_all(void);
209 PHPAPI int php_output_end(void);
210 PHPAPI void php_output_end_all(void);
211 PHPAPI int php_output_discard(void);
212 PHPAPI void php_output_discard_all(void);
213 
214 PHPAPI int php_output_get_contents(zval *p);
215 PHPAPI int php_output_get_length(zval *p);
216 PHPAPI int php_output_get_level(void);
217 PHPAPI php_output_handler* php_output_get_active_handler(void);
218 
219 PHPAPI int php_output_start_default(void);
220 PHPAPI int php_output_start_devnull(void);
221 
222 PHPAPI int php_output_start_user(zval *output_handler, size_t chunk_size, int flags);
223 PHPAPI int php_output_start_internal(const char *name, size_t name_len, php_output_handler_func_t output_handler, size_t chunk_size, int flags);
224 
225 PHPAPI php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags);
226 PHPAPI php_output_handler *php_output_handler_create_internal(const char *name, size_t name_len, php_output_handler_context_func_t handler, size_t chunk_size, int flags);
227 
228 PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*));
229 PHPAPI int php_output_handler_start(php_output_handler *handler);
230 PHPAPI int php_output_handler_started(const char *name, size_t name_len);
231 PHPAPI int php_output_handler_hook(php_output_handler_hook_t type, void *arg);
232 PHPAPI void php_output_handler_dtor(php_output_handler *handler);
233 PHPAPI void php_output_handler_free(php_output_handler **handler);
234 
235 PHPAPI int php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len);
236 PHPAPI int php_output_handler_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func);
237 PHPAPI int php_output_handler_reverse_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func);
238 
239 PHPAPI php_output_handler_alias_ctor_t php_output_handler_alias(const char *handler_name, size_t handler_name_len);
240 PHPAPI int php_output_handler_alias_register(const char *handler_name, size_t handler_name_len, php_output_handler_alias_ctor_t func);
241 
242 END_EXTERN_C()
243 
244 #endif
245