lhash.c 13 KB

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