lhash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 1995-2024 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_set_thunks(OPENSSL_LHASH *lh,
  43. OPENSSL_LH_HASHFUNCTHUNK hw,
  44. OPENSSL_LH_COMPFUNCTHUNK cw,
  45. OPENSSL_LH_DOALL_FUNC_THUNK daw,
  46. OPENSSL_LH_DOALL_FUNCARG_THUNK daaw)
  47. {
  48. if (lh == NULL)
  49. return NULL;
  50. lh->compw = cw;
  51. lh->hashw = hw;
  52. lh->daw = daw;
  53. lh->daaw = daaw;
  54. return lh;
  55. }
  56. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
  57. {
  58. OPENSSL_LHASH *ret;
  59. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  60. return NULL;
  61. if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
  62. goto err;
  63. ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
  64. ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
  65. ret->num_nodes = MIN_NODES / 2;
  66. ret->num_alloc_nodes = MIN_NODES;
  67. ret->pmax = MIN_NODES / 2;
  68. ret->up_load = UP_LOAD;
  69. ret->down_load = DOWN_LOAD;
  70. return ret;
  71. err:
  72. OPENSSL_free(ret->b);
  73. OPENSSL_free(ret);
  74. return NULL;
  75. }
  76. void OPENSSL_LH_free(OPENSSL_LHASH *lh)
  77. {
  78. if (lh == NULL)
  79. return;
  80. OPENSSL_LH_flush(lh);
  81. OPENSSL_free(lh->b);
  82. OPENSSL_free(lh);
  83. }
  84. void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
  85. {
  86. unsigned int i;
  87. OPENSSL_LH_NODE *n, *nn;
  88. if (lh == NULL)
  89. return;
  90. for (i = 0; i < lh->num_nodes; i++) {
  91. n = lh->b[i];
  92. while (n != NULL) {
  93. nn = n->next;
  94. OPENSSL_free(n);
  95. n = nn;
  96. }
  97. lh->b[i] = NULL;
  98. }
  99. lh->num_items = 0;
  100. }
  101. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
  102. {
  103. unsigned long hash;
  104. OPENSSL_LH_NODE *nn, **rn;
  105. void *ret;
  106. lh->error = 0;
  107. if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
  108. return NULL; /* 'lh->error++' already done in 'expand' */
  109. rn = getrn(lh, data, &hash);
  110. if (*rn == NULL) {
  111. if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
  112. lh->error++;
  113. return NULL;
  114. }
  115. nn->data = data;
  116. nn->next = NULL;
  117. nn->hash = hash;
  118. *rn = nn;
  119. ret = NULL;
  120. lh->num_items++;
  121. } else { /* replace same key */
  122. ret = (*rn)->data;
  123. (*rn)->data = data;
  124. }
  125. return ret;
  126. }
  127. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
  128. {
  129. unsigned long hash;
  130. OPENSSL_LH_NODE *nn, **rn;
  131. void *ret;
  132. lh->error = 0;
  133. rn = getrn(lh, data, &hash);
  134. if (*rn == NULL) {
  135. return NULL;
  136. } else {
  137. nn = *rn;
  138. *rn = nn->next;
  139. ret = nn->data;
  140. OPENSSL_free(nn);
  141. }
  142. lh->num_items--;
  143. if ((lh->num_nodes > MIN_NODES) &&
  144. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  145. contract(lh);
  146. return ret;
  147. }
  148. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
  149. {
  150. unsigned long hash;
  151. OPENSSL_LH_NODE **rn;
  152. if (lh->error != 0)
  153. lh->error = 0;
  154. rn = getrn(lh, data, &hash);
  155. return *rn == NULL ? NULL : (*rn)->data;
  156. }
  157. static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
  158. OPENSSL_LH_DOALL_FUNC_THUNK wfunc,
  159. OPENSSL_LH_DOALL_FUNC func,
  160. OPENSSL_LH_DOALL_FUNCARG func_arg,
  161. OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg,
  162. void *arg)
  163. {
  164. int i;
  165. OPENSSL_LH_NODE *a, *n;
  166. if (lh == NULL)
  167. return;
  168. /*
  169. * reverse the order so we search from 'top to bottom' We were having
  170. * memory leaks otherwise
  171. */
  172. for (i = lh->num_nodes - 1; i >= 0; i--) {
  173. a = lh->b[i];
  174. while (a != NULL) {
  175. n = a->next;
  176. if (use_arg)
  177. wfunc_arg(a->data, arg, func_arg);
  178. else
  179. wfunc(a->data, func);
  180. a = n;
  181. }
  182. }
  183. }
  184. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
  185. {
  186. if (lh == NULL)
  187. return;
  188. doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL,
  189. (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL);
  190. }
  191. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh,
  192. OPENSSL_LH_DOALL_FUNCARG func, void *arg)
  193. {
  194. if (lh == NULL)
  195. return;
  196. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
  197. (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg);
  198. }
  199. void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh,
  200. OPENSSL_LH_DOALL_FUNCARG_THUNK daaw,
  201. OPENSSL_LH_DOALL_FUNCARG fn, void *arg)
  202. {
  203. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
  204. (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg);
  205. }
  206. static int expand(OPENSSL_LHASH *lh)
  207. {
  208. OPENSSL_LH_NODE **n, **n1, **n2, *np;
  209. unsigned int p, pmax, nni, j;
  210. unsigned long hash;
  211. nni = lh->num_alloc_nodes;
  212. p = lh->p;
  213. pmax = lh->pmax;
  214. if (p + 1 >= pmax) {
  215. j = nni * 2;
  216. n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
  217. if (n == NULL) {
  218. lh->error++;
  219. return 0;
  220. }
  221. lh->b = n;
  222. memset(n + nni, 0, sizeof(*n) * (j - nni));
  223. lh->pmax = nni;
  224. lh->num_alloc_nodes = j;
  225. lh->p = 0;
  226. } else {
  227. lh->p++;
  228. }
  229. lh->num_nodes++;
  230. n1 = &(lh->b[p]);
  231. n2 = &(lh->b[p + pmax]);
  232. *n2 = NULL;
  233. for (np = *n1; np != NULL;) {
  234. hash = np->hash;
  235. if ((hash % nni) != p) { /* move it */
  236. *n1 = (*n1)->next;
  237. np->next = *n2;
  238. *n2 = np;
  239. } else
  240. n1 = &((*n1)->next);
  241. np = *n1;
  242. }
  243. return 1;
  244. }
  245. static void contract(OPENSSL_LHASH *lh)
  246. {
  247. OPENSSL_LH_NODE **n, *n1, *np;
  248. np = lh->b[lh->p + lh->pmax - 1];
  249. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  250. if (lh->p == 0) {
  251. n = OPENSSL_realloc(lh->b,
  252. (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
  253. if (n == NULL) {
  254. /* fputs("realloc error in lhash", stderr); */
  255. lh->error++;
  256. } else {
  257. lh->b = n;
  258. }
  259. lh->num_alloc_nodes /= 2;
  260. lh->pmax /= 2;
  261. lh->p = lh->pmax - 1;
  262. } else
  263. lh->p--;
  264. lh->num_nodes--;
  265. n1 = lh->b[(int)lh->p];
  266. if (n1 == NULL)
  267. lh->b[(int)lh->p] = np;
  268. else {
  269. while (n1->next != NULL)
  270. n1 = n1->next;
  271. n1->next = np;
  272. }
  273. }
  274. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
  275. const void *data, unsigned long *rhash)
  276. {
  277. OPENSSL_LH_NODE **ret, *n1;
  278. unsigned long hash, nn;
  279. if (lh->hashw != NULL)
  280. hash = lh->hashw(data, lh->hash);
  281. else
  282. hash = lh->hash(data);
  283. *rhash = hash;
  284. nn = hash % lh->pmax;
  285. if (nn < lh->p)
  286. nn = hash % lh->num_alloc_nodes;
  287. ret = &(lh->b[(int)nn]);
  288. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  289. if (n1->hash != hash) {
  290. ret = &(n1->next);
  291. continue;
  292. }
  293. if (lh->compw != NULL) {
  294. if (lh->compw(n1->data, data, lh->comp) == 0)
  295. break;
  296. } else {
  297. if (lh->comp(n1->data, data) == 0)
  298. break;
  299. }
  300. ret = &(n1->next);
  301. }
  302. return ret;
  303. }
  304. /*
  305. * The following hash seems to work very well on normal text strings no
  306. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  307. * as good as MD5, but still good.
  308. */
  309. unsigned long OPENSSL_LH_strhash(const char *c)
  310. {
  311. unsigned long ret = 0;
  312. long n;
  313. unsigned long v;
  314. int r;
  315. if ((c == NULL) || (*c == '\0'))
  316. return ret;
  317. n = 0x100;
  318. while (*c) {
  319. v = n | (*c);
  320. n += 0x100;
  321. r = (int)((v >> 2) ^ v) & 0x0f;
  322. /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
  323. ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
  324. ret &= 0xFFFFFFFFL;
  325. ret ^= v * v;
  326. c++;
  327. }
  328. return (ret >> 16) ^ ret;
  329. }
  330. /*
  331. * Case insensitive string hashing.
  332. *
  333. * The lower/upper case bit is masked out (forcing all letters to be capitals).
  334. * The major side effect on non-alpha characters is mapping the symbols and
  335. * digits into the control character range (which should be harmless).
  336. * The duplication (with respect to the hash value) of printable characters
  337. * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
  338. * respectively (which seems tolerable).
  339. *
  340. * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
  341. * characters. The only duplication is '0' mapping to '^', which is better
  342. * than for ASCII.
  343. */
  344. unsigned long ossl_lh_strcasehash(const char *c)
  345. {
  346. unsigned long ret = 0;
  347. long n;
  348. unsigned long v;
  349. int r;
  350. #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
  351. const long int case_adjust = ~0x40;
  352. #else
  353. const long int case_adjust = ~0x20;
  354. #endif
  355. if (c == NULL || *c == '\0')
  356. return ret;
  357. for (n = 0x100; *c != '\0'; n += 0x100) {
  358. v = n | (case_adjust & *c);
  359. r = (int)((v >> 2) ^ v) & 0x0f;
  360. /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
  361. ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
  362. ret &= 0xFFFFFFFFL;
  363. ret ^= v * v;
  364. c++;
  365. }
  366. return (ret >> 16) ^ ret;
  367. }
  368. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
  369. {
  370. return lh ? lh->num_items : 0;
  371. }
  372. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
  373. {
  374. return lh->down_load;
  375. }
  376. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
  377. {
  378. lh->down_load = down_load;
  379. }
  380. int OPENSSL_LH_error(OPENSSL_LHASH *lh)
  381. {
  382. return lh->error;
  383. }