Coverage Report

Created: 2025-12-31 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/main/output.c
Line
Count
Source
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: Zeev Suraski <zeev@php.net>                                 |
14
   |          Thies C. Arntzen <thies@thieso.net>                         |
15
   |          Marcus Boerger <helly@php.net>                              |
16
   | New API: Michael Wallner <mike@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#ifndef PHP_OUTPUT_DEBUG
21
# define PHP_OUTPUT_DEBUG 0
22
#endif
23
#ifndef PHP_OUTPUT_NOINLINE
24
# define PHP_OUTPUT_NOINLINE 0
25
#endif
26
27
#include "php.h"
28
#include "ext/standard/head.h"
29
#include "ext/standard/url_scanner_ex.h"
30
#include "SAPI.h"
31
#include "zend_stack.h"
32
#include "php_output.h"
33
34
PHPAPI ZEND_DECLARE_MODULE_GLOBALS(output)
35
36
const char php_output_default_handler_name[sizeof("default output handler")] = "default output handler";
37
const char php_output_devnull_handler_name[sizeof("null output handler")] = "null output handler";
38
39
#if PHP_OUTPUT_NOINLINE || PHP_OUTPUT_DEBUG
40
# undef inline
41
# define inline
42
#endif
43
44
/* {{{ aliases, conflict and reverse conflict hash tables */
45
static HashTable php_output_handler_aliases;
46
static HashTable php_output_handler_conflicts;
47
static HashTable php_output_handler_reverse_conflicts;
48
/* }}} */
49
50
/* {{{ forward declarations */
51
static inline bool php_output_lock_error(int op);
52
static inline void php_output_op(int op, const char *str, size_t len);
53
54
static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags);
55
static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context);
56
static inline bool php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf);
57
static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry);
58
59
static inline void php_output_context_init(php_output_context *context, int op);
60
static inline void php_output_context_reset(php_output_context *context);
61
static inline void php_output_context_swap(php_output_context *context);
62
static inline void php_output_context_dtor(php_output_context *context);
63
64
static int php_output_stack_pop(int flags);
65
static int php_output_stack_apply_op(void *h, void *c);
66
static int php_output_stack_apply_clean(void *h, void *c);
67
static int php_output_stack_apply_list(void *h, void *z);
68
static int php_output_stack_apply_status(void *h, void *z);
69
70
static zend_result php_output_handler_compat_func(void **handler_context, php_output_context *output_context);
71
static zend_result php_output_handler_default_func(void **handler_context, php_output_context *output_context);
72
static zend_result php_output_handler_devnull_func(void **handler_context, php_output_context *output_context);
73
/* }}} */
74
75
/* {{{ static void php_output_init_globals(zend_output_globals *G)
76
 * Initialize the module globals on MINIT */
77
static inline void php_output_init_globals(zend_output_globals *G)
78
14
{
79
14
  memset(G, 0, sizeof(*G));
80
14
}
81
/* }}} */
82
83
/* {{{ stderr/stdout writer if not PHP_OUTPUT_ACTIVATED */
84
static size_t php_output_stdout(const char *str, size_t str_len)
85
0
{
86
0
  fwrite(str, 1, str_len, stdout);
87
0
  return str_len;
88
0
}
89
static size_t php_output_stderr(const char *str, size_t str_len)
90
0
{
91
0
  fwrite(str, 1, str_len, stderr);
92
/* See http://support.microsoft.com/kb/190351 */
93
#ifdef PHP_WIN32
94
  fflush(stderr);
95
#endif
96
0
  return str_len;
97
0
}
98
static size_t (*php_output_direct)(const char *str, size_t str_len) = php_output_stderr;
99
/* }}} */
100
101
/* {{{ void php_output_header(void) */
102
static void php_output_header(void)
103
13.7M
{
104
13.7M
  if (!SG(headers_sent)) {
105
222k
    if (!OG(output_start_filename)) {
106
222k
      if (zend_is_compiling()) {
107
0
        OG(output_start_filename) = zend_get_compiled_filename();
108
0
        OG(output_start_lineno) = zend_get_compiled_lineno();
109
222k
      } else if (zend_is_executing()) {
110
19.3k
        OG(output_start_filename) = zend_get_executed_filename_ex();
111
19.3k
        OG(output_start_lineno) = zend_get_executed_lineno();
112
19.3k
      }
113
222k
      if (OG(output_start_filename)) {
114
16.7k
        zend_string_addref(OG(output_start_filename));
115
16.7k
      }
116
#if PHP_OUTPUT_DEBUG
117
      fprintf(stderr, "!!! output started at: %s (%d)\n",
118
        ZSTR_VAL(OG(output_start_filename)), OG(output_start_lineno));
119
#endif
120
222k
    }
121
222k
    if (!php_header()) {
122
0
      OG(flags) |= PHP_OUTPUT_DISABLED;
123
0
    }
124
222k
  }
125
13.7M
}
126
/* }}} */
127
128
static void reverse_conflict_dtor(zval *zv)
129
0
{
130
0
  HashTable *ht = Z_PTR_P(zv);
131
0
  zend_hash_destroy(ht);
132
0
}
133
134
/* {{{ void php_output_startup(void)
135
 * Set up module globals and initialize the conflict and reverse conflict hash tables */
136
PHPAPI void php_output_startup(void)
137
14
{
138
14
  ZEND_INIT_MODULE_GLOBALS(output, php_output_init_globals, NULL);
139
14
  zend_hash_init(&php_output_handler_aliases, 8, NULL, NULL, 1);
140
14
  zend_hash_init(&php_output_handler_conflicts, 8, NULL, NULL, 1);
141
14
  zend_hash_init(&php_output_handler_reverse_conflicts, 8, NULL, reverse_conflict_dtor, 1);
142
14
  php_output_direct = php_output_stdout;
143
14
}
144
/* }}} */
145
146
/* {{{ void php_output_shutdown(void)
147
 * Destroy module globals and the conflict and reverse conflict hash tables */
148
PHPAPI void php_output_shutdown(void)
149
0
{
150
0
  php_output_direct = php_output_stderr;
151
0
  zend_hash_destroy(&php_output_handler_aliases);
152
0
  zend_hash_destroy(&php_output_handler_conflicts);
153
0
  zend_hash_destroy(&php_output_handler_reverse_conflicts);
154
0
}
155
/* }}} */
156
157
/* {{{ SUCCESS|FAILURE php_output_activate(void)
158
 * Reset output globals and set up the output handler stack */
159
PHPAPI int php_output_activate(void)
160
222k
{
161
#ifdef ZTS
162
  memset(TSRMG_BULK_STATIC(output_globals_id, zend_output_globals*), 0, sizeof(zend_output_globals));
163
#else
164
222k
  memset(&output_globals, 0, sizeof(zend_output_globals));
165
222k
#endif
166
167
222k
  zend_stack_init(&OG(handlers), sizeof(php_output_handler *));
168
222k
  OG(flags) |= PHP_OUTPUT_ACTIVATED;
169
170
222k
  return SUCCESS;
171
222k
}
172
/* }}} */
173
174
/* {{{ void php_output_deactivate(void)
175
 * Destroy the output handler stack */
176
PHPAPI void php_output_deactivate(void)
177
222k
{
178
222k
  php_output_handler **handler = NULL;
179
180
222k
  if ((OG(flags) & PHP_OUTPUT_ACTIVATED)) {
181
222k
    php_output_header();
182
183
222k
    OG(flags) ^= PHP_OUTPUT_ACTIVATED;
184
222k
    OG(active) = NULL;
185
222k
    OG(running) = NULL;
186
187
    /* release all output handlers */
188
222k
    if (OG(handlers).elements) {
189
321
      while ((handler = zend_stack_top(&OG(handlers)))) {
190
0
        zend_stack_del_top(&OG(handlers));
191
        /* It's possible to start a new output handler and mark it as active,
192
         * however this loop will destroy all active handlers. */
193
0
        OG(active) = NULL;
194
0
        ZEND_ASSERT(OG(running) == NULL && "output is deactivated therefore running should stay NULL");
195
0
        php_output_handler_free(handler);
196
0
      }
197
321
    }
198
222k
    zend_stack_destroy(&OG(handlers));
199
222k
  }
200
201
222k
  if (OG(output_start_filename)) {
202
16.7k
    zend_string_release(OG(output_start_filename));
203
16.7k
    OG(output_start_filename) = NULL;
204
16.7k
  }
205
222k
}
206
/* }}} */
207
208
/* {{{ void php_output_set_status(int status)
209
 * Used by SAPIs to disable output */
