From 977dd748c53a24c01ed23d3be65f6489130795fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Thu, 27 Feb 2025 18:16:01 +0100 Subject: [PATCH] libc/crc32: make fill value of excluded area configurable MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit A new user of crc32_exclude_offset() is going to be added which requires a different fill value than the currently hardcoded "0". Add a parameter where each caller can specify their own value. Signed-off-by: Thomas Weißschuh --- include/crc32.h | 3 ++- lib/crc32.c | 4 ++-- libblkid/src/partitions/gpt.c | 2 +- libblkid/src/superblocks/cramfs.c | 2 +- libblkid/src/superblocks/zonefs.c | 2 +- libfdisk/src/gpt.c | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/crc32.h b/include/crc32.h index 0d34da0d24..c78d291b01 100644 --- a/include/crc32.h +++ b/include/crc32.h @@ -10,7 +10,8 @@ extern uint32_t ul_crc32(uint32_t seed, const unsigned char *buf, size_t len); extern uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t len, - size_t exclude_off, size_t exclude_len); + size_t exclude_off, size_t exclude_len, + uint8_t exclude_fill); #endif diff --git a/lib/crc32.c b/lib/crc32.c index 824693d017..b62b9f6380 100644 --- a/lib/crc32.c +++ b/lib/crc32.c @@ -122,7 +122,7 @@ uint32_t ul_crc32(uint32_t seed, const unsigned char *buf, size_t len) } uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t len, - size_t exclude_off, size_t exclude_len) + size_t exclude_off, size_t exclude_len, uint8_t exclude_fill) { uint32_t crc = seed; const unsigned char *p = buf; @@ -132,7 +132,7 @@ uint32_t ul_crc32_exclude_offset(uint32_t seed, const unsigned char *buf, size_t unsigned char x = *p++; if (i >= exclude_off && i < exclude_off + exclude_len) - x = 0; + x = exclude_fill; crc = crc32_add_char(crc, x); } diff --git a/libblkid/src/partitions/gpt.c b/libblkid/src/partitions/gpt.c index a8846be21f..dc5bcf3aa4 100644 --- a/libblkid/src/partitions/gpt.c +++ b/libblkid/src/partitions/gpt.c @@ -106,7 +106,7 @@ struct gpt_entry { static inline uint32_t count_crc32(const unsigned char *buf, size_t len, size_t exclude_off, size_t exclude_len) { - return (ul_crc32_exclude_offset(~0L, buf, len, exclude_off, exclude_len) ^ ~0L); + return (ul_crc32_exclude_offset(~0L, buf, len, exclude_off, exclude_len, 0) ^ ~0L); } static inline const unsigned char *get_lba_buffer(blkid_probe pr, diff --git a/libblkid/src/superblocks/cramfs.c b/libblkid/src/superblocks/cramfs.c index 6a8731f7bf..d9f3ed19d8 100644 --- a/libblkid/src/superblocks/cramfs.c +++ b/libblkid/src/superblocks/cramfs.c @@ -64,7 +64,7 @@ static int cramfs_verify_csum(blkid_probe pr, const struct blkid_idmag *mag, crc = ~ul_crc32_exclude_offset(~0LL, csummed, csummed_size, offsetof(struct cramfs_super, info.crc), - sizeof_member(struct cramfs_super, info.crc)); + sizeof_member(struct cramfs_super, info.crc), 0); return blkid_probe_verify_csum(pr, crc, expected); } diff --git a/libblkid/src/superblocks/zonefs.c b/libblkid/src/superblocks/zonefs.c index e8fcab34fe..8534e2ffe4 100644 --- a/libblkid/src/superblocks/zonefs.c +++ b/libblkid/src/superblocks/zonefs.c @@ -54,7 +54,7 @@ static int zonefs_verify_csum(blkid_probe pr, const struct zonefs_super *sb) uint32_t expected = le32_to_cpu(sb->s_crc); uint32_t crc = ul_crc32_exclude_offset( ~0LL, (unsigned char *) sb, sizeof(*sb), - offsetof(__typeof__(*sb), s_crc), sizeof(sb->s_crc)); + offsetof(__typeof__(*sb), s_crc), sizeof(sb->s_crc), 0); return blkid_probe_verify_csum(pr, crc, expected); } diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index 374246ce6b..2f850fa8a1 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -1043,7 +1043,7 @@ fail: static inline uint32_t count_crc32(const unsigned char *buf, size_t len, size_t ex_off, size_t ex_len) { - return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len) ^ ~0L); + return (ul_crc32_exclude_offset(~0L, buf, len, ex_off, ex_len, 0) ^ ~0L); } static inline uint32_t gpt_header_count_crc32(struct gpt_header *header) -- 2.47.2