d486f335b2ebec07c9bfef6e2a9890ddfc61a434
[openssl.git] / crypto / stack / stack.c
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 <stdio.h>
58 #include "internal/cryptlib.h"
59 #include <openssl/stack.h>
60 #include <openssl/objects.h>
61
62 struct stack_st {
63     int num;
64     char **data;
65     int sorted;
66     int num_alloc;
67     int (*comp) (const void *, const void *);
68 };
69
70 #undef MIN_NODES
71 #define MIN_NODES       4
72
73 #include <errno.h>
74
75 int (*sk_set_cmp_func(_STACK *sk, int (*c) (const void *, const void *)))
76  (const void *, const void *) {
77     int (*old) (const void *, const void *) = sk->comp;
78
79     if (sk->comp != c)
80         sk->sorted = 0;
81     sk->comp = c;
82
83     return old;
84 }
85
86 _STACK *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 = OPENSSL_realloc((char *)ret->data,
94                         (unsigned int)sizeof(char *) * sk->num_alloc);
95     if (s == NULL)
96         goto err;
97     ret->data = s;
98
99     ret->num = sk->num;
100     memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
101     ret->sorted = sk->sorted;
102     ret->num_alloc = sk->num_alloc;
103     ret->comp = sk->comp;
104     return (ret);
105  err:
106     sk_free(ret);
107     return (NULL);
108 }
109
110 _STACK *sk_deep_copy(_STACK *sk, void *(*copy_func) (void *),
111                      void (*free_func) (void *))
112 {
113     _STACK *ret;
114     int i;
115
116     if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
117         return ret;
118     ret->comp = sk->comp;
119     ret->sorted = sk->sorted;
120     ret->num = sk->num;
121     ret->num_alloc = sk->num > MIN_NODES ? sk->num : MIN_NODES;
122     ret->data = OPENSSL_malloc(sizeof(*ret->data) * ret->num_alloc);
123     if (ret->data == NULL) {
124         OPENSSL_free(ret);
125         return NULL;
126     }
127     for (i = 0; i < ret->num_alloc; i++)
128         ret->data[i] = NULL;
129
130     for (i = 0; i < ret->num; ++i) {
131         if (sk->data[i] == NULL)
132             continue;
133         if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
134             while (--i >= 0)
135                 if (ret->data[i] != NULL)
136                     free_func(ret->data[i]);
137             sk_free(ret);
138             return NULL;
139         }
140     }
141     return ret;
142 }
143
144 _STACK *sk_new_null(void)
145 {
146     return sk_new((int (*)(const void *, const void *))0);
147 }
148
149 _STACK *sk_new(int (*c) (const void *, const void *))
150 {
151     _STACK *ret;
152
153     if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)
154         goto err;
155     if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)
156         goto err;
157     ret->comp = c;
158     ret->num_alloc = MIN_NODES;
159     return (ret);
160
161  err:
162     OPENSSL_free(ret);
163     return (NULL);
164 }
165
166 int sk_insert(_STACK *st, void *data, int loc)
167 {
168     char **s;
169
170     if (st == NULL)
171         return 0;
172     if (st->num_alloc <= st->num + 1) {
173         s = OPENSSL_realloc((char *)st->data,
174                             (unsigned int)sizeof(char *) * st->num_alloc * 2);
175         if (s == NULL)
176             return (0);
177         st->data = s;
178         st->num_alloc *= 2;
179     }
180     if ((loc >= (int)st->num) || (loc < 0))
181         st->data[st->num] = data;
182     else {
183         memmove(&(st->data[loc + 1]),
184                 &(st->data[loc]), sizeof(char *) * (st->num - loc));
185         st->data[loc] = data;
186     }
187     st->num++;
188     st->sorted = 0;
189     return (st->num);
190 }
191
192 void *sk_delete_ptr(_STACK *st, void *p)
193 {
194     int i;
195
196     for (i = 0; i < st->num; i++)
197         if (st->data[i] == p)
198             return (sk_delete(st, i));
199     return (NULL);
200 }
201
202 void *sk_delete(_STACK *st, int loc)
203 {
204     char *ret;
205     int i, j;
206
207     if (!st || (loc < 0) || (loc >= st->num))
208         return NULL;
209
210     ret = st->data[loc];
211     if (loc != st->num - 1) {
212         j = st->num - 1;
213         for (i = loc; i < j; i++)
214             st->data[i] = st->data[i + 1];
215         /*
216          * In theory memcpy is not safe for this memcpy( &(st->data[loc]),
217          * &(st->data[loc+1]), sizeof(char *)*(st->num-loc-1));
218          */
219     }
220     st->num--;
221     return (ret);
222 }
223
224 static int internal_find(_STACK *st, void *data, int ret_val_options)
225 {
226     const void *const *r;
227     int i;
228
229     if (st == NULL)
230         return -1;
231
232     if (st->comp == NULL) {
233         for (i = 0; i < st->num; i++)
234             if (st->data[i] == data)
235                 return (i);
236         return (-1);
237     }
238     sk_sort(st);
239     if (data == NULL)
240         return (-1);
241     r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
242                         ret_val_options);
243     if (r == NULL)
244         return (-1);
245     return (int)((char **)r - st->data);
246 }
247
248 int sk_find(_STACK *st, void *data)
249 {
250     return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
251 }
252
253 int sk_find_ex(_STACK *st, void *data)
254 {
255     return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
256 }
257
258 int sk_push(_STACK *st, void *data)
259 {
260     return (sk_insert(st, data, st->num));
261 }
262
263 int sk_unshift(_STACK *st, void *data)
264 {
265     return (sk_insert(st, data, 0));
266 }
267
268 void *sk_shift(_STACK *st)
269 {
270     if (st == NULL)
271         return (NULL);
272     if (st->num <= 0)
273         return (NULL);
274     return (sk_delete(st, 0));
275 }
276
277 void *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
286 void sk_zero(_STACK *st)
287 {
288     if (st == NULL)
289         return;
290     if (st->num <= 0)
291         return;
292     memset(st->data, 0, sizeof(*st->data) * st->num);
293     st->num = 0;
294 }
295
296 void sk_pop_free(_STACK *st, void (*func) (void *))
297 {
298     int i;
299
300     if (st == NULL)
301         return;
302     for (i = 0; i < st->num; i++)
303         if (st->data[i] != NULL)
304             func(st->data[i]);
305     sk_free(st);
306 }
307
308 void sk_free(_STACK *st)
309 {
310     if (st == NULL)
311         return;
312     OPENSSL_free(st->data);
313     OPENSSL_free(st);
314 }
315
316 int sk_num(const _STACK *st)
317 {
318     if (st == NULL)
319         return -1;
320     return st->num;
321 }
322
323 void *sk_value(const _STACK *st, int i)
324 {
325     if (!st || (i < 0) || (i >= st->num))
326         return NULL;
327     return st->data[i];
328 }
329
330 void *sk_set(_STACK *st, int i, void *value)
331 {
332     if (!st || (i < 0) || (i >= st->num))
333         return NULL;
334     return (st->data[i] = value);
335 }
336
337 void sk_sort(_STACK *st)
338 {
339     if (st && !st->sorted && st->comp != NULL) {
340         int (*comp_func) (const void *, const void *);
341
342         /*
343          * same comment as in sk_find ... previously st->comp was declared as
344          * a (void*,void*) callback type, but this made the population of the
345          * callback pointer illogical - our callbacks compare type** with
346          * type**, so we leave the casting until absolutely necessary (ie.
347          * "now").
348          */
349         comp_func = (int (*)(const void *, const void *))(st->comp);
350         qsort(st->data, st->num, sizeof(char *), comp_func);
351         st->sorted = 1;
352     }
353 }
354
355 int sk_is_sorted(const _STACK *st)
356 {
357     if (!st)
358         return 1;
359     return st->sorted;
360 }