check_chain_extensions(): Add check that Basic Constraints of CA cert are marked...
[openssl.git] / include / openssl / lhash.h
1 /*
2  * Copyright 1995-2020 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 # ifndef OPENSSL_NO_DEPRECATED_3_0
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 # 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
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_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \
131                                                                    int (*cfn)(const type *, const type *)) \
132     { \
133         return (LHASH_OF(type) *) \
134             OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
135     } \
136     static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
137     { \
138         OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
139     } \
140     static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
141     { \
142         OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
143     } \
144     static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
145     { \
146         return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
147     } \
148     static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
149     { \
150         return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
151     } \
152     static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
153     { \
154         return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
155     } \
156     static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
157     { \
158         return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
159     } \
160     static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
161     { \
162         return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
163     } \
164     static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
165     { \
166         OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
167     } \
168     static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
169     { \
170         OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
171     } \
172     static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
173     { \
174         OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
175     } \
176     static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
177     { \
178         return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
179     } \
180     static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
181     { \
182         OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
183     } \
184     static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
185                                                           void (*doall)(type *)) \
186     { \
187         OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
188     } \
189     LHASH_OF(type)
190
191 #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
192     int_implement_lhash_doall(type, argtype, const type)
193
194 #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
195     int_implement_lhash_doall(type, argtype, type)
196
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 *), \
201                                    argtype *arg) \
202     { \
203         OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
204     } \
205     LHASH_OF(type)
206
207 DEFINE_LHASH_OF(OPENSSL_STRING);
208 # ifdef _MSC_VER
209 /*
210  * push and pop this warning:
211  *   warning C4090: 'function': different 'const' qualifiers
212  */
213 #  pragma warning (push)
214 #  pragma warning (disable: 4090)
215 # endif
216
217 DEFINE_LHASH_OF(OPENSSL_CSTRING);
218
219 # ifdef _MSC_VER
220 #  pragma warning (pop)
221 # endif
222
223 /*
224  * If called without higher optimization (min. -xO3) the Oracle Developer
225  * Studio compiler generates code for the defined (static inline) functions
226  * above.
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).
230  */
231 # ifdef __SUNPRO_C
232 #  pragma weak OPENSSL_LH_new
233 #  pragma weak OPENSSL_LH_flush
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