]> granicus.if.org Git - php/commitdiff
- implement new output API
authorMichael Wallner <mike@php.net>
Wed, 30 Aug 2006 07:42:02 +0000 (07:42 +0000)
committerMichael Wallner <mike@php.net>
Wed, 30 Aug 2006 07:42:02 +0000 (07:42 +0000)
- ported deflate/inflate code
- require zlib >= 1.2.0.4
- add gzdecode()

# if we want the filter code to benefit from zlib 1.2 the
# "streamable" API would have to be ported, too

ext/zlib/config0.m4
ext/zlib/php_zlib.h
ext/zlib/tests/006.phpt
ext/zlib/tests/007.phpt
ext/zlib/zlib.c

index 62e05fbd02da8cce7fa1e8fb9541afb83886e296..25c7f4f4202731e84f3d61f2f8587203fd17783b 100644 (file)
@@ -41,10 +41,17 @@ if test "$PHP_ZLIB" != "no" || test "$PHP_ZLIB_DIR" != "no"; then
   *) ac_extra=-L$ZLIB_DIR/$PHP_LIBDIR ;;
   esac
 
+  AC_MSG_CHECKING([for zlib version >= 1.2.0.4])
+  ZLIB_VERSION=`$EGREP "define ZLIB_VERSION" $ZLIB_DIR/include/zlib.h | $SED -e 's/[[^0-9\.]]//g'`
+  AC_MSG_RESULT([$ZLIB_VERSION])
+  if test `echo $ZLIB_VERSION | $SED -e 's/[[^0-9]]/ /g' | $AWK '{print $1*1000000 + $2*10000 + $3*100 + $4}'` -lt 1020004; then
+    AC_MSG_ERROR([libz version greater or equal to 1.2.0.4 required])
+  fi
+
   PHP_CHECK_LIBRARY(z, gzgets, [
     AC_DEFINE(HAVE_ZLIB,1,[ ]) 
   ],[
-    AC_MSG_ERROR(ZLIB extension requires zlib >= 1.0.9)
+    AC_MSG_ERROR(ZLIB extension requires gzgets in zlib)
   ],[
     $ac_extra
   ])
index 45e6734f1ed2aec5020d8319f2f75efbbfe07b4e..d647fa9350bf5c76ceaa667f8e9c72a547b16901 100644 (file)
 
 #include <zlib.h>
 
+#define PHP_ZLIB_ENCODING_RAW          -0xf
+#define PHP_ZLIB_ENCODING_GZIP         0x1f
+#define PHP_ZLIB_ENCODING_DEFLATE      0x0f
+
+#define PHP_ZLIB_ENCODING_ANY          0x2f
+
+#define PHP_ZLIB_OUTPUT_HANDLER_NAME "zlib output compression"
+#define PHP_ZLIB_BUFFER_SIZE_GUESS(in_len) (((size_t) ((double) in_len * (double) 1.015)) + 10 + 8 + 4 + 1)
+
 ZEND_BEGIN_MODULE_GLOBALS(zlib)
        /* variables for transparent gzip encoding */
        int compression_coding;
-       z_stream stream;
-       uLong crc;
-       int ob_gzhandler_status;
        long output_compression;
        long output_compression_level;
        char *output_handler;
-ZEND_END_MODULE_GLOBALS(zlib)
+ZEND_END_MODULE_GLOBALS(zlib);
+
+typedef struct _php_zlib_buffer {
+       char *data;
+       char *aptr;
+       size_t used;
+       size_t free;
+       size_t size;
+} php_zlib_buffer;
 
+typedef struct _php_zlib_context {
+       z_stream Z;
+       php_zlib_buffer buffer;
+} php_zlib_context;
+
+int php_zlib_encode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, int level TSRMLS_DC);
+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 TSRMLS_DC);
+php_output_handler *php_zlib_output_handler_init(zval *handler_name TSRMLS_DC);
+int php_zlib_output_handler(void **handler_context, php_output_context *output_context);
+void php_zlib_output_handler_dtor(void *opaq TSRMLS_DC);
+int php_zlib_output_conflict_check(zval *handler_name TSRMLS_DC);
+
+php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
+extern php_stream_ops php_stream_gzio_ops;
+extern php_stream_wrapper php_stream_gzip_wrapper;
 extern php_stream_filter_factory php_zlib_filter_factory;
 extern zend_module_entry php_zlib_module_entry;
 #define zlib_module_ptr &php_zlib_module_entry
+#define phpext_zlib_ptr zlib_module_ptr
 
 PHP_MINIT_FUNCTION(zlib);
 PHP_MSHUTDOWN_FUNCTION(zlib);
 PHP_RINIT_FUNCTION(zlib);
 PHP_MINFO_FUNCTION(zlib);
+
+PHP_FUNCTION(gzfile);
 PHP_FUNCTION(gzopen);
 PHP_FUNCTION(readgzfile);
-PHP_FUNCTION(gzfile);
+
+PHP_FUNCTION(ob_gzhandler);
+
 PHP_FUNCTION(gzcompress);
 PHP_FUNCTION(gzuncompress);
 PHP_FUNCTION(gzdeflate);
 PHP_FUNCTION(gzinflate);
 PHP_FUNCTION(gzencode);
-PHP_FUNCTION(ob_gzhandler);
+PHP_FUNCTION(gzdecode);
+
+PHP_FUNCTION(zlib_encode);
+PHP_FUNCTION(zlib_decode);
+
 PHP_FUNCTION(zlib_get_coding_type);
 
-php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
-extern php_stream_ops php_stream_gzio_ops;
-extern php_stream_wrapper php_stream_gzip_wrapper;
 
 #ifdef ZTS
-#define ZLIBG(v) TSRMG(zlib_globals_id, zend_zlib_globals *, v)
+#      define ZLIBG(v) TSRMG(zlib_globals_id, zend_zlib_globals *, v)
 #else
-#define ZLIBG(v) (zlib_globals.v)
+#      define ZLIBG(v) (zlib_globals.v)
 #endif
 
-#define phpext_zlib_ptr zlib_module_ptr
-
-#define CODING_GZIP            1
-#define CODING_DEFLATE 2
-
 #endif /* PHP_ZLIB_H */
 
 /*
index 62d466030bcc6510e90e99b7c8b4c3331219fd1f..c736bb3f719e6423a437d2c5389afb1aad731b0c 100644 (file)
@@ -22,8 +22,8 @@ var_dump($data2 = gzdeflate($string, 9));
 var_dump(gzinflate());
 var_dump(gzinflate(b""));
 var_dump(gzinflate(b"asfwe", 1000));
-var_dump(gzinflate(b"asdf", -1));
 
+var_dump(gzinflate(b"asdf", -1));
 var_dump(gzinflate(b"asdf"));
 var_dump(gzinflate(b"asdf", 9));
 
@@ -48,19 +48,17 @@ string(%d) "%s"
 
 Warning: gzinflate() expects at least 1 parameter, 0 given in %s on line %d
 NULL
-bool(false)
 
 Warning: gzinflate(): data error in %s on line %d
 bool(false)
 
-Warning: gzinflate(): length (-1) must be greater or equal zero in %s on line %d
-bool(false)
-
 Warning: gzinflate(): data error in %s on line %d
 bool(false)
 
-Warning: gzinflate(): data error in %s on line %d
+Warning: gzinflate(): length (-1) must be greater or equal zero in %s on line %d
 bool(false)
+string(0) ""
+string(0) ""
 string(94) "Answer me, it can't be so hard
 Cry to relieve what's in your heart
 Desolation, grief and agony"
index 4b0f892d2a667c6ae507149b996ebb879aee7c6d..c411944e0918892613d225597c1b85038b8df3da 100644 (file)
@@ -11,8 +11,8 @@ var_dump(gzencode(b"", -10));
 var_dump(gzencode(b"", 100));
 var_dump(gzencode(b"", 1, 100));
 
-var_dump(gzencode(b"", -1, 1));
-var_dump(gzencode(b"", 9, 2));
+var_dump(gzencode(b"", -1, ZLIB_ENCODING_GZIP));
+var_dump(gzencode(b"", 9, ZLIB_ENCODING_DEFLATE));
 
 $string = b"Light of my sun
 Light in this temple
@@ -21,8 +21,8 @@ Lies in the darkness";
 
 var_dump(gzencode($string, 9, 3));
 
-var_dump(gzencode($string, -1, 1));
-var_dump(gzencode($string, 9, 2));
+var_dump(gzencode($string, -1, ZLIB_ENCODING_GZIP));
+var_dump(gzencode($string, 9, ZLIB_ENCODING_DEFLATE));
 
 echo "Done\n";
 ?>
@@ -33,18 +33,18 @@ NULL
 Warning: gzencode() expects at most 3 parameters, 4 given in %s on line %d
 NULL
 
-Warning: gzencode(): compression level(-10) must be within -1..9 in %s on line %d
+Warning: gzencode(): compression level (-10) must be within -1..9 in %s on line %d
 bool(false)
 
-Warning: gzencode(): compression level(100) must be within -1..9 in %s on line %d
+Warning: gzencode(): compression level (100) must be within -1..9 in %s on line %d
 bool(false)
 
-Warning: gzencode(): encoding mode must be FORCE_GZIP or FORCE_DEFLATE in %s on line %d
+Warning: gzencode(): encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE in %s on line %d
 bool(false)
 string(%d) "%s"
 string(%d) "%s"
 
-Warning: gzencode(): encoding mode must be FORCE_GZIP or FORCE_DEFLATE in %s on line %d
+Warning: gzencode(): encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE in %s on line %d
 bool(false)
 string(%d) "%s"
 string(%d) "%s"
index 0337834f555b055270d0d5da9bd94e907ca3c1aa..2bf592c0631d74fb6e17ff2af2f69d70a2992748 100644 (file)
 /* $Id$ */
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#      include "config.h"
 #endif
 
 #include "php.h"
 #include "SAPI.h"
 #include "php_ini.h"