210
PHPAPI void php_output_set_status(int status)
211
0
{
212
0
  OG(flags) = (OG(flags) & ~0xf) | (status & 0xf);
213
0
}
214
/* }}} */
215
216
/* {{{ int php_output_get_status()
217
 * Get output control status */
218
PHPAPI int php_output_get_status(void)
219
0
{
220
0
  return (
221
0
    OG(flags)
222
0
    | (OG(active) ? PHP_OUTPUT_ACTIVE : 0)
223
0
    | (OG(running)? PHP_OUTPUT_LOCKED : 0)
224
0
  ) & 0xff;
225
0
}
226
/* }}} */
227
228
/* {{{ int php_output_write_unbuffered(const char *str, size_t len)
229
 * Unbuffered write */
230
PHPAPI size_t php_output_write_unbuffered(const char *str, size_t len)
231
0
{
232
0
  if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
233
0
    return sapi_module.ub_write(str, len);
234
0
  }
235
0
  return php_output_direct(str, len);
236
0
}
237
/* }}} */
238
239
/* {{{ int php_output_write(const char *str, size_t len)
240
 * Buffered write */
241
PHPAPI size_t php_output_write(const char *str, size_t len)
242
14.2M
{
243
14.2M
  if (OG(flags) & PHP_OUTPUT_ACTIVATED) {
244
14.2M
    php_output_op(PHP_OUTPUT_HANDLER_WRITE, str, len);
245
14.2M
    return len;
246
14.2M
  }
247
0
  if (OG(flags) & PHP_OUTPUT_DISABLED) {
248
0
    return 0;
249
0
  }
250
0
  return php_output_direct(str, len);
251
0
}
252
/* }}} */
253
254
/* {{{ SUCCESS|FAILURE php_output_flush(void)
255
 * Flush the most recent output handlers buffer */
256
PHPAPI zend_result php_output_flush(void)
257
0
{
258
0
  php_output_context context;
259
260
0
  if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
261
0
    php_output_context_init(&context, PHP_OUTPUT_HANDLER_FLUSH);
262
0
    php_output_handler_op(OG(active), &context);
263
0
    if (context.out.data && context.out.used) {
264
0
      zend_stack_del_top(&OG(handlers));
265
0
      php_output_write(context.out.data, context.out.used);
266
0
      zend_stack_push(&OG(handlers), &OG(active));
267
0
    }
268
0
    php_output_context_dtor(&context);
269
0
    return SUCCESS;
270
0
  }
271
0
  return FAILURE;
272
0
}
273
/* }}} */
274
275
/* {{{ void php_output_flush_all()
276
 * Flush all output buffers subsequently */
277
PHPAPI void php_output_flush_all(void)
278
0
{
279
0
  if (OG(active)) {
280
0
    php_output_op(PHP_OUTPUT_HANDLER_FLUSH, NULL, 0);
281
0
  }
282
0
}
283
/* }}} */
284
285
/* {{{ SUCCESS|FAILURE php_output_clean(void)
286
 * Cleans the most recent output handlers buffer if the handler is cleanable */
287
PHPAPI zend_result php_output_clean(void)
288
0
{
289
0
  php_output_context context;
290
291
0
  if (OG(active) && (OG(active)->flags & PHP_OUTPUT_HANDLER_CLEANABLE)) {
292
0
    php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
293
0
    php_output_handler_op(OG(active), &context);
294
0
    php_output_context_dtor(&context);
295
0
    return SUCCESS;
296
0
  }
297
0
  return FAILURE;
298
0
}
299
/* }}} */
300
301
/* {{{ void php_output_clean_all(void)
302
 * Cleans all output handler buffers, without regard whether the handler is cleanable */
303
PHPAPI void php_output_clean_all(void)
304
0
{
305
0
  php_output_context context;
306
307
0
  if (OG(active)) {
308
0
    php_output_context_init(&context, PHP_OUTPUT_HANDLER_CLEAN);
309
0
    zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_clean, &context);
310
0
  }
311
0
}
312
313
/* {{{ SUCCESS|FAILURE php_output_end(void)
314
 * Finalizes the most recent output handler at pops it off the stack if the handler is removable */
315
PHPAPI zend_result php_output_end(void)
316
1
{
317
1
  if (php_output_stack_pop(PHP_OUTPUT_POP_TRY)) {
318
1
    return SUCCESS;
319
1
  }
320
0
  return FAILURE;
321
1
}
322
/* }}} */
323
324
/* {{{ void php_output_end_all(void)
325
 * Finalizes all output handlers and ends output buffering without regard whether a handler is removable */
326
PHPAPI void php_output_end_all(void)
327
222k
{
328
222k
  while (OG(active) && php_output_stack_pop(PHP_OUTPUT_POP_FORCE));
329
222k
}
330
/* }}} */
331
332
/* {{{ SUCCESS|FAILURE php_output_discard(void)
333
 * Discards the most recent output handlers buffer and pops it off the stack if the handler is removable */
334
PHPAPI zend_result php_output_discard(void)
335
439
{
336
439
  if (php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_TRY)) {
337
439
    return SUCCESS;
338
439
  }
339
0
  return FAILURE;
340
439
}
341
/* }}} */
342
343
/* {{{ void php_output_discard_all(void)
344
 * Discard all output handlers and buffers without regard whether a handler is removable */
345
PHPAPI void php_output_discard_all(void)
346
34.5k
{
347
34.5k
  while (OG(active)) {
348
0
    php_output_stack_pop(PHP_OUTPUT_POP_DISCARD|PHP_OUTPUT_POP_FORCE);
349
0
  }
350
34.5k
}
351
/* }}} */
352
353
/* {{{ int php_output_get_level(void)
354
 * Get output buffering level, i.e. how many output handlers the stack contains */
355
PHPAPI int php_output_get_level(void)
356
443
{
357
443
  return OG(active) ? zend_stack_count(&OG(handlers)) : 0;
358
443
}
359
/* }}} */
360
361
/* {{{ SUCCESS|FAILURE php_output_get_contents(zval *z)
362
 * Get the contents of the active output handlers buffer */
363
PHPAPI zend_result php_output_get_contents(zval *p)
364
436
{
365
436
  if (OG(active)) {
366
436
    if (OG(active)->buffer.used) {
367
436
      ZVAL_STRINGL(p, OG(active)->buffer.data, OG(active)->buffer.used);
368
436
    } else {
369
0
      ZVAL_EMPTY_STRING(p);
370
0
    }
371
436
    return SUCCESS;
372
436
  } else {
373
0
    ZVAL_NULL(p);
374
0
    return FAILURE;
375
0
  }
376
436
}
377
378
/* {{{ SUCCESS|FAILURE php_output_get_length(zval *z)
379
 * Get the length of the active output handlers buffer */
380
PHPAPI zend_result php_output_get_length(zval *p)
381
0
{
382
0
  if (OG(active)) {
383
0
    ZVAL_LONG(p, OG(active)->buffer.used);
384
0
    return SUCCESS;
385
0
  } else {
386
0
    ZVAL_NULL(p);
387
0
    return FAILURE;
388
0
  }
389
0
}
390
/* }}} */
391
392
/* {{{ php_output_handler* php_output_get_active_handler(void)
393
 * Get active output handler */
394
PHPAPI php_output_handler* php_output_get_active_handler(void)
395
0
{
396
0
  return OG(active);
397
0
}
398
/* }}} */
399
400
/* {{{ SUCCESS|FAILURE php_output_handler_start_default(void)
401
 * Start a "default output handler" */
402
PHPAPI zend_result php_output_start_default(void)
403
436
{
404
436
  php_output_handler *handler;
405
406
436
  handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
407
436
  if (SUCCESS == php_output_handler_start(handler)) {
408
436
    return SUCCESS;
409
436
  }
410
0
  php_output_handler_free(&handler);
411
0
  return FAILURE;
412
436
}
413
/* }}} */
414
415
/* {{{ SUCCESS|FAILURE php_output_handler_start_devnull(void)
416
 * Start a "null output handler" */
417
PHPAPI zend_result php_output_start_devnull(void)
418
0
{
419
0
  php_output_handler *handler;
420
421
0
  handler = php_output_handler_create_internal(ZEND_STRL(php_output_devnull_handler_name), php_output_handler_devnull_func, PHP_OUTPUT_HANDLER_DEFAULT_SIZE, 0);
422
0
  if (SUCCESS == php_output_handler_start(handler)) {
423
0
    return SUCCESS;
424
0
  }
425
0
  php_output_handler_free(&handler);
426
0
  return FAILURE;
427
0
}
428
/* }}} */
429
430
/* {{{ SUCCESS|FAILURE php_output_start_user(zval *handler, size_t chunk_size, int flags)
431
 * Start a user level output handler */
