Skip to content

Commit

Permalink
Change gzread() and related to ignore junk after gzip streams.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
madler committed Sep 24, 2011
1 parent 8824da8 commit a9ae24b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 9 additions & 0 deletions gzread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() */
Expand Down
12 changes: 6 additions & 6 deletions zlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a9ae24b

Please sign in to comment.