xref: /php-src/main/php_output.h (revision 2447cb25)
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 #define PHP_OUTPUT_HANDLER_ABILITY_FLAGS(bitmask) ((bitmask) & ~0xf00f)
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 zend_result (*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 zend_result (*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 	zend_string *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 /* 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 zend_result php_output_flush(void);
206 PHPAPI void php_output_flush_all(void);
207 PHPAPI zend_result php_output_clean(void);
208 PHPAPI void php_output_clean_all(void);
209 PHPAPI zend_result php_output_end(void);
210 PHPAPI void php_output_end_all(void);
211 PHPAPI zend_result php_output_discard(void);
212 PHPAPI void php_output_discard_all(void);
213 
214 PHPAPI zend_result php_output_get_contents(zval *p);
215 PHPAPI zend_result 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 zend_result php_output_start_default(void);
220 PHPAPI zend_result php_output_start_devnull(void);
221 
222 PHPAPI zend_result php_output_start_user(zval *output_handler, size_t chunk_size, int flags);
223 PHPAPI zend_result 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 zend_result php_output_handler_start(php_output_handler *handler);
230 PHPAPI bool php_output_handler_started(const char *name, size_t name_len);
231 PHPAPI zend_result 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 bool php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len);
236 PHPAPI zend_result php_output_handler_conflict_register(const char *handler_name, size_t handler_name_len, php_output_handler_conflict_check_t check_func);
237 PHPAPI zend_result 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 zend_result 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