Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/stack/stack.c
Line
Count
Source (jump to first uncovered line)
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
22.8M
#define MIN_NODES       4
76
77
const char STACK_version[] = "Stack" OPENSSL_VERSION_PTEXT;
78
79
#include <errno.h>
80
81
int (*sk_set_cmp_func(_STACK *sk, int (*c) (const void *, const void *)))
82
2.48k
 (const void *, const void *) {
83
2.48k
    int (*old) (const void *, const void *) = sk->comp;
84
85
2.48k
    if (sk->comp != c)
86
2.48k
        sk->sorted = 0;
87
2.48k
    sk->comp = c;
88
89
2.48k
    return old;
90
2.48k
}
91
92
_STACK *sk_dup(_STACK *sk)
93
2.48k
{
94
2.48k
    _STACK *ret;
95
2.48k
    char **s;
96
97
2.48k
    if ((ret = sk_new(sk->comp)) == NULL)
98
0
        goto err;
99
2.48k
    s = (char **)OPENSSL_realloc((char *)ret->data,
100
2.48k
                                 (unsigned int)sizeof(char *) *
101
2.48k
                                 sk->num_alloc);
102
2.48k
    if (s == NULL)
103
0
        goto err;
104
2.48k
    ret->data = s;
105
106
2.48k
    ret->num = sk->num;
107
2.48k
    memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
108
2.48k
    ret->sorted = sk->sorted;
109
2.48k
    ret->num_alloc = sk->num_alloc;
110
2.48k
    ret->comp = sk->comp;
111
2.48k
    return (ret);
112
0
 err:
113
0
    if (ret)
114
0
        sk_free(ret);
115
0
    return (NULL);
116
2.48k
}
117
118
_STACK *sk_deep_copy(_STACK *sk, void *(*copy_func) (void *),
119
                     void (*free_func) (void *))
120
0
{
121
0
    _STACK *ret;
122
0
    int i;
123
124
0
    if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
125
0
        return ret;
126
0
    ret->comp = sk->comp;
127
0
    ret->sorted = sk->sorted;
128
0
    ret->num = sk->num;
129
0
    ret->num_alloc = sk->num > MIN_NODES ? sk->num : MIN_NODES;
130
0
    ret->data = OPENSSL_malloc(sizeof(char *) * ret->num_alloc);
131
0
    if (ret->data == NULL) {
132
0
        OPENSSL_free(ret);
133
0
        return NULL;
134
0
    }
135
0
    for (i = 0; i < ret->num_alloc; i++)
136
0
        ret->data[i] = NULL;
137
138
0
    for (i = 0; i < ret->num; ++i) {
139
0
        if (sk->data[i] == NULL)
140
0
            continue;
141
0
        if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
142
0
            while (--i >= 0)
143
0
                if (ret->data[i] != NULL)
144
0
                    free_func(ret->data[i]);
145
0
            sk_free(ret);
146
0
            return NULL;
147
0
        }
148
0
    }
149
0
    return ret;
150
0
}
151
152
_STACK *sk_new_null(void)
153
3.80M
{
154
3.80M
    return sk_new((int (*)(const void *, const void *))0);
155
3.80M
}
156
157
_STACK *sk_new(int (*c) (const void *, const void *))
158
3.80M
{
159
3.80M
    _STACK *ret;
160
3.80M
    int i;
161
162
3.80M
    if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
163
0
        goto err;
164
3.80M
    if ((ret->data = OPENSSL_malloc(sizeof(char *) * MIN_NODES)) == NULL)
165
0
        goto err;
166
19.0M
    for (i = 0; i < MIN_NODES; i++)
167
15.2M
        ret->data[i] = NULL;
168
3.80M
    ret->comp = c;
169
3.80M
    ret->num_alloc = MIN_NODES;
170
3.80M
    ret->num = 0;
171
3.80M
    ret->sorted = 0;
172
3.80M
    return (ret);
173
0
 err:
174
0
    if (ret)
175
0
        OPENSSL_free(ret);
176
0
    return (NULL);
177
3.80M
}
178
179
int sk_insert(_STACK *st, void *data, int loc)
180
7.05M
{
181
7.05M
    char **s;
182
183
7.05M
    if (st == NULL)
184
0
        return 0;
185
7.05M
    if (st->num_alloc <= st->num + 1) {
186
561k
        s = OPENSSL_realloc((char *)st->data,
187
561k
                            (unsigned int)sizeof(char *) * st->num_alloc * 2);
188
561k
        if (s == NULL)
189
0
            return (0);
190
561k
        st->data = s;
191
561k
        st->num_alloc *= 2;
192
561k
    }
193
7.05M
    if ((loc >= (int)st->num) || (loc < 0))
194
7.05M
        st->data[st->num] = data;
195
133
    else {
196
133
        int i;
197
133
        char **f, **t;
198
199
133
        f = st->data;
200
133
        t = &(st->data[1]);
201
798
        for (i = st->num; i >= loc; i--)
202
665
            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
133
        st->data[loc] = data;
209
133
    }
210
7.05M
    st->num++;
211
7.05M
    st->sorted = 0;
212
7.05M
    return (st->num);
213
7.05M
}
214
215
void *sk_delete_ptr(_STACK *st, void *p)
216
741
{
217
741
    int i;
218
219
2.07k
    for (i = 0; i < st->num; i++)
220
1.46k
        if (st->data[i] == p)
221
133
            return (sk_delete(st, i));
222
608
    return (NULL);
223
741
}
224
225
void *sk_delete(_STACK *st, int loc)
226
133
{
227
133
    char *ret;
228
133
    int i, j;
229
230
133
    if (!st || (loc < 0) || (loc >= st->num))
231
0
        return NULL;
232
233
133
    ret = st->data[loc];
234
133
    if (loc != st->num - 1) {
235
0
        j = st->num - 1;
236
0
        for (i = loc; i < j; i++)
237
0
            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
0
    }
243
133
    st->num--;
244
133
    return (ret);
245
133
}
246
247
static int internal_find(_STACK *st, void *data, int ret_val_options)
248
157k
{
249
157k
    const void *const *r;
250
157k
    int i;
251
252
157k
    if (st == NULL)
253
0
        return -1;
254
255
157k
    if (st->comp == NULL) {
256
0
        for (i = 0; i < st->num; i++)
257
0
            if (st->data[i] == data)
258
0
                return (i);
259
0
        return (-1);
260
0
    }
261
157k
    sk_sort(st);
262
157k
    if (data == NULL)
263
0
        return (-1);
264
157k
    r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
265
157k
                        ret_val_options);