-
-#include <stdlib.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#ifdef PHP_WIN32
-#define O_RDONLY _O_RDONLY
-#include "win32/param.h"
-#else
-#include <sys/param.h>
-/* #include <sys/uio.h> */
-#endif
-#include "ext/standard/head.h"
-#include "ext/standard/php_standard.h"
 #include "ext/standard/info.h"
-#include "php_zlib.h"
-#include "fopen_wrappers.h"
-#if HAVE_PWD_H
-#ifdef PHP_WIN32
-#include "win32/pwd.h"
-#else
-#include <pwd.h>
-#endif
-#endif
-#if defined(HAVE_UNISTD_H) && defined(PHP_WIN32)
-#undef HAVE_UNISTD_H
-#endif
-
-#ifdef COMPILE_DL_ZLIB
-#ifndef PUTS
-#define PUTS(a) php_printf("%s",a)
-#endif
-#ifndef PUTC
-#define PUTC(a) PUTS(a)
-#endif
-#ifndef PHPWRITE
-#define PHPWRITE(a,n) php_write((a),(n) TSRMLS_CC)
-#endif
-#endif
-
-/* Win32 needs some more memory */
-#ifdef PHP_WIN32
-#define PHP_ZLIB_MODIFIER 100
-#else
-#define PHP_ZLIB_MODIFIER 1000
-#endif
-
-#define OS_CODE                        0x03 /* FIXME */
-#define GZIP_HEADER_LENGTH             10
-#define GZIP_FOOTER_LENGTH             8
-
-/* True globals, no need for thread safety */
-static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
+#include "ext/standard/file.h"
 
-static int php_enable_output_compression(int buffer_size TSRMLS_DC);
-static int php_ob_gzhandler_check(zval *handler_name TSRMLS_DC);
-
-/* {{{ php_zlib_functions[]
- */
-zend_function_entry php_zlib_functions[] = {
-       PHP_FE(readgzfile,                                              NULL)
-       PHP_FALIAS(gzrewind,    rewind,                 NULL)
-       PHP_FALIAS(gzclose,             fclose,                 NULL)
-       PHP_FALIAS(gzeof,               feof,                   NULL)
-       PHP_FALIAS(gzgetc,              fgetc,                  NULL)
-       PHP_FALIAS(gzgets,              fgets,                  NULL)
-       PHP_FALIAS(gzgetss,             fgetss,                 NULL)
-       PHP_FALIAS(gzread,              fread,                  NULL)
-       PHP_FE(gzopen,                                                  NULL)
-       PHP_FALIAS(gzpassthru,  fpassthru,              NULL)
-       PHP_FALIAS(gzseek,              fseek,                  NULL)
-       PHP_FALIAS(gztell,              ftell,                  NULL)
-       PHP_FALIAS(gzwrite,             fwrite,                 NULL)
-       PHP_FALIAS(gzputs,              fwrite,                 NULL)
-       PHP_FE(gzfile,                                                  NULL)
-       PHP_FE(gzcompress,                              NULL)
-       PHP_FE(gzuncompress,                            NULL)
-       PHP_FE(gzdeflate,                               NULL)
-       PHP_FE(gzinflate,                               NULL)
-       PHP_FE(gzencode,                                                NULL)
-       PHP_FE(ob_gzhandler,                                    NULL)
-       PHP_FE(zlib_get_coding_type,                    NULL)
-       {NULL, NULL, NULL}
-};
-/* }}} */
+#include "php_zlib.h"
 
-ZEND_DECLARE_MODULE_GLOBALS(zlib)
+ZEND_DECLARE_MODULE_GLOBALS(zlib);
 
-/* {{{ php_zlib_module_entry
- */
-zend_module_entry php_zlib_module_entry = {
-       STANDARD_MODULE_HEADER,
-       "zlib",
-       php_zlib_functions,
-       PHP_MINIT(zlib),
-       PHP_MSHUTDOWN(zlib),
-       PHP_RINIT(zlib),
-       NULL,
-       PHP_MINFO(zlib),
-       "1.1",
-       PHP_MODULE_GLOBALS(zlib),
-       NULL,
-       NULL,
-       NULL,
-       STANDARD_MODULE_PROPERTIES_EX
-};
+/* {{{ php_zlib_output_conflict_check() */
+int php_zlib_output_conflict_check(zval *handler_name TSRMLS_DC)
+{
+       if (php_output_get_level(TSRMLS_C) > 0) {
+               PHP_OUTPUT_CONFLICT(PHP_ZLIB_OUTPUT_HANDLER_NAME, return FAILURE);
+               PHP_OUTPUT_CONFLICT("ob_gzhandler", return FAILURE);
+               PHP_OUTPUT_CONFLICT("mb_output_handler", return FAILURE);
+               PHP_OUTPUT_CONFLICT("URL-Rewriter", return FAILURE);
+       }
+       return SUCCESS;
+}
 /* }}} */
 
-#ifdef COMPILE_DL_ZLIB
-ZEND_GET_MODULE(php_zlib)
-#endif
-
-/* {{{ OnUpdate_zlib_output_compression */
-static PHP_INI_MH(OnUpdate_zlib_output_compression)
+/* {{{ php_zlib_output_encoding() */
+int php_zlib_output_encoding(TSRMLS_D)
 {
-       char *ini_value;
-
-       if (new_value == NULL) {
-               return FAILURE;
-       }
+       zval **enc;
        
-       if (!strncasecmp(new_value, "off", sizeof("off"))) {
-               new_value = "0";
-               new_value_length = sizeof("0");
-       } else if (!strncasecmp(new_value, "on", sizeof("on"))) {
-               new_value = "1";
-               new_value_length = sizeof("1");
-       }
-
-       ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0); 
-       if (ini_value != NULL && strlen(ini_value) != 0 && zend_atoi(new_value, new_value_length) != 0) {
-               php_error_docref("ref.outcontrol" TSRMLS_CC, E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!");
-               return FAILURE;
-       }
-
-       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
-               php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_compression - headers already sent");
-               return FAILURE;
+       if (!ZLIBG(compression_coding)) {
+               zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC);
+               
+               if (PG(http_globals)[TRACK_VARS_SERVER] && SUCCESS == zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void *) &enc)) {
+                       convert_to_string(*enc);
+                       if (strstr(Z_STRVAL_PP(enc), "gzip")) {
+                               ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_GZIP;
+                       } else if (strstr(Z_STRVAL_PP(enc), "deflate")) {
+                               ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_DEFLATE;
+                       }
+               }
        }
-
-       OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
-
-       return SUCCESS;
+       
+       return ZLIBG(compression_coding);
 }
 /* }}} */
 
-/* {{{ OnUpdate_zlib_output_compression_level */
-static PHP_INI_MH(OnUpdate_zlib_output_compression_level)
+/* {{{ php_zlib_output_handler_init() */
+php_output_handler *php_zlib_output_handler_init(zval *handler_name TSRMLS_DC)
 {
-       OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
-
-       return SUCCESS;
+       php_output_handler *h = NULL;
+       
+       if (php_zlib_output_encoding(TSRMLS_C)) {
+               if ((h = php_output_handler_create_internal(handler_name, php_zlib_output_handler, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC))) {
+                       php_output_handler_set_context(h, ecalloc(1, sizeof(php_zlib_context)), php_zlib_output_handler_dtor TSRMLS_CC);
+               }
+       }
+       
+       return h;
 }
 /* }}} */
 
-/* {{{ OnUpdate_zlib_output_handler */
-static PHP_INI_MH(OnUpdate_zlib_output_handler)
+/* {{{ php_zlib_output_handler() */
+int php_zlib_output_handler(void **handler_context, php_output_context *output_context)
 {
-       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
-               php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_handler - headers already sent");
+       php_zlib_context *ctx = *(php_zlib_context **) handler_context;
+       int flags = Z_NO_FLUSH, level;
+       PHP_OUTPUT_TSRMLS(output_context);
+       
+       if (!php_zlib_output_encoding(TSRMLS_C)) {
                return FAILURE;
        }
-
-       OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
-
+       
+       if (output_context->op & PHP_OUTPUT_HANDLER_START) {
+               /* start up */
+               if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
+                       return FAILURE;
+               }
+       }
+       
+       if (output_context->op & PHP_OUTPUT_HANDLER_CLEAN) {
+               /* free buffers */
+               deflateEnd(&ctx->Z);
+               
+               if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
+                       /* discard */
+                       return SUCCESS;
+               } else {
+                       /* restart */
+                       if (Z_OK != deflateInit2(&ctx->Z, ZLIBG(output_compression_level), Z_DEFLATED, ZLIBG(compression_coding), MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) {
+                               return FAILURE;
+                       }
+                       ctx->buffer.used = 0;
+               }
+       } else {
+               
+               if (output_context->in.used) {
+                       /* append input */
+                       if (ctx->buffer.free < output_context->in.used) {
+                               if (!(ctx->buffer.aptr = erealloc_recoverable(ctx->buffer.data, ctx->buffer.used + ctx->buffer.free + output_context->in.used))) {
+                                       deflateEnd(&ctx->Z);
+                                       return FAILURE;
+                               }
+                               ctx->buffer.data = ctx->buffer.aptr;
+                               ctx->buffer.free += output_context->in.used;
+                       }
+                       memcpy(ctx->buffer.data + ctx->buffer.used, output_context->in.data, output_context->in.used);
+                       ctx->buffer.free -= output_context->in.used;
+                       ctx->buffer.used += output_context->in.used;
+               }
+               
+               output_context->out.size = PHP_ZLIB_BUFFER_SIZE_GUESS(output_context->in.used);
+               output_context->out.data = emalloc(output_context->out.size);
+               output_context->out.free = 1;
+               output_context->out.used = 0;
+               
+               ctx->Z.avail_in = ctx->buffer.used;
+               ctx->Z.next_in = (Bytef *) ctx->buffer.data;
+               ctx->Z.avail_out = output_context->out.size;
+               ctx->Z.next_out = (Bytef *) output_context->out.data;
+               
+               if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
+                       flags = Z_FINISH;
+               } else if (output_context->op & PHP_OUTPUT_HANDLER_FLUSH) {
+                       flags = Z_SYNC_FLUSH;
+               }
+               
+               switch (deflate(&ctx->Z, flags)) {
+                       case Z_OK:
+                               if (flags == Z_FINISH) {
+                                       deflateEnd(&ctx->Z);
+                                       return FAILURE;
+                               }
+                       case Z_STREAM_END:
+                               if (ctx->Z.avail_in) {
+                                       memmove(ctx->buffer.data, ctx->buffer.data + ctx->buffer.used - ctx->Z.avail_in, ctx->Z.avail_in);
+                               }
+                               ctx->buffer.free += ctx->buffer.used - ctx->Z.avail_in;
+                               ctx->buffer.used = ctx->Z.avail_in;
+                               output_context->out.used = output_context->out.size - ctx->Z.avail_out;
+                               break;
+                       default:
+                               deflateEnd(&ctx->Z);
+                               return FAILURE;
+               }
+               
+               php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags TSRMLS_CC);
+               
+               if (!(flags & PHP_OUTPUT_HANDLER_STARTED)) {
+                       if (SG(headers_sent)) {
+                               deflateEnd(&ctx->Z);
+                               return FAILURE;
+                       }
+                       switch (ZLIBG(compression_coding)) {
+                               case PHP_ZLIB_ENCODING_GZIP:
+                                       sapi_add_header_ex(ZEND_STRL("Content-Encoding: gzip"), 1, 1 TSRMLS_CC);
+                                       break;
+                               case PHP_ZLIB_ENCODING_DEFLATE:
+                                       sapi_add_header_ex(ZEND_STRL("Content-Encoding: deflate"), 1, 1 TSRMLS_CC);
+                                       break;
+                               default:
+                                       deflateEnd(&ctx->Z);
+                                       return FAILURE;
+                       }
+                       sapi_add_header_ex(ZEND_STRL("Vary: Accept-Encoding"), 1, 1 TSRMLS_CC);
+                       php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC);
+               }
+               
+               if (output_context->op & PHP_OUTPUT_HANDLER_FINAL) {
+                       deflateEnd(&ctx->Z);
+               }
+       }
        return SUCCESS;
 }
 /* }}} */
 
-
-PHP_INI_BEGIN()
-       STD_PHP_INI_BOOLEAN("zlib.output_compression",      "0", PHP_INI_ALL, OnUpdate_zlib_output_compression,       output_compression,       zend_zlib_globals, zlib_globals)
-       STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdate_zlib_output_compression_level, output_compression_level, zend_zlib_globals, zlib_globals)
-       STD_PHP_INI_ENTRY("zlib.output_handler",             "", PHP_INI_ALL, OnUpdate_zlib_output_handler,           output_handler,           zend_zlib_globals, zlib_globals)
-PHP_INI_END()
-
-/* {{{ PHP_MINIT_FUNCTION
- */
-PHP_MINIT_FUNCTION(zlib)
+/* {{{ php_zlib_output_handler_dtor() */
+void php_zlib_output_handler_dtor(void *opaq TSRMLS_DC)
 {
-       zval tmp;
-
-       php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper TSRMLS_CC);
-       php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory TSRMLS_CC);
-       INIT_PZVAL(&tmp);
-       ZVAL_ASCII_STRINGL(&tmp, "ob_gzhandler", sizeof("ob_gzhandler")-1, 0);
-       php_output_handler_conflict_register(&tmp, php_ob_gzhandler_check TSRMLS_CC);
-
-       REGISTER_LONG_CONSTANT("FORCE_GZIP", CODING_GZIP, CONST_CS | CONST_PERSISTENT);
-       REGISTER_LONG_CONSTANT("FORCE_DEFLATE", CODING_DEFLATE, CONST_CS | CONST_PERSISTENT);
-
-       REGISTER_INI_ENTRIES();
-
-       return SUCCESS;
+       php_zlib_context *ctx = (php_zlib_context *) opaq;
+       
+       if (ctx) {
+               if (ctx->buffer.data) {
+                       efree(ctx->buffer.data);
+               }
+               efree(ctx);
+       }
 }
 /* }}} */
 
-/* {{{ PHP_RINIT_FUNCTION
- */
-PHP_RINIT_FUNCTION(zlib)
+/* {{{ php_zlib_encode() */
+int php_zlib_encode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, int level TSRMLS_DC)
 {
-       uint chunk_size = ZLIBG(output_compression);
-
-       ZLIBG(ob_gzhandler_status) = 0;
-       ZLIBG(compression_coding) = 0;
-       if (chunk_size) {
-               if (chunk_size == 1) {
-                       chunk_size = 4096; /* use the default size */
-                       ZLIBG(output_compression) = chunk_size;
+       int status;
+       z_stream Z;
+       
+       memset(&Z, 0, sizeof(z_stream));
+       
+       if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
+               *out_len = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len);
+               *out_buf = emalloc(*out_len);
+               
+               Z.next_in = (Bytef *) in_buf;
+               Z.next_out = (Bytef *) *out_buf;
+               Z.avail_in = in_len;
+               Z.avail_out = *out_len;
+               
+               status = deflate(&Z, Z_FINISH);
+               deflateEnd(&Z);
+               
+               if (Z_STREAM_END == status) {
+                       /* size buffer down to actual length */
+                       *out_buf = erealloc(*out_buf, Z.total_out + 1);
+                       (*out_buf)[*out_len = Z.total_out] = '\0';
+                       return SUCCESS;
+               } else {
+                       efree(*out_buf);
                }
-               php_enable_output_compression(chunk_size TSRMLS_CC);
        }
-       return SUCCESS;
+       
+       *out_buf = NULL;
+       *out_len = 0;
+       
+       php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
+       return FAILURE;
 }
 /* }}} */
 
-/* {{{ PHP_MSHUTDOWN_FUNCTION
- */
-PHP_MSHUTDOWN_FUNCTION(zlib)
+/* {{{ inflate_rounds() */
+static inline int inflate_rounds(z_stream *Z, size_t max, char **buf, size_t *len)
 {
-       php_unregister_url_stream_wrapper("zlib" TSRMLS_CC);
-       php_stream_filter_unregister_factory("zlib.*" TSRMLS_CC);
+       int status, round = 0;
+       php_zlib_buffer buffer = {NULL, NULL, 0, 0, 0};
        
-       UNREGISTER_INI_ENTRIES();
-
-       return SUCCESS;
+       *buf = NULL;
+       *len = 0;
+       
+       buffer.size = Z->avail_in;
+       
+       do {
+               if ((max && (max < buffer.used)) || !(buffer.aptr = erealloc_recoverable(buffer.data, buffer.size))) {
+                       status = Z_MEM_ERROR;
+               } else {
+                       buffer.data = buffer.aptr;
+                       Z->avail_out = buffer.free = buffer.size - buffer.used;
+                       Z->next_out = (Bytef *) buffer.data + buffer.used;
+#if 0
+                       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);
+#endif
+                       status = inflate(Z, Z_NO_FLUSH);
+                       
+                       buffer.used += buffer.free - Z->avail_out;
+                       buffer.free = Z->avail_out;
+#if 0
+                       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);
+#endif
+                       buffer.size += (buffer.size >> 3) + 1;
+               }
+       } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < 100);
+       
+       if (status == Z_OK || status == Z_STREAM_END) {
+               buffer.data = erealloc(buffer.data, buffer.used + 1);
+               buffer.data[buffer.used] = '\0';
+               *buf = buffer.data;
+               *len = buffer.used;
+       } else if (buffer.data) {
+               efree(buffer.data);
+       }
+       
+       return status;
 }
 /* }}} */
 
