lhash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* crypto/lhash/lhash.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /*-
  59. * Code for dynamic hash table routines
  60. * Author - Eric Young v 2.0
  61. *
  62. * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
  63. * present. eay 18-Jun-98
  64. *
  65. * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
  66. *
  67. * 2.0 eay - Fixed a bug that occurred when using lh_delete
  68. * from inside lh_doall(). As entries were deleted,
  69. * the 'table' was 'contract()ed', making some entries
  70. * jump from the end of the table to the start, there by
  71. * skipping the lh_doall() processing. eay - 4/12/95
  72. *
  73. * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
  74. * were not being free()ed. 21/11/95
  75. *
  76. * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
  77. * 19/09/95
  78. *
  79. * 1.7 eay - Removed the fputs() for realloc failures - the code
  80. * should silently tolerate them. I have also fixed things
  81. * lint complained about 04/05/95
  82. *
  83. * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
  84. *
  85. * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
  86. *
  87. * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
  88. *
  89. * 1.3 eay - Fixed a few lint problems 19/3/1991
  90. *
  91. * 1.2 eay - Fixed lh_doall problem 13/3/1991
  92. *
  93. * 1.1 eay - Added lh_doall
  94. *
  95. * 1.0 eay - First version
  96. */
  97. #include <stdio.h>
  98. #include <string.h>
  99. #include <stdlib.h>
  100. #include <openssl/crypto.h>
  101. #include <openssl/lhash.h>
  102. /*
  103. * A hashing implementation that appears to be based on the linear hashing
  104. * alogrithm:
  105. * https://en.wikipedia.org/wiki/Linear_hashing
  106. *
  107. * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
  108. * addressing", Proc. 6th Conference on Very Large Databases: 212-223
  109. * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
  110. *
  111. * From the wikipedia article "Linear hashing is used in the BDB Berkeley
  112. * database system, which in turn is used by many software systems such as
  113. * OpenLDAP, using a C implementation derived from the CACM article and first
  114. * published on the Usenet in 1988 by Esmond Pitt."
  115. *
  116. * The CACM paper is available here:
  117. * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
  118. */
  119. const char lh_version[] = "lhash" OPENSSL_VERSION_PTEXT;
  120. #undef MIN_NODES
  121. #define MIN_NODES 16
  122. #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
  123. #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
  124. static int expand(_LHASH *lh);
  125. static void contract(_LHASH *lh);
  126. static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
  127. _LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
  128. {
  129. _LHASH *ret;
  130. int i;
  131. if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)
  132. goto err0;
  133. if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)
  134. goto err1;
  135. for (i = 0; i < MIN_NODES; i++)
  136. ret->b[i] = NULL;
  137. ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
  138. ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
  139. ret->num_nodes = MIN_NODES / 2;
  140. ret->num_alloc_nodes = MIN_NODES;
  141. ret->p = 0;
  142. ret->pmax = MIN_NODES / 2;
  143. ret->up_load = UP_LOAD;
  144. ret->down_load = DOWN_LOAD;
  145. ret->num_items = 0;
  146. ret->num_expands = 0;
  147. ret->num_expand_reallocs = 0;
  148. ret->num_contracts = 0;
  149. ret->num_contract_reallocs = 0;
  150. ret->num_hash_calls = 0;
  151. ret->num_comp_calls = 0;
  152. ret->num_insert = 0;
  153. ret->num_replace = 0;
  154. ret->num_delete = 0;
  155. ret->num_no_delete = 0;
  156. ret->num_retrieve = 0;
  157. ret->num_retrieve_miss = 0;
  158. ret->num_hash_comps = 0;
  159. ret->error = 0;
  160. return (ret);
  161. err1:
  162. OPENSSL_free(ret);
  163. err0:
  164. return (NULL);
  165. }
  166. void lh_free(_LHASH *lh)
  167. {
  168. unsigned int i;
  169. LHASH_NODE *n, *nn;
  170. if (lh == NULL)
  171. return;
  172. for (i = 0; i < lh->num_nodes; i++) {
  173. n = lh->b[i];
  174. while (n != NULL) {
  175. nn = n->next;
  176. OPENSSL_free(n);
  177. n = nn;
  178. }
  179. }
  180. OPENSSL_free(lh->b);
  181. OPENSSL_free(lh);
  182. }
  183. void *lh_insert(_LHASH *lh, void *data)
  184. {
  185. unsigned long hash;
  186. LHASH_NODE *nn, **rn;
  187. void *ret;
  188. lh->error = 0;
  189. if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)
  190. && !expand(lh))
  191. return NULL;
  192. rn = getrn(lh, data, &hash);
  193. if (*rn == NULL) {
  194. if ((nn = (LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) {
  195. lh->error++;
  196. return (NULL);
  197. }
  198. nn->data = data;
  199. nn->next = NULL;
  200. #ifndef OPENSSL_NO_HASH_COMP
  201. nn->hash = hash;
  202. #endif
  203. *rn = nn;
  204. ret = NULL;
  205. lh->num_insert++;
  206. lh->num_items++;
  207. } else { /* replace same key */
  208. ret = (*rn)->data;
  209. (*rn)->data = data;
  210. lh->num_replace++;
  211. }
  212. return (ret);
  213. }
  214. void *lh_delete(_LHASH *lh, const void *data)
  215. {
  216. unsigned long hash;
  217. LHASH_NODE *nn, **rn;
  218. void *ret;
  219. lh->error = 0;
  220. rn = getrn(lh, data, &hash);
  221. if (*rn == NULL) {
  222. lh->num_no_delete++;
  223. return (NULL);
  224. } else {
  225. nn = *rn;
  226. *rn = nn->next;
  227. ret = nn->data;
  228. OPENSSL_free(nn);
  229. lh->num_delete++;
  230. }
  231. lh->num_items--;
  232. if ((lh->num_nodes > MIN_NODES) &&
  233. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  234. contract(lh);
  235. return (ret);
  236. }
  237. void *lh_retrieve(_LHASH *lh, const void *data)
  238. {
  239. unsigned long hash;
  240. LHASH_NODE **rn;
  241. void *ret;
  242. lh->error = 0;
  243. rn = getrn(lh, data, &hash);
  244. if (*rn == NULL) {
  245. lh->num_retrieve_miss++;
  246. return (NULL);
  247. } else {
  248. ret = (*rn)->data;
  249. lh->num_retrieve++;
  250. }
  251. return (ret);
  252. }
  253. static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
  254. LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
  255. {
  256. int i;
  257. LHASH_NODE *a, *n;
  258. if (lh == NULL)
  259. return;
  260. /*
  261. * reverse the order so we search from 'top to bottom' We were having
  262. * memory leaks otherwise
  263. */
  264. for (i = lh->num_nodes - 1; i >= 0; i--) {
  265. a = lh->b[i];
  266. while (a != NULL) {
  267. /*
  268. * 28/05/91 - eay - n added so items can be deleted via lh_doall
  269. */
  270. /*
  271. * 22/05/08 - ben - eh? since a is not passed, this should not be
  272. * needed
  273. */
  274. n = a->next;
  275. if (use_arg)
  276. func_arg(a->data, arg);
  277. else
  278. func(a->data);
  279. a = n;
  280. }
  281. }
  282. }
  283. void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
  284. {
  285. doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
  286. }
  287. void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
  288. {
  289. doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
  290. }
  291. static int expand(_LHASH *lh)
  292. {
  293. LHASH_NODE **n, **n1, **n2, *np;
  294. unsigned int p, pmax, nni, j;
  295. unsigned long hash;
  296. nni = lh->num_alloc_nodes;
  297. p = lh->p;
  298. pmax = lh->pmax;
  299. if (p + 1 >= pmax) {
  300. j = nni * 2;
  301. n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
  302. if (n == NULL) {
  303. lh->error++;
  304. return 0;
  305. }
  306. lh->b = n;
  307. memset(n + nni, 0, sizeof(*n) * (j - nni));
  308. lh->pmax = nni;
  309. lh->num_alloc_nodes = j;
  310. lh->num_expand_reallocs++;
  311. lh->p = 0;
  312. } else {
  313. lh->p++;
  314. }
  315. lh->num_nodes++;
  316. lh->num_expands++;
  317. n1 = &(lh->b[p]);
  318. n2 = &(lh->b[p + pmax]);
  319. *n2 = NULL;
  320. for (np = *n1; np != NULL;) {
  321. #ifndef OPENSSL_NO_HASH_COMP
  322. hash = np->hash;
  323. #else
  324. hash = lh->hash(np->data);
  325. lh->num_hash_calls++;
  326. #endif
  327. if ((hash % nni) != p) { /* move it */
  328. *n1 = (*n1)->next;
  329. np->next = *n2;
  330. *n2 = np;
  331. } else
  332. n1 = &((*n1)->next);
  333. np = *n1;
  334. }
  335. return 1;
  336. }
  337. static void contract(_LHASH *lh)
  338. {
  339. LHASH_NODE **n, *n1, *np;
  340. np = lh->b[lh->p + lh->pmax - 1];
  341. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  342. if (lh->p == 0) {
  343. n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
  344. (unsigned int)(sizeof(LHASH_NODE *)
  345. * lh->pmax));
  346. if (n == NULL) {
  347. /* fputs("realloc error in lhash",stderr); */
  348. lh->error++;
  349. return;
  350. }
  351. lh->num_contract_reallocs++;
  352. lh->num_alloc_nodes /= 2;
  353. lh->pmax /= 2;
  354. lh->p = lh->pmax - 1;
  355. lh->b = n;
  356. } else
  357. lh->p--;
  358. lh->num_nodes--;
  359. lh->num_contracts++;
  360. n1 = lh->b[(int)lh->p];
  361. if (n1 == NULL)
  362. lh->b[(int)lh->p] = np;
  363. else {
  364. while (n1->next != NULL)
  365. n1 = n1->next;
  366. n1->next = np;
  367. }
  368. }
  369. static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
  370. {
  371. LHASH_NODE **ret, *n1;
  372. unsigned long hash, nn;
  373. LHASH_COMP_FN_TYPE cf;
  374. hash = (*(lh->hash)) (data);
  375. lh->num_hash_calls++;
  376. *rhash = hash;
  377. nn = hash % lh->pmax;
  378. if (nn < lh->p)
  379. nn = hash % lh->num_alloc_nodes;
  380. cf = lh->comp;
  381. ret = &(lh->b[(int)nn]);
  382. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  383. #ifndef OPENSSL_NO_HASH_COMP
  384. lh->num_hash_comps++;
  385. if (n1->hash != hash) {
  386. ret = &(n1->next);
  387. continue;
  388. }
  389. #endif
  390. lh->num_comp_calls++;
  391. if (cf(n1->data, data) == 0)
  392. break;
  393. ret = &(n1->next);
  394. }
  395. return (ret);
  396. }
  397. /*
  398. * The following hash seems to work very well on normal text strings no
  399. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  400. * as good as MD5, but still good.
  401. */
  402. unsigned long lh_strhash(const char *c)
  403. {
  404. unsigned long ret = 0;
  405. long n;
  406. unsigned long v;
  407. int r;
  408. if ((c == NULL) || (*c == '\0'))
  409. return (ret);
  410. /*-
  411. unsigned char b[16];
  412. MD5(c,strlen(c),b);
  413. return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
  414. */
  415. n = 0x100;
  416. while (*c) {
  417. v = n | (*c);
  418. n += 0x100;
  419. r = (int)((v >> 2) ^ v) & 0x0f;
  420. ret = (ret << r) | (ret >> (32 - r));
  421. ret &= 0xFFFFFFFFL;
  422. ret ^= v * v;
  423. c++;
  424. }
  425. return ((ret >> 16) ^ ret);
  426. }
  427. unsigned long lh_num_items(const _LHASH *lh)
  428. {
  429. return lh ? lh->num_items : 0;
  430. }