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 #include <openssl/lhash.h>
58
59 #include <assert.h>
60 #include <limits.h>
61 #include <string.h>
62
63 #include <openssl/mem.h>
64
65 #include "internal.h"
66 #include "../internal.h"
67
68
69 // kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|.
70 static const size_t kMinNumBuckets = 16;
71
72 // kMaxAverageChainLength contains the maximum, average chain length. When the
73 // average chain length exceeds this value, the hash table will be resized.
74 static const size_t kMaxAverageChainLength = 2;
75 static const size_t kMinAverageChainLength = 1;
76
77 // lhash_item_st is an element of a hash chain. It points to the opaque data
78 // for this element and to the next item in the chain. The linked-list is NULL
79 // terminated.
80 typedef struct lhash_item_st {
81 void *data;
82 struct lhash_item_st *next;
83 // hash contains the cached, hash value of |data|.
84 uint32_t hash;
85 } LHASH_ITEM;
86
87 struct lhash_st {
88 // num_items contains the total number of items in the hash table.
89 size_t num_items;
90 // buckets is an array of |num_buckets| pointers. Each points to the head of
91 // a chain of LHASH_ITEM objects that have the same hash value, mod
92 // |num_buckets|.
93 LHASH_ITEM **buckets;
94 // num_buckets contains the length of |buckets|. This value is always >=
95 // kMinNumBuckets.
96 size_t num_buckets;
97 // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
98 // calls. If non-zero then this suppresses resizing of the |buckets| array,
99 // which would otherwise disrupt the iteration.
100 unsigned callback_depth;
101
102 lhash_cmp_func comp;
103 lhash_hash_func hash;
104 };
105
OPENSSL_lh_new(lhash_hash_func hash,lhash_cmp_func comp)106 _LHASH *OPENSSL_lh_new(lhash_hash_func hash, lhash_cmp_func comp) {
107 _LHASH *ret = OPENSSL_malloc(sizeof(_LHASH));
108 if (ret == NULL) {
109 return NULL;
110 }
111 OPENSSL_memset(ret, 0, sizeof(_LHASH));
112
113 ret->num_buckets = kMinNumBuckets;
114 ret->buckets = OPENSSL_malloc(sizeof(LHASH_ITEM *) * ret->num_buckets);
115 if (ret->buckets == NULL) {
116 OPENSSL_free(ret);
117 return NULL;
118 }
119 OPENSSL_memset(ret->buckets, 0, sizeof(LHASH_ITEM *) * ret->num_buckets);
120
121 ret->comp = comp;
122 ret->hash = hash;
123 return ret;
124 }
125
OPENSSL_lh_free(_LHASH * lh)126 void OPENSSL_lh_free(_LHASH *lh) {
127 if (lh == NULL) {
128 return;
129 }
130
131 for (size_t i = 0; i < lh->num_buckets; i++) {
132 LHASH_ITEM *next;
133 for (LHASH_ITEM *n = lh->buckets[i]; n != NULL; n = next) {
134 next = n->next;
135 OPENSSL_free(n);
136 }
137 }
138
139 OPENSSL_free(lh->buckets);
140 OPENSSL_free(lh);
141 }
142
OPENSSL_lh_num_items(const _LHASH * lh)143 size_t OPENSSL_lh_num_items(const _LHASH *lh) { return lh->num_items; }
144
145 // get_next_ptr_and_hash returns a pointer to the pointer that points to the
146 // item equal to |data|. In other words, it searches for an item equal to |data|
147 // and, if it's at the start of a chain, then it returns a pointer to an
148 // element of |lh->buckets|, otherwise it returns a pointer to the |next|
149 // element of the previous item in the chain. If an element equal to |data| is
150 // not found, it returns a pointer that points to a NULL pointer. If |out_hash|
151 // is not NULL, then it also puts the hash value of |data| in |*out_hash|.
get_next_ptr_and_hash(const _LHASH * lh,uint32_t * out_hash,const void * data,lhash_hash_func_helper call_hash_func,lhash_cmp_func_helper call_cmp_func)152 static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash,
153 const void *data,
154 lhash_hash_func_helper call_hash_func,
155 lhash_cmp_func_helper call_cmp_func) {
156 const uint32_t hash = call_hash_func(lh->hash, data);
157 if (out_hash != NULL) {
158 *out_hash = hash;
159 }
160
161 LHASH_ITEM **ret = &lh->buckets[hash % lh->num_buckets];
162 for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
163 if (call_cmp_func(lh->comp, cur->data, data) == 0) {
164 break;
165 }
166 ret = &cur->next;
167 }
168
169 return ret;
170 }
171
172 // get_next_ptr_by_key behaves like |get_next_ptr_and_hash| but takes a key
173 // which may be a different type from the values stored in |lh|.
get_next_ptr_by_key(const _LHASH * lh,const void * key,uint32_t key_hash,int (* cmp_key)(const void * key,const void * value))174 static LHASH_ITEM **get_next_ptr_by_key(const _LHASH *lh, const void *key,
175 uint32_t key_hash,
176 int (*cmp_key)(const void *key,
177 const void *value)) {
178 LHASH_ITEM **ret = &lh->buckets[key_hash % lh->num_buckets];
179 for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
180 if (cmp_key(key, cur->data) == 0) {
181 break;
182 }
183 ret = &cur->next;
184 }
185
186 return ret;
187 }
188
OPENSSL_lh_retrieve(const _LHASH * lh,const void * data,lhash_hash_func_helper call_hash_func,lhash_cmp_func_helper call_cmp_func)189 void *OPENSSL_lh_retrieve(const _LHASH *lh, const void *data,
190 lhash_hash_func_helper call_hash_func,
191 lhash_cmp_func_helper call_cmp_func) {
192 LHASH_ITEM **next_ptr =
193 get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);
194 return *next_ptr == NULL ? NULL : (*next_ptr)->data;
195 }
196
OPENSSL_lh_retrieve_key(const _LHASH * lh,const void * key,uint32_t key_hash,int (* cmp_key)(const void * key,const void * value))197 void *OPENSSL_lh_retrieve_key(const _LHASH *lh, const void *key,
198 uint32_t key_hash,
199 int (*cmp_key)(const void *key,
200 const void *value)) {
201 LHASH_ITEM **next_ptr = get_next_ptr_by_key(lh, key, key_hash, cmp_key);
202 return *next_ptr == NULL ? NULL : (*next_ptr)->data;
203 }
204
205 // lh_rebucket allocates a new array of |new_num_buckets| pointers and
206 // redistributes the existing items into it before making it |lh->buckets| and
207 // freeing the old array.
lh_rebucket(_LHASH * lh,const size_t new_num_buckets)208 static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) {
209 LHASH_ITEM **new_buckets, *cur, *next;
210 size_t i, alloc_size;
211
212 alloc_size = sizeof(LHASH_ITEM *) * new_num_buckets;
213 if (alloc_size / sizeof(LHASH_ITEM*) != new_num_buckets) {
214 return;
215 }
216
217 new_buckets = OPENSSL_malloc(alloc_size);
218 if (new_buckets == NULL) {
219 return;
220 }
221 OPENSSL_memset(new_buckets, 0, alloc_size);
222
223 for (i = 0; i < lh->num_buckets; i++) {
224 for (cur = lh->buckets[i]; cur != NULL; cur = next) {
225 const size_t new_bucket = cur->hash % new_num_buckets;
226 next = cur->next;
227 cur->next = new_buckets[new_bucket];
228 new_buckets[new_bucket] = cur;
229 }
230 }
231
232 OPENSSL_free(lh->buckets);
233
234 lh->num_buckets = new_num_buckets;
235 lh->buckets = new_buckets;
236 }
237
238 // lh_maybe_resize resizes the |buckets| array if needed.
lh_maybe_resize(_LHASH * lh)239 static void lh_maybe_resize(_LHASH *lh) {
240 size_t avg_chain_length;
241
242 if (lh->callback_depth > 0) {
243 // Don't resize the hash if we are currently iterating over it.
244 return;
245 }
246
247 assert(lh->num_buckets >= kMinNumBuckets);
248 avg_chain_length = lh->num_items / lh->num_buckets;
249
250 if (avg_chain_length > kMaxAverageChainLength) {
251 const size_t new_num_buckets = lh->num_buckets * 2;
252
253 if (new_num_buckets > lh->num_buckets) {
254 lh_rebucket(lh, new_num_buckets);
255 }
256 } else if (avg_chain_length < kMinAverageChainLength &&
257 lh->num_buckets > kMinNumBuckets) {
258 size_t new_num_buckets = lh->num_buckets / 2;
259
260 if (new_num_buckets < kMinNumBuckets) {
261 new_num_buckets = kMinNumBuckets;
262 }
263
264 lh_rebucket(lh, new_num_buckets);
265 }
266 }
267
OPENSSL_lh_insert(_LHASH * lh,void ** old_data,void * data,lhash_hash_func_helper call_hash_func,lhash_cmp_func_helper call_cmp_func)268 int OPENSSL_lh_insert(_LHASH *lh, void **old_data, void *data,
269 lhash_hash_func_helper call_hash_func,
270 lhash_cmp_func_helper call_cmp_func) {
271 uint32_t hash;
272 LHASH_ITEM **next_ptr, *item;
273
274 *old_data = NULL;
275 next_ptr =
276 get_next_ptr_and_hash(lh, &hash, data, call_hash_func, call_cmp_func);
277
278
279 if (*next_ptr != NULL) {
280 // An element equal to |data| already exists in the hash table. It will be
281 // replaced.
282 *old_data = (*next_ptr)->data;
283 (*next_ptr)->data = data;
284 return 1;
285 }
286
287 // An element equal to |data| doesn't exist in the hash table yet.
288 item = OPENSSL_malloc(sizeof(LHASH_ITEM));
289 if (item == NULL) {
290 return 0;
291 }
292
293 item->data = data;
294 item->hash = hash;
295 item->next = NULL;
296 *next_ptr = item;
297 lh->num_items++;
298 lh_maybe_resize(lh);
299
300 return 1;
301 }
302
OPENSSL_lh_delete(_LHASH * lh,const void * data,lhash_hash_func_helper call_hash_func,lhash_cmp_func_helper call_cmp_func)303 void *OPENSSL_lh_delete(_LHASH *lh, const void *data,
304 lhash_hash_func_helper call_hash_func,
305 lhash_cmp_func_helper call_cmp_func) {
306 LHASH_ITEM **next_ptr, *item, *ret;
307
308 next_ptr =
309 get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);
310
311 if (*next_ptr == NULL) {
312 // No such element.
313 return NULL;
314 }
315
316 item = *next_ptr;
317 *next_ptr = item->next;
318 ret = item->data;
319 OPENSSL_free(item);
320
321 lh->num_items--;
322 lh_maybe_resize(lh);
323
324 return ret;
325 }
326
OPENSSL_lh_doall_arg(_LHASH * lh,void (* func)(void *,void *),void * arg)327 void OPENSSL_lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) {
328 if (lh == NULL) {
329 return;
330 }
331
332 if (lh->callback_depth < UINT_MAX) {
333 // |callback_depth| is a saturating counter.
334 lh->callback_depth++;
335 }
336
337 for (size_t i = 0; i < lh->num_buckets; i++) {
338 LHASH_ITEM *next;
339 for (LHASH_ITEM *cur = lh->buckets[i]; cur != NULL; cur = next) {
340 next = cur->next;
341 func(cur->data, arg);
342 }
343 }
344
345 if (lh->callback_depth < UINT_MAX) {
346 lh->callback_depth--;
347 }
348
349 // The callback may have added or removed elements and the non-zero value of
350 // |callback_depth| will have suppressed any resizing. Thus any needed
351 // resizing is done here.
352 lh_maybe_resize(lh);
353 }
354