lhash.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 1995-2019 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. # if !OPENSSL_API_3
  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. # if !OPENSSL_API_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. # define DEFINE_LHASH_OF(type) \
  111. LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
  112. static ossl_inline LHASH_OF(type) * \
  113. lh_##type##_new(unsigned long (*hfn)(const type *), \
  114. int (*cfn)(const type *, const type *)) \
  115. { \
  116. return (LHASH_OF(type) *) \
  117. OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
  118. } \
  119. static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
  120. { \
  121. OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
  122. } \
  123. static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
  124. { \
  125. OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
  126. } \
  127. static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
  128. { \
  129. return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
  130. } \
  131. static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
  132. { \
  133. return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
  134. } \
  135. static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
  136. { \
  137. return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
  138. } \
  139. static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
  140. { \
  141. return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
  142. } \
  143. static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
  144. { \
  145. return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
  146. } \
  147. static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  148. { \
  149. OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
  150. } \
  151. static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  152. { \
  153. OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
  154. } \
  155. static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  156. { \
  157. OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
  158. } \
  159. static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
  160. { \
  161. return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
  162. } \
  163. static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
  164. { \
  165. OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
  166. } \
  167. static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
  168. void (*doall)(type *)) \
  169. { \
  170. OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
  171. } \
  172. LHASH_OF(type)
  173. #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
  174. int_implement_lhash_doall(type, argtype, const type)
  175. #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
  176. int_implement_lhash_doall(type, argtype, type)
  177. #define int_implement_lhash_doall(type, argtype, cbargtype) \
  178. static ossl_unused ossl_inline void \
  179. lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
  180. void (*fn)(cbargtype *, argtype *), \
  181. argtype *arg) \
  182. { \
  183. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
  184. } \
  185. LHASH_OF(type)
  186. DEFINE_LHASH_OF(OPENSSL_STRING);
  187. # ifdef _MSC_VER
  188. /*
  189. * push and pop this warning:
  190. * warning C4090: 'function': different 'const' qualifiers
  191. */
  192. # pragma warning (push)
  193. # pragma warning (disable: 4090)
  194. # endif
  195. DEFINE_LHASH_OF(OPENSSL_CSTRING);
  196. # ifdef _MSC_VER
  197. # pragma warning (pop)
  198. # endif
  199. /*
  200. * If called without higher optimization (min. -xO3) the Oracle Developer
  201. * Studio compiler generates code for the defined (static inline) functions
  202. * above.
  203. * This would later lead to the linker complaining about missing symbols when
  204. * this header file is included but the resulting object is not linked against
  205. * the Crypto library (openssl#6912).
  206. */
  207. # ifdef __SUNPRO_C
  208. # pragma weak OPENSSL_LH_new
  209. # pragma weak OPENSSL_LH_free
  210. # pragma weak OPENSSL_LH_insert
  211. # pragma weak OPENSSL_LH_delete
  212. # pragma weak OPENSSL_LH_retrieve
  213. # pragma weak OPENSSL_LH_error
  214. # pragma weak OPENSSL_LH_num_items
  215. # pragma weak OPENSSL_LH_node_stats_bio
  216. # pragma weak OPENSSL_LH_node_usage_stats_bio
  217. # pragma weak OPENSSL_LH_stats_bio
  218. # pragma weak OPENSSL_LH_get_down_load
  219. # pragma weak OPENSSL_LH_set_down_load
  220. # pragma weak OPENSSL_LH_doall
  221. # pragma weak OPENSSL_LH_doall_arg
  222. # endif /* __SUNPRO_C */
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif