lhash.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 HEADER_LHASH_H
  13. # define HEADER_LHASH_H
  14. # include <openssl/e_os2.h>
  15. # include <openssl/bio.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef struct lhash_node_st OPENSSL_LH_NODE;
  20. typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *);
  21. typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *);
  22. typedef void (*OPENSSL_LH_DOALL_FUNC) (void *);
  23. typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *);
  24. typedef struct lhash_st OPENSSL_LHASH;
  25. /*
  26. * Macros for declaring and implementing type-safe wrappers for LHASH
  27. * callbacks. This way, callbacks can be provided to LHASH structures without
  28. * function pointer casting and the macro-defined callbacks provide
  29. * per-variable casting before deferring to the underlying type-specific
  30. * callbacks. NB: It is possible to place a "static" in front of both the
  31. * DECLARE and IMPLEMENT macros if the functions are strictly internal.
  32. */
  33. /* First: "hash" functions */
  34. # define DECLARE_LHASH_HASH_FN(name, o_type) \
  35. unsigned long name##_LHASH_HASH(const void *);
  36. # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
  37. unsigned long name##_LHASH_HASH(const void *arg) { \
  38. const o_type *a = arg; \
  39. return name##_hash(a); }
  40. # define LHASH_HASH_FN(name) name##_LHASH_HASH
  41. /* Second: "compare" functions */
  42. # define DECLARE_LHASH_COMP_FN(name, o_type) \
  43. int name##_LHASH_COMP(const void *, const void *);
  44. # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
  45. int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
  46. const o_type *a = arg1; \
  47. const o_type *b = arg2; \
  48. return name##_cmp(a,b); }
  49. # define LHASH_COMP_FN(name) name##_LHASH_COMP
  50. /* Fourth: "doall_arg" functions */
  51. # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  52. void name##_LHASH_DOALL_ARG(void *, void *);
  53. # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  54. void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
  55. o_type *a = arg1; \
  56. a_type *b = arg2; \
  57. name##_doall_arg(a, b); }
  58. # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
  59. # define LH_LOAD_MULT 256
  60. int OPENSSL_LH_error(OPENSSL_LHASH *lh);
  61. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
  62. void OPENSSL_LH_free(OPENSSL_LHASH *lh);
  63. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
  64. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
  65. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
  66. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
  67. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
  68. unsigned long OPENSSL_LH_strhash(const char *c);
  69. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh);
  70. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh);
  71. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load);
  72. # ifndef OPENSSL_NO_STDIO
  73. void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp);
  74. void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp);
  75. void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp);
  76. # endif
  77. void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  78. void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  79. void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  80. # if OPENSSL_API_COMPAT < 0x10100000L
  81. # define _LHASH OPENSSL_LHASH
  82. # define LHASH_NODE OPENSSL_LH_NODE
  83. # define lh_error OPENSSL_LH_error
  84. # define lh_new OPENSSL_LH_new
  85. # define lh_free OPENSSL_LH_free
  86. # define lh_insert OPENSSL_LH_insert
  87. # define lh_delete OPENSSL_LH_delete
  88. # define lh_retrieve OPENSSL_LH_retrieve
  89. # define lh_doall OPENSSL_LH_doall
  90. # define lh_doall_arg OPENSSL_LH_doall_arg
  91. # define lh_strhash OPENSSL_LH_strhash
  92. # define lh_num_items OPENSSL_LH_num_items
  93. # ifndef OPENSSL_NO_STDIO
  94. # define lh_stats OPENSSL_LH_stats
  95. # define lh_node_stats OPENSSL_LH_node_stats
  96. # define lh_node_usage_stats OPENSSL_LH_node_usage_stats
  97. # endif
  98. # define lh_stats_bio OPENSSL_LH_stats_bio
  99. # define lh_node_stats_bio OPENSSL_LH_node_stats_bio
  100. # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio
  101. # endif
  102. /* Type checking... */
  103. # define LHASH_OF(type) struct lhash_st_##type
  104. # define DEFINE_LHASH_OF(type) \
  105. LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
  106. static ossl_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \
  107. int (*cfn)(const type *, const type *)) \
  108. { \
  109. return (LHASH_OF(type) *) \
  110. OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
  111. } \
  112. static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
  113. { \
  114. OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
  115. } \
  116. static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
  117. { \
  118. return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
  119. } \
  120. static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
  121. { \
  122. return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
  123. } \
  124. static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
  125. { \
  126. return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
  127. } \
  128. static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
  129. { \
  130. return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
  131. } \
  132. static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
  133. { \
  134. return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
  135. } \
  136. static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  137. { \
  138. OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
  139. } \
  140. static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  141. { \
  142. OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
  143. } \
  144. static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  145. { \
  146. OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
  147. } \
  148. static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
  149. { \
  150. return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
  151. } \
  152. static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
  153. { \
  154. OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
  155. } \
  156. static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
  157. void (*doall)(type *)) \
  158. { \
  159. OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
  160. } \
  161. LHASH_OF(type)
  162. #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
  163. int_implement_lhash_doall(type, argtype, const type)
  164. #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
  165. int_implement_lhash_doall(type, argtype, type)
  166. #define int_implement_lhash_doall(type, argtype, cbargtype) \
  167. static ossl_unused ossl_inline void \
  168. lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
  169. void (*fn)(cbargtype *, argtype *), \
  170. argtype *arg) \
  171. { \
  172. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
  173. } \
  174. LHASH_OF(type)
  175. DEFINE_LHASH_OF(OPENSSL_STRING);
  176. # ifdef _MSC_VER
  177. /*
  178. * push and pop this warning:
  179. * warning C4090: 'function': different 'const' qualifiers
  180. */
  181. # pragma warning (push)
  182. # pragma warning (disable: 4090)
  183. # endif
  184. DEFINE_LHASH_OF(OPENSSL_CSTRING);
  185. # ifdef _MSC_VER
  186. # pragma warning (pop)
  187. # endif
  188. /*
  189. * If called without higher optimization (min. -xO3) the Oracle Developer
  190. * Studio compiler generates code for the defined (static inline) functions
  191. * above.
  192. * This would later lead to the linker complaining about missing symbols when
  193. * this header file is included but the resulting object is not linked against
  194. * the Crypto library (openssl#6912).
  195. */
  196. # ifdef __SUNPRO_C
  197. # pragma weak OPENSSL_LH_new
  198. # pragma weak OPENSSL_LH_free
  199. # pragma weak OPENSSL_LH_insert
  200. # pragma weak OPENSSL_LH_delete
  201. # pragma weak OPENSSL_LH_retrieve
  202. # pragma weak OPENSSL_LH_error
  203. # pragma weak OPENSSL_LH_num_items
  204. # pragma weak OPENSSL_LH_node_stats_bio
  205. # pragma weak OPENSSL_LH_node_usage_stats_bio
  206. # pragma weak OPENSSL_LH_stats_bio
  207. # pragma weak OPENSSL_LH_get_down_load
  208. # pragma weak OPENSSL_LH_set_down_load
  209. # pragma weak OPENSSL_LH_doall
  210. # pragma weak OPENSSL_LH_doall_arg
  211. # endif /* __SUNPRO_C */
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif