]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: add function blkid_probe_verify_csum_buf
authorThomas Weißschuh <thomas@t-8ch.de>
Sat, 19 Nov 2022 00:55:40 +0000 (01:55 +0100)
committerThomas Weißschuh <thomas@t-8ch.de>
Mon, 21 Nov 2022 14:29:58 +0000 (15:29 +0100)
This function is more convenient for checksums with longer digests.

libblkid/src/blkidP.h
libblkid/src/probe.c

index 78b1cd1da6d29a9f9f2ca13a50e4a3e026f4e275..43ceb9c9811de4cf0199ec62ad4c52a1597676b0 100644 (file)
@@ -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));
index f54c4177858daff4a39e8eb054775a1d82fc70e7..203f0ae49890eae6b215e746dff9416b97a4af69 100644 (file)
@@ -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