blob: bc8c6f851bc0efaa5dcf328b2bff5a3c57963934 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
Adam Langley4c921e12014-07-14 15:28:14 -070057#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -070058
Adam Langley2b2d66d2015-01-30 17:08:37 -080059#include <string.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/mem.h>
62
63/* kMinSize is the number of pointers that will be initially allocated in a new
64 * stack. */
65static const size_t kMinSize = 4;
66
67_STACK *sk_new(stack_cmp_func comp) {
68 _STACK *ret;
69
70 ret = OPENSSL_malloc(sizeof(_STACK));
71 if (ret == NULL) {
72 goto err;
73 }
74 memset(ret, 0, sizeof(_STACK));
75
76 ret->data = OPENSSL_malloc(sizeof(void *) * kMinSize);
77 if (ret->data == NULL) {
78 goto err;
79 }
80
81 memset(ret->data, 0, sizeof(void *) * kMinSize);
82
83 ret->comp = comp;
84 ret->num_alloc = kMinSize;
85
86 return ret;
87
88err:
89 if (ret) {
90 OPENSSL_free(ret);
91 }
92 return NULL;
93}
94
95_STACK *sk_new_null(void) { return sk_new(NULL); }
96
97size_t sk_num(const _STACK *sk) {
98 if (sk == NULL) {
99 return 0;
100 }
101 return sk->num;
102}
103
104void sk_zero(_STACK *sk) {
105 if (sk == NULL || sk->num == 0) {
106 return;
107 }
108 memset(sk->data, 0, sizeof(void*) * sk->num);
109 sk->num = 0;
110 sk->sorted = 0;
111}
112
113void *sk_value(const _STACK *sk, size_t i) {
114 if (!sk || i >= sk->num) {
115 return NULL;
116 }
117 return sk->data[i];
118}
119
120void *sk_set(_STACK *sk, size_t i, void *value) {
121 if (!sk || i >= sk->num) {
122 return NULL;
123 }
124 return sk->data[i] = value;
125}
126
127void sk_free(_STACK *sk) {
128 if (sk == NULL) {
129 return;
130 }
131 OPENSSL_free(sk->data);
132 OPENSSL_free(sk);
133}
134
135void sk_pop_free(_STACK *sk, void (*func)(void *)) {
136 size_t i;
137
138 if (sk == NULL) {
139 return;
140 }
141
142 for (i = 0; i < sk->num; i++) {
143 if (sk->data[i] != NULL) {
144 func(sk->data[i]);
145 }
146 }
147 sk_free(sk);
148}
149
150size_t sk_insert(_STACK *sk, void *p, size_t where) {
151 if (sk == NULL) {
152 return 0;
153 }
154
155 if (sk->num_alloc <= sk->num + 1) {
156 /* Attempt to double the size of the array. */
157 size_t new_alloc = sk->num_alloc << 1;
158 size_t alloc_size = new_alloc * sizeof(void *);
159 void **data;
160
161 /* If the doubling overflowed, try to increment. */
162 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
163 new_alloc = sk->num_alloc + 1;
164 alloc_size = new_alloc * sizeof(void *);
165 }
166
167 /* If the increment also overflowed, fail. */
168 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
169 return 0;
170 }
171
172 data = OPENSSL_realloc(sk->data, alloc_size);
173 if (data == NULL) {
174 return 0;
175 }
176
177 sk->data = data;
178 sk->num_alloc = new_alloc;
179 }
180
181 if (where >= sk->num) {
182 sk->data[sk->num] = p;
183 } else {
184 memmove(&sk->data[where + 1], &sk->data[where],
185 sizeof(void *) * (sk->num - where));
186 sk->data[where] = p;
187 }
188
189 sk->num++;
190 sk->sorted = 0;
191
192 return sk->num;
193}
194
195void *sk_delete(_STACK *sk, size_t where) {
196 void *ret;
197
198 if (!sk || where >= sk->num) {
199 return NULL;
200 }
201
202 ret = sk->data[where];
203
204 if (where != sk->num - 1) {
205 memmove(&sk->data[where], &sk->data[where + 1],
206 sizeof(void *) * (sk->num - where - 1));
207 }
208
209 sk->num--;
210 return ret;
211}
212
213void *sk_delete_ptr(_STACK *sk, void *p) {
214 size_t i;
215
216 if (sk == NULL) {
217 return NULL;
218 }
219
220 for (i = 0; i < sk->num; i++) {
221 if (sk->data[i] == p) {
222 return sk_delete(sk, i);
223 }
224 }
225
226 return NULL;
227}
228
229int sk_find(_STACK *sk, size_t *out_index, void *p) {
230 const void *const *r;
231 size_t i;
232 int (*comp_func)(const void *,const void *);
233
234 if (sk == NULL) {
Doug Hogan41846c72015-04-22 23:48:22 -0700235 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700236 }
237
238 if (sk->comp == NULL) {
239 /* Use pointer equality when no comparison function has been set. */
240 for (i = 0; i < sk->num; i++) {
241 if (sk->data[i] == p) {
242 if (out_index) {
243 *out_index = i;
244 }
245 return 1;
246 }
247 }
248 return 0;
249 }
250
251 if (p == NULL) {
252 return 0;
253 }
254
255 sk_sort(sk);
256
257 /* sk->comp is a function that takes pointers to pointers to elements, but
258 * qsort and bsearch take a comparison function that just takes pointers to
259 * elements. However, since we're passing an array of pointers to
260 * qsort/bsearch, we can just cast the comparison function and everything
261 * works. */
262 comp_func=(int (*)(const void *,const void *))(sk->comp);
263 r = bsearch(&p, sk->data, sk->num, sizeof(void *), comp_func);
264 if (r == NULL) {
265 return 0;
266 }
267 i = ((void **)r) - sk->data;
268 /* This function always returns the first result. */
269 while (i > 0 && sk->comp((const void**) &p, (const void**) &sk->data[i-1]) == 0) {
270 i--;
271 }
272 if (out_index) {
273 *out_index = i;
274 }
275 return 1;
276}
277
278void *sk_shift(_STACK *sk) {
279 if (sk == NULL) {
280 return NULL;
281 }
282 if (sk->num == 0) {
283 return NULL;
284 }
285 return sk_delete(sk, 0);
286}
287
288size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); }
289
290void *sk_pop(_STACK *sk) {
291 if (sk == NULL) {
292 return NULL;
293 }
294 if (sk->num == 0) {
295 return NULL;
296 }
297 return sk_delete(sk, sk->num - 1);
298}
299
300_STACK *sk_dup(const _STACK *sk) {
301 _STACK *ret;
302 void **s;
303
304 if (sk == NULL) {
305 return NULL;
306 }
307
308 ret = sk_new(sk->comp);
309 if (ret == NULL) {
310 goto err;
311 }
312
313 s = (void **)OPENSSL_realloc(ret->data, sizeof(void *) * sk->num_alloc);
314 if (s == NULL) {
315 goto err;
316 }
317 ret->data = s;
318
319 ret->num = sk->num;
320 memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
321 ret->sorted = sk->sorted;
322 ret->num_alloc = sk->num_alloc;
323 ret->comp = sk->comp;
324 return ret;
325
326err:
327 if (ret) {
328 sk_free(ret);
329 }
330 return NULL;
331}
332
333void sk_sort(_STACK *sk) {
334 int (*comp_func)(const void *,const void *);
335
336 if (sk == NULL || sk->sorted) {
337 return;
338 }
339
340 /* See the comment in sk_find about this cast. */
341 comp_func = (int (*)(const void *, const void *))(sk->comp);
342 qsort(sk->data, sk->num, sizeof(void *), comp_func);
343 sk->sorted = 1;
344}
345
346int sk_is_sorted(const _STACK *sk) {
347 if (!sk) {
348 return 1;
349 }
350 return sk->sorted;
351}
352
353stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
354 stack_cmp_func old = sk->comp;
355
356 if (sk->comp != comp) {
357 sk->sorted = 0;
358 }
359 sk->comp = comp;
360
361 return old;
362}
Adam Langleya1048a72015-02-11 14:27:21 -0800363
364_STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *),
365 void (*free_func)(void *)) {
366 _STACK *ret = sk_dup(sk);
367 if (ret == NULL) {
368 return NULL;
369 }
370
371 size_t i;
372 for (i = 0; i < ret->num; i++) {
373 if (ret->data[i] == NULL) {
374 continue;
375 }
376 ret->data[i] = copy_func(ret->data[i]);
377 if (ret->data[i] == NULL) {
378 size_t j;
379 for (j = 0; j < i; j++) {
380 if (ret->data[j] != NULL) {
381 free_func(ret->data[j]);
382 }
383 }
384 sk_free(ret);
385 return NULL;
386 }
387 }
388
389 return ret;
390}