選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

343 行
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. if (lh == NULL) {
  93. return;
  94. }
  95. for (size_t i = 0; i < lh->num_buckets; i++) {
  96. LHASH_ITEM *next;
  97. for (LHASH_ITEM *n = lh->buckets[i]; n != NULL; n = next) {
  98. next = n->next;
  99. OPENSSL_free(n);
  100. }
  101. }
  102. OPENSSL_free(lh->buckets);
  103. OPENSSL_free(lh);
  104. }
  105. size_t lh_num_items(const _LHASH *lh) { return lh->num_items; }
  106. /* get_next_ptr_and_hash returns a pointer to the pointer that points to the
  107. * item equal to |data|. In other words, it searches for an item equal to |data|
  108. * and, if it's at the start of a chain, then it returns a pointer to an
  109. * element of |lh->buckets|, otherwise it returns a pointer to the |next|
  110. * element of the previous item in the chain. If an element equal to |data| is
  111. * not found, it returns a pointer that points to a NULL pointer. If |out_hash|
  112. * is not NULL, then it also puts the hash value of |data| in |*out_hash|. */
  113. static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash,
  114. const void *data) {
  115. const uint32_t hash = lh->hash(data);
  116. LHASH_ITEM *cur, **ret;
  117. if (out_hash != NULL) {
  118. *out_hash = hash;
  119. }
  120. ret = &lh->buckets[hash % lh->num_buckets];
  121. for (cur = *ret; cur != NULL; cur = *ret) {
  122. if (lh->comp(cur->data, data) == 0) {
  123. break;
  124. }
  125. ret = &cur->next;
  126. }
  127. return ret;
  128. }
  129. void *lh_retrieve(const _LHASH *lh, const void *data) {
  130. LHASH_ITEM **next_ptr;
  131. next_ptr = get_next_ptr_and_hash(lh, NULL, data);
  132. if (*next_ptr == NULL) {
  133. return NULL;
  134. }
  135. return (*next_ptr)->data;
  136. }
  137. /* lh_rebucket allocates a new array of |new_num_buckets| pointers and
  138. * redistributes the existing items into it before making it |lh->buckets| and
  139. * freeing the old array. */
  140. static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) {
  141. LHASH_ITEM **new_buckets, *cur, *next;
  142. size_t i, alloc_size;
  143. alloc_size = sizeof(LHASH_ITEM *) * new_num_buckets;
  144. if (alloc_size / sizeof(LHASH_ITEM*) != new_num_buckets) {
  145. return;
  146. }
  147. new_buckets = OPENSSL_malloc(alloc_size);
  148. if (new_buckets == NULL) {
  149. return;
  150. }
  151. memset(new_buckets, 0, alloc_size);
  152. for (i = 0; i < lh->num_buckets; i++) {
  153. for (cur = lh->buckets[i]; cur != NULL; cur = next) {
  154. const size_t new_bucket = cur->hash % new_num_buckets;
  155. next = cur->next;
  156. cur->next = new_buckets[new_bucket];
  157. new_buckets[new_bucket] = cur;
  158. }
  159. }
  160. OPENSSL_free(lh->buckets);
  161. lh->num_buckets = new_num_buckets;
  162. lh->buckets = new_buckets;
  163. }
  164. /* lh_maybe_resize resizes the |buckets| array if needed. */
  165. static void lh_maybe_resize(_LHASH *lh) {
  166. size_t avg_chain_length;
  167. if (lh->callback_depth > 0) {
  168. /* Don't resize the hash if we are currently iterating over it. */
  169. return;
  170. }
  171. assert(lh->num_buckets >= kMinNumBuckets);
  172. avg_chain_length = lh->num_items / lh->num_buckets;
  173. if (avg_chain_length > kMaxAverageChainLength) {
  174. const size_t new_num_buckets = lh->num_buckets * 2;
  175. if (new_num_buckets > lh->num_buckets) {
  176. lh_rebucket(lh, new_num_buckets);
  177. }
  178. } else if (avg_chain_length < kMinAverageChainLength &&
  179. lh->num_buckets > kMinNumBuckets) {
  180. size_t new_num_buckets = lh->num_buckets / 2;
  181. if (new_num_buckets < kMinNumBuckets) {
  182. new_num_buckets = kMinNumBuckets;
  183. }
  184. lh_rebucket(lh, new_num_buckets);
  185. }
  186. }
  187. int lh_insert(_LHASH *lh, void **old_data, void *data) {
  188. uint32_t hash;
  189. LHASH_ITEM **next_ptr, *item;
  190. *old_data = NULL;
  191. next_ptr = get_next_ptr_and_hash(lh, &hash, data);
  192. if (*next_ptr != NULL) {
  193. /* An element equal to |data| already exists in the hash table. It will be
  194. * replaced. */
  195. *old_data = (*next_ptr)->data;
  196. (*next_ptr)->data = data;
  197. return 1;
  198. }
  199. /* An element equal to |data| doesn't exist in the hash table yet. */
  200. item = OPENSSL_malloc(sizeof(LHASH_ITEM));
  201. if (item == NULL) {
  202. return 0;
  203. }
  204. item->data = data;
  205. item->hash = hash;
  206. item->next = NULL;
  207. *next_ptr = item;
  208. lh->num_items++;
  209. lh_maybe_resize(lh);
  210. return 1;
  211. }
  212. void *lh_delete(_LHASH *lh, const void *data) {
  213. LHASH_ITEM **next_ptr, *item, *ret;
  214. next_ptr = get_next_ptr_and_hash(lh, NULL, data);
  215. if (*next_ptr == NULL) {
  216. /* No such element. */
  217. return NULL;
  218. }
  219. item = *next_ptr;
  220. *next_ptr = item->next;
  221. ret = item->data;
  222. OPENSSL_free(item);
  223. lh->num_items--;
  224. lh_maybe_resize(lh);
  225. return ret;
  226. }
  227. static void lh_doall_internal(_LHASH *lh, void (*no_arg_func)(void *),
  228. void (*arg_func)(void *, void *), void *arg) {
  229. if (lh == NULL) {
  230. return;
  231. }
  232. if (lh->callback_depth < UINT_MAX) {
  233. /* |callback_depth| is a saturating counter. */
  234. lh->callback_depth++;
  235. }
  236. for (size_t i = 0; i < lh->num_buckets; i++) {
  237. LHASH_ITEM *next;
  238. for (LHASH_ITEM *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. }