From 13332145c2c0e2928149461439b2f45806138921 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Mon, 25 May 2026 17:21:51 +0100 Subject: [PATCH] fx: add functions for hashing wstr and generic buffers --- fx/hash/hash.c | 31 +++++++++++++++++++++++++++++-- fx/include/fx/hash.h | 8 +++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/fx/hash/hash.c b/fx/hash/hash.c index f79fe52..3ffbc72 100644 --- a/fx/hash/hash.c +++ b/fx/hash/hash.c @@ -40,8 +40,8 @@ static const struct fx_hash_function_ops *hash_functions[] = { [FX_HASH_SHAKE128] = &z__fx_shake128_ops, [FX_HASH_SHAKE256] = &z__fx_shake256_ops, }; -static const size_t nr_hash_functions - = sizeof hash_functions / sizeof hash_functions[0]; +static const size_t nr_hash_functions = sizeof hash_functions + / sizeof hash_functions[0]; uint64_t fx_hash_cstr(const char *s) { @@ -66,6 +66,33 @@ uint64_t fx_hash_cstr_ex(const char *s, size_t *len) return hash; } +uint64_t fx_hash_wstr(const fx_wchar *s) +{ + uint64_t hash = FNV1_OFFSET_BASIS; + size_t i = 0; + + for (i = 0; s[i]; i++) { + hash ^= s[i]; + hash *= FNV1_PRIME; + } + + return hash; +} + +uint64_t fx_hash_buffer(const void *p, size_t len) +{ + const char *s = p; + uint64_t hash = FNV1_OFFSET_BASIS; + size_t i = 0; + + for (i = 0; i < len; i++) { + hash ^= s[i]; + hash *= FNV1_PRIME; + } + + return hash; +} + enum fx_status fx_hash_ctx_init( struct fx_hash_ctx *ctx, enum fx_hash_function func) diff --git a/fx/include/fx/hash.h b/fx/include/fx/hash.h index 13687a9..c026c92 100644 --- a/fx/include/fx/hash.h +++ b/fx/include/fx/hash.h @@ -1,6 +1,7 @@ #ifndef FX_CORE_HASH_H_ #define FX_CORE_HASH_H_ +#include #include #include #include @@ -99,15 +100,16 @@ typedef struct fx_hash_ctx { } fx_hash_ctx; FX_API uint64_t fx_hash_cstr(const char *s); +FX_API uint64_t fx_hash_wstr(const fx_wchar *s); FX_API uint64_t fx_hash_cstr_ex(const char *s, size_t *len); +FX_API uint64_t fx_hash_buffer(const void *s, size_t len); FX_API fx_status fx_hash_ctx_init(fx_hash_ctx *ctx, fx_hash_function func); FX_API fx_status fx_hash_ctx_reset(fx_hash_ctx *ctx); FX_API fx_status fx_hash_ctx_update(fx_hash_ctx *ctx, const void *p, size_t len); -FX_API fx_status fx_hash_ctx_update_rope( - fx_hash_ctx *ctx, - const struct fx_rope *rope); +FX_API fx_status +fx_hash_ctx_update_rope(fx_hash_ctx *ctx, const struct fx_rope *rope); FX_API fx_status fx_hash_ctx_finish(fx_hash_ctx *ctx, void *out_digest, size_t out_max);