blob: 7525c0877d873fba834a92bce59259d20577a2fd [file] [log] [blame]
David Benjamin820731a2015-07-23 20:01:51 -04001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
Adam Langley95c29f32014-06-20 12:00:00 -07003 *
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_H
58#define OPENSSL_HEADER_LHASH_H
59
60#include <openssl/base.h>
61#include <openssl/type_check.h>
62
63#if defined(__cplusplus)
64extern "C" {
65#endif
66
67
David Benjamin4512b792017-08-18 19:21:50 -040068// lhash is a traditional, chaining hash table that automatically expands and
69// contracts as needed. One should not use the lh_* functions directly, rather
70// use the type-safe macro wrappers:
71//
72// A hash table of a specific type of object has type |LHASH_OF(type)|. This
73// can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed
74// with |DECLARE_LHASH_OF(type)|. For example:
75//
76// struct foo {
77// int bar;
78// };
79//
80// DEFINE_LHASH_OF(struct foo);
81//
82// Although note that the hash table will contain /pointers/ to |foo|.
83//
84// A macro will be defined for each of the lh_* functions below. For
85// LHASH_OF(foo), the macros would be lh_foo_new, lh_foo_num_items etc.
Adam Langley95c29f32014-06-20 12:00:00 -070086
87
88#define LHASH_OF(type) struct lhash_st_##type
89
90#define DEFINE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
91
92#define DECLARE_LHASH_OF(type) LHASH_OF(type);
93
David Benjamin4512b792017-08-18 19:21:50 -040094// The make_macros.sh script in this directory parses the following lines and
95// generates the lhash_macros.h file that contains macros for the following
96// types of stacks:
97//
98// LHASH_OF:ASN1_OBJECT
99// LHASH_OF:CONF_VALUE
100// LHASH_OF:CRYPTO_BUFFER
101// LHASH_OF:SSL_SESSION
Adam Langley95c29f32014-06-20 12:00:00 -0700102
103#define IN_LHASH_H
104#include <openssl/lhash_macros.h>
105#undef IN_LHASH_H
106
107
David Benjamin4512b792017-08-18 19:21:50 -0400108// lhash_item_st is an element of a hash chain. It points to the opaque data
109// for this element and to the next item in the chain. The linked-list is NULL
110// terminated.
Adam Langley95c29f32014-06-20 12:00:00 -0700111typedef struct lhash_item_st {
112 void *data;
113 struct lhash_item_st *next;
David Benjamin4512b792017-08-18 19:21:50 -0400114 // hash contains the cached, hash value of |data|.
Adam Langley95c29f32014-06-20 12:00:00 -0700115 uint32_t hash;
116} LHASH_ITEM;
117
David Benjamin4512b792017-08-18 19:21:50 -0400118// lhash_cmp_func is a comparison function that returns a value equal, or not
119// equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
120// respectively. Note the difference between this and |stack_cmp_func| in that
121// this takes pointers to the objects directly.
Adam Langley95c29f32014-06-20 12:00:00 -0700122typedef int (*lhash_cmp_func)(const void *a, const void *b);
123
David Benjamin4512b792017-08-18 19:21:50 -0400124// lhash_hash_func is a function that maps an object to a uniformly distributed
125// uint32_t.
Adam Langley95c29f32014-06-20 12:00:00 -0700126typedef uint32_t (*lhash_hash_func)(const void *a);
127
128typedef struct lhash_st {
David Benjamin4512b792017-08-18 19:21:50 -0400129 // num_items contains the total number of items in the hash table.
Adam Langley95c29f32014-06-20 12:00:00 -0700130 size_t num_items;
David Benjamin4512b792017-08-18 19:21:50 -0400131 // buckets is an array of |num_buckets| pointers. Each points to the head of
132 // a chain of LHASH_ITEM objects that have the same hash value, mod
133 // |num_buckets|.
Adam Langley95c29f32014-06-20 12:00:00 -0700134 LHASH_ITEM **buckets;
David Benjamin4512b792017-08-18 19:21:50 -0400135 // num_buckets contains the length of |buckets|. This value is always >=
136 // kMinNumBuckets.
Adam Langley95c29f32014-06-20 12:00:00 -0700137 size_t num_buckets;
David Benjamin4512b792017-08-18 19:21:50 -0400138 // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
139 // calls. If non-zero then this suppresses resizing of the |buckets| array,
140 // which would otherwise disrupt the iteration.
Adam Langley95c29f32014-06-20 12:00:00 -0700141 unsigned callback_depth;
142
143 lhash_cmp_func comp;
144 lhash_hash_func hash;
145} _LHASH;
146
David Benjamin4512b792017-08-18 19:21:50 -0400147// lh_new returns a new, empty hash table or NULL on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700148OPENSSL_EXPORT _LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp);
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjamin4512b792017-08-18 19:21:50 -0400150// lh_free frees the hash table itself but none of the elements. See
151// |lh_doall|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700152OPENSSL_EXPORT void lh_free(_LHASH *lh);
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin4512b792017-08-18 19:21:50 -0400154// lh_num_items returns the number of items in |lh|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700155OPENSSL_EXPORT size_t lh_num_items(const _LHASH *lh);
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamin4512b792017-08-18 19:21:50 -0400157// lh_retrieve finds an element equal to |data| in the hash table and returns
158// it. If no such element exists, it returns NULL.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700159OPENSSL_EXPORT void *lh_retrieve(const _LHASH *lh, const void *data);
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin4512b792017-08-18 19:21:50 -0400161// lh_insert inserts |data| into the hash table. If an existing element is
162// equal to |data| (with respect to the comparison function) then |*old_data|
163// will be set to that value and it will be replaced. Otherwise, or in the
164// event of an error, |*old_data| will be set to NULL. It returns one on
165// success or zero in the case of an allocation error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700166OPENSSL_EXPORT int lh_insert(_LHASH *lh, void **old_data, void *data);
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin4512b792017-08-18 19:21:50 -0400168// lh_delete removes an element equal to |data| from the hash table and returns
169// it. If no such element is found, it returns NULL.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700170OPENSSL_EXPORT void *lh_delete(_LHASH *lh, const void *data);
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamin4512b792017-08-18 19:21:50 -0400172// lh_doall calls |func| on each element of the hash table.
173// TODO(fork): rename this
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700174OPENSSL_EXPORT void lh_doall(_LHASH *lh, void (*func)(void *));
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin4512b792017-08-18 19:21:50 -0400176// lh_doall_arg calls |func| on each element of the hash table and also passes
177// |arg| as the second argument.
178// TODO(fork): rename this
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700179OPENSSL_EXPORT void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *),
180 void *arg);
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamin4512b792017-08-18 19:21:50 -0400182// lh_strhash is the default hash function which processes NUL-terminated
183// strings.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700184OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
Adam Langley95c29f32014-06-20 12:00:00 -0700185
186
187#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400188} // extern C
Adam Langley95c29f32014-06-20 12:00:00 -0700189#endif
190
David Benjamin4512b792017-08-18 19:21:50 -0400191#endif // OPENSSL_HEADER_LHASH_H