From: Jeremy Linton Date: Fri, 14 Apr 2023 23:07:29 +0000 (-0500) Subject: lib: Add crc32c function that can deal with holes X-Git-Tag: v2.39-rc3~26^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2598e25fb68ff37182fdb026b52055f177ceb24d;p=thirdparty%2Futil-linux.git 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 --- 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; +}