Change gzread() and related to ignore junk after gzip streams.
authorMark Adler <madler@alumni.caltech.edu>
Sat, 24 Sep 2011 17:26:07 +0000 (10:26 -0700)
committerMark Adler <madler@alumni.caltech.edu>
Sat, 24 Sep 2011 17:26:07 +0000 (10:26 -0700)
Previously the new gz* functions (introduced in 1.2.4) would read and
return raw data after the last gzip stream.  This is inconsistent with
the behavior of gzip and the previous versions of zlib.  Now when one
or more gzip streams have been decoded from the file, which is then
followed by data that is not a gzip stream (as detemined by not finding
the magic header), then that subsequent trailing garbage is ignored,
and no error is returned.

gzread.c
zlib.h

index 8fc2915..213ad8c 100644 (file)
--- a/gzread.c
+++ b/gzread.c
@@ -207,6 +207,15 @@ local int gz_head(state)
         }
     }
 
+    /* no gzip header -- if we were decoding gzip before, then this is trailing
+       garbage.  Ignore the trailing garbage and finish. */
+    if (state->direct == 0) {
+        strm->avail_in = 0;
+        state->eof = 1;
+        state->have = 0;
+        return 0;
+    }
+
     /* doing raw i/o, save start of raw data for seeking, copy any leftover
        input to output -- this assumes that the output buffer is larger than
        the input buffer, which also assures space for gzungetc() */
diff --git a/zlib.h b/zlib.h
index 8c722a6..4c505b9 100644 (file)
--- a/zlib.h
+++ b/zlib.h
@@ -1259,14 +1259,14 @@ ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
 ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
 /*
      Reads the given number of uncompressed bytes from the compressed file.  If
-   the input file was not in gzip format, gzread copies the given number of
-   bytes into the buffer.
+   the input file is not in gzip format, gzread copies the given number of
+   bytes into the buffer directly from the file.
 
      After reaching the end of a gzip stream in the input, gzread will continue
-   to read, looking for another gzip stream, or failing that, reading the rest
-   of the input file directly without decompression.  The entire input file
-   will be read if gzread is called until it returns less than the requested
-   len.
+   to read, looking for another gzip stream.  Any number of gzip streams may be
+   concatenated in the input file, and will all be decompressed by gzread().
+   If something other than a gzip stream is encountered after a gzip stream,
+   that remaining trailing garbage is ignored (and no error is returned).
 
      gzread returns the number of uncompressed bytes actually read, less than
    len for end of file, or -1 for error.