Skip to content

Commit

Permalink
Allow deflatePrime() to insert bits in the middle of a stream.
Browse files Browse the repository at this point in the history
This allows the insertion of multiple empty static blocks for the
purpose of efficiently bringing a stream to a byte boundary.
  • Loading branch information
madler committed Jan 7, 2012
1 parent 19761b8 commit 263b1a0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
22 changes: 18 additions & 4 deletions deflate.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* deflate.c -- compress data using the deflation algorithm
* Copyright (C) 1995-2011 Jean-loup Gailly and Mark Adler
* Copyright (C) 1995-2012 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/

Expand Down Expand Up @@ -52,7 +52,7 @@
#include "deflate.h"

const char deflate_copyright[] =
" deflate 1.2.5.3 Copyright 1995-2011 Jean-loup Gailly and Mark Adler ";
" deflate 1.2.5.3 Copyright 1995-2012 Jean-loup Gailly and Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
Expand Down Expand Up @@ -464,9 +464,23 @@ int ZEXPORT deflatePrime (strm, bits, value)
int bits;
int value;
{
deflate_state *s;
int put;

if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
strm->state->bi_valid = bits;
strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
s = strm->state;
if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3))
return Z_BUF_ERROR;
do {
put = Buf_size - s->bi_valid;
if (put > bits)
put = bits;
s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
s->bi_valid += put;
_tr_flush_bits(s);
value >>= put;
bits -= put;
} while (bits);
return Z_OK;
}

Expand Down
5 changes: 4 additions & 1 deletion deflate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* deflate.h -- internal compression state
* Copyright (C) 1995-2010 Jean-loup Gailly
* Copyright (C) 1995-2012 Jean-loup Gailly
* For conditions of distribution and use, see copyright notice in zlib.h
*/

Expand Down Expand Up @@ -48,6 +48,9 @@
#define MAX_BITS 15
/* All codes must not exceed MAX_BITS bits */

#define Buf_size 16
/* size of bit buffer in bi_buf */

#define INIT_STATE 42
#define EXTRA_STATE 69
#define NAME_STATE 73
Expand Down
7 changes: 1 addition & 6 deletions trees.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* trees.c -- output deflated data using Huffman coding
* Copyright (C) 1995-2010 Jean-loup Gailly
* Copyright (C) 1995-2012 Jean-loup Gailly
* detect_data_type() function provided freely by Cosmin Truta, 2006
* For conditions of distribution and use, see copyright notice in zlib.h
*/
Expand Down Expand Up @@ -74,11 +74,6 @@ local const uch bl_order[BL_CODES]
* probability, to avoid transmitting the lengths for unused bit length codes.
*/

#define Buf_size (8 * 2*sizeof(char))
/* Number of bits used within bi_buf. (bi_buf might be implemented on
* more than 16 bits on some systems.)
*/

/* ===========================================================================
* Local data. These are initialized only once.
*/
Expand Down
5 changes: 3 additions & 2 deletions zlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,9 @@ ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
than or equal to 16, and that many of the least significant bits of value
will be inserted in the output.
deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
stream state was inconsistent.
deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough
room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the
source stream state was inconsistent.
*/

ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
Expand Down

0 comments on commit 263b1a0

Please sign in to comment.