1 /* 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
57 #include <openssl/stack.h>
58
59 #include <string.h>
60
61 #include <openssl/mem.h>
62
63 /* kMinSize is the number of pointers that will be initially allocated in a new
64 * stack. */
65 static const size_t kMinSize = 4;
66
sk_new(stack_cmp_func comp)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
88 err:
89 OPENSSL_free(ret);
90 return NULL;
91 }
92
sk_new_null(void)93 _STACK *sk_new_null(void) { return sk_new(NULL); }
94
sk_num(const _STACK * sk)95 size_t sk_num(const _STACK *sk) {
96 if (sk == NULL) {
97 return 0;
98 }
99 return sk->num;
100 }
101
sk_zero(_STACK * sk)102 void sk_zero(_STACK *sk) {
103 if (sk == NULL || sk->num == 0) {
104 return;
105 }
106 memset(sk->data, 0, sizeof(void*) * sk->num);
107 sk->num = 0;
108 sk->sorted = 0;
109 }
110
sk_value(const _STACK * sk,size_t i)111 void *sk_value(const _STACK *sk, size_t i) {
112 if (!sk || i >= sk->num) {
113 return NULL;
114 }
115 return sk->data[i];
116 }
117
sk_set(_STACK * sk,size_t i,void * value)118 void *sk_set(_STACK *sk, size_t i, void *value) {
119 if (!sk || i >= sk->num) {
120 return NULL;
121 }
122 return sk->data[i] = value;
123 }
124
sk_free(_STACK * sk)125 void sk_free(_STACK *sk) {
126 if (sk == NULL) {
127 return;
128 }
129 OPENSSL_free(sk->data);
130 OPENSSL_free(sk);
131 }
132
sk_pop_free(_STACK * sk,void (* func)(void *))133 void sk_pop_free(_STACK *sk, void (*func)(void *)) {
134 size_t i;
135
136 if (sk == NULL) {
137 return;
138 }
139
140 for (i = 0; i < sk->num; i++) {
141 if (sk->data[i] != NULL) {
142 func(sk->data[i]);
143 }
144 }
145 sk_free(sk);
146 }
147
sk_insert(_STACK * sk,void * p,size_t where)148 size_t sk_insert(_STACK *sk, void *p, size_t where) {
149 if (sk == NULL) {
150 return 0;
151 }
152
153 if (sk->num_alloc <= sk->num + 1) {
154 /* Attempt to double the size of the array. */
155 size_t new_alloc = sk->num_alloc << 1;
156 size_t alloc_size = new_alloc * sizeof(void *);
157 void **data;
158
159 /* If the doubling overflowed, try to increment. */
160 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
161 new_alloc = sk->num_alloc + 1;
162 alloc_size = new_alloc * sizeof(void *);
163 }
164
165 /* If the increment also overflowed, fail. */
166 if (new_alloc < sk->num_alloc || alloc_size / sizeof(void *) != new_alloc) {
167 return 0;
168 }
169
170 data = OPENSSL_realloc(sk->data, alloc_size);
171 if (data == NULL) {
172 return 0;
173 }
174
175 sk->data = data;
176 sk->num_alloc = new_alloc;
177 }
178
179 if (where >= sk->num) {
180 sk->data[sk->num] = p;
181 } else {
182 memmove(&sk->data[where + 1], &sk->data[where],
183 sizeof(void *) * (sk->num - where));
184 sk->data[where] = p;
185 }
186
187 sk->num++;
188 sk->sorted = 0;
189
190 return sk->num;
191 }
192
sk_delete(_STACK * sk,size_t where)193 void *sk_delete(_STACK *sk, size_t where) {
194 void *ret;
195
196 if (!sk || where >= sk->num) {
197 return NULL;
198 }
199
200 ret = sk->data[where];
201
202 if (where != sk->num - 1) {
203 memmove(&sk->data[where], &sk->data[where + 1],
204 sizeof(void *) * (sk->num - where - 1));
205 }
206
207 sk->num--;
208 return ret;
209 }
210
sk_delete_ptr(_STACK * sk,void * p)211 void *sk_delete_ptr(_STACK *sk, void *p) {
212 size_t i;
213
214 if (sk == NULL) {
215 return NULL;
216 }
217
218 for (i = 0; i < sk->num; i++) {
219 if (sk->data[i] == p) {
220 return sk_delete(sk, i);
221 }
222 }
223
224 return NULL;
225 }
226
sk_find(_STACK * sk,size_t * out_index,void * p)227 int sk_find(_STACK *sk, size_t *out_index, void *p) {
228 const void *const *r;
229 size_t i;
230 int (*comp_func)(const void *,const void *);
231
232 if (sk == NULL) {
233 return 0;
234 }
235
236 if (sk->comp == NULL) {
237 /* Use pointer equality when no comparison function has been set. */
238 for (i = 0; i < sk->num; i++) {
239 if (sk->data[i] == p) {
240 if (out_index) {
241 *out_index = i;
242 }
243 return 1;
244 }
245 }
246 return 0;
247 }
248
249 if (p == NULL) {
250 return 0;
251 }
252
253 sk_sort(sk);
254
255 /* sk->comp is a function that takes pointers to pointers to elements, but
256 * qsort and bsearch take a comparison function that just takes pointers to
257 * elements. However, since we're passing an array of pointers to
258 * qsort/bsearch, we can just cast the comparison function and everything
259 * works. */
260 comp_func=(int (*)(const void *,const void *))(sk->comp);
261 r = bsearch(&p, sk->data, sk->num, sizeof(void *), comp_func);
262 if (r == NULL) {
263 return 0;
264 }
265 i = ((void **)r) - sk->data;
266 /* This function always returns the first result. */
267 while (i > 0 && sk->comp((const void**) &p, (const void**) &sk->data[i-1]) == 0) {
268 i--;
269 }
270 if (out_index) {
271 *out_index = i;
272 }
273 return 1;
274 }
275
sk_shift(_STACK * sk)276 void *sk_shift(_STACK *sk) {
277 if (sk == NULL) {
278 return NULL;
279 }
280 if (sk->num == 0) {
281 return NULL;
282 }
283 return sk_delete(sk, 0);
284 }
285
sk_push(_STACK * sk,void * p)286 size_t sk_push(_STACK *sk, void *p) { return (sk_insert(sk, p, sk->num)); }
287
sk_pop(_STACK * sk)288 void *sk_pop(_STACK *sk) {
289 if (sk == NULL) {
290 return NULL;
291 }
292 if (sk->num == 0) {
293 return NULL;
294 }
295 return sk_delete(sk, sk->num - 1);
296 }
297
sk_dup(const _STACK * sk)298 _STACK *sk_dup(const _STACK *sk) {
299 _STACK *ret;
300 void **s;
301
302 if (sk == NULL) {
303 return NULL;
304 }
305
306 ret = sk_new(sk->comp);
307 if (ret == NULL) {
308 goto err;
309 }
310
311 s = (void **)OPENSSL_realloc(ret->data, sizeof(void *) * sk->num_alloc);
312 if (s == NULL) {
313 goto err;
314 }
315 ret->data = s;
316
317 ret->num = sk->num;
318 memcpy(ret->data, sk->data, sizeof(void *) * sk->num);
319 ret->sorted = sk->sorted;
320 ret->num_alloc = sk->num_alloc;
321 ret->comp = sk->comp;
322 return ret;
323
324 err:
325 sk_free(ret);
326 return NULL;
327 }
328
sk_sort(_STACK * sk)329 void sk_sort(_STACK *sk) {
330 int (*comp_func)(const void *,const void *);
331
332 if (sk == NULL || sk->sorted) {
333 return;
334 }
335
336 /* See the comment in sk_find about this cast. */
337 comp_func = (int (*)(const void *, const void *))(sk->comp);
338 qsort(sk->data, sk->num, sizeof(void *), comp_func);
339 sk->sorted = 1;
340 }
341
sk_is_sorted(const _STACK * sk)342 int sk_is_sorted(const _STACK *sk) {
343 if (!sk) {
344 return 1;
345 }
346 return sk->sorted;
347 }
348
sk_set_cmp_func(_STACK * sk,stack_cmp_func comp)349 stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) {
350 stack_cmp_func old = sk->comp;
351
352 if (sk->comp != comp) {
353 sk->sorted = 0;
354 }
355 sk->comp = comp;
356
357 return old;
358 }
359
sk_deep_copy(const _STACK * sk,void * (* copy_func)(void *),void (* free_func)(void *))360 _STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *),
361 void (*free_func)(void *)) {
362 _STACK *ret = sk_dup(sk);
363 if (ret == NULL) {
364 return NULL;
365 }
366
367 size_t i;
368 for (i = 0; i < ret->num; i++) {
369 if (ret->data[i] == NULL) {
370 continue;
371 }
372 ret->data[i] = copy_func(ret->data[i]);
373 if (ret->data[i] == NULL) {
374 size_t j;
375 for (j = 0; j < i; j++) {
376 if (ret->data[j] != NULL) {
377 free_func(ret->data[j]);
378 }
379 }
380 sk_free(ret);
381 return NULL;
382 }
383 }
384
385 return ret;
386 }
387