summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/lhash
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/lhash')
-rw-r--r--src/lib/libcrypto/lhash/lh_stats.c254
-rw-r--r--src/lib/libcrypto/lhash/lhash.c463
-rw-r--r--src/lib/libcrypto/lhash/lhash.h235
3 files changed, 0 insertions, 952 deletions
diff --git a/src/lib/libcrypto/lhash/lh_stats.c b/src/lib/libcrypto/lhash/lh_stats.c
deleted file mode 100644
index e7dde47806..0000000000
--- a/src/lib/libcrypto/lhash/lh_stats.c
+++ /dev/null
@@ -1,254 +0,0 @@
1/* $OpenBSD: lh_stats.c,v 1.12 2014/07/11 08:44:48 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include <openssl/opensslconf.h>
64
65#ifndef OPENSSL_NO_BIO
66#include <openssl/bio.h>
67#endif
68#include <openssl/lhash.h>
69
70#ifdef OPENSSL_NO_BIO
71
72void
73lh_stats(LHASH *lh, FILE *out)
74{
75 fprintf(out, "num_items = %lu\n", lh->num_items);
76 fprintf(out, "num_nodes = %u\n", lh->num_nodes);
77 fprintf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
78 fprintf(out, "num_expands = %lu\n", lh->num_expands);
79 fprintf(out, "num_expand_reallocs = %lu\n", lh->num_expand_reallocs);
80 fprintf(out, "num_contracts = %lu\n", lh->num_contracts);
81 fprintf(out, "num_contract_reallocs = %lu\n",
82 lh->num_contract_reallocs);
83 fprintf(out, "num_hash_calls = %lu\n", lh->num_hash_calls);
84 fprintf(out, "num_comp_calls = %lu\n", lh->num_comp_calls);
85 fprintf(out, "num_insert = %lu\n", lh->num_insert);
86 fprintf(out, "num_replace = %lu\n", lh->num_replace);
87 fprintf(out, "num_delete = %lu\n", lh->num_delete);
88 fprintf(out, "num_no_delete = %lu\n", lh->num_no_delete);
89 fprintf(out, "num_retrieve = %lu\n", lh->num_retrieve);
90 fprintf(out, "num_retrieve_miss = %lu\n", lh->num_retrieve_miss);
91 fprintf(out, "num_hash_comps = %lu\n", lh->num_hash_comps);
92#if 0
93 fprintf(out, "p = %u\n", lh->p);
94 fprintf(out, "pmax = %u\n", lh->pmax);
95 fprintf(out, "up_load = %lu\n", lh->up_load);
96 fprintf(out, "down_load = %lu\n", lh->down_load);
97#endif
98}
99
100void
101lh_node_stats(LHASH *lh, FILE *out)
102{
103 LHASH_NODE *n;
104 unsigned int i, num;
105
106 for (i = 0; i < lh->num_nodes; i++) {
107 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
108 num++;
109 fprintf(out, "node %6u -> %3u\n", i, num);
110 }
111}
112
113void
114lh_node_usage_stats(LHASH *lh, FILE *out)
115{
116 LHASH_NODE *n;
117 unsigned long num;
118 unsigned int i;
119 unsigned long total = 0, n_used = 0;
120
121 for (i = 0; i < lh->num_nodes; i++) {
122 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
123 num++;
124 if (num != 0) {
125 n_used++;
126 total += num;
127 }
128 }
129 fprintf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
130 fprintf(out, "%lu items\n", total);
131 if (n_used == 0)
132 return;
133 fprintf(out, "load %d.%02d actual load %d.%02d\n",
134 (int)(total / lh->num_nodes),
135 (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
136 (int)(total / n_used),
137 (int)((total % n_used) * 100 / n_used));
138}
139
140#else
141
142void
143lh_stats(const _LHASH *lh, FILE *fp)
144{
145 BIO *bp;
146
147 bp = BIO_new(BIO_s_file());
148 if (bp == NULL)
149 goto end;
150 BIO_set_fp(bp, fp, BIO_NOCLOSE);
151 lh_stats_bio(lh, bp);
152 BIO_free(bp);
153end:;
154}
155
156void
157lh_node_stats(const _LHASH *lh, FILE *fp)
158{
159 BIO *bp;
160
161 bp = BIO_new(BIO_s_file());
162 if (bp == NULL)
163 goto end;
164 BIO_set_fp(bp, fp, BIO_NOCLOSE);
165 lh_node_stats_bio(lh, bp);
166 BIO_free(bp);
167end:;
168}
169
170void
171lh_node_usage_stats(const _LHASH *lh, FILE *fp)
172{
173 BIO *bp;
174
175 bp = BIO_new(BIO_s_file());
176 if (bp == NULL)
177 goto end;
178 BIO_set_fp(bp, fp, BIO_NOCLOSE);
179 lh_node_usage_stats_bio(lh, bp);
180 BIO_free(bp);
181end:;
182}
183
184
185void
186lh_stats_bio(const _LHASH *lh, BIO *out)
187{
188 BIO_printf(out, "num_items = %lu\n", lh->num_items);
189 BIO_printf(out, "num_nodes = %u\n", lh->num_nodes);
190 BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
191 BIO_printf(out, "num_expands = %lu\n", lh->num_expands);
192 BIO_printf(out, "num_expand_reallocs = %lu\n",
193 lh->num_expand_reallocs);
194 BIO_printf(out, "num_contracts = %lu\n", lh->num_contracts);
195 BIO_printf(out, "num_contract_reallocs = %lu\n",
196 lh->num_contract_reallocs);
197 BIO_printf(out, "num_hash_calls = %lu\n", lh->num_hash_calls);
198 BIO_printf(out, "num_comp_calls = %lu\n", lh->num_comp_calls);
199 BIO_printf(out, "num_insert = %lu\n", lh->num_insert);
200 BIO_printf(out, "num_replace = %lu\n", lh->num_replace);
201 BIO_printf(out, "num_delete = %lu\n", lh->num_delete);
202 BIO_printf(out, "num_no_delete = %lu\n", lh->num_no_delete);
203 BIO_printf(out, "num_retrieve = %lu\n", lh->num_retrieve);
204 BIO_printf(out, "num_retrieve_miss = %lu\n", lh->num_retrieve_miss);
205 BIO_printf(out, "num_hash_comps = %lu\n", lh->num_hash_comps);
206#if 0
207 BIO_printf(out, "p = %u\n", lh->p);
208 BIO_printf(out, "pmax = %u\n", lh->pmax);
209 BIO_printf(out, "up_load = %lu\n", lh->up_load);
210 BIO_printf(out, "down_load = %lu\n", lh->down_load);
211#endif
212}
213
214void
215lh_node_stats_bio(const _LHASH *lh, BIO *out)
216{
217 LHASH_NODE *n;
218 unsigned int i, num;
219
220 for (i = 0; i < lh->num_nodes; i++) {
221 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
222 num++;
223 BIO_printf(out, "node %6u -> %3u\n", i, num);
224 }
225}
226
227void
228lh_node_usage_stats_bio(const _LHASH *lh, BIO *out)
229{
230 LHASH_NODE *n;
231 unsigned long num;
232 unsigned int i;
233 unsigned long total = 0, n_used = 0;
234
235 for (i = 0; i < lh->num_nodes; i++) {
236 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
237 num++;
238 if (num != 0) {
239 n_used++;
240 total += num;
241 }
242 }
243 BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
244 BIO_printf(out, "%lu items\n", total);
245 if (n_used == 0)
246 return;
247 BIO_printf(out, "load %d.%02d actual load %d.%02d\n",
248 (int)(total / lh->num_nodes),
249 (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
250 (int)(total / n_used),
251 (int)((total % n_used) * 100 / n_used));
252}
253
254#endif
diff --git a/src/lib/libcrypto/lhash/lhash.c b/src/lib/libcrypto/lhash/lhash.c
deleted file mode 100644
index f4994f7471..0000000000
--- a/src/lib/libcrypto/lhash/lhash.c
+++ /dev/null
@@ -1,463 +0,0 @@
1/* $OpenBSD: lhash.c,v 1.17 2014/07/10 22:45:57 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Code for dynamic hash table routines
60 * Author - Eric Young v 2.0
61 *
62 * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
63 * present. eay 18-Jun-98
64 *
65 * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
66 *
67 * 2.0 eay - Fixed a bug that occurred when using lh_delete
68 * from inside lh_doall(). As entries were deleted,
69 * the 'table' was 'contract()ed', making some entries
70 * jump from the end of the table to the start, there by
71 * skipping the lh_doall() processing. eay - 4/12/95
72 *
73 * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
74 * were not being free()ed. 21/11/95
75 *
76 * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
77 * 19/09/95
78 *
79 * 1.7 eay - Removed the fputs() for realloc failures - the code
80 * should silently tolerate them. I have also fixed things
81 * lint complained about 04/05/95
82 *
83 * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
84 *
85 * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
86 *
87 * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
88 *
89 * 1.3 eay - Fixed a few lint problems 19/3/1991
90 *
91 * 1.2 eay - Fixed lh_doall problem 13/3/1991
92 *
93 * 1.1 eay - Added lh_doall
94 *
95 * 1.0 eay - First version
96 */
97#include <stdio.h>
98#include <string.h>
99#include <stdlib.h>
100
101#include <openssl/opensslconf.h>
102
103#include <openssl/crypto.h>
104#include <openssl/lhash.h>
105
106#undef MIN_NODES
107#define MIN_NODES 16
108#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
109#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
110
111static void expand(_LHASH *lh);
112static void contract(_LHASH *lh);
113static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
114
115_LHASH *
116lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
117{
118 _LHASH *ret;
119 int i;
120
121 if ((ret = malloc(sizeof(_LHASH))) == NULL)
122 goto err0;
123 if ((ret->b = reallocarray(NULL, MIN_NODES, sizeof(LHASH_NODE *))) == NULL)
124 goto err1;
125 for (i = 0; i < MIN_NODES; i++)
126 ret->b[i] = NULL;
127 ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
128 ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
129 ret->num_nodes = MIN_NODES / 2;
130 ret->num_alloc_nodes = MIN_NODES;
131 ret->p = 0;
132 ret->pmax = MIN_NODES / 2;
133 ret->up_load = UP_LOAD;
134 ret->down_load = DOWN_LOAD;
135 ret->num_items = 0;
136
137 ret->num_expands = 0;
138 ret->num_expand_reallocs = 0;
139 ret->num_contracts = 0;
140 ret->num_contract_reallocs = 0;
141 ret->num_hash_calls = 0;
142 ret->num_comp_calls = 0;
143 ret->num_insert = 0;
144 ret->num_replace = 0;
145 ret->num_delete = 0;
146 ret->num_no_delete = 0;
147 ret->num_retrieve = 0;
148 ret->num_retrieve_miss = 0;
149 ret->num_hash_comps = 0;
150
151 ret->error = 0;
152 return (ret);
153
154err1:
155 free(ret);
156err0:
157 return (NULL);
158}
159
160void
161lh_free(_LHASH *lh)
162{
163 unsigned int i;
164 LHASH_NODE *n, *nn;
165
166 if (lh == NULL)
167 return;
168
169 for (i = 0; i < lh->num_nodes; i++) {
170 n = lh->b[i];
171 while (n != NULL) {
172 nn = n->next;
173 free(n);
174 n = nn;
175 }
176 }
177 free(lh->b);
178 free(lh);
179}
180
181void *
182lh_insert(_LHASH *lh, void *data)
183{
184 unsigned long hash;
185 LHASH_NODE *nn, **rn;
186 void *ret;
187
188 lh->error = 0;
189 if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))
190 expand(lh);
191
192 rn = getrn(lh, data, &hash);
193
194 if (*rn == NULL) {
195 if ((nn = malloc(sizeof(LHASH_NODE))) == NULL) {
196 lh->error++;
197 return (NULL);
198 }
199 nn->data = data;
200 nn->next = NULL;
201#ifndef OPENSSL_NO_HASH_COMP
202 nn->hash = hash;
203#endif
204 *rn = nn;
205 ret = NULL;
206 lh->num_insert++;
207 lh->num_items++;
208 }
209 else /* replace same key */
210 {
211 ret = (*rn)->data;
212 (*rn)->data = data;
213 lh->num_replace++;
214 }
215 return (ret);
216}
217
218void *
219lh_delete(_LHASH *lh, const void *data)
220{
221 unsigned long hash;
222 LHASH_NODE *nn, **rn;
223 void *ret;
224
225 lh->error = 0;
226 rn = getrn(lh, data, &hash);
227
228 if (*rn == NULL) {
229 lh->num_no_delete++;
230 return (NULL);
231 } else {
232 nn= *rn;
233 *rn = nn->next;
234 ret = nn->data;
235 free(nn);
236 lh->num_delete++;
237 }
238
239 lh->num_items--;
240 if ((lh->num_nodes > MIN_NODES) &&
241 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
242 contract(lh);
243
244 return (ret);
245}
246
247void *
248lh_retrieve(_LHASH *lh, const void *data)
249{
250 unsigned long hash;
251 LHASH_NODE **rn;
252 void *ret;
253
254 lh->error = 0;
255 rn = getrn(lh, data, &hash);
256
257 if (*rn == NULL) {
258 lh->num_retrieve_miss++;
259 return (NULL);
260 } else {
261 ret = (*rn)->data;
262 lh->num_retrieve++;
263 }
264 return (ret);
265}
266
267static void
268doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
269 LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
270{
271 int i;
272 LHASH_NODE *a, *n;
273
274 if (lh == NULL)
275 return;
276
277 /* reverse the order so we search from 'top to bottom'
278 * We were having memory leaks otherwise */
279 for (i = lh->num_nodes - 1; i >= 0; i--) {
280 a = lh->b[i];
281 while (a != NULL) {
282 /* 28/05/91 - eay - n added so items can be deleted
283 * via lh_doall */
284 /* 22/05/08 - ben - eh? since a is not passed,
285 * this should not be needed */
286 n = a->next;
287 if (use_arg)
288 func_arg(a->data, arg);
289 else
290 func(a->data);
291 a = n;
292 }
293 }
294}
295
296void
297lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
298{
299 doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
300}
301
302void
303lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
304{
305 doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
306}
307
308static void
309expand(_LHASH *lh)
310{
311 LHASH_NODE **n, **n1, **n2, *np;
312 unsigned int p, i, j;
313 unsigned long hash, nni;
314
315 lh->num_nodes++;
316 lh->num_expands++;
317 p = (int)lh->p++;
318 n1 = &(lh->b[p]);
319 n2 = &(lh->b[p + (int)lh->pmax]);
320 *n2 = NULL; /* 27/07/92 - eay - undefined pointer bug */
321 nni = lh->num_alloc_nodes;
322
323 for (np = *n1; np != NULL; ) {
324#ifndef OPENSSL_NO_HASH_COMP
325 hash = np->hash;
326#else
327 hash = lh->hash(np->data);
328 lh->num_hash_calls++;
329#endif
330 if ((hash % nni) != p) { /* move it */
331 *n1 = (*n1)->next;
332 np->next= *n2;
333 *n2 = np;
334 } else
335 n1 = &((*n1)->next);
336 np= *n1;
337 }
338
339 if ((lh->p) >= lh->pmax) {
340 j = (int)lh->num_alloc_nodes * 2;
341 n = reallocarray(lh->b, j, sizeof(LHASH_NODE *));
342 if (n == NULL) {
343/* fputs("realloc error in lhash", stderr); */
344 lh->error++;
345 lh->p = 0;
346 return;
347 }
348 /* else */
349 for (i = (int)lh->num_alloc_nodes; i < j; i++)/* 26/02/92 eay */
350 n[i] = NULL; /* 02/03/92 eay */
351 lh->pmax = lh->num_alloc_nodes;
352 lh->num_alloc_nodes = j;
353 lh->num_expand_reallocs++;
354 lh->p = 0;
355 lh->b = n;
356 }
357}
358
359static void
360contract(_LHASH *lh)
361{
362 LHASH_NODE **n, *n1, *np;
363
364 np = lh->b[lh->p + lh->pmax - 1];
365 lh->b[lh->p+lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
366 if (lh->p == 0) {
367 n = reallocarray(lh->b, lh->pmax, sizeof(LHASH_NODE *));
368 if (n == NULL) {
369/* fputs("realloc error in lhash", stderr); */
370 lh->error++;
371 return;
372 }
373 lh->num_contract_reallocs++;
374 lh->num_alloc_nodes /= 2;
375 lh->pmax /= 2;
376 lh->p = lh->pmax - 1;
377 lh->b = n;
378 } else
379 lh->p--;
380
381 lh->num_nodes--;
382 lh->num_contracts++;
383
384 n1 = lh->b[(int)lh->p];
385 if (n1 == NULL)
386 lh->b[(int)lh->p] = np;
387 else {
388 while (n1->next != NULL)
389 n1 = n1->next;
390 n1->next = np;
391 }
392}
393
394static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
395{
396 LHASH_NODE **ret, *n1;
397 unsigned long hash, nn;
398 LHASH_COMP_FN_TYPE cf;
399
400 hash = (*(lh->hash))(data);
401 lh->num_hash_calls++;
402 *rhash = hash;
403
404 nn = hash % lh->pmax;
405 if (nn < lh->p)
406 nn = hash % lh->num_alloc_nodes;
407
408 cf = lh->comp;
409 ret = &(lh->b[(int)nn]);
410 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
411#ifndef OPENSSL_NO_HASH_COMP
412 lh->num_hash_comps++;
413 if (n1->hash != hash) {
414 ret = &(n1->next);
415 continue;
416 }
417#endif
418 lh->num_comp_calls++;
419 if (cf(n1->data, data) == 0)
420 break;
421 ret = &(n1->next);
422 }
423 return (ret);
424}
425
426/* The following hash seems to work very well on normal text strings
427 * no collisions on /usr/dict/words and it distributes on %2^n quite
428 * well, not as good as MD5, but still good.
429 */
430unsigned long
431lh_strhash(const char *c)
432{
433 unsigned long ret = 0;
434 long n;
435 unsigned long v;
436 int r;
437
438 if ((c == NULL) || (*c == '\0'))
439 return (ret);
440/*
441 unsigned char b[16];
442 MD5(c,strlen(c),b);
443 return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
444*/
445
446 n = 0x100;
447 while (*c) {
448 v = n | (*c);
449 n += 0x100;
450 r = (int)((v >> 2) ^ v) & 0x0f;
451 ret = (ret << r)|(ret >> (32 - r));
452 ret &= 0xFFFFFFFFL;
453 ret ^= v * v;
454 c++;
455 }
456 return ((ret >> 16) ^ ret);
457}
458
459unsigned long
460lh_num_items(const _LHASH *lh)
461{
462 return lh ? lh->num_items : 0;
463}
diff --git a/src/lib/libcrypto/lhash/lhash.h b/src/lib/libcrypto/lhash/lhash.h
deleted file mode 100644
index 9c63657396..0000000000
--- a/src/lib/libcrypto/lhash/lhash.h
+++ /dev/null
@@ -1,235 +0,0 @@
1/* $OpenBSD: lhash.h,v 1.12 2014/06/12 15:49:29 deraadt Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Header for dynamic hash table routines
60 * Author - Eric Young
61 */
62
63#ifndef HEADER_LHASH_H
64#define HEADER_LHASH_H
65
66#include <openssl/opensslconf.h>
67
68#include <stdio.h>
69
70#ifndef OPENSSL_NO_BIO
71#include <openssl/bio.h>
72#endif
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78typedef struct lhash_node_st {
79 void *data;
80 struct lhash_node_st *next;
81#ifndef OPENSSL_NO_HASH_COMP
82 unsigned long hash;
83#endif
84} LHASH_NODE;
85
86typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
87typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
88typedef void (*LHASH_DOALL_FN_TYPE)(void *);
89typedef void (*LHASH_DOALL_ARG_FN_TYPE)(void *, void *);
90
91/* Macros for declaring and implementing type-safe wrappers for LHASH callbacks.
92 * This way, callbacks can be provided to LHASH structures without function
93 * pointer casting and the macro-defined callbacks provide per-variable casting
94 * before deferring to the underlying type-specific callbacks. NB: It is
95 * possible to place a "static" in front of both the DECLARE and IMPLEMENT
96 * macros if the functions are strictly internal. */
97
98/* First: "hash" functions */
99#define DECLARE_LHASH_HASH_FN(name, o_type) \
100 unsigned long name##_LHASH_HASH(const void *);
101#define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
102 unsigned long name##_LHASH_HASH(const void *arg) { \
103 const o_type *a = arg; \
104 return name##_hash(a); }
105#define LHASH_HASH_FN(name) name##_LHASH_HASH
106
107/* Second: "compare" functions */
108#define DECLARE_LHASH_COMP_FN(name, o_type) \
109 int name##_LHASH_COMP(const void *, const void *);
110#define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
111 int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
112 const o_type *a = arg1; \
113 const o_type *b = arg2; \
114 return name##_cmp(a,b); }
115#define LHASH_COMP_FN(name) name##_LHASH_COMP
116
117/* Third: "doall" functions */
118#define DECLARE_LHASH_DOALL_FN(name, o_type) \
119 void name##_LHASH_DOALL(void *);
120#define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \
121 void name##_LHASH_DOALL(void *arg) { \
122 o_type *a = arg; \
123 name##_doall(a); }
124#define LHASH_DOALL_FN(name) name##_LHASH_DOALL
125
126/* Fourth: "doall_arg" functions */
127#define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
128 void name##_LHASH_DOALL_ARG(void *, void *);
129#define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
130 void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
131 o_type *a = arg1; \
132 a_type *b = arg2; \
133 name##_doall_arg(a, b); }
134#define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
135
136typedef struct lhash_st {
137 LHASH_NODE **b;
138 LHASH_COMP_FN_TYPE comp;
139 LHASH_HASH_FN_TYPE hash;
140 unsigned int num_nodes;
141 unsigned int num_alloc_nodes;
142 unsigned int p;
143 unsigned int pmax;
144 unsigned long up_load; /* load times 256 */
145 unsigned long down_load; /* load times 256 */
146 unsigned long num_items;
147
148 unsigned long num_expands;
149 unsigned long num_expand_reallocs;
150 unsigned long num_contracts;
151 unsigned long num_contract_reallocs;
152 unsigned long num_hash_calls;
153 unsigned long num_comp_calls;
154 unsigned long num_insert;
155 unsigned long num_replace;
156 unsigned long num_delete;
157 unsigned long num_no_delete;
158 unsigned long num_retrieve;
159 unsigned long num_retrieve_miss;
160 unsigned long num_hash_comps;
161
162 int error;
163} _LHASH; /* Do not use _LHASH directly, use LHASH_OF
164 * and friends */
165
166#define LH_LOAD_MULT 256
167
168/* Indicates a malloc() error in the last call, this is only bad
169 * in lh_insert(). */
170#define lh_error(lh) ((lh)->error)
171
172_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c);
173void lh_free(_LHASH *lh);
174void *lh_insert(_LHASH *lh, void *data);
175void *lh_delete(_LHASH *lh, const void *data);
176void *lh_retrieve(_LHASH *lh, const void *data);
177void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func);
178void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg);
179unsigned long lh_strhash(const char *c);
180unsigned long lh_num_items(const _LHASH *lh);
181
182void lh_stats(const _LHASH *lh, FILE *out);
183void lh_node_stats(const _LHASH *lh, FILE *out);
184void lh_node_usage_stats(const _LHASH *lh, FILE *out);
185
186#ifndef OPENSSL_NO_BIO
187void lh_stats_bio(const _LHASH *lh, BIO *out);
188void lh_node_stats_bio(const _LHASH *lh, BIO *out);
189void lh_node_usage_stats_bio(const _LHASH *lh, BIO *out);
190#endif
191
192/* Type checking... */
193
194#define LHASH_OF(type) struct lhash_st_##type
195
196#define DECLARE_LHASH_OF(type) LHASH_OF(type) { int dummy; }
197
198#define CHECKED_LHASH_OF(type,lh) \
199 ((_LHASH *)CHECKED_PTR_OF(LHASH_OF(type),lh))
200
201/* Define wrapper functions. */
202#define LHM_lh_new(type, name) \
203 ((LHASH_OF(type) *)lh_new(LHASH_HASH_FN(name), LHASH_COMP_FN(name)))
204#define LHM_lh_error(type, lh) \
205 lh_error(CHECKED_LHASH_OF(type,lh))
206#define LHM_lh_insert(type, lh, inst) \
207 ((type *)lh_insert(CHECKED_LHASH_OF(type, lh), \
208 CHECKED_PTR_OF(type, inst)))
209#define LHM_lh_retrieve(type, lh, inst) \
210 ((type *)lh_retrieve(CHECKED_LHASH_OF(type, lh), \
211 CHECKED_PTR_OF(type, inst)))
212#define LHM_lh_delete(type, lh, inst) \
213 ((type *)lh_delete(CHECKED_LHASH_OF(type, lh), \
214 CHECKED_PTR_OF(type, inst)))
215#define LHM_lh_doall(type, lh,fn) lh_doall(CHECKED_LHASH_OF(type, lh), fn)
216#define LHM_lh_doall_arg(type, lh, fn, arg_type, arg) \
217 lh_doall_arg(CHECKED_LHASH_OF(type, lh), fn, CHECKED_PTR_OF(arg_type, arg))
218#define LHM_lh_num_items(type, lh) lh_num_items(CHECKED_LHASH_OF(type, lh))
219#define LHM_lh_down_load(type, lh) (CHECKED_LHASH_OF(type, lh)->down_load)
220#define LHM_lh_node_stats_bio(type, lh, out) \
221 lh_node_stats_bio(CHECKED_LHASH_OF(type, lh), out)
222#define LHM_lh_node_usage_stats_bio(type, lh, out) \
223 lh_node_usage_stats_bio(CHECKED_LHASH_OF(type, lh), out)
224#define LHM_lh_stats_bio(type, lh, out) \
225 lh_stats_bio(CHECKED_LHASH_OF(type, lh), out)
226#define LHM_lh_free(type, lh) lh_free(CHECKED_LHASH_OF(type, lh))
227
228DECLARE_LHASH_OF(OPENSSL_STRING);
229DECLARE_LHASH_OF(OPENSSL_CSTRING);
230
231#ifdef __cplusplus
232}
233#endif
234
235#endif