2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/stack.h>
13 #include <openssl/objects.h>
20 int (*comp) (const void *, const void *);
28 int (*sk_set_cmp_func(_STACK *sk, int (*c) (const void *, const void *)))
29 (const void *, const void *) {
30 int (*old) (const void *, const void *) = sk->comp;
39 _STACK *sk_dup(_STACK *sk)
44 if ((ret = sk_new(sk->comp)) == NULL)
46 s = OPENSSL_realloc((char *)ret->data,
47 (unsigned int)sizeof(char *) * sk->num_alloc);
53 memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
54 ret->sorted = sk->sorted;
55 ret->num_alloc = sk->num_alloc;
63 _STACK *sk_deep_copy(_STACK *sk, void *(*copy_func) (void *),
64 void (*free_func) (void *))
69 if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
72 ret->sorted = sk->sorted;
74 ret->num_alloc = sk->num > MIN_NODES ? sk->num : MIN_NODES;
75 ret->data = OPENSSL_malloc(sizeof(*ret->data) * ret->num_alloc);
76 if (ret->data == NULL) {
80 for (i = 0; i < ret->num_alloc; i++)
83 for (i = 0; i < ret->num; ++i) {
84 if (sk->data[i] == NULL)
86 if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
88 if (ret->data[i] != NULL)
89 free_func(ret->data[i]);
97 _STACK *sk_new_null(void)
99 return sk_new((int (*)(const void *, const void *))0);
102 _STACK *sk_new(int (*c) (const void *, const void *))
106 if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)
108 if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)
111 ret->num_alloc = MIN_NODES;
119 int sk_insert(_STACK *st, void *data, int loc)
125 if (st->num_alloc <= st->num + 1) {
126 s = OPENSSL_realloc((char *)st->data,
127 (unsigned int)sizeof(char *) * st->num_alloc * 2);
133 if ((loc >= (int)st->num) || (loc < 0))
134 st->data[st->num] = data;
136 memmove(&(st->data[loc + 1]),
137 &(st->data[loc]), sizeof(char *) * (st->num - loc));
138 st->data[loc] = data;
145 void *sk_delete_ptr(_STACK *st, void *p)
149 for (i = 0; i < st->num; i++)
150 if (st->data[i] == p)
151 return (sk_delete(st, i));
155 void *sk_delete(_STACK *st, int loc)
160 if (!st || (loc < 0) || (loc >= st->num))
164 if (loc != st->num - 1) {
166 for (i = loc; i < j; i++)
167 st->data[i] = st->data[i + 1];
169 * In theory memcpy is not safe for this memcpy( &(st->data[loc]),
170 * &(st->data[loc+1]), sizeof(char *)*(st->num-loc-1));
177 static int internal_find(_STACK *st, void *data, int ret_val_options)
179 const void *const *r;
185 if (st->comp == NULL) {
186 for (i = 0; i < st->num; i++)
187 if (st->data[i] == data)
194 r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
198 return (int)((char **)r - st->data);
201 int sk_find(_STACK *st, void *data)
203 return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
206 int sk_find_ex(_STACK *st, void *data)
208 return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
211 int sk_push(_STACK *st, void *data)
213 return (sk_insert(st, data, st->num));
216 int sk_unshift(_STACK *st, void *data)
218 return (sk_insert(st, data, 0));
221 void *sk_shift(_STACK *st)
227 return (sk_delete(st, 0));
230 void *sk_pop(_STACK *st)
236 return (sk_delete(st, st->num - 1));
239 void sk_zero(_STACK *st)
245 memset(st->data, 0, sizeof(*st->data) * st->num);
249 void sk_pop_free(_STACK *st, void (*func) (void *))
255 for (i = 0; i < st->num; i++)
256 if (st->data[i] != NULL)
261 void sk_free(_STACK *st)
265 OPENSSL_free(st->data);
269 int sk_num(const _STACK *st)
276 void *sk_value(const _STACK *st, int i)
278 if (!st || (i < 0) || (i >= st->num))
283 void *sk_set(_STACK *st, int i, void *value)
285 if (!st || (i < 0) || (i >= st->num))
287 return (st->data[i] = value);
290 void sk_sort(_STACK *st)
292 if (st && !st->sorted && st->comp != NULL) {
293 int (*comp_func) (const void *, const void *);
296 * same comment as in sk_find ... previously st->comp was declared as
297 * a (void*,void*) callback type, but this made the population of the
298 * callback pointer illogical - our callbacks compare type** with
299 * type**, so we leave the casting until absolutely necessary (ie.
302 comp_func = (int (*)(const void *, const void *))(st->comp);
303 qsort(st->data, st->num, sizeof(char *), comp_func);
308 int sk_is_sorted(const _STACK *st)