432
PHPAPI zend_result php_output_start_user(zval *output_handler, size_t chunk_size, int flags)
433
7
{
434
7
  php_output_handler *handler;
435
436
7
  if (output_handler) {
437
6
    handler = php_output_handler_create_user(output_handler, chunk_size, flags);
438
6
  } else {
439
1
    handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
440
1
  }
441
7
  if (SUCCESS == php_output_handler_start(handler)) {
442
7
    return SUCCESS;
443
7
  }
444
0
  php_output_handler_free(&handler);
445
0
  return FAILURE;
446
7
}
447
/* }}} */
448
449
/* {{{ SUCCESS|FAILURE php_output_start_internal(zval *name, php_output_handler_func_t handler, size_t chunk_size, int flags)
450
 * Start an internal output handler that does not have to maintain a non-global state */
451
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)
452
0
{
453
0
  php_output_handler *handler;
454
455
0
  handler = php_output_handler_create_internal(name, name_len, php_output_handler_compat_func, chunk_size, flags);
456
0
  php_output_handler_set_context(handler, output_handler, NULL);
457
0
  if (SUCCESS == php_output_handler_start(handler)) {
458
0
    return SUCCESS;
459
0
  }
460
0
  php_output_handler_free(&handler);
461
0
  return FAILURE;
462
0
}
463
/* }}} */
464
465
/* {{{ php_output_handler *php_output_handler_create_user(zval *handler, size_t chunk_size, int flags)
466
 * Create a user level output handler */
467
PHPAPI php_output_handler *php_output_handler_create_user(zval *output_handler, size_t chunk_size, int flags)
468
6
{
469
6
  zend_string *handler_name = NULL;
470
6
  char *error = NULL;
471
6
  php_output_handler *handler = NULL;
472
6
  php_output_handler_alias_ctor_t alias = NULL;
473
6
  php_output_handler_user_func_t *user = NULL;
474
475
6
  switch (Z_TYPE_P(output_handler)) {
476
0
    case IS_NULL:
477
0
      handler = php_output_handler_create_internal(ZEND_STRL(php_output_default_handler_name), php_output_handler_default_func, chunk_size, flags);
478
0
      break;
479
0
    case IS_STRING:
480
0
      if (Z_STRLEN_P(output_handler) && (alias = php_output_handler_alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler)))) {
481
0
        handler = alias(Z_STRVAL_P(output_handler), Z_STRLEN_P(output_handler), chunk_size, flags);
482
0
        break;
483
0
      }
484
0
      ZEND_FALLTHROUGH;
485
6
    default:
486
6
      user = ecalloc(1, sizeof(php_output_handler_user_func_t));
487
6
      if (SUCCESS == zend_fcall_info_init(output_handler, 0, &user->fci, &user->fcc, &handler_name, &error)) {
488
6
        handler = php_output_handler_init(handler_name, chunk_size, PHP_OUTPUT_HANDLER_ABILITY_FLAGS(flags) | PHP_OUTPUT_HANDLER_USER);
489
6
        ZVAL_COPY(&user->zoh, output_handler);
490
6
        handler->func.user = user;
491
6
      } else {
492
0
        efree(user);
493
0
      }
494
6
      if (error) {
495
0
        php_error_docref("ref.outcontrol", E_WARNING, "%s", error);
496
0
        efree(error);
497
0
      }
498
6
      if (handler_name) {
499
6
        zend_string_release_ex(handler_name, 0);
500
6
      }
501
6
  }
502
503
6
  return handler;
504
6
}
505
/* }}} */
506
507
/* {{{ php_output_handler *php_output_handler_create_internal(zval *name, php_output_handler_context_func_t handler, size_t chunk_size, int flags)
508
 * Create an internal output handler that can maintain a non-global state */
509
PHPAPI php_output_handler *php_output_handler_create_internal(const char *name, size_t name_len, php_output_handler_context_func_t output_handler, size_t chunk_size, int flags)
510
437
{
511
437
  php_output_handler *handler;
512
437
  zend_string *str = zend_string_init(name, name_len, 0);
513
514
437
  handler = php_output_handler_init(str, chunk_size, PHP_OUTPUT_HANDLER_ABILITY_FLAGS(flags) | PHP_OUTPUT_HANDLER_INTERNAL);
515
437
  handler->func.internal = output_handler;
516
437
  zend_string_release_ex(str, 0);
517
518
437
  return handler;
519
437
}
520
/* }}} */
521
522
/* {{{ void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
523
 * Set the context/state of an output handler. Calls the dtor of the previous context if there is one */
524
PHPAPI void php_output_handler_set_context(php_output_handler *handler, void *opaq, void (*dtor)(void*))
525
0
{
526
0
  if (handler->dtor && handler->opaq) {
527
0
    handler->dtor(handler->opaq);
528
0
  }
529
0
  handler->dtor = dtor;
530
0
  handler->opaq = opaq;
531
0
}
532
/* }}} */
533
534
/* {{{ SUCCESS|FAILURE php_output_handler_start(php_output_handler *handler)
535
 * Starts the set up output handler and pushes it on top of the stack. Checks for any conflicts regarding the output handler to start */
536
PHPAPI zend_result php_output_handler_start(php_output_handler *handler)
537
443
{
538
443
  HashTable *rconflicts;
539
443
  php_output_handler_conflict_check_t conflict;
540
541
443
  if (php_output_lock_error(PHP_OUTPUT_HANDLER_START) || !handler) {
542
0
    return FAILURE;
543
0
  }
544
443
  if (NULL != (conflict = zend_hash_find_ptr(&php_output_handler_conflicts, handler->name))) {
545
0
    if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
546
0
      return FAILURE;
547
0
    }
548
0
  }
549
443
  if (NULL != (rconflicts = zend_hash_find_ptr(&php_output_handler_reverse_conflicts, handler->name))) {
550
0
    ZEND_HASH_PACKED_FOREACH_PTR(rconflicts, conflict) {
551
0
      if (SUCCESS != conflict(ZSTR_VAL(handler->name), ZSTR_LEN(handler->name))) {
552
0
        return FAILURE;
553
0
      }
554
0
    } ZEND_HASH_FOREACH_END();
555
0
  }
556
  /* zend_stack_push returns stack level */
557
443
  handler->level = zend_stack_push(&OG(handlers), &handler);
558
443
  OG(active) = handler;
559
443
  return SUCCESS;
560
443
}
561
/* }}} */
562
563
/* {{{ bool php_output_handler_started(zval *name)
564
 * Check whether a certain output handler is in use */
565
PHPAPI bool php_output_handler_started(const char *name, size_t name_len)
566
0
{
567
0
  php_output_handler **handlers;
568
0
  int i, count = php_output_get_level();
569
570
0
  if (count) {
571
0
    handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
572
573
0
    for (i = 0; i < count; ++i) {
574
0
      if (zend_string_equals_cstr(handlers[i]->name, name, name_len)) {
575
0
        return true;
576
0
      }
577
0
    }
578
0
  }
579
580
0
  return false;
581
0
}
582
/* }}} */
583
584
/* {{{ bool php_output_handler_conflict(zval *handler_new, zval *handler_old)
585
 * Check whether a certain handler is in use and issue a warning that the new handler would conflict with the already used one */
586
PHPAPI bool php_output_handler_conflict(const char *handler_new, size_t handler_new_len, const char *handler_set, size_t handler_set_len)
587
0
{
588
0
  if (php_output_handler_started(handler_set, handler_set_len)) {
589
0
    if (handler_new_len != handler_set_len || memcmp(handler_new, handler_set, handler_set_len)) {
590
0
      php_error_docref("ref.outcontrol", E_WARNING, "Output handler '%s' conflicts with '%s'", handler_new, handler_set);
591
0
    } else {
592
0
      php_error_docref("ref.outcontrol", E_WARNING, "Output handler '%s' cannot be used twice", handler_new);
593
0
    }
594
0
    return true;
595
0
  }
596
0
  return false;
597
0
}
598
/* }}} */
599
600
/* {{{ SUCCESS|FAILURE php_output_handler_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
601
 * Register a conflict checking function on MINIT */
