- finish work on encoding api
authorMichael Wallner <mike@php.net>
Fri, 30 Dec 2005 14:30:20 +0000 (14:30 +0000)
committerMichael Wallner <mike@php.net>
Fri, 30 Dec 2005 14:30:20 +0000 (14:30 +0000)
http_encoding_api.c
http_inflatestream_object.c
php_http_encoding_api.h
tests/encoding_objects_001.phpt

index c0d1140d4d452f5e0d304caea652959836709c1d..f4b5746b092ef5473fa591361bcd52624386b4ee 100644 (file)
@@ -24,6 +24,7 @@
 #include "php_http_send_api.h"
 #include "php_http_headers_api.h"
 
+/* {{{ */
 #ifdef HTTP_HAVE_ZLIB
 PHP_MINIT_FUNCTION(http_encoding)
 {
@@ -46,10 +47,10 @@ PHP_RINIT_FUNCTION(http_encoding)
        getGlobals(G);
        
        if (G->send.inflate.start_auto) {
-               php_ob_set_internal_handler(_http_ob_inflatehandler, 0x1000, "http inflate", 0 TSRMLS_CC);
+               php_ob_set_internal_handler(_http_ob_inflatehandler, HTTP_INFLATE_BUFFER_SIZE, "http inflate", 0 TSRMLS_CC);
        }
        if (G->send.deflate.start_auto) {
-               php_ob_set_internal_handler(_http_ob_deflatehandler, 0x8000, "http deflate", 0 TSRMLS_CC);
+               php_ob_set_internal_handler(_http_ob_deflatehandler, HTTP_DEFLATE_BUFFER_SIZE, "http deflate", 0 TSRMLS_CC);
        }
        return SUCCESS;
 }
@@ -67,12 +68,14 @@ PHP_RSHUTDOWN_FUNCTION(http_encoding)
        return SUCCESS;
 }
 #endif
+/* }}} */
 
+/* {{{ eol_match(char **, int *) */
 static inline int eol_match(char **line, int *eol_len)
 {
        char *ptr = *line;
        
-       while (0x20 == *ptr) ++ptr;
+       while (' ' == *ptr) ++ptr;
 
        if (ptr == http_locate_eol(*line, eol_len)) {
                *line = ptr;
@@ -81,6 +84,7 @@ static inline int eol_match(char **line, int *eol_len)
                return 0;
        }
 }
+/* }}} */
                        
 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
@@ -156,8 +160,73 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
 }
 /* }}} */
 
+/* {{{ int http_encoding_response_start(size_t) */
+PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
+{
+       if (    php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
+                       php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
+               HTTP_G(send).deflate.encoding = 0;
+       } else {
+               if (!HTTP_G(send).deflate.encoding) {
+                       /* emit a content-length header */
+                       if (content_length) {
+                               char cl_header_str[128];
+                               size_t cl_header_len;
+                               cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
+                               http_send_header_string_ex(cl_header_str, cl_header_len, 1);
+                       }
+               } else {
+#ifndef HTTP_HAVE_ZLIB
+                       HTTP_G(send).deflate.encoding = 0;
+                       php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
+#else
+                       HashTable *selected;
+                       zval zsupported;
+                       
+                       INIT_PZVAL(&zsupported);
+                       array_init(&zsupported);
+                       add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
+                       add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
+                       add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
+                       
+                       HTTP_G(send).deflate.encoding = 0;
+                       
+                       if ((selected = http_negotiate_encoding(&zsupported))) {
+                               STATUS hs = FAILURE;
+                               char *encoding = NULL;
+                               ulong idx;
+                               
+                               if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
+                                       if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
+                                               if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
+                                                       HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
+                                               }
+                                       } else if (!strcmp(encoding, "deflate")) {
+                                               if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
+                                                       HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
+                                               }
+                                       }
+                                       if (SUCCESS == hs) {
+                                               http_send_header_string("Vary: Accept-Encoding");
+                                       }
+                               }
+                               
+                               zend_hash_destroy(selected);
+                               FREE_HASHTABLE(selected);
+                       }
+                       
+                       zval_dtor(&zsupported);
+                       return HTTP_G(send).deflate.encoding;
+#endif
+               }
+       }
+       return 0;
+}
+/* }}} */
+
 #ifdef HTTP_HAVE_ZLIB
 
