lhash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* crypto/lhash/lhash.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  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 (tjh@cryptsoft.com).
  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 (eay@cryptsoft.com)"
  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 (tjh@cryptsoft.com)"
  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. const char lh_version[] = "lhash" OPENSSL_VERSION_PTEXT;
  103. #undef MIN_NODES
  104. #define MIN_NODES 16
  105. #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
  106. #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
  107. static void expand(_LHASH *lh);
  108. static void contract(_LHASH *lh);
  109. static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
  110. _LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
  111. {
  112. _LHASH *ret;
  113. int i;
  114. if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)
  115. goto err0;
  116. if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)
  117. goto err1;
  118. for (i = 0; i < MIN_NODES; i++)
  119. ret->b[i] = NULL;
  120. ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
  121. ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
  122. ret->num_nodes = MIN_NODES / 2;
  123. ret->num_alloc_nodes = MIN_NODES;
  124. ret->p = 0;
  125. ret->pmax = MIN_NODES / 2;
  126. ret->up_load = UP_LOAD;
  127. ret->down_load = DOWN_LOAD;
  128. ret->num_items = 0;
  129. ret->num_expands = 0;
  130. ret->num_expand_reallocs = 0;
  131. ret->num_contracts = 0;
  132. ret->num_contract_reallocs = 0;
  133. ret->num_hash_calls = 0;
  134. ret->num_comp_calls = 0;
  135. ret->num_insert = 0;
  136. ret->num_replace = 0;
  137. ret->num_delete = 0;
  138. ret->num_no_delete = 0;
  139. ret->num_retrieve = 0;
  140. ret->num_retrieve_miss = 0;
  141. ret->num_hash_comps = 0;
  142. ret->error = 0;
  143. return (ret);
  144. err1:
  145. OPENSSL_free(ret);
  146. err0:
  147. return (NULL);
  148. }
  149. void lh_free(_LHASH *lh)
  150. {
  151. unsigned int i;
  152. LHASH_NODE *n, *nn;
  153. if (lh == NULL)
  154. return;
  155. for (i = 0; i < lh->num_nodes; i++) {
  156. n = lh->b[i];
  157. while (n != NULL) {
  158. nn = n->next;
  159. OPENSSL_free(n);
  160. n = nn;
  161. }
  162. }
  163. OPENSSL_free(lh->b);
  164. OPENSSL_free(lh);
  165. }
  166. void *lh_insert(_LHASH *lh, void *data)
  167. {
  168. unsigned long hash;
  169. LHASH_NODE *nn, **rn;
  170. void *ret;
  171. lh->error = 0;
  172. if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
  173. expand(lh);
  174. rn = getrn(lh, data, &hash);
  175. if (*rn == NULL) {
  176. if ((nn = (LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) {
  177. lh->error++;
  178. return (NULL);
  179. }
  180. nn->data = data;
  181. nn->next = NULL;
  182. #ifndef OPENSSL_NO_HASH_COMP
  183. nn->hash = hash;
  184. #endif
  185. *rn = nn;
  186. ret = NULL;
  187. lh->num_insert++;
  188. lh->num_items++;
  189. } else { /* replace same key */
  190. ret = (*rn)->data;
  191. (*rn)->data = data;
  192. lh->num_replace++;
  193. }
  194. return (ret);
  195. }
  196. void *lh_delete(_LHASH *lh, const void *data)
  197. {
  198. unsigned long hash;
  199. LHASH_NODE *nn, **rn;
  200. void *ret;
  201. lh->error = 0;
  202. rn = getrn(lh, data, &hash);
  203. if (*rn == NULL) {
  204. lh->num_no_delete++;
  205. return (NULL);
  206. } else {
  207. nn = *rn;
  208. *rn = nn->next;
  209. ret = nn->data;
  210. OPENSSL_free(nn);
  211. lh->num_delete++;
  212. }
  213. lh->num_items--;
  214. if ((lh->num_nodes > MIN_NODES) &&
  215. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  216. contract(lh);
  217. return (ret);
  218. }
  219. void *lh_retrieve(_LHASH *lh, const void *data)
  220. {
  221. unsigned long hash;
  222. LHASH_NODE **rn;
  223. void *ret;
  224. lh->error = 0;
  225. rn = getrn(lh, data, &hash);
  226. if (*rn == NULL) {
  227. lh->num_retrieve_miss++;
  228. return (NULL);
  229. } else {
  230. ret = (*rn)->data;
  231. lh->num_retrieve++;
  232. }
  233. return (ret);
  234. }
  235. static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
  236. LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
  237. {
  238. int i;
  239. LHASH_NODE *a, *n;
  240. if (lh == NULL)
  241. return;
  242. /*
  243. * reverse the order so we search from 'top to bottom' We were having
  244. * memory leaks otherwise
  245. */
  246. for (i = lh->num_nodes - 1; i >= 0; i--) {
  247. a = lh->b[i];
  248. while (a != NULL) {
  249. /*
  250. * 28/05/91 - eay - n added so items can be deleted via lh_doall
  251. */
  252. /*
  253. * 22/05/08 - ben - eh? since a is not passed, this should not be
  254. * needed
  255. */
  256. n = a->next;
  257. if (use_arg)
  258. func_arg(a->data, arg);
  259. else
  260. func(a->data);
  261. a = n;
  262. }
  263. }
  264. }
  265. void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
  266. {
  267. doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
  268. }
  269. void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
  270. {
  271. doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
  272. }
  273. static void expand(_LHASH *lh)
  274. {
  275. LHASH_NODE **n, **n1, **n2, *np;
  276. unsigned int p, i, j;
  277. unsigned long hash, nni;
  278. lh->num_nodes++;
  279. lh->num_expands++;
  280. p = (int)lh->p++;
  281. n1 = &(lh->b[p]);
  282. n2 = &(lh->b[p + (int)lh->pmax]);
  283. *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */
  284. nni = lh->num_alloc_nodes;
  285. for (np = *n1; np != NULL;) {
  286. #ifndef OPENSSL_NO_HASH_COMP
  287. hash = np->hash;
  288. #else
  289. hash = lh->hash(np->data);
  290. lh->num_hash_calls++;
  291. #endif
  292. if ((hash % nni) != p) { /* move it */
  293. *n1 = (*n1)->next;
  294. np->next = *n2;
  295. *n2 = np;
  296. } else
  297. n1 = &((*n1)->next);
  298. np = *n1;
  299. }
  300. if ((lh->p) >= lh->pmax) {
  301. j = (int)lh->num_alloc_nodes * 2;
  302. n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
  303. (int)(sizeof(LHASH_NODE *) * j));
  304. if (n == NULL) {
  305. lh->error++;
  306. lh->num_nodes--;
  307. lh->p = 0;
  308. return;
  309. }
  310. /* else */
  311. for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
  312. n[i] = NULL; /* 02/03/92 eay */
  313. lh->pmax = lh->num_alloc_nodes;
  314. lh->num_alloc_nodes = j;
  315. lh->num_expand_reallocs++;
  316. lh->p = 0;
  317. lh->b = n;
  318. }
  319. }
  320. static void contract(_LHASH *lh)
  321. {
  322. LHASH_NODE **n, *n1, *np;
  323. np = lh->b[lh->p + lh->pmax - 1];
  324. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  325. if (lh->p == 0) {
  326. n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
  327. (unsigned int)(sizeof(LHASH_NODE *)
  328. * lh->pmax));
  329. if (n == NULL) {
  330. /* fputs("realloc error in lhash",stderr); */
  331. lh->error++;
  332. return;
  333. }
  334. lh->num_contract_reallocs++;
  335. lh->num_alloc_nodes /= 2;
  336. lh->pmax /= 2;
  337. lh->p = lh->pmax - 1;
  338. lh->b = n;
  339. } else
  340. lh->p--;
  341. lh->num_nodes--;
  342. lh->num_contracts++;
  343. n1 = lh->b[(int)lh->p];
  344. if (n1 == NULL)
  345. lh->b[(int)lh->p] = np;
  346. else {
  347. while (n1->next != NULL)
  348. n1 = n1->next;
  349. n1->next = np;
  350. }
  351. }
  352. static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
  353. {
  354. LHASH_NODE **ret, *n1;
  355. unsigned long hash, nn;
  356. LHASH_COMP_FN_TYPE cf;
  357. hash = (*(lh->hash)) (data);
  358. lh->num_hash_calls++;
  359. *rhash = hash;
  360. nn = hash % lh->pmax;
  361. if (nn < lh->p)
  362. nn = hash % lh->num_alloc_nodes;
  363. cf = lh->comp;
  364. ret = &(lh->b[(int)nn]);
  365. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  366. #ifndef OPENSSL_NO_HASH_COMP
  367. lh->num_hash_comps++;
  368. if (n1->hash != hash) {
  369. ret = &(n1->next);
  370. continue;
  371. }
  372. #endif
  373. lh->num_comp_calls++;
  374. if (cf(n1->data, data) == 0)
  375. break;
  376. ret = &(n1->next);
  377. }
  378. return (ret);
  379. }
  380. /*
  381. * The following hash seems to work very well on normal text strings no
  382. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  383. * as good as MD5, but still good.
  384. */
  385. unsigned long lh_strhash(const char *c)
  386. {
  387. unsigned long ret = 0;
  388. long n;
  389. unsigned long v;
  390. int r;
  391. if ((c == NULL) || (*c == '\0'))
  392. return (ret);
  393. /*-
  394. unsigned char b[16];
  395. MD5(c,strlen(c),b);
  396. return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
  397. */
  398. n = 0x100;
  399. while (*c) {
  400. v = n | (*c);
  401. n += 0x100;
  402. r = (int)((v >> 2) ^ v) & 0x0f;
  403. ret = (ret << r) | (ret >> (32 - r));
  404. ret &= 0xFFFFFFFFL;
  405. ret ^= v * v;
  406. c++;
  407. }
  408. return ((ret >> 16) ^ ret);
  409. }
  410. unsigned long lh_num_items(const _LHASH *lh)
  411. {
  412. return lh ? lh->num_items : 0;
  413. }