602
PHPAPI zend_result php_output_handler_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
603
0
{
604
0
  zend_string *str;
605
606
0
  if (!EG(current_module)) {
607
0
    zend_error_noreturn(E_ERROR, "Cannot register an output handler conflict outside of MINIT");
608
0
    return FAILURE;
609
0
  }
610
0
  str = zend_string_init_interned(name, name_len, 1);
611
0
  zend_hash_update_ptr(&php_output_handler_conflicts, str, check_func);
612
0
  zend_string_release_ex(str, 1);
613
0
  return SUCCESS;
614
0
}
615
/* }}} */
616
617
/* {{{ SUCCESS|FAILURE php_output_handler_reverse_conflict_register(zval *name, php_output_handler_conflict_check_t check_func)
618
 * Register a reverse conflict checking function on MINIT */
619
PHPAPI zend_result php_output_handler_reverse_conflict_register(const char *name, size_t name_len, php_output_handler_conflict_check_t check_func)
620
0
{
621
0
  HashTable rev, *rev_ptr = NULL;
622
623
0
  if (!EG(current_module)) {
624
0
    zend_error_noreturn(E_ERROR, "Cannot register a reverse output handler conflict outside of MINIT");
625
0
    return FAILURE;
626
0
  }
627
628
0
  if (NULL != (rev_ptr = zend_hash_str_find_ptr(&php_output_handler_reverse_conflicts, name, name_len))) {
629
0
    return zend_hash_next_index_insert_ptr(rev_ptr, check_func) ? SUCCESS : FAILURE;
630
0
  } else {
631
0
    zend_string *str;
632
633
0
    zend_hash_init(&rev, 8, NULL, NULL, 1);
634
0
    if (NULL == zend_hash_next_index_insert_ptr(&rev, check_func)) {
635
0
      zend_hash_destroy(&rev);
636
0
      return FAILURE;
637
0
    }
638
0
    str = zend_string_init_interned(name, name_len, 1);
639
0
    zend_hash_update_mem(&php_output_handler_reverse_conflicts, str, &rev, sizeof(HashTable));
640
0
    zend_string_release_ex(str, 1);
641
0
    return SUCCESS;
642
0
  }
643
0
}
644
/* }}} */
645
646
/* {{{ php_output_handler_alias_ctor_t php_output_handler_alias(zval *name)
647
 * Get an internal output handler for a user handler if it exists */
648
PHPAPI php_output_handler_alias_ctor_t php_output_handler_alias(const char *name, size_t name_len)
649
0
{
650
0
  return zend_hash_str_find_ptr(&php_output_handler_aliases, name, name_len);
651
0
}
652
/* }}} */
653
654
/* {{{ SUCCESS|FAILURE php_output_handler_alias_register(zval *name, php_output_handler_alias_ctor_t func)
655
 * Registers an internal output handler as alias for a user handler */
656
PHPAPI zend_result php_output_handler_alias_register(const char *name, size_t name_len, php_output_handler_alias_ctor_t func)
657
0
{
658
0
  zend_string *str;
659
660
0
  if (!EG(current_module)) {
661
0
    zend_error_noreturn(E_ERROR, "Cannot register an output handler alias outside of MINIT");
662
0
    return FAILURE;
663
0
  }
664
0
  str = zend_string_init_interned(name, name_len, 1);
665
0
  zend_hash_update_ptr(&php_output_handler_aliases, str, func);
666
0
  zend_string_release_ex(str, 1);
667
0
  return SUCCESS;
668
0
}
669
/* }}} */
670
671
/* {{{ SUCCESS|FAILURE php_output_handler_hook(php_output_handler_hook_t type, void *arg)
672
 * Output handler hook for output handler functions to check/modify the current handlers abilities */
673
PHPAPI zend_result php_output_handler_hook(php_output_handler_hook_t type, void *arg)
674
0
{
675
0
  if (OG(running)) {
676
0
    switch (type) {
677
0
      case PHP_OUTPUT_HANDLER_HOOK_GET_OPAQ:
678
0
        *(void ***) arg = &OG(running)->opaq;
679
0
        return SUCCESS;
680
0
      case PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS:
681
0
        *(int *) arg = OG(running)->flags;
682
0
        return SUCCESS;
683
0
      case PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL:
684
0
        *(int *) arg = OG(running)->level;
685
0
        return SUCCESS;
686
0
      case PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE:
687
0
        OG(running)->flags &= ~(PHP_OUTPUT_HANDLER_REMOVABLE|PHP_OUTPUT_HANDLER_CLEANABLE);
688
0
        return SUCCESS;
689
0
      case PHP_OUTPUT_HANDLER_HOOK_DISABLE:
690
0
        OG(running)->flags |= PHP_OUTPUT_HANDLER_DISABLED;
691
0
        return SUCCESS;
692
0
      default:
693
0
        break;
694
0
    }
695
0
  }
696
0
  return FAILURE;
697
0
}
698
/* }}} */
699
700
/* {{{ void php_output_handler_dtor(php_output_handler *handler)
701
 * Destroy an output handler */
702
PHPAPI void php_output_handler_dtor(php_output_handler *handler)
703
443
{
704
443
  if (handler->name) {
705
443
    zend_string_release_ex(handler->name, 0);
706
443
  }
707
443
  if (handler->buffer.data) {
708
442
    efree(handler->buffer.data);
709
442
  }
710
443
  if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
711
6
    zval_ptr_dtor(&handler->func.user->zoh);
712
6
    efree(handler->func.user);
713
6
  }
714
443
  if (handler->dtor && handler->opaq) {
715
0
    handler->dtor(handler->opaq);
716
0
  }
717
443
  memset(handler, 0, sizeof(*handler));
718
443
}
719
/* }}} */
720
721
/* {{{ void php_output_handler_free(php_output_handler **handler)
722
 * Destroy and free an output handler */
723
PHPAPI void php_output_handler_free(php_output_handler **h)
724
443
{
725
443
  php_output_handler *handler = *h;
726
443
  if (handler) {
727
443
    *h = NULL;
728
443
    php_output_handler_dtor(handler);
729
443
    efree(handler);
730
443
  }
731
443
}
732
/* }}} */
733
734
/* void php_output_set_implicit_flush(int enabled)
735
 * Enable or disable implicit flush */
736
PHPAPI void php_output_set_implicit_flush(int flush)
737
222k
{
738
222k
  if (flush) {
739
222k
    OG(flags) |= PHP_OUTPUT_IMPLICITFLUSH;
740
222k
  } else {
741
0
    OG(flags) &= ~PHP_OUTPUT_IMPLICITFLUSH;
742
0
  }
743
222k
}
744
/* }}} */
745
746
/* {{{ char *php_output_get_start_filename(void)
747
 * Get the file name where output has started */
748
PHPAPI const char *php_output_get_start_filename(void)
749
1
{
750
1
  return OG(output_start_filename) ? ZSTR_VAL(OG(output_start_filename)) : NULL;
751
1
}
752
/* }}} */
753
754
/* {{{ int php_output_get_start_lineno(void)
755
 * Get the line number where output has started */
756
PHPAPI int php_output_get_start_lineno(void)
757
1
{
758
1
  return OG(output_start_lineno);
759
1
}
760
/* }}} */
761
762
/* {{{ static bool php_output_lock_error(int op)
763
 * Checks whether an unallowed operation is attempted from within the output handler and issues a fatal error */
764
static inline bool php_output_lock_error(int op)
765
14.9M
{
766
  /* if there's no ob active, ob has been stopped */
767
14.9M
  if (op && OG(active) && OG(running)) {
768
    /* fatal error */
769
0
    php_output_deactivate();
770
0
    php_error_docref("ref.outcontrol", E_ERROR, "Cannot use output buffering in output buffering display handlers");
771
0
    return true;
772
0
  }
773
14.9M
  return false;
774
14.9M
}
775
/* }}} */
776
777
/* {{{ static php_output_context *php_output_context_init(php_output_context *context, int op)
778
 * Initialize a new output context */
779
static inline void php_output_context_init(php_output_context *context, int op)
780
14.2M
{
781
14.2M
  memset(context, 0, sizeof(php_output_context));
782
14.2M
  context->op = op;
783
14.2M
}
784
/* }}} */
785
786
/* {{{ static void php_output_context_reset(php_output_context *context)
787
 * Reset an output context */
788
static inline void php_output_context_reset(php_output_context *context)
789
5
{
790
5
  int op = context->op;
791
5
  php_output_context_dtor(context);
792
5
  memset(context, 0, sizeof(php_output_context));
793
5
  context->op = op;
794
5
}
795
/* }}} */
796
797
/* {{{ static void php_output_context_feed(php_output_context *context, char *, size_t, size_t)
798
 * Feed output contexts input buffer */
