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 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 +#include +#include #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