-/* {{{ PHP_MINFO_FUNCTION
- */
-PHP_MINFO_FUNCTION(zlib)
+/* {{{ php_zlib_decode() */
+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 TSRMLS_DC)
 {
-       php_info_print_table_start();
-       php_info_print_table_row(2, "ZLib Support", "enabled");
-       php_info_print_table_row(2, "Stream Wrapper support", "compress.zlib://");
-       php_info_print_table_row(2, "Stream Filter support", "zlib.inflate, zlib.deflate");
-       php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION);
-       php_info_print_table_row(2, "Linked Version", (char *) zlibVersion());
-       php_info_print_table_end();
+       int status = Z_DATA_ERROR;
+       z_stream Z;
+       
+       memset(&Z, 0, sizeof(z_stream));
+       if (in_len) {
+retry_raw_inflate:
+               status = inflateInit2(&Z, encoding);
+               if (Z_OK == status) {
+                       Z.next_in = (Bytef *) in_buf;
+                       Z.avail_in = in_len;
+                       
+                       switch (status = inflate_rounds(&Z, max_len, out_buf, out_len)) {
+                               case Z_OK:
+                               case Z_STREAM_END:
+                                       inflateEnd(&Z);
+                                       return SUCCESS;
+                               
+                               case Z_DATA_ERROR:
+                                       /* raw deflated data? */
+                                       if (PHP_ZLIB_ENCODING_ANY == encoding) {
+                                               inflateEnd(&Z);
+                                               encoding = PHP_ZLIB_ENCODING_RAW;
+                                               goto retry_raw_inflate;
+                                       }
+                       }
+                       inflateEnd(&Z);
+               }
+       }
+       
+       *out_buf = NULL;
+       *out_len = 0;
+       
+       php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
+       return FAILURE;
+}
+/* }}} */
 
-       DISPLAY_INI_ENTRIES();
+/* {{{ proto string zlib_get_coding_type(void) U
+   Returns the coding type used for output compression */
+PHP_FUNCTION(zlib_get_coding_type)
+{
+       if (ZEND_NUM_ARGS()) {
+               WRONG_PARAM_COUNT;
+       }
+       switch (ZLIBG(compression_coding)) {
+               case PHP_ZLIB_ENCODING_GZIP:
+                       RETURN_ASCII_STRINGL("gzip", sizeof("gzip") - 1, 1);
+               case PHP_ZLIB_ENCODING_DEFLATE:
+                       RETURN_ASCII_STRINGL("deflate", sizeof("deflate") - 1, 1);
+               default:
+                       RETURN_FALSE;
+       }
 }
 /* }}} */
 
@@ -281,42 +361,35 @@ PHP_FUNCTION(gzfile)
        zstr filename;
        int filename_len;
        zend_uchar filename_type;
-       long flags = 0;
-       char buf[8192];
-       register int i = 0;
-       int use_include_path = 0;
+       int flags = REPORT_ERRORS;
+       char buf[8192] = {0};
+       long use_include_path = 0;
        php_stream *stream;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &filename, &filename_len, &filename_type, &flags) == FAILURE) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &filename, &filename_len, &filename_type, &use_include_path)) {
                return;
        }
 
-       use_include_path = flags ? USE_PATH : 0;
-
-       if (filename_type == IS_UNICODE) {
-               if (php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL) == FAILURE) {
-                       RETURN_FALSE;
-               }
+       if (filename_type == IS_UNICODE && SUCCESS != php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL)) {
+               RETURN_FALSE;
        }
 
+       if (use_include_path) {
+               flags |= USE_PATH;
+       }
        /* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */
-       stream = php_stream_gzopen(NULL, filename.s, "rb", use_include_path | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC);
+       stream = php_stream_gzopen(NULL, filename.s, "rb", flags, NULL, NULL STREAMS_CC TSRMLS_CC);
        if (filename_type == IS_UNICODE) {
                efree(filename.s);
        }
-       if (stream == NULL) {
+       if (!stream) {
                /* Error reporting is already done by stream code */
                RETURN_FALSE;
        }
 
-       /* Initialize return array */
        array_init(return_value);
-
-       /* Now loop through the file and do the magic quotes thing if needed */
-       memset(buf,0,sizeof(buf));
-
-       while (php_stream_gets(stream, ZSTR(buf), sizeof(buf) - 1) != NULL) {
-               add_index_string(return_value, i++, buf, 1);
+       while (php_stream_gets(stream, ZSTR(buf), sizeof(buf) - 1)) {
+               add_next_index_string(return_value, buf, 1);
        }
        php_stream_close(stream);
 }
@@ -330,23 +403,22 @@ PHP_FUNCTION(gzopen)
        char *mode;
        int filename_len, mode_len;
        zend_uchar filename_type;
-       long flags = 0;
+       int flags = REPORT_ERRORS;
        php_stream *stream;
-       int use_include_path = 0;
+       long use_include_path = 0;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ts|l", &filename, &filename_len, &filename_type, &mode, &mode_len, &flags) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ts|l", &filename, &filename_len, &filename_type, &mode, &mode_len, &use_include_path) == FAILURE) {
                return;
        }
 
-       use_include_path = flags ? USE_PATH : 0;
-
-       if (filename_type == IS_UNICODE) {
-               if (php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL) == FAILURE) {
-                       RETURN_FALSE;
-               }
+       if (filename_type == IS_UNICODE && SUCCESS != php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL)) {
+               RETURN_FALSE;
        }
 
-       stream = php_stream_gzopen(NULL, filename.s, mode, use_include_path | REPORT_ERRORS, NULL, NULL STREAMS_CC TSRMLS_CC);
+       if (use_include_path) {
+               flags |= USE_PATH;
+       }
+       stream = php_stream_gzopen(NULL, filename.s, mode, flags, NULL, NULL STREAMS_CC TSRMLS_CC);
 
        if (filename_type == IS_UNICODE) {
                efree(filename.s);
@@ -358,9 +430,6 @@ PHP_FUNCTION(gzopen)
 }      
 /* }}} */
 
-/*
- * Read a file and write the ouput to stdout
- */
 /* {{{ proto int readgzfile(string filename [, int use_include_path]) U
    Output a .gz-file */
 PHP_FUNCTION(readgzfile)
@@ -368,24 +437,23 @@ PHP_FUNCTION(readgzfile)
        zstr filename;
        int filename_len;
        zend_uchar filename_type;
-       long flags = 0;
+       int flags = REPORT_ERRORS;
        php_stream *stream;
        int size;
-       int use_include_path = 0;
+       long use_include_path = 0;
        
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &filename, &filename_len, &filename_type, &flags) == FAILURE) {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|l", &filename, &filename_len, &filename_type, &use_include_path) == FAILURE) {
                return;
        }
 
-       use_include_path = flags ? USE_PATH : 0;
-
-       if (filename_type == IS_UNICODE) {
-               if (php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL) == FAILURE) {
-                       RETURN_FALSE;
-               }
+       if (filename_type == IS_UNICODE && SUCCESS != php_stream_path_encode(NULL, &filename.s, &filename_len, filename.u, filename_len, REPORT_ERRORS, NULL)) {
+               RETURN_FALSE;
        }
 
-       stream = php_stream_gzopen(NULL, filename.s, "rb", use_include_path, NULL, NULL STREAMS_CC TSRMLS_CC);
+       if (use_include_path) {
+               flags |= USE_PATH;
+       }
+       stream = php_stream_gzopen(NULL, filename.s, "rb", flags, NULL, NULL STREAMS_CC TSRMLS_CC);
        if (filename_type == IS_UNICODE) {
                efree(filename.s);
        }
@@ -393,663 +461,292 @@ PHP_FUNCTION(readgzfile)
                RETURN_FALSE;
        }
        size = php_stream_passthru(stream);
-       php_stream_close(stream);
+       php_stream_close(stream);
        RETURN_LONG(size);
 }
 /* }}} */
 
