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