lhash.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 1995-2017 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. /*
  16. * A hashing implementation that appears to be based on the linear hashing
  17. * alogrithm:
  18. * https://en.wikipedia.org/wiki/Linear_hashing
  19. *
  20. * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
  21. * addressing", Proc. 6th Conference on Very Large Databases: 212-223
  22. * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
  23. *
  24. * From the wikipedia article "Linear hashing is used in the BDB Berkeley
  25. * database system, which in turn is used by many software systems such as
  26. * OpenLDAP, using a C implementation derived from the CACM article and first
  27. * published on the Usenet in 1988 by Esmond Pitt."
  28. *
  29. * The CACM paper is available here:
  30. * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
  31. */
  32. #undef MIN_NODES
  33. #define MIN_NODES 16
  34. #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
  35. #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
  36. static int expand(OPENSSL_LHASH *lh);
  37. static void contract(OPENSSL_LHASH *lh);
  38. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
  39. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
  40. {
  41. OPENSSL_LHASH *ret;
  42. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  43. return NULL;
  44. if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
  45. goto err;
  46. ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
  47. ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
  48. ret->num_nodes = MIN_NODES / 2;
  49. ret->num_alloc_nodes = MIN_NODES;
  50. ret->pmax = MIN_NODES / 2;
  51. ret->up_load = UP_LOAD;
  52. ret->down_load = DOWN_LOAD;
  53. return ret;
  54. err:
  55. OPENSSL_free(ret->b);
  56. OPENSSL_free(ret);
  57. return NULL;
  58. }
  59. void OPENSSL_LH_free(OPENSSL_LHASH *lh)
  60. {
  61. unsigned int i;
  62. OPENSSL_LH_NODE *n, *nn;
  63. if (lh == NULL)
  64. return;
  65. for (i = 0; i < lh->num_nodes; i++) {
  66. n = lh->b[i];
  67. while (n != NULL) {
  68. nn = n->next;
  69. OPENSSL_free(n);
  70. n = nn;
  71. }
  72. }
  73. OPENSSL_free(lh->b);
  74. OPENSSL_free(lh);
  75. }
  76. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
  77. {
  78. unsigned long hash;
  79. OPENSSL_LH_NODE *nn, **rn;
  80. void *ret;
  81. lh->error = 0;
  82. if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
  83. return NULL; /* 'lh->error++' already done in 'expand' */
  84. rn = getrn(lh, data, &hash);
  85. if (*rn == NULL) {
  86. if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
  87. lh->error++;
  88. return NULL;
  89. }
  90. nn->data = data;
  91. nn->next = NULL;
  92. nn->hash = hash;
  93. *rn = nn;
  94. ret = NULL;
  95. lh->num_insert++;
  96. lh->num_items++;
  97. } else { /* replace same key */
  98. ret = (*rn)->data;
  99. (*rn)->data = data;
  100. lh->num_replace++;
  101. }
  102. return ret;
  103. }
  104. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
  105. {
  106. unsigned long hash;
  107. OPENSSL_LH_NODE *nn, **rn;
  108. void *ret;
  109. lh->error = 0;
  110. rn = getrn(lh, data, &hash);
  111. if (*rn == NULL) {
  112. lh->num_no_delete++;
  113. return NULL;
  114. } else {
  115. nn = *rn;
  116. *rn = nn->next;
  117. ret = nn->data;
  118. OPENSSL_free(nn);
  119. lh->num_delete++;
  120. }
  121. lh->num_items--;
  122. if ((lh->num_nodes > MIN_NODES) &&
  123. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  124. contract(lh);
  125. return ret;
  126. }
  127. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
  128. {
  129. unsigned long hash;
  130. OPENSSL_LH_NODE **rn;
  131. void *ret;
  132. lh->error = 0;
  133. rn = getrn(lh, data, &hash);
  134. if (*rn == NULL) {
  135. lh->num_retrieve_miss++;
  136. return NULL;
  137. } else {
  138. ret = (*rn)->data;
  139. lh->num_retrieve++;
  140. }
  141. return ret;
  142. }
  143. static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
  144. OPENSSL_LH_DOALL_FUNC func,
  145. OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
  146. {
  147. int i;
  148. OPENSSL_LH_NODE *a, *n;
  149. if (lh == NULL)
  150. return;
  151. /*
  152. * reverse the order so we search from 'top to bottom' We were having
  153. * memory leaks otherwise
  154. */
  155. for (i = lh->num_nodes - 1; i >= 0; i--) {
  156. a = lh->b[i];
  157. while (a != NULL) {
  158. n = a->next;
  159. if (use_arg)
  160. func_arg(a->data, arg);
  161. else
  162. func(a->data);
  163. a = n;
  164. }
  165. }
  166. }
  167. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
  168. {
  169. doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
  170. }
  171. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
  172. {
  173. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
  174. }
  175. static int expand(OPENSSL_LHASH *lh)
  176. {
  177. OPENSSL_LH_NODE **n, **n1, **n2, *np;
  178. unsigned int p, pmax, nni, j;
  179. unsigned long hash;
  180. nni = lh->num_alloc_nodes;
  181. p = lh->p;
  182. pmax = lh->pmax;
  183. if (p + 1 >= pmax) {
  184. j = nni * 2;
  185. n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
  186. if (n == NULL) {
  187. lh->error++;
  188. return 0;
  189. }
  190. lh->b = n;
  191. memset(n + nni, 0, sizeof(*n) * (j - nni));
  192. lh->pmax = nni;
  193. lh->num_alloc_nodes = j;
  194. lh->num_expand_reallocs++;
  195. lh->p = 0;
  196. } else {
  197. lh->p++;
  198. }
  199. lh->num_nodes++;
  200. lh->num_expands++;
  201. n1 = &(lh->b[p]);
  202. n2 = &(lh->b[p + pmax]);
  203. *n2 = NULL;
  204. for (np = *n1; np != NULL;) {
  205. hash = np->hash;
  206. if ((hash % nni) != p) { /* move it */
  207. *n1 = (*n1)->next;
  208. np->next = *n2;
  209. *n2 = np;
  210. } else
  211. n1 = &((*n1)->next);
  212. np = *n1;
  213. }
  214. return 1;
  215. }
  216. static void contract(OPENSSL_LHASH *lh)
  217. {
  218. OPENSSL_LH_NODE **n, *n1, *np;
  219. np = lh->b[lh->p + lh->pmax - 1];
  220. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  221. if (lh->p == 0) {
  222. n = OPENSSL_realloc(lh->b,
  223. (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
  224. if (n == NULL) {
  225. /* fputs("realloc error in lhash",stderr); */
  226. lh->error++;
  227. return;
  228. }
  229. lh->num_contract_reallocs++;
  230. lh->num_alloc_nodes /= 2;
  231. lh->pmax /= 2;
  232. lh->p = lh->pmax - 1;
  233. lh->b = n;
  234. } else
  235. lh->p--;
  236. lh->num_nodes--;
  237. lh->num_contracts++;
  238. n1 = lh->b[(int)lh->p];
  239. if (n1 == NULL)
  240. lh->b[(int)lh->p] = np;
  241. else {
  242. while (n1->next != NULL)
  243. n1 = n1->next;
  244. n1->next = np;
  245. }
  246. }
  247. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
  248. const void *data, unsigned long *rhash)
  249. {
  250. OPENSSL_LH_NODE **ret, *n1;
  251. unsigned long hash, nn;
  252. OPENSSL_LH_COMPFUNC cf;
  253. hash = (*(lh->hash)) (data);
  254. lh->num_hash_calls++;
  255. *rhash = hash;
  256. nn = hash % lh->pmax;
  257. if (nn < lh->p)
  258. nn = hash % lh->num_alloc_nodes;
  259. cf = lh->comp;
  260. ret = &(lh->b[(int)nn]);
  261. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  262. lh->num_hash_comps++;
  263. if (n1->hash != hash) {
  264. ret = &(n1->next);
  265. continue;
  266. }
  267. lh->num_comp_calls++;
  268. if (cf(n1->data, data) == 0)
  269. break;
  270. ret = &(n1->next);
  271. }
  272. return ret;
  273. }
  274. /*
  275. * The following hash seems to work very well on normal text strings no
  276. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  277. * as good as MD5, but still good.
  278. */
  279. unsigned long OPENSSL_LH_strhash(const char *c)
  280. {
  281. unsigned long ret = 0;
  282. long n;
  283. unsigned long v;
  284. int r;
  285. if ((c == NULL) || (*c == '\0'))
  286. return ret;
  287. n = 0x100;
  288. while (*c) {
  289. v = n | (*c);
  290. n += 0x100;
  291. r = (int)((v >> 2) ^ v) & 0x0f;
  292. ret = (ret << r) | (ret >> (32 - r));
  293. ret &= 0xFFFFFFFFL;
  294. ret ^= v * v;
  295. c++;
  296. }
  297. return (ret >> 16) ^ ret;
  298. }
  299. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
  300. {
  301. return lh ? lh->num_items : 0;
  302. }
  303. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
  304. {
  305. return lh->down_load;
  306. }
  307. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
  308. {
  309. lh->down_load = down_load;
  310. }
  311. int OPENSSL_LH_error(OPENSSL_LHASH *lh)
  312. {
  313. return lh->error;
  314. }