Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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