Invoke tear_down when exiting test_encode_tls_sct() prematurely
[openssl.git] / include / openssl / lhash.h
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 /*
11  * Header for dynamic hash table routines Author - Eric Young
12  */
13
14 #ifndef OPENSSL_LHASH_H
15 # define OPENSSL_LHASH_H
16 # pragma once
17
18 # include <openssl/macros.h>
19 # if !OPENSSL_API_3
20 #  define HEADER_LHASH_H
21 # endif
22
23 # include <openssl/e_os2.h>
24 # include <openssl/bio.h>
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
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;
36
37 /*
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.
44  */
45
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
54
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
64
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) { \
70                 o_type *a = arg1; \
71                 a_type *b = arg2; \
72                 name##_doall_arg(a, b); }
73 # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
74
75
76 # define LH_LOAD_MULT    256
77
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);
91
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);
96 # endif
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);
100
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
118 #  endif
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
122 # endif
123
124 /* Type checking... */
125
126 # define LHASH_OF(type) struct lhash_st_##type
127
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 *)) \
133     { \
134         return (LHASH_OF(type) *) \
135             OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
136     } \
137     static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
138     { \
139         OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
140     } \
141     static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
142     { \
143         OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
144     } \
145     static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
146     { \
147         return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
148     } \
149     static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
150     { \
151         return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
152     } \
153     static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
154     { \
155         return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
156     } \
157     static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
158     { \
159         return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
160     } \
161     static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
162     { \
163         return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
164     } \
165     static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
166     { \
167         OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
168     } \
169     static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
170     { \
171         OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
172     } \
173     static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
174     { \
175         OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
176     } \
177     static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
178     { \
179         return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
180     } \
181     static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
182     { \
183         OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
184     } \
185     static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
186                                                           void (*doall)(type *)) \
187     { \
188         OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
189     } \
190     LHASH_OF(type)
191
192 #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
193     int_implement_lhash_doall(type, argtype, const type)
194
195 #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
196     int_implement_lhash_doall(type, argtype, type)
197
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 *), \
202                                    argtype *arg) \
203     { \
204         OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
205     } \
206     LHASH_OF(type)
207
208 DEFINE_LHASH_OF(OPENSSL_STRING);
209 # ifdef _MSC_VER
210 /*
211  * push and pop this warning:
212  *   warning C4090: 'function': different 'const' qualifiers
213  */
214 #  pragma warning (push)
215 #  pragma warning (disable: 4090)
216 # endif
217
218 DEFINE_LHASH_OF(OPENSSL_CSTRING);
219
220 # ifdef _MSC_VER
221 #  pragma warning (pop)
222 # endif
223
224 /*
225  * If called without higher optimization (min. -xO3) the Oracle Developer
226  * Studio compiler generates code for the defined (static inline) functions
227  * above.
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).
231  */
232 # ifdef __SUNPRO_C
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 */
248
249 #ifdef  __cplusplus
250 }
251 #endif
252
253 #endif