Add deflateUsed() function to get the used bits in the last byte.

This returns the number of used bits in the last byte of a stream
that has just been compressed with deflate.
This commit is contained in:
Mark Adler 2024-07-01 19:24:01 -05:00
parent 534864bccd
commit e011d8c164
17 changed files with 239 additions and 178 deletions

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -1,7 +1,7 @@
LIBRARY LIBRARY
; zlib data compression and ZIP file I/O library ; zlib data compression and ZIP file I/O library
VERSION 1.3 VERSION 1.3
EXPORTS EXPORTS
adler32 @1 adler32 @1
@ -153,6 +153,9 @@ EXPORTS
crc32_z @175 crc32_z @175
; zlib1 v1.2.12 added: ; zlib1 v1.2.12 added:
crc32_combine_gen @176 crc32_combine_gen @176
crc32_combine_gen64 @177 crc32_combine_gen64 @177
crc32_combine_op @178 crc32_combine_op @178
; zlib1 v1.3.2 added:
deflateUsed @179

View File

@ -719,6 +719,14 @@ int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
return Z_OK; return Z_OK;
} }
/* ========================================================================= */
int ZEXPORT deflateUsed(z_streamp strm, int *bits) {
if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
if (bits != Z_NULL)
*bits = strm->state->bi_used;
return Z_OK;
}
/* ========================================================================= */ /* ========================================================================= */
int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) { int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
deflate_state *s; deflate_state *s;

View File

@ -271,6 +271,9 @@ typedef struct internal_state {
/* Number of valid bits in bi_buf. All bits above the last valid bit /* Number of valid bits in bi_buf. All bits above the last valid bit
* are always zero. * are always zero.
*/ */
int bi_used;
/* Last number of used bits when going to a byte boundary.
*/
ulg high_water; ulg high_water;
/* High water mark offset in window for initialized bytes -- bytes above /* High water mark offset in window for initialized bytes -- bytes above

View File

@ -124,4 +124,10 @@ STRPGMEXP PGMLVL(*CURRENT) SIGNATURE('ZLIB')
EXPORT SYMBOL("crc32_combine_gen") EXPORT SYMBOL("crc32_combine_gen")
EXPORT SYMBOL("crc32_combine_op") EXPORT SYMBOL("crc32_combine_op")
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
/* Version 1.3.2 additional entry points. */
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
EXPORT SYMBOL("deflateUsed")
ENDPGMEXP ENDPGMEXP

View File

@ -371,6 +371,10 @@
D pending 10U 0 Pending bytes D pending 10U 0 Pending bytes
D bits 10I 0 Pending bits D bits 10I 0 Pending bits
* *
D deflateUsed PR 10I 0 extproc('deflateUsed') Get used bits
D strm like(z_stream) Compression stream
D bits 10I 0 Used bits
*
D deflatePrime PR 10I 0 extproc('deflatePrime') Change level & strat D deflatePrime PR 10I 0 extproc('deflatePrime') Change level & strat
D strm like(z_stream) Compression stream D strm like(z_stream) Compression stream
D bits 10I 0 value # of bits to insert D bits 10I 0 value # of bits to insert

View File

@ -184,6 +184,7 @@ local void bi_windup(deflate_state *s) {
} else if (s->bi_valid > 0) { } else if (s->bi_valid > 0) {
put_byte(s, (Byte)s->bi_buf); put_byte(s, (Byte)s->bi_buf);
} }
s->bi_used = ((s->bi_valid - 1) & 7) + 1;
s->bi_buf = 0; s->bi_buf = 0;
s->bi_valid = 0; s->bi_valid = 0;
#ifdef ZLIB_DEBUG #ifdef ZLIB_DEBUG
@ -466,6 +467,7 @@ void ZLIB_INTERNAL _tr_init(deflate_state *s) {
s->bi_buf = 0; s->bi_buf = 0;
s->bi_valid = 0; s->bi_valid = 0;
s->bi_used = 0;
#ifdef ZLIB_DEBUG #ifdef ZLIB_DEBUG
s->compressed_len = 0L; s->compressed_len = 0L;
s->bits_sent = 0L; s->bits_sent = 0L;

View File

@ -15,6 +15,7 @@ EXPORTS
deflateTune deflateTune
deflateBound deflateBound
deflatePending deflatePending
deflateUsed
deflatePrime deflatePrime
deflateSetHeader deflateSetHeader
inflateSetDictionary inflateSetDictionary

View File

@ -59,6 +59,7 @@
# define deflateSetDictionary z_deflateSetDictionary # define deflateSetDictionary z_deflateSetDictionary
# define deflateSetHeader z_deflateSetHeader # define deflateSetHeader z_deflateSetHeader
# define deflateTune z_deflateTune # define deflateTune z_deflateTune
# define deflateUsed z_deflateUsed
# define deflate_copyright z_deflate_copyright # define deflate_copyright z_deflate_copyright
# define get_crc_table z_get_crc_table # define get_crc_table z_get_crc_table
# ifndef Z_SOLO # ifndef Z_SOLO

View File

@ -61,6 +61,7 @@
# define deflateSetDictionary z_deflateSetDictionary # define deflateSetDictionary z_deflateSetDictionary
# define deflateSetHeader z_deflateSetHeader # define deflateSetHeader z_deflateSetHeader
# define deflateTune z_deflateTune # define deflateTune z_deflateTune
# define deflateUsed z_deflateUsed
# define deflate_copyright z_deflate_copyright # define deflate_copyright z_deflate_copyright
# define get_crc_table z_get_crc_table # define get_crc_table z_get_crc_table
# ifndef Z_SOLO # ifndef Z_SOLO

View File

@ -59,6 +59,7 @@
# define deflateSetDictionary z_deflateSetDictionary # define deflateSetDictionary z_deflateSetDictionary
# define deflateSetHeader z_deflateSetHeader # define deflateSetHeader z_deflateSetHeader
# define deflateTune z_deflateTune # define deflateTune z_deflateTune
# define deflateUsed z_deflateUsed
# define deflate_copyright z_deflate_copyright # define deflate_copyright z_deflate_copyright
# define get_crc_table z_get_crc_table # define get_crc_table z_get_crc_table
# ifndef Z_SOLO # ifndef Z_SOLO

12
zlib.h
View File

@ -791,6 +791,18 @@ ZEXTERN int ZEXPORT deflatePending(z_streamp strm,
stream state was inconsistent. stream state was inconsistent.
*/ */
ZEXTERN int ZEXPORT deflateUsed(z_streamp strm,
int *bits);
/*
deflateUsed() returns in *bits the most recent number of deflate bits used
in the last byte when flushing to a byte boundary. The result is in 1..8, or
0 if there has not yet been a flush. This helps determine the location of
the last bit of a deflate stream.
deflateUsed returns Z_OK if success, or Z_STREAM_ERROR if the source
stream state was inconsistent.
*/
ZEXTERN int ZEXPORT deflatePrime(z_streamp strm, ZEXTERN int ZEXPORT deflatePrime(z_streamp strm,
int bits, int bits,
int value); int value);

View File

@ -98,3 +98,7 @@ ZLIB_1.2.12 {
crc32_combine_gen64; crc32_combine_gen64;
crc32_combine_op; crc32_combine_op;
} ZLIB_1.2.9; } ZLIB_1.2.9;
ZLIB_1.3.2 {
deflateUsed;
} ZLIB_1.2.12;