| modification, are permitted provided that the conditions mentioned |
| in the accompanying LICENSE file are met. |
+--------------------------------------------------------------------+
- | Copyright (c) 2004-2010, Michael Wallner <mike@php.net> |
+ | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
+--------------------------------------------------------------------+
*/
-/* $Id: http_encoding_api.c 298592 2010-04-26 11:47:29Z mike $ */
-
-#include "php_http.h"
+#include "php_http_api.h"
#include <zlib.h>
const char *e_ptr = encoded;
*decoded_len = 0;
- *decoded = ecalloc(1, encoded_len);
+ *decoded = ecalloc(1, encoded_len + 1);
while ((encoded + encoded_len - e_ptr) > 0) {
ulong chunk_len = 0, rest;
php_http_error(HE_NOTICE, PHP_HTTP_E_ENCODING, "Data does not seem to be chunked encoded");
memcpy(*decoded, encoded, encoded_len);
*decoded_len = encoded_len;
+ decoded[*decoded_len] = '\0';
return encoded + encoded_len;
} else {
efree(*decoded);
fprintf(stderr, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
#endif
status = inflate(Z, flush);
-
- buffer.used += buffer.free - Z->avail_out;
- buffer.free = Z->avail_out;
+ php_http_buffer_account(&buffer, buffer.free - Z->avail_out);
#if 0
fprintf(stderr, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
#endif
status = inflateInit2(&Z, wbits);
if (Z_OK == status) {
Z.next_in = (Bytef *) data;
- Z.avail_in = data_len;
+ Z.avail_in = data_len + 1; /* include the terminating NULL, see #61287 */
switch (status = php_http_inflate_rounds(&Z, Z_NO_FLUSH, decoded, decoded_len)) {
case Z_STREAM_END:
{
TSRMLS_FETCH_FROM_CTX(from->ts);
- if (!from->ops->copy) {
- return NULL;
+ if (from->ops->copy) {
+ int freeme;
+ php_http_encoding_stream_t *ns;
+
+ if ((freeme = !to)) {
+ to = pemalloc(sizeof(*to), (from->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
+ }
+ memset(to, 0, sizeof(*to));
+
+ to->flags = from->flags;
+ to->ops = from->ops;
+ TSRMLS_SET_CTX(to->ts);
+
+ if ((ns = to->ops->copy(from, to))) {
+ return ns;
+ } else {
+ return to;
+ }
+
+ if (freeme) {
+ pefree(to, (to->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
+ }
}
- return from->ops->copy(from, php_http_encoding_stream_init(to, from->ops, from->flags TSRMLS_CC));
+ return NULL;
}
PHP_HTTP_API STATUS php_http_encoding_stream_reset(php_http_encoding_stream_t **s)
static php_http_encoding_stream_t *deflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
{
- z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
-
- deflateCopy(to_ctx, from_ctx);
- php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
+ int status, p = to->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT;
+ z_streamp from_ctx = from->ctx, to_ctx = pecalloc(1, sizeof(*to_ctx), p);
+ TSRMLS_FETCH_FROM_CTX(from->ts);
- return to;
+ if (Z_OK == (status = deflateCopy(to_ctx, from_ctx))) {
+ if ((to_ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
+ php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
+ to->ctx = to_ctx;
+ return to;
+ }
+ deflateEnd(to_ctx);
+ status = Z_MEM_ERROR;
+ }
+ php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to copy deflate encoding stream: %s", zError(status));
+ return NULL;
}
static php_http_encoding_stream_t *inflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
{
- z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
-
- inflateCopy(to_ctx, from_ctx);
- php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
+ int status, p = from->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT;
+ z_streamp from_ctx = from->ctx, to_ctx = pecalloc(1, sizeof(*to_ctx), p);
+ TSRMLS_FETCH_FROM_CTX(from->ts);
- return to;
+ if (Z_OK == (status = inflateCopy(to_ctx, from_ctx))) {
+ if ((to_ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
+ php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
+ to->ctx = to_ctx;
+ return to;
+ }
+ inflateEnd(to_ctx);
+ status = Z_MEM_ERROR;
+ }
+ php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to copy inflate encoding stream: %s", zError(status));
+ return NULL;
}
static php_http_encoding_stream_t *dechunk_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
{
- struct dechunk_ctx *from_ctx = from->ctx, *to_ctx = to->ctx;
-
- to_ctx->hexlen = from_ctx->hexlen;
- to_ctx->zeroed = from_ctx->zeroed;
- php_http_buffer_append(&to_ctx->buffer, PHP_HTTP_BUFFER_VAL(&from_ctx->buffer), PHP_HTTP_BUFFER_LEN(&from_ctx->buffer));
+ int p = from->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT;
+ struct dechunk_ctx *from_ctx = from->ctx, *to_ctx = pemalloc(sizeof(*to_ctx), p);
+ TSRMLS_FETCH_FROM_CTX(from->ts);
- return to;
+ if (php_http_buffer_init_ex(&to_ctx->buffer, PHP_HTTP_BUFFER_DEFAULT_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0)) {
+ to_ctx->hexlen = from_ctx->hexlen;
+ to_ctx->zeroed = from_ctx->zeroed;
+ php_http_buffer_append(&to_ctx->buffer, PHP_HTTP_BUFFER_VAL(&from_ctx->buffer), PHP_HTTP_BUFFER_LEN(&from_ctx->buffer));
+ to->ctx = to_ctx;
+ return to;
+ }
+ pefree(to_ctx, p);
+ php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to copy inflate encoding stream: out of memory");
+ return NULL;
}
static STATUS deflate_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len)
if (SUCCESS == php_http_encoding_stream_finish(obj->stream, &encoded_str, &encoded_len)) {
if (SUCCESS == php_http_encoding_stream_reset(&obj->stream)) {
- RETURN_STRINGL(encoded_str, encoded_len, 0);
+ if (encoded_str) {
+ RETURN_STRINGL(encoded_str, encoded_len, 0);
+ } else {
+ RETURN_EMPTY_STRING();
+ }
} else {
STR_FREE(encoded_str);
}
{
char *str;
int len;
- zval *zlen;
+ zval *zlen = NULL;
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &str, &len, &zlen)) {
const char *end_ptr;