xref: /openbsd-src/lib/libcrypto/stack/stack.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
1 /* $OpenBSD: stack.c,v 1.24 2024/01/13 16:32:53 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 #undef MIN_NODES
67 #define MIN_NODES	4
68 
69 int
70 (*sk_set_cmp_func(_STACK *sk, int (*c)(const void *, const void *)))(
71     const void *, const void *)
72 {
73 	int (*old)(const void *, const void *) = sk->comp;
74 
75 	if (sk->comp != c)
76 		sk->sorted = 0;
77 	sk->comp = c;
78 
79 	return old;
80 }
81 LCRYPTO_ALIAS(sk_set_cmp_func);
82 
83 _STACK *
84 sk_dup(_STACK *sk)
85 {
86 	_STACK *ret;
87 	char **s;
88 
89 	if ((ret = sk_new(sk->comp)) == NULL)
90 		goto err;
91 	s = reallocarray(ret->data, sk->num_alloc, sizeof(char *));
92 	if (s == NULL)
93 		goto err;
94 	ret->data = s;
95 
96 	ret->num = sk->num;
97 	memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
98 	ret->sorted = sk->sorted;
99 	ret->num_alloc = sk->num_alloc;
100 	ret->comp = sk->comp;
101 	return (ret);
102 
103 err:
104 	if (ret)
105 		sk_free(ret);
106 	return (NULL);
107 }
108 LCRYPTO_ALIAS(sk_dup);
109 
110 _STACK *
111 sk_new_null(void)
112 {
113 	return sk_new((int (*)(const void *, const void *))0);
114 }
115 LCRYPTO_ALIAS(sk_new_null);
116 
117 _STACK *
118 sk_new(int (*c)(const void *, const void *))
119 {
120 	_STACK *ret;
121 	int i;
122 
123 	if ((ret = malloc(sizeof(_STACK))) == NULL)
124 		goto err;
125 	if ((ret->data = reallocarray(NULL, MIN_NODES, sizeof(char *))) == NULL)
126 		goto err;
127 	for (i = 0; i < MIN_NODES; i++)
128 		ret->data[i] = NULL;
129 	ret->comp = c;
130 	ret->num_alloc = MIN_NODES;
131 	ret->num = 0;
132 	ret->sorted = 0;
133 	return (ret);
134 
135 err:
136 	free(ret);
137 	return (NULL);
138 }
139 LCRYPTO_ALIAS(sk_new);
140 
141 int
142 sk_insert(_STACK *st, void *data, int loc)
143 {
144 	char **s;
145 
146 	if (st == NULL)
147 		return 0;
148 	if (st->num_alloc <= st->num + 1) {
149 		s = reallocarray(st->data, st->num_alloc, 2 * sizeof(char *));
150 		if (s == NULL)
151 			return (0);
152 		st->data = s;
153 		st->num_alloc *= 2;
154 	}
155 	if ((loc >= (int)st->num) || (loc < 0))
156 		st->data[st->num] = data;
157 	else {
158 		memmove(&(st->data[loc + 1]), &(st->data[loc]),
159 		    sizeof(char *)*(st->num - loc));
160 		st->data[loc] = data;
161 	}
162 	st->num++;
163 	st->sorted = 0;
164 	return (st->num);
165 }
166 LCRYPTO_ALIAS(sk_insert);
167 
168 void *
169 sk_delete_ptr(_STACK *st, void *p)
170 {
171 	int i;
172 
173 	for (i = 0; i < st->num; i++)
174 		if (st->data[i] == p)
175 			return (sk_delete(st, i));
176 	return (NULL);
177 }
178 LCRYPTO_ALIAS(sk_delete_ptr);
179 
180 void *
181 sk_delete(_STACK *st, int loc)
182 {
183 	char *ret;
184 
185 	if (!st || (loc < 0) || (loc >= st->num))
186 		return NULL;
187 
188 	ret = st->data[loc];
189 	if (loc != st->num - 1) {
190 		memmove(&(st->data[loc]), &(st->data[loc + 1]),
191 		    sizeof(char *)*(st->num - 1 - loc));
192 	}
193 	st->num--;
194 	return (ret);
195 }
196 LCRYPTO_ALIAS(sk_delete);
197 
198 static int
199 internal_find(_STACK *st, void *data, int ret_val_options)
200 {
201 	const void * const *r;
202 	int i;
203 
204 	if (st == NULL)
205 		return -1;
206 
207 	if (st->comp == NULL) {
208 		for (i = 0; i < st->num; i++)
209 			if (st->data[i] == data)
210 				return (i);
211 		return (-1);
212 	}
213 	sk_sort(st);
214 	if (data == NULL)
215 		return (-1);
216 	r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
217 	    ret_val_options);
218 	if (r == NULL)
219 		return (-1);
220 	return (int)((char **)r - st->data);
221 }
222 
223 int
224 sk_find(_STACK *st, void *data)
225 {
226 	return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
227 }
228 LCRYPTO_ALIAS(sk_find);
229 
230 int
231 sk_find_ex(_STACK *st, void *data)
232 {
233 	return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
234 }
235 LCRYPTO_ALIAS(sk_find_ex);
236 
237 int
238 sk_push(_STACK *st, void *data)
239 {
240 	return (sk_insert(st, data, st->num));
241 }
242 LCRYPTO_ALIAS(sk_push);
243 
244 int
245 sk_unshift(_STACK *st, void *data)
246 {
247 	return (sk_insert(st, data, 0));
248 }
249 LCRYPTO_ALIAS(sk_unshift);
250 
251 void *
252 sk_shift(_STACK *st)
253 {
254 	if (st == NULL)
255 		return (NULL);
256 	if (st->num <= 0)
257 		return (NULL);
258 	return (sk_delete(st, 0));
259 }
260 LCRYPTO_ALIAS(sk_shift);
261 
262 void *
263 sk_pop(_STACK *st)
264 {
265 	if (st == NULL)
266 		return (NULL);
267 	if (st->num <= 0)
268 		return (NULL);
269 	return (sk_delete(st, st->num - 1));
270 }
271 LCRYPTO_ALIAS(sk_pop);
272 
273 void
274 sk_zero(_STACK *st)
275 {
276 	if (st == NULL)
277 		return;
278 	if (st->num <= 0)
279 		return;
280 	memset(st->data, 0, sizeof(st->data)*st->num);
281 	st->num = 0;
282 }
283 LCRYPTO_ALIAS(sk_zero);
284 
285 void
286 sk_pop_free(_STACK *st, void (*func)(void *))
287 {
288 	int i;
289 
290 	if (st == NULL)
291 		return;
292 	for (i = 0; i < st->num; i++)
293 		if (st->data[i] != NULL)
294 			func(st->data[i]);
295 	sk_free(st);
296 }
297 LCRYPTO_ALIAS(sk_pop_free);
298 
299 void
300 sk_free(_STACK *st)
301 {
302 	if (st == NULL)
303 		return;
304 	free(st->data);
305 	free(st);
306 }
307 LCRYPTO_ALIAS(sk_free);
308 
309 int
310 sk_num(const _STACK *st)
311 {
312 	if (st == NULL)
313 		return -1;
314 	return st->num;
315 }
316 LCRYPTO_ALIAS(sk_num);
317 
318 void *
319 sk_value(const _STACK *st, int i)
320 {
321 	if (!st || (i < 0) || (i >= st->num))
322 		return NULL;
323 	return st->data[i];
324 }
325 LCRYPTO_ALIAS(sk_value);
326 
327 void *
328 sk_set(_STACK *st, int i, void *value)
329 {
330 	if (!st || (i < 0) || (i >= st->num))
331 		return NULL;
332 	st->sorted = 0;
333 	return (st->data[i] = value);
334 }
335 LCRYPTO_ALIAS(sk_set);
336 
337 void
338 sk_sort(_STACK *st)
339 {
340 	if (st && !st->sorted) {
341 		int (*comp_func)(const void *, const void *);
342 
343 		/* same comment as in sk_find ... previously st->comp was declared
344 		 * as a (void*,void*) callback type, but this made the population
345 		 * of the callback pointer illogical - our callbacks compare
346 		 * type** with type**, so we leave the casting until absolutely
347 		 * necessary (ie. "now"). */
348 		comp_func = (int (*)(const void *, const void *))(st->comp);
349 		qsort(st->data, st->num, sizeof(char *), comp_func);
350 		st->sorted = 1;
351 	}
352 }
353 LCRYPTO_ALIAS(sk_sort);
354 
355 int
356 sk_is_sorted(const _STACK *st)
357 {
358 	if (st == NULL)
359 		return 1;
360 
361 	if (st->sorted)
362 		return 1;
363 
364 	/* If there is no comparison function we cannot sort. */
365 	if (st->comp == NULL)
366 		return 0;
367 
368 	/* Lists with zero or one elements are always sorted. */
369 	return st->num <= 1;
370 }
371 LCRYPTO_ALIAS(sk_is_sorted);
372