1 | /* |
2 | * Copyright 1995-2021 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 | |
12 | /* |
13 | * Header for dynamic hash table routines Author - Eric Young |
14 | */ |
15 | |
16 | #ifndef OPENSSL_LHASH_H |
17 | # define OPENSSL_LHASH_H |
18 | # pragma once |
19 | |
20 | # include <openssl/macros.h> |
21 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
22 | # define |
23 | # endif |
24 | |
25 | # include <openssl/e_os2.h> |
26 | # include <openssl/bio.h> |
27 | |
28 | #ifdef __cplusplus |
29 | extern "C" { |
30 | #endif |
31 | |
32 | typedef struct lhash_node_st OPENSSL_LH_NODE; |
33 | typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *); |
34 | typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *); |
35 | typedef void (*OPENSSL_LH_DOALL_FUNC) (void *); |
36 | typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *); |
37 | typedef struct lhash_st OPENSSL_LHASH; |
38 | |
39 | /* |
40 | * Macros for declaring and implementing type-safe wrappers for LHASH |
41 | * callbacks. This way, callbacks can be provided to LHASH structures without |
42 | * function pointer casting and the macro-defined callbacks provide |
43 | * per-variable casting before deferring to the underlying type-specific |
44 | * callbacks. NB: It is possible to place a "static" in front of both the |
45 | * DECLARE and IMPLEMENT macros if the functions are strictly internal. |
46 | */ |
47 | |
48 | /* First: "hash" functions */ |
49 | # define DECLARE_LHASH_HASH_FN(name, o_type) \ |
50 | unsigned long name##_LHASH_HASH(const void *); |
51 | # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \ |
52 | unsigned long name##_LHASH_HASH(const void *arg) { \ |
53 | const o_type *a = arg; \ |
54 | return name##_hash(a); } |
55 | # define LHASH_HASH_FN(name) name##_LHASH_HASH |
56 | |
57 | /* Second: "compare" functions */ |
58 | # define DECLARE_LHASH_COMP_FN(name, o_type) \ |
59 | int name##_LHASH_COMP(const void *, const void *); |
60 | # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \ |
61 | int name##_LHASH_COMP(const void *arg1, const void *arg2) { \ |
62 | const o_type *a = arg1; \ |
63 | const o_type *b = arg2; \ |
64 | return name##_cmp(a,b); } |
65 | # define LHASH_COMP_FN(name) name##_LHASH_COMP |
66 | |
67 | /* Fourth: "doall_arg" functions */ |
68 | # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ |
69 | void name##_LHASH_DOALL_ARG(void *, void *); |
70 | # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \ |
71 | void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \ |
72 | o_type *a = arg1; \ |
73 | a_type *b = arg2; \ |
74 | name##_doall_arg(a, b); } |
75 | # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG |
76 | |
77 | |
78 | # define LH_LOAD_MULT 256 |
79 | |
80 | int OPENSSL_LH_error(OPENSSL_LHASH *lh); |
81 | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c); |
82 | void OPENSSL_LH_free(OPENSSL_LHASH *lh); |
83 | void OPENSSL_LH_flush(OPENSSL_LHASH *lh); |
84 | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data); |
85 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data); |
86 | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data); |
87 | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func); |
88 | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg); |
89 | unsigned long OPENSSL_LH_strhash(const char *c); |
90 | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh); |
91 | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh); |
92 | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load); |
93 | |
94 | # ifndef OPENSSL_NO_STDIO |
95 | void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp); |
96 | void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp); |
97 | void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp); |
98 | # endif |
99 | void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out); |
100 | void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out); |
101 | void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out); |
102 | |
103 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
104 | # define _LHASH OPENSSL_LHASH |
105 | # define LHASH_NODE OPENSSL_LH_NODE |
106 | # define lh_error OPENSSL_LH_error |
107 | # define lh_new OPENSSL_LH_new |
108 | # define lh_free OPENSSL_LH_free |
109 | # define lh_insert OPENSSL_LH_insert |
110 | # define lh_delete OPENSSL_LH_delete |
111 | # define lh_retrieve OPENSSL_LH_retrieve |
112 | # define lh_doall OPENSSL_LH_doall |
113 | # define lh_doall_arg OPENSSL_LH_doall_arg |
114 | # define lh_strhash OPENSSL_LH_strhash |
115 | # define lh_num_items OPENSSL_LH_num_items |
116 | # ifndef OPENSSL_NO_STDIO |
117 | # define lh_stats OPENSSL_LH_stats |
118 | # define lh_node_stats OPENSSL_LH_node_stats |
119 | # define lh_node_usage_stats OPENSSL_LH_node_usage_stats |
120 | # endif |
121 | # define lh_stats_bio OPENSSL_LH_stats_bio |
122 | # define lh_node_stats_bio OPENSSL_LH_node_stats_bio |
123 | # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio |
124 | # endif |
125 | |
126 | /* Type checking... */ |
127 | |
128 | # define LHASH_OF(type) struct lhash_st_##type |
129 | |
130 | /* Helper macro for internal use */ |
131 | # define DEFINE_LHASH_OF_INTERNAL(type) \ |
132 | LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \ |
133 | typedef int (*lh_##type##_compfunc)(const type *a, const type *b); \ |
134 | typedef unsigned long (*lh_##type##_hashfunc)(const type *a); \ |
135 | typedef void (*lh_##type##_doallfunc)(type *a); \ |
136 | static ossl_unused ossl_inline type *ossl_check_##type##_lh_plain_type(type *ptr) \ |
137 | { \ |
138 | return ptr; \ |
139 | } \ |
140 | static ossl_unused ossl_inline const type *ossl_check_const_##type##_lh_plain_type(const type *ptr) \ |
141 | { \ |
142 | return ptr; \ |
143 | } \ |
144 | static ossl_unused ossl_inline const OPENSSL_LHASH *ossl_check_const_##type##_lh_type(const LHASH_OF(type) *lh) \ |
145 | { \ |
146 | return (const OPENSSL_LHASH *)lh; \ |
147 | } \ |
148 | static ossl_unused ossl_inline OPENSSL_LHASH *ossl_check_##type##_lh_type(LHASH_OF(type) *lh) \ |
149 | { \ |
150 | return (OPENSSL_LHASH *)lh; \ |
151 | } \ |
152 | static ossl_unused ossl_inline OPENSSL_LH_COMPFUNC ossl_check_##type##_lh_compfunc_type(lh_##type##_compfunc cmp) \ |
153 | { \ |
154 | return (OPENSSL_LH_COMPFUNC)cmp; \ |
155 | } \ |
156 | static ossl_unused ossl_inline OPENSSL_LH_HASHFUNC ossl_check_##type##_lh_hashfunc_type(lh_##type##_hashfunc hfn) \ |
157 | { \ |
158 | return (OPENSSL_LH_HASHFUNC)hfn; \ |
159 | } \ |
160 | static ossl_unused ossl_inline OPENSSL_LH_DOALL_FUNC ossl_check_##type##_lh_doallfunc_type(lh_##type##_doallfunc dfn) \ |
161 | { \ |
162 | return (OPENSSL_LH_DOALL_FUNC)dfn; \ |
163 | } \ |
164 | LHASH_OF(type) |
165 | |
166 | # define DEFINE_LHASH_OF(type) \ |
167 | LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \ |
168 | static ossl_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \ |
169 | int (*cfn)(const type *, const type *)) \ |
170 | { \ |
171 | return (LHASH_OF(type) *) \ |
172 | OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \ |
173 | } \ |
174 | static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \ |
175 | { \ |
176 | OPENSSL_LH_free((OPENSSL_LHASH *)lh); \ |
177 | } \ |
178 | static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \ |
179 | { \ |
180 | OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \ |
181 | } \ |
182 | static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \ |
183 | { \ |
184 | return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \ |
185 | } \ |
186 | static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \ |
187 | { \ |
188 | return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \ |
189 | } \ |
190 | static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \ |
191 | { \ |
192 | return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \ |
193 | } \ |
194 | static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \ |
195 | { \ |
196 | return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \ |
197 | } \ |
198 | static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \ |
199 | { \ |
200 | return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \ |
201 | } \ |
202 | static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ |
203 | { \ |
204 | OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \ |
205 | } \ |
206 | static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ |
207 | { \ |
208 | OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \ |
209 | } \ |
210 | static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \ |
211 | { \ |
212 | OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \ |
213 | } \ |
214 | static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \ |
215 | { \ |
216 | return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \ |
217 | } \ |
218 | static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \ |
219 | { \ |
220 | OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \ |
221 | } \ |
222 | static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \ |
223 | void (*doall)(type *)) \ |
224 | { \ |
225 | OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \ |
226 | } \ |
227 | static ossl_unused ossl_inline void lh_##type##_doall_arg(LHASH_OF(type) *lh, \ |
228 | void (*doallarg)(type *, void *), \ |
229 | void *arg) \ |
230 | { \ |
231 | OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, \ |
232 | (OPENSSL_LH_DOALL_FUNCARG)doallarg, arg); \ |
233 | } \ |
234 | LHASH_OF(type) |
235 | |
236 | #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \ |
237 | int_implement_lhash_doall(type, argtype, const type) |
238 | |
239 | #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \ |
240 | int_implement_lhash_doall(type, argtype, type) |
241 | |
242 | #define int_implement_lhash_doall(type, argtype, cbargtype) \ |
243 | static ossl_unused ossl_inline void \ |
244 | lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \ |
245 | void (*fn)(cbargtype *, argtype *), \ |
246 | argtype *arg) \ |
247 | { \ |
248 | OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \ |
249 | } \ |
250 | LHASH_OF(type) |
251 | |
252 | DEFINE_LHASH_OF_INTERNAL(OPENSSL_STRING); |
253 | #define lh_OPENSSL_STRING_new(hfn, cmp) ((LHASH_OF(OPENSSL_STRING) *)OPENSSL_LH_new(ossl_check_OPENSSL_STRING_lh_hashfunc_type(hfn), ossl_check_OPENSSL_STRING_lh_compfunc_type(cmp))) |
254 | #define lh_OPENSSL_STRING_free(lh) OPENSSL_LH_free(ossl_check_OPENSSL_STRING_lh_type(lh)) |
255 | #define lh_OPENSSL_STRING_flush(lh) OPENSSL_LH_flush(ossl_check_OPENSSL_STRING_lh_type(lh)) |
256 | #define lh_OPENSSL_STRING_insert(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_insert(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_OPENSSL_STRING_lh_plain_type(ptr))) |
257 | #define lh_OPENSSL_STRING_delete(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_delete(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_const_OPENSSL_STRING_lh_plain_type(ptr))) |
258 | #define lh_OPENSSL_STRING_retrieve(lh, ptr) ((OPENSSL_STRING *)OPENSSL_LH_retrieve(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_const_OPENSSL_STRING_lh_plain_type(ptr))) |
259 | #define lh_OPENSSL_STRING_error(lh) OPENSSL_LH_error(ossl_check_OPENSSL_STRING_lh_type(lh)) |
260 | #define lh_OPENSSL_STRING_num_items(lh) OPENSSL_LH_num_items(ossl_check_OPENSSL_STRING_lh_type(lh)) |
261 | #define lh_OPENSSL_STRING_node_stats_bio(lh, out) OPENSSL_LH_node_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out) |
262 | #define lh_OPENSSL_STRING_node_usage_stats_bio(lh, out) OPENSSL_LH_node_usage_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out) |
263 | #define lh_OPENSSL_STRING_stats_bio(lh, out) OPENSSL_LH_stats_bio(ossl_check_const_OPENSSL_STRING_lh_type(lh), out) |
264 | #define lh_OPENSSL_STRING_get_down_load(lh) OPENSSL_LH_get_down_load(ossl_check_OPENSSL_STRING_lh_type(lh)) |
265 | #define lh_OPENSSL_STRING_set_down_load(lh, dl) OPENSSL_LH_set_down_load(ossl_check_OPENSSL_STRING_lh_type(lh), dl) |
266 | #define lh_OPENSSL_STRING_doall(lh, dfn) OPENSSL_LH_doall(ossl_check_OPENSSL_STRING_lh_type(lh), ossl_check_OPENSSL_STRING_lh_doallfunc_type(dfn)) |
267 | DEFINE_LHASH_OF_INTERNAL(OPENSSL_CSTRING); |
268 | #define lh_OPENSSL_CSTRING_new(hfn, cmp) ((LHASH_OF(OPENSSL_CSTRING) *)OPENSSL_LH_new(ossl_check_OPENSSL_CSTRING_lh_hashfunc_type(hfn), ossl_check_OPENSSL_CSTRING_lh_compfunc_type(cmp))) |
269 | #define lh_OPENSSL_CSTRING_free(lh) OPENSSL_LH_free(ossl_check_OPENSSL_CSTRING_lh_type(lh)) |
270 | #define lh_OPENSSL_CSTRING_flush(lh) OPENSSL_LH_flush(ossl_check_OPENSSL_CSTRING_lh_type(lh)) |
271 | #define lh_OPENSSL_CSTRING_insert(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_insert(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_OPENSSL_CSTRING_lh_plain_type(ptr))) |
272 | #define lh_OPENSSL_CSTRING_delete(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_delete(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_const_OPENSSL_CSTRING_lh_plain_type(ptr))) |
273 | #define lh_OPENSSL_CSTRING_retrieve(lh, ptr) ((OPENSSL_CSTRING *)OPENSSL_LH_retrieve(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_const_OPENSSL_CSTRING_lh_plain_type(ptr))) |
274 | #define lh_OPENSSL_CSTRING_error(lh) OPENSSL_LH_error(ossl_check_OPENSSL_CSTRING_lh_type(lh)) |
275 | #define lh_OPENSSL_CSTRING_num_items(lh) OPENSSL_LH_num_items(ossl_check_OPENSSL_CSTRING_lh_type(lh)) |
276 | #define lh_OPENSSL_CSTRING_node_stats_bio(lh, out) OPENSSL_LH_node_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out) |
277 | #define lh_OPENSSL_CSTRING_node_usage_stats_bio(lh, out) OPENSSL_LH_node_usage_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out) |
278 | #define lh_OPENSSL_CSTRING_stats_bio(lh, out) OPENSSL_LH_stats_bio(ossl_check_const_OPENSSL_CSTRING_lh_type(lh), out) |
279 | #define lh_OPENSSL_CSTRING_get_down_load(lh) OPENSSL_LH_get_down_load(ossl_check_OPENSSL_CSTRING_lh_type(lh)) |
280 | #define lh_OPENSSL_CSTRING_set_down_load(lh, dl) OPENSSL_LH_set_down_load(ossl_check_OPENSSL_CSTRING_lh_type(lh), dl) |
281 | #define lh_OPENSSL_CSTRING_doall(lh, dfn) OPENSSL_LH_doall(ossl_check_OPENSSL_CSTRING_lh_type(lh), ossl_check_OPENSSL_CSTRING_lh_doallfunc_type(dfn)) |
282 | |
283 | |
284 | #ifdef __cplusplus |
285 | } |
286 | #endif |
287 | |
288 | #endif |
289 | |