lhash.c 9.7 KB

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