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:SSL_SESSION */
100
101#define IN_LHASH_H
102#include <openssl/lhash_macros.h>
103#undef IN_LHASH_H
104
105
106/* lhash_item_st is an element of a hash chain. It points to the opaque data
107 * for this element and to the next item in the chain. The linked-list is NULL
108 * terminated. */
109typedef struct lhash_item_st {
110  void *data;
111  struct lhash_item_st *next;
112  /* hash contains the cached, hash value of |data|. */
113  uint32_t hash;
114} LHASH_ITEM;
115
116/* lhash_cmp_func is a comparison function that returns a value equal, or not
117 * equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
118 * respectively. Note the difference between this and |stack_cmp_func| in that
119 * this takes pointers to the objects directly. */
120typedef int (*lhash_cmp_func)(const void *a, const void *b);
121
122/* lhash_hash_func is a function that maps an object to a uniformly distributed
123 * uint32_t. */
124typedef uint32_t (*lhash_hash_func)(const void *a);
125
126typedef struct lhash_st {
127  /* num_items contains the total number of items in the hash table. */
128  size_t num_items;
129  /* buckets is an array of |num_buckets| pointers. Each points to the head of
130   * a chain of LHASH_ITEM objects that have the same hash value, mod
131   * |num_buckets|. */
132  LHASH_ITEM **buckets;
133  /* num_buckets contains the length of |buckets|. This value is always >=
134   * kMinNumBuckets. */
135  size_t num_buckets;
136  /* callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
137   * calls. If non-zero then this suppresses resizing of the |buckets| array,
138   * which would otherwise disrupt the iteration. */
139  unsigned callback_depth;
140
141  lhash_cmp_func comp;
142  lhash_hash_func hash;
143} _LHASH;
144
145/* lh_new returns a new, empty hash table or NULL on error. If |comp| is NULL,
146 * |strcmp| will be used. If |hash| is NULL, a generic hash function will be
147 * used. */
148OPENSSL_EXPORT _LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp);
149
150/* lh_free frees the hash table itself but none of the elements. See
151 * |lh_doall|. */
152OPENSSL_EXPORT void lh_free(_LHASH *lh);
153
154/* lh_num_items returns the number of items in |lh|. */
155OPENSSL_EXPORT size_t lh_num_items(const _LHASH *lh);
156
157/* lh_retrieve finds an element equal to |data| in the hash table and returns
158 * it. If no such element exists, it returns NULL. */
159OPENSSL_EXPORT void *lh_retrieve(const _LHASH *lh, const void *data);
160
161/* 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. */
166OPENSSL_EXPORT int lh_insert(_LHASH *lh, void **old_data, void *data);
167
168/* 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. */
170OPENSSL_EXPORT void *lh_delete(_LHASH *lh, const void *data);
171
172/* lh_doall calls |func| on each element of the hash table.
173 * TODO(fork): rename this */
174OPENSSL_EXPORT void lh_doall(_LHASH *lh, void (*func)(void *));
175
176/* 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 */
179OPENSSL_EXPORT void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *),
180                                 void *arg);
181
182/* lh_strhash is the default hash function which processes NUL-terminated
183 * strings. */
184OPENSSL_EXPORT uint32_t lh_strhash(const char *c);
185
186
187#if defined(__cplusplus)
188} /* extern C */
189#endif
190
191#endif /* OPENSSL_HEADER_STACK_H */
192