Skip to content

Commit

Permalink
Avoid adding empty gzip member after gzflush with Z_FINISH.
Browse files Browse the repository at this point in the history
  • Loading branch information
madler authored and Dead2 committed Oct 22, 2019
1 parent fee07f2 commit 72c9ed1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions gzguts.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ typedef struct {
/* just for writing */
int level; /* compression level */
int strategy; /* compression strategy */
int reset; /* true if a reset is pending after a Z_FINISH */
/* seek request */
z_off64_t skip; /* amount to skip (already rewound if backwards) */
int seek; /* true if seek request pending */
Expand Down
2 changes: 2 additions & 0 deletions gzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static void gz_reset(gz_state *state) {
state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */
}
else /* for writing ... */
state->reset = 0; /* no deflateReset pending */
state->seek = 0; /* no seek request pending */
gz_error(state, Z_OK, NULL); /* clear error */
state->x.pos = 0; /* no uncompressed data yet */
Expand Down
12 changes: 10 additions & 2 deletions gzwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ static int gz_comp(gz_state *state, int flush) {
return 0;
}

/* check for a pending reset */
if (state->reset) {
/* don't start a new gzip member unless there is data to write */
if (strm->avail_in == 0)
return 0;
PREFIX(deflateReset)(strm);
state->reset = 0;
}

/* run deflate() on provided input until it produces no more output */
ret = Z_OK;
do {
Expand Down Expand Up @@ -122,8 +131,7 @@ static int gz_comp(gz_state *state, int flush) {

/* if that completed a deflate stream, allow another to start */
if (flush == Z_FINISH)
PREFIX(deflateReset)(strm);

state->reset = 1;
/* all done, no errors */
return 0;
}
Expand Down

0 comments on commit 72c9ed1

Please sign in to comment.