[or-cvs] Allow tor_gzip_uncompress to extract as much as possible fr...

Nick Mathewson nickm at seul.org
Thu Oct 13 22:48:12 UTC 2005


Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv32655/src/common

Modified Files:
	torgzip.c torgzip.h 
Log Message:
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.]

Index: torgzip.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/torgzip.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- torgzip.c	30 Sep 2005 01:09:52 -0000	1.22
+++ torgzip.c	13 Oct 2005 22:48:09 -0000	1.23
@@ -154,7 +154,8 @@
 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 @@
   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 @@
         }
         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);

Index: torgzip.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/torgzip.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- torgzip.h	9 Jun 2005 19:03:31 -0000	1.7
+++ torgzip.h	13 Oct 2005 22:48:09 -0000	1.8
@@ -23,7 +23,8 @@
 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);
 
 int is_gzip_supported(void);
 



More information about the tor-commits mailing list