+ 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 &~ 0xf);
+ 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 &~ 0xf));
+ }
+
+ 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);
+ }
+}
+
+static const char http_encoding_gzip_header[] = {
+ (const char) 0x1f, // fixed value
+ (const char) 0x8b, // fixed value
+ (const char) Z_DEFLATED, // compression algorithm
+ (const char) 0, // none of the possible flags defined by the GZIP "RFC"
+ (const char) 0, // MTIME
+ (const char) 0, // =*=
+ (const char) 0, // =*=
+ (const char) 0, // =*=
+ (const char) 0, // two possible flag values for 9 compression levels? o_O
+#ifdef PHP_WIN32
+ (const char) 0x0b // OS_CODE
+#else
+ (const char) 0x03 // OS_CODE
+#endif
+};
+
+PHP_HTTP_API STATUS _http_encoding_gzencode_verify(const char *data, size_t data_len, const char **encoded, size_t *encoded_len, int error_level TSRMLS_DC)
+{
+ size_t offset = sizeof(http_encoding_gzip_header);