2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Header for dynamic hash table routines Author - Eric Young
14 #ifndef OPENSSL_LHASH_H
15 # define OPENSSL_LHASH_H
18 # include <openssl/macros.h>
19 # ifndef OPENSSL_NO_DEPRECATED_3_0
20 # define HEADER_LHASH_H
23 # include <openssl/e_os2.h>
24 # include <openssl/bio.h>
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;
38 * Macros for declaring and implementing type-safe wrappers for LHASH
39 * callbacks. This way, callbacks can be provided to LHASH structures without
40 * function pointer casting and the macro-defined callbacks provide
41 * per-variable casting before deferring to the underlying type-specific
42 * callbacks. NB: It is possible to place a "static" in front of both the
43 * DECLARE and IMPLEMENT macros if the functions are strictly internal.
46 /* First: "hash" functions */
47 # define DECLARE_LHASH_HASH_FN(name, o_type) \
48 unsigned long name##_LHASH_HASH(const void *);
49 # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
50 unsigned long name##_LHASH_HASH(const void *arg) { \
51 const o_type *a = arg; \
52 return name##_hash(a); }
53 # define LHASH_HASH_FN(name) name##_LHASH_HASH
55 /* Second: "compare" functions */
56 # define DECLARE_LHASH_COMP_FN(name, o_type) \
57 int name##_LHASH_COMP(const void *, const void *);
58 # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
59 int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
60 const o_type *a = arg1; \
61 const o_type *b = arg2; \
62 return name##_cmp(a,b); }
63 # define LHASH_COMP_FN(name) name##_LHASH_COMP
65 /* Fourth: "doall_arg" functions */
66 # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
67 void name##_LHASH_DOALL_ARG(void *, void *);
68 # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
69 void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
72 name##_doall_arg(a, b); }
73 # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
76 # define LH_LOAD_MULT 256
78 int OPENSSL_LH_error(OPENSSL_LHASH *lh);
79 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
80 void OPENSSL_LH_free(OPENSSL_LHASH *lh);
81 void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
82 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
83 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
84 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
85 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
86 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
87 unsigned long OPENSSL_LH_strhash(const char *c);
88 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh);
89 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh);
90 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load);
92 # ifndef OPENSSL_NO_STDIO
93 void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp);
94 void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp);
95 void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp);
97 void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
98 void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
99 void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
101 # if !OPENSSL_API_1_1_0
102 # define _LHASH OPENSSL_LHASH
103 # define LHASH_NODE OPENSSL_LH_NODE
104 # define lh_error OPENSSL_LH_error
105 # define lh_new OPENSSL_LH_new
106 # define lh_free OPENSSL_LH_free
107 # define lh_insert OPENSSL_LH_insert
108 # define lh_delete OPENSSL_LH_delete
109 # define lh_retrieve OPENSSL_LH_retrieve
110 # define lh_doall OPENSSL_LH_doall
111 # define lh_doall_arg OPENSSL_LH_doall_arg
112 # define lh_strhash OPENSSL_LH_strhash
113 # define lh_num_items OPENSSL_LH_num_items
114 # ifndef OPENSSL_NO_STDIO
115 # define lh_stats OPENSSL_LH_stats
116 # define lh_node_stats OPENSSL_LH_node_stats
117 # define lh_node_usage_stats OPENSSL_LH_node_usage_stats
119 # define lh_stats_bio OPENSSL_LH_stats_bio
120 # define lh_node_stats_bio OPENSSL_LH_node_stats_bio
121 # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio
124 /* Type checking... */
126 # define LHASH_OF(type) struct lhash_st_##type
128 # define DEFINE_LHASH_OF(type) \
129 LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
130 static ossl_inline LHASH_OF(type) * \
131 lh_##type##_new(unsigned long (*hfn)(const type *), \
132 int (*cfn)(const type *, const type *)) \
134 return (LHASH_OF(type) *) \
135 OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
137 static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
139 OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
141 static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
143 OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
145 static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
147 return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
149 static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
151 return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
153 static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
155 return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
157 static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
159 return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
161 static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
163 return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
165 static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
167 OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
169 static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
171 OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
173 static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
175 OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
177 static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
179 return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
181 static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
183 OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
185 static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
186 void (*doall)(type *)) \
188 OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
192 #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
193 int_implement_lhash_doall(type, argtype, const type)
195 #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
196 int_implement_lhash_doall(type, argtype, type)
198 #define int_implement_lhash_doall(type, argtype, cbargtype) \
199 static ossl_unused ossl_inline void \
200 lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
201 void (*fn)(cbargtype *, argtype *), \
204 OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
208 DEFINE_LHASH_OF(OPENSSL_STRING);
211 * push and pop this warning:
212 * warning C4090: 'function': different 'const' qualifiers
214 # pragma warning (push)
215 # pragma warning (disable: 4090)
218 DEFINE_LHASH_OF(OPENSSL_CSTRING);
221 # pragma warning (pop)
225 * If called without higher optimization (min. -xO3) the Oracle Developer
226 * Studio compiler generates code for the defined (static inline) functions
228 * This would later lead to the linker complaining about missing symbols when
229 * this header file is included but the resulting object is not linked against
230 * the Crypto library (openssl#6912).
233 # pragma weak OPENSSL_LH_new
234 # pragma weak OPENSSL_LH_free
235 # pragma weak OPENSSL_LH_insert
236 # pragma weak OPENSSL_LH_delete
237 # pragma weak OPENSSL_LH_retrieve
238 # pragma weak OPENSSL_LH_error
239 # pragma weak OPENSSL_LH_num_items
240 # pragma weak OPENSSL_LH_node_stats_bio
241 # pragma weak OPENSSL_LH_node_usage_stats_bio
242 # pragma weak OPENSSL_LH_stats_bio
243 # pragma weak OPENSSL_LH_get_down_load
244 # pragma weak OPENSSL_LH_set_down_load
245 # pragma weak OPENSSL_LH_doall
246 # pragma weak OPENSSL_LH_doall_arg
247 # endif /* __SUNPRO_C */