799
static inline void php_output_context_feed(php_output_context *context, char *data, size_t size, size_t used, bool free)
800
437
{
801
437
  if (context->in.free && context->in.data) {
802
0
    efree(context->in.data);
803
0
  }
804
437
  context->in.data = data;
805
437
  context->in.used = used;
806
437
  context->in.free = free;
807
437
  context->in.size = size;
808
437
}
809
/* }}} */
810
811
/* {{{ static void php_output_context_swap(php_output_context *context)
812
 * Swap output contexts buffers */
813
static inline void php_output_context_swap(php_output_context *context)
814
0
{
815
0
  if (context->in.free && context->in.data) {
816
0
    efree(context->in.data);
817
0
  }
818
0
  context->in.data = context->out.data;
819
0
  context->in.used = context->out.used;
820
0
  context->in.free = context->out.free;
821
0
  context->in.size = context->out.size;
822
0
  context->out.data = NULL;
823
0
  context->out.used = 0;
824
0
  context->out.free = 0;
825
0
  context->out.size = 0;
826
0
}
827
/* }}} */
828
829
/* {{{ static void php_output_context_pass(php_output_context *context)
830
 * Pass input to output buffer */
831
static inline void php_output_context_pass(php_output_context *context)
832
437
{
833
437
  context->out.data = context->in.data;
834
437
  context->out.used = context->in.used;
835
437
  context->out.size = context->in.size;
836
437
  context->out.free = context->in.free;
837
437
  context->in.data = NULL;
838
437
  context->in.used = 0;
839
437
  context->in.free = 0;
840
437
  context->in.size = 0;
841
437
}
842
/* }}} */
843
844
/* {{{ static void php_output_context_dtor(php_output_context *context)
845
 * Destroy the contents of an output context */
846
static inline void php_output_context_dtor(php_output_context *context)
847
14.2M
{
848
14.2M
  if (context->in.free && context->in.data) {
849
0
    efree(context->in.data);
850
0
    context->in.data = NULL;
851
0
  }
852
14.2M
  if (context->out.free && context->out.data) {
853
1
    efree(context->out.data);
854
1
    context->out.data = NULL;
855
1
  }
856
14.2M
}
857
/* }}} */
858
859
/* {{{ static php_output_handler *php_output_handler_init(zval *name, size_t chunk_size, int flags)
860
 * Allocates and initializes a php_output_handler structure */
861
static inline php_output_handler *php_output_handler_init(zend_string *name, size_t chunk_size, int flags)
862
443
{
863
443
  php_output_handler *handler;
864
865
443
  handler = ecalloc(1, sizeof(php_output_handler));
866
443
  handler->name = zend_string_copy(name);
867
443
  handler->size = chunk_size;
868
443
  handler->flags = flags;
869
443
  handler->buffer.size = PHP_OUTPUT_HANDLER_INITBUF_SIZE(chunk_size);
870
443
  handler->buffer.data = emalloc(handler->buffer.size);
871
872
443
  return handler;
873
443
}
874
/* }}} */
875
876
/* {{{ static bool php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf)
877
 * Appends input to the output handlers buffer and indicates whether the buffer does not have to be processed by the output handler */
878
static inline bool php_output_handler_append(php_output_handler *handler, const php_output_buffer *buf)
879
720k
{
880
720k
  if (buf->used) {
881
719k
    OG(flags) |= PHP_OUTPUT_WRITTEN;
882
    /* store it away */
883
719k
    if ((handler->buffer.size - handler->buffer.used) <= buf->used) {
884
27
      size_t grow_int = PHP_OUTPUT_HANDLER_INITBUF_SIZE(handler->size);
885
27
      size_t grow_buf = PHP_OUTPUT_HANDLER_INITBUF_SIZE(buf->used - (handler->buffer.size - handler->buffer.used));
886
27
      size_t grow_max = MAX(grow_int, grow_buf);
887
888
27
      handler->buffer.data = safe_erealloc(handler->buffer.data, 1, handler->buffer.size, grow_max);
889
27
      handler->buffer.size += grow_max;
890
27
    }
891
719k
    memcpy(handler->buffer.data + handler->buffer.used, buf->data, buf->used);
892
719k
    handler->buffer.used += buf->used;
893
894
    /* chunked buffering */
895
719k
    if (handler->size && (handler->buffer.used >= handler->size)) {
896
      /* store away errors and/or any intermediate output */
897
0
      return OG(running) ? true : false;
898
0
    }
899
719k
  }
900
720k
  return true;
901
720k
}
902
/* }}} */
903
904
/* {{{ static php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
905
 * Output handler operation dispatcher, applying context op to the php_output_handler handler */
