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