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 # ifndef OPENSSL_NO_DEPRECATED_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_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \
131 int (*cfn)(const type *, const type *)) \
133 return (LHASH_OF(type) *) \
134 OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
136 static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
138 OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
140 static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
142 OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
144 static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
146 return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
148 static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
150 return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
152 static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
154 return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
156 static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
158 return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
160 static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
162 return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
164 static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
166 OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
168 static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
170 OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
172 static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
174 OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
176 static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
178 return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
180 static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
182 OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
184 static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
185 void (*doall)(type *)) \
187 OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
191 #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
192 int_implement_lhash_doall(type, argtype, const type)
194 #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
195 int_implement_lhash_doall(type, argtype, type)
197 #define int_implement_lhash_doall(type, argtype, cbargtype) \
198 static ossl_unused ossl_inline void \
199 lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
200 void (*fn)(cbargtype *, argtype *), \
203 OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
207 DEFINE_LHASH_OF(OPENSSL_STRING);
210 * push and pop this warning:
211 * warning C4090: 'function': different 'const' qualifiers
213 # pragma warning (push)
214 # pragma warning (disable: 4090)
217 DEFINE_LHASH_OF(OPENSSL_CSTRING);
220 # pragma warning (pop)
224 * If called without higher optimization (min. -xO3) the Oracle Developer
225 * Studio compiler generates code for the defined (static inline) functions
227 * This would later lead to the linker complaining about missing symbols when
228 * this header file is included but the resulting object is not linked against
229 * the Crypto library (openssl#6912).
232 # pragma weak OPENSSL_LH_new
233 # pragma weak OPENSSL_LH_free
234 # pragma weak OPENSSL_LH_insert
235 # pragma weak OPENSSL_LH_delete
236 # pragma weak OPENSSL_LH_retrieve
237 # pragma weak OPENSSL_LH_error
238 # pragma weak OPENSSL_LH_num_items
239 # pragma weak OPENSSL_LH_node_stats_bio
240 # pragma weak OPENSSL_LH_node_usage_stats_bio
241 # pragma weak OPENSSL_LH_stats_bio
242 # pragma weak OPENSSL_LH_get_down_load
243 # pragma weak OPENSSL_LH_set_down_load
244 # pragma weak OPENSSL_LH_doall
245 # pragma weak OPENSSL_LH_doall_arg
246 # endif /* __SUNPRO_C */