+PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
+{
+ int status;
+
+ *encoded_len = 0x800;
+ *encoded = emalloc(*encoded_len);
+
+ s->stream.avail_in = 0;
+ s->stream.next_in = NULL;
+ s->stream.avail_out = *encoded_len;
+ s->stream.next_out = (Bytef *) *encoded;
+
+ switch (status = deflate(&s->stream, Z_SYNC_FLUSH))
+ {
+ case Z_OK:
+ case Z_STREAM_END:
+ *encoded_len = 0x800 - s->stream.avail_out;
+ *encoded = erealloc(*encoded, *encoded_len + 1);
+ (*encoded)[*encoded_len] = '\0';
+ return SUCCESS;
+ break;
+ }
+
+ STR_SET(*encoded, NULL);
+ *encoded_len = 0;
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
+ return FAILURE;
+}
+
+PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
+{
+ int status;
+
+ *decoded_len = 0x800;
+ *decoded = emalloc(*decoded_len);
+
+ s->stream.avail_in = 0;
+ s->stream.next_in = NULL;
+ s->stream.avail_out = *decoded_len;
+ s->stream.next_out = (Bytef *) *decoded;
+
+ switch (status = inflate(&s->stream, Z_SYNC_FLUSH))
+ {
+ case Z_OK:
+ case Z_STREAM_END:
+ *decoded_len = 0x800 - s->stream.avail_out;
+ *decoded = erealloc(*decoded, *decoded_len + 1);
+ (*decoded)[*decoded_len] = '\0';
+ return SUCCESS;
+ break;
+ }
+
+ 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 TSRMLS_DC)
+{
+ int status;
+
+ *encoded_len = 0x800;
+ *encoded = emalloc(*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(*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 TSRMLS_DC)
+{
+ int status;
+
+ *decoded_len = s->stream.avail_in << 2;
+ *decoded = emalloc(*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(*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;
+ }
+}
+
+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
+};
+