From e2d4f759137dea60a402a1810a4014a7cf00116b Mon Sep 17 00:00:00 2001 From: Pedro Falcato Date: Thu, 30 Nov 2023 02:46:10 +0000 Subject: MdePkg/BaseLib: Fix CRC16-ANSI calculation REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4609 The current CalculateCrc16Ansi implementation does the following: 1) Invert the passed checksum 2) Calculate the new checksum by going through data and using the lookup table 3) Invert it back again This emulated my design for CalculateCrc32c, where 0 is passed as the initial checksum, and it inverts in the end. However, CRC16 does not invert the checksum on input and output. So this is incorrect. Fix the problem by not inverting input checksums nor output checksums. Callers should now pass CRC16ANSI_INIT as the initial value instead of "0". This is a breaking change. This problem was found out-of-list when older ext4 filesystems (that use crc16 checksums) failed to mount with "corruption". Cc: Liming Gao Cc: Michael D Kinney Cc: Zhiguang Liu Signed-off-by: Pedro Falcato Reviewed-by: Michael D Kinney --- MdePkg/Library/BaseLib/CheckSum.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'MdePkg/Library') diff --git a/MdePkg/Library/BaseLib/CheckSum.c b/MdePkg/Library/BaseLib/CheckSum.c index b6a0765731..57d324c82b 100644 --- a/MdePkg/Library/BaseLib/CheckSum.c +++ b/MdePkg/Library/BaseLib/CheckSum.c @@ -678,13 +678,13 @@ CalculateCrc16Ansi ( Buf = Buffer; - Crc = ~InitialValue; + Crc = InitialValue; while (Length-- != 0) { Crc = mCrc16LookupTable[(Crc & 0xFF) ^ *(Buf++)] ^ (Crc >> 8); } - return ~Crc; + return Crc; } GLOBAL_REMOVE_IF_UNREFERENCED STATIC CONST UINT32 mCrc32cLookupTable[256] = { -- cgit v1.2.3