+/* {{{ */
 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
        switch (flags & 0xf) \
        { \
@@ -216,7 +285,9 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
 #define HTTP_WINDOW_BITS_GZIP  0x0000001f
 #define HTTP_WINDOW_BITS_ANY   0x0000002f
 #define HTTP_WINDOW_BITS_RAW   -0x000000f
+/* }}} */
 
+/* {{{ STATUS http_encoding_deflate(int, char *, size_t, char **, size_t *) */
 PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        int status, level, wbits, strategy;
@@ -232,7 +303,7 @@ PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t d
        
        status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
        if (Z_OK == status) {
-               *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
+               *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
                *encoded = emalloc_rel(*encoded_len);
                
                Z.next_in = (Bytef *) data;
@@ -254,13 +325,15 @@ PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t d
                }
        }
        
-       http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s (%s)", zError(status));
+       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)
 {
-       int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
+       int status, round = 0, wbits = HTTP_WINDOW_BITS_ANY;
        z_stream Z;
        phpstr buffer;
        
@@ -285,7 +358,7 @@ retry_inflate:
                                Z.next_out = (Bytef *) buffer.data + (buffer.used = Z.total_out);
                                status = inflate(&Z, Z_NO_FLUSH);
                        } while (Z_OK == status);
-               } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
+               } while (Z_BUF_ERROR == status && ++round < HTTP_INFLATE_ROUNDS);
                
                if (Z_DATA_ERROR == status && HTTP_WINDOW_BITS_ANY == wbits) {
                        /* raw deflated data? */
@@ -309,8 +382,9 @@ retry_inflate:
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
-
+/* {{{ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *, int) */
 PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        int status, level, wbits, strategy, free_stream;
@@ -328,7 +402,7 @@ PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encod
        if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
                int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
                
-               if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
+               if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
                        return s;
                }
                deflateEnd(&s->stream);
@@ -341,7 +415,9 @@ PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encod
        }
        return NULL;
 }
+/* }}} */
 
+/* {{{ http_encoding_stream *http_encoding_inflate_stream_init(http_encoding_stream *, int) */
 PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        int status, wbits, free_stream;
@@ -357,7 +433,7 @@ PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encod
        if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
                int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
                
-               if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
+               if ((s->stream.opaque = phpstr_init_ex(NULL, HTTP_DEFLATE_BUFFER_SIZE, p))) {
                        return s;
                }
                inflateEnd(&s->stream);
@@ -370,7 +446,9 @@ PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encod
        }
        return NULL;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_deflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
 PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        int status;
@@ -382,7 +460,7 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s
        s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
        
        /* deflate */
-       *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
+       *encoded_len = HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
        *encoded = emalloc_rel(*encoded_len);
        s->stream.avail_out = *encoded_len;
        s->stream.next_out = (Bytef *) *encoded;
@@ -407,10 +485,12 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_inflate_stream_update(http_encoding_stream *, char *, size_t, char **, size_t *) */
 PHP_HTTP_API STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
-       int status, max = 0;
+       int status, round = 0;
        
        /* append input to buffer */
        phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
@@ -455,19 +535,21 @@ retry_raw_inflate:
                                }
                        break;
                }
