diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/lhash/lhash.c | 242 |
1 files changed, 119 insertions, 123 deletions
diff --git a/src/lib/libcrypto/lhash/lhash.c b/src/lib/libcrypto/lhash/lhash.c index ded0d00dde..a7e9b86d5a 100644 --- a/src/lib/libcrypto/lhash/lhash.c +++ b/src/lib/libcrypto/lhash/lhash.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: lhash.c,v 1.24 2024/05/06 14:38:20 jsing Exp $ */ | 1 | /* $OpenBSD: lhash.c,v 1.25 2024/05/07 13:40:42 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -110,9 +110,124 @@ | |||
| 110 | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ | 110 | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ |
| 111 | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ | 111 | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ |
| 112 | 112 | ||
| 113 | static void expand(_LHASH *lh); | 113 | static void |
| 114 | static void contract(_LHASH *lh); | 114 | expand(_LHASH *lh) |
| 115 | static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash); | 115 | { |
| 116 | LHASH_NODE **n, **n1, **n2, *np; | ||
| 117 | unsigned int p, i, j; | ||
| 118 | unsigned long hash, nni; | ||
| 119 | |||
| 120 | lh->num_nodes++; | ||
| 121 | lh->num_expands++; | ||
| 122 | p = (int)lh->p++; | ||
| 123 | n1 = &(lh->b[p]); | ||
| 124 | n2 = &(lh->b[p + (int)lh->pmax]); | ||
| 125 | *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */ | ||
| 126 | nni = lh->num_alloc_nodes; | ||
| 127 | |||
| 128 | for (np = *n1; np != NULL; ) { | ||
| 129 | #ifndef OPENSSL_NO_HASH_COMP | ||
| 130 | hash = np->hash; | ||
| 131 | #else | ||
| 132 | hash = lh->hash(np->data); | ||
| 133 | lh->num_hash_calls++; | ||
| 134 | #endif | ||
| 135 | if ((hash % nni) != p) { /* move it */ | ||
| 136 | *n1 = (*n1)->next; | ||
| 137 | np->next= *n2; | ||
| 138 | *n2 = np; | ||
| 139 | } else | ||
| 140 | n1 = &((*n1)->next); | ||
| 141 | np= *n1; | ||
| 142 | } | ||
| 143 | |||
| 144 | if ((lh->p) >= lh->pmax) { | ||
| 145 | j = (int)lh->num_alloc_nodes * 2; | ||
| 146 | n = reallocarray(lh->b, j, sizeof(LHASH_NODE *)); | ||
| 147 | if (n == NULL) { | ||
| 148 | /* fputs("realloc error in lhash", stderr); */ | ||
| 149 | lh->error++; | ||
| 150 | lh->p = 0; | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | /* else */ | ||
| 154 | for (i = (int)lh->num_alloc_nodes; i < j; i++)/* 26/02/92 eay */ | ||
| 155 | n[i] = NULL; /* 02/03/92 eay */ | ||
| 156 | lh->pmax = lh->num_alloc_nodes; | ||
| 157 | lh->num_alloc_nodes = j; | ||
| 158 | lh->num_expand_reallocs++; | ||
| 159 | lh->p = 0; | ||
| 160 | lh->b = n; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | static void | ||
| 165 | contract(_LHASH *lh) | ||
| 166 | { | ||
| 167 | LHASH_NODE **n, *n1, *np; | ||
| 168 | |||
| 169 | np = lh->b[lh->p + lh->pmax - 1]; | ||
| 170 | lh->b[lh->p+lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ | ||
| 171 | if (lh->p == 0) { | ||
| 172 | n = reallocarray(lh->b, lh->pmax, sizeof(LHASH_NODE *)); | ||
| 173 | if (n == NULL) { | ||
| 174 | /* fputs("realloc error in lhash", stderr); */ | ||
| 175 | lh->error++; | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | lh->num_contract_reallocs++; | ||
| 179 | lh->num_alloc_nodes /= 2; | ||
| 180 | lh->pmax /= 2; | ||
| 181 | lh->p = lh->pmax - 1; | ||
| 182 | lh->b = n; | ||
| 183 | } else | ||
| 184 | lh->p--; | ||
| 185 | |||
| 186 | lh->num_nodes--; | ||
| 187 | lh->num_contracts++; | ||
| 188 | |||
| 189 | n1 = lh->b[(int)lh->p]; | ||
| 190 | if (n1 == NULL) | ||
| 191 | lh->b[(int)lh->p] = np; | ||
| 192 | else { | ||
| 193 | while (n1->next != NULL) | ||
| 194 | n1 = n1->next; | ||
| 195 | n1->next = np; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | static LHASH_NODE ** | ||
| 200 | getrn(_LHASH *lh, const void *data, unsigned long *rhash) | ||
| 201 | { | ||
| 202 | LHASH_NODE **ret, *n1; | ||
| 203 | unsigned long hash, nn; | ||
| 204 | LHASH_COMP_FN_TYPE cf; | ||
| 205 | |||
| 206 | hash = (*(lh->hash))(data); | ||
| 207 | lh->num_hash_calls++; | ||
| 208 | *rhash = hash; | ||
| 209 | |||
| 210 | nn = hash % lh->pmax; | ||
| 211 | if (nn < lh->p) | ||
| 212 | nn = hash % lh->num_alloc_nodes; | ||
| 213 | |||
| 214 | cf = lh->comp; | ||
| 215 | ret = &(lh->b[(int)nn]); | ||
| 216 | for (n1 = *ret; n1 != NULL; n1 = n1->next) { | ||
| 217 | #ifndef OPENSSL_NO_HASH_COMP | ||
| 218 | lh->num_hash_comps++; | ||
| 219 | if (n1->hash != hash) { | ||
| 220 | ret = &(n1->next); | ||
| 221 | continue; | ||
| 222 | } | ||
| 223 | #endif | ||
| 224 | lh->num_comp_calls++; | ||
| 225 | if (cf(n1->data, data) == 0) | ||
| 226 | break; | ||
| 227 | ret = &(n1->next); | ||
| 228 | } | ||
| 229 | return (ret); | ||
| 230 | } | ||
| 116 | 231 | ||
| 117 | _LHASH * | 232 | _LHASH * |
| 118 | lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c) | 233 | lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c) |
| @@ -313,125 +428,6 @@ lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg) | |||
| 313 | } | 428 | } |
| 314 | LCRYPTO_ALIAS(lh_doall_arg); | 429 | LCRYPTO_ALIAS(lh_doall_arg); |
| 315 | 430 | ||
| 316 | static void | ||
| 317 | expand(_LHASH *lh) | ||
| 318 | { | ||
| 319 | LHASH_NODE **n, **n1, **n2, *np; | ||
| 320 | unsigned int p, i, j; | ||
| 321 | unsigned long hash, nni; | ||
| 322 | |||
| 323 | lh->num_nodes++; | ||
| 324 | lh->num_expands++; | ||
| 325 | p = (int)lh->p++; | ||
| 326 | n1 = &(lh->b[p]); | ||
| 327 | n2 = &(lh->b[p + (int)lh->pmax]); | ||
| 328 | *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */ | ||
| 329 | nni = lh->num_alloc_nodes; | ||
| 330 | |||
| 331 | for (np = *n1; np != NULL; ) { | ||
| 332 | #ifndef OPENSSL_NO_HASH_COMP | ||
| 333 | hash = np->hash; | ||
| 334 | #else | ||
| 335 | hash = lh->hash(np->data); | ||
| 336 | lh->num_hash_calls++; | ||
| 337 | #endif | ||
| 338 | if ((hash % nni) != p) { /* move it */ | ||
| 339 | *n1 = (*n1)->next; | ||
| 340 | np->next= *n2; | ||
| 341 | *n2 = np; | ||
| 342 | } else | ||
| 343 | n1 = &((*n1)->next); | ||
| 344 | np= *n1; | ||
| 345 | } | ||
| 346 | |||
| 347 | if ((lh->p) >= lh->pmax) { | ||
| 348 | j = (int)lh->num_alloc_nodes * 2; | ||
| 349 | n = reallocarray(lh->b, j, sizeof(LHASH_NODE *)); | ||
| 350 | if (n == NULL) { | ||
| 351 | /* fputs("realloc error in lhash", stderr); */ | ||
| 352 | lh->error++; | ||
| 353 | lh->p = 0; | ||
| 354 | return; | ||
| 355 | } | ||
| 356 | /* else */ | ||
| 357 | for (i = (int)lh->num_alloc_nodes; i < j; i++)/* 26/02/92 eay */ | ||
| 358 | n[i] = NULL; /* 02/03/92 eay */ | ||
| 359 | lh->pmax = lh->num_alloc_nodes; | ||
| 360 | lh->num_alloc_nodes = j; | ||
| 361 | lh->num_expand_reallocs++; | ||
| 362 | lh->p = 0; | ||
| 363 | lh->b = n; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | static void | ||
| 368 | contract(_LHASH *lh) | ||
| 369 | { | ||
| 370 | LHASH_NODE **n, *n1, *np; | ||
| 371 | |||
| 372 | np = lh->b[lh->p + lh->pmax - 1]; | ||
| 373 | lh->b[lh->p+lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ | ||
| 374 | if (lh->p == 0) { | ||
| 375 | n = reallocarray(lh->b, lh->pmax, sizeof(LHASH_NODE *)); | ||
| 376 | if (n == NULL) { | ||
| 377 | /* fputs("realloc error in lhash", stderr); */ | ||
| 378 | lh->error++; | ||
| 379 | return; | ||
| 380 | } | ||
| 381 | lh->num_contract_reallocs++; | ||
| 382 | lh->num_alloc_nodes /= 2; | ||
| 383 | lh->pmax /= 2; | ||
| 384 | lh->p = lh->pmax - 1; | ||
| 385 | lh->b = n; | ||
| 386 | } else | ||
| 387 | lh->p--; | ||
| 388 | |||
| 389 | lh->num_nodes--; | ||
| 390 | lh->num_contracts++; | ||
| 391 | |||
| 392 | n1 = lh->b[(int)lh->p]; | ||
| 393 | if (n1 == NULL) | ||
| 394 | lh->b[(int)lh->p] = np; | ||
| 395 | else { | ||
| 396 | while (n1->next != NULL) | ||
| 397 | n1 = n1->next; | ||
| 398 | n1->next = np; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | static LHASH_NODE ** | ||
| 403 | getrn(_LHASH *lh, const void *data, unsigned long *rhash) | ||
| 404 | { | ||
| 405 | LHASH_NODE **ret, *n1; | ||
| 406 | unsigned long hash, nn; | ||
| 407 | LHASH_COMP_FN_TYPE cf; | ||
| 408 | |||
| 409 | hash = (*(lh->hash))(data); | ||
| 410 | lh->num_hash_calls++; | ||
| 411 | *rhash = hash; | ||
| 412 | |||
| 413 | nn = hash % lh->pmax; | ||
| 414 | if (nn < lh->p) | ||
| 415 | nn = hash % lh->num_alloc_nodes; | ||
| 416 | |||
| 417 | cf = lh->comp; | ||
| 418 | ret = &(lh->b[(int)nn]); | ||
| 419 | for (n1 = *ret; n1 != NULL; n1 = n1->next) { | ||
| 420 | #ifndef OPENSSL_NO_HASH_COMP | ||
| 421 | lh->num_hash_comps++; | ||
| 422 | if (n1->hash != hash) { | ||
| 423 | ret = &(n1->next); | ||
| 424 | continue; | ||
| 425 | } | ||
| 426 | #endif | ||
| 427 | lh->num_comp_calls++; | ||
| 428 | if (cf(n1->data, data) == 0) | ||
| 429 | break; | ||
| 430 | ret = &(n1->next); | ||
| 431 | } | ||
| 432 | return (ret); | ||
| 433 | } | ||
| 434 | |||
| 435 | /* The following hash seems to work very well on normal text strings | 431 | /* The following hash seems to work very well on normal text strings |
| 436 | * no collisions on /usr/dict/words and it distributes on %2^n quite | 432 | * no collisions on /usr/dict/words and it distributes on %2^n quite |
| 437 | * well, not as good as MD5, but still good. | 433 | * well, not as good as MD5, but still good. |
