diff options
Diffstat (limited to 'src/crypto/lhash/lhash.c')
-rw-r--r-- | src/crypto/lhash/lhash.c | 71 |
1 files changed, 26 insertions, 45 deletions
diff --git a/src/crypto/lhash/lhash.c b/src/crypto/lhash/lhash.c index 27960d98..50545291 100644 --- a/src/crypto/lhash/lhash.c +++ b/src/crypto/lhash/lhash.c @@ -65,11 +65,11 @@ #include "../internal.h" -/* kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|. */ +// kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|. static const size_t kMinNumBuckets = 16; -/* kMaxAverageChainLength contains the maximum, average chain length. When the - * average chain length exceeds this value, the hash table will be resized. */ +// kMaxAverageChainLength contains the maximum, average chain length. When the +// average chain length exceeds this value, the hash table will be resized. static const size_t kMaxAverageChainLength = 2; static const size_t kMinAverageChainLength = 1; @@ -112,13 +112,13 @@ void lh_free(_LHASH *lh) { size_t lh_num_items(const _LHASH *lh) { return lh->num_items; } -/* get_next_ptr_and_hash returns a pointer to the pointer that points to the - * item equal to |data|. In other words, it searches for an item equal to |data| - * and, if it's at the start of a chain, then it returns a pointer to an - * element of |lh->buckets|, otherwise it returns a pointer to the |next| - * element of the previous item in the chain. If an element equal to |data| is - * not found, it returns a pointer that points to a NULL pointer. If |out_hash| - * is not NULL, then it also puts the hash value of |data| in |*out_hash|. */ +// get_next_ptr_and_hash returns a pointer to the pointer that points to the +// item equal to |data|. In other words, it searches for an item equal to |data| +// and, if it's at the start of a chain, then it returns a pointer to an +// element of |lh->buckets|, otherwise it returns a pointer to the |next| +// element of the previous item in the chain. If an element equal to |data| is +// not found, it returns a pointer that points to a NULL pointer. If |out_hash| +// is not NULL, then it also puts the hash value of |data| in |*out_hash|. static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash, const void *data) { const uint32_t hash = lh->hash(data); @@ -151,9 +151,9 @@ void *lh_retrieve(const _LHASH *lh, const void *data) { return (*next_ptr)->data; } -/* lh_rebucket allocates a new array of |new_num_buckets| pointers and - * redistributes the existing items into it before making it |lh->buckets| and - * freeing the old array. */ +// lh_rebucket allocates a new array of |new_num_buckets| pointers and +// redistributes the existing items into it before making it |lh->buckets| and +// freeing the old array. static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) { LHASH_ITEM **new_buckets, *cur, *next; size_t i, alloc_size; @@ -184,12 +184,12 @@ static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) { lh->buckets = new_buckets; } -/* lh_maybe_resize resizes the |buckets| array if needed. */ +// lh_maybe_resize resizes the |buckets| array if needed. static void lh_maybe_resize(_LHASH *lh) { size_t avg_chain_length; if (lh->callback_depth > 0) { - /* Don't resize the hash if we are currently iterating over it. */ + // Don't resize the hash if we are currently iterating over it. return; } @@ -223,14 +223,14 @@ int lh_insert(_LHASH *lh, void **old_data, void *data) { if (*next_ptr != NULL) { - /* An element equal to |data| already exists in the hash table. It will be - * replaced. */ + // An element equal to |data| already exists in the hash table. It will be + // replaced. *old_data = (*next_ptr)->data; (*next_ptr)->data = data; return 1; } - /* An element equal to |data| doesn't exist in the hash table yet. */ + // An element equal to |data| doesn't exist in the hash table yet. item = OPENSSL_malloc(sizeof(LHASH_ITEM)); if (item == NULL) { return 0; @@ -252,7 +252,7 @@ void *lh_delete(_LHASH *lh, const void *data) { next_ptr = get_next_ptr_and_hash(lh, NULL, data); if (*next_ptr == NULL) { - /* No such element. */ + // No such element. return NULL; } @@ -274,7 +274,7 @@ static void lh_doall_internal(_LHASH *lh, void (*no_arg_func)(void *), } if (lh->callback_depth < UINT_MAX) { - /* |callback_depth| is a saturating counter. */ + // |callback_depth| is a saturating counter. lh->callback_depth++; } @@ -294,9 +294,9 @@ static void lh_doall_internal(_LHASH *lh, void (*no_arg_func)(void *), lh->callback_depth--; } - /* The callback may have added or removed elements and the non-zero value of - * |callback_depth| will have suppressed any resizing. Thus any needed - * resizing is done here. */ + // The callback may have added or removed elements and the non-zero value of + // |callback_depth| will have suppressed any resizing. Thus any needed + // resizing is done here. lh_maybe_resize(lh); } @@ -309,28 +309,9 @@ void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) { } uint32_t lh_strhash(const char *c) { - /* The following hash seems to work very well on normal text strings - * no collisions on /usr/dict/words and it distributes on %2^n quite - * well, not as good as MD5, but still good. */ - unsigned long ret = 0; - long n; - unsigned long v; - int r; - - if ((c == NULL) || (*c == '\0')) { - return (ret); - } - - n = 0x100; - while (*c) { - v = n | (*c); - n += 0x100; - r = (int)((v >> 2) ^ v) & 0x0f; - ret = (ret << r) | (ret >> (32 - r)); - ret &= 0xFFFFFFFFL; - ret ^= v * v; - c++; + if (c == NULL) { + return 0; } - return ((ret >> 16) ^ ret); + return OPENSSL_hash32(c, strlen(c)); } |