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:
parent
0b363eb245
commit
474f1f4268
@ -138,6 +138,8 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
unsigned char *buffer = NULL;
|
||||
int error;
|
||||
|
||||
*new_len = 0; /* default, left there if an error occurs */
|
||||
|
||||
memset(&stream, 0, sizeof(bz_stream));
|
||||
stream.next_in = data;
|
||||
stream.avail_in = len;
|
||||
@ -168,7 +170,6 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
|
||||
error = BZ2_bzDecompress(&stream);
|
||||
if (error == BZ_STREAM_END) {
|
||||
*new_len = stream.total_out_lo32;
|
||||
error = BZ_OK;
|
||||
break;
|
||||
}
|
||||
@ -181,13 +182,13 @@ bzip2_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
|
||||
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);
|
||||
*new_len = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -123,6 +123,8 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
unsigned char *buffer = NULL;
|
||||
int error;
|
||||
|
||||
*new_len = 0; /* default, left there if an error occurs */
|
||||
|
||||
if (!len) return NULL;
|
||||
memset(&stream, 0, sizeof(z_stream));
|
||||
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);
|
||||
if (error == Z_STREAM_END) {
|
||||
*new_len = stream.total_out;
|
||||
error = Z_OK;
|
||||
break;
|
||||
}
|
||||
@ -155,13 +156,13 @@ deflate_decode_buffer(unsigned char *data, int len, int *new_len)
|
||||
|
||||
inflateEnd(&stream);
|
||||
|
||||
if (error != Z_OK) {
|
||||
if (error == Z_OK) {
|
||||
*new_len = stream.total_out;
|
||||
return buffer;
|
||||
} else {
|
||||
if (buffer) mem_free(buffer);
|
||||
*new_len = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void
|
||||
|
Loading…
Reference in New Issue
Block a user