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