You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lhash.h 25 kB

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