-/* {{{ proto string gzcompress(string data [, int level]) U
-   Gzip-compress a string */
-PHP_FUNCTION(gzcompress)
-{
-       int data_len, status;
-       long level = Z_DEFAULT_COMPRESSION;
-       unsigned long l2;
-       char *data, *s2;
+#define PHP_ZLIB_ENCODE_FUNC(name, default_encoding) \
+PHP_FUNCTION(name) \
+{ \
+       char *in_buf, *out_buf; \
+       int in_len; \
+       size_t out_len; \
+       long level = -1; \
+       long encoding = default_encoding; \
+       if (default_encoding) { \
+               if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|ll", &in_buf, &in_len, &level, &encoding)) { \
+                       return; \
+               } \
+       } else { \
+               if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sl|l", &in_buf, &in_len, &encoding, &level)) { \
+                       return; \
+               } \
+       } \
+       if (level < -1 || level > 9) { \
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level); \
+               RETURN_FALSE; \
+       } \
+       switch (encoding) { \
+               case PHP_ZLIB_ENCODING_RAW: \
+               case PHP_ZLIB_ENCODING_GZIP: \
+               case PHP_ZLIB_ENCODING_DEFLATE: \
+                       break; \
+               default: \
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE"); \
+                       RETURN_FALSE; \
+       } \
+       if (SUCCESS != php_zlib_encode(in_buf, in_len, &out_buf, &out_len, encoding, level TSRMLS_CC)) { \
+               RETURN_FALSE; \
+       } \
+       RETURN_STRINGL(out_buf, out_len, 0); \
+}
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &data, &data_len, &level) == FAILURE) {
-               return;
-       }
+#define PHP_ZLIB_DECODE_FUNC(name, encoding) \
+PHP_FUNCTION(name) \
+{ \
+       char *in_buf, *out_buf; \
+       int in_len; \
+       size_t out_len; \
+       long max_len = 0; \
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &in_buf, &in_len, &max_len)) { \
+               return; \
+       } \
+       if (max_len < 0) { \
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", max_len); \
+               RETURN_FALSE; \
+       } \
+       if (SUCCESS != php_zlib_decode(in_buf, in_len, &out_buf, &out_len, encoding, max_len TSRMLS_CC)) { \
+               RETURN_FALSE; \
+       } \
+       RETURN_STRINGL(out_buf, out_len, 0); \
+}
 
-       if ((level < -1) || (level > 9)) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level);
-               RETURN_FALSE;
-       }
+/* {{{ proto string zlib_encode(string data, int encoding[, int level = -1]) U
+       Compress data with the specified encoding */
+PHP_ZLIB_ENCODE_FUNC(zlib_encode, 0);
+/* }}} */
 
-       l2 = data_len + (data_len / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
-       s2 = (char *) emalloc(l2);
-       if (!s2) {
-               RETURN_FALSE;
-       }
-               
-       if (level >= 0) {
-               status = compress2(s2, &l2, data, data_len, level);
-       } else {
-               status = compress(s2, &l2, data, data_len);
-       }
-       
-       if (status == Z_OK) {
-               s2 = erealloc(s2, l2 + 1);
-               s2[l2] = '\0';
-               RETURN_STRINGL(s2, l2, 0);
-       } else {
-               efree(s2);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-               RETURN_FALSE;
-       }
-}
+/* {{{ proto string zlib_decode(string data[, int max_decoded_len]) U
+       Uncompress any raw/gzip/zlib encoded data */
+PHP_ZLIB_DECODE_FUNC(zlib_decode, PHP_ZLIB_ENCODING_ANY);
 /* }}} */
 
-/* {{{ proto string gzuncompress(string data [, int length]) U
-   Unzip a gzip-compressed string */
-PHP_FUNCTION(gzuncompress)
-{
-       int data_len, status;
-       unsigned int factor=1, maxfactor=16;
-       long limit = 0;
-       unsigned long plength=0, length;
-       char *data, *s1=NULL, *s2=NULL;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &data, &data_len, &limit) == FAILURE) {
-               return;
-       }
-
-       if (limit < 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit);
-               RETURN_FALSE;
-       }
-       plength = limit;
-
-       /*
-        zlib::uncompress() wants to know the output data length
-        if none was given as a parameter
-        we try from input length * 2 up to input length * 2^15
-        doubling it whenever it wasn't big enough
-        that should be eneugh for all real life cases  
-       */
-       do {
-               length = plength ? plength : (unsigned long)data_len * (1 << factor++);
-               s2 = (char *) erealloc(s1, length);
-               status = uncompress(s2, &length, data, data_len);
-               s1 = s2;
-       } while ((status == Z_BUF_ERROR) && (!plength) && (factor < maxfactor));
+/* NOTE: The naming of these userland functions was quite unlucky */
 
-       if (status == Z_OK) {
-               s2 = erealloc(s2, length + 1); /* space for \0 */
-               s2[ length ] = '\0';
-               RETURN_STRINGL(s2, length, 0);
-       } else {
-               efree(s2);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-               RETURN_FALSE;
-       }
-}
+/* {{{ proto string gzdeflate(string data[, int level = -1[, int encoding = ZLIB_ENCODING_RAW]) U
+       Encode data with the raw deflate encoding */
+PHP_ZLIB_ENCODE_FUNC(gzdeflate, PHP_ZLIB_ENCODING_RAW);
 /* }}} */
-
-/* {{{ proto string gzdeflate(string data [, int level]) U
-   Gzip-compress a string */
-PHP_FUNCTION(gzdeflate)
-{
-       int data_len,status;
-       long level = Z_DEFAULT_COMPRESSION;
-       z_stream stream;
-       char *data, *s2;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &data, &data_len, &level) == FAILURE) {
-               return;
-       }
-
-       if ((level < -1) || (level > 9)) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level (%ld) must be within -1..9", level);
-               RETURN_FALSE;
-       }
-
-       stream.data_type = Z_ASCII;
-       stream.zalloc = (alloc_func) Z_NULL;
-       stream.zfree  = (free_func) Z_NULL;
-       stream.opaque = (voidpf) Z_NULL;
-
-       stream.next_in = (Bytef *) data;
-       stream.avail_in = data_len;
-
-       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
-
-       s2 = (char *) emalloc(stream.avail_out);
-       if (!s2) {
-               RETURN_FALSE;
-       }
-       
-       stream.next_out = s2;
-
-       /* init with -MAX_WBITS disables the zlib internal headers */
-       status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, 0);
-       if (status == Z_OK) {
-               status = deflate(&stream, Z_FINISH);
-               if (status != Z_STREAM_END) {
-                       deflateEnd(&stream);
-                       if (status == Z_OK) {
-                               status = Z_BUF_ERROR;
-                       }
-               } else {
-                       status = deflateEnd(&stream);
-               }
-       }
-
-       if (status == Z_OK) {
-               s2 = erealloc(s2,stream.total_out + 1); /* resize to buffer to the "right" size */
-               s2[ stream.total_out ] = '\0';
-               RETURN_STRINGL(s2, stream.total_out, 0);
-       } else {
-               efree(s2);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-               RETURN_FALSE;
-       }
-}
+/* {{{ proto string gzencode(string data[, int level = -1[, int encoding = ZLIB_ENCODING_GZIP]) U
+       Encode data with the gzip encoding */
+PHP_ZLIB_ENCODE_FUNC(gzencode, PHP_ZLIB_ENCODING_GZIP);
+/* }}} */
+/* {{{ proto string gzcompress(string data[, int level = -1[, int encoding = ZLIB_ENCODING_DEFLATE]) U
+       Encode data with the zlib encoding */
+PHP_ZLIB_ENCODE_FUNC(gzcompress, PHP_ZLIB_ENCODING_DEFLATE);
+/* }}} */
+/* {{{ proto string gzinflate(string data[, int max_decoded_len]) U
+       Decode raw deflate encoded data */
+PHP_ZLIB_DECODE_FUNC(gzinflate, PHP_ZLIB_ENCODING_RAW);
+/* }}} */
+/* {{{ proto string gzdecode(string data[, int max_decoded_len]) U
+       Decode gzip encoded data */
+PHP_ZLIB_DECODE_FUNC(gzdecode, PHP_ZLIB_ENCODING_GZIP);
+/* }}} */
+/* {{{ proto string gzuncompress(string data[, int max_decoded_len]) U
+       Decode zlib encoded data */
+PHP_ZLIB_DECODE_FUNC(gzuncompress, PHP_ZLIB_ENCODING_DEFLATE);
 /* }}} */
 
