1/* 2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10{- 11use OpenSSL::stackhash qw(generate_lhash_macros); 12-} 13 14/* 15 * Header for dynamic hash table routines Author - Eric Young 16 */ 17 18#ifndef OPENSSL_LHASH_H 19# define OPENSSL_LHASH_H 20# pragma once 21 22# include <openssl/macros.h> 23# ifndef OPENSSL_NO_DEPRECATED_3_0 24# define HEADER_LHASH_H 25# endif 26 27# include <openssl/e_os2.h> 28# include <openssl/bio.h> 29# ifndef OPENSSL_NO_STDIO 30# include <stdio.h> 31# endif 32 33#ifdef __cplusplus 34extern "C" { 35#endif 36 37typedef struct lhash_node_st OPENSSL_LH_NODE; 38typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *); 39typedef int (*OPENSSL_LH_COMPFUNCTHUNK) (const void *, const void *, OPENSSL_LH_COMPFUNC cfn); 40typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *); 41typedef unsigned long (*OPENSSL_LH_HASHFUNCTHUNK) (const void *, OPENSSL_LH_HASHFUNC hfn); 42typedef void (*OPENSSL_LH_DOALL_FUNC) (void *); 43typedef void (*OPENSSL_LH_DOALL_FUNC_THUNK) (void *, OPENSSL_LH_DOALL_FUNC doall); 44typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *); 45typedef void (*OPENSSL_LH_DOALL_FUNCARG_THUNK) (void *, void *, OPENSSL_LH_DOALL_FUNCARG doall); 46typedef struct lhash_st OPENSSL_LHASH; 47 48/* 49 * Macros for declaring and implementing type-safe wrappers for LHASH 50 * callbacks. This way, callbacks can be provided to LHASH structures without 51 * function pointer casting and the macro-defined callbacks provide 52 * per-variable casting before deferring to the underlying type-specific 53 * callbacks. NB: It is possible to place a "static" in front of both the 54 * DECLARE and IMPLEMENT macros if the functions are strictly internal. 55 */ 56 57/* First: "hash" functions */ 58# define DECLARE_LHASH_HASH_FN(name, o_type) \ 59 unsigned long name##_LHASH_HASH(const void *); 60# define IMPLEMENT_LHASH_HASH_FN(name, o_type) \ 61 unsigned long name##_LHASH_HASH(const void *arg) { \ 62 const o_type *a = arg; \ 63 return name##_hash(a); } 64# define LHASH_HASH_FN(name) name##_LHASH_HASH 65 66/* Second: "compare" functions */ 67# define DECLARE_LHASH_COMP_FN(name, o_type) \ 68 int name##_LHASH_COMP(const void *, const void *); 69# define IMPLEMENT_LHASH_COMP_FN(name, o_type) \ 70 int name##_LHASH_COMP(const void *arg1, const void *arg2) { \ 71 const o_type *a = arg1; \ 72 const o_type *b = arg2; \ 73 return name##_cmp(a,b); } 74# define LHASH_COMP_FN(name) name##_LHASH_COMP 75 76/* Fourth: "doall_arg" functions */ 77# define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ 78 void name##_LHASH_DOALL_ARG(void *, void *); 79# define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ 80 void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \ 81 o_type *a = arg1; \ 82 a_type *b = arg2; \ 83 name##_doall_arg(a, b); } 84# define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG 85 86 87# define LH_LOAD_MULT 256 88 89int OPENSSL_LH_error(OPENSSL_LHASH *lh); 90OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c); 91OPENSSL_LHASH *OPENSSL_LH_set_thunks(OPENSSL_LHASH *lh, 92 OPENSSL_LH_HASHFUNCTHUNK hw, 93 OPENSSL_LH_COMPFUNCTHUNK cw, 94 OPENSSL_LH_DOALL_FUNC_THUNK daw, 95 OPENSSL_LH_DOALL_FUNCARG_THUNK daaw); 96void OPENSSL_LH_free(OPENSSL_LHASH *lh); 97void OPENSSL_LH_flush(OPENSSL_LHASH *lh); 98void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data); 99void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data); 100void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data); 101void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func); 102void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, 103 OPENSSL_LH_DOALL_FUNCARG func, void *arg); 104void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh, 105 OPENSSL_LH_DOALL_FUNCARG_THUNK daaw, 106 OPENSSL_LH_DOALL_FUNCARG fn, void *arg); 107 108unsigned long OPENSSL_LH_strhash(const char *c); 109unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh); 110unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh); 111void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load); 112 113# ifndef OPENSSL_NO_STDIO 114# ifndef OPENSSL_NO_DEPRECATED_3_1 115OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp); 116OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp); 117OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp); 118# endif 119# endif 120# ifndef OPENSSL_NO_DEPRECATED_3_1 121OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out); 122OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out); 123OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out); 124# endif 125 126# ifndef OPENSSL_NO_DEPRECATED_1_1_0 127# define _LHASH OPENSSL_LHASH 128# define LHASH_NODE OPENSSL_LH_NODE 129# define lh_error OPENSSL_LH_error 130# define lh_new OPENSSL_LH_new 131# define lh_free OPENSSL_LH_free 132# define lh_insert OPENSSL_LH_insert 133# define lh_delete OPENSSL_LH_delete 134# define lh_retrieve OPENSSL_LH_retrieve 135# define lh_doall OPENSSL_LH_doall 136# define lh_doall_arg OPENSSL_LH_doall_arg 137# define lh_strhash OPENSSL_LH_strhash 138# define lh_num_items OPENSSL_LH_num_items 139# ifndef OPENSSL_NO_STDIO 140# define lh_stats OPENSSL_LH_stats 141# define lh_node_stats OPENSSL_LH_node_stats 142# define lh_node_usage_stats OPENSSL_LH_node_usage_stats 143# endif 144# define lh_stats_bio OPENSSL_LH_stats_bio 145# define lh_node_stats_bio OPENSSL_LH_node_stats_bio 146# define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio 147# endif 148 149/* Type checking... */ 150 151# define LHASH_OF(type) struct lhash_st_##type 152 153/* Helper macro for internal use */ 154# define DEFINE_LHASH_OF_INTERNAL(type) \ 155 LHASH_OF(type) { \ 156 union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; \ 157 }; \ 158 typedef int (*lh_##type##_compfunc)(const type *a, const type *b); \ 159 typedef unsigned long (*lh_##type##_hashfunc)(const type *a); \ 160 typedef void (*lh_##type##_doallfunc)(type *a); \ 161 static ossl_inline unsigned long lh_##type##_hash_thunk(const void *data, OPENSSL_LH_HASHFUNC hfn) \ 162 { \ 163 unsigned long (*hfn_conv)(const type *) = (unsigned long (*)(const type *))hfn; \ 164 return hfn_conv((const type *)data); \ 165 } \ 166 static ossl_inline int lh_##type##_comp_thunk(const void *da, const void *db, OPENSSL_LH_COMPFUNC cfn) \ 167 { \ 168 int (*cfn_conv)(const type *, const type *) = (int (*)(const type *, const type *))cfn; \ 169 return cfn_conv((const type *)da, (const type *)db); \ 170 } \ 171 static ossl_inline void lh_##type##_doall_thunk(void *node, OPENSSL_LH_DOALL_FUNC doall) \ 172 { \ 173 void (*doall_conv)(type *) = (void (*)(type *))doall; \ 174 doall_conv((type *)node); \ 175 } \ 176 static ossl_inline void lh_##type##_doall_arg_thunk(void *node, void *arg, OPENSSL_LH_DOALL_FUNCARG doall) \ 177 { \ 178 void (*doall_conv)(type *, void *) = (void (*)(type *, void *))doall; \ 179 doall_conv((type *)node, arg); \ 180 } \ 181 static ossl_unused ossl_inline type *\ 182 ossl_check_##type##_lh_plain_type(type *ptr) \ 183 { \ 184 return ptr; \ 185 } \ 186 static ossl_unused ossl_inline const type * \ 187 ossl_check_const_##type##_lh_plain_type(const type *ptr) \ 188 { \ 189 return ptr; \ 190 } \ 191 static ossl_unused ossl_inline const OPENSSL_LHASH * \ 192 ossl_check_const_##type##_lh_type(const LHASH_OF(type) *lh) \ 193 { \ 194 return (const OPENSSL_LHASH *)lh; \ 195 } \ 196 static ossl_unused ossl_inline OPENSSL_LHASH * \ 197 ossl_check_##type##_lh_type(LHASH_OF(type) *lh) \ 198 { \ 199 return (OPENSSL_LHASH *)lh; \ 200 } \ 201 static ossl_unused ossl_inline OPENSSL_LH_COMPFUNC \ 202 ossl_check_##type##_lh_compfunc_type(lh_##type##_compfunc cmp) \ 203 { \ 204 return (OPENSSL_LH_COMPFUNC)cmp; \ 205 } \ 206 static ossl_unused ossl_inline OPENSSL_LH_HASHFUNC \ 207 ossl_check_##type##_lh_hashfunc_type(lh_##type##_hashfunc hfn) \ 208 { \ 209 return (OPENSSL_LH_HASHFUNC)hfn; \ 210 } \ 211 static ossl_unused ossl_inline OPENSSL_LH_DOALL_FUNC \ 212 ossl_check_##type##_lh_doallfunc_type(lh_##type##_doallfunc dfn) \ 213 { \ 214 return (OPENSSL_LH_DOALL_FUNC)dfn; \ 215 } \ 216 LHASH_OF(type) 217 218# ifndef OPENSSL_NO_DEPRECATED_3_1 219# define DEFINE_LHASH_OF_DEPRECATED(type) \ 220 static ossl_unused ossl_inline void \ 221 lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ 222 { \ 223 OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \ 224 } \ 225 static ossl_unused ossl_inline void \ 226 lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ 227 { \ 228 OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \ 229 } \ 230 static ossl_unused ossl_inline void \ 231 lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ 232 { \ 233 OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \ 234 } 235# else 236# define DEFINE_LHASH_OF_DEPRECATED(type) 237# endif 238 239# define DEFINE_LHASH_OF_EX(type) \ 240 LHASH_OF(type) { \ 241 union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; \ 242 }; \ 243 static unsigned long \ 244 lh_##type##_hfn_thunk(const void *data, OPENSSL_LH_HASHFUNC hfn) \ 245 { \ 246 unsigned long (*hfn_conv)(const type *) = (unsigned long (*)(const type *))hfn; \ 247 return hfn_conv((const type *)data); \ 248 } \ 249 static int lh_##type##_cfn_thunk(const void *da, const void *db, OPENSSL_LH_COMPFUNC cfn) \ 250 { \ 251 int (*cfn_conv)(const type *, const type *) = (int (*)(const type *, const type *))cfn; \ 252 return cfn_conv((const type *)da, (const type *)db); \ 253 } \ 254 static ossl_unused ossl_inline void \ 255 lh_##type##_free(LHASH_OF(type) *lh) \ 256 { \ 257 OPENSSL_LH_free((OPENSSL_LHASH *)lh); \ 258 } \ 259 static ossl_unused ossl_inline void \ 260 lh_##type##_flush(LHASH_OF(type) *lh) \ 261 { \ 262 OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \ 263 } \ 264 static ossl_unused ossl_inline type * \ 265 lh_##type##_insert(LHASH_OF(type) *lh, type *d) \ 266 { \ 267 return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \ 268 } \ 269 static ossl_unused ossl_inline type * \ 270 lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \ 271 { \ 272 return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \ 273 } \ 274 static ossl_unused ossl_inline type * \ 275 lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \ 276 { \ 277 return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \ 278 } \ 279 static ossl_unused ossl_inline int \ 280 lh_##type##_error(LHASH_OF(type) *lh) \ 281 { \ 282 return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \ 283 } \ 284 static ossl_unused ossl_inline unsigned long \ 285 lh_##type##_num_items(LHASH_OF(type) *lh) \ 286 { \ 287 return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \ 288 } \ 289 static ossl_unused ossl_inline unsigned long \ 290 lh_##type##_get_down_load(LHASH_OF(type) *lh) \ 291 { \ 292 return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \ 293 } \ 294 static ossl_unused ossl_inline void \ 295 lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \ 296 { \ 297 OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \ 298 } \ 299 static ossl_unused ossl_inline void \ 300 lh_##type##_doall_thunk(void *node, OPENSSL_LH_DOALL_FUNC doall) \ 301 { \ 302 void (*doall_conv)(type *) = (void (*)(type *))doall; \ 303 doall_conv((type *)node); \ 304 } \ 305 static ossl_unused ossl_inline void \ 306 lh_##type##_doall_arg_thunk(void *node, void *arg, OPENSSL_LH_DOALL_FUNCARG doall) \ 307 { \ 308 void (*doall_conv)(type *, void *) = (void (*)(type *, void *))doall; \ 309 doall_conv((type *)node, arg); \ 310 } \ 311 static ossl_unused ossl_inline void \ 312 lh_##type##_doall(LHASH_OF(type) *lh, void (*doall)(type *)) \ 313 { \ 314 OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \ 315 } \ 316 static ossl_unused ossl_inline LHASH_OF(type) * \ 317 lh_##type##_new(unsigned long (*hfn)(const type *), \ 318 int (*cfn)(const type *, const type *)) \ 319 { \ 320 return (LHASH_OF(type) *)OPENSSL_LH_set_thunks(OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn), \ 321 lh_##type##_hfn_thunk, lh_##type##_cfn_thunk, \ 322 lh_##type##_doall_thunk, \ 323 lh_##type##_doall_arg_thunk); \ 324 } \ 325 static ossl_unused ossl_inline void \ 326 lh_##type##_doall_arg(LHASH_OF(type) *lh, \ 327 void (*doallarg)(type *, void *), void *arg) \ 328 { \ 329 OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, \ 330 (OPENSSL_LH_DOALL_FUNCARG)doallarg, arg); \ 331 } \ 332 LHASH_OF(type) 333 334# define DEFINE_LHASH_OF(type) \ 335 DEFINE_LHASH_OF_EX(type); \ 336 DEFINE_LHASH_OF_DEPRECATED(type) \ 337 LHASH_OF(type) 338 339#define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \ 340 int_implement_lhash_doall(type, argtype, const type) 341 342#define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \ 343 int_implement_lhash_doall(type, argtype, type) 344 345#define int_implement_lhash_doall(type, argtype, cbargtype) \ 346 static ossl_unused ossl_inline void \ 347 lh_##type##_doall_##argtype##_thunk(void *node, void *arg, OPENSSL_LH_DOALL_FUNCARG fn) \ 348 { \ 349 void (*fn_conv)(cbargtype *, argtype *) = (void (*)(cbargtype *, argtype *))fn; \ 350 fn_conv((cbargtype *)node, (argtype *)arg); \ 351 } \ 352 static ossl_unused ossl_inline void \ 353 lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \ 354 void (*fn)(cbargtype *, argtype *), \ 355 argtype *arg) \ 356 { \ 357 OPENSSL_LH_doall_arg_thunk((OPENSSL_LHASH *)lh, \ 358 lh_##type##_doall_##argtype##_thunk, \ 359 (OPENSSL_LH_DOALL_FUNCARG)fn, \ 360 (void *)arg); \ 361 } \ 362 LHASH_OF(type) 363 364{- 365 generate_lhash_macros("OPENSSL_STRING") 366 .generate_lhash_macros("OPENSSL_CSTRING"); 367-} 368 369#ifdef __cplusplus 370} 371#endif 372 373#endif 374