您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

337 行
10 KiB

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