-/* {{{ proto string gzinflate(string data [, int length]) U
-   Unzip a gzip-compressed string */
-PHP_FUNCTION(gzinflate)
+PHP_FUNCTION(ob_gzhandler)
 {
-       int data_len, status;
-       unsigned int factor=1, maxfactor=16;
-       long limit = 0;
-       unsigned long plength=0, length;
-       char *data, *s1=NULL, *s2=NULL;
-       z_stream stream;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|l", &data, &data_len, &limit) == FAILURE) {
-               return;
-       }
-
-       if (!data_len) {
-               RETURN_FALSE;
-       }
-
-       if (limit < 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "length (%ld) must be greater or equal zero", limit);
-               RETURN_FALSE;
-       }
-       plength = limit;
-
-       /*
-         stream.avail_out wants to know the output data length
-         if none was given as a parameter
-         we try from input length * 2 up to input length * 2^15
-         doubling it whenever it wasn't big enough
-         that should be enaugh for all real life cases 
-       */
-
-       stream.zalloc = (alloc_func) Z_NULL;
-       stream.zfree = (free_func) Z_NULL;
-
-       do {
-               length = plength ? plength : (unsigned long)data_len * (1 << factor++);
-               s2 = (char *) erealloc(s1, length);
-
-               if (!s2 && s1) {
-                       efree(s1);
-                       RETURN_FALSE;
-               }
-
-               stream.next_in = (Bytef *) data;
-               stream.avail_in = (uInt) data_len + 1; /* there is room for \0 */
-
-               stream.next_out = s2;
-               stream.avail_out = (uInt) length;
-
-               /* init with -MAX_WBITS disables the zlib internal headers */
-               status = inflateInit2(&stream, -MAX_WBITS);
-               if (status == Z_OK) {
-                       status = inflate(&stream, Z_FINISH);
-                       if (status != Z_STREAM_END) {
-                               inflateEnd(&stream);
-                               if (status == Z_OK) {
-                                       status = Z_BUF_ERROR;
-                               }
-                       } else {
-                               status = inflateEnd(&stream);
-                       }
-               }
-               s1 = s2;
-               
-       } while ((status == Z_BUF_ERROR) && (!plength) && (factor < maxfactor));
-
-       if (status == Z_OK) {
-               s2 = erealloc(s2, stream.total_out + 1); /* room for \0 */
-               s2[ stream.total_out ] = '\0';
-               RETURN_STRINGL(s2, stream.total_out, 0);
-       } else {
-               efree(s2);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-               RETURN_FALSE;
-       }
+       php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_gzhandler is intended to be used with ob_start()");
 }
-/* }}} */
 
-/* {{{ proto string zlib_get_coding_type(void) U
-   Returns the coding type used for output compression */
-PHP_FUNCTION(zlib_get_coding_type)
-{
-       switch (ZLIBG(compression_coding)) {
-               case CODING_GZIP:
-                       RETURN_ASCII_STRINGL("gzip", sizeof("gzip") - 1, 1);
+#ifdef COMPILE_DL_ZLIB
+ZEND_GET_MODULE(php_zlib)
+#endif
 
-               case CODING_DEFLATE:
-                       RETURN_ASCII_STRINGL("deflate", sizeof("deflate") - 1, 1);
-       }
+/* {{{ php_zlib_functions[] */
+zend_function_entry php_zlib_functions[] = {
+       PHP_FE(readgzfile,                                              NULL)
+       PHP_FALIAS(gzrewind,    rewind,                 NULL)
+       PHP_FALIAS(gzclose,             fclose,                 NULL)
+       PHP_FALIAS(gzeof,               feof,                   NULL)
+       PHP_FALIAS(gzgetc,              fgetc,                  NULL)
+       PHP_FALIAS(gzgets,              fgets,                  NULL)
+       PHP_FALIAS(gzgetss,             fgetss,                 NULL)
+       PHP_FALIAS(gzread,              fread,                  NULL)
+       PHP_FE(gzopen,                                                  NULL)
+       PHP_FALIAS(gzpassthru,  fpassthru,              NULL)
+       PHP_FALIAS(gzseek,              fseek,                  NULL)
+       PHP_FALIAS(gztell,              ftell,                  NULL)
+       PHP_FALIAS(gzwrite,             fwrite,                 NULL)
+       PHP_FALIAS(gzputs,              fwrite,                 NULL)
+       PHP_FE(gzfile,                                                  NULL)
+       PHP_FE(gzcompress,                                              NULL)
+       PHP_FE(gzuncompress,                                    NULL)
+       PHP_FE(gzdeflate,                                               NULL)
+       PHP_FE(gzinflate,                                               NULL)
+       PHP_FE(gzencode,                                                NULL)
+       PHP_FE(gzdecode,                                                NULL)
+       PHP_FE(ob_gzhandler,                                    NULL)
+       PHP_FE(zlib_encode,                                             NULL)
+       PHP_FE(zlib_decode,                                             NULL)
+       PHP_FE(zlib_get_coding_type,                    NULL)
+       {NULL, NULL, NULL}
+};
+/* }}} */
 
-       RETURN_FALSE;
-}
 
