1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved.
2 *
3 * This package is an SSL implementation written
4 * by Eric Young (eay@cryptsoft.com).
5 * The implementation was written so as to conform with Netscapes SSL.
6 *
7 * This library is free for commercial and non-commercial use as long as
8 * the following conditions are aheared to.  The following conditions
9 * apply to all code found in this distribution, be it the RC4, RSA,
10 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
11 * included with this distribution is covered by the same copyright terms
12 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
13 *
14 * Copyright remains Eric Young's, and as such any Copyright notices in
15 * the code are not to be removed.
16 * If this package is used in a product, Eric Young should be given attribution
17 * as the author of the parts of the library used.
18 * This can be in the form of a textual message at program startup or
19 * in documentation (online or textual) provided with the package.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the copyright
25 *    notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 *    notice, this list of conditions and the following disclaimer in the
28 *    documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 *    must display the following acknowledgement:
31 *    "This product includes cryptographic software written by
32 *     Eric Young (eay@cryptsoft.com)"
33 *    The word 'cryptographic' can be left out if the rouines from the library
34 *    being used are not cryptographic related :-).
35 * 4. If you include any Windows specific code (or a derivative thereof) from
36 *    the apps directory (application code) you must include an acknowledgement:
37 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 *
51 * The licence and distribution terms for any publically available version or
52 * derivative of this code cannot be changed.  i.e. this code cannot simply be
53 * copied and put under another distribution licence
54 * [including the GNU Public Licence.] */
55
56#ifndef OPENSSL_HEADER_LHASH_H
57#define OPENSSL_HEADER_LHASH_H
58
59#include <openssl/base.h>
60#include <openssl/type_check.h>
61
62#if defined(__cplusplus)
63extern "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 lh_* functions below. For
84 * LHASH_OF(foo), the macros would be lh_foo_new, lh_foo_num_items etc. */
85
86
87#define LHASH_OF(type) struct lhash_st_##type
88
89#define DEFINE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
90
91#define DECLARE_LHASH_OF(type) LHASH_OF(type);
92
93/* The make_macros.sh script in this directory parses the following lines and
94 * generates the lhash_macros.h file that contains macros for the following
95 * types of stacks:
96 *
97 * LHASH_OF:ASN1_OBJECT
98 * LHASH_OF:CONF_VALUE
99 * LHASH_OF:ERR_STATE
100 * LHASH_OF:ERR_STRING_DATA
101 * LHASH_OF:EX_CLASS_ITEM
102 * LHASH_OF:SSL_SESSION */
103
104#define IN_LHASH_H
105#include <openssl/lhash_macros.h>
106#undef IN_LHASH_H
107
108
109/* lhash_item_st is an element of a hash chain. It points to the opaque data
110 * for this element and to the next item in the chain. The linked-list is NULL
111 * terminated. */
112typedef struct lhash_item_st {
113  void *data;
114  struct lhash_item_st *next;
115  /* hash contains the cached, hash value of |data|. */
116  uint32_t hash;
117} LHASH_ITEM;
118
119/* lhash_cmp_func is a comparision function that returns a value equal, or not
120 * equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
121 * respectively. Note the difference between this and |stack_cmp_func| in that
122 * this takes pointers to the objects directly. */
123typedef int (*lhash_cmp_func)(const void *a, const void *b);
124
125/* lhash_hash_func is a function that maps an object to a uniformly distributed
126 * uint32_t. */
127typedef uint32_t (*lhash_hash_func)(const void *a);
128
129typedef struct lhash_st {
130  /* num_items contains the total number of items in the hash table. */
131  size_t num_items;
132  /* buckets is an array of |num_buckets| pointers. Each points to the head of
133   * a chain of LHASH_ITEM objects that have the same hash value, mod
134   * |num_buckets|. */
135  LHASH_ITEM **buckets;
136  /* num_buckets contains the length of |buckets|. This value is always >=
137   * kMinNumBuckets. */
138  size_t num_buckets;
139  /* callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
140   * calls. If non-zero then this suppresses resizing of the |buckets| array,
141   * which would otherwise disrupt the iteration. */
142  unsigned callback_depth;
143
144  lhash_cmp_func comp;
145  lhash_hash_func hash;
146} _LHASH;
147
148/* lh_new returns a new, empty hash table or NULL on error. If |comp| is NULL,
149 * |strcmp| will be used. If |hash| is NULL, a generic hash function will be
150 * used. */
151OPENSSL_EXPORT _LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp);
152
153/* lh_free frees the hash table itself but none of the elements. See
154 * |lh_doall|. */
155OPENSSL_EXPORT void lh_free(_LHASH *lh);
156
157/* lh_num_items returns the number of items in |lh|. */
158OPENSSL_EXPORT size_t lh_num_items(const _LHASH *lh);
159
160/* lh_retrieve finds an element equal to |data| in the hash table and returns
161 * it. If no such element exists, it returns NULL. */
162OPENSSL_EXPORT void *lh_retrieve(const _LHASH *lh, const void *data);
163
164/* lh_insert inserts |data| into the hash table. If an existing element is
165 * equal to |data| (with respect to the comparison function) then |*old_data|
166 * will be set to that value and it will be replaced. Otherwise, or in the
167 * event of an error, |*old_data| will be set to NULL. It returns one on
168 * success or zero in the case of an allocation error. */
169OPENSSL_EXPORT int lh_insert(_LHASH *lh, void **old_data, void *data);
170
171/* lh_delete removes an element equal to |data| from the hash table and returns
172 * it. If no such element is found, it returns NULL. */
173OPENSSL_EXPORT void *lh_delete(_LHASH *lh, const void *data);
174
175/* lh_doall calls |func| on each element of the hash table.
176 * TODO(fork): rename this */
177OPENSSL_EXPORT void lh_doall(_LHASH *lh, void (*func)(void *));
178
179/* lh_doall_arg calls |func| on each element of the hash table and also passes
180 * |arg| as the second argument.
181 * TODO(fork): rename this */
182OPENSSL_EXPORT void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *),
183                                 void *arg);
184
185/* lh_strhash is the default hash function which processes NUL-terminated
186 * strings. */
187OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
188
189
190#if defined(__cplusplus)
191} /* extern C */
192#endif
193
194#endif /* OPENSSL_HEADER_STACK_H */
195