util/blake3: add _mesa_blake3_from_printed_string

To convert printed blake3 string back to blake3 hash.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32570>
This commit is contained in:
Qiang Yu 2024-12-09 16:08:40 +08:00 committed by Marge Bot
parent 0fd99353a6
commit 679c450184
2 changed files with 30 additions and 2 deletions

View file

@ -53,6 +53,13 @@ blake3_to_uint32(const blake3_hash blake3,
out[i / 4] |= (uint32_t)blake3[i] << ((i % 4) * 8);
}
static void
blake3_from_uint32(blake3_hash blake3, uint32_t in[BLAKE3_OUT_LEN32])
{
for (unsigned i = 0; i < BLAKE3_OUT_LEN; i++)
blake3[i] = (in[i / 4] >> ((i % 4) * 8)) & 0xff;
}
void
_mesa_blake3_print(FILE *f, const blake3_hash blake3)
{
@ -63,6 +70,24 @@ _mesa_blake3_print(FILE *f, const blake3_hash blake3)
}
}
bool
_mesa_blake3_from_printed_string(blake3_hash blake3, const char *printed)
{
unsigned expected_len = BLAKE3_OUT_LEN32 * 12 - 2;
if (strlen(printed) != expected_len)
return false;
uint32_t u32[BLAKE3_OUT_LEN32];
for (unsigned i = 0; i < BLAKE3_OUT_LEN32; i++) {
int ret = sscanf(printed, i == BLAKE3_OUT_LEN32 - 1 ? "0x%08" PRIx32 : "0x%08" PRIx32 ", ", &u32[i]);
if (ret != 1)
return false;
printed += 12;
}
blake3_from_uint32(blake3, u32);
return true;
}
bool
_mesa_printed_blake3_equal(const blake3_hash blake3,
const uint32_t printed_blake3[BLAKE3_OUT_LEN32])
@ -71,4 +96,4 @@ _mesa_printed_blake3_equal(const blake3_hash blake3,
blake3_to_uint32(blake3, u32);
return memcmp(u32, printed_blake3, sizeof(u32)) == 0;
}
}

View file

@ -68,6 +68,9 @@ _mesa_blake3_compute(const void *data, size_t size, blake3_hash result);
void
_mesa_blake3_print(FILE *f, const blake3_hash blake3);
bool
_mesa_blake3_from_printed_string(blake3_hash blake3, const char *printed);
bool
_mesa_printed_blake3_equal(const blake3_hash blake3,
const uint32_t printed_blake3[BLAKE3_OUT_LEN32]);
@ -76,4 +79,4 @@ _mesa_printed_blake3_equal(const blake3_hash blake3,
} /* extern C */
#endif
#endif
#endif