ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
uthash.h
Go to the documentation of this file.
1
24
25#pragma once
26
27// NOTE: This wrapper provides UBSan-safe hash functions for uthash
28// Files should include common.h BEFORE including any uthash-using headers
29
30// Only define custom allocators if SAFE_MALLOC is available
31// This allows headers to include uthash before common.h is fully processed
32#ifdef SAFE_MALLOC
33#define uthash_malloc(sz) SAFE_MALLOC(sz, void *)
34#define uthash_free(ptr, sz) SAFE_FREE(ptr)
35#endif
36
37// Include fnv1a for hash function (it doesn't depend on common.h)
38#include "util/fnv1a.h"
39
40// UBSan-safe hash wrapper for uthash (fnv1a uses 64-bit arithmetic, no
41// overflow) Note: uthash expects HASH_FUNCTION(keyptr, keylen, hashv) where
42// hashv is an output parameter
43#undef HASH_FUNCTION
44static inline unsigned uthash_ascii_chat_hash(const void *keyptr,
45 size_t keylen) {
46 return keylen == 0 || keyptr == NULL
47 ? 1U
48 : (unsigned)fnv1a_hash_bytes(keyptr, keylen);
49}
50#define HASH_FUNCTION(keyptr, keylen, hashv) \
51 do { \
52 (hashv) = uthash_ascii_chat_hash((keyptr), (keylen)); \
53 } while (0)
54
55// Include the upstream uthash header
56#include <ascii-chat-deps/uthash/src/uthash.h>
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62// Redefine HASH_JEN_MIX to use safe arithmetic that doesn't trigger UBSan
63// This prevents undefined behavior from left shifts that exceed uint32_t range
64// The original uthash uses shifts that can overflow, but we fix it by using
65// 64-bit arithmetic for intermediate calculations, then casting back to 32-bit
66// Must be defined AFTER including uthash.h since uthash.h unconditionally
67// defines it
68#undef HASH_JEN_MIX
69#define HASH_JEN_MIX(a, b, c) \
70 do { \
71 /* Use 64-bit arithmetic for shifts to avoid undefined behavior, then mask \
72 * back to 32-bit */ \
73 uint64_t _a = (uint64_t)(a), _b = (uint64_t)(b), _c = (uint64_t)(c); \
74 _a -= _b; \
75 _a -= _c; \
76 _a ^= (_c >> 13); \
77 a = (unsigned)(_a & 0xFFFFFFFFU); \
78 \
79 _b -= _c; \
80 _b -= (uint64_t)a; \
81 _b ^= (((uint64_t)a << 8) & 0xFFFFFFFFU); \
82 b = (unsigned)(_b & 0xFFFFFFFFU); \
83 \
84 _c -= (uint64_t)a; \
85 _c -= (uint64_t)b; \
86 _c ^= ((uint64_t)b >> 13); \
87 c = (unsigned)(_c & 0xFFFFFFFFU); \
88 \
89 _a = (uint64_t)a; \
90 _a -= (uint64_t)b; \
91 _a -= (uint64_t)c; \
92 _a ^= ((uint64_t)c >> 12); \
93 a = (unsigned)(_a & 0xFFFFFFFFU); \
94 \
95 _b = (uint64_t)b; \
96 _b -= (uint64_t)c; \
97 _b -= (uint64_t)a; \
98 _b ^= (((uint64_t)a << 16) & 0xFFFFFFFFU); \
99 b = (unsigned)(_b & 0xFFFFFFFFU); \
100 \
101 _c = (uint64_t)c; \
102 _c -= (uint64_t)a; \
103 _c -= (uint64_t)b; \
104 _c ^= ((uint64_t)b >> 5); \
105 c = (unsigned)(_c & 0xFFFFFFFFU); \
106 \
107 _a = (uint64_t)a; \
108 _a -= (uint64_t)b; \
109 _a -= (uint64_t)c; \
110 _a ^= ((uint64_t)c >> 3); \
111 a = (unsigned)(_a & 0xFFFFFFFFU); \
112 \
113 _b = (uint64_t)b; \
114 _b -= (uint64_t)c; \
115 _b -= (uint64_t)a; \
116 _b ^= (((uint64_t)a << 10) & 0xFFFFFFFFU); \
117 b = (unsigned)(_b & 0xFFFFFFFFU); \
118 \
119 _c = (uint64_t)c; \
120 _c -= (uint64_t)a; \
121 _c -= (uint64_t)b; \
122 _c ^= ((uint64_t)b >> 15); \
123 c = (unsigned)(_c & 0xFFFFFFFFU); \
124 } while (0)
125
126#ifdef __cplusplus
127}
128#endif
️⃣ FNV-1a Hash Function Implementation