lhash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/lhash.h>
  14. #include "lhash_lcl.h"
  15. #undef MIN_NODES
  16. #define MIN_NODES 16
  17. #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
  18. #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
  19. static int expand(OPENSSL_LHASH *lh);
  20. static void contract(OPENSSL_LHASH *lh);
  21. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
  22. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
  23. {
  24. OPENSSL_LHASH *ret;
  25. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  26. goto err0;
  27. if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
  28. goto err1;
  29. ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
  30. ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
  31. ret->num_nodes = MIN_NODES / 2;
  32. ret->num_alloc_nodes = MIN_NODES;
  33. ret->pmax = MIN_NODES / 2;
  34. ret->up_load = UP_LOAD;
  35. ret->down_load = DOWN_LOAD;
  36. return (ret);
  37. err1:
  38. OPENSSL_free(ret);
  39. err0:
  40. return (NULL);
  41. }
  42. void OPENSSL_LH_free(OPENSSL_LHASH *lh)
  43. {
  44. unsigned int i;
  45. OPENSSL_LH_NODE *n, *nn;
  46. if (lh == NULL)
  47. return;
  48. for (i = 0; i < lh->num_nodes; i++) {
  49. n = lh->b[i];
  50. while (n != NULL) {
  51. nn = n->next;
  52. OPENSSL_free(n);
  53. n = nn;
  54. }
  55. }
  56. OPENSSL_free(lh->b);
  57. OPENSSL_free(lh);
  58. }
  59. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
  60. {
  61. unsigned long hash;
  62. OPENSSL_LH_NODE *nn, **rn;
  63. void *ret;
  64. lh->error = 0;
  65. if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
  66. return NULL; /* 'lh->error++' already done in 'expand' */
  67. rn = getrn(lh, data, &hash);
  68. if (*rn == NULL) {
  69. if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
  70. lh->error++;
  71. return (NULL);
  72. }
  73. nn->data = data;
  74. nn->next = NULL;
  75. nn->hash = hash;
  76. *rn = nn;
  77. ret = NULL;
  78. lh->num_insert++;
  79. lh->num_items++;
  80. } else { /* replace same key */
  81. ret = (*rn)->data;
  82. (*rn)->data = data;
  83. lh->num_replace++;
  84. }
  85. return (ret);
  86. }
  87. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
  88. {
  89. unsigned long hash;
  90. OPENSSL_LH_NODE *nn, **rn;
  91. void *ret;
  92. lh->error = 0;
  93. rn = getrn(lh, data, &hash);
  94. if (*rn == NULL) {
  95. lh->num_no_delete++;
  96. return (NULL);
  97. } else {
  98. nn = *rn;
  99. *rn = nn->next;
  100. ret = nn->data;
  101. OPENSSL_free(nn);
  102. lh->num_delete++;
  103. }
  104. lh->num_items--;
  105. if ((lh->num_nodes > MIN_NODES) &&
  106. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  107. contract(lh);
  108. return (ret);
  109. }
  110. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
  111. {
  112. unsigned long hash;
  113. OPENSSL_LH_NODE **rn;
  114. void *ret;
  115. lh->error = 0;
  116. rn = getrn(lh, data, &hash);
  117. if (*rn == NULL) {
  118. lh->num_retrieve_miss++;
  119. return (NULL);
  120. } else {
  121. ret = (*rn)->data;
  122. lh->num_retrieve++;
  123. }
  124. return (ret);
  125. }
  126. static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
  127. OPENSSL_LH_DOALL_FUNC func,
  128. OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
  129. {
  130. int i;
  131. OPENSSL_LH_NODE *a, *n;
  132. if (lh == NULL)
  133. return;
  134. /*
  135. * reverse the order so we search from 'top to bottom' We were having
  136. * memory leaks otherwise
  137. */
  138. for (i = lh->num_nodes - 1; i >= 0; i--) {
  139. a = lh->b[i];
  140. while (a != NULL) {
  141. n = a->next;
  142. if (use_arg)
  143. func_arg(a->data, arg);
  144. else
  145. func(a->data);
  146. a = n;
  147. }
  148. }
  149. }
  150. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
  151. {
  152. doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
  153. }
  154. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
  155. {
  156. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
  157. }
  158. static int expand(OPENSSL_LHASH *lh)
  159. {
  160. OPENSSL_LH_NODE **n, **n1, **n2, *np;
  161. unsigned int p, i, j;
  162. unsigned long hash, nni;
  163. lh->num_nodes++;
  164. lh->num_expands++;
  165. p = (int)lh->p++;
  166. n1 = &(lh->b[p]);
  167. n2 = &(lh->b[p + (int)lh->pmax]);
  168. *n2 = NULL;
  169. nni = lh->num_alloc_nodes;
  170. for (np = *n1; np != NULL;) {
  171. hash = np->hash;
  172. if ((hash % nni) != p) { /* move it */
  173. *n1 = (*n1)->next;
  174. np->next = *n2;
  175. *n2 = np;
  176. } else
  177. n1 = &((*n1)->next);
  178. np = *n1;
  179. }
  180. if ((lh->p) >= lh->pmax) {
  181. j = (int)lh->num_alloc_nodes * 2;
  182. n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
  183. if (n == NULL) {
  184. lh->error++;
  185. lh->num_nodes--;
  186. lh->p = 0;
  187. return 0;
  188. }
  189. for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
  190. n[i] = NULL; /* 02/03/92 eay */
  191. lh->pmax = lh->num_alloc_nodes;
  192. lh->num_alloc_nodes = j;
  193. lh->num_expand_reallocs++;
  194. lh->p = 0;
  195. lh->b = n;
  196. }
  197. return 1;
  198. }
  199. static void contract(OPENSSL_LHASH *lh)
  200. {
  201. OPENSSL_LH_NODE **n, *n1, *np;
  202. np = lh->b[lh->p + lh->pmax - 1];
  203. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  204. if (lh->p == 0) {
  205. n = OPENSSL_realloc(lh->b,
  206. (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
  207. if (n == NULL) {
  208. /* fputs("realloc error in lhash",stderr); */
  209. lh->error++;
  210. return;
  211. }
  212. lh->num_contract_reallocs++;
  213. lh->num_alloc_nodes /= 2;
  214. lh->pmax /= 2;
  215. lh->p = lh->pmax - 1;
  216. lh->b = n;
  217. } else
  218. lh->p--;
  219. lh->num_nodes--;
  220. lh->num_contracts++;
  221. n1 = lh->b[(int)lh->p];
  222. if (n1 == NULL)
  223. lh->b[(int)lh->p] = np;
  224. else {
  225. while (n1->next != NULL)
  226. n1 = n1->next;
  227. n1->next = np;
  228. }
  229. }
  230. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
  231. const void *data, unsigned long *rhash)
  232. {
  233. OPENSSL_LH_NODE **ret, *n1;
  234. unsigned long hash, nn;
  235. OPENSSL_LH_COMPFUNC cf;
  236. hash = (*(lh->hash)) (data);
  237. lh->num_hash_calls++;
  238. *rhash = hash;
  239. nn = hash % lh->pmax;
  240. if (nn < lh->p)
  241. nn = hash % lh->num_alloc_nodes;
  242. cf = lh->comp;
  243. ret = &(lh->b[(int)nn]);
  244. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  245. lh->num_hash_comps++;
  246. if (n1->hash != hash) {
  247. ret = &(n1->next);
  248. continue;
  249. }
  250. lh->num_comp_calls++;
  251. if (cf(n1->data, data) == 0)
  252. break;
  253. ret = &(n1->next);
  254. }
  255. return (ret);
  256. }
  257. /*
  258. * The following hash seems to work very well on normal text strings no
  259. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  260. * as good as MD5, but still good.
  261. */
  262. unsigned long OPENSSL_LH_strhash(const char *c)
  263. {
  264. unsigned long ret = 0;
  265. long n;
  266. unsigned long v;
  267. int r;
  268. if ((c == NULL) || (*c == '\0'))
  269. return (ret);
  270. n = 0x100;
  271. while (*c) {
  272. v = n | (*c);
  273. n += 0x100;
  274. r = (int)((v >> 2) ^ v) & 0x0f;
  275. ret = (ret << r) | (ret >> (32 - r));
  276. ret &= 0xFFFFFFFFL;
  277. ret ^= v * v;
  278. c++;
  279. }
  280. return ((ret >> 16) ^ ret);
  281. }
  282. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
  283. {
  284. return lh ? lh->num_items : 0;
  285. }
  286. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
  287. {
  288. return lh->down_load;
  289. }
  290. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
  291. {
  292. lh->down_load = down_load;
  293. }
  294. int OPENSSL_LH_error(OPENSSL_LHASH *lh)
  295. {
  296. return lh->error;
  297. }