lhash.h 14 KB

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