+ STR_SET(*decoded, NULL);
+ *decoded_len = 0;
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush inflate stream: %s", zError(status));
+ return FAILURE;
+}
+
+PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
+{
+ int status;
+
+ *encoded_len = 0x800;
+ *encoded = emalloc_rel(*encoded_len);
+
+ /* deflate remaining input */
+ s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
+ s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
+
+ s->stream.avail_out = *encoded_len;
+ s->stream.next_out = (Bytef *) *encoded;
+
+ do {
+ status = deflate(&s->stream, Z_FINISH);
+ } while (Z_OK == status);
+
+ if (Z_STREAM_END == status) {
+ /* cut processed intp off */
+ phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
+
+ /* size down */
+ *encoded_len -= s->stream.avail_out;
+ *encoded = erealloc_rel(*encoded, *encoded_len + 1);
+ (*encoded)[*encoded_len] = '\0';
+ return SUCCESS;
+ }
+
+ STR_SET(*encoded, NULL);
+ *encoded_len = 0;
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
+ return FAILURE;
+}
+
+PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
+{
+ int status;
+
+ *decoded_len = s->stream.avail_in << 2;
+ *decoded = emalloc_rel(*decoded_len);
+
+ /* inflate remaining input */
+ s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
+ s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
+
+ s->stream.avail_out = *decoded_len;
+ s->stream.next_out = (Bytef *) *decoded;
+
+ if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
+ /* cut processed input off */
+ phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
+
+ /* size down */
+ *decoded_len -= s->stream.avail_out;
+ *decoded = erealloc_rel(*decoded, *decoded_len + 1);
+ (*decoded)[*decoded_len] = '\0';
+ return SUCCESS;
+ }
+
+ STR_SET(*decoded, NULL);
+ *decoded_len = 0;
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
+ return FAILURE;
+}
+
+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);
+ }
+}
+
+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);
+ }
+}
+
+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;
+ }
+}
+
+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 *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);
+ }