-/* {{{ php_do_deflate
- */
-static int php_do_deflate(uint str_length, Bytef **p_buffer, uint *p_buffer_len, zend_bool do_start, zend_bool do_end TSRMLS_DC)
+/* {{{ OnUpdate_zlib_output_compression */
+static PHP_INI_MH(OnUpdate_zlib_output_compression)
 {
-       Bytef *buffer;
-       uInt prev_outlen, outlen;
-       int err;
-       int start_offset = ((do_start && ZLIBG(compression_coding) == CODING_GZIP) ? 10 : 0);
-       int end_offset = (do_end ? 8 : 0);
+       char *ini_value;
 
-       outlen = (uint) (str_length + (str_length / PHP_ZLIB_MODIFIER) + 12 + 1); /* leave some room for a trailing \0 */
-       if ((outlen + start_offset + end_offset) > *p_buffer_len) {
-               buffer = (Bytef *) emalloc(outlen + start_offset + end_offset);
-       } else {
-               buffer = *p_buffer;
+       if (new_value == NULL) {
+               return FAILURE;
        }
        
-       ZLIBG(stream).next_out = buffer + start_offset;
-       ZLIBG(stream).avail_out = outlen;
-
-       err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH);
-       while (err == Z_OK && !ZLIBG(stream).avail_out) {
-               prev_outlen = outlen;
-               outlen *= 3;
-               if ((outlen + start_offset + end_offset) > *p_buffer_len) {
-                       buffer = erealloc(buffer, outlen + start_offset + end_offset);
-               }
-               
-               ZLIBG(stream).next_out = buffer + start_offset + prev_outlen;
-               ZLIBG(stream).avail_out = prev_outlen * 2;
-
-               err = deflate(&ZLIBG(stream), Z_SYNC_FLUSH);
+       if (!strncasecmp(new_value, "off", sizeof("off"))) {
+               new_value = "0";
+               new_value_length = sizeof("0");
+       } else if (!strncasecmp(new_value, "on", sizeof("on"))) {
+               new_value = "1";
+               new_value_length = sizeof("1");
        }
 
-       if (do_end) {
-               err = deflate(&ZLIBG(stream), Z_FINISH);
-               buffer[outlen + start_offset - ZLIBG(stream).avail_out] = '\0';
+       ini_value = zend_ini_string("output_handler", sizeof("output_handler"), 0);
+       if (ini_value != NULL && strlen(ini_value) != 0 && zend_atoi(new_value, new_value_length) != 0) {
+               php_error_docref("ref.outcontrol" TSRMLS_CC, E_CORE_ERROR, "Cannot use both zlib.output_compression and output_handler together!!");
+               return FAILURE;
        }
 
-       *p_buffer = buffer;
-       *p_buffer_len = outlen - ZLIBG(stream).avail_out;
+       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
+               php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_compression - headers already sent");
+               return FAILURE;
+       }
 
-       return err;
+       return OnUpdateLong(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
 }
 /* }}} */
 
-/* {{{ php_deflate_string
- */
-static int php_deflate_string(const char *str, uint str_length, char **newstr, uint *new_length, zend_bool do_start, zend_bool do_end TSRMLS_DC)
+/* {{{ OnUpdate_zlib_output_handler */
+static PHP_INI_MH(OnUpdate_zlib_output_handler)
 {
-       int err;
-
-       if (do_start) {
-               ZLIBG(stream).zalloc = Z_NULL;
-               ZLIBG(stream).zfree = Z_NULL;
-               ZLIBG(stream).opaque = Z_NULL;
-
-               switch (ZLIBG(compression_coding)) {
-                       case CODING_GZIP:
-                               /* windowBits is passed < 0 to suppress zlib header & trailer */
-                               if (deflateInit2(&ZLIBG(stream), ZLIBG(output_compression_level), Z_DEFLATED,   -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
-                                       /* TODO: print out error */
-                                       return FAILURE;
-                               }
-               
-                               ZLIBG(crc) = crc32(0L, Z_NULL, 0);
-                               break;
-
-                       case CODING_DEFLATE:
-                               if (deflateInit(&ZLIBG(stream), ZLIBG(output_compression_level)) != Z_OK) {
-                                       /* TODO: print out error */
-                                       return FAILURE;
-                               }
-                               break;          
-               }
-       }
-
-       ZLIBG(stream).next_in = (Bytef *) str;
-       ZLIBG(stream).avail_in = (uInt) str_length;
-
-       if (ZLIBG(compression_coding) == CODING_GZIP) {
-               ZLIBG(crc) = crc32(ZLIBG(crc), (const Bytef *) str, str_length);
-       }
-
-       err = php_do_deflate(str_length, (Bytef **) newstr, new_length, do_start, do_end TSRMLS_CC);
-       /* TODO: error handling (err may be Z_STREAM_ERROR, Z_BUF_ERROR, ?) */
-
-       if (do_start && ZLIBG(compression_coding) == CODING_GZIP) {
-               /* Write a very simple .gz header: */
-               (*newstr)[0] = gz_magic[0];
-               (*newstr)[1] = gz_magic[1];
-               (*newstr)[2] = Z_DEFLATED;
-               (*newstr)[3] = (*newstr)[4] = (*newstr)[5] = (*newstr)[6] = (*newstr)[7] = (*newstr)[8] = 0;
-               (*newstr)[9] = OS_CODE;
-               *new_length += 10;
-       }
-       if (do_end) {
-               if (ZLIBG(compression_coding) == CODING_GZIP) {
-                       char *trailer = (*newstr) + (*new_length);
-
-                       /* write crc & stream.total_in in LSB order */
-                       trailer[0] = (char) ZLIBG(crc) & 0xFF;
-                       trailer[1] = (char) (ZLIBG(crc) >> 8) & 0xFF;
-                       trailer[2] = (char) (ZLIBG(crc) >> 16) & 0xFF;
-                       trailer[3] = (char) (ZLIBG(crc) >> 24) & 0xFF;
-                       trailer[4] = (char) ZLIBG(stream).total_in & 0xFF;
-                       trailer[5] = (char) (ZLIBG(stream).total_in >> 8) & 0xFF;
-                       trailer[6] = (char) (ZLIBG(stream).total_in >> 16) & 0xFF;
-                       trailer[7] = (char) (ZLIBG(stream).total_in >> 24) & 0xFF;
-                       trailer[8] = '\0';
-                       *new_length += 8;
-               }
-               deflateEnd(&ZLIBG(stream));
+       if (stage == PHP_INI_STAGE_RUNTIME && SG(headers_sent) && !SG(request_info).no_headers) {
+               php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "Cannot change zlib.output_handler - headers already sent");
+               return FAILURE;
        }
 
-       return SUCCESS;
+       return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
 }
 /* }}} */
 
-/* {{{ proto string gzencode(string data [, int level [, int encoding_mode]]) U
-   GZ encode a string */
-PHP_FUNCTION(gzencode)
-{
-       char *data, *s2;
-       int data_len;
-       long level = Z_DEFAULT_COMPRESSION, coding = CODING_GZIP;
-       int status;
-       z_stream stream;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|ll", &data, &data_len, &level, &coding) == FAILURE) {
-               return;
-       }
-
-       if ((level < -1) || (level > 9)) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "compression level(%ld) must be within -1..9", level);
-               RETURN_FALSE;
-       }
-
-       if ((coding != CODING_GZIP) && (coding != CODING_DEFLATE)) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be FORCE_GZIP or FORCE_DEFLATE");
-               RETURN_FALSE;
-       }
-
-       stream.zalloc = Z_NULL;
-       stream.zfree = Z_NULL;
-       stream.opaque = Z_NULL;
-
-       stream.next_in = (Bytef *) data;
-       stream.avail_in = data_len;
-
-       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
-       s2 = (char *) emalloc(stream.avail_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0));
-
-       /* add gzip file header */
-       s2[0] = gz_magic[0];
-       s2[1] = gz_magic[1];
-       s2[2] = Z_DEFLATED;
-       s2[3] = s2[4] = s2[5] = s2[6] = s2[7] = s2[8] = 0; /* time set to 0 */
-       s2[9] = OS_CODE;
-
-       stream.next_out = &(s2[GZIP_HEADER_LENGTH]);
-
-       switch (coding) {
-               case CODING_GZIP:
-                       /* windowBits is passed < 0 to suppress zlib header & trailer */
-                       if ((status = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY)) != Z_OK) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-                               RETURN_FALSE;
-                       }
-               
-                       break;
-               case CODING_DEFLATE:
-                       if ((status = deflateInit(&stream, level)) != Z_OK) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-                               RETURN_FALSE;
-                       }
-                       break;          
-       }
-
-       status = deflate(&stream, Z_FINISH);
-       if (status != Z_STREAM_END) {
-               deflateEnd(&stream);
-               if (status == Z_OK) {
-                       status = Z_BUF_ERROR;
-               }
-       } else {
-               status = deflateEnd(&stream);
-       }
-
-       if (status == Z_OK) {
-               /* resize to buffer to the "right" size */
-               s2 = erealloc(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0) + 1);
-
-               if (coding == CODING_GZIP) {
-                       char *trailer = s2 + (stream.total_out + GZIP_HEADER_LENGTH);
-                       uLong crc = crc32(0L, Z_NULL, 0);
-
-                       crc = crc32(crc, (const Bytef *) data, data_len);
+/* {{{ INI */
+PHP_INI_BEGIN()
+       STD_PHP_INI_BOOLEAN("zlib.output_compression",      "0", PHP_INI_ALL, OnUpdate_zlib_output_compression,       output_compression,       zend_zlib_globals, zlib_globals)
+       STD_PHP_INI_ENTRY("zlib.output_compression_level", "-1", PHP_INI_ALL, OnUpdateLong,                           output_compression_level, zend_zlib_globals, zlib_globals)
+       STD_PHP_INI_ENTRY("zlib.output_handler",             "", PHP_INI_ALL, OnUpdate_zlib_output_handler,           output_handler,           zend_zlib_globals, zlib_globals)
+PHP_INI_END()
+/* }}} */
 
-                       /* write crc & stream.total_in in LSB order */
-                       trailer[0] = (char) crc & 0xFF;
-                       trailer[1] = (char) (crc >> 8) & 0xFF;
-                       trailer[2] = (char) (crc >> 16) & 0xFF;
-                       trailer[3] = (char) (crc >> 24) & 0xFF;
-                       trailer[4] = (char) stream.total_in & 0xFF;
-                       trailer[5] = (char) (stream.total_in >> 8) & 0xFF;
-                       trailer[6] = (char) (stream.total_in >> 16) & 0xFF;
-                       trailer[7] = (char) (stream.total_in >> 24) & 0xFF;
-                       trailer[8] = '\0';
-               } else {
-                       s2[stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0)] = '\0';
-               }
-               RETURN_STRINGL(s2, stream.total_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0), 0);
-       } else {
-               efree(s2);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status));
-               RETURN_FALSE;
-       }
+/* {{{ PHP_MINIT_FUNCTION */
+PHP_MINIT_FUNCTION(zlib)
+{
+       php_register_url_stream_wrapper("compress.zlib", &php_stream_gzip_wrapper TSRMLS_CC);
+       php_stream_filter_register_factory("zlib.*", &php_zlib_filter_factory TSRMLS_CC);
+       
+       PHP_OUTPUT_ALIAS_REGISTER("ob_gzhandler", php_zlib_output_handler_init);
+       PHP_OUTPUT_CONFLICT_REGISTER("ob_gzhandler", php_zlib_output_conflict_check);
+       PHP_OUTPUT_CONFLICT_REGISTER(PHP_ZLIB_OUTPUT_HANDLER_NAME, php_zlib_output_conflict_check);
+       
+       REGISTER_LONG_CONSTANT("FORCE_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("FORCE_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
+       
+       REGISTER_LONG_CONSTANT("ZLIB_ENCODING_RAW", PHP_ZLIB_ENCODING_RAW, CONST_CS|CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("ZLIB_ENCODING_GZIP", PHP_ZLIB_ENCODING_GZIP, CONST_CS|CONST_PERSISTENT);
+       REGISTER_LONG_CONSTANT("ZLIB_ENCODING_DEFLATE", PHP_ZLIB_ENCODING_DEFLATE, CONST_CS|CONST_PERSISTENT);
+       REGISTER_INI_ENTRIES();
+       return SUCCESS;
 }
 /* }}} */
 
-/* {{{ php_ob_gzhandler_check
- */
-static int php_ob_gzhandler_check(zval *handler_name TSRMLS_DC)
+/* {{{ PHP_MSHUTDOWN_FUNCTION */
+PHP_MSHUTDOWN_FUNCTION(zlib)
 {
-       /* check for wrong usages */
-       if (php_output_get_level(TSRMLS_C) > 0) {
-               zval tmp;
-               
-               if (php_output_handler_started(handler_name TSRMLS_CC)) {
-                       php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used twice");
-                       return FAILURE;
-               }
-               INIT_PZVAL(&tmp);
-               ZVAL_ASCII_STRINGL(&tmp, "mb_output_handler", sizeof("mb_output_handler")-1, ZSTR_DUPLICATE);
-               if (php_output_handler_started(&tmp TSRMLS_CC)) {
-                       zval_dtor(&tmp);
-                       php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'mb_output_handler'");
-                       return FAILURE;
-               }
-               zval_dtor(&tmp);
-               ZVAL_ASCII_STRINGL(&tmp, "URL-Reqriter", sizeof("URL-Rewriter")-1, ZSTR_DUPLICATE);
-               if (php_output_handler_started(&tmp TSRMLS_CC)) {
-                       zval_dtor(&tmp);
-                       php_error_docref("ref.outcontrol" TSRMLS_CC, E_WARNING, "output handler 'ob_gzhandler' cannot be used after 'URL-Rewriter'");
-                       return FAILURE;
-               }
-               zval_dtor(&tmp);
-               ZVAL_ASCII_STRINGL(&tmp, "zlib output compression", sizeof("zlib output compression")-1, ZSTR_DUPLICATE);
-               if (php_output_handler_conflict(handler_name, &tmp TSRMLS_CC)) {
-                       zval_dtor(&tmp);
-                       return FAILURE;
-               }
-               zval_dtor(&tmp);
-       }
-
+       php_unregister_url_stream_wrapper("zlib" TSRMLS_CC);
+       php_stream_filter_unregister_factory("zlib.*" TSRMLS_CC);
+       
+       UNREGISTER_INI_ENTRIES();
        return SUCCESS;
 }
 /* }}} */
 
-/* {{{ proto string ob_gzhandler(string str, int mode)
-   Encode str based on accept-encoding setting - designed to be called from ob_start() */
-PHP_FUNCTION(ob_gzhandler)
+/* {{{ PHP_RINIT_FUNCTION */
+PHP_RINIT_FUNCTION(zlib)
 {
-       char *string;
-       int string_len;
-       long mode;
-       zval **a_encoding;
-       zend_bool return_original = 0;
-       zend_bool do_start, do_end;
-
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &string, &string_len, &mode) == FAILURE) {
-               return;
-       }
-
-       if(ZLIBG(ob_gzhandler_status) == -1) {
-               RETURN_FALSE;
-       }
-
-       zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
-
-       if (!PG(http_globals)[TRACK_VARS_SERVER]
-               || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE
-       ) {
-               ZLIBG(ob_gzhandler_status) = -1;
-       } else {
-               convert_to_string_ex(a_encoding);
-               if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
-                       ZLIBG(compression_coding) = CODING_GZIP;
-               } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
-                       ZLIBG(compression_coding) = CODING_DEFLATE;
-               } else {
-                       ZLIBG(ob_gzhandler_status) = -1;
-               }
-       }
+       zval *zoh, *tmp;
+       php_output_handler *h;
        
