lhash.c 9.9 KB

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