Skip to content

Commit

Permalink
IBM Z: Adjust compressBound() for DFLTCC
Browse files Browse the repository at this point in the history
When DFLTCC was introduced, deflateBound() was adjusted, but
compressBound() was not, leading to compression failures when using
compressBound() + compress() with poorly compressible data.
  • Loading branch information
iii-i authored and Dead2 committed Oct 13, 2021
1 parent 33aacca commit 63e0002
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
24 changes: 24 additions & 0 deletions arch/s390/dfltcc_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,28 @@ void Z_INTERNAL dfltcc_free_window(PREFIX3(streamp) strm, void *w);

#define TRY_FREE_WINDOW dfltcc_free_window

#define DFLTCC_BLOCK_HEADER_BITS 3
#define DFLTCC_HLITS_COUNT_BITS 5
#define DFLTCC_HDISTS_COUNT_BITS 5
#define DFLTCC_HCLENS_COUNT_BITS 4
#define DFLTCC_MAX_HCLENS 19
#define DFLTCC_HCLEN_BITS 3
#define DFLTCC_MAX_HLITS 286
#define DFLTCC_MAX_HDISTS 30
#define DFLTCC_MAX_HLIT_HDIST_BITS 7
#define DFLTCC_MAX_SYMBOL_BITS 16
#define DFLTCC_MAX_EOBS_BITS 15
#define DFLTCC_MAX_PADDING_BITS 7

#define DEFLATE_BOUND_COMPLEN(source_len) \
((DFLTCC_BLOCK_HEADER_BITS + \
DFLTCC_HLITS_COUNT_BITS + \
DFLTCC_HDISTS_COUNT_BITS + \
DFLTCC_HCLENS_COUNT_BITS + \
DFLTCC_MAX_HCLENS * DFLTCC_HCLEN_BITS + \
(DFLTCC_MAX_HLITS + DFLTCC_MAX_HDISTS) * DFLTCC_MAX_HLIT_HDIST_BITS + \
(source_len) * DFLTCC_MAX_SYMBOL_BITS + \
DFLTCC_MAX_EOBS_BITS + \
DFLTCC_MAX_PADDING_BITS) >> 3)

#endif
3 changes: 1 addition & 2 deletions arch/s390/dfltcc_deflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ int Z_INTERNAL dfltcc_deflate_get_dictionary(PREFIX3(streamp) strm, unsigned cha
#define DEFLATE_BOUND_ADJUST_COMPLEN(strm, complen, source_len) \
do { \
if (dfltcc_can_deflate((strm))) \
(complen) = (3 + 5 + 5 + 4 + 19 * 3 + (286 + 30) * 7 + \
(source_len) * 16 + 15 + 7) >> 3; \
(complen) = DEFLATE_BOUND_COMPLEN(source_len); \
} while (0)

#define DEFLATE_NEED_CONSERVATIVE_BOUND(strm) (dfltcc_can_deflate((strm)))
Expand Down
19 changes: 18 additions & 1 deletion compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
* For conditions of distribution and use, see copyright notice in zlib.h
*/

#define ZLIB_INTERNAL
#include "zbuild.h"
#include "zutil.h"
#if defined(ZLIB_COMPAT)
# include "zlib.h"
#else
# include "zlib-ng.h"
#endif

/* ===========================================================================
* Architecture-specific hooks.
*/
#ifdef S390_DFLTCC_DEFLATE
# include "arch/s390/dfltcc_common.h"
#else
/* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings.
* Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */
# define DEFLATE_BOUND_COMPLEN(source_len) 0
#endif

/* ===========================================================================
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
Expand Down Expand Up @@ -73,6 +84,12 @@ int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_size_t *destLen, const unsi
this function needs to be updated.
*/
z_size_t Z_EXPORT PREFIX(compressBound)(z_size_t sourceLen) {
z_size_t complen = DEFLATE_BOUND_COMPLEN(sourceLen);

if (complen > 0)
/* Architecture-specific code provided an upper bound. */
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 & gzip headers and footers */
Expand Down
2 changes: 2 additions & 0 deletions zutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ 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 */

/* target dependencies */

#ifdef AMIGA
Expand Down

0 comments on commit 63e0002

Please sign in to comment.