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