]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Correct incorrect inputs provided to the CRC functions.
authorMark Adler <madler@alumni.caltech.edu>
Tue, 10 May 2022 15:11:32 +0000 (08:11 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 25 May 2022 10:04:35 +0000 (12:04 +0200)
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 <nathan@nathanm.com>
crc32_braid.c
crc32_braid_comb.c

index 3c3cedffbd20b06daf631224baca82a164d9ff9d..fee7992bb5743fa4180d8ad49eb8d8cfd18c9902 100644 (file)
@@ -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. */
index 51ebf9d009850cedbd63fa85732c1f66ef91e014..e33fccfd6d9aff5f2361ef8455f4461c76d851b2 100644 (file)
 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