lhash.h.in 11 KB

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