xref: /aosp_15_r20/external/mesa3d/src/util/mesa-blake3.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* Copyright © 2023 Valve Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice (including the next
11  * paragraph) shall be included in all copies or substantial portions of the
12  * Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef MESA_BLAKE3_H
24 #define MESA_BLAKE3_H
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include "blake3/blake3.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #define mesa_blake3 blake3_hasher
36 #define BLAKE3_OUT_LEN32 (BLAKE3_OUT_LEN / 4)
37 #define BLAKE3_HEX_LEN (2 * BLAKE3_OUT_LEN + 1)
38 
39 typedef uint8_t blake3_hash[BLAKE3_OUT_LEN];
40 
41 static inline void
_mesa_blake3_init(struct mesa_blake3 * ctx)42 _mesa_blake3_init(struct mesa_blake3 *ctx)
43 {
44   blake3_hasher_init(ctx);
45 }
46 
47 static inline void
_mesa_blake3_update(struct mesa_blake3 * ctx,const void * data,size_t size)48 _mesa_blake3_update(struct mesa_blake3 *ctx, const void *data, size_t size)
49 {
50    blake3_hasher_update(ctx, data, size);
51 }
52 
53 static inline void
_mesa_blake3_final(struct mesa_blake3 * ctx,blake3_hash result)54 _mesa_blake3_final(struct mesa_blake3 *ctx, blake3_hash result)
55 {
56    blake3_hasher_finalize(ctx, result, BLAKE3_OUT_LEN);
57 }
58 
59 void
60 _mesa_blake3_format(char *buf, const unsigned char *blake3);
61 
62 void
63 _mesa_blake3_hex_to_blake3(unsigned char *buf, const char *hex);
64 
65 void
66 _mesa_blake3_compute(const void *data, size_t size, blake3_hash result);
67 
68 void
69 _mesa_blake3_print(FILE *f, const blake3_hash blake3);
70 
71 bool
72 _mesa_printed_blake3_equal(const blake3_hash blake3,
73                            const uint32_t printed_blake3[BLAKE3_OUT_LEN32]);
74 
75 #ifdef __cplusplus
76 } /* extern C */
77 #endif
78 
79 #endif