From: Michael Wallner Date: Fri, 23 Dec 2005 12:42:34 +0000 (+0000) Subject: - add phpstr_shrink() X-Git-Tag: RELEASE_0_21_0~35 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=3017eb87773ddee0b170d532ff92e195f2f69612;p=m6w6%2Fext-http - add phpstr_shrink() - fix some warnings --- diff --git a/http_encoding_api.c b/http_encoding_api.c index 3cabd06..46f44ce 100644 --- a/http_encoding_api.c +++ b/http_encoding_api.c @@ -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 */ diff --git a/http_request_api.c b/http_request_api.c index 15b101f..6090bc1 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -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"); diff --git a/http_util_object.c b/http_util_object.c index 2e1d11b..fbc3160 100644 --- a/http_util_object.c +++ b/http_util_object.c @@ -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) diff --git a/phpstr/phpstr.c b/phpstr/phpstr.c index 8ab8737..98a8e63 100644 --- a/phpstr/phpstr.c +++ b/phpstr/phpstr.c @@ -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)) { diff --git a/phpstr/phpstr.h b/phpstr/phpstr.h index 565b72b..976a795 100644 --- a/phpstr/phpstr.h +++ b/phpstr/phpstr.h @@ -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)) diff --git a/tests/stream_filters_002.phpt b/tests/stream_filters_002.phpt index bd2f81b..ad44ca0 100644 --- a/tests/stream_filters_002.phpt +++ b/tests/stream_filters_002.phpt @@ -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);