+/* }}} */
+
+/* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
+PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+{
+ if (s) {
+ if (s->stream.opaque) {
+ phpstr_free((phpstr **) &s->stream.opaque);
+ }
+ deflateEnd(&s->stream);
+ }
+}
+/* }}} */
+
+/* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
+PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+{
+ if (s) {
+ if (s->stream.opaque) {
+ phpstr_free((phpstr **) &s->stream.opaque);
+ }
+ inflateEnd(&s->stream);
+ }
+}
+/* }}} */
+
+/* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
+PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+{
+ if (s) {
+ http_encoding_deflate_stream_dtor(*s);
+ if (*s) {
+ pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
+ }
+ *s = NULL;
+ }
+}
+/* }}} */
+
+/* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
+PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+{
+ if (s) {
+ http_encoding_inflate_stream_dtor(*s);
+ if (*s) {
+ pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
+ }
+ *s = NULL;
+ }
+}
+/* }}} */
+
+/* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
+void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+{
+ int encoding;
+
+ *handled_output = NULL;
+ *handled_output_len = 0;
+
+ if (mode & PHP_OUTPUT_HANDLER_START) {
+ int flags;
+
+ if (HTTP_G->send.deflate.stream) {
+ zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
+ return;
+ }
+
+ HTTP_G->send.deflate.response = 1;
+ encoding = http_encoding_response_start(0, 1);
+ HTTP_G->send.deflate.response = 0;
+
+ switch (encoding) {
+ case HTTP_ENCODING_GZIP:
+ flags = HTTP_DEFLATE_TYPE_GZIP;
+ break;
+
+ case HTTP_ENCODING_DEFLATE:
+ flags = HTTP_DEFLATE_TYPE_ZLIB;
+ break;
+
+ default:
+ goto deflate_passthru_plain;
+ }
+
+ flags |= (HTTP_G->send.deflate.start_flags &~ 0xf0);
+ HTTP_G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
+ }
+
+ if (HTTP_G->send.deflate.stream) {
+ if (output_len) {
+ http_encoding_deflate_stream_update((http_encoding_stream *) HTTP_G->send.deflate.stream, output, output_len, handled_output, handled_output_len);
+ }
+
+ if (mode & PHP_OUTPUT_HANDLER_END) {
+ char *remaining = NULL;
+ size_t remaining_len = 0;
+
+ http_encoding_deflate_stream_finish((http_encoding_stream *) HTTP_G->send.deflate.stream, &remaining, &remaining_len);
+ http_encoding_deflate_stream_free((http_encoding_stream **) &HTTP_G->send.deflate.stream);
+ if (remaining) {
+ *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
+ memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
+ (*handled_output)[*handled_output_len += remaining_len] = '\0';
+ efree(remaining);
+ }
+ }
+ } else {
+deflate_passthru_plain:
+ *handled_output = estrndup(output, *handled_output_len = output_len);
+ }
+}
+/* }}} */
+
+/* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
+void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+{
+ *handled_output = NULL;
+ *handled_output_len = 0;
+
+ if (mode & PHP_OUTPUT_HANDLER_START) {
+ if (HTTP_G->send.inflate.stream) {
+ zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
+ return;
+ }
+ HTTP_G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G->send.inflate.start_flags &~ 0xf0));
+ }
+
+ if (HTTP_G->send.inflate.stream) {
+ if (output_len) {
+ http_encoding_inflate_stream_update((http_encoding_stream *) HTTP_G->send.inflate.stream, output, output_len, handled_output, handled_output_len);
+ }
+
+ if (mode & PHP_OUTPUT_HANDLER_END) {
+ char *remaining = NULL;
+ size_t remaining_len = 0;
+
+ http_encoding_inflate_stream_finish((http_encoding_stream *) HTTP_G->send.inflate.stream, &remaining, &remaining_len);
+ http_encoding_inflate_stream_free((http_encoding_stream **) &HTTP_G->send.inflate.stream);
+ if (remaining) {
+ *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
+ memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
+ (*handled_output)[*handled_output_len += remaining_len] = '\0';
+ efree(remaining);
+ }
+ }
+ } else {
+ *handled_output = estrndup(output, *handled_output_len = output_len);
+ }
+}
+/* }}} */