lhash.c 12 KB

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