-       } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
+       } while (Z_BUF_ERROR == status && ++round < HTTP_INFLATE_ROUNDS);
        
        STR_SET(*decoded, NULL);
        *decoded_len = 0;
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_deflate_stream_flush(http_encoding_stream *, char **, size_t *) */
 PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(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 = 0x8000;
+       *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
        *encoded = emalloc_rel(*encoded_len);
        
        s->stream.avail_in = 0;
@@ -479,7 +561,7 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s,
        {
                case Z_OK:
                case Z_STREAM_END:
-                       *encoded_len = 0x8000 - s->stream.avail_out;
+                       *encoded_len = HTTP_DEFLATE_BUFFER_SIZE - s->stream.avail_out;
                        *encoded = erealloc_rel(*encoded, *encoded_len + 1);
                        (*encoded)[*encoded_len] = '\0';
                        return SUCCESS;
@@ -491,19 +573,23 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s,
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_inflate_straem_flush(http_encoding_stream *, char **, size_t *) */
 PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
 {
        /* noop */
        *decoded = estrndup("", *decoded_len = 0);
        return SUCCESS;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_deflate_stream_finish(http_encoding_stream *, char **, size_t *) */
 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 = 0x8000;
+       *encoded_len = HTTP_DEFLATE_BUFFER_SIZE;
        *encoded = emalloc_rel(*encoded_len);
        
        /* deflate remaining input */
@@ -533,12 +619,14 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
+/* {{{ STATUS http_encoding_inflate_stream_finish(http_encoding_stream *, char **, size_t *) */
 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 = PHPSTR_LEN(s->stream.opaque) << 2;
+       *decoded_len = (PHPSTR_LEN(s->stream.opaque) + 1) * HTTP_INFLATE_ROUNDS;
        *decoded = emalloc_rel(*decoded_len);
        
        /* inflate remaining input */
@@ -564,7 +652,9 @@ PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s
        http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
        return FAILURE;
 }
+/* }}} */
 
+/* {{{ void http_encoding_deflate_stream_dtor(http_encoding_stream *) */
 PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
@@ -574,7 +664,9 @@ PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSR
                deflateEnd(&s->stream);
        }
 }
+/* }}} */
 
+/* {{{ void http_encoding_inflate_stream_dtor(http_encoding_stream *) */
 PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
@@ -584,7 +676,9 @@ PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSR
                inflateEnd(&s->stream);
        }
 }
+/* }}} */
 
+/* {{{ void http_encoding_deflate_stream_free(http_encoding_stream **) */
 PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
@@ -595,7 +689,9 @@ PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TS
                *s = NULL;
        }
 }
+/* }}} */
 
+/* {{{ void http_encoding_inflate_stream_free(http_encoding_stream **) */
 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
@@ -606,7 +702,9 @@ PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TS
                *s = NULL;
        }
 }
+/* }}} */
 
+/* {{{ void http_ob_deflatehandler(char *, uint, char **, uint *, int) */
 void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
 {
        getGlobals(G);
@@ -664,7 +762,9 @@ deflate_passthru_plain:
                *handled_output = estrndup(output, *handled_output_len = output_len);
        }
 }
+/* }}} */
 
