]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/lhash/lhash.c
Fix #2400 Add NO_RENEGOTIATE option
[thirdparty/openssl.git] / crypto / lhash / lhash.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
RS
4 * Licensed under the OpenSSL license (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
d02b48c6
RE
8 */
9
d02b48c6
RE
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
ec577822
BM
13#include <openssl/crypto.h>
14#include <openssl/lhash.h>
739a1eb1
RS
15#include "lhash_lcl.h"
16
d02b48c6 17
0f113f3e
MC
18#undef MIN_NODES
19#define MIN_NODES 16
20#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
21#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
d02b48c6 22
0a1d3a81 23static int expand(OPENSSL_LHASH *lh);
739a1eb1
RS
24static void contract(OPENSSL_LHASH *lh);
25static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
d02b48c6 26
739a1eb1 27OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
0f113f3e 28{
739a1eb1 29 OPENSSL_LHASH *ret;
0f113f3e 30
64b25758 31 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
0f113f3e 32 goto err0;
64b25758 33 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
0f113f3e 34 goto err1;
739a1eb1
RS
35 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
36 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
0f113f3e
MC
37 ret->num_nodes = MIN_NODES / 2;
38 ret->num_alloc_nodes = MIN_NODES;
0f113f3e
MC
39 ret->pmax = MIN_NODES / 2;
40 ret->up_load = UP_LOAD;
41 ret->down_load = DOWN_LOAD;
0f113f3e 42 return (ret);
64b25758 43
0f113f3e
MC
44 err1:
45 OPENSSL_free(ret);
46 err0:
47 return (NULL);
48}
d02b48c6 49
739a1eb1 50void OPENSSL_LH_free(OPENSSL_LHASH *lh)
0f113f3e
MC
51{
52 unsigned int i;
739a1eb1 53 OPENSSL_LH_NODE *n, *nn;
0f113f3e
MC
54
55 if (lh == NULL)
56 return;
57
58 for (i = 0; i < lh->num_nodes; i++) {
59 n = lh->b[i];
60 while (n != NULL) {
61 nn = n->next;
62 OPENSSL_free(n);
63 n = nn;
64 }
65 }
66 OPENSSL_free(lh->b);
67 OPENSSL_free(lh);
68}
d02b48c6 69
739a1eb1 70void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
0f113f3e
MC
71{
72 unsigned long hash;
739a1eb1 73 OPENSSL_LH_NODE *nn, **rn;
0f113f3e 74 void *ret;
152d2646 75
0f113f3e 76 lh->error = 0;
152d2646 77 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
78 return NULL; /* 'lh->error++' already done in 'expand' */
79
0f113f3e
MC
80 rn = getrn(lh, data, &hash);
81
82 if (*rn == NULL) {
b4faea50 83 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
0f113f3e
MC
84 lh->error++;
85 return (NULL);
86 }
87 nn->data = data;
88 nn->next = NULL;
0f113f3e 89 nn->hash = hash;
0f113f3e
MC
90 *rn = nn;
91 ret = NULL;
92 lh->num_insert++;
93 lh->num_items++;
94 } else { /* replace same key */
95
96 ret = (*rn)->data;
97 (*rn)->data = data;
98 lh->num_replace++;
99 }
100 return (ret);
101}
d02b48c6 102
739a1eb1 103void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
0f113f3e
MC
104{
105 unsigned long hash;
739a1eb1 106 OPENSSL_LH_NODE *nn, **rn;
0f113f3e
MC
107 void *ret;
108
109 lh->error = 0;
110 rn = getrn(lh, data, &hash);
111
112 if (*rn == NULL) {
113 lh->num_no_delete++;
114 return (NULL);
115 } else {
116 nn = *rn;
117 *rn = nn->next;
118 ret = nn->data;
119 OPENSSL_free(nn);
120 lh->num_delete++;
121 }
122
123 lh->num_items--;
124 if ((lh->num_nodes > MIN_NODES) &&
125 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
126 contract(lh);
127
128 return (ret);
129}
d02b48c6 130
739a1eb1 131void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
0f113f3e
MC
132{
133 unsigned long hash;
739a1eb1 134 OPENSSL_LH_NODE **rn;
0f113f3e
MC
135 void *ret;
136
137 lh->error = 0;
138 rn = getrn(lh, data, &hash);
139
140 if (*rn == NULL) {
141 lh->num_retrieve_miss++;
142 return (NULL);
143 } else {
144 ret = (*rn)->data;
145 lh->num_retrieve++;
146 }
147 return (ret);
148}
d02b48c6 149
739a1eb1
RS
150static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
151 OPENSSL_LH_DOALL_FUNC func,
152 OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
0f113f3e
MC
153{
154 int i;
739a1eb1 155 OPENSSL_LH_NODE *a, *n;
0f113f3e
MC
156
157 if (lh == NULL)
158 return;
159
160 /*
161 * reverse the order so we search from 'top to bottom' We were having
162 * memory leaks otherwise
163 */
164 for (i = lh->num_nodes - 1; i >= 0; i--) {
165 a = lh->b[i];
166 while (a != NULL) {
0f113f3e
MC
167 n = a->next;
168 if (use_arg)
169 func_arg(a->data, arg);
170 else
171 func(a->data);
172 a = n;
173 }
174 }
175}
d02b48c6 176
739a1eb1 177void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
0f113f3e 178{
739a1eb1 179 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
0f113f3e 180}
18602745 181
739a1eb1 182void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
0f113f3e 183{
739a1eb1 184 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
0f113f3e 185}
18602745 186
0a1d3a81 187static int expand(OPENSSL_LHASH *lh)
0f113f3e 188{
739a1eb1 189 OPENSSL_LH_NODE **n, **n1, **n2, *np;
0f113f3e
MC
190 unsigned int p, i, j;
191 unsigned long hash, nni;
192
193 lh->num_nodes++;
194 lh->num_expands++;
195 p = (int)lh->p++;
196 n1 = &(lh->b[p]);
197 n2 = &(lh->b[p + (int)lh->pmax]);
739a1eb1 198 *n2 = NULL;
0f113f3e
MC
199 nni = lh->num_alloc_nodes;
200
201 for (np = *n1; np != NULL;) {
0f113f3e 202 hash = np->hash;
0f113f3e
MC
203 if ((hash % nni) != p) { /* move it */
204 *n1 = (*n1)->next;
205 np->next = *n2;
206 *n2 = np;
207 } else
208 n1 = &((*n1)->next);
209 np = *n1;
210 }
211
212 if ((lh->p) >= lh->pmax) {
213 j = (int)lh->num_alloc_nodes * 2;
739a1eb1 214 n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));
0f113f3e 215 if (n == NULL) {
0f113f3e 216 lh->error++;
6fcace45 217 lh->num_nodes--;
0f113f3e 218 lh->p = 0;
152d2646 219 return 0;
0f113f3e 220 }
0f113f3e
MC
221 for (i = (int)lh->num_alloc_nodes; i < j; i++) /* 26/02/92 eay */
222 n[i] = NULL; /* 02/03/92 eay */
223 lh->pmax = lh->num_alloc_nodes;
224 lh->num_alloc_nodes = j;
225 lh->num_expand_reallocs++;
226 lh->p = 0;
227 lh->b = n;
228 }
152d2646 229 return 1;
0f113f3e 230}
d02b48c6 231
739a1eb1 232static void contract(OPENSSL_LHASH *lh)
0f113f3e 233{
739a1eb1 234 OPENSSL_LH_NODE **n, *n1, *np;
0f113f3e
MC
235
236 np = lh->b[lh->p + lh->pmax - 1];
237 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
238 if (lh->p == 0) {
b196e7d9 239 n = OPENSSL_realloc(lh->b,
739a1eb1 240 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
0f113f3e 241 if (n == NULL) {
b196e7d9 242 /* fputs("realloc error in lhash",stderr); */
0f113f3e
MC
243 lh->error++;
244 return;
245 }
246 lh->num_contract_reallocs++;
247 lh->num_alloc_nodes /= 2;
248 lh->pmax /= 2;
249 lh->p = lh->pmax - 1;
250 lh->b = n;
251 } else
252 lh->p--;
253
254 lh->num_nodes--;
255 lh->num_contracts++;
256
257 n1 = lh->b[(int)lh->p];
258 if (n1 == NULL)
259 lh->b[(int)lh->p] = np;
260 else {
261 while (n1->next != NULL)
262 n1 = n1->next;
263 n1->next = np;
264 }
265}
d02b48c6 266
739a1eb1
RS
267static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
268 const void *data, unsigned long *rhash)
0f113f3e 269{
739a1eb1 270 OPENSSL_LH_NODE **ret, *n1;
0f113f3e 271 unsigned long hash, nn;
739a1eb1 272 OPENSSL_LH_COMPFUNC cf;
0f113f3e
MC
273
274 hash = (*(lh->hash)) (data);
275 lh->num_hash_calls++;
276 *rhash = hash;
277
278 nn = hash % lh->pmax;
279 if (nn < lh->p)
280 nn = hash % lh->num_alloc_nodes;
281
282 cf = lh->comp;
283 ret = &(lh->b[(int)nn]);
284 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
0f113f3e
MC
285 lh->num_hash_comps++;
286 if (n1->hash != hash) {
287 ret = &(n1->next);
288 continue;
289 }
0f113f3e
MC
290 lh->num_comp_calls++;
291 if (cf(n1->data, data) == 0)
292 break;
293 ret = &(n1->next);
294 }
295 return (ret);
296}
297
298/*
299 * The following hash seems to work very well on normal text strings no
300 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
301 * as good as MD5, but still good.
d02b48c6 302 */
739a1eb1 303unsigned long OPENSSL_LH_strhash(const char *c)
0f113f3e
MC
304{
305 unsigned long ret = 0;
306 long n;
307 unsigned long v;
308 int r;
309
310 if ((c == NULL) || (*c == '\0'))
311 return (ret);
d02b48c6 312
0f113f3e
MC
313 n = 0x100;
314 while (*c) {
315 v = n | (*c);
316 n += 0x100;
317 r = (int)((v >> 2) ^ v) & 0x0f;
318 ret = (ret << r) | (ret >> (32 - r));
319 ret &= 0xFFFFFFFFL;
320 ret ^= v * v;
321 c++;
322 }
323 return ((ret >> 16) ^ ret);
324}
d02b48c6 325
739a1eb1 326unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
0f113f3e
MC
327{
328 return lh ? lh->num_items : 0;
329}
e6b5c341 330
739a1eb1 331unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
e6b5c341
DSH
332{
333 return lh->down_load;
334}
335
739a1eb1 336void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
e6b5c341
DSH
337{
338 lh->down_load = down_load;
339}
340
739a1eb1 341int OPENSSL_LH_error(OPENSSL_LHASH *lh)
e6b5c341
DSH
342{
343 return lh->error;
344}