From 7c0b0e660a5c83bc46ac024c22af5ddce87acdc5 Mon Sep 17 00:00:00 2001 From: Saroj Kumar Date: Fri, 29 Mar 2024 16:13:49 +0530 Subject: [PATCH] mesa: Add functions to print blake3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Marek Olšák Reviewed-by: Daniel Schürmann Acked-by: David Heidelberg Part-of: --- src/util/mesa-blake3.c | 33 ++++++++++++++++++++++++++++++++- src/util/mesa-blake3.h | 13 ++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/util/mesa-blake3.c b/src/util/mesa-blake3.c index 83fc459c262..b813a288684 100644 --- a/src/util/mesa-blake3.c +++ b/src/util/mesa-blake3.c @@ -20,8 +20,9 @@ * DEALINGS IN THE SOFTWARE. */ +#include +#include #include "mesa-blake3.h" - #include "hex.h" void _mesa_blake3_format(char *buf, const unsigned char *blake3) @@ -40,4 +41,34 @@ void _mesa_blake3_compute(const void *data, size_t size, blake3_hash result) _mesa_blake3_init(&ctx); _mesa_blake3_update(&ctx, data, size); _mesa_blake3_final(&ctx, result); +} + +static void +blake3_to_uint32(const blake3_hash blake3, + uint32_t out[BLAKE3_OUT_LEN32]) +{ + memset(out, 0, BLAKE3_OUT_LEN); + + for (unsigned i = 0; i < BLAKE3_OUT_LEN; i++) + out[i / 4] |= (uint32_t)blake3[i] << ((i % 4) * 8); +} + +void +_mesa_blake3_print(FILE *f, const blake3_hash blake3) +{ + uint32_t u32[BLAKE3_OUT_LEN32]; + blake3_to_uint32(blake3, u32); + for (unsigned i = 0; i < BLAKE3_OUT_LEN32; i++) { + fprintf(f, i ? ", 0x%08" PRIx32 : "0x%08" PRIx32, u32[i]); + } +} + +bool +_mesa_printed_blake3_equal(const blake3_hash blake3, + const uint32_t printed_blake3[BLAKE3_OUT_LEN32]) +{ + uint32_t u32[BLAKE3_OUT_LEN32]; + blake3_to_uint32(blake3, u32); + + return memcmp(u32, printed_blake3, sizeof(u32)) == 0; } \ No newline at end of file diff --git a/src/util/mesa-blake3.h b/src/util/mesa-blake3.h index a0b53fd29f8..10313a99672 100644 --- a/src/util/mesa-blake3.h +++ b/src/util/mesa-blake3.h @@ -23,6 +23,9 @@ #ifndef MESA_BLAKE3_H #define MESA_BLAKE3_H +#include +#include +#include #include "blake3/blake3.h" #ifdef __cplusplus @@ -30,6 +33,8 @@ extern "C" { #endif #define mesa_blake3 blake3_hasher +#define BLAKE3_OUT_LEN32 (BLAKE3_OUT_LEN / 4) +#define BLAKE3_HEX_LEN (2 * BLAKE3_OUT_LEN + 1) typedef uint8_t blake3_hash[BLAKE3_OUT_LEN]; @@ -60,9 +65,15 @@ _mesa_blake3_hex_to_blake3(unsigned char *buf, const char *hex); void _mesa_blake3_compute(const void *data, size_t size, blake3_hash result); +void +_mesa_blake3_print(FILE *f, const blake3_hash blake3); + +bool +_mesa_printed_blake3_equal(const blake3_hash blake3, + const uint32_t printed_blake3[BLAKE3_OUT_LEN32]); + #ifdef __cplusplus } /* extern C */ #endif - #endif \ No newline at end of file