266
157k
    if (r == NULL)
267
157k
        return (-1);
268
0
    return (int)((char **)r - st->data);
269
157k
}
270
271
int sk_find(_STACK *st, void *data)
272
157k
{
273
157k
    return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
274
157k
}
275
276
int sk_find_ex(_STACK *st, void *data)
277
0
{
278
0
    return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
279
0
}
280
281
int sk_push(_STACK *st, void *data)
282
7.05M
{
283
7.05M
    return (sk_insert(st, data, st->num));
284
7.05M
}
285
286
int sk_unshift(_STACK *st, void *data)
287
0
{
288
0
    return (sk_insert(st, data, 0));
289
0
}
290
291
void *sk_shift(_STACK *st)
292
0
{
293
0
    if (st == NULL)
294
0
        return (NULL);
295
0
    if (st->num <= 0)
296
0
        return (NULL);
297
0
    return (sk_delete(st, 0));
298
0
}
299
300
void *sk_pop(_STACK *st)
301
0
{
302
0
    if (st == NULL)
303
0
        return (NULL);
304
0
    if (st->num <= 0)
305
0
        return (NULL);
306
0
    return (sk_delete(st, st->num - 1));
307
0
}
308
309
void sk_zero(_STACK *st)
310
0
{
311
0
    if (st == NULL)
312
0
        return;
313
0
    if (st->num <= 0)
314
0
        return;
315
0
    memset((char *)st->data, 0, sizeof(*st->data) * st->num);
316
0
    st->num = 0;
317
0
}
318
319
void sk_pop_free(_STACK *st, void (*func) (void *))
320
2.45M
{
321
2.45M
    int i;
322
323
2.45M
    if (st == NULL)
324
0
        return;
325
7.52M
    for (i = 0; i < st->num; i++)
326
5.06M
        if (st->data[i] != NULL)
327
5.06M
            func(st->data[i]);
328
2.45M
    sk_free(st);
329
2.45M
}
330
331
void sk_free(_STACK *st)
332
4.28M
{
333
4.28M
    if (st == NULL)
334
478k
        return;
335
3.80M
    if (st->data != NULL)
336
3.80M
        OPENSSL_free(st->data);
337
3.80M
    OPENSSL_free(st);
338
3.80M
}
339
340
int sk_num(const _STACK *st)
341
19.3M
{
342
19.3M
    if (st == NULL)
343
315k
        return -1;
344
18.9M
    return st->num;
345
19.3M
}
346
347
void *sk_value(const _STACK *st, int i)
348
10.8M
{
349
10.8M
    if (!st || (i < 0) || (i >= st->num))
350
18
        return NULL;
351
10.8M
    return st->data[i];
352
10.8M
}
353
354
void *sk_set(_STACK *st, int i, void *value)
355
1.18M
{
356
1.18M
    if (!st || (i < 0) || (i >= st->num))
357
0
        return NULL;
358
1.18M
    return (st->data[i] = value);
359
1.18M
}
360
361
void sk_sort(_STACK *st)
362
160k
{
363
160k
    if (st && !st->sorted && st->comp != NULL) {
364
160k
        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
160k
        comp_func = (int (*)(const void *, const void *))(st->comp);
374
160k
        qsort(st->data, st->num, sizeof(char *), comp_func);
375
160k
        st->sorted = 1;
376
160k
    }
377
160k
}
378
379
int sk_is_sorted(const _STACK *st)
380
0
{
381
0
    if (!st)
382
0
        return 1;
383
0
    return st->sorted;
384
0
}