906
static inline php_output_handler_status_t php_output_handler_op(php_output_handler *handler, php_output_context *context)
907
720k
{
908
720k
  php_output_handler_status_t status;
909
720k
  int original_op = context->op;
910
911
#if PHP_OUTPUT_DEBUG
912
  fprintf(stderr, ">>> op(%d, "
913
          "handler=%p, "
914
          "name=%s, "
915
          "flags=%d, "
916
          "buffer.data=%s, "
917
          "buffer.used=%zu, "
918
          "buffer.size=%zu, "
919
          "in.data=%s, "
920
          "in.used=%zu)\n",
921
      context->op,
922
      handler,
923
      handler->name,
924
      handler->flags,
925
      handler->buffer.used?handler->buffer.data:"",
926
      handler->buffer.used,
927
      handler->buffer.size,
928
      context->in.used?context->in.data:"",
929
      context->in.used
930
  );
931
#endif
932
933
720k
  if (handler->flags & PHP_OUTPUT_HANDLER_DISABLED) {
934
0
    return PHP_OUTPUT_HANDLER_FAILURE;
935
0
  }
936
937
720k
  if (php_output_lock_error(context->op)) {
938
    /* fatal error */
939
0
    return PHP_OUTPUT_HANDLER_FAILURE;
940
0
  }
941
942
  /* php_output_lock_error() doesn't fail for PHP_OUTPUT_HANDLER_WRITE but
943
   * anything that gets written will silently be discarded, remember that we
944
   * tried to write so a deprecation warning can be emitted at the end. */
945
720k
  if (context->op == PHP_OUTPUT_HANDLER_WRITE && OG(active) && OG(running)) {
946
1
    handler->flags |= PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT;
947
1
  }
948
949
720k
  bool still_have_handler = true;
950
  /* storable? */
951
720k
  if (php_output_handler_append(handler, &context->in) && !context->op) {
952
719k
    context->op = original_op;
953
719k
    return PHP_OUTPUT_HANDLER_NO_DATA;
954
719k
  } else {
955
    /* need to start? */
956
443
    if (!(handler->flags & PHP_OUTPUT_HANDLER_STARTED)) {
957
443
      context->op |= PHP_OUTPUT_HANDLER_START;
958
443
    }
959
960
443
    OG(running) = handler;
961
443
    if (handler->flags & PHP_OUTPUT_HANDLER_USER) {
962
6
      zval ob_args[2];
963
6
      zval retval;
964
965
      /* ob_data */
966
6
      ZVAL_STRINGL(&ob_args[0], handler->buffer.data, handler->buffer.used);
967
      /* ob_mode */
968
6
      ZVAL_LONG(&ob_args[1], (zend_long) context->op);
969
970
      /* Set FCI info */
971
6
      handler->func.user->fci.param_count = 2;
972
6
      handler->func.user->fci.params = ob_args;
973
6
      handler->func.user->fci.retval = &retval;
974
975
6
      if (SUCCESS == zend_call_function(&handler->func.user->fci, &handler->func.user->fcc) && Z_TYPE(retval) != IS_UNDEF) {
976
5
        if (handler->flags & PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT) {
977
          // Make sure that we don't get lost in the current output buffer
978
          // by disabling it
979
0
          handler->flags |= PHP_OUTPUT_HANDLER_DISABLED;
980
0
          if (handler->flags & PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT) {
981
            // The handler might not always produce output
982
0
            handler->flags &= ~PHP_OUTPUT_HANDLER_PRODUCED_OUTPUT;
983
0
            php_error_docref(
984
0
              NULL,
985
0
              E_DEPRECATED,
986
0
              "Producing output from user output handler %s is deprecated",
987
0
              ZSTR_VAL(handler->name)
988
0
            );
989
0
          }
990
991
          // Check if the handler is still in the list of handlers to
992
          // determine if the PHP_OUTPUT_HANDLER_DISABLED flag can
993
          // be removed
994
0
          still_have_handler = false;
995
0
          int handler_count = php_output_get_level();
996
0
          if (handler_count) {
997
0
            php_output_handler **handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
998
0
            for (int handler_num = 0; handler_num < handler_count; ++handler_num) {
999
0
              php_output_handler *curr_handler = handlers[handler_num];
1000
0
              if (curr_handler == handler) {
1001
0
                handler->flags &= (~PHP_OUTPUT_HANDLER_DISABLED);
1002
0
                still_have_handler = true;
1003
0
                break;
1004
0
              }
1005
0
            }
1006
0
          }
1007
0
        }
1008
5
        if (Z_TYPE(retval) == IS_FALSE) {
1009
          /* call failed, pass internal buffer along */
1010
0
          status = PHP_OUTPUT_HANDLER_FAILURE;
1011
5
        } else {
1012
          /* user handler may have returned TRUE */
1013
5
          status = PHP_OUTPUT_HANDLER_NO_DATA;
1014
5
          if (Z_TYPE(retval) != IS_FALSE && Z_TYPE(retval) != IS_TRUE) {
1015
5
            convert_to_string(&retval);
1016
5
            if (Z_STRLEN(retval)) {
1017
0
              context->out.data = estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
1018
0
              context->out.used = Z_STRLEN(retval);
1019
0
              context->out.free = 1;
1020
0
              status = PHP_OUTPUT_HANDLER_SUCCESS;
1021
0
            }
1022
5
          }
1023
5
        }
1024
5
      } else {
1025
        /* call failed, pass internal buffer along */
1026
1
        status = PHP_OUTPUT_HANDLER_FAILURE;
1027
1
      }
1028
1029
      /* Free arguments and return value */
1030
6
      zval_ptr_dtor(&ob_args[0]);
1031
6
      zval_ptr_dtor(&ob_args[1]);
1032
6
      zval_ptr_dtor(&retval);
1033
1034
437
    } else {
1035
1036
437
      php_output_context_feed(context, handler->buffer.data, handler->buffer.size, handler->buffer.used, 0);
1037
1038
437
      if (SUCCESS == handler->func.internal(&handler->opaq, context)) {
1039
437
        if (context->out.used) {
1040
437
          status = PHP_OUTPUT_HANDLER_SUCCESS;
1041
437
        } else {
1042
0
          status = PHP_OUTPUT_HANDLER_NO_DATA;
1043
0
        }
1044
437
      } else {
1045
0
        status = PHP_OUTPUT_HANDLER_FAILURE;
1046
0
      }
1047
437
    }
1048
443
    if (still_have_handler) {
1049
443
      handler->flags |= PHP_OUTPUT_HANDLER_STARTED;
1050
443
    }
1051
443
    OG(running) = NULL;
1052
443
  }
1053
1054
443
  if (!still_have_handler) {
1055
    // Handler and context will have both already been freed
1056
0
    return status;
1057
0
  }
1058
1059
443
  switch (status) {
1060
1
    case PHP_OUTPUT_HANDLER_FAILURE:
1061
      /* disable this handler */
1062
1
      handler->flags |= PHP_OUTPUT_HANDLER_DISABLED;
1063
      /* discard any output */
1064
1
      if (context->out.data && context->out.free) {
1065
0
        efree(context->out.data);
1066
0
      }
1067
      /* returns handlers buffer */
1068
1
      context->out.data = handler->buffer.data;
1069
1
      context->out.used = handler->buffer.used;
1070
1
      context->out.free = 1;
1071
1
      handler->buffer.data = NULL;
1072
1
      handler->buffer.used = 0;
1073
1
      handler->buffer.size = 0;
1074
1
      break;
1075
5
    case PHP_OUTPUT_HANDLER_NO_DATA:
1076
      /* handler ate all */
1077
5
      php_output_context_reset(context);
1078
5
      ZEND_FALLTHROUGH;
1079
442
    case PHP_OUTPUT_HANDLER_SUCCESS:
1080
      /* no more buffered data */
1081
442
      handler->buffer.used = 0;
1082
442
      handler->flags |= PHP_OUTPUT_HANDLER_PROCESSED;
1083
442
      break;
1084
443
  }
1085
1086
443
  context->op = original_op;
1087
443
  return status;
1088
443
}
1089
/* }}} */
1090
1091
1092
/* {{{ static void php_output_op(int op, const char *str, size_t len)
1093
 * Output op dispatcher, passes input and output handlers output through the output handler stack until it gets written to the SAPI */
