Mesa (main): util: add SHA1 printing and comparison functions

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Nov 26 13:07:22 UTC 2021


Module: Mesa
Branch: main
Commit: ba8075031a2ffbea9ec5df0338c7703615764134
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=ba8075031a2ffbea9ec5df0338c7703615764134

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Thu Nov 18 17:19:39 2021 -0500

util: add SHA1 printing and comparison functions

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13869>

---

 src/util/mesa-sha1.c | 34 ++++++++++++++++++++++++++++++++++
 src/util/mesa-sha1.h | 10 ++++++++++
 2 files changed, 44 insertions(+)

diff --git a/src/util/mesa-sha1.c b/src/util/mesa-sha1.c
index 410e82c7104..701ba8a28b6 100644
--- a/src/util/mesa-sha1.c
+++ b/src/util/mesa-sha1.c
@@ -26,6 +26,7 @@
 
 #include "sha1/sha1.h"
 #include "mesa-sha1.h"
+#include <string.h>
 
 void
 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
@@ -64,3 +65,36 @@ _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex)
       buf[i] = strtol(tmp, NULL, 16);
    }
 }
+
+static void
+sha1_to_uint32(const uint8_t sha1[SHA1_DIGEST_LENGTH],
+               uint32_t out[SHA1_DIGEST_LENGTH32])
+{
+   memset(out, 0, SHA1_DIGEST_LENGTH);
+
+   for (unsigned i = 0; i < SHA1_DIGEST_LENGTH; i++)
+      out[i / 4] |= (uint32_t)sha1[i] << ((i % 4) * 8);
+}
+
+void
+_mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH])
+{
+   uint32_t u32[SHA1_DIGEST_LENGTH];
+   sha1_to_uint32(sha1, u32);
+
+   for (unsigned i = 0; i < SHA1_DIGEST_LENGTH32; i++) {
+      fprintf(f, "0x%08x", u32[i]);
+      if (i < SHA1_DIGEST_LENGTH32 - 1)
+         fprintf(f, ", ");
+   }
+}
+
+bool
+_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],
+                         const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32])
+{
+   uint32_t u32[SHA1_DIGEST_LENGTH32];
+   sha1_to_uint32(sha1, u32);
+
+   return memcmp(u32, printed_sha1, sizeof(u32)) == 0;
+}
diff --git a/src/util/mesa-sha1.h b/src/util/mesa-sha1.h
index 9842bbb1e2d..9d174fd9b99 100644
--- a/src/util/mesa-sha1.h
+++ b/src/util/mesa-sha1.h
@@ -24,6 +24,8 @@
 #define MESA_SHA1_H
 
 #include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
 #include "c99_compat.h"
 #include "sha1/sha1.h"
 
@@ -32,6 +34,7 @@ extern "C" {
 #endif
 
 #define mesa_sha1 _SHA1_CTX
+#define SHA1_DIGEST_LENGTH32 (SHA1_DIGEST_LENGTH / 4)
 
 static inline void
 _mesa_sha1_init(struct mesa_sha1 *ctx)
@@ -60,6 +63,13 @@ _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex);
 void
 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20]);
 
+void
+_mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH]);
+
+bool
+_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],
+                         const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32]);
+
 #ifdef __cplusplus
 } /* extern C */
 #endif



More information about the mesa-commit mailing list