Coverage Report

Created: 2023-06-07 07:11

/src/boringssl/crypto/lhash/internal.h
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#ifndef OPENSSL_HEADER_LHASH_INTERNAL_H
58
#define OPENSSL_HEADER_LHASH_INTERNAL_H
59
60
#include <openssl/lhash.h>
61
62
#if defined(__cplusplus)
63
extern "C" {
64
#endif
65
66
67
// lhash is a traditional, chaining hash table that automatically expands and
68
// contracts as needed. One should not use the lh_* functions directly, rather
69
// use the type-safe macro wrappers:
70
//
71
// A hash table of a specific type of object has type |LHASH_OF(type)|. This
72
// can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed
73
// with |DECLARE_LHASH_OF(type)|. For example:
74
//
75
//   struct foo {
76
//     int bar;
77
//   };
78
//
79
//   DEFINE_LHASH_OF(struct foo)
80
//
81
// Although note that the hash table will contain /pointers/ to |foo|.
82
//
83
// A macro will be defined for each of the |OPENSSL_lh_*| functions below. For
84
// |LHASH_OF(foo)|, the macros would be |lh_foo_new|, |lh_foo_num_items| etc.
85
86
87
// lhash_cmp_func is a comparison function that returns a value equal, or not
88
// equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
89
// respectively. Note the difference between this and |stack_cmp_func| in that
90
// this takes pointers to the objects directly.
91
//
92
// This function's actual type signature is int (*)(const T*, const T*). The
93
// low-level |lh_*| functions will be passed a type-specific wrapper to call it
94
// correctly.
95
typedef int (*lhash_cmp_func)(const void *a, const void *b);
96
typedef int (*lhash_cmp_func_helper)(lhash_cmp_func func, const void *a,
97
                                     const void *b);
98
99
// lhash_hash_func is a function that maps an object to a uniformly distributed
100
// uint32_t.
101
//
102
// This function's actual type signature is uint32_t (*)(const T*). The
103
// low-level |lh_*| functions will be passed a type-specific wrapper to call it
104
// correctly.
105
typedef uint32_t (*lhash_hash_func)(const void *a);
106
typedef uint32_t (*lhash_hash_func_helper)(lhash_hash_func func, const void *a);
107
108
typedef struct lhash_st _LHASH;
109
110
// OPENSSL_lh_new returns a new, empty hash table or NULL on error.
111
OPENSSL_EXPORT _LHASH *OPENSSL_lh_new(lhash_hash_func hash,
112
                                      lhash_cmp_func comp);
113
114
// OPENSSL_lh_free frees the hash table itself but none of the elements. See
115
// |OPENSSL_lh_doall|.
116
OPENSSL_EXPORT void OPENSSL_lh_free(_LHASH *lh);
117
118
// OPENSSL_lh_num_items returns the number of items in |lh|.
119
OPENSSL_EXPORT size_t OPENSSL_lh_num_items(const _LHASH *lh);
120
121
// OPENSSL_lh_retrieve finds an element equal to |data| in the hash table and
122
// returns it. If no such element exists, it returns NULL.
123
OPENSSL_EXPORT void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data,
124
                                         lhash_hash_func_helper call_hash_func,
125
                                         lhash_cmp_func_helper call_cmp_func);
126
127
// OPENSSL_lh_retrieve_key finds an element matching |key|, given the specified
128
// hash and comparison function. This differs from |OPENSSL_lh_retrieve| in that
129
// the key may be a different type than the values stored in |lh|. |key_hash|
130
// and |cmp_key| must be compatible with the functions passed into
131
// |OPENSSL_lh_new|.
132
OPENSSL_EXPORT void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key,
133
                                             uint32_t key_hash,
134
                                             int (*cmp_key)(const void *key,
135
                                                            const void *value));
136
137
// OPENSSL_lh_insert inserts |data| into the hash table. If an existing element
138
// is equal to |data| (with respect to the comparison function) then |*old_data|
139
// will be set to that value and it will be replaced. Otherwise, or in the
140
// event of an error, |*old_data| will be set to NULL. It returns one on
141
// success or zero in the case of an allocation error.
142
OPENSSL_EXPORT int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data,
143
                                     lhash_hash_func_helper call_hash_func,
144
                                     lhash_cmp_func_helper call_cmp_func);
145
146
// OPENSSL_lh_delete removes an element equal to |data| from the hash table and
147
// returns it. If no such element is found, it returns NULL.
148
OPENSSL_EXPORT void *OPENSSL_lh_delete(_LHASH *lh, const void *data,
149
                                       lhash_hash_func_helper call_hash_func,
150
                                       lhash_cmp_func_helper call_cmp_func);
151
152
// OPENSSL_lh_doall_arg calls |func| on each element of the hash table and also
153
// passes |arg| as the second argument.
154
// TODO(fork): rename this
155
OPENSSL_EXPORT void OPENSSL_lh_doall_arg(_LHASH *lh,
156
                                         void (*func)(void *, void *),
157
                                         void *arg);