1094
static inline void php_output_op(int op, const char *str, size_t len)
1095
14.2M
{
1096
14.2M
  php_output_context context;
1097
14.2M
  php_output_handler **active;
1098
14.2M
  int obh_cnt;
1099
1100
14.2M
  if (php_output_lock_error(op)) {
1101
0
    return;
1102
0
  }
1103
1104
14.2M
  php_output_context_init(&context, op);
1105
1106
  /*
1107
   * broken up for better performance:
1108
   *  - apply op to the one active handler; note that OG(active) might be popped off the stack on a flush
1109
   *  - or apply op to the handler stack
1110
   */
1111
14.2M
  if (OG(active) && (obh_cnt = zend_stack_count(&OG(handlers)))) {
1112
719k
    context.in.data = (char *) str;
1113
719k
    context.in.used = len;
1114
1115
719k
    if (obh_cnt > 1) {
1116
0
      zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_TOPDOWN, php_output_stack_apply_op, &context);
1117
719k
    } else if ((active = zend_stack_top(&OG(handlers))) && (!((*active)->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
1118
719k
      php_output_handler_op(*active, &context);
1119
719k
    } else {
1120
0
      php_output_context_pass(&context);
1121
0
    }
1122
13.4M
  } else {
1123
13.4M
    context.out.data = (char *) str;
1124
13.4M
    context.out.used = len;
1125
13.4M
  }
1126
1127
14.2M
  if (context.out.data && context.out.used) {
1128
13.4M
    php_output_header();
1129
1130
13.4M
    if (!(OG(flags) & PHP_OUTPUT_DISABLED)) {
1131
#if PHP_OUTPUT_DEBUG
1132
      fprintf(stderr, "::: sapi_write('%s', %zu)\n", context.out.data, context.out.used);
1133
#endif
1134
13.4M
      sapi_module.ub_write(context.out.data, context.out.used);
1135
1136
13.4M
      if (OG(flags) & PHP_OUTPUT_IMPLICITFLUSH) {
1137
13.4M
        sapi_flush();
1138
13.4M
      }
1139
1140
13.4M
      OG(flags) |= PHP_OUTPUT_SENT;
1141
13.4M
    }
1142
13.4M
  }
1143
14.2M
  php_output_context_dtor(&context);
1144
14.2M
}
1145
/* }}} */
1146
1147
/* {{{ static int php_output_stack_apply_op(void *h, void *c)
1148
 * Operation callback for the stack apply function */
1149
static int php_output_stack_apply_op(void *h, void *c)
1150
0
{
1151
0
  int was_disabled;
1152
0
  php_output_handler_status_t status;
1153
0
  php_output_handler *handler = *(php_output_handler **) h;
1154
0
  php_output_context *context = (php_output_context *) c;
1155
1156
0
  if ((was_disabled = (handler->flags & PHP_OUTPUT_HANDLER_DISABLED))) {
1157
0
    status = PHP_OUTPUT_HANDLER_FAILURE;
1158
0
  } else {
1159
0
    status = php_output_handler_op(handler, context);
1160
0
  }
1161
1162
  /*
1163
   * handler ate all => break
1164
   * handler returned data or failed resp. is disabled => continue
1165
   */
1166
0
  switch (status) {
1167
0
    case PHP_OUTPUT_HANDLER_NO_DATA:
1168
0
      return 1;
1169
1170
0
    case PHP_OUTPUT_HANDLER_SUCCESS:
1171
      /* swap contexts buffers, unless this is the last handler in the stack */
1172
0
      if (handler->level) {
1173
0
        php_output_context_swap(context);
1174
0
      }
1175
0
      return 0;
1176
1177
0
    case PHP_OUTPUT_HANDLER_FAILURE:
1178
0
    default:
1179
0
      if (was_disabled) {
1180
        /* pass input along, if it's the last handler in the stack */
1181
0
        if (!handler->level) {
1182
0
          php_output_context_pass(context);
1183
0
        }
1184
0
      } else {
1185
        /* swap buffers, unless this is the last handler */
1186
0
        if (handler->level) {
1187
0
          php_output_context_swap(context);
1188
0
        }
1189
0
      }
1190
0
      return 0;
1191
0
  }
1192
0
}
1193
/* }}} */
1194
1195
/* {{{ static int php_output_stack_apply_clean(void *h, void *c)
1196
 * Clean callback for the stack apply function */
1197
static int php_output_stack_apply_clean(void *h, void *c)
1198
0
{
1199
0
  php_output_handler *handler = *(php_output_handler **) h;
1200
0
  php_output_context *context = (php_output_context *) c;
1201
1202
0
  handler->buffer.used = 0;
1203
0
  php_output_handler_op(handler, context);
1204
0
  php_output_context_reset(context);
1205
0
  return 0;
1206
0
}
1207
/* }}} */
1208
1209
/* {{{ static int php_output_stack_apply_list(void *h, void *z)
1210
 * List callback for the stack apply function */
1211
static int php_output_stack_apply_list(void *h, void *z)
1212
0
{
1213
0
  php_output_handler *handler = *(php_output_handler **) h;
1214
0
  zval *array = (zval *) z;
1215
1216
0
  add_next_index_str(array, zend_string_copy(handler->name));
1217
0
  return 0;
1218
0
}
1219
/* }}} */
1220
1221
/* {{{ static int php_output_stack_apply_status(void *h, void *z)
1222
 * Status callback for the stack apply function */
1223
static int php_output_stack_apply_status(void *h, void *z)
1224
0
{
1225
0
  php_output_handler *handler = *(php_output_handler **) h;
1226
0
  zval arr, *array = (zval *) z;
1227
1228
0
  add_next_index_zval(array, php_output_handler_status(handler, &arr));
1229
1230
0
  return 0;
1231
0
}
1232
1233
/* {{{ static zval *php_output_handler_status(php_output_handler *handler, zval *entry)
1234
 * Returns an array with the status of the output handler */
1235
static inline zval *php_output_handler_status(php_output_handler *handler, zval *entry)
1236
0
{
1237
0
  ZEND_ASSERT(entry != NULL);
1238
1239
0
  array_init(entry);
1240
0
  add_assoc_str(entry, "name", zend_string_copy(handler->name));
1241
0
  add_assoc_long(entry, "type", (zend_long) (handler->flags & 0xf));
1242
0
  add_assoc_long(entry, "flags", (zend_long) handler->flags);
1243
0
  add_assoc_long(entry, "level", (zend_long) handler->level);
1244
0
  add_assoc_long(entry, "chunk_size", (zend_long) handler->size);
1245
0
  add_assoc_long(entry, "buffer_size", (zend_long) handler->buffer.size);
1246
0
  add_assoc_long(entry, "buffer_used", (zend_long) handler->buffer.used);
1247
1248
0
  return entry;
1249
0
}
1250
/* }}} */
1251
1252
/* {{{ static int php_output_stack_pop(int flags)
1253
 * Pops an output handler off the stack */
1254
static int php_output_stack_pop(int flags)
1255
443
{
1256
443
  php_output_context context;
1257
443
  php_output_handler **current, *orphan = OG(active);
1258
1259
443
  if (!orphan) {
1260
0
    if (!(flags & PHP_OUTPUT_POP_SILENT)) {
1261
0
      php_error_docref("ref.outcontrol", E_NOTICE, "Failed to %s buffer. No buffer to %s", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send");
1262
0
    }
1263
0
    return 0;
1264
443
  } else if (!(flags & PHP_OUTPUT_POP_FORCE) && !(orphan->flags & PHP_OUTPUT_HANDLER_REMOVABLE)) {
1265
0
    if (!(flags & PHP_OUTPUT_POP_SILENT)) {
1266
0
      php_error_docref("ref.outcontrol", E_NOTICE, "Failed to %s buffer of %s (%d)", (flags&PHP_OUTPUT_POP_DISCARD)?"discard":"send", ZSTR_VAL(orphan->name), orphan->level);
1267
0
    }
1268
0
    return 0;
1269
443
  } else {
1270
443
    php_output_context_init(&context, PHP_OUTPUT_HANDLER_FINAL);
1271
1272
    /* don't run the output handler if it's disabled */
1273
443
    if (!(orphan->flags & PHP_OUTPUT_HANDLER_DISABLED)) {
1274
      /* didn't it start yet? */
1275
443
      if (!(orphan->flags & PHP_OUTPUT_HANDLER_STARTED)) {
1276
443
        context.op |= PHP_OUTPUT_HANDLER_START;
1277
443
      }
1278
      /* signal that we're cleaning up */
1279
443
      if (flags & PHP_OUTPUT_POP_DISCARD) {
1280
439
        context.op |= PHP_OUTPUT_HANDLER_CLEAN;
1281
439
      }
1282
443
      php_output_handler_op(orphan, &context);
1283
443
    }
1284
    // If it isn't still in the stack, cannot free it
1285
443
    bool still_have_handler = false;
1286
443
    int handler_count = php_output_get_level();
1287
443
    if (handler_count) {
1288
443
      php_output_handler **handlers = (php_output_handler **) zend_stack_base(&OG(handlers));
1289
443
      for (int handler_num = 0; handler_num < handler_count; ++handler_num) {
1290
443
        php_output_handler *curr_handler = handlers[handler_num];
1291
443
        if (curr_handler == orphan) {
1292
443
          still_have_handler = true;
1293
443
          break;
1294
443
        }
1295
443
      }
1296
443
    }
1297
1298
    /* pop it off the stack */
1299
443
    zend_stack_del_top(&OG(handlers));
1300
443
    if ((current = zend_stack_top(&OG(handlers)))) {
1301
0
      OG(active) = *current;
1302
443
    } else {
1303
443
      OG(active) = NULL;
1304
443
    }
1305
1306
    /* pass output along */
1307
443
    if (context.out.data && context.out.used && !(flags & PHP_OUTPUT_POP_DISCARD)) {
1308
2
      php_output_write(context.out.data, context.out.used);
1309
2
    }
1310
1311
    /* destroy the handler (after write!) */
1312
443
    if (still_have_handler) {
1313
443
      php_output_handler_free(&orphan);
1314
443
    }
1315
443
    php_output_context_dtor(&context);
1316
1317
443
    return 1;
1318
443
  }
1319
443
}
1320
/* }}} */
1321
1322
/* {{{ static SUCCESS|FAILURE php_output_handler_compat_func(void *ctx, php_output_context *)
1323
 * php_output_handler_context_func_t for php_output_handler_func_t output handlers */
1324
static zend_result php_output_handler_compat_func(void **handler_context, php_output_context *output_context)
1325
0
{
1326
0
  php_output_handler_func_t func = *(php_output_handler_func_t *) handler_context;
1327
1328
0
  if (func) {
1329
0
    char *out_str = NULL;
1330
0
    size_t out_len = 0;
1331
1332
0
    func(output_context->in.data, output_context->in.used, &out_str, &out_len, output_context->op);
1333
1334
0
    if (out_str) {
1335
0
      output_context->out.data = out_str;
1336
0
      output_context->out.used = out_len;
1337
0
      output_context->out.free = 1;
1338
0
    } else {
1339
0
      php_output_context_pass(output_context);
1340
0
    }
1341
1342
0
    return SUCCESS;
1343
0
  }
1344
0
  return FAILURE;
1345
0
}
1346
/* }}} */
1347
1348
/* {{{ static SUCCESS|FAILURE php_output_handler_default_func(void *ctx, php_output_context *)
1349
 * Default output handler */
1350
static zend_result php_output_handler_default_func(void **handler_context, php_output_context *output_context)
1351
437
{
1352
437
  php_output_context_pass(output_context);
1353
437
  return SUCCESS;
1354
437
}
1355
/* }}} */
1356
1357
/* {{{ static SUCCESS|FAILURE php_output_handler_devnull_func(void *ctx, php_output_context *)
1358
 * Null output handler */
1359
static zend_result php_output_handler_devnull_func(void **handler_context, php_output_context *output_context)
1360
0
{
1361
0
  return SUCCESS;
1362
0
}
1363
/* }}} */
1364
1365
/*
1366
 * USERLAND (nearly 1:1 of old output.c)
1367
 */
1368
1369
/* {{{ Turn on Output Buffering (specifying an optional output handler). */
1370
PHP_FUNCTION(ob_start)
1371
7
{
1372
7
  zval *output_handler = NULL;
1373
7
  zend_long chunk_size = 0;
1374
7
  zend_long flags = PHP_OUTPUT_HANDLER_STDFLAGS;
1375
1376
7
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|zll", &output_handler, &chunk_size, &flags) == FAILURE) {
1377
0
    RETURN_THROWS();
1378
0
  }
1379
1380
7
  if (chunk_size < 0) {
1381
0
    chunk_size = 0;
1382
0
  }
1383
1384
7
  if (php_output_start_user(output_handler, chunk_size, flags) == FAILURE) {
1385
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to create buffer");
1386
0
    RETURN_FALSE;
1387
0
  }
1388
7
  RETURN_TRUE;
1389
7
}
1390
/* }}} */
1391
1392
/* {{{ Flush (send) contents of the output buffer. The last buffer content is sent to next buffer */
1393
PHP_FUNCTION(ob_flush)
1394
0
{
1395
0
  if (zend_parse_parameters_none() == FAILURE) {
1396
0
    RETURN_THROWS();
1397
0
  }
1398
1399
0
  if (!OG(active)) {
1400
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to flush buffer. No buffer to flush");
1401
0
    RETURN_FALSE;
1402
0
  }
1403
1404
0
  if (SUCCESS != php_output_flush()) {
1405
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to flush buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1406
0
    RETURN_FALSE;
1407
0
  }
1408
0
  RETURN_TRUE;
1409
0
}
1410
/* }}} */
1411
1412
/* {{{ Clean (delete) the current output buffer */
1413
PHP_FUNCTION(ob_clean)
1414
0
{
1415
0
  if (zend_parse_parameters_none() == FAILURE) {
1416
0
    RETURN_THROWS();
1417
0
  }
1418
1419
0
  if (!OG(active)) {
1420
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1421
0
    RETURN_FALSE;
1422
0
  }
1423
1424
0
  if (SUCCESS != php_output_clean()) {
1425
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1426
0
    RETURN_FALSE;
1427
0
  }
1428
0
  RETURN_TRUE;
1429
0
}
1430
/* }}} */
1431
1432
/* {{{ Flush (send) the output buffer, and delete current output buffer */
1433
PHP_FUNCTION(ob_end_flush)
1434
0
{
1435
0
  if (zend_parse_parameters_none() == FAILURE) {
1436
0
    RETURN_THROWS();
1437
0
  }
1438
1439
0
  if (!OG(active)) {
1440
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete and flush buffer. No buffer to delete or flush");
1441
0
    RETURN_FALSE;
1442
0
  }
1443
1444
0
  RETURN_BOOL(SUCCESS == php_output_end());
1445
0
}
1446
/* }}} */
1447
1448
/* {{{ Clean the output buffer, and delete current output buffer */
1449
PHP_FUNCTION(ob_end_clean)
1450
4
{
1451
4
  if (zend_parse_parameters_none() == FAILURE) {
1452
0
    RETURN_THROWS();
1453
0
  }
1454
1455
4
  if (!OG(active)) {
1456
1
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1457
1
    RETURN_FALSE;
1458
1
  }
1459
1460
3
  RETURN_BOOL(SUCCESS == php_output_discard());
1461
3
}
1462
/* }}} */
1463
1464
/* {{{ Get current buffer contents, flush (send) the output buffer, and delete current output buffer */
1465
PHP_FUNCTION(ob_get_flush)
1466
0
{
1467
0
  if (zend_parse_parameters_none() == FAILURE) {
1468
0
    RETURN_THROWS();
1469
0
  }
1470
1471
0
  if (php_output_get_contents(return_value) == FAILURE) {
1472
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete and flush buffer. No buffer to delete or flush");
1473
0
    RETURN_FALSE;
1474
0
  }
1475
1476
0
  if (SUCCESS != php_output_end()) {
1477
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1478
0
  }
1479
0
}
1480
/* }}} */
1481
1482
/* {{{ Get current buffer contents and delete current output buffer */
1483
PHP_FUNCTION(ob_get_clean)
1484
1
{
1485
1
  if (zend_parse_parameters_none() == FAILURE) {
1486
0
    RETURN_THROWS();
1487
0
  }
1488
1489
1
  if(!OG(active)) {
1490
0
    RETURN_FALSE;
1491
0
  }
1492
1493
1
  if (php_output_get_contents(return_value) == FAILURE) {
1494
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer. No buffer to delete");
1495
0
    RETURN_FALSE;
1496
0
  }
1497
1498
1
  if (SUCCESS != php_output_discard()) {
1499
0
    php_error_docref("ref.outcontrol", E_NOTICE, "Failed to delete buffer of %s (%d)", ZSTR_VAL(OG(active)->name), OG(active)->level);
1500
0
  }
1501
1
}
1502
/* }}} */
1503
1504
/* {{{ Return the contents of the output buffer */
1505
PHP_FUNCTION(ob_get_contents)
1506
0
{
1507
0
  if (zend_parse_parameters_none() == FAILURE) {
1508
0
    RETURN_THROWS();
1509
0
  }
1510
1511
0
  if (php_output_get_contents(return_value) == FAILURE) {
1512
0
    RETURN_FALSE;
1513
0
  }
1514
0
}
1515
/* }}} */
1516
1517
/* {{{ Return the nesting level of the output buffer */
1518
PHP_FUNCTION(ob_get_level)
1519
0
{
1520
0
  if (zend_parse_parameters_none() == FAILURE) {
1521
0
    RETURN_THROWS();
1522
0
  }
1523
1524
0
  RETURN_LONG(php_output_get_level());
1525
0
}
1526
/* }}} */
1527
1528
/* {{{ Return the length of the output buffer */
1529
PHP_FUNCTION(ob_get_length)
1530
0
{
1531
0
  if (zend_parse_parameters_none() == FAILURE) {
1532
0
    RETURN_THROWS();
1533
0
  }
1534
1535
0
  if (php_output_get_length(return_value) == FAILURE) {
1536
0
    RETURN_FALSE;
1537
0
  }
1538
0
}
1539
/* }}} */
1540
1541
/* {{{ List all output_buffers in an array */
1542
PHP_FUNCTION(ob_list_handlers)
1543
0
{
1544
0
  if (zend_parse_parameters_none() == FAILURE) {
1545
0
    RETURN_THROWS();
1546
0
  }
1547
1548
0
  array_init(return_value);
1549
1550
0
  if (!OG(active)) {
1551
0
    return;
1552
0
  }
1553
1554
0
  zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_list, return_value);
1555
0
}
1556
/* }}} */
1557
1558
/* {{{ Return the status of the active or all output buffers */
1559
PHP_FUNCTION(ob_get_status)
1560
0
{
1561
0
  bool full_status = 0;
1562
1563
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &full_status) == FAILURE) {
1564
0
    RETURN_THROWS();
1565
0
  }
