Adding missing NULL pointer check
[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 /* Helper macro for internal use */
129 # define DEFINE_LHASH_OF_INTERNAL(type) \
130     LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
131     typedef int (*lh_##type##_compfunc)(const type *a, const type *b); \
132     typedef unsigned long (*lh_##type##_hashfunc)(const type *a); \
133     typedef void (*lh_##type##_doallfunc)(type *a); \
134     static ossl_unused ossl_inline type *ossl_check_##type##_lh_plain_type(type *ptr) \
135     { \
136         return ptr; \
137     } \
138     static ossl_unused ossl_inline const type *ossl_check_const_##type##_lh_plain_type(const type *ptr) \
139     { \
140         return ptr; \
141     } \
142     static ossl_unused ossl_inline const OPENSSL_LHASH *ossl_check_const_##type##_lh_type(const LHASH_OF(type) *lh) \
143     { \
144         return (const OPENSSL_LHASH *)lh; \
145     } \
146     static ossl_unused ossl_inline OPENSSL_LHASH *ossl_check_##type##_lh_type(LHASH_OF(type) *lh) \
147     { \
148         return (OPENSSL_LHASH *)lh; \
149     } \
150     static ossl_unused ossl_inline OPENSSL_LH_COMPFUNC ossl_check_##type##_lh_compfunc_type(lh_##type##_compfunc cmp) \
151     { \
152         return (OPENSSL_LH_COMPFUNC)cmp; \
153     } \
154     static ossl_unused ossl_inline OPENSSL_LH_HASHFUNC ossl_check_##type##_lh_hashfunc_type(lh_##type##_hashfunc hfn) \
155     { \
156         return (OPENSSL_LH_HASHFUNC)hfn; \
157     } \
158     static ossl_unused ossl_inline OPENSSL_LH_DOALL_FUNC ossl_check_##type##_lh_doallfunc_type(lh_##type##_doallfunc dfn) \
159     { \
160         return (OPENSSL_LH_DOALL_FUNC)dfn; \
161     } \
162     LHASH_OF(type)
163
164 # define DEFINE_LHASH_OF(type) \
165     LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
166     static ossl_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \
167                                                                    int (*cfn)(const type *, const type *)) \
168     { \
169         return (LHASH_OF(type) *) \
170             OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
171     } \
172     static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
173     { \
174         OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
175     } \
176     static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
177     { \
178         OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
179     } \
180     static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
181     { \
182         return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
183     } \
184     static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
185     { \
186         return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
187     } \
188     static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
189     { \
190         return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
191     } \
192     static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
193     { \
194         return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
195     } \
196     static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
197     { \
198         return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
199     } \
200     static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
201     { \
202         OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
203     } \
204     static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
205     { \
206         OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
207     } \
208     static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
209     { \
210         OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
211     } \
212     static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
213     { \
214         return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
215     } \
216     static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
217     { \
218         OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
219     } \
220     static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
221                                                           void (*doall)(type *)) \
222     { \
223         OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
224     } \
225     LHASH_OF(type)
226
227 #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
228     int_implement_lhash_doall(type, argtype, const type)
229
230 #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
231     int_implement_lhash_doall(type, argtype, type)
232
233 #define int_implement_lhash_doall(type, argtype, cbargtype) \
234     static ossl_unused ossl_inline void \
235         lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
236                                    void (*fn)(cbargtype *, argtype *), \
237                                    argtype *arg) \
238     { \
239         OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
240     } \
241     LHASH_OF(type)
242
243 DEFINE_LHASH_OF(OPENSSL_STRING);
244 # ifdef _MSC_VER
245 /*
246  * push and pop this warning:
247  *   warning C4090: 'function': different 'const' qualifiers
248  */
249 #  pragma warning (push)
250 #  pragma warning (disable: 4090)
251 # endif
252
253 DEFINE_LHASH_OF(OPENSSL_CSTRING);
254
255 # ifdef _MSC_VER
256 #  pragma warning (pop)
257 # endif
258
259 #ifdef  __cplusplus
260 }
261 #endif
262
263 #endif