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