1566
1567
0
  if (!OG(active)) {
1568
0
    array_init(return_value);
1569
0
    return;
1570
0
  }
1571
1572
0
  if (full_status) {
1573
0
    array_init(return_value);
1574
0
    zend_stack_apply_with_argument(&OG(handlers), ZEND_STACK_APPLY_BOTTOMUP, php_output_stack_apply_status, return_value);
1575
0
  } else {
1576
0
    php_output_handler_status(OG(active), return_value);
1577
0
  }
1578
0
}
1579
/* }}} */
1580
1581
/* {{{ Turn implicit flush on/off and is equivalent to calling flush() after every output call */
1582
PHP_FUNCTION(ob_implicit_flush)
1583
0
{
1584
0
  zend_long flag = 1;
1585
1586
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &flag) == FAILURE) {
1587
0
    RETURN_THROWS();
1588
0
  }
1589
1590
0
  php_output_set_implicit_flush((int) flag);
1591
0
}
1592
/* }}} */
1593
1594
/* {{{ Reset(clear) URL rewriter values */
1595
PHP_FUNCTION(output_reset_rewrite_vars)
1596
0
{
1597
0
  if (zend_parse_parameters_none() == FAILURE) {
1598
0
    RETURN_THROWS();
1599
0
  }
1600
1601
0
  if (php_url_scanner_reset_vars() == SUCCESS) {
1602
0
    RETURN_TRUE;
1603
0
  } else {
1604
0
    RETURN_FALSE;
1605
0
  }
1606
0
}
1607
/* }}} */
1608
1609
/* {{{ Add URL rewriter values */
1610
PHP_FUNCTION(output_add_rewrite_var)
1611
0
{
1612
0
  char *name, *value;
1613
0
  size_t name_len, value_len;
1614
1615
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &value, &value_len) == FAILURE) {
1616
0
    RETURN_THROWS();
1617
0
  }
1618
1619
0
  if (php_url_scanner_add_var(name, name_len, value, value_len, 1) == SUCCESS) {
1620
0
    RETURN_TRUE;
1621
0
  } else {
1622
0
    RETURN_FALSE;
1623
0
  }
1624
0
}
1625
/* }}} */