Skip to content

Commit

Permalink
Write out all of the available bits when using Z_BLOCK.
Browse files Browse the repository at this point in the history
Previously, the bit buffer would hold 1 to 16 bits after "all" of the
output is provided after a Z_BLOCK deflate() call.  Now at most seven
bits remain in the output buffer after Z_BLOCK.  flush_pending() now
flushes the bit buffer before copying out the byte buffer, in order
for it to really flush as much as possible.
  • Loading branch information
madler committed Jan 7, 2012
1 parent 8f5ecee commit 0b828b4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions deflate.c
Expand Up @@ -638,19 +638,22 @@ local void putShortMSB (s, b)
local void flush_pending(strm) local void flush_pending(strm)
z_streamp strm; z_streamp strm;
{ {
unsigned len = strm->state->pending; unsigned len;
deflate_state *s = strm->state;


_tr_flush_bits(s);
len = s->pending;
if (len > strm->avail_out) len = strm->avail_out; if (len > strm->avail_out) len = strm->avail_out;
if (len == 0) return; if (len == 0) return;


zmemcpy(strm->next_out, strm->state->pending_out, len); zmemcpy(strm->next_out, s->pending_out, len);
strm->next_out += len; strm->next_out += len;
strm->state->pending_out += len; s->pending_out += len;
strm->total_out += len; strm->total_out += len;
strm->avail_out -= len; strm->avail_out -= len;
strm->state->pending -= len; s->pending -= len;
if (strm->state->pending == 0) { if (s->pending == 0) {
strm->state->pending_out = strm->state->pending_buf; s->pending_out = s->pending_buf;
} }
} }


Expand Down
1 change: 1 addition & 0 deletions deflate.h
Expand Up @@ -296,6 +296,7 @@ void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
ulg stored_len, int last)); ulg stored_len, int last));
void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
ulg stored_len, int last)); ulg stored_len, int last));
Expand Down
9 changes: 9 additions & 0 deletions trees.c
Expand Up @@ -876,6 +876,15 @@ void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
} }


/* ===========================================================================
* Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
*/
void ZLIB_INTERNAL _tr_flush_bits(s)
deflate_state *s;
{
bi_flush(s);
}

/* =========================================================================== /* ===========================================================================
* Send one empty static block to give enough lookahead for inflate. * Send one empty static block to give enough lookahead for inflate.
* This takes 10 bits, of which 7 may remain in the bit buffer. * This takes 10 bits, of which 7 may remain in the bit buffer.
Expand Down

0 comments on commit 0b828b4

Please sign in to comment.