Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

345 строки
10 KiB

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