- gcc didn't like those changes
[m6w6/ext-http] / http_encoding_api.c
index 0b68397491bf5d5cbf59fe5ad6c1d17b36747d73..d1b16c4f7547989a931ed8206a56f24e1ad16e3b 100644 (file)
@@ -193,7 +193,7 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
 #define HTTP_WINDOW_BITS_ANY   0x0000002f
 #define HTTP_WINDOW_BITS_RAW   -0x000000f
 
-STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
 {
        int status, level, wbits, strategy;
        z_stream Z;
@@ -234,7 +234,7 @@ STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
 {
        int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
        z_stream Z;
@@ -258,7 +258,7 @@ retry_inflate:
 
                        do {
                                Z.avail_out = (buffer.free -= Z.total_out - buffer.used);
-                               Z.next_out = buffer.data + (buffer.used = Z.total_out);
+                               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);
@@ -287,7 +287,7 @@ retry_inflate:
 }
 
 
-http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
+PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
 {
        int status, level, wbits, strategy, free_stream;
        
@@ -318,7 +318,7 @@ http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s
        return NULL;
 }
 
-http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
+PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
 {
        int status, wbits, free_stream;
        
@@ -347,19 +347,21 @@ http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s
        return NULL;
 }
 
-STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
+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 TSRMLS_DC)
 {
        int status;
        
        /* append input to our buffer */
        phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
        
-       s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
+       s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
        s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
        
        /* deflate */
-       s->stream.avail_out = *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
-       s->stream.next_out = *encoded = emalloc(*encoded_len);
+       *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
+       *encoded = emalloc(*encoded_len);
+       s->stream.avail_out = *encoded_len;
+       s->stream.next_out = (Bytef *) *encoded;
        
        switch (status = deflate(&s->stream, Z_NO_FLUSH))
        {
@@ -382,7 +384,7 @@ STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+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 TSRMLS_DC)
 {
        int status, max = 0;
        
@@ -399,10 +401,10 @@ STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char
                *decoded = erealloc(*decoded, *decoded_len);
                
 retry_raw_inflate:
-               s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
+               s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
                s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
        
-               s->stream.next_out = *decoded;
+               s->stream.next_out = (Bytef *) *decoded;
                s->stream.avail_out = *decoded_len;
                
                switch (status = inflate(&s->stream, Z_NO_FLUSH))
@@ -437,16 +439,77 @@ retry_raw_inflate:
        return FAILURE;
 }
 
-STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
+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 = PHPSTR_VAL(s->stream.opaque);
+       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 = 0x800;
-       s->stream.next_out = *encoded = emalloc(*encoded_len);
+       s->stream.avail_out = *encoded_len;
+       s->stream.next_out = (Bytef *) *encoded;
        
        do {
                status = deflate(&s->stream, Z_FINISH);
@@ -469,16 +532,19 @@ STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **enco
        return FAILURE;
 }
 
-STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
+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 = PHPSTR_VAL(s->stream.opaque);
+       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.avail_in << 2;
-       s->stream.next_out = *decoded = emalloc(*decoded_len);
+       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 */
@@ -497,7 +563,7 @@ STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **deco
        return FAILURE;
 }
 
-void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
                if (s->stream.opaque) {
@@ -507,7 +573,7 @@ void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
        }
 }
 
-void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
 {
        if (s) {
                if (s->stream.opaque) {
@@ -517,7 +583,7 @@ void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
        }
 }
 
-void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
                http_encoding_deflate_stream_dtor(*s);
@@ -528,7 +594,7 @@ void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
        }
 }
 
-void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
+PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
 {
        if (s) {
                http_encoding_inflate_stream_dtor(*s);