-       if (ZLIBG(ob_gzhandler_status == -1)) {
-               /* don't call this handler any more */
-               php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_DISABLE, NULL TSRMLS_CC);
-               RETURN_FALSE;
-       }
+       ZLIBG(compression_coding) = 0;
        
-       do_start = ((mode & PHP_OUTPUT_HANDLER_START) ? 1 : 0);
-       do_end = ((mode & PHP_OUTPUT_HANDLER_END) ? 1 : 0);
-       Z_STRVAL_P(return_value) = NULL;
-       Z_STRLEN_P(return_value) = 0;
-
-       if (php_deflate_string(string, string_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), do_start, do_end TSRMLS_CC) == SUCCESS) {
-               Z_TYPE_P(return_value) = IS_STRING;
-               if (do_start) {
-                       switch (ZLIBG(compression_coding)) {
-                               case CODING_GZIP:
-                                       if (sapi_add_header("Content-Encoding: gzip", sizeof("Content-Encoding: gzip") - 1, 1) == FAILURE) {
-                                               return_original = 1;
-                                       }
-                                       if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) {
-                                               return_original = 1;
-                                       }
-                                       break;
-                               case CODING_DEFLATE:
-                                       if (sapi_add_header("Content-Encoding: deflate", sizeof("Content-Encoding: deflate") - 1, 1) == FAILURE) {
-                                               return_original = 1;
-                                       }
-                                       if (sapi_add_header_ex("Vary: Accept-Encoding", sizeof("Vary: Accept-Encoding") - 1, 1, 0 TSRMLS_CC)==FAILURE) {
-                                               return_original = 1;
-                                       }
-                                       break;
-                               default:
-                                       return_original = 1;
-                                       break;
+       switch (ZLIBG(output_compression)) {
+               case 0:
+                       break;
+               case 1:
+                       ZLIBG(output_compression) = PHP_OUTPUT_HANDLER_DEFAULT_SIZE;
+               default:
+                       MAKE_STD_ZVAL(tmp);
+                       ZVAL_ASCII_STRING(tmp, PHP_ZLIB_OUTPUT_HANDLER_NAME, ZSTR_DUPLICATE);
+                       if ((h = php_zlib_output_handler_init(tmp TSRMLS_CC)) && (SUCCESS == php_output_handler_start(h TSRMLS_CC))) {
+                               if (ZLIBG(output_handler) && *ZLIBG(output_handler)) {
+                                       MAKE_STD_ZVAL(zoh);
+                                       ZVAL_ASCII_STRING(zoh, ZLIBG(output_handler), ZSTR_DUPLICATE);
+                                       php_output_start_user(zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
+                                       zval_ptr_dtor(&zoh);
+                               }
                        }
-               }
-
-               if (return_original) {
-                       zval_dtor(return_value);
-               }
-
-       } else {
-               return_original = 1;
-       }
-
-       if (return_original) {
-               /* don't call this handler any more */
-               php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_DISABLE, NULL TSRMLS_CC);
-               /* return the original string */
-               RETURN_STRINGL(string, string_len, 1);
-       } else {
-               /* don't allow cleaning and removing any longer */
-               php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_IMMUTABLE, NULL TSRMLS_CC);
+                       zval_ptr_dtor(&tmp);
+                       break;
        }
+       return SUCCESS;
 }
 /* }}} */
 
-/* {{{ php_gzip_output_handler
- */
-static void php_gzip_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+/* {{{ PHP_MINFO_FUNCTION */
+PHP_MINFO_FUNCTION(zlib)
 {
-       zend_bool do_start, do_end;
+       php_info_print_table_start();
+       php_info_print_table_header(2, "ZLib Support", "enabled");
+       php_info_print_table_row(2, "Stream Wrapper", "compress.zlib://");
+       php_info_print_table_row(2, "Stream Filter", "zlib.inflate, zlib.deflate");
+       php_info_print_table_row(2, "Compiled Version", ZLIB_VERSION);
+       php_info_print_table_row(2, "Linked Version", (char *) zlibVersion());
+       php_info_print_table_end();
 
-       if (!ZLIBG(output_compression)) {
-               *handled_output = NULL;
-       } else {
-               do_start = (mode & PHP_OUTPUT_HANDLER_START ? 1 : 0);
-               do_end = (mode & PHP_OUTPUT_HANDLER_END ? 1 : 0);
-               if (php_deflate_string(output, output_len, handled_output, handled_output_len, do_start, do_end TSRMLS_CC) != SUCCESS) {
-                       zend_error(E_ERROR, "Compression failed");
-               }
-       }
+       DISPLAY_INI_ENTRIES();
 }
 /* }}} */
 
-/* {{{ php_enable_output_compression
- */
-static int php_enable_output_compression(int buffer_size TSRMLS_DC)
-{
-       zval **a_encoding, *output_handler;
-
-       zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC);
-
-       if (!PG(http_globals)[TRACK_VARS_SERVER]
-               || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void **) &a_encoding) == FAILURE
-       ) {
-               return FAILURE;
-       }
-
-       convert_to_string_ex(a_encoding);
-
-       if (php_memnstr(Z_STRVAL_PP(a_encoding), "gzip", 4, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
-               ZLIBG(compression_coding) = CODING_GZIP;
-       } else if (php_memnstr(Z_STRVAL_PP(a_encoding), "deflate", 7, Z_STRVAL_PP(a_encoding) + Z_STRLEN_PP(a_encoding))) {
-               ZLIBG(compression_coding) = CODING_DEFLATE;
-       } else {
-               return FAILURE;
-       }
-
-       MAKE_STD_ZVAL(output_handler);
-       ZVAL_ASCII_STRINGL(output_handler, "zlib output compression", sizeof("zlib output compression")-1, ZSTR_DUPLICATE);
-       php_output_start_internal(output_handler, php_gzip_output_handler, buffer_size, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
-       zval_ptr_dtor(&output_handler);
-       
-       if (ZLIBG(output_handler) && *ZLIBG(output_handler)) {
-               MAKE_STD_ZVAL(output_handler);
-               ZVAL_ASCII_STRING(output_handler, ZLIBG(output_handler), ZSTR_DUPLICATE);
-               php_output_start_user(output_handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
-               zval_ptr_dtor(&output_handler);
-       }
-       return SUCCESS;
-}
+/* {{{ php_zlib_module_entry */
+zend_module_entry php_zlib_module_entry = {
+       STANDARD_MODULE_HEADER,
+       "zlib",
+       php_zlib_functions,
+       PHP_MINIT(zlib),
+       PHP_MSHUTDOWN(zlib),
+       PHP_RINIT(zlib),
+       NULL,
+       PHP_MINFO(zlib),
+       "2.0",
+       PHP_MODULE_GLOBALS(zlib),
+       NULL,
+       NULL,
+       NULL,
+       STANDARD_MODULE_PROPERTIES_EX
+};
 /* }}} */
 
 /*