diff options
author | Nick Mathewson <nickm@torproject.org> | 2005-10-13 22:48:09 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2005-10-13 22:48:09 +0000 |
commit | 11b76b9ca5b39eeeb4fcff1593db2efe14cc5827 (patch) | |
tree | 6e35d52ca6f89c1c482a836f6064449781020bc3 /src/common/torgzip.c | |
parent | 8808b262068b6998a5be9dd6eabf50be82efe8a2 (diff) | |
download | tor-11b76b9ca5b39eeeb4fcff1593db2efe14cc5827.tar tor-11b76b9ca5b39eeeb4fcff1593db2efe14cc5827.tar.gz |
Allow tor_gzip_uncompress to extract as much as possible from truncated compressed data. Also, fix a bug where truncated compressed data could break tor_gzip_uncompress. [This last part is a backport candidate.]
svn:r5247
Diffstat (limited to 'src/common/torgzip.c')
-rw-r--r-- | src/common/torgzip.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/common/torgzip.c b/src/common/torgzip.c index fea803253..8ea35d75e 100644 --- a/src/common/torgzip.c +++ b/src/common/torgzip.c @@ -154,7 +154,8 @@ tor_gzip_compress(char **out, size_t *out_len, int tor_gzip_uncompress(char **out, size_t *out_len, const char *in, size_t in_len, - compress_method_t method) + compress_method_t method, + int complete_only) { struct z_stream_s *stream = NULL; size_t out_size; @@ -195,11 +196,12 @@ tor_gzip_uncompress(char **out, size_t *out_len, stream->avail_out = out_size; while (1) { - switch (inflate(stream, Z_FINISH)) + switch (inflate(stream, complete_only ? Z_FINISH : Z_SYNC_FLUSH)) { case Z_STREAM_END: if (stream->avail_in == 0) goto done; + /* There may be more compressed data here. */ if (inflateInit2(stream, method_bits(method)) != Z_OK) { log_fn(LOG_WARN, "Error from inflateInit2: %s", stream->msg?stream->msg:"<no message>"); @@ -207,10 +209,16 @@ tor_gzip_uncompress(char **out, size_t *out_len, } break; case Z_OK: + if (!complete_only && stream->avail_in == 0) + goto done; /* In case zlib doesn't work as I think.... */ if (stream->avail_out >= stream->avail_in+16) break; case Z_BUF_ERROR: + if (stream->avail_out > 0) { + log_fn(LOG_WARN, "possible truncated or corrupt zlib data"); + goto err; + } offset = stream->next_out - (unsigned char*)*out; out_size *= 2; *out = tor_realloc(*out, out_size); |