aboutsummaryrefslogtreecommitdiff
path: root/openssl-1.1.0h/crypto/lhash
diff options
context:
space:
mode:
Diffstat (limited to 'openssl-1.1.0h/crypto/lhash')
-rw-r--r--openssl-1.1.0h/crypto/lhash/build.info3
-rw-r--r--openssl-1.1.0h/crypto/lhash/lh_stats.c130
-rw-r--r--openssl-1.1.0h/crypto/lhash/lhash.c372
-rw-r--r--openssl-1.1.0h/crypto/lhash/lhash_lcl.h49
-rw-r--r--openssl-1.1.0h/crypto/lhash/num.pl23
5 files changed, 577 insertions, 0 deletions
diff --git a/openssl-1.1.0h/crypto/lhash/build.info b/openssl-1.1.0h/crypto/lhash/build.info
new file mode 100644
index 0000000..30797f2
--- /dev/null
+++ b/openssl-1.1.0h/crypto/lhash/build.info
@@ -0,0 +1,3 @@
+LIBS=../../libcrypto
+SOURCE[../../libcrypto]=\
+ lhash.c lh_stats.c
diff --git a/openssl-1.1.0h/crypto/lhash/lh_stats.c b/openssl-1.1.0h/crypto/lhash/lh_stats.c
new file mode 100644
index 0000000..5586afa
--- /dev/null
+++ b/openssl-1.1.0h/crypto/lhash/lh_stats.c
@@ -0,0 +1,130 @@
+/*
+ * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+/*
+ * If you wish to build this outside of OpenSSL, remove the following lines
+ * and things should work as expected
+ */
+#include "internal/cryptlib.h"
+
+#include <openssl/bio.h>
+#include <openssl/lhash.h>
+#include "lhash_lcl.h"
+
+# ifndef OPENSSL_NO_STDIO
+void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp)
+{
+ BIO *bp;
+
+ bp = BIO_new(BIO_s_file());
+ if (bp == NULL)
+ return;
+ BIO_set_fp(bp, fp, BIO_NOCLOSE);
+ OPENSSL_LH_stats_bio(lh, bp);
+ BIO_free(bp);
+}
+
+void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp)
+{
+ BIO *bp;
+
+ bp = BIO_new(BIO_s_file());
+ if (bp == NULL)
+ return;
+ BIO_set_fp(bp, fp, BIO_NOCLOSE);
+ OPENSSL_LH_node_stats_bio(lh, bp);
+ BIO_free(bp);
+}
+
+void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp)
+{
+ BIO *bp;
+
+ bp = BIO_new(BIO_s_file());
+ if (bp == NULL)
+ return;
+ BIO_set_fp(bp, fp, BIO_NOCLOSE);
+ OPENSSL_LH_node_usage_stats_bio(lh, bp);
+ BIO_free(bp);
+}
+
+# endif
+
+void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
+{
+ OPENSSL_LHASH *lh_mut = (OPENSSL_LHASH *) lh;
+ int ret;
+
+ BIO_printf(out, "num_items = %lu\n", lh->num_items);
+ BIO_printf(out, "num_nodes = %u\n", lh->num_nodes);
+ BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
+ BIO_printf(out, "num_expands = %lu\n", lh->num_expands);
+ BIO_printf(out, "num_expand_reallocs = %lu\n", lh->num_expand_reallocs);
+ BIO_printf(out, "num_contracts = %lu\n", lh->num_contracts);
+ BIO_printf(out, "num_contract_reallocs = %lu\n",
+ lh->num_contract_reallocs);
+ CRYPTO_atomic_add(&lh_mut->num_hash_calls, 0, &ret,
+ lh->retrieve_stats_lock);
+ BIO_printf(out, "num_hash_calls = %d\n", ret);
+ CRYPTO_atomic_add(&lh_mut->num_comp_calls, 0, &ret,
+ lh->retrieve_stats_lock);
+ BIO_printf(out, "num_comp_calls = %d\n", ret);
+ BIO_printf(out, "num_insert = %lu\n", lh->num_insert);
+ BIO_printf(out, "num_replace = %lu\n", lh->num_replace);
+ BIO_printf(out, "num_delete = %lu\n", lh->num_delete);
+ BIO_printf(out, "num_no_delete = %lu\n", lh->num_no_delete);
+ CRYPTO_atomic_add(&lh_mut->num_retrieve, 0, &ret, lh->retrieve_stats_lock);
+ BIO_printf(out, "num_retrieve = %d\n", ret);
+ CRYPTO_atomic_add(&lh_mut->num_retrieve_miss, 0, &ret,
+ lh->retrieve_stats_lock);
+ BIO_printf(out, "num_retrieve_miss = %d\n", ret);
+ CRYPTO_atomic_add(&lh_mut->num_hash_comps, 0, &ret,
+ lh->retrieve_stats_lock);
+ BIO_printf(out, "num_hash_comps = %d\n", ret);
+}
+
+void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
+{
+ OPENSSL_LH_NODE *n;
+ unsigned int i, num;
+
+ for (i = 0; i < lh->num_nodes; i++) {
+ for (n = lh->b[i], num = 0; n != NULL; n = n->next)
+ num++;
+ BIO_printf(out, "node %6u -> %3u\n", i, num);
+ }
+}
+
+void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
+{
+ OPENSSL_LH_NODE *n;
+ unsigned long num;
+ unsigned int i;
+ unsigned long total = 0, n_used = 0;
+
+ for (i = 0; i < lh->num_nodes; i++) {
+ for (n = lh->b[i], num = 0; n != NULL; n = n->next)
+ num++;
+ if (num != 0) {
+ n_used++;
+ total += num;
+ }
+ }
+ BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
+ BIO_printf(out, "%lu items\n", total);
+ if (n_used == 0)
+ return;
+ BIO_printf(out, "load %d.%02d actual load %d.%02d\n",
+ (int)(total / lh->num_nodes),
+ (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
+ (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
+}
diff --git a/openssl-1.1.0h/crypto/lhash/lhash.c b/openssl-1.1.0h/crypto/lhash/lhash.c
new file mode 100644
index 0000000..f485411
--- /dev/null
+++ b/openssl-1.1.0h/crypto/lhash/lhash.c
@@ -0,0 +1,372 @@
+/*
+ * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <openssl/crypto.h>
+#include <openssl/lhash.h>
+#include "lhash_lcl.h"
+
+/*
+ * A hashing implementation that appears to be based on the linear hashing
+ * algorithm:
+ * https://en.wikipedia.org/wiki/Linear_hashing
+ *
+ * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
+ * addressing", Proc. 6th Conference on Very Large Databases: 212-223
+ * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
+ *
+ * From the wikipedia article "Linear hashing is used in the BDB Berkeley
+ * database system, which in turn is used by many software systems such as
+ * OpenLDAP, using a C implementation derived from the CACM article and first
+ * published on the Usenet in 1988 by Esmond Pitt."
+ *
+ * The CACM paper is available here:
+ * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
+ */
+
+#undef MIN_NODES
+#define MIN_NODES 16
+#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
+#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
+
+static int expand(OPENSSL_LHASH *lh);
+static void contract(OPENSSL_LHASH *lh);
+static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
+
+OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
+{
+ OPENSSL_LHASH *ret;
+
+ if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
+ return NULL;
+ if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
+ goto err;
+ if ((ret->retrieve_stats_lock = CRYPTO_THREAD_lock_new()) == NULL)
+ goto err;
+ ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
+ ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
+ ret->num_nodes = MIN_NODES / 2;
+ ret->num_alloc_nodes = MIN_NODES;
+ ret->pmax = MIN_NODES / 2;
+ ret->up_load = UP_LOAD;
+ ret->down_load = DOWN_LOAD;
+ return (ret);
+
+err:
+ OPENSSL_free(ret->b);
+ OPENSSL_free(ret);
+ return NULL;
+}
+
+void OPENSSL_LH_free(OPENSSL_LHASH *lh)
+{
+ unsigned int i;
+ OPENSSL_LH_NODE *n, *nn;
+
+ if (lh == NULL)
+ return;
+
+ for (i = 0; i < lh->num_nodes; i++) {
+ n = lh->b[i];
+ while (n != NULL) {
+ nn = n->next;
+ OPENSSL_free(n);
+ n = nn;
+ }
+ }
+ CRYPTO_THREAD_lock_free(lh->retrieve_stats_lock);
+ OPENSSL_free(lh->b);
+ OPENSSL_free(lh);
+}
+
+void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
+{
+ unsigned long hash;
+ OPENSSL_LH_NODE *nn, **rn;
+ void *ret;
+
+ lh->error = 0;
+ if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
+ return NULL; /* 'lh->error++' already done in 'expand' */
+
+ rn = getrn(lh, data, &hash);
+
+ if (*rn == NULL) {
+ if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
+ lh->error++;
+ return (NULL);
+ }
+ nn->data = data;
+ nn->next = NULL;
+ nn->hash = hash;
+ *rn = nn;
+ ret = NULL;
+ lh->num_insert++;
+ lh->num_items++;
+ } else { /* replace same key */
+
+ ret = (*rn)->data;
+ (*rn)->data = data;
+ lh->num_replace++;
+ }
+ return (ret);
+}
+
+void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
+{
+ unsigned long hash;
+ OPENSSL_LH_NODE *nn, **rn;
+ void *ret;
+
+ lh->error = 0;
+ rn = getrn(lh, data, &hash);
+
+ if (*rn == NULL) {
+ lh->num_no_delete++;
+ return (NULL);
+ } else {
+ nn = *rn;
+ *rn = nn->next;
+ ret = nn->data;
+ OPENSSL_free(nn);
+ lh->num_delete++;
+ }
+
+ lh->num_items--;
+ if ((lh->num_nodes > MIN_NODES) &&
+ (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
+ contract(lh);
+
+ return (ret);
+}
+
+void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
+{
+ unsigned long hash;
+ OPENSSL_LH_NODE **rn;
+ void *ret;
+ int scratch;
+
+ lh->error = 0;
+ rn = getrn(lh, data, &hash);
+
+ if (*rn == NULL) {
+ CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);
+ return NULL;
+ } else {
+ ret = (*rn)->data;
+ CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
+ }
+ return ret;
+}
+
+static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
+ OPENSSL_LH_DOALL_FUNC func,
+ OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
+{
+ int i;
+ OPENSSL_LH_NODE *a, *n;
+
+ if (lh == NULL)
+ return;
+
+ /*
+ * reverse the order so we search from 'top to bottom' We were having
+ * memory leaks otherwise
+ */
+ for (i = lh->num_nodes - 1; i >= 0; i--) {
+ a = lh->b[i];
+ while (a != NULL) {
+ n = a->next;
+ if (use_arg)
+ func_arg(a->data, arg);
+ else
+ func(a->data);
+ a = n;
+ }
+ }
+}
+
+void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
+{
+ doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
+}
+
+void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
+{
+ doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
+}
+
+static int expand(OPENSSL_LHASH *lh)
+{
+ OPENSSL_LH_NODE **n, **n1, **n2, *np;
+ unsigned int p, pmax, nni, j;
+ unsigned long hash;
+
+ nni = lh->num_alloc_nodes;
+ p = lh->p;
+ pmax = lh->pmax;
+ if (p + 1 >= pmax) {
+ j = nni * 2;
+ n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
+ if (n == NULL) {
+ lh->error++;
+ return 0;
+ }
+ lh->b = n;
+ memset(n + nni, 0, sizeof(*n) * (j - nni));
+ lh->pmax = nni;
+ lh->num_alloc_nodes = j;
+ lh->num_expand_reallocs++;
+ lh->p = 0;
+ } else {
+ lh->p++;
+ }
+
+ lh->num_nodes++;
+ lh->num_expands++;
+ n1 = &(lh->b[p]);
+ n2 = &(lh->b[p + pmax]);
+ *n2 = NULL;
+
+ for (np = *n1; np != NULL;) {
+ hash = np->hash;
+ if ((hash % nni) != p) { /* move it */
+ *n1 = (*n1)->next;
+ np->next = *n2;
+ *n2 = np;
+ } else
+ n1 = &((*n1)->next);
+ np = *n1;
+ }
+
+ return 1;
+}
+
+static void contract(OPENSSL_LHASH *lh)
+{
+ OPENSSL_LH_NODE **n, *n1, *np;
+
+ np = lh->b[lh->p + lh->pmax - 1];
+ lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
+ if (lh->p == 0) {
+ n = OPENSSL_realloc(lh->b,
+ (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
+ if (n == NULL) {
+ /* fputs("realloc error in lhash",stderr); */
+ lh->error++;
+ return;
+ }
+ lh->num_contract_reallocs++;
+ lh->num_alloc_nodes /= 2;
+ lh->pmax /= 2;
+ lh->p = lh->pmax - 1;
+ lh->b = n;
+ } else
+ lh->p--;
+
+ lh->num_nodes--;
+ lh->num_contracts++;
+
+ n1 = lh->b[(int)lh->p];
+ if (n1 == NULL)
+ lh->b[(int)lh->p] = np;
+ else {
+ while (n1->next != NULL)
+ n1 = n1->next;
+ n1->next = np;
+ }
+}
+
+static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
+ const void *data, unsigned long *rhash)
+{
+ OPENSSL_LH_NODE **ret, *n1;
+ unsigned long hash, nn;
+ OPENSSL_LH_COMPFUNC cf;
+ int scratch;
+
+ hash = (*(lh->hash)) (data);
+ CRYPTO_atomic_add(&lh->num_hash_calls, 1, &scratch, lh->retrieve_stats_lock);
+ *rhash = hash;
+
+ nn = hash % lh->pmax;
+ if (nn < lh->p)
+ nn = hash % lh->num_alloc_nodes;
+
+ cf = lh->comp;
+ ret = &(lh->b[(int)nn]);
+ for (n1 = *ret; n1 != NULL; n1 = n1->next) {
+ CRYPTO_atomic_add(&lh->num_hash_comps, 1, &scratch, lh->retrieve_stats_lock);
+ if (n1->hash != hash) {
+ ret = &(n1->next);
+ continue;
+ }
+ CRYPTO_atomic_add(&lh->num_comp_calls, 1, &scratch, lh->retrieve_stats_lock);
+ if (cf(n1->data, data) == 0)
+ break;
+ ret = &(n1->next);
+ }
+ return (ret);
+}
+
+/*
+ * The following hash seems to work very well on normal text strings no
+ * collisions on /usr/dict/words and it distributes on %2^n quite well, not
+ * as good as MD5, but still good.
+ */
+unsigned long OPENSSL_LH_strhash(const char *c)
+{
+ unsigned long ret = 0;
+ long n;
+ unsigned long v;
+ int r;
+
+ if ((c == NULL) || (*c == '\0'))
+ return (ret);
+/*-
+ unsigned char b[16];
+ MD5(c,strlen(c),b);
+ return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
+*/
+
+ n = 0x100;
+ while (*c) {
+ v = n | (*c);
+ n += 0x100;
+ r = (int)((v >> 2) ^ v) & 0x0f;
+ ret = (ret << r) | (ret >> (32 - r));
+ ret &= 0xFFFFFFFFL;
+ ret ^= v * v;
+ c++;
+ }
+ return ((ret >> 16) ^ ret);
+}
+
+unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
+{
+ return lh ? lh->num_items : 0;
+}
+
+unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
+{
+ return lh->down_load;
+}
+
+void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
+{
+ lh->down_load = down_load;
+}
+
+int OPENSSL_LH_error(OPENSSL_LHASH *lh)
+{
+ return lh->error;
+}
diff --git a/openssl-1.1.0h/crypto/lhash/lhash_lcl.h b/openssl-1.1.0h/crypto/lhash/lhash_lcl.h
new file mode 100644
index 0000000..01d463f
--- /dev/null
+++ b/openssl-1.1.0h/crypto/lhash/lhash_lcl.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+#include <openssl/crypto.h>
+
+struct lhash_node_st {
+ void *data;
+ struct lhash_node_st *next;
+ unsigned long hash;
+};
+
+struct lhash_st {
+ OPENSSL_LH_NODE **b;
+ OPENSSL_LH_COMPFUNC comp;
+ OPENSSL_LH_HASHFUNC hash;
+ /*
+ * some stats are updated on lookup, which callers aren't expecting to have
+ * to take an exclusive lock around. This lock protects them on platforms
+ * without atomics, and their types are int rather than unsigned long below
+ * so they can be adjusted with CRYPTO_atomic_add.
+ */
+ CRYPTO_RWLOCK *retrieve_stats_lock;
+ unsigned int num_nodes;
+ unsigned int num_alloc_nodes;
+ unsigned int p;
+ unsigned int pmax;
+ unsigned long up_load; /* load times 256 */
+ unsigned long down_load; /* load times 256 */
+ unsigned long num_items;
+ unsigned long num_expands;
+ unsigned long num_expand_reallocs;
+ unsigned long num_contracts;
+ unsigned long num_contract_reallocs;
+ int num_hash_calls;
+ int num_comp_calls;
+ unsigned long num_insert;
+ unsigned long num_replace;
+ unsigned long num_delete;
+ unsigned long num_no_delete;
+ int num_retrieve;
+ int num_retrieve_miss;
+ int num_hash_comps;
+ int error;
+};
diff --git a/openssl-1.1.0h/crypto/lhash/num.pl b/openssl-1.1.0h/crypto/lhash/num.pl
new file mode 100644
index 0000000..8a8c42c
--- /dev/null
+++ b/openssl-1.1.0h/crypto/lhash/num.pl
@@ -0,0 +1,23 @@
+#! /usr/bin/env perl
+# Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the OpenSSL license (the "License"). You may not use
+# this file except in compliance with the License. You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+#node 10 -> 4
+
+while (<>)
+ {
+ next unless /^node/;
+ s|\R$||; # Better chomp
+ @a=split;
+ $num{$a[3]}++;
+ }
+
+@a=sort {$a <=> $b } keys %num;
+foreach (0 .. $a[$#a])
+ {
+ printf "%4d:%4d\n",$_,$num{$_};
+ }