+/* {{{ void http_ob_inflatehandler(char *, uint, char **, uint *, int) */
 void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
 {
        getGlobals(G);
@@ -700,197 +800,10 @@ void _http_ob_inflatehandler(char *output, uint output_len, char **handled_outpu
                *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);
-       
-       if (data_len < offset) {
-               goto really_bad_gzip_header;
-       }
-       
-       if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
-               http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized GZIP header start: 0x%02X 0x%02X", (int) data[0], (int) (data[1] & 0xFF));
-               return FAILURE;
-       }
-       
-       if (data[2] != (const char) Z_DEFLATED) {
-               http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
-               /* still try to decode */
-       }
-       if ((data[3] & 0x4) == 0x4) {
-               if (data_len < offset + 2) {
-                       goto really_bad_gzip_header;
-               }
-               /* there are extra fields, the length follows the common header as 2 bytes LSB */
-               offset += (unsigned) ((data[offset] & 0xFF));
-               offset += 1;
-               offset += (unsigned) ((data[offset] & 0xFF) << 8);
-               offset += 1;
-       }
-       if ((data[3] & 0x8) == 0x8) {
-               if (data_len <= offset) {
-                       goto really_bad_gzip_header;
-               }
-               /* there's a file name */
-               offset += strlen(&data[offset]) + 1 /*NUL*/;
-       }
-       if ((data[3] & 0x10) == 0x10) {
-               if (data_len <= offset) {
-                       goto really_bad_gzip_header;
-               }
-               /* there's a comment */
-               offset += strlen(&data[offset]) + 1 /* NUL */;
-       }
-       if ((data[3] & 0x2) == 0x2) {
-               /* there's a CRC16 of the header */
-               offset += 2;
-               if (data_len <= offset) {
-                       goto really_bad_gzip_header;
-               } else {
-                       ulong crc, cmp;
-                       
-                       cmp =  (unsigned) ((data[offset-2] & 0xFF));
-                       cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
-                       
-                       crc = crc32(0L, Z_NULL, 0);
-                       crc = crc32(crc, (const Bytef *) data, sizeof(http_encoding_gzip_header));
-                       
-                       if (cmp != (crc & 0xFFFF)) {
-                               http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
-                               return FAILURE;
-                       }
-               }
-       }
-       
-       if (data_len < offset + 8) {
-               http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
-               return FAILURE;
-       }
-       
-       if (encoded) {
-               *encoded = data + offset;
-       }
-       if (encoded_len) {
-               *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;     
-       }
-       
-       return SUCCESS;
-       
-really_bad_gzip_header:
-       http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
-       return FAILURE;
-}
-
-PHP_HTTP_API STATUS _http_encoding_gzdecode_verify(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
-{
-       STATUS status = SUCCESS;
-       ulong len, cmp, crc;
-       
-       crc = crc32(0L, Z_NULL, 0);
-       crc = crc32(crc, (const Bytef *) decoded, decoded_len);
-       
-       cmp  = (unsigned) ((data[data_len-8] & 0xFF));
-       cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
-       cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
-       cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
-       len  = (unsigned) ((data[data_len-4] & 0xFF));
-       len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
-       len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
-       len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
-       
-       if (cmp != crc) {
-               http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
-               status = FAILURE;
-       }
-       if (len != decoded_len) {
-               http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, decoded_len);
-               status = FAILURE;
-       }
-       return status;
-}
+/* }}} */
 
 #endif /* HTTP_HAVE_ZLIB */
 
-PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
-{
-       if (    php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
-                       php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
-               HTTP_G(send).deflate.encoding = 0;
-       } else {
-               if (!HTTP_G(send).deflate.encoding) {
-                       /* emit a content-length header */
-                       if (content_length) {
-                               char cl_header_str[128];
-                               size_t cl_header_len;
-                               cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
-                               http_send_header_string_ex(cl_header_str, cl_header_len, 1);
-                       }
-               } else {
-#ifndef HTTP_HAVE_ZLIB
-                       HTTP_G(send).deflate.encoding = 0;
-                       php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
-#else
-                       HashTable *selected;
-                       zval zsupported;
-                       
-                       INIT_PZVAL(&zsupported);
-                       array_init(&zsupported);
-                       add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
-                       add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
-                       add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
-                       
-                       HTTP_G(send).deflate.encoding = 0;
-                       
-                       if ((selected = http_negotiate_encoding(&zsupported))) {
-                               STATUS hs = FAILURE;
-                               char *encoding = NULL;
-                               ulong idx;
-                               
-                               if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
-                                       if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
-                                               if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
-                                                       HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
-                                               }
-                                       } else if (!strcmp(encoding, "deflate")) {
-                                               if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
-                                                       HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
-                                               }
-                                       }
-                                       if (SUCCESS == hs) {
-                                               http_send_header_string("Vary: Accept-Encoding");
-                                       }
-                               }
-                               
-                               zend_hash_destroy(selected);
-                               FREE_HASHTABLE(selected);
-                       }
-                       
-                       zval_dtor(&zsupported);
-                       return HTTP_G(send).deflate.encoding;
-#endif
-               }
-       }
-       return 0;
-}
-
 /*
  * Local variables:
  * tab-width: 4
index 853e4fd3363693ea905898ced2d094f1ad1f0ddc..39abcee3edbfbc74e3019c57eee5be33f881943f 100644 (file)
@@ -218,14 +218,15 @@ PHP_METHOD(HttpInflateStream, finish)
                        updated = erealloc(updated, updated_len + decoded_len + 1);
                        updated[updated_len + decoded_len] = '\0';
                        memcpy(updated + updated_len, decoded, decoded_len);
+                       STR_FREE(decoded);
                        updated_len += decoded_len;
                        RETVAL_STRINGL(updated, updated_len, 0);
-                       STR_FREE(decoded);
                } else {
-                       RETVAL_STRINGL(decoded, decoded_len, 0);
                        STR_FREE(updated);
+                       RETVAL_STRINGL(decoded, decoded_len, 0);
                }
        } else {
+               STR_FREE(updated);
                RETVAL_FALSE;
        }
        
index 4c01360b5df6f42f365ca28bc41f48c9f4da08b4..a50fc49485c9acf20fbf05a63fa5aa93bee8b044 100644 (file)
@@ -27,19 +27,20 @@ extern PHP_MINIT_FUNCTION(http_encoding);
 extern PHP_RINIT_FUNCTION(http_encoding);
 extern PHP_RSHUTDOWN_FUNCTION(http_encoding);
 
-/* 100% compression should be fairly good */
-#define HTTP_ENCODING_MAXTRY 100
-/* safe padding */
-#define HTTP_ENCODING_SAFPAD 28
-/* add 1% extra space in case we need to encode widely differing (binary) data */
-#define HTTP_ENCODING_BUFLEN(l) (l + (l / 100) + HTTP_ENCODING_SAFPAD)
-
 typedef enum {
        HTTP_ENCODING_NONE,
        HTTP_ENCODING_GZIP,
        HTTP_ENCODING_DEFLATE,
 } http_encoding_type;
 
