xref: /PHP-7.2/ext/zlib/zlib.c (revision 902d39a3)
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    | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
16    |          Stefan Röhrich <sr@linux.de>                                |
17    |          Zeev Suraski <zeev@zend.com>                                |
18    |          Jade Nicoletti <nicoletti@nns.ch>                           |
19    |          Michael Wallner <mike@php.net>                              |
20    +----------------------------------------------------------------------+
21  */
22 
23 /* $Id$ */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include "php.h"
30 #include "SAPI.h"
31 #include "php_ini.h"
32 #include "ext/standard/info.h"
33 #include "ext/standard/file.h"
34 #include "ext/standard/php_string.h"
35 #include "php_zlib.h"
36 
37 /*
38  * zlib include files can define the following preprocessor defines which rename
39  * the corresponding PHP functions to gzopen64, gzseek64 and gztell64 and thereby
40  * breaking some software, most notably PEAR's Archive_Tar, which halts execution
41  * without error message on gzip compressed archivesa.
42  *
43  * This only seems to happen on 32bit systems with large file support.
44  */
45 #undef gzopen
46 #undef gzseek
47 #undef gztell
48 
49 int le_deflate;
50 int le_inflate;
51 
52 ZEND_DECLARE_MODULE_GLOBALS(zlib);
53 
54 /* {{{ Memory management wrappers */
55 
php_zlib_alloc(voidpf opaque,uInt items,uInt size)56 static voidpf php_zlib_alloc(voidpf opaque, uInt items, uInt size)
57 {
58 	return (voidpf)safe_emalloc(items, size, 0);
59 }
60 
php_zlib_free(voidpf opaque,voidpf address)61 static void php_zlib_free(voidpf opaque, voidpf address)
62 {
63 	efree((void*)address);
64 }
65 /* }}} */
66 
67 /* {{{ Incremental deflate/inflate resource destructors */
68 
deflate_rsrc_dtor(zend_resource * res)69 void deflate_rsrc_dtor(zend_resource *res)
70 {
71 	z_stream *ctx = zend_fetch_resource(res, NULL, le_deflate);
72 	deflateEnd(ctx);
73 	efree(ctx);
74 }
75 
inflate_rsrc_dtor(zend_resource * res)76 void inflate_rsrc_dtor(zend_resource *res)
77 {
78 	z_stream *ctx = zend_fetch_resource(res, NULL, le_inflate);
79 	if (((php_zlib_context *) ctx)->inflateDict) {
80 		efree(((php_zlib_context *) ctx)->inflateDict);
81 	}
82 	inflateEnd(ctx);
83 	efree(ctx);
84 }
85 
86 /* }}} */
87 
88 /* {{{ php_zlib_output_conflict_check() */
php_zlib_output_conflict_check(const char * handler_name,size_t handler_name_len)89 static int php_zlib_output_conflict_check(const char *handler_name, size_t handler_name_len)
90 {
91 	if (php_output_get_level() > 0) {
92 		if (php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME))
93 		||	php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("ob_gzhandler"))
94 		||  php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("mb_output_handler"))
95 		||	php_output_handler_conflict(handler_name, handler_name_len, ZEND_STRL("URL-Rewriter"))) {
96 			return FAILURE;
97 		}
98 	}
99 	return SUCCESS;
100 }
101 /* }}} */
102 
103 /* {{{ php_zlib_output_encoding() */
php_zlib_output_encoding(void)104 static int php_zlib_output_encoding(void)
105 {
106 	zval *enc;
107 
108 	if (!ZLIBG(compression_coding)) {
109 		if ((Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) &&
110 			(enc = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING") - 1))) {
111 			convert_to_string(enc);
112 			if (strstr(Z_STRVAL_P(enc), "gzip")) {
113 				ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_GZIP;
114 			} else if (strstr(Z_STRVAL_P(enc), "deflate")) {
115 				ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_DEFLATE;
116 			}
117 		}
118 	}
119 	return ZLIBG(compression_coding);
120 }
121 /* }}} */
122 
123 /* {{{ php_zlib_output_handler_ex() */
php_zlib_output_handler_ex(php_zlib_context * ctx,php_output_context * output_context)124 static int php_zlib_output_handler_ex(php_zlib_context *ctx, php_output_context *output_context)
125 {
126 	int flags = Z_SYNC_FLUSH;
127 
128 	if (output_context->op & PHP_OUTPUT_HANDLER_START) {
129 		/* start up */
130 		if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
131 			return FAILURE;
132 		}
133 	}
134 
135 	if (output_context->op & PHP_OUTPUT_HANDLER_CLEAN) {
136 		/* free buffers */
137 		deflateEnd(&ctx->Z);
138 
139 		if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
140 			/* discard */
141 			return SUCCESS;
142 		} else {
143 			/* restart */
144 			if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
145 				return FAILURE;
146 			}
147 			ctx->buffer.used = 0;
148 		}
149 	} else {
150 		if (output_context->in.used) {
151 			/* append input */
152 			if (ctx->buffer.free < output_context->in.used) {
153 				if (!(ctx->buffer.aptr = erealloc_recoverable(ctx->buffer.data, ctx->buffer.used + ctx->buffer.free + output_context->in.used))) {
154 					deflateEnd(&ctx->Z);
155 					return FAILURE;
156 				}
157 				ctx->buffer.data = ctx->buffer.aptr;
158 				ctx->buffer.free += output_context->in.used;
159 			}
160 			memcpy(ctx->buffer.data + ctx->buffer.used, output_context->in.data, output_context->in.used);
161 			ctx->buffer.free -= output_context->in.used;
162 			ctx->buffer.used += output_context->in.used;
163 		}
164 		output_context->out.size = PHP_ZLIB_BUFFER_SIZE_GUESS(output_context->in.used);
165 		output_context->out.data = emalloc(output_context->out.size);
166 		output_context->out.free = 1;
167 		output_context->out.used = 0;
168 
169 		ctx->Z.avail_in = ctx->buffer.used;
170 		ctx->Z.next_in = (Bytef *) ctx->buffer.data;
171 		ctx->Z.avail_out = output_context->out.size;
172 		ctx->Z.next_out = (Bytef *) output_context->out.data;
173 
174 		if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
175 			flags = Z_FINISH;
176 		} else if (output_context->op & PHP_OUTPUT_HANDLER_FLUSH) {
177 			flags = Z_FULL_FLUSH;
178 		}
179 
180 		switch (deflate(&ctx->Z, flags)) {
181 			case Z_OK:
182 				if (flags == Z_FINISH) {
183 					deflateEnd(&ctx->Z);
184 					return FAILURE;
185 				}
186 			case Z_STREAM_END:
187 				if (ctx->Z.avail_in) {
188 					memmove(ctx->buffer.data, ctx->buffer.data + ctx->buffer.used - ctx->Z.avail_in, ctx->Z.avail_in);
189 				}
190 				ctx->buffer.free += ctx->buffer.used - ctx->Z.avail_in;
191 				ctx->buffer.used = ctx->Z.avail_in;
192 				output_context->out.used = output_context->out.size - ctx->Z.avail_out;
193 				break;
194 			default:
195 				deflateEnd(&ctx->Z);
196 				return FAILURE;
197 		}
198 
199 		if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
200 			deflateEnd(&ctx->Z);
201 		}
202 	}
203 
204 	return SUCCESS;
205 }
206 /* }}} */
207 
208 /* {{{ php_zlib_output_handler() */
php_zlib_output_handler(void ** handler_context,php_output_context * output_context)209 static int php_zlib_output_handler(void **handler_context, php_output_context *output_context)
210 {
211 	php_zlib_context *ctx = *(php_zlib_context **) handler_context;
212 
213 	if (!php_zlib_output_encoding()) {
214 		/* "Vary: Accept-Encoding" header sent along uncompressed content breaks caching in MSIE,
215 			so let's just send it with successfully compressed content or unless the complete
216 			buffer gets discarded, see http://bugs.php.net/40325;
217 
218 			Test as follows:
219 			+Vary: $ HTTP_ACCEPT_ENCODING=gzip ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n";'
220 			+Vary: $ HTTP_ACCEPT_ENCODING= ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n";'
221 			-Vary: $ HTTP_ACCEPT_ENCODING=gzip ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n"; ob_end_clean();'
222 			-Vary: $ HTTP_ACCEPT_ENCODING= ./sapi/cgi/php <<<'<?php ob_start("ob_gzhandler"); echo "foo\n"; ob_end_clean();'
223 		*/
224 		if ((output_context->op & PHP_OUTPUT_HANDLER_START)
225 		&&	(output_context->op != (PHP_OUTPUT_HANDLER_START|PHP_OUTPUT_HANDLER_CLEAN|PHP_OUTPUT_HANDLER_FINAL))
226 		) {
227 			sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
228 		}
229 		return FAILURE;
230 	}
231 
232 	if (SUCCESS != php_zlib_output_handler_ex(ctx, output_context)) {
233 		return FAILURE;
234 	}
235 
236 	if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) {
237 		int flags;
238 
239 		if (SUCCESS == php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags)) {
240 			/* only run this once */
241 			if (!(flags & PHP_OUTPUT_HANDLER_STARTED)) {
242 				if (SG(headers_sent) || !ZLIBG(output_compression)) {
243 					deflateEnd(&ctx->Z);
244 					return FAILURE;
245 				}
246 				switch (ZLIBG(compression_coding)) {
247 					case PHP_ZLIB_ENCODING_GZIP:
248 						sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1);
249 						break;
250 					case PHP_ZLIB_ENCODING_DEFLATE:
251 						sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1);
252 						break;
253 					default:
254 						deflateEnd(&ctx->Z);
255 						return FAILURE;
256 				}
257 				sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
258 				php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL);
259 			}
260 		}
261 	}
262 
263 	return SUCCESS;
264 }
265 /* }}} */
266 
267 /* {{{ php_zlib_output_handler_context_init() */
php_zlib_output_handler_context_init(void)268 static php_zlib_context *php_zlib_output_handler_context_init(void)
269 {
270 	php_zlib_context *ctx = (php_zlib_context *) ecalloc(1, sizeof(php_zlib_context));
271 	ctx->Z.zalloc = php_zlib_alloc;
272 	ctx->Z.zfree = php_zlib_free;
273 	return ctx;
274 }
275 /* }}} */
276 
277 /* {{{ php_zlib_output_handler_context_dtor() */
php_zlib_output_handler_context_dtor(void * opaq)278 static void php_zlib_output_handler_context_dtor(void *opaq)
279 {
280 	php_zlib_context *ctx = (php_zlib_context *) opaq;
281 
282 	if (ctx) {
283 		if (ctx->buffer.data) {
284 			efree(ctx->buffer.data);
285 		}
286 		efree(ctx);
287 	}
288 }
289 /* }}} */
290 
291 /* {{{ php_zlib_output_handler_init() */
php_zlib_output_handler_init(const char * handler_name,size_t handler_name_len,size_t chunk_size,int flags)292 static php_output_handler *php_zlib_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags)
293 {
294 	php_output_handler *h = NULL;
295 
296 	if (!ZLIBG(output_compression)) {
297 		ZLIBG(output_compression) = chunk_size ? chunk_size : PHP_OUTPUT_HANDLER_DEFAULT_SIZE;
298 	}
299 
300     ZLIBG(handler_registered) = 1;
301 
302 	if ((h = php_output_handler_create_internal(handler_name, handler_name_len, php_zlib_output_handler, chunk_size, flags))) {
303 		php_output_handler_set_context(h, php_zlib_output_handler_context_init(), php_zlib_output_handler_context_dtor);
304 	}
305 
306 	return h;
307 }
308 /* }}} */
309 
310 /* {{{ php_zlib_output_compression_start() */
php_zlib_output_compression_start(void)311 static void php_zlib_output_compression_start(void)
312 {
313 	zval zoh;
314 	php_output_handler *h;
315 
316 	switch (ZLIBG(output_compression)) {
317 		case 0:
318 			break;
319 		case 1:
320 			ZLIBG(output_compression) = PHP_OUTPUT_HANDLER_DEFAULT_SIZE;
321 			/* break omitted intentionally */
322 		default:
323 			if (	php_zlib_output_encoding() &&
324 					(h = php_zlib_output_handler_init(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME), ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS)) &&
325 					(SUCCESS == php_output_handler_start(h))) {
326 				if (ZLIBG(output_handler) && *ZLIBG(output_handler)) {
327 					ZVAL_STRING(&zoh, ZLIBG(output_handler));
328 					php_output_start_user(&zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS);
329 					zval_ptr_dtor(&zoh);
330 				}
331 			}
332 			break;
333 	}
334 }
335 /* }}} */
336 
337 /* {{{ php_zlib_encode() */
php_zlib_encode(const char * in_buf,size_t in_len,int encoding,int level)338 static zend_string *php_zlib_encode(const char *in_buf, size_t in_len, int encoding, int level)
339 {
340 	int status;
341 	z_stream Z;
342 	zend_string *out;
343 
344 	memset(&Z, 0, sizeof(z_stream));
345 	Z.zalloc = php_zlib_alloc;
346 	Z.zfree = php_zlib_free;
347 
348 	if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
349 		out = zend_string_alloc(PHP_ZLIB_BUFFER_SIZE_GUESS(in_len), 0);
350 
351 		Z.next_in = (Bytef *) in_buf;
352 		Z.next_out = (Bytef *) ZSTR_VAL(out);
353 		Z.avail_in = in_len;
354 		Z.avail_out = ZSTR_LEN(out);
355 
356 		status = deflate(&Z, Z_FINISH);
357 		deflateEnd(&Z);
358 
359 		if (Z_STREAM_END == status) {
360 			/* size buffer down to actual length */
361 			out = zend_string_truncate(out, Z.total_out, 0);
362 			ZSTR_VAL(out)[ZSTR_LEN(out)] = '\0';
363 			return out;
364 		} else {
365 			zend_string_free(out);
366 		}
367 	}
368 
369 	php_error_docref(NULL, E_WARNING, "%s", zError(status));
370 	return NULL;
371 }
372 /* }}} */
373 
374 /* {{{ php_zlib_inflate_rounds() */
php_zlib_inflate_rounds(z_stream * Z,size_t max,char ** buf,size_t * len)375 static inline int php_zlib_inflate_rounds(z_stream *Z, size_t max, char **buf, size_t *len)
376 {
377 	int status, round = 0;
378 	php_zlib_buffer buffer = {NULL, NULL, 0, 0, 0};
379 
380 	*buf = NULL;
381 	*len = 0;
382 
383 	buffer.size = (max && (max < Z->avail_in)) ? max : Z->avail_in;
384 
385 	do {
386 		if ((max && (max <= buffer.used)) || !(buffer.aptr = erealloc_recoverable(buffer.data, buffer.size))) {
387 			status = Z_MEM_ERROR;
388 		} else {
389 			buffer.data = buffer.aptr;
390 			Z->avail_out = buffer.free = buffer.size - buffer.used;
391 			Z->next_out = (Bytef *) buffer.data + buffer.used;
392 #if 0
393 			fprintf(stderr, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
394 #endif
395 			status = inflate(Z, Z_NO_FLUSH);
396 
397 			buffer.used += buffer.free - Z->avail_out;
398 			buffer.free = Z->avail_out;
399 #if 0
400 			fprintf(stderr, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
401 #endif
402 			buffer.size += (buffer.size >> 3) + 1;
403 		}
404 	} while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < 100);
405 
406 	if (status == Z_STREAM_END) {
407 		buffer.data = erealloc(buffer.data, buffer.used + 1);
408 		buffer.data[buffer.used] = '\0';
409 		*buf = buffer.data;
410 		*len = buffer.used;
411 	} else {
412 		if (buffer.data) {
413 			efree(buffer.data);
414 		}
415 		/* HACK: See zlib/examples/zpipe.c inf() function for explanation. */
416 		/* This works as long as this function is not used for streaming. Required to catch very short invalid data. */
417 		status = (status == Z_OK) ? Z_DATA_ERROR : status;
418 	}
419 	return status;
420 }
421 /* }}} */
422 
423 /* {{{ php_zlib_decode() */
php_zlib_decode(const char * in_buf,size_t in_len,char ** out_buf,size_t * out_len,int encoding,size_t max_len)424 static int php_zlib_decode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, size_t max_len)
425 {
426 	int status = Z_DATA_ERROR;
427 	z_stream Z;
428 
429 	memset(&Z, 0, sizeof(z_stream));
430 	Z.zalloc = php_zlib_alloc;
431 	Z.zfree = php_zlib_free;
432 
433 	if (in_len) {
434 retry_raw_inflate:
435 		status = inflateInit2(&Z, encoding);
436 		if (Z_OK == status) {
437 			Z.next_in = (Bytef *) in_buf;
438 			Z.avail_in = in_len + 1; /* NOTE: data must be zero terminated */
439 
440 			switch (status = php_zlib_inflate_rounds(&Z, max_len, out_buf, out_len)) {
441 				case Z_STREAM_END:
442 					inflateEnd(&Z);
443 					return SUCCESS;
444 
445 				case Z_DATA_ERROR:
446 					/* raw deflated data? */
447 					if (PHP_ZLIB_ENCODING_ANY == encoding) {
448 						inflateEnd(&Z);
449 						encoding = PHP_ZLIB_ENCODING_RAW;
450 						goto retry_raw_inflate;
451 					}
452 			}
453 			inflateEnd(&Z);
454 		}
455 	}
456 
457 	*out_buf = NULL;
458 	*out_len = 0;
459 
460 	php_error_docref(NULL, E_WARNING, "%s", zError(status));
461 	return FAILURE;
462 }
463 /* }}} */
464 
465 /* {{{ php_zlib_cleanup_ob_gzhandler_mess() */
php_zlib_cleanup_ob_gzhandler_mess(void)466 static void php_zlib_cleanup_ob_gzhandler_mess(void)
467 {
468 	if (ZLIBG(ob_gzhandler)) {
469 		deflateEnd(&(ZLIBG(ob_gzhandler)->Z));
470 		php_zlib_output_handler_context_dtor(ZLIBG(ob_gzhandler));
471 		ZLIBG(ob_gzhandler) = NULL;
472 	}
473 }
474 /* }}} */
475 
476 /* {{{ proto string ob_gzhandler(string data, int flags)
477    Legacy hack */
PHP_FUNCTION(ob_gzhandler)478 static PHP_FUNCTION(ob_gzhandler)
479 {
480 	char *in_str;
481 	size_t in_len;
482 	zend_long flags = 0;
483 	php_output_context ctx = {0};
484 	int encoding, rv;
485 
486 	/*
487 	 * NOTE that the real ob_gzhandler is an alias to "zlib output compression".
488 	 * This is a really bad hack, because
489 	 * - we have to initialize a php_zlib_context on demand
490 	 * - we have to clean it up in RSHUTDOWN
491 	 * - OG(running) is not set or set to any other output handler
492 	 * - we have to mess around with php_output_context */
493 
494 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &in_str, &in_len, &flags)) {
495 		RETURN_FALSE;
496 	}
497 
498 	if (!(encoding = php_zlib_output_encoding())) {
499 		RETURN_FALSE;
500 	}
501 
502 	if (flags & PHP_OUTPUT_HANDLER_START) {
503 		switch (encoding) {
504 			case PHP_ZLIB_ENCODING_GZIP:
505 				sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1);
506 				break;
507 			case PHP_ZLIB_ENCODING_DEFLATE:
508 				sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1);
509 				break;
510 		}
511 		sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 0);
512 	}
513 
514 	if (!ZLIBG(ob_gzhandler)) {
515 		ZLIBG(ob_gzhandler) = php_zlib_output_handler_context_init();
516 	}
517 
518 	ctx.op = flags;
519 	ctx.in.data = in_str;
520 	ctx.in.used = in_len;
521 
522 	rv = php_zlib_output_handler_ex(ZLIBG(ob_gzhandler), &ctx);
523 
524 	if (SUCCESS != rv) {
525 		if (ctx.out.data && ctx.out.free) {
526 			efree(ctx.out.data);
527 		}
528 		php_zlib_cleanup_ob_gzhandler_mess();
529 		RETURN_FALSE;
530 	}
531 
532 	if (ctx.out.data) {
533 		RETVAL_STRINGL(ctx.out.data, ctx.out.used);
534 		if (ctx.out.free) {
535 			efree(ctx.out.data);
536 		}
537 	} else {
538 		RETVAL_EMPTY_STRING();
539 	}
540 }
541 /* }}} */
542 
543 /* {{{ proto string zlib_get_coding_type(void)
544    Returns the coding type used for output compression */
PHP_FUNCTION(zlib_get_coding_type)545 static PHP_FUNCTION(zlib_get_coding_type)
546 {
547 	if (zend_parse_parameters_none() == FAILURE) {
548 		return;
549 	}
550 	switch (ZLIBG(compression_coding)) {
551 		case PHP_ZLIB_ENCODING_GZIP:
552 			RETURN_STRINGL("gzip", sizeof("gzip") - 1);
553 		case PHP_ZLIB_ENCODING_DEFLATE:
554 			RETURN_STRINGL("deflate", sizeof("deflate") - 1);
555 		default:
556 			RETURN_FALSE;
557 	}
558 }
559 /* }}} */
560 
561 /* {{{ proto array gzfile(string filename [, int use_include_path])
562    Read and uncompress entire .gz-file into an array */
PHP_FUNCTION(gzfile)563 static PHP_FUNCTION(gzfile)
564 {
565 	char *filename;
566 	size_t filename_len;
567 	int flags = REPORT_ERRORS;
568 	char buf[8192] = {0};
569 	register int i = 0;
570 	zend_long use_include_path = 0;
571 	php_stream *stream;
572 
573 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &filename, &filename_len, &use_include_path)) {
574 		return;
575 	}
576 
577 	if (use_include_path) {
578 		flags |= USE_PATH;
579 	}
580 
581 	/* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */
582 	stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
583 
584 	if (!stream) {
585 		/* Error reporting is already done by stream code */
586 		RETURN_FALSE;
587 	}
588 
589 	/* Initialize return array */
590 	array_init(return_value);
591 
592 	/* Now loop through the file and do the magic quotes thing if needed */
593 	memset(buf, 0, sizeof(buf));
594 
595 	while (php_stream_gets(stream, buf, sizeof(buf) - 1) != NULL) {
596 		add_index_string(return_value, i++, buf);
597 	}
598 	php_stream_close(stream);
599 }
600 /* }}} */
601 
602 /* {{{ proto resource gzopen(string filename, string mode [, int use_include_path])
603    Open a .gz-file and return a .gz-file pointer */
PHP_FUNCTION(gzopen)604 static PHP_FUNCTION(gzopen)
605 {
606 	char *filename;
607 	char *mode;
608 	size_t filename_len, mode_len;
609 	int flags = REPORT_ERRORS;
610 	php_stream *stream;
611 	zend_long use_include_path = 0;
612 
613 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps|l", &filename, &filename_len, &mode, &mode_len, &use_include_path) == FAILURE) {
614 		return;
615 	}
616 
617 	if (use_include_path) {
618 		flags |= USE_PATH;
619 	}
620 
621 	stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, NULL STREAMS_CC);
622 
623 	if (!stream) {
624 		RETURN_FALSE;
625 	}
626 	php_stream_to_zval(stream, return_value);
627 }
628 /* }}} */
629 
630 /* {{{ proto int readgzfile(string filename [, int use_include_path])
631    Output a .gz-file */
PHP_FUNCTION(readgzfile)632 static PHP_FUNCTION(readgzfile)
633 {
634 	char *filename;
635 	size_t filename_len;
636 	int flags = REPORT_ERRORS;
637 	php_stream *stream;
638 	size_t size;
639 	zend_long use_include_path = 0;
640 
641 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|l", &filename, &filename_len, &use_include_path) == FAILURE) {
642 		return;
643 	}
644 
645 	if (use_include_path) {
646 		flags |= USE_PATH;
647 	}
648 
649 	stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
650 
651 	if (!stream) {
652 		RETURN_FALSE;
653 	}
654 	size = php_stream_passthru(stream);
655 	php_stream_close(stream);
656 	RETURN_LONG(size);
657 }
658 /* }}} */
659 
660 #define PHP_ZLIB_ENCODE_FUNC(name, default_encoding) \
661 static PHP_FUNCTION(name) \
662 { \
663 	zend_string *in, *out; \
664 	zend_long level = -1; \
665 	zend_long encoding = default_encoding; \
666 	if (default_encoding) { \
667 		if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S|ll", &in, &level, &encoding)) { \
668 			return; \
669 		} \
670 	} else { \
671 		if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sl|l", &in, &encoding, &level)) { \
672 			return; \
673 		} \
674 	} \
675 	if (level < -1 || level > 9) { \
676 		php_error_docref(NULL, E_WARNING, "compression level (" ZEND_LONG_FMT ") must be within -1..9", level); \
677 		RETURN_FALSE; \
678 	} \
679 	switch (encoding) { \
680 		case PHP_ZLIB_ENCODING_RAW: \
681 		case PHP_ZLIB_ENCODING_GZIP: \
682 		case PHP_ZLIB_ENCODING_DEFLATE: \
683 			break; \
684 		default: \
685 			php_error_docref(NULL, E_WARNING, "encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE"); \
686 			RETURN_FALSE; \
687 	} \
688 	if ((out = php_zlib_encode(ZSTR_VAL(in), ZSTR_LEN(in), encoding, level)) == NULL) { \
689 		RETURN_FALSE; \
690 	} \
691 	RETURN_STR(out); \
692 }
693 
694 #define PHP_ZLIB_DECODE_FUNC(name, encoding) \
695 static PHP_FUNCTION(name) \
696 { \
697 	char *in_buf, *out_buf; \
698 	size_t in_len; \
699 	size_t out_len; \
700 	zend_long max_len = 0; \
701 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &in_buf, &in_len, &max_len)) { \
702 		return; \
703 	} \
704 	if (max_len < 0) { \
705 		php_error_docref(NULL, E_WARNING, "length (" ZEND_LONG_FMT ") must be greater or equal zero", max_len); \
706 		RETURN_FALSE; \
707 	} \
708 	if (SUCCESS != php_zlib_decode(in_buf, in_len, &out_buf, &out_len, encoding, max_len)) { \
709 		RETURN_FALSE; \
710 	} \
711 	RETVAL_STRINGL(out_buf, out_len); \
712 	efree(out_buf); \
713 }
714 
715 /* {{{ proto binary zlib_encode(binary data, int encoding[, int level = -1])
716    Compress data with the specified encoding */
717 PHP_ZLIB_ENCODE_FUNC(zlib_encode, 0);
718 /* }}} */
719 
720 /* {{{ proto binary zlib_decode(binary data[, int max_decoded_len])
721    Uncompress any raw/gzip/zlib encoded data */
722 PHP_ZLIB_DECODE_FUNC(zlib_decode, PHP_ZLIB_ENCODING_ANY);
723 /* }}} */
724 
725 /* NOTE: The naming of these userland functions was quite unlucky */
726 /* {{{ proto binary gzdeflate(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_RAW])
727    Encode data with the raw deflate encoding */
728 PHP_ZLIB_ENCODE_FUNC(gzdeflate, PHP_ZLIB_ENCODING_RAW);
729 /* }}} */
730 
731 /* {{{ proto binary gzencode(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_GZIP])
732    Encode data with the gzip encoding */
733 PHP_ZLIB_ENCODE_FUNC(gzencode, PHP_ZLIB_ENCODING_GZIP);
734 /* }}} */
735 
736 /* {{{ proto binary gzcompress(binary data[, int level = -1[, int encoding = ZLIB_ENCODING_DEFLATE])
737    Encode data with the zlib encoding */
738 PHP_ZLIB_ENCODE_FUNC(gzcompress, PHP_ZLIB_ENCODING_DEFLATE);
739 /* }}} */
740 
741 /* {{{ proto binary gzinflate(binary data[, int max_decoded_len])
742    Decode raw deflate encoded data */
743 PHP_ZLIB_DECODE_FUNC(gzinflate, PHP_ZLIB_ENCODING_RAW);
744 /* }}} */
745 
746 /* {{{ proto binary gzdecode(binary data[, int max_decoded_len])
747    Decode gzip encoded data */
748 PHP_ZLIB_DECODE_FUNC(gzdecode, PHP_ZLIB_ENCODING_GZIP);
749 /* }}} */
750 
751 /* {{{ proto binary gzuncompress(binary data[, int max_decoded_len])
752    Decode zlib encoded data */
753 PHP_ZLIB_DECODE_FUNC(gzuncompress, PHP_ZLIB_ENCODING_DEFLATE);
754 /* }}} */
755 
zlib_create_dictionary_string(HashTable * options,char ** dict,size_t * dictlen)756 static zend_bool zlib_create_dictionary_string(HashTable *options, char **dict, size_t *dictlen) {
757 	zval *option_buffer;
758 
759 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("dictionary"))) != NULL) {
760 		ZVAL_DEREF(option_buffer);
761 		switch (Z_TYPE_P(option_buffer)) {
762 			case IS_STRING: {
763 				zend_string *str = Z_STR_P(option_buffer);
764 				*dict = emalloc(ZSTR_LEN(str));
765 				memcpy(*dict, ZSTR_VAL(str), ZSTR_LEN(str));
766 				*dictlen = ZSTR_LEN(str);
767 			} break;
768 
769 			case IS_ARRAY: {
770 				HashTable *dictionary = Z_ARR_P(option_buffer);
771 
772 				if (zend_hash_num_elements(dictionary) > 0) {
773 					char *dictptr;
774 					zval *cur;
775 					zend_string **strings = emalloc(sizeof(zend_string *) * zend_hash_num_elements(dictionary));
776 					zend_string **end, **ptr = strings - 1;
777 
778 					ZEND_HASH_FOREACH_VAL(dictionary, cur) {
779 						size_t i;
780 
781 						*++ptr = zval_get_string(cur);
782 						if (!*ptr || ZSTR_LEN(*ptr) == 0) {
783 							if (*ptr) {
784 								efree(*ptr);
785 							}
786 							while (--ptr >= strings) {
787 								efree(ptr);
788 							}
789 							efree(strings);
790 							php_error_docref(NULL, E_WARNING, "dictionary entries must be non-empty strings");
791 							return 0;
792 						}
793 						for (i = 0; i < ZSTR_LEN(*ptr); i++) {
794 							if (ZSTR_VAL(*ptr)[i] == 0) {
795 								do {
796 									efree(ptr);
797 								} while (--ptr >= strings);
798 								efree(strings);
799 								php_error_docref(NULL, E_WARNING, "dictionary entries must not contain a NULL-byte");
800 								return 0;
801 							}
802 						}
803 
804 						*dictlen += ZSTR_LEN(*ptr) + 1;
805 					} ZEND_HASH_FOREACH_END();
806 
807 					dictptr = *dict = emalloc(*dictlen);
808 					ptr = strings;
809 					end = strings + zend_hash_num_elements(dictionary);
810 					do {
811 						memcpy(dictptr, ZSTR_VAL(*ptr), ZSTR_LEN(*ptr));
812 						dictptr += ZSTR_LEN(*ptr);
813 						*dictptr++ = 0;
814 						zend_string_release(*ptr);
815 					} while (++ptr != end);
816 					efree(strings);
817 				}
818 			} break;
819 
820 			default:
821 				php_error_docref(NULL, E_WARNING, "dictionary must be of type zero-terminated string or array, got %s", zend_get_type_by_const(Z_TYPE_P(option_buffer)));
822 				return 0;
823 		}
824 	}
825 
826 	return 1;
827 }
828 
829 /* {{{ proto resource inflate_init(int encoding)
830    Initialize an incremental inflate context with the specified encoding */
PHP_FUNCTION(inflate_init)831 PHP_FUNCTION(inflate_init)
832 {
833 	z_stream *ctx;
834 	zend_long encoding, window = 15;
835 	char *dict = NULL;
836 	size_t dictlen = 0;
837 	HashTable *options = NULL;
838 	zval *option_buffer;
839 
840 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|H", &encoding, &options)) {
841 		return;
842 	}
843 
844 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("window"))) != NULL) {
845 		window = zval_get_long(option_buffer);
846 	}
847 	if (window < 8 || window > 15) {
848 		php_error_docref(NULL, E_WARNING, "zlib window size (lograithm) (" ZEND_LONG_FMT ") must be within 8..15", window);
849 		RETURN_FALSE;
850 	}
851 
852 	if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
853 		RETURN_FALSE;
854 	}
855 
856 	switch (encoding) {
857 		case PHP_ZLIB_ENCODING_RAW:
858 		case PHP_ZLIB_ENCODING_GZIP:
859 		case PHP_ZLIB_ENCODING_DEFLATE:
860 			break;
861 		default:
862 			php_error_docref(NULL, E_WARNING, "encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE");
863 			RETURN_FALSE;
864 	}
865 
866 	ctx = ecalloc(1, sizeof(php_zlib_context));
867 	ctx->zalloc = php_zlib_alloc;
868 	ctx->zfree = php_zlib_free;
869 	((php_zlib_context *) ctx)->inflateDict = dict;
870 	((php_zlib_context *) ctx)->inflateDictlen = dictlen;
871 	((php_zlib_context *) ctx)->status = Z_OK;
872 
873 	if (encoding < 0) {
874 		encoding += 15 - window;
875 	} else {
876 		encoding -= 15 - window;
877 	}
878 
879 	if (Z_OK == inflateInit2(ctx, encoding)) {
880 		if (encoding == PHP_ZLIB_ENCODING_RAW && dictlen > 0) {
881 			php_zlib_context *php_ctx = (php_zlib_context *) ctx;
882 			switch (inflateSetDictionary(ctx, (Bytef *) php_ctx->inflateDict, php_ctx->inflateDictlen)) {
883 				case Z_OK:
884 					efree(php_ctx->inflateDict);
885 					php_ctx->inflateDict = NULL;
886 					break;
887 				case Z_DATA_ERROR:
888 					php_error_docref(NULL, E_WARNING, "dictionary does not match expected dictionary (incorrect adler32 hash)");
889 					efree(php_ctx->inflateDict);
890 					php_ctx->inflateDict = NULL;
891 					RETURN_FALSE;
892 				EMPTY_SWITCH_DEFAULT_CASE()
893 			}
894 		}
895 		RETURN_RES(zend_register_resource(ctx, le_inflate));
896 	} else {
897 		efree(ctx);
898 		php_error_docref(NULL, E_WARNING, "failed allocating zlib.inflate context");
899 		RETURN_FALSE;
900 	}
901 }
902 /* }}} */
903 
904 /* {{{ proto string inflate_add(resource context, string encoded_data[, int flush_mode = ZLIB_SYNC_FLUSH])
905    Incrementally inflate encoded data in the specified context */
PHP_FUNCTION(inflate_add)906 PHP_FUNCTION(inflate_add)
907 {
908 	zend_string *out;
909 	char *in_buf;
910 	size_t in_len, buffer_used = 0, CHUNK_SIZE = 8192;
911 	zval *res;
912 	z_stream *ctx;
913 	zend_long flush_type = Z_SYNC_FLUSH;
914 	int status;
915 
916 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &res, &in_buf, &in_len, &flush_type)) {
917 		return;
918 	}
919 
920 	if (!(ctx = zend_fetch_resource_ex(res, NULL, le_inflate))) {
921 		php_error_docref(NULL, E_WARNING, "Invalid zlib.inflate resource");
922 		RETURN_FALSE;
923 	}
924 
925 	switch (flush_type) {
926 		case Z_NO_FLUSH:
927 		case Z_PARTIAL_FLUSH:
928 		case Z_SYNC_FLUSH:
929 		case Z_FULL_FLUSH:
930 		case Z_BLOCK:
931 		case Z_FINISH:
932 			break;
933 
934 		default:
935 			php_error_docref(NULL, E_WARNING,
936 				"flush mode must be ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_BLOCK or ZLIB_FINISH");
937 			RETURN_FALSE;
938 	}
939 
940 	/* Lazy-resetting the zlib stream so ctx->total_in remains available until the next inflate_add() call. */
941 	if (((php_zlib_context *) ctx)->status == Z_STREAM_END)
942 	{
943 		((php_zlib_context *) ctx)->status = Z_OK;
944 		inflateReset(ctx);
945 	}
946 
947 	if (in_len <= 0 && flush_type != Z_FINISH) {
948 		RETURN_EMPTY_STRING();
949 	}
950 
951 	out = zend_string_alloc((in_len > CHUNK_SIZE) ? in_len : CHUNK_SIZE, 0);
952 	ctx->next_in = (Bytef *) in_buf;
953 	ctx->next_out = (Bytef *) ZSTR_VAL(out);
954 	ctx->avail_in = in_len;
955 	ctx->avail_out = ZSTR_LEN(out);
956 
957 	do {
958 		status = inflate(ctx, flush_type);
959 		buffer_used = ZSTR_LEN(out) - ctx->avail_out;
960 
961 		((php_zlib_context *) ctx)->status = status; /* Save status for exposing to userspace */
962 
963 		switch (status) {
964 			case Z_OK:
965 				if (ctx->avail_out == 0) {
966 					/* more output buffer space needed; realloc and try again */
967 					out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
968 					ctx->avail_out = CHUNK_SIZE;
969 					ctx->next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
970 					break;
971 				} else {
972 					goto complete;
973 				}
974 			case Z_STREAM_END:
975 				goto complete;
976 			case Z_BUF_ERROR:
977 				if (flush_type == Z_FINISH && ctx->avail_out == 0) {
978 					/* more output buffer space needed; realloc and try again */
979 					out = zend_string_realloc(out, ZSTR_LEN(out) + CHUNK_SIZE, 0);
980 					ctx->avail_out = CHUNK_SIZE;
981 					ctx->next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
982 					break;
983 				} else {
984 					/* No more input data; we're finished */
985 					goto complete;
986 				}
987 			case Z_NEED_DICT:
988 				if (((php_zlib_context *) ctx)->inflateDict) {
989 					php_zlib_context *php_ctx = (php_zlib_context *) ctx;
990 					switch (inflateSetDictionary(ctx, (Bytef *) php_ctx->inflateDict, php_ctx->inflateDictlen)) {
991 						case Z_OK:
992 							efree(php_ctx->inflateDict);
993 							php_ctx->inflateDict = NULL;
994 							break;
995 						case Z_DATA_ERROR:
996 							php_error_docref(NULL, E_WARNING, "dictionary does not match expected dictionary (incorrect adler32 hash)");
997 							efree(php_ctx->inflateDict);
998 							zend_string_release(out);
999 							php_ctx->inflateDict = NULL;
1000 							RETURN_FALSE;
1001 						EMPTY_SWITCH_DEFAULT_CASE()
1002 					}
1003 					break;
1004 				} else {
1005 					php_error_docref(NULL, E_WARNING, "inflating this data requires a preset dictionary, please specify it in the options array of inflate_init()");
1006 					RETURN_FALSE;
1007 				}
1008 			default:
1009 				zend_string_release(out);
1010 				php_error_docref(NULL, E_WARNING, "%s", zError(status));
1011 				RETURN_FALSE;
1012 		}
1013 	} while (1);
1014 
1015 	complete: {
1016 		out = zend_string_realloc(out, buffer_used, 0);
1017 		ZSTR_VAL(out)[buffer_used] = 0;
1018 		RETURN_STR(out);
1019 	}
1020 }
1021 /* }}} */
1022 
1023 /* {{{ proto bool inflate_get_status(resource context)
1024    Get decompression status, usually returns either ZLIB_OK or ZLIB_STREAM_END. */
PHP_FUNCTION(inflate_get_status)1025 PHP_FUNCTION(inflate_get_status)
1026 {
1027 	zval *res;
1028 	z_stream *ctx;
1029 
1030 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &res))
1031 	{
1032 		RETURN_NULL();
1033 	}
1034 
1035 	if (!(ctx = zend_fetch_resource_ex(res, NULL, le_inflate))) {
1036 		php_error_docref(NULL, E_WARNING, "Invalid zlib.inflate resource");
1037 		RETURN_FALSE;
1038 	}
1039 
1040 	RETURN_LONG(((php_zlib_context *) ctx)->status);
1041 }
1042 /* }}} */
1043 
1044 /* {{{ proto bool inflate_get_read_len(resource context)
1045    Get number of bytes read so far. */
PHP_FUNCTION(inflate_get_read_len)1046 PHP_FUNCTION(inflate_get_read_len)
1047 {
1048 	zval *res;
1049 	z_stream *ctx;
1050 
1051 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &res))
1052 	{
1053 		RETURN_NULL();
1054 	}
1055 
1056 	if (!(ctx = zend_fetch_resource_ex(res, NULL, le_inflate))) {
1057 		php_error_docref(NULL, E_WARNING, "Invalid zlib.inflate resource");
1058 		RETURN_FALSE;
1059 	}
1060 
1061 	RETURN_LONG(ctx->total_in);
1062 }
1063 /* }}} */
1064 
1065 /* {{{ proto resource deflate_init(int encoding[, array options])
1066    Initialize an incremental deflate context using the specified encoding */
PHP_FUNCTION(deflate_init)1067 PHP_FUNCTION(deflate_init)
1068 {
1069 	z_stream *ctx;
1070 	zend_long encoding, level = -1, memory = 8, window = 15, strategy = Z_DEFAULT_STRATEGY;
1071 	char *dict = NULL;
1072 	size_t dictlen = 0;
1073 	HashTable *options = NULL;
1074 	zval *option_buffer;
1075 
1076 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l|H", &encoding, &options)) {
1077 		return;
1078 	}
1079 
1080 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("level"))) != NULL) {
1081 		level = zval_get_long(option_buffer);
1082 	}
1083 	if (level < -1 || level > 9) {
1084 		php_error_docref(NULL, E_WARNING, "compression level (" ZEND_LONG_FMT ") must be within -1..9", level);
1085 		RETURN_FALSE;
1086 	}
1087 
1088 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("memory"))) != NULL) {
1089 		memory = zval_get_long(option_buffer);
1090 	}
1091 	if (memory < 1 || memory > 9) {
1092 		php_error_docref(NULL, E_WARNING, "compression memory level (" ZEND_LONG_FMT ") must be within 1..9", memory);
1093 		RETURN_FALSE;
1094 	}
1095 
1096 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("window"))) != NULL) {
1097 		window = zval_get_long(option_buffer);
1098 	}
1099 	if (window < 8 || window > 15) {
1100 		php_error_docref(NULL, E_WARNING, "zlib window size (logarithm) (" ZEND_LONG_FMT ") must be within 8..15", window);
1101 		RETURN_FALSE;
1102 	}
1103 
1104 	if (options && (option_buffer = zend_hash_str_find(options, ZEND_STRL("strategy"))) != NULL) {
1105 		strategy = zval_get_long(option_buffer);
1106 	}
1107 	switch (strategy) {
1108 		case Z_FILTERED:
1109 		case Z_HUFFMAN_ONLY:
1110 		case Z_RLE:
1111 		case Z_FIXED:
1112 		case Z_DEFAULT_STRATEGY:
1113 			break;
1114 		default:
1115 			php_error_docref(NULL, E_WARNING, "strategy must be one of ZLIB_FILTERED, ZLIB_HUFFMAN_ONLY, ZLIB_RLE, ZLIB_FIXED or ZLIB_DEFAULT_STRATEGY");
1116 			RETURN_FALSE;
1117 	}
1118 
1119 	if (!zlib_create_dictionary_string(options, &dict, &dictlen)) {
1120 		RETURN_FALSE;
1121 	}
1122 
1123 	switch (encoding) {
1124 		case PHP_ZLIB_ENCODING_RAW:
1125 		case PHP_ZLIB_ENCODING_GZIP:
1126 		case PHP_ZLIB_ENCODING_DEFLATE:
1127 			break;
1128 		default:
1129 			php_error_docref(NULL, E_WARNING,
1130 				"encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE");
1131 			RETURN_FALSE;
1132 	}
1133 
1134 	ctx = ecalloc(1, sizeof(php_zlib_context));
1135 	ctx->zalloc = php_zlib_alloc;
1136 	ctx->zfree = php_zlib_free;
1137 
1138 	if (encoding < 0) {
1139 		encoding += 15 - window;
1140 	} else {
1141 		encoding -= 15 - window;
1142 	}
1143 
1144 	if (Z_OK == deflateInit2(ctx, level, Z_DEFLATED, encoding, memory, strategy)) {
1145 		if (dict) {
1146 			int success = deflateSetDictionary(ctx, (Bytef *) dict, dictlen);
1147 			ZEND_ASSERT(success == Z_OK);
1148 			efree(dict);
1149 		}
1150 
1151 		RETURN_RES(zend_register_resource(ctx, le_deflate));
1152 	} else {
1153 		efree(ctx);
1154 		php_error_docref(NULL, E_WARNING, "failed allocating zlib.deflate context");
1155 		RETURN_FALSE;
1156 	}
1157 }
1158 /* }}} */
1159 
1160 /* {{{ proto string deflate_add(resource context, string data[, int flush_mode = ZLIB_SYNC_FLUSH])
1161    Incrementally deflate data in the specified context */
PHP_FUNCTION(deflate_add)1162 PHP_FUNCTION(deflate_add)
1163 {
1164 	zend_string *out;
1165 	char *in_buf;
1166 	size_t in_len, out_size, buffer_used;
1167 	zval *res;
1168 	z_stream *ctx;
1169 	zend_long flush_type = Z_SYNC_FLUSH;
1170 	int status;
1171 
1172 	if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &res, &in_buf, &in_len, &flush_type)) {
1173 		return;
1174 	}
1175 
1176 	if (!(ctx = zend_fetch_resource_ex(res, NULL, le_deflate))) {
1177 		php_error_docref(NULL, E_WARNING, "Invalid deflate resource");
1178 		RETURN_FALSE;
1179 	}
1180 
1181 	switch (flush_type) {
1182 		case Z_BLOCK:
1183 #if ZLIB_VERNUM < 0x1240L
1184 			php_error_docref(NULL, E_WARNING,
1185 				"zlib >= 1.2.4 required for BLOCK deflate; current version: %s", ZLIB_VERSION);
1186 			RETURN_FALSE;
1187 #endif
1188 		case Z_NO_FLUSH:
1189 		case Z_PARTIAL_FLUSH:
1190 		case Z_SYNC_FLUSH:
1191 		case Z_FULL_FLUSH:
1192 		case Z_FINISH:
1193 			break;
1194 
1195 		default:
1196 			php_error_docref(NULL, E_WARNING,
1197 				"flush mode must be ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_BLOCK or ZLIB_FINISH");
1198 			RETURN_FALSE;
1199 	}
1200 
1201 	if (in_len <= 0 && flush_type != Z_FINISH) {
1202 		RETURN_EMPTY_STRING();
1203 	}
1204 
1205 	out_size = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len);
1206 	out_size = (out_size < 64) ? 64 : out_size;
1207 	out = zend_string_alloc(out_size, 0);
1208 
1209 	ctx->next_in = (Bytef *) in_buf;
1210 	ctx->next_out = (Bytef *) ZSTR_VAL(out);
1211 	ctx->avail_in = in_len;
1212 	ctx->avail_out = ZSTR_LEN(out);
1213 
1214 	buffer_used = 0;
1215 
1216 	do {
1217 		if (ctx->avail_out == 0) {
1218 			/* more output buffer space needed; realloc and try again */
1219 			/* adding 64 more bytes solved every issue I have seen    */
1220 			out = zend_string_realloc(out, ZSTR_LEN(out) + 64, 0);
1221 			ctx->avail_out = 64;
1222 			ctx->next_out = (Bytef *) ZSTR_VAL(out) + buffer_used;
1223 		}
1224 		status = deflate(ctx, flush_type);
1225 		buffer_used = ZSTR_LEN(out) - ctx->avail_out;
1226 	} while (status == Z_OK && ctx->avail_out == 0);
1227 
1228 	switch (status) {
1229 		case Z_OK:
1230 			ZSTR_LEN(out) = (char *) ctx->next_out - ZSTR_VAL(out);
1231 			ZSTR_VAL(out)[ZSTR_LEN(out)] = 0;
1232 			RETURN_STR(out);
1233 			break;
1234 		case Z_STREAM_END:
1235 			ZSTR_LEN(out) = (char *) ctx->next_out - ZSTR_VAL(out);
1236 			ZSTR_VAL(out)[ZSTR_LEN(out)] = 0;
1237 			deflateReset(ctx);
1238 			RETURN_STR(out);
1239 			break;
1240 		default:
1241 			zend_string_release(out);
1242 			php_error_docref(NULL, E_WARNING, "zlib error (%s)", zError(status));
1243 			RETURN_FALSE;
1244 	}
1245 }
1246 /* }}} */
1247 
1248 #ifdef COMPILE_DL_ZLIB
1249 #ifdef ZTS
1250 ZEND_TSRMLS_CACHE_DEFINE()
1251 #endif
1252 ZEND_GET_MODULE(php_zlib)
1253 #endif
1254 
1255 /* {{{ arginfo */
1256 ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_gzhandler, 0, 0, 2)
1257 	ZEND_ARG_INFO(0, data)
1258 	ZEND_ARG_INFO(0, flags)
1259 ZEND_END_ARG_INFO()
1260 
1261 ZEND_BEGIN_ARG_INFO(arginfo_zlib_get_coding_type, 0)
1262 ZEND_END_ARG_INFO()
1263 
1264 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzfile, 0, 0, 1)
1265 	ZEND_ARG_INFO(0, filename)
1266 	ZEND_ARG_INFO(0, use_include_path)
1267 ZEND_END_ARG_INFO()
1268 
1269 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzopen, 0, 0, 2)
1270 	ZEND_ARG_INFO(0, filename)
1271 	ZEND_ARG_INFO(0, mode)
1272 	ZEND_ARG_INFO(0, use_include_path)
1273 ZEND_END_ARG_INFO()
1274 
1275 ZEND_BEGIN_ARG_INFO_EX(arginfo_readgzfile, 0, 0, 1)
1276 	ZEND_ARG_INFO(0, filename)
1277 	ZEND_ARG_INFO(0, use_include_path)
1278 ZEND_END_ARG_INFO()
1279 
1280 ZEND_BEGIN_ARG_INFO_EX(arginfo_zlib_encode, 0, 0, 2)
1281 	ZEND_ARG_INFO(0, data)
1282 	ZEND_ARG_INFO(0, encoding)
1283 	ZEND_ARG_INFO(0, level)
1284 ZEND_END_ARG_INFO()
1285 
1286 ZEND_BEGIN_ARG_INFO_EX(arginfo_zlib_decode, 0, 0, 1)
1287 	ZEND_ARG_INFO(0, data)
1288 	ZEND_ARG_INFO(0, max_decoded_len)
1289 ZEND_END_ARG_INFO()
1290 
1291 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzdeflate, 0, 0, 1)
1292 	ZEND_ARG_INFO(0, data)
1293 	ZEND_ARG_INFO(0, level)
1294 	ZEND_ARG_INFO(0, encoding)
1295 ZEND_END_ARG_INFO()
1296 
1297 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzencode, 0, 0, 1)
1298 	ZEND_ARG_INFO(0, data)
1299 	ZEND_ARG_INFO(0, level)
1300 	ZEND_ARG_INFO(0, encoding)
1301 ZEND_END_ARG_INFO()
1302 
1303 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzcompress, 0, 0, 1)
1304 	ZEND_ARG_INFO(0, data)
1305 	ZEND_ARG_INFO(0, level)
1306 	ZEND_ARG_INFO(0, encoding)
1307 ZEND_END_ARG_INFO()
1308 
1309 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzinflate, 0, 0, 1)
1310 	ZEND_ARG_INFO(0, data)
1311 	ZEND_ARG_INFO(0, max_decoded_len)
1312 ZEND_END_ARG_INFO()
1313 
1314 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzdecode, 0, 0, 1)
1315 	ZEND_ARG_INFO(0, data)
1316 	ZEND_ARG_INFO(0, max_decoded_len)
1317 ZEND_END_ARG_INFO()
1318 
1319 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzuncompress, 0, 0, 1)
1320 	ZEND_ARG_INFO(0, data)
1321 	ZEND_ARG_INFO(0, max_decoded_len)
1322 ZEND_END_ARG_INFO()
1323 
1324 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzputs, 0, 0, 2)
1325 	ZEND_ARG_INFO(0, fp)
1326 	ZEND_ARG_INFO(0, str)
1327 	ZEND_ARG_INFO(0, length)
1328 ZEND_END_ARG_INFO()
1329 
1330 ZEND_BEGIN_ARG_INFO(arginfo_gzpassthru, 0)
1331 	ZEND_ARG_INFO(0, fp)
1332 ZEND_END_ARG_INFO()
1333 
1334 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzseek, 0, 0, 2)
1335 	ZEND_ARG_INFO(0, fp)
1336 	ZEND_ARG_INFO(0, offset)
1337 	ZEND_ARG_INFO(0, whence)
1338 ZEND_END_ARG_INFO()
1339 
1340 ZEND_BEGIN_ARG_INFO(arginfo_gzread, 0)
1341 	ZEND_ARG_INFO(0, fp)
1342 	ZEND_ARG_INFO(0, length)
1343 ZEND_END_ARG_INFO()
1344 
1345 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzgetss, 0, 0, 1)
1346 	ZEND_ARG_INFO(0, fp)
1347 	ZEND_ARG_INFO(0, length)
1348 	ZEND_ARG_INFO(0, allowable_tags)
1349 ZEND_END_ARG_INFO()
1350 
1351 ZEND_BEGIN_ARG_INFO_EX(arginfo_gzgets, 0, 0, 1)
1352 	ZEND_ARG_INFO(0, fp)
1353 	ZEND_ARG_INFO(0, length)
1354 ZEND_END_ARG_INFO()
1355 
1356 ZEND_BEGIN_ARG_INFO_EX(arginfo_deflate_init, 0, 0, 1)
1357 	ZEND_ARG_INFO(0, encoding)
1358 	ZEND_ARG_INFO(0, level)
1359 ZEND_END_ARG_INFO()
1360 
1361 ZEND_BEGIN_ARG_INFO_EX(arginfo_deflate_add, 0, 0, 2)
1362 	ZEND_ARG_INFO(0, resource)
1363 	ZEND_ARG_INFO(0, add)
1364 	ZEND_ARG_INFO(0, flush_behavior)
1365 ZEND_END_ARG_INFO()
1366 
1367 ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_init, 0, 0, 1)
1368 	ZEND_ARG_INFO(0, encoding)
1369 	ZEND_ARG_INFO(0, options)
1370 ZEND_END_ARG_INFO()
1371 
1372 ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_add, 0, 0, 2)
1373 	ZEND_ARG_INFO(0, context)
1374 	ZEND_ARG_INFO(0, encoded_data)
1375 	ZEND_ARG_INFO(0, flush_mode)
1376 ZEND_END_ARG_INFO()
1377 
1378 ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_get_status, 0, 0, 1)
1379 	ZEND_ARG_INFO(0, resource)
1380 ZEND_END_ARG_INFO()
1381 
1382 ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_get_read_len, 0, 0, 1)
1383 	ZEND_ARG_INFO(0, resource)
1384 ZEND_END_ARG_INFO()
1385 
1386 /* }}} */
1387 
1388 /* {{{ php_zlib_functions[] */
1389 static const zend_function_entry php_zlib_functions[] = {
1390 	PHP_FE(readgzfile,						arginfo_readgzfile)
1391 	PHP_FALIAS(gzrewind,	rewind,			arginfo_gzpassthru)
1392 	PHP_FALIAS(gzclose,		fclose,			arginfo_gzpassthru)
1393 	PHP_FALIAS(gzeof,		feof,			arginfo_gzpassthru)
1394 	PHP_FALIAS(gzgetc,		fgetc,			arginfo_gzpassthru)
1395 	PHP_FALIAS(gzgets,		fgets,			arginfo_gzgets)
1396 	PHP_FALIAS(gzgetss,		fgetss,			arginfo_gzgetss)
1397 	PHP_FALIAS(gzread,		fread,			arginfo_gzread)
1398 	PHP_FE(gzopen,							arginfo_gzopen)
1399 	PHP_FALIAS(gzpassthru,	fpassthru,		arginfo_gzpassthru)
1400 	PHP_FALIAS(gzseek,		fseek,			arginfo_gzseek)
1401 	PHP_FALIAS(gztell,		ftell,			arginfo_gzpassthru)
1402 	PHP_FALIAS(gzwrite,		fwrite,			arginfo_gzputs)
1403 	PHP_FALIAS(gzputs,		fwrite,			arginfo_gzputs)
1404 	PHP_FE(gzfile,							arginfo_gzfile)
1405 	PHP_FE(gzcompress,						arginfo_gzcompress)
1406 	PHP_FE(gzuncompress,					arginfo_gzuncompress)
1407 	PHP_FE(gzdeflate,						arginfo_gzdeflate)
1408 	PHP_FE(gzinflate,						arginfo_gzinflate)
1409 	PHP_FE(gzencode,						arginfo_gzencode)
1410 	PHP_FE(gzdecode,						arginfo_gzdecode)
1411 	PHP_FE(zlib_encode,						arginfo_zlib_encode)
1412 	PHP_FE(zlib_decode,						arginfo_zlib_decode)
1413 	PHP_FE(zlib_get_coding_type,			arginfo_zlib_get_coding_type)
1414 	PHP_FE(deflate_init,					arginfo_deflate_init)
1415 	PHP_FE(deflate_add,						arginfo_deflate_add)
1416 	PHP_FE(inflate_init,					arginfo_inflate_init)
1417 	PHP_FE(inflate_add,						arginfo_inflate_add)
1418 	PHP_FE(inflate_get_status,				arginfo_inflate_get_status)
1419 	PHP_FE(inflate_get_read_len,				arginfo_inflate_get_read_len)
1420 	PHP_FE(ob_gzhandler,					arginfo_ob_gzhandler)
1421 	PHP_FE_END
1422 };
1423 /* }}} */
1424 
1425 /* {{{ OnUpdate_zlib_output_compression */
PHP_INI_MH(OnUpdate_zlib_output_compression)1426 static PHP_INI_MH(OnUpdate_zlib_output_compression)
1427 {
1428 	int int_value;
1429 	char *ini_value;
1430 	zend_long *p;
1431 #ifndef ZTS
1432 	char *base = (char *) mh_arg2;
1433 #else
1434 	char *base;
1435 
1436 	base = (char *) ts_resource(*((int *) mh_arg2));
1437 #endif
1438 
1439 	if (new_value == NULL) {
1440 		return FAILURE;
1441 	}
1442 
1443 	if (!strncasecmp(ZSTR_VAL(new_value), "off", sizeof("off"))) {
1444 		int_value = 0;
1445 	} else if (!strncasecmp(ZSTR_VAL(new_value), "on", sizeof("on"))) {
1446 		int_value = 1;
1447 	} else {
1448 		int_value = zend_atoi(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
1449 	}
1450 	ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0);
1451 
1452 	if (ini_value && *ini_value && int_value) {
1453 		php_error_docref("ref.outcontrol", E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!");
1454 		return FAILURE;
1455 	}
1456 	if (stage == PHP_INI_STAGE_RUNTIME) {
1457 		int status = php_output_get_status();
1458 		if (status & PHP_OUTPUT_SENT) {
1459 			php_error_docref("ref.outcontrol", E_WARNING, "Cannot change zlib.output_compression - headers already sent");
1460 			return FAILURE;
1461 		}
1462 	}
1463 
1464 	p = (zend_long *) (base+(size_t) mh_arg1);
1465 	*p = int_value;
1466 
1467 	ZLIBG(output_compression) = ZLIBG(output_compression_default);
1468 	if (stage == PHP_INI_STAGE_RUNTIME && int_value) {
1469 		if (!php_output_handler_started(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME))) {
1470 			php_zlib_output_compression_start();
1471 		}
1472 	}
1473 
1474 	return SUCCESS;
1475 }
1476 /* }}} */
1477 
1478 /* {{{ OnUpdate_zlib_output_handler */
PHP_INI_MH(OnUpdate_zlib_output_handler)1479 static PHP_INI_MH(OnUpdate_zlib_output_handler)
1480 {
1481 	if (stage == PHP_INI_STAGE_RUNTIME && (php_output_get_status() & PHP_OUTPUT_SENT)) {
1482 		php_error_docref("ref.outcontrol", E_WARNING, "Cannot change zlib.output_handler - headers already sent");
1483 		return FAILURE;
1484 	}
1485 
1486 	return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
1487 }
1488 /* }}} */
1489 
1490 /* {{{ INI */
1491 PHP_INI_BEGIN()
1492 	STD_PHP_INI_BOOLEAN("zlib.output_compression",      "0", PHP_INI_ALL, OnUpdate_zlib_output_compression,       output_compression_default,       zend_zlib_globals, zlib_globals)
1493 	STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdateLong,                           output_compression_level, zend_zlib_globals, zlib_globals)
1494 	STD_PHP_INI_ENTRY("zlib.output_handler",             "", PHP_INI_ALL, OnUpdate_zlib_output_handler,           output_handler,           zend_zlib_globals, zlib_globals)
PHP_INI_END()1495 PHP_INI_END()
1496 
1497 /* }}} */
1498 
1499 /* {{{ PHP_MINIT_FUNCTION */
1500 static PHP_MINIT_FUNCTION(zlib)
1501 {
1502 	php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper);
1503 	php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory);
1504 
1505 	php_output_handler_alias_register(ZEND_STRL("ob_gzhandler"), php_zlib_output_handler_init);
1506 	php_output_handler_conflict_register(ZEND_STRL("ob_gzhandler"), php_zlib_output_conflict_check);
1507 	php_output_handler_conflict_register(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME), php_zlib_output_conflict_check);
1508 
1509 	le_deflate = zend_register_list_destructors_ex(deflate_rsrc_dtor, NULL, "zlib.deflate", module_number);
1510 	le_inflate = zend_register_list_destructors_ex(inflate_rsrc_dtor, NULL, "zlib.inflate", module_number);
1511 
1512 	REGISTER_LONG_CONSTANT("FORCE_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
1513 	REGISTER_LONG_CONSTANT("FORCE_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
1514 
1515 	REGISTER_LONG_CONSTANT("ZLIB_ENCODING_RAW", PHP_ZLIB_ENCODING_RAW, CONST_CS|CONST_PERSISTENT);
1516 	REGISTER_LONG_CONSTANT("ZLIB_ENCODING_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
1517 	REGISTER_LONG_CONSTANT("ZLIB_ENCODING_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
1518 
1519 	REGISTER_LONG_CONSTANT("ZLIB_NO_FLUSH", Z_NO_FLUSH, CONST_CS|CONST_PERSISTENT);
1520 	REGISTER_LONG_CONSTANT("ZLIB_PARTIAL_FLUSH", Z_PARTIAL_FLUSH, CONST_CS|CONST_PERSISTENT);
1521 	REGISTER_LONG_CONSTANT("ZLIB_SYNC_FLUSH", Z_SYNC_FLUSH, CONST_CS|CONST_PERSISTENT);
1522 	REGISTER_LONG_CONSTANT("ZLIB_FULL_FLUSH", Z_FULL_FLUSH, CONST_CS|CONST_PERSISTENT);
1523 	REGISTER_LONG_CONSTANT("ZLIB_BLOCK", Z_BLOCK, CONST_CS|CONST_PERSISTENT);
1524 	REGISTER_LONG_CONSTANT("ZLIB_FINISH", Z_FINISH, CONST_CS|CONST_PERSISTENT);
1525 
1526 	REGISTER_LONG_CONSTANT("ZLIB_FILTERED", Z_FILTERED, CONST_CS|CONST_PERSISTENT);
1527 	REGISTER_LONG_CONSTANT("ZLIB_HUFFMAN_ONLY", Z_HUFFMAN_ONLY, CONST_CS|CONST_PERSISTENT);
1528 	REGISTER_LONG_CONSTANT("ZLIB_RLE", Z_RLE, CONST_CS|CONST_PERSISTENT);
1529 	REGISTER_LONG_CONSTANT("ZLIB_FIXED", Z_FIXED, CONST_CS|CONST_PERSISTENT);
1530 	REGISTER_LONG_CONSTANT("ZLIB_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY, CONST_CS|CONST_PERSISTENT);
1531 
1532 	REGISTER_STRING_CONSTANT("ZLIB_VERSION", ZLIB_VERSION, CONST_CS|CONST_PERSISTENT);
1533 	REGISTER_LONG_CONSTANT("ZLIB_VERNUM", ZLIB_VERNUM, CONST_CS|CONST_PERSISTENT);
1534 
1535 	REGISTER_LONG_CONSTANT("ZLIB_OK", Z_OK, CONST_CS|CONST_PERSISTENT);
1536 	REGISTER_LONG_CONSTANT("ZLIB_STREAM_END", Z_STREAM_END, CONST_CS|CONST_PERSISTENT);
1537 	REGISTER_LONG_CONSTANT("ZLIB_NEED_DICT", Z_NEED_DICT, CONST_CS|CONST_PERSISTENT);
1538 	REGISTER_LONG_CONSTANT("ZLIB_ERRNO", Z_ERRNO, CONST_CS|CONST_PERSISTENT);
1539 	REGISTER_LONG_CONSTANT("ZLIB_STREAM_ERROR", Z_STREAM_ERROR, CONST_CS|CONST_PERSISTENT);
1540 	REGISTER_LONG_CONSTANT("ZLIB_DATA_ERROR", Z_DATA_ERROR, CONST_CS|CONST_PERSISTENT);
1541 	REGISTER_LONG_CONSTANT("ZLIB_MEM_ERROR", Z_MEM_ERROR, CONST_CS|CONST_PERSISTENT);
1542 	REGISTER_LONG_CONSTANT("ZLIB_BUF_ERROR", Z_BUF_ERROR, CONST_CS|CONST_PERSISTENT);
1543 	REGISTER_LONG_CONSTANT("ZLIB_VERSION_ERROR", Z_VERSION_ERROR, CONST_CS|CONST_PERSISTENT);
1544 
1545 	REGISTER_INI_ENTRIES();
1546 	return SUCCESS;
1547 }
1548 /* }}} */
1549 
1550 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(zlib)1551 static PHP_MSHUTDOWN_FUNCTION(zlib)
1552 {
1553 	php_unregister_url_stream_wrapper("zlib");
1554 	php_stream_filter_unregister_factory("zlib.*");
1555 
1556 	UNREGISTER_INI_ENTRIES();
1557 
1558 	return SUCCESS;
1559 }
1560 /* }}} */
1561 
1562 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(zlib)1563 static PHP_RINIT_FUNCTION(zlib)
1564 {
1565 	ZLIBG(compression_coding) = 0;
1566     if (!ZLIBG(handler_registered)) {
1567         ZLIBG(output_compression) = ZLIBG(output_compression_default);
1568         php_zlib_output_compression_start();
1569     }
1570 
1571 	return SUCCESS;
1572 }
1573 /* }}} */
1574 
1575 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(zlib)1576 static PHP_RSHUTDOWN_FUNCTION(zlib)
1577 {
1578 	php_zlib_cleanup_ob_gzhandler_mess();
1579     ZLIBG(handler_registered) = 0;
1580 
1581     return SUCCESS;
1582 }
1583 /* }}} */
1584 
1585 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(zlib)1586 static PHP_MINFO_FUNCTION(zlib)
1587 {
1588 	php_info_print_table_start();
1589 	php_info_print_table_header(2, "ZLib Support", "enabled");
1590 	php_info_print_table_row(2, "Stream Wrapper", "compress.zlib://");
1591 	php_info_print_table_row(2, "Stream Filter", "zlib.inflate, zlib.deflate");
1592 	php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION);
1593 	php_info_print_table_row(2, "Linked Version", (char *) zlibVersion());
1594 	php_info_print_table_end();
1595 
1596 	DISPLAY_INI_ENTRIES();
1597 }
1598 /* }}} */
1599 
1600 /* {{{ ZEND_MODULE_GLOBALS_CTOR */
PHP_GINIT_FUNCTION(zlib)1601 static PHP_GINIT_FUNCTION(zlib)
1602 {
1603 #if defined(COMPILE_DL_ZLIB) && defined(ZTS)
1604 	ZEND_TSRMLS_CACHE_UPDATE();
1605 #endif
1606 	zlib_globals->ob_gzhandler = NULL;
1607     zlib_globals->handler_registered = 0;
1608 }
1609 /* }}} */
1610 
1611 /* {{{ php_zlib_module_entry */
1612 zend_module_entry php_zlib_module_entry = {
1613 	STANDARD_MODULE_HEADER,
1614 	"zlib",
1615 	php_zlib_functions,
1616 	PHP_MINIT(zlib),
1617 	PHP_MSHUTDOWN(zlib),
1618 	PHP_RINIT(zlib),
1619 	PHP_RSHUTDOWN(zlib),
1620 	PHP_MINFO(zlib),
1621 	PHP_ZLIB_VERSION,
1622 	PHP_MODULE_GLOBALS(zlib),
1623 	PHP_GINIT(zlib),
1624 	NULL,
1625 	NULL,
1626 	STANDARD_MODULE_PROPERTIES_EX
1627 };
1628 /* }}} */
1629 
1630 /*
1631  * Local variables:
1632  * tab-width: 4
1633  * c-basic-offset: 4
1634  * End:
1635  * vim600: sw=4 ts=4 fdm=marker
1636  * vim<600: sw=4 ts=4
1637  */
1638