From 21054319fb17378e25e4f58163fc539c07e6bbac Mon Sep 17 00:00:00 2001 From: tb Date: Sun, 8 May 2022 14:07:54 +0000 Subject: [PATCH] Backport an upstream fix for CRC calculation. This fixes Java applications on some older hardware, see https://github.com/madler/zlib/issues/613 Pointed out by tj and sthen commit ec3df00224d4b396e2ac6586ab5d25f673caa4c2 Author: Mark Adler Date: Wed Mar 30 11:14:53 2022 -0700 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. --- sys/lib/libz/crc32.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sys/lib/libz/crc32.c b/sys/lib/libz/crc32.c index a1129e87521..df94f8e73d2 100644 --- a/sys/lib/libz/crc32.c +++ b/sys/lib/libz/crc32.c @@ -1,4 +1,4 @@ -/* $OpenBSD: crc32.c,v 1.14 2022/05/08 14:05:29 tb Exp $ */ +/* $OpenBSD: crc32.c,v 1.15 2022/05/08 14:07:54 tb Exp $ */ /* crc32.c -- compute the CRC-32 of a data stream * Copyright (C) 1995-2022 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h @@ -629,7 +629,7 @@ unsigned long ZEXPORT crc32_z(crc, buf, len) #endif /* DYNAMIC_CRC_TABLE */ /* Pre-condition the CRC */ - crc ^= 0xffffffff; + crc = (~crc) & 0xffffffff; /* Compute the CRC up to a word boundary. */ while (len && ((z_size_t)buf & 7) != 0) { @@ -748,7 +748,7 @@ unsigned long ZEXPORT crc32_z(crc, buf, len) #endif /* DYNAMIC_CRC_TABLE */ /* Pre-condition the CRC */ - crc ^= 0xffffffff; + crc = (~crc) & 0xffffffff; #ifdef W @@ -1076,7 +1076,7 @@ uLong ZEXPORT crc32_combine64(crc1, crc2, len2) #ifdef DYNAMIC_CRC_TABLE once(&made, make_crc_table); #endif /* DYNAMIC_CRC_TABLE */ - return multmodp(x2nmodp(len2, 3), crc1) ^ crc2; + return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff); } /* ========================================================================= */ @@ -1111,5 +1111,5 @@ uLong crc32_combine_op(crc1, crc2, op) uLong crc2; uLong op; { - return multmodp(op, crc1) ^ crc2; + return multmodp(op, crc1) ^ (crc2 & 0xffffffff); } -- 2.20.1