559085a7b84bb96738851da6078dc31410eaef1a
[openssl.git] / crypto / stack / stack.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <openssl/stack.h>
14 #include <openssl/objects.h>
15 #include <errno.h>
16 #include <openssl/e_os2.h>      /* For ossl_inline */
17
18 /*
19  * The initial number of nodes in the array.
20  */
21 static const int min_nodes = 4;
22 static const int max_nodes = SIZE_MAX / sizeof(void *) < INT_MAX
23                              ? (int)(SIZE_MAX / sizeof(void *))
24                              : INT_MAX;
25
26 struct stack_st {
27     int num;
28     const void **data;
29     int sorted;
30     int num_alloc;
31     OPENSSL_sk_compfunc comp;
32 };
33
34 OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, OPENSSL_sk_compfunc c)
35 {
36     OPENSSL_sk_compfunc old = sk->comp;
37
38     if (sk->comp != c)
39         sk->sorted = 0;
40     sk->comp = c;
41
42     return old;
43 }
44
45 OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
46 {
47     OPENSSL_STACK *ret;
48
49     if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
50         return NULL;
51
52     /* direct structure assignment */
53     *ret = *sk;
54
55     if (sk->num == 0) {
56         /* postpone |ret->data| allocation */
57         ret->data = NULL;
58         ret->num_alloc = 0;
59         return ret;
60     }
61     /* duplicate |sk->data| content */
62     if ((ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc)) == NULL)
63         goto err;
64     memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
65     return ret;
66  err:
67     OPENSSL_sk_free(ret);
68     return NULL;
69 }
70
71 OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk,
72                              OPENSSL_sk_copyfunc copy_func,
73                              OPENSSL_sk_freefunc free_func)
74 {
75     OPENSSL_STACK *ret;
76     int i;
77
78     if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
79         return NULL;
80
81     /* direct structure assignment */
82     *ret = *sk;
83
84     if (sk->num == 0) {
85         /* postpone |ret| data allocation */
86         ret->data = NULL;
87         ret->num_alloc = 0;
88         return ret;
89     }
90
91     ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
92     ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
93     if (ret->data == NULL) {
94         OPENSSL_free(ret);
95         return NULL;
96     }
97
98     for (i = 0; i < ret->num; ++i) {
99         if (sk->data[i] == NULL)
100             continue;
101         if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
102             while (--i >= 0)
103                 if (ret->data[i] != NULL)
104                     free_func((void *)ret->data[i]);
105             OPENSSL_sk_free(ret);
106             return NULL;
107         }
108     }
109     return ret;
110 }
111
112 OPENSSL_STACK *OPENSSL_sk_new_null(void)
113 {
114     return OPENSSL_zalloc(sizeof(OPENSSL_STACK));
115 }
116
117 OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
118 {
119     OPENSSL_STACK *ret = OPENSSL_sk_new_null();
120
121     if (ret != NULL)
122         ret->comp = c;
123     return ret;
124 }
125
126 /*
127  * Calculate the array growth based on the target size.
128  *
129  * The growth fraction is a rational number and is defined by a numerator
130  * and a denominator.  According to Andrew Koenig in his paper "Why Are
131  * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
132  * than the golden ratio (1.618...).
133  *
134  * We use 3/2 = 1.5 for simplicity of calculation and overflow checking.
135  * Another option 8/5 = 1.6 allows for slightly faster growth, although safe
136  * computation is more difficult.
137  *
138  * The limit to avoid overflow is spot on.  The modulo three correction term
139  * ensures that the limit is the largest number than can be expanded by the
140  * growth factor without exceeding the hard limit.
141  *
142  * Do not call it with |current| lower than 2, or it will infinitely loop.
143  */
144 static ossl_inline int compute_growth(int target, int current)
145 {
146     const int limit = (max_nodes / 3) * 2 + (max_nodes % 3 ? 1 : 0);
147
148     while (current < target) {
149         /* Check to see if we're at the hard limit */
150         if (current >= max_nodes)
151             return 0;
152
153         /* Expand the size by a factor of 3/2 if it is within range */
154         current = current < limit ? current + current / 2 : max_nodes;
155     }
156     return current;
157 }
158
159 /* internal STACK storage allocation */
160 static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
161 {
162     const void **tmpdata;
163     int num_alloc;
164
165     /* Check to see the reservation isn't exceeding the hard limit */
166     if (n > max_nodes - st->num)
167         return 0;
168
169     /* Figure out the new size */
170     num_alloc = st->num + n;
171     if (num_alloc < min_nodes)
172         num_alloc = min_nodes;
173
174     /* If |st->data| allocation was postponed */
175     if (st->data == NULL) {
176         /*
177          * At this point, |st->num_alloc| and |st->num| are 0;
178          * so |num_alloc| value is |n| or |min_nodes| if greater than |n|.
179          */
180         st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);
181         if (st->data == NULL)
182             return 0;
183         st->num_alloc = num_alloc;
184         return 1;
185     }
186
187     if (!exact) {
188         if (num_alloc <= st->num_alloc)
189             return 1;
190         num_alloc = compute_growth(num_alloc, st->num_alloc);
191         if (num_alloc == 0)
192             return 0;
193     } else if (num_alloc == st->num_alloc) {
194         return 1;
195     }
196
197     tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
198     if (tmpdata == NULL)
199         return 0;
200
201     st->data = tmpdata;
202     st->num_alloc = num_alloc;
203     return 1;
204 }
205
206 int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n)
207 {
208     if (st == NULL)
209         return 0;
210
211     if (n < 0)
212         return 1;
213     return sk_reserve(st, n, 1);
214 }
215
216 int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
217 {
218     if (st == NULL || st->num == max_nodes)
219         return 0;
220
221     if (!sk_reserve(st, 1, 0))
222         return 0;
223
224     if ((loc >= st->num) || (loc < 0)) {
225         st->data[st->num] = data;
226     } else {
227         memmove(&st->data[loc + 1], &st->data[loc],
228                 sizeof(st->data[0]) * (st->num - loc));
229         st->data[loc] = data;
230     }
231     st->num++;
232     st->sorted = 0;
233     return st->num;
234 }
235
236 static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc)
237 {
238     const void *ret = st->data[loc];
239
240     if (loc != st->num - 1)
241          memmove(&st->data[loc], &st->data[loc + 1],
242                  sizeof(st->data[0]) * (st->num - loc - 1));
243     st->num--;
244
245     return (void *)ret;
246 }
247
248 void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p)
249 {
250     int i;
251
252     for (i = 0; i < st->num; i++)
253         if (st->data[i] == p)
254             return internal_delete(st, i);
255     return NULL;
256 }
257
258 void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
259 {
260     if (st == NULL || loc < 0 || loc >= st->num)
261         return NULL;
262
263     return internal_delete(st, loc);
264 }
265
266 static int internal_find(OPENSSL_STACK *st, const void *data,
267                          int ret_val_options)
268 {
269     const void *r;
270     int i;
271
272     if (st == NULL || st->num == 0)
273         return -1;
274
275     if (st->comp == NULL) {
276         for (i = 0; i < st->num; i++)
277             if (st->data[i] == data)
278                 return i;
279         return -1;
280     }
281
282     if (!st->sorted) {
283         if (st->num > 1)
284             qsort(st->data, st->num, sizeof(void *), st->comp);
285         st->sorted = 1; /* empty or single-element stack is considered sorted */
286     }
287     if (data == NULL)
288         return -1;
289     r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
290                         ret_val_options);
291
292     return r == NULL ? -1 : (int)((const void **)r - st->data);
293 }
294
295 int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
296 {
297     return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
298 }
299
300 int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data)
301 {
302     return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
303 }
304
305 int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
306 {
307     if (st == NULL)
308         return -1;
309     return OPENSSL_sk_insert(st, data, st->num);
310 }
311
312 int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
313 {
314     return OPENSSL_sk_insert(st, data, 0);
315 }
316
317 void *OPENSSL_sk_shift(OPENSSL_STACK *st)
318 {
319     if (st == NULL || st->num == 0)
320         return NULL;
321     return internal_delete(st, 0);
322 }
323
324 void *OPENSSL_sk_pop(OPENSSL_STACK *st)
325 {
326     if (st == NULL || st->num == 0)
327         return NULL;
328     return internal_delete(st, st->num - 1);
329 }
330
331 void OPENSSL_sk_zero(OPENSSL_STACK *st)
332 {
333     if (st == NULL || st->num == 0)
334         return;
335     memset(st->data, 0, sizeof(*st->data) * st->num);
336     st->num = 0;
337 }
338
339 void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func)
340 {
341     int i;
342
343     if (st == NULL)
344         return;
345     for (i = 0; i < st->num; i++)
346         if (st->data[i] != NULL)
347             func((char *)st->data[i]);
348     OPENSSL_sk_free(st);
349 }
350
351 void OPENSSL_sk_free(OPENSSL_STACK *st)
352 {
353     if (st == NULL)
354         return;
355     OPENSSL_free(st->data);
356     OPENSSL_free(st);
357 }
358
359 int OPENSSL_sk_num(const OPENSSL_STACK *st)
360 {
361     return st == NULL ? -1 : st->num;
362 }
363
364 void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
365 {
366     if (st == NULL || i < 0 || i >= st->num)
367         return NULL;
368     return (void *)st->data[i];
369 }
370
371 void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data)
372 {
373     if (st == NULL || i < 0 || i >= st->num)
374         return NULL;
375     st->data[i] = data;
376     st->sorted = 0;
377     return (void *)st->data[i];
378 }
379
380 void OPENSSL_sk_sort(OPENSSL_STACK *st)
381 {
382     if (st != NULL && !st->sorted && st->comp != NULL) {
383         if (st->num > 1)
384             qsort(st->data, st->num, sizeof(void *), st->comp);
385         st->sorted = 1; /* empty or single-element stack is considered sorted */
386     }
387 }
388
389 int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)
390 {
391     return st == NULL ? 1 : st->sorted;
392 }