mesa: Add functions to print blake3
Reviewed-by: Marek Olšák <marek.olsak@amd.com> Reviewed-by: Daniel Schürmann <daniel@schuermann.dev> Acked-by: David Heidelberg <david.heidelberg@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/28156>
This commit is contained in:
+32
-1
@@ -20,8 +20,9 @@
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#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;
|
||||
}
|
||||
+12
-1
@@ -23,6 +23,9 @@
|
||||
#ifndef MESA_BLAKE3_H
|
||||
#define MESA_BLAKE3_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
||||
Reference in New Issue
Block a user