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