2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <openssl/stack.h>
14 #include <openssl/objects.h>
16 #include <openssl/e_os2.h> /* For ossl_inline */
19 * The initial number of nodes in the array.
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 *))
31 OPENSSL_sk_compfunc comp;
34 OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, OPENSSL_sk_compfunc c)
36 OPENSSL_sk_compfunc old = sk->comp;
45 OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk)
52 if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
55 /* direct structure assignment */
59 /* postpone |ret->data| allocation */
64 /* duplicate |sk->data| content */
65 if ((ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc)) == NULL)
67 memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
74 OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk,
75 OPENSSL_sk_copyfunc copy_func,
76 OPENSSL_sk_freefunc free_func)
84 if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
87 /* direct structure assignment */
91 /* postpone |ret| data allocation */
97 ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes;
98 ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc);
99 if (ret->data == NULL) {
104 for (i = 0; i < ret->num; ++i) {
105 if (sk->data[i] == NULL)
107 if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
109 if (ret->data[i] != NULL)
110 free_func((void *)ret->data[i]);
111 OPENSSL_sk_free(ret);
118 OPENSSL_STACK *OPENSSL_sk_new_null(void)
120 return OPENSSL_zalloc(sizeof(OPENSSL_STACK));
123 OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c)
125 OPENSSL_STACK *ret = OPENSSL_sk_new_null();
133 * Calculate the array growth based on the target size.
135 * The growth fraction is a rational number and is defined by a numerator
136 * and a denominator. According to Andrew Koenig in his paper "Why Are
137 * Vectors Efficient?" from JOOP 11(5) 1998, this factor should be less
138 * than the golden ratio (1.618...).
140 * We use 3/2 = 1.5 for simplicity of calculation and overflow checking.
141 * Another option 8/5 = 1.6 allows for slightly faster growth, although safe
142 * computation is more difficult.
144 * The limit to avoid overflow is spot on. The modulo three correction term
145 * ensures that the limit is the largest number than can be expanded by the
146 * growth factor without exceeding the hard limit.
148 * Do not call it with |current| lower than 2, or it will infinitely loop.
150 static ossl_inline int compute_growth(int target, int current)
152 const int limit = (max_nodes / 3) * 2 + (max_nodes % 3 ? 1 : 0);
154 while (current < target) {
155 /* Check to see if we're at the hard limit */
156 if (current >= max_nodes)
159 /* Expand the size by a factor of 3/2 if it is within range */
160 current = current < limit ? current + current / 2 : max_nodes;
165 /* internal STACK storage allocation */
166 static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
168 const void **tmpdata;
171 /* Check to see the reservation isn't exceeding the hard limit */
172 if (n > max_nodes - st->num)
175 /* Figure out the new size */
176 num_alloc = st->num + n;
177 if (num_alloc < min_nodes)
178 num_alloc = min_nodes;
180 /* If |st->data| allocation was postponed */
181 if (st->data == NULL) {
183 * At this point, |st->num_alloc| and |st->num| are 0;
184 * so |num_alloc| value is |n| or |min_nodes| if greater than |n|.
186 st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);
187 if (st->data == NULL)
189 st->num_alloc = num_alloc;
194 if (num_alloc <= st->num_alloc)
196 num_alloc = compute_growth(num_alloc, st->num_alloc);
199 } else if (num_alloc == st->num_alloc) {
203 tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
208 st->num_alloc = num_alloc;
212 int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n)
214 if (st == NULL || st->num < 0)
219 return sk_reserve(st, n, 1);
222 int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
224 if (st == NULL || st->num < 0 || st->num == max_nodes)
227 if (!sk_reserve(st, 1, 0))
230 if ((loc >= st->num) || (loc < 0)) {
231 st->data[st->num] = data;
233 memmove(&st->data[loc + 1], &st->data[loc],
234 sizeof(st->data[0]) * (st->num - loc));
235 st->data[loc] = data;
242 static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc)
244 const void *ret = st->data[loc];
246 if (loc != st->num - 1)
247 memmove(&st->data[loc], &st->data[loc + 1],
248 sizeof(st->data[0]) * (st->num - loc - 1));
254 void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p)
258 for (i = 0; i < st->num; i++)
259 if (st->data[i] == p)
260 return internal_delete(st, i);
264 void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc)
266 if (st == NULL || loc < 0 || loc >= st->num)
269 return internal_delete(st, loc);
272 static int internal_find(OPENSSL_STACK *st, const void *data,
278 if (st == NULL || st->num == 0)
281 if (st->comp == NULL) {
282 for (i = 0; i < st->num; i++)
283 if (st->data[i] == data)
290 r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
294 return (int)((const void **)r - st->data);
297 int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)
299 return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
302 int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data)
304 return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
307 int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
311 return OPENSSL_sk_insert(st, data, st->num);
314 int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data)
316 return (OPENSSL_sk_insert(st, data, 0));
319 void *OPENSSL_sk_shift(OPENSSL_STACK *st)
325 return internal_delete(st, 0);
328 void *OPENSSL_sk_pop(OPENSSL_STACK *st)
334 return internal_delete(st, st->num - 1);
337 void OPENSSL_sk_zero(OPENSSL_STACK *st)
343 memset(st->data, 0, sizeof(*st->data) * st->num);
347 void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func)
353 for (i = 0; i < st->num; i++)
354 if (st->data[i] != NULL)
355 func((char *)st->data[i]);
359 void OPENSSL_sk_free(OPENSSL_STACK *st)
363 OPENSSL_free(st->data);
367 int OPENSSL_sk_num(const OPENSSL_STACK *st)
374 void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i)
376 if (st == NULL || i < 0 || i >= st->num)
378 return (void *)st->data[i];
381 void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data)
383 if (st == NULL || i < 0 || i >= st->num)
386 return (void *)st->data[i];
389 void OPENSSL_sk_sort(OPENSSL_STACK *st)
391 if (st && !st->sorted && st->comp != NULL) {
392 if (st->data != NULL)
393 qsort(st->data, st->num, sizeof(void *), st->comp);
398 int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st)