1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

encoding: Set *new_len even if data was truncated.

Previously, bzip2_decode_buffer and deflate_decode_buffer left
*new_len unchanged if the compressed input data ended unexpectedly.
This behaviour was also inherited by decode_encoded_buffer,
whose only caller render_encoded_document preinitializes the variable
and so did not crash.

With this change, the functions now store in *new_len the number of
bytes that were successfully decoded, even if more bytes were expected.
An error should perhaps be reported to the user, but I don't think the
previous version did that either, as it returned a non-NULL pointer.
This commit is contained in:
Kalle Olavi Niemitalo 2008-02-17 18:55:41 +02:00 committed by Kalle Olavi Niemitalo
parent 0b363eb245
commit 474f1f4268
2 changed files with 12 additions and 10 deletions

View File

@ -138,6 +138,8 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
unsigned char *buffer = NULL; unsigned char *buffer = NULL;
int error; int error;
*new_len = 0; /* default, left there if an error occurs */
memset(&stream, 0, sizeof(bz_stream)); memset(&stream, 0, sizeof(bz_stream));
stream.next_in = data; stream.next_in = data;
stream.avail_in = len; stream.avail_in = len;
@ -168,7 +170,6 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
error = BZ2_bzDecompress(&stream); error = BZ2_bzDecompress(&stream);
if (error == BZ_STREAM_END) { if (error == BZ_STREAM_END) {
*new_len = stream.total_out_lo32;
error = BZ_OK; error = BZ_OK;
break; break;
} }
@ -181,13 +182,13 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
BZ2_bzDecompressEnd(&stream); BZ2_bzDecompressEnd(&stream);
if (error != BZ_OK) { if (error == BZ_OK) {
*new_len = stream.total_out_lo32;
return buffer;
} else {
if (buffer) mem_free(buffer); if (buffer) mem_free(buffer);
*new_len = 0;
return NULL; return NULL;
} }
return buffer;
} }
static void static void

View File

@ -123,6 +123,8 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len)
unsigned char *buffer = NULL; unsigned char *buffer = NULL;
int error; int error;
*new_len = 0; /* default, left there if an error occurs */
if (!len) return NULL; if (!len) return NULL;
memset(&stream, 0, sizeof(z_stream)); memset(&stream, 0, sizeof(z_stream));
stream.next_in = data; stream.next_in = data;
@ -147,7 +149,6 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len)
error = inflate(&stream, Z_SYNC_FLUSH); error = inflate(&stream, Z_SYNC_FLUSH);
if (error == Z_STREAM_END) { if (error == Z_STREAM_END) {
*new_len = stream.total_out;
error = Z_OK; error = Z_OK;
break; break;
} }
@ -155,13 +156,13 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len)
inflateEnd(&stream); inflateEnd(&stream);
if (error != Z_OK) { if (error == Z_OK) {
*new_len = stream.total_out;
return buffer;
} else {
if (buffer) mem_free(buffer); if (buffer) mem_free(buffer);
*new_len = 0;
return NULL; return NULL;
} }
return buffer;
} }
static void static void