+ int status, level, wbits, strategy;
+ z_stream Z;
+
+ HTTP_DEFLATE_LEVEL_SET(flags, level);
+ HTTP_DEFLATE_WBITS_SET(flags, wbits);
+ HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
+
+ memset(&Z, 0, sizeof(z_stream));
+ *encoded = NULL;
+ *encoded_len = 0;
+
+ status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
+ if (Z_OK == status) {
+ *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
+ *encoded = emalloc_rel(*encoded_len);
+
+ Z.next_in = (Bytef *) data;
+ Z.next_out = (Bytef *) *encoded;
+ Z.avail_in = data_len;
+ Z.avail_out = *encoded_len;
+
+ status = deflate(&Z, Z_FINISH);
+ deflateEnd(&Z);
+
+ if (Z_STREAM_END == status) {
+ /* size buffer down to actual length */
+ *encoded = erealloc_rel(*encoded, Z.total_out + 1);
+ (*encoded)[*encoded_len = Z.total_out] = '\0';
+ return SUCCESS;
+ } else {
+ STR_SET(*encoded, NULL);
+ *encoded_len = 0;
+ }
+ }
+
+ http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
+ return FAILURE;
+}
+/* }}} */
+
+/* {{{ STATUS http_encoding_inflate(char *, size_t, char **, size_t) */
+PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
+{
+ z_stream Z;
+ int status, wbits = HTTP_WINDOW_BITS_ANY;
+
+ memset(&Z, 0, sizeof(z_stream));
+
+retry_raw_inflate:
+ status = inflateInit2(&Z, wbits);
+ if (Z_OK == status) {
+ Z.next_in = (Bytef *) data;
+ Z.avail_in = data_len;
+
+ switch (status = http_inflate_rounds(&Z, Z_NO_FLUSH, decoded, decoded_len)) {
+ case Z_STREAM_END:
+ inflateEnd(&Z);
+ return SUCCESS;
+
+ case Z_OK:
+ status = Z_DATA_ERROR;
+ break;
+
+ case Z_DATA_ERROR:
+ /* raw deflated data? */
+ if (HTTP_WINDOW_BITS_ANY == wbits) {
+ inflateEnd(&Z);
+ wbits = HTTP_WINDOW_BITS_RAW;
+ goto retry_raw_inflate;
+ }
+ }
+ inflateEnd(&Z);