From: Mark Adler Date: Tue, 10 May 2022 15:11:32 +0000 (-0700) Subject: Correct incorrect inputs provided to the CRC functions. X-Git-Tag: 2.1.0-beta1~236 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=856884022cc7202b159ba76c8e89bb1d9f7300d5;p=thirdparty%2Fzlib-ng.git Correct incorrect inputs provided to the CRC functions. The previous releases of zlib were not sensitive to incorrect CRC inputs with bits set above the low 32. This commit restores that behavior, so that applications with such bugs will continue to operate as before. Co-authored-by: Nathan Moinvaziri --- diff --git a/crc32_braid.c b/crc32_braid.c index 3c3cedffb..fee7992bb 100644 --- a/crc32_braid.c +++ b/crc32_braid.c @@ -112,10 +112,10 @@ static z_word_t crc_word(z_word_t data) { /* ========================================================================= */ Z_INTERNAL uint32_t crc32_braid(uint32_t crc, const unsigned char *buf, uint64_t len) { - Z_REGISTER uint32_t c = crc; + Z_REGISTER uint32_t c; /* Pre-condition the CRC */ - c ^= 0xffffffff; + c = (~crc) & 0xffffffff; #ifdef W /* If provided enough bytes, do a braided CRC calculation. */ diff --git a/crc32_braid_comb.c b/crc32_braid_comb.c index 51ebf9d00..e33fccfd6 100644 --- a/crc32_braid_comb.c +++ b/crc32_braid_comb.c @@ -17,31 +17,22 @@ static uint32_t crc32_combine_(uint32_t crc1, uint32_t crc2, z_off64_t len2) { return multmodp(x2nmodp(len2, 3), crc1) ^ crc2; } +static uint32_t crc32_combine_gen_(z_off64_t len2) { + return x2nmodp(len2, 3); +} +static uint32_t crc32_combine_op_(uint32_t crc1, uint32_t crc2, const uint32_t op) { + return multmodp(op, crc1) ^ crc2; +} /* ========================================================================= */ + #ifdef ZLIB_COMPAT unsigned long Z_EXPORT PREFIX(crc32_combine)(unsigned long crc1, unsigned long crc2, z_off_t len2) { return (unsigned long)crc32_combine_((uint32_t)crc1, (uint32_t)crc2, len2); } - unsigned long Z_EXPORT PREFIX4(crc32_combine)(unsigned long crc1, unsigned long crc2, z_off64_t len2) { return (unsigned long)crc32_combine_((uint32_t)crc1, (uint32_t)crc2, len2); } -#else -uint32_t Z_EXPORT PREFIX4(crc32_combine)(uint32_t crc1, uint32_t crc2, z_off64_t len2) { - return crc32_combine_(crc1, crc2, len2); -} -#endif - -/* ========================================================================= */ - -static uint32_t crc32_combine_gen_(z_off64_t len2) { - return x2nmodp(len2, 3); -} - -/* ========================================================================= */ - -#ifdef ZLIB_COMPAT unsigned long Z_EXPORT PREFIX(crc32_combine_gen)(z_off_t len2) { return crc32_combine_gen_(len2); } @@ -49,14 +40,17 @@ unsigned long Z_EXPORT PREFIX4(crc32_combine_gen)(z_off64_t len2) { return crc32_combine_gen_(len2); } unsigned long Z_EXPORT PREFIX(crc32_combine_op)(unsigned long crc1, unsigned long crc2, const unsigned long op) { - return multmodp(op, crc1) ^ crc2; + return (unsigned long)crc32_combine_op_((uint32_t)crc1, (uint32_t)crc2, (uint32_t)op); } #else +uint32_t Z_EXPORT PREFIX4(crc32_combine)(uint32_t crc1, uint32_t crc2, z_off64_t len2) { + return crc32_combine_(crc1, crc2, len2); +} uint32_t Z_EXPORT PREFIX(crc32_combine_gen)(z_off64_t len2) { return crc32_combine_gen_(len2); } uint32_t Z_EXPORT PREFIX(crc32_combine_op)(uint32_t crc1, uint32_t crc2, const uint32_t op) { - return multmodp(op, crc1) ^ crc2; + return crc32_combine_op_(crc1, crc2, op); } #endif