| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_LHASH_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_LHASH_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/lhash.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// lhash is a traditional, chaining hash table that automatically expands and |
| 15 |
// contracts as needed. One should not use the lh_* functions directly, rather |
| 16 |
// use the type-safe macro wrappers: |
| 17 |
// |
| 18 |
// A hash table of a specific type of object has type |LHASH_OF(type)|. This |
| 19 |
// can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed |
| 20 |
// with |DECLARE_LHASH_OF(type)|. For example: |
| 21 |
// |
| 22 |
// struct foo { |
| 23 |
// int bar; |
| 24 |
// }; |
| 25 |
// |
| 26 |
// DEFINE_LHASH_OF(struct foo) |
| 27 |
// |
| 28 |
// Although note that the hash table will contain /pointers/ to |foo|. |
| 29 |
// |
| 30 |
// A macro will be defined for each of the |OPENSSL_lh_*| functions below. For |
| 31 |
// |LHASH_OF(foo)|, the macros would be |lh_foo_new|, |lh_foo_num_items| etc. |
| 32 |
|
| 33 |
|
| 34 |
// lhash_cmp_func is a comparison function that returns a value equal, or not |
| 35 |
// equal, to zero depending on whether |*a| is equal, or not equal to |*b|, |
| 36 |
// respectively. Note the difference between this and |stack_cmp_func| in that |
| 37 |
// this takes pointers to the objects directly. |
| 38 |
// |
| 39 |
// This function's actual type signature is int (*)(const T*, const T*). The |
| 40 |
// low-level |lh_*| functions will be passed a type-specific wrapper to call it |
| 41 |
// correctly. |
| 42 |
typedef int (*lhash_cmp_func)(const void *a, const void *b); |
| 43 |
typedef int (*lhash_cmp_func_helper)(lhash_cmp_func func, const void *a, |
| 44 |
const void *b); |
| 45 |
|
| 46 |
// lhash_hash_func is a function that maps an object to a uniformly distributed |
| 47 |
// uint32_t. |
| 48 |
// |
| 49 |
// This function's actual type signature is uint32_t (*)(const T*). The |
| 50 |
// low-level |lh_*| functions will be passed a type-specific wrapper to call it |
| 51 |
// correctly. |
| 52 |
typedef uint32_t (*lhash_hash_func)(const void *a); |
| 53 |
typedef uint32_t (*lhash_hash_func_helper)(lhash_hash_func func, const void *a); |
| 54 |
|
| 55 |
|
| 56 |
// OPENSSL_lh_new returns a new, empty hash table or NULL on error. |
| 57 |
OPENSSL_EXPORT _LHASH *OPENSSL_lh_new(lhash_hash_func hash, |
| 58 |
lhash_cmp_func comp); |
| 59 |
|
| 60 |
// OPENSSL_lh_free frees the hash table itself but none of the elements. See |
| 61 |
// |OPENSSL_lh_doall|. |
| 62 |
OPENSSL_EXPORT void OPENSSL_lh_free(_LHASH *lh); |
| 63 |
|
| 64 |
// OPENSSL_lh_num_items returns the number of items in |lh|. |
| 65 |
OPENSSL_EXPORT size_t OPENSSL_lh_num_items(const _LHASH *lh); |
| 66 |
|
| 67 |
// OPENSSL_lh_retrieve finds an element equal to |data| in the hash table and |
| 68 |
// returns it. If no such element exists, it returns NULL. |
| 69 |
OPENSSL_EXPORT void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data, |
| 70 |
lhash_hash_func_helper call_hash_func, |
| 71 |
lhash_cmp_func_helper call_cmp_func); |
| 72 |
|
| 73 |
// OPENSSL_lh_retrieve_key finds an element matching |key|, given the specified |
| 74 |
// hash and comparison function. This differs from |OPENSSL_lh_retrieve| in that |
| 75 |
// the key may be a different type than the values stored in |lh|. |key_hash| |
| 76 |
// and |cmp_key| must be compatible with the functions passed into |
| 77 |
// |OPENSSL_lh_new|. |
| 78 |
OPENSSL_EXPORT void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key, |
| 79 |
uint32_t key_hash, |
| 80 |
int (*cmp_key)(const void *key, |
| 81 |
const void *value)); |
| 82 |
|
| 83 |
// OPENSSL_lh_insert inserts |data| into the hash table. If an existing element |
| 84 |
// is equal to |data| (with respect to the comparison function) then |*old_data| |
| 85 |
// will be set to that value and it will be replaced. Otherwise, or in the |
| 86 |
// event of an error, |*old_data| will be set to NULL. It returns one on |
| 87 |
// success or zero in the case of an allocation error. |
| 88 |
OPENSSL_EXPORT int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data, |
| 89 |
lhash_hash_func_helper call_hash_func, |
| 90 |
lhash_cmp_func_helper call_cmp_func); |
| 91 |
|
| 92 |
// OPENSSL_lh_delete removes an element equal to |data| from the hash table and |
| 93 |
// returns it. If no such element is found, it returns NULL. |
| 94 |
OPENSSL_EXPORT void *OPENSSL_lh_delete(_LHASH *lh, const void *data, |
| 95 |
lhash_hash_func_helper call_hash_func, |
| 96 |
lhash_cmp_func_helper call_cmp_func); |
| 97 |
|
| 98 |
// OPENSSL_lh_doall_arg calls |func| on each element of the hash table and also |
| 99 |
// passes |arg| as the second argument. |
| 100 |
// TODO(fork): rename this |
| 101 |
OPENSSL_EXPORT void OPENSSL_lh_doall_arg(_LHASH *lh, |
| 102 |
void (*func)(void *, void *), |
| 103 |
void *arg); |
| 104 |
|
| 105 |
#define DEFINE_LHASH_OF(type) \ |
| 106 |
/* We disable MSVC C4191 in this macro, which warns when pointers are cast \ |
| 107 |
* to the wrong type. While the cast itself is valid, it is often a bug \ |
| 108 |
* because calling it through the cast is UB. However, we never actually \ |
| 109 |
* call functions as |lhash_cmp_func|. The type is just a type-erased \ |
| 110 |
* function pointer. (C does not guarantee function pointers fit in \ |
| 111 |
* |void*|, and GCC will warn on this.) Thus we just disable the false \ |
| 112 |
* positive warning. */ \ |
| 113 |
OPENSSL_MSVC_PRAGMA(warning(push)) \ |
| 114 |
OPENSSL_MSVC_PRAGMA(warning(disable : 4191)) \ |
| 115 |
\ |
| 116 |
DECLARE_LHASH_OF(type) \ |
| 117 |
\ |
| 118 |
typedef int (*lhash_##type##_cmp_func)(const type *, const type *); \ |
| 119 |
typedef uint32_t (*lhash_##type##_hash_func)(const type *); \ |
| 120 |
\ |
| 121 |
OPENSSL_INLINE int lh_##type##_call_cmp_func(lhash_cmp_func func, \ |
| 122 |
const void *a, const void *b) { \ |
| 123 |
return ((lhash_##type##_cmp_func)func)((const type *)a, (const type *)b); \ |
| 124 |
} \ |
| 125 |
\ |
| 126 |
OPENSSL_INLINE uint32_t lh_##type##_call_hash_func(lhash_hash_func func, \ |
| 127 |
const void *a) { \ |
| 128 |
return ((lhash_##type##_hash_func)func)((const type *)a); \ |
| 129 |
} \ |
| 130 |
\ |
| 131 |
OPENSSL_INLINE LHASH_OF(type) *lh_##type##_new( \ |
| 132 |
lhash_##type##_hash_func hash, lhash_##type##_cmp_func comp) { \ |
| 133 |
return (LHASH_OF(type) *)OPENSSL_lh_new((lhash_hash_func)hash, \ |
| 134 |
(lhash_cmp_func)comp); \ |
| 135 |
} \ |
| 136 |
\ |
| 137 |
OPENSSL_INLINE void lh_##type##_free(LHASH_OF(type) *lh) { \ |
| 138 |
OPENSSL_lh_free((_LHASH *)lh); \ |
| 139 |
} \ |
| 140 |
\ |
| 141 |
OPENSSL_INLINE size_t lh_##type##_num_items(const LHASH_OF(type) *lh) { \ |
| 142 |
return OPENSSL_lh_num_items((const _LHASH *)lh); \ |
| 143 |
} \ |
| 144 |
\ |
| 145 |
OPENSSL_INLINE type *lh_##type##_retrieve(const LHASH_OF(type) *lh, \ |
| 146 |
const type *data) { \ |
| 147 |
return (type *)OPENSSL_lh_retrieve((const _LHASH *)lh, data, \ |
| 148 |
lh_##type##_call_hash_func, \ |
| 149 |
lh_##type##_call_cmp_func); \ |
| 150 |
} \ |
| 151 |
\ |
| 152 |
typedef struct { \ |
| 153 |
int (*cmp_key)(const void *key, const type *value); \ |
| 154 |
const void *key; \ |
| 155 |
} LHASH_CMP_KEY_##type; \ |
| 156 |
\ |
| 157 |
OPENSSL_INLINE int lh_##type##_call_cmp_key(const void *key, \ |
| 158 |
const void *value) { \ |
| 159 |
const LHASH_CMP_KEY_##type *cb = (const LHASH_CMP_KEY_##type *)key; \ |
| 160 |
return cb->cmp_key(cb->key, (const type *)value); \ |
| 161 |
} \ |
| 162 |
\ |
| 163 |
OPENSSL_INLINE type *lh_##type##_retrieve_key( \ |
| 164 |
const LHASH_OF(type) *lh, const void *key, uint32_t key_hash, \ |
| 165 |
int (*cmp_key)(const void *key, const type *value)) { \ |
| 166 |
LHASH_CMP_KEY_##type cb = {cmp_key, key}; \ |
| 167 |
return (type *)OPENSSL_lh_retrieve_key((const _LHASH *)lh, &cb, key_hash, \ |
| 168 |
lh_##type##_call_cmp_key); \ |
| 169 |
} \ |
| 170 |
\ |
| 171 |
OPENSSL_INLINE int lh_##type##_insert(LHASH_OF(type) *lh, type **old_data, \ |
| 172 |
type *data) { \ |
| 173 |
void *old_data_void = NULL; \ |
| 174 |
int ret = OPENSSL_lh_insert((_LHASH *)lh, &old_data_void, data, \ |
| 175 |
lh_##type##_call_hash_func, \ |
| 176 |
lh_##type##_call_cmp_func); \ |
| 177 |
*old_data = (type *)old_data_void; \ |
| 178 |
return ret; \ |
| 179 |
} \ |
| 180 |
\ |
| 181 |
OPENSSL_INLINE type *lh_##type##_delete(LHASH_OF(type) *lh, \ |
| 182 |
const type *data) { \ |
| 183 |
return (type *)OPENSSL_lh_delete((_LHASH *)lh, data, \ |
| 184 |
lh_##type##_call_hash_func, \ |
| 185 |
lh_##type##_call_cmp_func); \ |
| 186 |
} \ |
| 187 |
\ |
| 188 |
typedef struct { \ |
| 189 |
void (*doall_arg)(type *, void *); \ |
| 190 |
void *arg; \ |
| 191 |
} LHASH_DOALL_##type; \ |
| 192 |
\ |
| 193 |
OPENSSL_INLINE void lh_##type##_call_doall_arg(void *value, void *arg) { \ |
| 194 |
const LHASH_DOALL_##type *cb = (const LHASH_DOALL_##type *)arg; \ |
| 195 |
cb->doall_arg((type *)value, cb->arg); \ |
| 196 |
} \ |
| 197 |
\ |
| 198 |
OPENSSL_INLINE void lh_##type##_doall_arg( \ |
| 199 |
LHASH_OF(type) *lh, void (*func)(type *, void *), void *arg) { \ |
| 200 |
LHASH_DOALL_##type cb = {func, arg}; \ |
| 201 |
OPENSSL_lh_doall_arg((_LHASH *)lh, lh_##type##_call_doall_arg, &cb); \ |
| 202 |
} \ |
| 203 |
\ |
| 204 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 205 |
|
| 206 |
|
| 207 |
#if defined(__cplusplus) |
| 208 |
} // extern C |
| 209 |
#endif |
| 210 |
|
| 211 |
#endif // OPENSSL_HEADER_LHASH_INTERNAL_H |