]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: Add crc32c function that can deal with holes
authorJeremy Linton <jeremy.linton@arm.com>
Fri, 14 Apr 2023 23:07:29 +0000 (18:07 -0500)
committerJeremy Linton <jeremy.linton@arm.com>
Fri, 14 Apr 2023 23:49:27 +0000 (18:49 -0500)
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 <jeremy.linton@arm.com>
include/crc32c.h
lib/crc32c.c

index 494857f3e2b7969d80fbc3029971690313cd1590..3d27461844dc6fefe1429b9b856750c6e789a477 100644 (file)
@@ -9,5 +9,9 @@
 #include <stdint.h>
 
 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 */
index 49e7543f6b873e68842b037042b0005a1e3fb750..05b14281bbeb6683e4a5b403d05d2f9e8dd1421c 100644 (file)
@@ -10,6 +10,7 @@
  *  code or tables extracted from it, as desired without restriction.
  */
 
+#include <assert.h>
 #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;
+}