From 2598e25fb68ff37182fdb026b52055f177ceb24d Mon Sep 17 00:00:00 2001 From: Jeremy Linton Date: Fri, 14 Apr 2023 18:07:29 -0500 Subject: [PATCH] lib: Add crc32c function that can deal with holes XFS, and possibly other filesystems expect that the CRC field is excluded (or rather RAZ) during the CRC operation. Lets create a generic helper that is similar to the CRC32 version ul_crc32_exclude_offset() which computes the CRC while replacing exclude_len bytes of exclude_off with zeros. Signed-off-by: Jeremy Linton --- include/crc32c.h | 4 ++++ lib/crc32c.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/crc32c.h b/include/crc32c.h index 494857f3e2..3d27461844 100644 --- a/include/crc32c.h +++ b/include/crc32c.h @@ -9,5 +9,9 @@ #include extern uint32_t crc32c(uint32_t crc, const void *buf, size_t size); +extern uint32_t ul_crc32c_exclude_offset(uint32_t crc, const unsigned char *buf, + size_t size, size_t exclude_off, + size_t exclude_len); + #endif /* UL_CRC32C_H */ diff --git a/lib/crc32c.c b/lib/crc32c.c index 49e7543f6b..05b14281bb 100644 --- a/lib/crc32c.c +++ b/lib/crc32c.c @@ -10,6 +10,7 @@ * code or tables extracted from it, as desired without restriction. */ +#include #include "crc32c.h" static const uint32_t crc32Table[256] = { @@ -100,3 +101,20 @@ crc32c(uint32_t crc, const void *buf, size_t size) return crc; } + +uint32_t +ul_crc32c_exclude_offset(uint32_t crc, const unsigned char *buf, size_t size, + size_t exclude_off, size_t exclude_len) +{ + size_t i; + assert((exclude_off + exclude_len) < size); + + crc = crc32c(crc, buf, exclude_off); + for (i = 0; i < exclude_len; i++) { + uint8_t zero = 0; + crc = crc32c(crc, &zero, 1); + } + crc = crc32c(crc, &buf[exclude_off + exclude_len], + size - (exclude_off + exclude_len)); + return crc; +} -- 2.47.2