2
0

lhash.c 9.0 KB

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