2
0

lhash.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "internal/ctype.h"
  16. #include "internal/lhash.h"
  17. #include "lhash_lcl.h"
  18. /*
  19. * A hashing implementation that appears to be based on the linear hashing
  20. * alogrithm:
  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. * http://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. unsigned int i;
  71. OPENSSL_LH_NODE *n, *nn;
  72. if (lh == NULL)
  73. return;
  74. for (i = 0; i < lh->num_nodes; i++) {
  75. n = lh->b[i];
  76. while (n != NULL) {
  77. nn = n->next;
  78. OPENSSL_free(n);
  79. n = nn;
  80. }
  81. }
  82. OPENSSL_free(lh->b);
  83. OPENSSL_free(lh);
  84. }
  85. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
  86. {
  87. unsigned long hash;
  88. OPENSSL_LH_NODE *nn, **rn;
  89. void *ret;
  90. lh->error = 0;
  91. if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
  92. return NULL; /* 'lh->error++' already done in 'expand' */
  93. rn = getrn(lh, data, &hash);
  94. if (*rn == NULL) {
  95. if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
  96. lh->error++;
  97. return NULL;
  98. }
  99. nn->data = data;
  100. nn->next = NULL;
  101. nn->hash = hash;
  102. *rn = nn;
  103. ret = NULL;
  104. lh->num_insert++;
  105. lh->num_items++;
  106. } else { /* replace same key */
  107. ret = (*rn)->data;
  108. (*rn)->data = data;
  109. lh->num_replace++;
  110. }
  111. return ret;
  112. }
  113. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
  114. {
  115. unsigned long hash;
  116. OPENSSL_LH_NODE *nn, **rn;
  117. void *ret;
  118. lh->error = 0;
  119. rn = getrn(lh, data, &hash);
  120. if (*rn == NULL) {
  121. lh->num_no_delete++;
  122. return NULL;
  123. } else {
  124. nn = *rn;
  125. *rn = nn->next;
  126. ret = nn->data;
  127. OPENSSL_free(nn);
  128. lh->num_delete++;
  129. }
  130. lh->num_items--;
  131. if ((lh->num_nodes > MIN_NODES) &&
  132. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  133. contract(lh);
  134. return ret;
  135. }
  136. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
  137. {
  138. unsigned long hash;
  139. OPENSSL_LH_NODE **rn;
  140. void *ret;
  141. tsan_store((TSAN_QUALIFIER int *)&lh->error, 0);
  142. rn = getrn(lh, data, &hash);
  143. if (*rn == NULL) {
  144. tsan_counter(&lh->num_retrieve_miss);
  145. return NULL;
  146. } else {
  147. ret = (*rn)->data;
  148. tsan_counter(&lh->num_retrieve);
  149. }
  150. return ret;
  151. }
  152. static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
  153. OPENSSL_LH_DOALL_FUNC func,
  154. OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
  155. {
  156. int i;
  157. OPENSSL_LH_NODE *a, *n;
  158. if (lh == NULL)
  159. return;
  160. /*
  161. * reverse the order so we search from 'top to bottom' We were having
  162. * memory leaks otherwise
  163. */
  164. for (i = lh->num_nodes - 1; i >= 0; i--) {
  165. a = lh->b[i];
  166. while (a != NULL) {
  167. n = a->next;
  168. if (use_arg)
  169. func_arg(a->data, arg);
  170. else
  171. func(a->data);
  172. a = n;
  173. }
  174. }
  175. }
  176. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
  177. {
  178. doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
  179. }
  180. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
  181. {
  182. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
  183. }
  184. static int expand(OPENSSL_LHASH *lh)
  185. {
  186. OPENSSL_LH_NODE **n, **n1, **n2, *np;
  187. unsigned int p, pmax, nni, j;
  188. unsigned long hash;
  189. nni = lh->num_alloc_nodes;
  190. p = lh->p;
  191. pmax = lh->pmax;
  192. if (p + 1 >= pmax) {
  193. j = nni * 2;
  194. n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
  195. if (n == NULL) {
  196. lh->error++;
  197. return 0;
  198. }
  199. lh->b = n;
  200. memset(n + nni, 0, sizeof(*n) * (j - nni));
  201. lh->pmax = nni;
  202. lh->num_alloc_nodes = j;
  203. lh->num_expand_reallocs++;
  204. lh->p = 0;
  205. } else {
  206. lh->p++;
  207. }
  208. lh->num_nodes++;
  209. lh->num_expands++;
  210. n1 = &(lh->b[p]);
  211. n2 = &(lh->b[p + pmax]);
  212. *n2 = NULL;
  213. for (np = *n1; np != NULL;) {
  214. hash = np->hash;
  215. if ((hash % nni) != p) { /* move it */
  216. *n1 = (*n1)->next;
  217. np->next = *n2;
  218. *n2 = np;
  219. } else
  220. n1 = &((*n1)->next);
  221. np = *n1;
  222. }
  223. return 1;
  224. }
  225. static void contract(OPENSSL_LHASH *lh)
  226. {
  227. OPENSSL_LH_NODE **n, *n1, *np;
  228. np = lh->b[lh->p + lh->pmax - 1];
  229. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  230. if (lh->p == 0) {
  231. n = OPENSSL_realloc(lh->b,
  232. (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
  233. if (n == NULL) {
  234. /* fputs("realloc error in lhash",stderr); */
  235. lh->error++;
  236. return;
  237. }
  238. lh->num_contract_reallocs++;
  239. lh->num_alloc_nodes /= 2;
  240. lh->pmax /= 2;
  241. lh->p = lh->pmax - 1;
  242. lh->b = n;
  243. } else
  244. lh->p--;
  245. lh->num_nodes--;
  246. lh->num_contracts++;
  247. n1 = lh->b[(int)lh->p];
  248. if (n1 == NULL)
  249. lh->b[(int)lh->p] = np;
  250. else {
  251. while (n1->next != NULL)
  252. n1 = n1->next;
  253. n1->next = np;
  254. }
  255. }
  256. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
  257. const void *data, unsigned long *rhash)
  258. {
  259. OPENSSL_LH_NODE **ret, *n1;
  260. unsigned long hash, nn;
  261. OPENSSL_LH_COMPFUNC cf;
  262. hash = (*(lh->hash)) (data);
  263. tsan_counter(&lh->num_hash_calls);
  264. *rhash = hash;
  265. nn = hash % lh->pmax;
  266. if (nn < lh->p)
  267. nn = hash % lh->num_alloc_nodes;
  268. cf = lh->comp;
  269. ret = &(lh->b[(int)nn]);
  270. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  271. tsan_counter(&lh->num_hash_comps);
  272. if (n1->hash != hash) {
  273. ret = &(n1->next);
  274. continue;
  275. }
  276. tsan_counter(&lh->num_comp_calls);
  277. if (cf(n1->data, data) == 0)
  278. break;
  279. ret = &(n1->next);
  280. }
  281. return ret;
  282. }
  283. /*
  284. * The following hash seems to work very well on normal text strings no
  285. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  286. * as good as MD5, but still good.
  287. */
  288. unsigned long OPENSSL_LH_strhash(const char *c)
  289. {
  290. unsigned long ret = 0;
  291. long n;
  292. unsigned long v;
  293. int r;
  294. if ((c == NULL) || (*c == '\0'))
  295. return ret;
  296. n = 0x100;
  297. while (*c) {
  298. v = n | (*c);
  299. n += 0x100;
  300. r = (int)((v >> 2) ^ v) & 0x0f;
  301. ret = (ret << r) | (ret >> (32 - r));
  302. ret &= 0xFFFFFFFFL;
  303. ret ^= v * v;
  304. c++;
  305. }
  306. return (ret >> 16) ^ ret;
  307. }
  308. unsigned long openssl_lh_strcasehash(const char *c)
  309. {
  310. unsigned long ret = 0;
  311. long n;
  312. unsigned long v;
  313. int r;
  314. if (c == NULL || *c == '\0')
  315. return ret;
  316. for (n = 0x100; *c != '\0'; n += 0x100) {
  317. v = n | ossl_tolower(*c);
  318. r = (int)((v >> 2) ^ v) & 0x0f;
  319. ret = (ret << r) | (ret >> (32 - r));
  320. ret &= 0xFFFFFFFFL;
  321. ret ^= v * v;
  322. c++;
  323. }
  324. return (ret >> 16) ^ ret;
  325. }
  326. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
  327. {
  328. return lh ? lh->num_items : 0;
  329. }
  330. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
  331. {
  332. return lh->down_load;
  333. }
  334. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
  335. {
  336. lh->down_load = down_load;
  337. }
  338. int OPENSSL_LH_error(OPENSSL_LHASH *lh)
  339. {
  340. return lh->error;
  341. }