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