158
159
#define DEFINE_LHASH_OF(type)                                                  \
160
  /* We disable MSVC C4191 in this macro, which warns when pointers are cast   \
161
   * to the wrong type. While the cast itself is valid, it is often a bug      \
162
   * because calling it through the cast is UB. However, we never actually     \
163
   * call functions as |lhash_cmp_func|. The type is just a type-erased        \
164
   * function pointer. (C does not guarantee function pointers fit in          \
165
   * |void*|, and GCC will warn on this.) Thus we just disable the false       \
166
   * positive warning. */                                                      \
167
  OPENSSL_MSVC_PRAGMA(warning(push))                                           \
168
  OPENSSL_MSVC_PRAGMA(warning(disable : 4191))                                 \
169
                                                                               \
170
  DECLARE_LHASH_OF(type)                                                       \
171
                                                                               \
172
  typedef int (*lhash_##type##_cmp_func)(const type *, const type *);          \
173
  typedef uint32_t (*lhash_##type##_hash_func)(const type *);                  \
174
                                                                               \
175
  OPENSSL_INLINE int lh_##type##_call_cmp_func(lhash_cmp_func func,            \
176
0
                                               const void *a, const void *b) { \
177
0
    return ((lhash_##type##_cmp_func)func)((const type *)a, (const type *)b);  \
178
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_call_cmp_func(int (*)(void const*, void const*), void const*, void const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_call_cmp_func
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_call_cmp_func
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_call_cmp_func
Unexecuted instantiation: conf.c:lh_CONF_VALUE_call_cmp_func
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_call_cmp_func
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_call_cmp_func
179
                                                                               \
180
  OPENSSL_INLINE uint32_t lh_##type##_call_hash_func(lhash_hash_func func,     \
181
0
                                                     const void *a) {          \
182
0
    return ((lhash_##type##_hash_func)func)((const type *)a);                  \
183
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_call_hash_func(unsigned int (*)(void const*), void const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_call_hash_func
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_call_hash_func
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_call_hash_func
Unexecuted instantiation: conf.c:lh_CONF_VALUE_call_hash_func
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_call_hash_func
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_call_hash_func
184
                                                                               \
185
  OPENSSL_INLINE LHASH_OF(type) *lh_##type##_new(                              \
186
2
      lhash_##type##_hash_func hash, lhash_##type##_cmp_func comp) {           \
187
2
    return (LHASH_OF(type) *)OPENSSL_lh_new((lhash_hash_func)hash,             \
188
2
                                            (lhash_cmp_func)comp);             \
189
2
  }                                                                            \
lh_SSL_SESSION_new(unsigned int (*)(ssl_session_st const*), int (*)(ssl_session_st const*, ssl_session_st const*))
Line
Count
Source
186
2
      lhash_##type##_hash_func hash, lhash_##type##_cmp_func comp) {           \
187
2
    return (LHASH_OF(type) *)OPENSSL_lh_new((lhash_hash_func)hash,             \
188
2
                                            (lhash_cmp_func)comp);             \
189
2
  }                                                                            \
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_new
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_new
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_new
Unexecuted instantiation: conf.c:lh_CONF_VALUE_new
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_new
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_new
190
                                                                               \
191
0
  OPENSSL_INLINE void lh_##type##_free(LHASH_OF(type) *lh) {                   \
192
0
    OPENSSL_lh_free((_LHASH *)lh);                                             \
193
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_free(lhash_st_SSL_SESSION*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_free
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_free
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_free
Unexecuted instantiation: conf.c:lh_CONF_VALUE_free
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_free
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_free
194
                                                                               \
195
0
  OPENSSL_INLINE size_t lh_##type##_num_items(const LHASH_OF(type) *lh) {      \
196
0
    return OPENSSL_lh_num_items((const _LHASH *)lh);                           \
197
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_num_items(lhash_st_SSL_SESSION const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_num_items
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_num_items
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_num_items
Unexecuted instantiation: conf.c:lh_CONF_VALUE_num_items
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_num_items
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_num_items
198
                                                                               \
199
  OPENSSL_INLINE type *lh_##type##_retrieve(const LHASH_OF(type) *lh,          \
200
0
                                            const type *data) {                \
201
0
    return (type *)OPENSSL_lh_retrieve((const _LHASH *)lh, data,               \
202
0
                                       lh_##type##_call_hash_func,             \
203
0
                                       lh_##type##_call_cmp_func);             \
204
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_retrieve(lhash_st_SSL_SESSION const*, ssl_session_st const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_retrieve
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_retrieve
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_retrieve
Unexecuted instantiation: conf.c:lh_CONF_VALUE_retrieve
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_retrieve
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_retrieve
205
                                                                               \
206
  typedef struct {                                                             \
207
    int (*cmp_key)(const void *key, const type *value);                        \
208
    const void *key;                                                           \
209
  } LHASH_CMP_KEY_##type;                                                      \
210
                                                                               \
211
  OPENSSL_INLINE int lh_##type##_call_cmp_key(const void *key,                 \
212
0
                                              const void *value) {             \
213
0
    const LHASH_CMP_KEY_##type *cb = (const LHASH_CMP_KEY_##type *)key;        \
214
0
    return cb->cmp_key(cb->key, (const type *)value);                          \
215
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_call_cmp_key(void const*, void const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_call_cmp_key
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_call_cmp_key
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_call_cmp_key
Unexecuted instantiation: conf.c:lh_CONF_VALUE_call_cmp_key
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_call_cmp_key
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_call_cmp_key
216
                                                                               \
217
  OPENSSL_INLINE type *lh_##type##_retrieve_key(                               \
218
      const LHASH_OF(type) *lh, const void *key, uint32_t key_hash,            \
219
0
      int (*cmp_key)(const void *key, const type *value)) {                    \
220
0
    LHASH_CMP_KEY_##type cb = {cmp_key, key};                                  \
221
0
    return (type *)OPENSSL_lh_retrieve_key((const _LHASH *)lh, &cb, key_hash,  \
222
0
                                           lh_##type##_call_cmp_key);          \
223
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_retrieve_key(lhash_st_SSL_SESSION const*, void const*, unsigned int, int (*)(void const*, ssl_session_st const*))
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_retrieve_key
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_retrieve_key
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_retrieve_key
Unexecuted instantiation: conf.c:lh_CONF_VALUE_retrieve_key
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_retrieve_key
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_retrieve_key
224
                                                                               \
225
  OPENSSL_INLINE int lh_##type##_insert(LHASH_OF(type) *lh, type **old_data,   \
226
0
                                        type *data) {                          \
227
0
    void *old_data_void = NULL;                                                \
228
0
    int ret = OPENSSL_lh_insert((_LHASH *)lh, &old_data_void, data,            \
229
0
                                lh_##type##_call_hash_func,                    \
230
0
                                lh_##type##_call_cmp_func);                    \
231
0
    *old_data = (type *)old_data_void;                                         \
232
0
    return ret;                                                                \
233
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_insert(lhash_st_SSL_SESSION*, ssl_session_st**, ssl_session_st*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_insert
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_insert
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_insert
Unexecuted instantiation: conf.c:lh_CONF_VALUE_insert
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_insert
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_insert
234
                                                                               \
235
  OPENSSL_INLINE type *lh_##type##_delete(LHASH_OF(type) *lh,                  \
236
0
                                          const type *data) {                  \
237
0
    return (type *)OPENSSL_lh_delete((_LHASH *)lh, data,                       \
238
0
                                     lh_##type##_call_hash_func,               \
239
0
                                     lh_##type##_call_cmp_func);               \
240
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_delete(lhash_st_SSL_SESSION*, ssl_session_st const*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_delete
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_delete
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_delete
Unexecuted instantiation: conf.c:lh_CONF_VALUE_delete
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_delete
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_delete
241
                                                                               \
242
  typedef struct {                                                             \
243
    void (*doall_arg)(type *, void *);                                         \
244
    void *arg;                                                                 \
245
  } LHASH_DOALL_##type;                                                        \
246
                                                                               \
247
0
  OPENSSL_INLINE void lh_##type##_call_doall_arg(void *value, void *arg) {     \
248
0
    const LHASH_DOALL_##type *cb = (const LHASH_DOALL_##type *)arg;            \
249
0
    cb->doall_arg((type *)value, cb->arg);                                     \
250
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_call_doall_arg(void*, void*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_call_doall_arg
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_call_doall_arg
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_call_doall_arg
Unexecuted instantiation: conf.c:lh_CONF_VALUE_call_doall_arg
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_call_doall_arg
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_call_doall_arg
251
                                                                               \
252
  OPENSSL_INLINE void lh_##type##_doall_arg(                                   \
253
0
      LHASH_OF(type) *lh, void (*func)(type *, void *), void *arg) {           \
254
0
    LHASH_DOALL_##type cb = {func, arg};                                       \
255
0
    OPENSSL_lh_doall_arg((_LHASH *)lh, lh_##type##_call_doall_arg, &cb);       \
256
0
  }                                                                            \
Unexecuted instantiation: lh_SSL_SESSION_doall_arg(lhash_st_SSL_SESSION*, void (*)(ssl_session_st*, void*), void*)
Unexecuted instantiation: pool.c:lh_CRYPTO_BUFFER_doall_arg
Unexecuted instantiation: v3_utl.c:lh_CONF_VALUE_doall_arg
Unexecuted instantiation: a_strnid.c:lh_ASN1_STRING_TABLE_doall_arg
Unexecuted instantiation: conf.c:lh_CONF_VALUE_doall_arg
Unexecuted instantiation: obj.c:lh_ASN1_OBJECT_doall_arg
Unexecuted instantiation: asn1_gen.c:lh_CONF_VALUE_doall_arg
257
                                                                               \
258
  OPENSSL_MSVC_PRAGMA(warning(pop))
259
260
261
#if defined(__cplusplus)
262
}  // extern C
263
#endif
264
265
#endif  // OPENSSL_HEADER_LHASH_INTERNAL_H