lhash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright 1995-2022 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. * Header for dynamic hash table routines Author - Eric Young
  11. */
  12. #ifndef OPENSSL_LHASH_H
  13. # define OPENSSL_LHASH_H
  14. # pragma once
  15. # include <openssl/macros.h>
  16. # ifndef OPENSSL_NO_DEPRECATED_3_0
  17. # define HEADER_LHASH_H
  18. # endif
  19. # include <openssl/e_os2.h>
  20. # include <openssl/bio.h>
  21. # ifndef OPENSSL_NO_STDIO
  22. # include <stdio.h>
  23. # endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. typedef struct lhash_node_st OPENSSL_LH_NODE;
  28. typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *);
  29. typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *);
  30. typedef void (*OPENSSL_LH_DOALL_FUNC) (void *);
  31. typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *);
  32. typedef struct lhash_st OPENSSL_LHASH;
  33. /*
  34. * Macros for declaring and implementing type-safe wrappers for LHASH
  35. * callbacks. This way, callbacks can be provided to LHASH structures without
  36. * function pointer casting and the macro-defined callbacks provide
  37. * per-variable casting before deferring to the underlying type-specific
  38. * callbacks. NB: It is possible to place a "static" in front of both the
  39. * DECLARE and IMPLEMENT macros if the functions are strictly internal.
  40. */
  41. /* First: "hash" functions */
  42. # define DECLARE_LHASH_HASH_FN(name, o_type) \
  43. unsigned long name##_LHASH_HASH(const void *);
  44. # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
  45. unsigned long name##_LHASH_HASH(const void *arg) { \
  46. const o_type *a = arg; \
  47. return name##_hash(a); }
  48. # define LHASH_HASH_FN(name) name##_LHASH_HASH
  49. /* Second: "compare" functions */
  50. # define DECLARE_LHASH_COMP_FN(name, o_type) \
  51. int name##_LHASH_COMP(const void *, const void *);
  52. # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
  53. int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
  54. const o_type *a = arg1; \
  55. const o_type *b = arg2; \
  56. return name##_cmp(a,b); }
  57. # define LHASH_COMP_FN(name) name##_LHASH_COMP
  58. /* Fourth: "doall_arg" functions */
  59. # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  60. void name##_LHASH_DOALL_ARG(void *, void *);
  61. # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  62. void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
  63. o_type *a = arg1; \
  64. a_type *b = arg2; \
  65. name##_doall_arg(a, b); }
  66. # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
  67. # define LH_LOAD_MULT 256
  68. int OPENSSL_LH_error(OPENSSL_LHASH *lh);
  69. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
  70. void OPENSSL_LH_free(OPENSSL_LHASH *lh);
  71. void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
  72. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
  73. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
  74. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
  75. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
  76. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
  77. unsigned long OPENSSL_LH_strhash(const char *c);
  78. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh);
  79. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh);
  80. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load);
  81. # ifndef OPENSSL_NO_STDIO
  82. # ifndef OPENSSL_NO_DEPRECATED_3_1
  83. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp);
  84. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp);
  85. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp);
  86. # endif
  87. # endif
  88. # ifndef OPENSSL_NO_DEPRECATED_3_1
  89. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  90. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  91. OSSL_DEPRECATEDIN_3_1 void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  92. # endif
  93. # ifndef OPENSSL_NO_DEPRECATED_1_1_0
  94. # define _LHASH OPENSSL_LHASH
  95. # define LHASH_NODE OPENSSL_LH_NODE
  96. # define lh_error OPENSSL_LH_error
  97. # define lh_new OPENSSL_LH_new
  98. # define lh_free OPENSSL_LH_free
  99. # define lh_insert OPENSSL_LH_insert
  100. # define lh_delete OPENSSL_LH_delete
  101. # define lh_retrieve OPENSSL_LH_retrieve
  102. # define lh_doall OPENSSL_LH_doall
  103. # define lh_doall_arg OPENSSL_LH_doall_arg
  104. # define lh_strhash OPENSSL_LH_strhash
  105. # define lh_num_items OPENSSL_LH_num_items
  106. # ifndef OPENSSL_NO_STDIO
  107. # define lh_stats OPENSSL_LH_stats
  108. # define lh_node_stats OPENSSL_LH_node_stats
  109. # define lh_node_usage_stats OPENSSL_LH_node_usage_stats
  110. # endif
  111. # define lh_stats_bio OPENSSL_LH_stats_bio
  112. # define lh_node_stats_bio OPENSSL_LH_node_stats_bio
  113. # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio
  114. # endif
  115. /* Type checking... */
  116. # define LHASH_OF(type) struct lhash_st_##type
  117. /* Helper macro for internal use */
  118. # define DEFINE_LHASH_OF_INTERNAL(type) \
  119. LHASH_OF(type) { \
  120. union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; \
  121. }; \
  122. typedef int (*lh_##type##_compfunc)(const type *a, const type *b); \
  123. typedef unsigned long (*lh_##type##_hashfunc)(const type *a); \
  124. typedef void (*lh_##type##_doallfunc)(type *a); \
  125. static ossl_unused ossl_inline type *\
  126. ossl_check_##type##_lh_plain_type(type *ptr) \
  127. { \
  128. return ptr; \
  129. } \
  130. static ossl_unused ossl_inline const type * \
  131. ossl_check_const_##type##_lh_plain_type(const type *ptr) \
  132. { \
  133. return ptr; \
  134. } \
  135. static ossl_unused ossl_inline const OPENSSL_LHASH * \
  136. ossl_check_const_##type##_lh_type(const LHASH_OF(type) *lh) \
  137. { \
  138. return (const OPENSSL_LHASH *)lh; \
  139. } \
  140. static ossl_unused ossl_inline OPENSSL_LHASH * \
  141. ossl_check_##type##_lh_type(LHASH_OF(type) *lh) \
  142. { \
  143. return (OPENSSL_LHASH *)lh; \
  144. } \
  145. static ossl_unused ossl_inline OPENSSL_LH_COMPFUNC \
  146. ossl_check_##type##_lh_compfunc_type(lh_##type##_compfunc cmp) \
  147. { \
  148. return (OPENSSL_LH_COMPFUNC)cmp; \
  149. } \
  150. static ossl_unused ossl_inline OPENSSL_LH_HASHFUNC \
  151. ossl_check_##type##_lh_hashfunc_type(lh_##type##_hashfunc hfn) \
  152. { \
  153. return (OPENSSL_LH_HASHFUNC)hfn; \
  154. } \
  155. static ossl_unused ossl_inline OPENSSL_LH_DOALL_FUNC \
  156. ossl_check_##type##_lh_doallfunc_type(lh_##type##_doallfunc dfn) \
  157. { \
  158. return (OPENSSL_LH_DOALL_FUNC)dfn; \
  159. } \
  160. LHASH_OF(type)
  161. # ifndef OPENSSL_NO_DEPRECATED_3_1
  162. # define DEFINE_LHASH_OF_DEPRECATED(type) \
  163. static ossl_unused ossl_inline void \
  164. lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  165. { \
  166. OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
  167. } \
  168. static ossl_unused ossl_inline void \
  169. lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  170. { \
  171. OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
  172. } \
  173. static ossl_unused ossl_inline void \
  174. lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  175. { \
  176. OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
  177. }
  178. # else
  179. # define DEFINE_LHASH_OF_DEPRECATED(type)
  180. # endif
  181. # define DEFINE_LHASH_OF_EX(type) \
  182. LHASH_OF(type) { \
  183. union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; \
  184. }; \
  185. static ossl_unused ossl_inline LHASH_OF(type) * \
  186. lh_##type##_new(unsigned long (*hfn)(const type *), \
  187. int (*cfn)(const type *, const type *)) \
  188. { \
  189. return (LHASH_OF(type) *) \
  190. OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
  191. } \
  192. static ossl_unused ossl_inline void \
  193. lh_##type##_free(LHASH_OF(type) *lh) \
  194. { \
  195. OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
  196. } \
  197. static ossl_unused ossl_inline void \
  198. lh_##type##_flush(LHASH_OF(type) *lh) \
  199. { \
  200. OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
  201. } \
  202. static ossl_unused ossl_inline type * \
  203. lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
  204. { \
  205. return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
  206. } \
  207. static ossl_unused ossl_inline type * \
  208. lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
  209. { \
  210. return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
  211. } \
  212. static ossl_unused ossl_inline type * \
  213. lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
  214. { \
  215. return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
  216. } \
  217. static ossl_unused ossl_inline int \
  218. lh_##type##_error(LHASH_OF(type) *lh) \
  219. { \
  220. return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
  221. } \
  222. static ossl_unused ossl_inline unsigned long \
  223. lh_##type##_num_items(LHASH_OF(type) *lh) \
  224. { \
  225. return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
  226. } \
  227. static ossl_unused ossl_inline unsigned long \
  228. lh_##type##_get_down_load(LHASH_OF(type) *lh) \
  229. { \
  230. return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
  231. } \
  232. static ossl_unused ossl_inline void \
  233. lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
  234. { \
  235. OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
  236. } \
  237. static ossl_unused ossl_inline void \
  238. lh_##type##_doall(LHASH_OF(type) *lh, void (*doall)(type *)) \
  239. { \
  240. OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
  241. } \
  242. static ossl_unused ossl_inline void \
  243. lh_##type##_doall_arg(LHASH_OF(type) *lh, \
  244. void (*doallarg)(type *, void *), void *arg) \
  245. { \
  246. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, \
  247. (OPENSSL_LH_DOALL_FUNCARG)doallarg, arg); \
  248. } \
  249. LHASH_OF(type)
  250. # define DEFINE_LHASH_OF(type) \
  251. DEFINE_LHASH_OF_EX(type); \
  252. DEFINE_LHASH_OF_DEPRECATED(type) \
  253. LHASH_OF(type)
  254. #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
  255. int_implement_lhash_doall(type, argtype, const type)
  256. #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
  257. int_implement_lhash_doall(type, argtype, type)
  258. #define int_implement_lhash_doall(type, argtype, cbargtype) \
  259. static ossl_unused ossl_inline void \
  260. lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
  261. void (*fn)(cbargtype *, argtype *), \
  262. argtype *arg) \
  263. { \
  264. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, \
  265. (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
  266. } \
  267. LHASH_OF(type)
  268. DEFINE_LHASH_OF_INTERNAL(OPENSSL_STRING);
  269. #define lh_OPENSSL_STRING_new(hfn, cmp) ((LHASH_OF(OPENSSL_STRING) *)OPENSSL_LH_new(ossl_check_OPENSSL_STRING_lh_hashfunc_type(hfn), ossl_check_OPENSSL_STRING_lh_compfunc_type(cmp)))
  270. #define lh_OPENSSL_STRING_free(lh) OPENSSL_LH_free(ossl_check_OPENSSL_STRING_lh_type(lh))
  271. #define lh_OPENSSL_STRING_flush(lh) OPENSSL_LH_flush(ossl_check_OPENSSL_STRING_lh_type(lh))
  272. #define lh_OPENSSL_STRING_insert(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_insert(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_OPENSSL_STRING_lh_plain_type(ptr)))
  273. #define lh_OPENSSL_STRING_delete(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_delete(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_const_OPENSSL_STRING_lh_plain_type(ptr)))
  274. #define lh_OPENSSL_STRING_retrieve(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_retrieve(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_const_OPENSSL_STRING_lh_plain_type(ptr)))
  275. #define lh_OPENSSL_STRING_error(lh) OPENSSL_LH_error(ossl_check_OPENSSL_STRING_lh_type(lh))
  276. #define lh_OPENSSL_STRING_num_items(lh) OPENSSL_LH_num_items(ossl_check_OPENSSL_STRING_lh_type(lh))
  277. #define lh_OPENSSL_STRING_node_stats_bio(lh, out) OPENSSL_LH_node_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out)
  278. #define lh_OPENSSL_STRING_node_usage_stats_bio(lh, out) OPENSSL_LH_node_usage_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out)
  279. #define lh_OPENSSL_STRING_stats_bio(lh, out) OPENSSL_LH_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out)
  280. #define lh_OPENSSL_STRING_get_down_load(lh) OPENSSL_LH_get_down_load(ossl_check_OPENSSL_STRING_lh_type(lh))
  281. #define lh_OPENSSL_STRING_set_down_load(lh, dl) OPENSSL_LH_set_down_load(ossl_check_OPENSSL_STRING_lh_type(lh), dl)
  282. #define lh_OPENSSL_STRING_doall(lh, dfn) OPENSSL_LH_doall(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_OPENSSL_STRING_lh_doallfunc_type(dfn))
  283. DEFINE_LHASH_OF_INTERNAL(OPENSSL_CSTRING);
  284. #define lh_OPENSSL_CSTRING_new(hfn, cmp) ((LHASH_OF(OPENSSL_CSTRING) *)OPENSSL_LH_new(ossl_check_OPENSSL_CSTRING_lh_hashfunc_type(hfn), ossl_check_OPENSSL_CSTRING_lh_compfunc_type(cmp)))
  285. #define lh_OPENSSL_CSTRING_free(lh) OPENSSL_LH_free(ossl_check_OPENSSL_CSTRING_lh_type(lh))
  286. #define lh_OPENSSL_CSTRING_flush(lh) OPENSSL_LH_flush(ossl_check_OPENSSL_CSTRING_lh_type(lh))
  287. #define lh_OPENSSL_CSTRING_insert(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_insert(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_OPENSSL_CSTRING_lh_plain_type(ptr)))
  288. #define lh_OPENSSL_CSTRING_delete(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_delete(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_const_OPENSSL_CSTRING_lh_plain_type(ptr)))
  289. #define lh_OPENSSL_CSTRING_retrieve(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_retrieve(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_const_OPENSSL_CSTRING_lh_plain_type(ptr)))
  290. #define lh_OPENSSL_CSTRING_error(lh) OPENSSL_LH_error(ossl_check_OPENSSL_CSTRING_lh_type(lh))
  291. #define lh_OPENSSL_CSTRING_num_items(lh) OPENSSL_LH_num_items(ossl_check_OPENSSL_CSTRING_lh_type(lh))
  292. #define lh_OPENSSL_CSTRING_node_stats_bio(lh, out) OPENSSL_LH_node_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out)
  293. #define lh_OPENSSL_CSTRING_node_usage_stats_bio(lh, out) OPENSSL_LH_node_usage_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out)
  294. #define lh_OPENSSL_CSTRING_stats_bio(lh, out) OPENSSL_LH_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out)
  295. #define lh_OPENSSL_CSTRING_get_down_load(lh) OPENSSL_LH_get_down_load(ossl_check_OPENSSL_CSTRING_lh_type(lh))
  296. #define lh_OPENSSL_CSTRING_set_down_load(lh, dl) OPENSSL_LH_set_down_load(ossl_check_OPENSSL_CSTRING_lh_type(lh), dl)
  297. #define lh_OPENSSL_CSTRING_doall(lh, dfn) OPENSSL_LH_doall(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_OPENSSL_CSTRING_lh_doallfunc_type(dfn))
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif