- add phpstr_shrink()
authorMichael Wallner <mike@php.net>
Fri, 23 Dec 2005 12:42:34 +0000 (12:42 +0000)
committerMichael Wallner <mike@php.net>
Fri, 23 Dec 2005 12:42:34 +0000 (12:42 +0000)
- fix some warnings

http_encoding_api.c
http_request_api.c
http_util_object.c
phpstr/phpstr.c
phpstr/phpstr.h
tests/stream_filters_002.phpt

index 3cabd063f7101d247e8776772b3281318034fd68..46f44ce4ad771ef8895883fdfe8a4fdb86bef61d 100644 (file)
@@ -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);
@@ -354,12 +354,12 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s
        /* 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);
+       s->stream.next_out = (Bytef *) *encoded = emalloc(*encoded_len);
        
        switch (status = deflate(&s->stream, Z_NO_FLUSH))
        {
@@ -399,10 +399,10 @@ PHP_HTTP_API STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s
                *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))
@@ -444,7 +444,7 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s,
        s->stream.avail_in = 0;
        s->stream.next_in = NULL;
        s->stream.avail_out = *encoded_len = 0x800;
-       s->stream.next_out = *encoded = emalloc(*encoded_len);
+       s->stream.next_out = (Bytef *) *encoded = emalloc(*encoded_len);
        
        switch (status = deflate(&s->stream, Z_SYNC_FLUSH))
        {
@@ -470,7 +470,7 @@ PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s,
        s->stream.avail_in = 0;
        s->stream.next_in = NULL;
        s->stream.avail_out = *decoded_len = 0x800;
-       s->stream.next_out = *decoded = emalloc(*decoded_len);
+       s->stream.next_out = (Bytef *) *decoded = emalloc(*decoded_len);
        
        switch (status = inflate(&s->stream, Z_SYNC_FLUSH))
        {
@@ -494,11 +494,11 @@ PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s
        int status;
        
        /* 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.next_out = (Bytef *) *encoded = emalloc(*encoded_len);
        
        do {
                status = deflate(&s->stream, Z_FINISH);
@@ -526,11 +526,11 @@ PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s
        int status;
        
        /* 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.next_out = (Bytef *) *decoded = emalloc(*decoded_len);
        
        if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
                /* cut processed input off */
index 15b101f728367400b8e27774ce40c7ecf87a74de..6090bc15cf62e244a9b6deb55a1049fcbc7bbc90 100644 (file)
@@ -502,7 +502,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                }
        }
 
-#if LIBCURL_VERSIONNUM >= 0x070f01
+#if LIBCURL_VERSION_NUM >= 0x070f01
        /* reset cookies */
        if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
                HTTP_CURL_OPT(COOKIELIST, "ALL");
index 2e1d11b5665eab72e9d18fc1e91a7ef1229438b2..fbc316033cab63c7be0b18783bb2e7c5f8f5928e 100644 (file)
@@ -85,20 +85,9 @@ HTTP_BEGIN_ARGS(chunkedDecode, 1)
        HTTP_ARG_VAL(encoded_string, 0)
 HTTP_END_ARGS;
 
-HTTP_BEGIN_ARGS(gzEncode, 1)
-       HTTP_ARG_VAL(plain, 0)
-       HTTP_ARG_VAL(level, 0)
-       HTTP_ARG_VAL(mtime, 0)
-HTTP_END_ARGS;
-
-HTTP_BEGIN_ARGS(gzDecode, 1)
-       HTTP_ARG_VAL(encoded, 0)
-HTTP_END_ARGS;
-
 HTTP_BEGIN_ARGS(deflate, 1)
        HTTP_ARG_VAL(plain, 0)
-       HTTP_ARG_VAL(level, 0)
-       HTTP_ARG_VAL(zlib_header, 0)
+       HTTP_ARG_VAL(flags, 0)
 HTTP_END_ARGS;
 
 HTTP_BEGIN_ARGS(inflate, 1)
index 8ab8737d0d68e424426eda5ec1b75f12155de096..98a8e63ebcc8bd52b83b1c93235b76a9e11074b1 100644 (file)
@@ -36,6 +36,9 @@ PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t
 
 PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size)
 {
+#if 0
+       fprintf(stderr, "RESIZE: size=%lu, used=%lu, free=%lu\n", buf->size, buf->used, buf->free);
+#endif
        if (buf->free < len) {
                size_t size = override_size ? override_size : buf->size;
                
@@ -63,6 +66,22 @@ PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size
        return 0;
 }
 
+PHPSTR_API size_t phpstr_shrink(phpstr *buf)
+{
+       /* avoid another realloc on fixation */
+       if (buf->free > 1) {
+               char *ptr = perealloc(buf->data, buf->used + 1, buf->pmem);
+               
+               if (ptr) {
+                       buf->data = ptr;
+               } else {
+                       return NOMEM;
+               }
+               buf->free = 1;
+       }
+       return buf->used;
+}
+
 PHPSTR_API size_t phpstr_append(phpstr *buf, const char *append, size_t append_len)
 {
        if (NOMEM == phpstr_resize(buf, append_len)) {
index 565b72b2ae076c6e362435ffbf8751c3e66dc27f..976a795b4368e393e1e94732b5b5ff1d446469fa 100644 (file)
@@ -107,6 +107,9 @@ PHPSTR_API phpstr *phpstr_from_string_ex(phpstr *buf, const char *string, size_t
 #define phpstr_resize(b, s) phpstr_resize_ex((b), (s), 0)
 PHPSTR_API size_t phpstr_resize_ex(phpstr *buf, size_t len, size_t override_size);
 
+/* shrink memory chunk to actually used size (+1) */
+PHPSTR_API size_t phpstr_shrink(phpstr *buf);
+
 /* append data to the phpstr */
 #define phpstr_appends(b, a) phpstr_append((b), (a), sizeof(a)-1)
 #define phpstr_appendl(b, a) phpstr_append((b), (a), strlen(a))
index bd2f81bee9a5f7625ebe2c8228ee1bd7f56283ed..ad44ca01fce5f25e4019af82045d53f406eb159c 100644 (file)
@@ -17,20 +17,26 @@ $n = tempnam(dirname(__FILE__), 'hsf');
 $f = fopen($n, 'wb');
 stream_filter_append($f, 'http.deflate', STREAM_FILTER_WRITE, HTTP_DEFLATE_TYPE_GZIP);
 fwrite($f, $d);
+fflush($f);
+fwrite($f, $d);
 fclose($f);
-var_dump($d == http_inflate(file_get_contents($n)));
+var_dump($d.$d == http_inflate(file_get_contents($n)));
 
 $f = fopen($n, 'wb');
 stream_filter_append($f, 'http.deflate', STREAM_FILTER_WRITE);
 fwrite($f, $d);
+fflush($f);
+fwrite($f, $d);
 fclose($f);
-var_dump($d == http_inflate(file_get_contents($n)));
+var_dump($d.$d == http_inflate(file_get_contents($n)));
 
 $f = fopen($n, 'wb');
 stream_filter_append($f, 'http.deflate', STREAM_FILTER_WRITE, HTTP_DEFLATE_TYPE_RAW);
 fwrite($f, $d);
+fflush($f);
+fwrite($f, $d);
 fclose($f);
-var_dump($d == http_inflate(file_get_contents($n)));
+var_dump($d.$d == http_inflate(file_get_contents($n)));
 
 unlink($n);