Skip to content

Commit 0b828b4

Browse files
committed
Write out all of the available bits when using Z_BLOCK.
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.
1 parent 8f5ecee commit 0b828b4

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

deflate.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,19 +638,22 @@ local void putShortMSB (s, b)
638638
local void flush_pending(strm)
639639
z_streamp strm;
640640
{
641-
unsigned len = strm->state->pending;
641+
unsigned len;
642+
deflate_state *s = strm->state;
642643

644+
_tr_flush_bits(s);
645+
len = s->pending;
643646
if (len > strm->avail_out) len = strm->avail_out;
644647
if (len == 0) return;
645648

646-
zmemcpy(strm->next_out, strm->state->pending_out, len);
649+
zmemcpy(strm->next_out, s->pending_out, len);
647650
strm->next_out += len;
648-
strm->state->pending_out += len;
651+
s->pending_out += len;
649652
strm->total_out += len;
650653
strm->avail_out -= len;
651-
strm->state->pending -= len;
652-
if (strm->state->pending == 0) {
653-
strm->state->pending_out = strm->state->pending_buf;
654+
s->pending -= len;
655+
if (s->pending == 0) {
656+
s->pending_out = s->pending_buf;
654657
}
655658
}
656659

deflate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ void ZLIB_INTERNAL _tr_init OF((deflate_state *s));
296296
int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
297297
void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf,
298298
ulg stored_len, int last));
299+
void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s));
299300
void ZLIB_INTERNAL _tr_align OF((deflate_state *s));
300301
void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf,
301302
ulg stored_len, int last));

trees.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,15 @@ void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last)
876876
copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
877877
}
878878

879+
/* ===========================================================================
880+
* Flush the bits in the bit buffer to pending output (leaves at most 7 bits)
881+
*/
882+
void ZLIB_INTERNAL _tr_flush_bits(s)
883+
deflate_state *s;
884+
{
885+
bi_flush(s);
886+
}
887+
879888
/* ===========================================================================
880889
* Send one empty static block to give enough lookahead for inflate.
881890
* This takes 10 bits, of which 7 may remain in the bit buffer.

0 commit comments

Comments
 (0)