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