From: Thomas Weißschuh Date: Sat, 19 Nov 2022 00:55:40 +0000 (+0100) Subject: libblkid: add function blkid_probe_verify_csum_buf X-Git-Tag: v2.39-rc1~410^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8c4084703dffd911e10b3b1ee2c2e60998bfc697;p=thirdparty%2Futil-linux.git libblkid: add function blkid_probe_verify_csum_buf This function is more convenient for checksums with longer digests. --- diff --git a/libblkid/src/blkidP.h b/libblkid/src/blkidP.h index 78b1cd1da6..43ceb9c981 100644 --- a/libblkid/src/blkidP.h +++ b/libblkid/src/blkidP.h @@ -517,6 +517,8 @@ extern int blkid_probe_set_magic(blkid_probe pr, uint64_t offset, extern int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected) __attribute__((nonnull)); +extern int blkid_probe_verify_csum_buf(blkid_probe pr, size_t n, const void *csum, + const void *expected) __attribute__((nonnull)); extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len) __attribute__((nonnull)); diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c index f54c417785..203f0ae498 100644 --- a/libblkid/src/probe.c +++ b/libblkid/src/probe.c @@ -1780,16 +1780,34 @@ int blkid_probe_set_magic(blkid_probe pr, uint64_t offset, return rc; } -int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected) +static void blkid_probe_log_csum_mismatch(blkid_probe pr, size_t n, const void *csum, + const void *expected) { - if (csum != expected) { + char csum_hex[256]; + char expected_hex[sizeof(csum_hex)]; + int hex_size = min(sizeof(csum_hex), n * 2); + + for (int i = 0; i < hex_size; i+=2) { + sprintf(&csum_hex[i], "%02X", ((const unsigned char *) csum)[i / 2]); + sprintf(&expected_hex[i], "%02X", ((const unsigned char *) expected)[i / 2]); + } + + ul_debug( + "incorrect checksum for type %s," + " got %*s, expected %*s", + blkid_probe_get_probername(pr), + hex_size, csum_hex, hex_size, expected_hex); +} + + +int blkid_probe_verify_csum_buf(blkid_probe pr, size_t n, const void *csum, + const void *expected) +{ + if (memcmp(csum, expected, n) != 0) { struct blkid_chain *chn = blkid_probe_get_chain(pr); - DBG(LOWPROBE, ul_debug( - "incorrect checksum for type %s," - " got %"PRIX64", expected %"PRIX64"", - blkid_probe_get_probername(pr), - csum, expected)); + ON_DBG(LOWPROBE, blkid_probe_log_csum_mismatch(pr, n, csum, expected)); + /* * Accept bad checksum if BLKID_SUBLKS_BADCSUM flags is set */ @@ -1805,6 +1823,11 @@ accept: return 1; } +int blkid_probe_verify_csum(blkid_probe pr, uint64_t csum, uint64_t expected) +{ + return blkid_probe_verify_csum_buf(pr, sizeof(csum), &csum, &expected); +} + /** * blkid_probe_get_devno: * @pr: probe