+#define HTTP_INFLATE_ROUNDS 100
+
+#define HTTP_DEFLATE_BUFFER_SIZE_GUESS(S) \
+       (((size_t) ((double) S * (double) 1.015)) + 10 + 8 + 4 + 1)
+
+#define HTTP_DEFLATE_BUFFER_SIZE               0x8000
+#define HTTP_INFLATE_BUFFER_SIZE               0x1000
+
 #define HTTP_DEFLATE_LEVEL_DEF                 0x00000000
 #define HTTP_DEFLATE_LEVEL_MIN                 0x00000001
 #define HTTP_DEFLATE_LEVEL_MAX                 0x00000002
@@ -96,10 +97,11 @@ PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSR
 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC);
 
 #define http_ob_deflatehandler(o, ol, h, hl, m) _http_ob_deflatehandler((o), (ol), (h), (hl), (m) TSRMLS_CC)
-void _http_ob_deflatehandler(char *, uint, char **, uint *, int TSRMLS_DC);
+extern void _http_ob_deflatehandler(char *, uint, char **, uint *, int TSRMLS_DC);
 
 #define http_ob_inflatehandler(o, ol, h, hl, m) _http_ob_inflatehandler((o), (ol), (h), (hl), (m) TSRMLS_CC)
-void _http_ob_inflatehandler(char *, uint, char **, uint *, int TSRMLS_DC);
+extern void _http_ob_inflatehandler(char *, uint, char **, uint *, int TSRMLS_DC);
+
 #endif /* HTTP_HAVE_ZLIB */
 
 #endif
index f2fc6e29588e7b792919111808346fd5078b35a4..67da0ce634b3945178b037a6d63e0c02fccb2bc2 100644 (file)
@@ -14,11 +14,22 @@ $i = new HttpInflateStream;
 echo $i->flush($d->flush("Hi "));
 echo $i->finish($d->finish("there!\n"));
 echo $i->finish($d->finish("Yo...\n"));
+
+$id = $i->update($d->update($pd = file_get_contents(__FILE__)));
+foreach (glob('*.phpt') as $f) {
+       $id .= $i->update($d->update($tmp = file_get_contents($f)));
+       $pd .= $tmp;
+}
+$id .= $i->finish($d->finish());
+
+var_dump($id == $pd);
+
 echo "Done\n";
 ?>
 --EXPECTF--
 %sTEST
 Hi there!
 Yo...
+bool(true)
 Done