+void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+{
+ getGlobals(G);
+
+ *handled_output = NULL;
+ *handled_output_len = 0;
+
+ if (mode & PHP_OUTPUT_HANDLER_START) {
+ int flags;
+
+ if (G->send.deflate.stream) {
+ zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
+ return;
+ }
+
+ G->send.deflate.encoding = !0;
+
+ switch (http_encoding_response_start(0))
+ {
+ 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;
+ break;
+ }
+
+ flags |= (G->send.deflate.start_flags &~ 0xf0);
+ G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
+ }
+
+ if (G->send.deflate.stream) {
+ http_encoding_deflate_stream_update((http_encoding_stream *) 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 *) G->send.deflate.stream, &remaining, &remaining_len);
+ http_encoding_deflate_stream_free((http_encoding_stream **) &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 *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+{
+ getGlobals(G);
+
+ *handled_output = NULL;
+ *handled_output_len = 0;
+
+ if (mode & PHP_OUTPUT_HANDLER_START) {
+ if (G->send.inflate.stream) {
+ zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
+ return;
+ }
+ G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G(send).inflate.start_flags &~ 0xf0));
+ }
+
+ if (G->send.inflate.stream) {
+ http_encoding_inflate_stream_update((http_encoding_stream *) 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 *) G->send.inflate.stream, &remaining, &remaining_len);
+ http_encoding_inflate_stream_free((http_encoding_stream **) &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);
+ }
+}
+