Skip to content

Commit

Permalink
Use named define instead of magic number for deflate block header ove…
Browse files Browse the repository at this point in the history
…rhead and padding.
  • Loading branch information
Dead2 committed Dec 14, 2021
1 parent 4134223 commit 855c712
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
7 changes: 3 additions & 4 deletions compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ z_size_t Z_EXPORT PREFIX(compressBound)(z_size_t sourceLen) {
return complen + ZLIB_WRAPLEN;

#ifndef NO_QUICK_STRATEGY
/* Quick deflate strategy worse case is 9 bits per literal, rounded to nearest byte,
plus the size of block & zlib headers and footers, and one extra byte for padding
extremely small blocks */
return sourceLen + ((sourceLen + 13 + 7) >> 3) + ZLIB_WRAPLEN + 1;
/* Quick deflate strategy worst case is 9 bits per literal, rounded to nearest byte (+7 bits),
plus the size of deflate block headers, padding and zlib wrapper */
return sourceLen + ((sourceLen + 7) >> 3) + ((DEFLATE_WRAPBITS + DEFLATE_PADBITS) >> 3) + ZLIB_WRAPLEN;
#else
return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN;
#endif
Expand Down
7 changes: 3 additions & 4 deletions deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,9 @@ unsigned long Z_EXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long
return complen + wraplen;

#ifndef NO_QUICK_STRATEGY
/* Quick deflate strategy worst case is 9 bits per literal, rounded to nearest byte,
plus the size of block headers & wrappers, and one extra byte for padding
extremely small blocks */
return sourceLen + ((sourceLen + 13 + 7) >> 3) + wraplen + 1;
/* Quick deflate strategy worst case is 9 bits per literal, rounded to nearest byte (+7 bits),
plus the size of deflate block headers, padding and zlib/gzip wrapper */
return sourceLen + ((sourceLen + 7) >> 3) + ((DEFLATE_WRAPBITS + DEFLATE_PADBITS) >> 3) + wraplen;
#else
return sourceLen + (sourceLen >> 4) + 7 + wraplen;
#endif
Expand Down
6 changes: 4 additions & 2 deletions zutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ extern z_const char * const PREFIX(z_errmsg)[10]; /* indexed by 2-zlib_error */
#define ADLER32_INITIAL_VALUE 1 /* initial adler-32 hash value */
#define CRC32_INITIAL_VALUE 0 /* initial crc-32 hash value */

#define ZLIB_WRAPLEN 6 /* zlib format overhead */
#define GZIP_WRAPLEN 18 /* gzip format overhead */
#define ZLIB_WRAPLEN 6 /* zlib format overhead */
#define GZIP_WRAPLEN 18 /* gzip format overhead */
#define DEFLATE_WRAPBITS 13 /* deflate block overhead: 3 bits for block start + 10 bits for block end */
#define DEFLATE_PADBITS 3 /* padding used to round deflate_wrapbits up to nearest byte */

/* target dependencies */

Expand Down

0 comments on commit 855c712

Please sign in to comment.