1DEFINE_STACK_OF(3) OpenSSL DEFINE_STACK_OF(3)
2
3
4
6 DEFINE_STACK_OF, DEFINE_STACK_OF_CONST, DEFINE_SPECIAL_STACK_OF,
7 DEFINE_SPECIAL_STACK_OF_CONST, sk_TYPE_num, sk_TYPE_value, sk_TYPE_new,
8 sk_TYPE_new_null, sk_TYPE_reserve, sk_TYPE_free, sk_TYPE_zero,
9 sk_TYPE_delete, sk_TYPE_delete_ptr, sk_TYPE_push, sk_TYPE_unshift,
10 sk_TYPE_pop, sk_TYPE_shift, sk_TYPE_pop_free, sk_TYPE_insert,
11 sk_TYPE_set, sk_TYPE_find, sk_TYPE_find_ex, sk_TYPE_sort,
12 sk_TYPE_is_sorted, sk_TYPE_dup, sk_TYPE_deep_copy,
13 sk_TYPE_set_cmp_func, sk_TYPE_new_reserve - stack container
14
16 #include <openssl/safestack.h>
17
18 STACK_OF(TYPE)
19 DEFINE_STACK_OF(TYPE)
20 DEFINE_STACK_OF_CONST(TYPE)
21 DEFINE_SPECIAL_STACK_OF(FUNCTYPE, TYPE)
22 DEFINE_SPECIAL_STACK_OF_CONST(FUNCTYPE, TYPE)
23
24 typedef int (*sk_TYPE_compfunc)(const TYPE *const *a, const TYPE *const *b);
25 typedef TYPE * (*sk_TYPE_copyfunc)(const TYPE *a);
26 typedef void (*sk_TYPE_freefunc)(TYPE *a);
27
28 int sk_TYPE_num(const STACK_OF(TYPE) *sk);
29 TYPE *sk_TYPE_value(const STACK_OF(TYPE) *sk, int idx);
30 STACK_OF(TYPE) *sk_TYPE_new(sk_TYPE_compfunc compare);
31 STACK_OF(TYPE) *sk_TYPE_new_null(void);
32 int sk_TYPE_reserve(STACK_OF(TYPE) *sk, int n);
33 void sk_TYPE_free(const STACK_OF(TYPE) *sk);
34 void sk_TYPE_zero(const STACK_OF(TYPE) *sk);
35 TYPE *sk_TYPE_delete(STACK_OF(TYPE) *sk, int i);
36 TYPE *sk_TYPE_delete_ptr(STACK_OF(TYPE) *sk, TYPE *ptr);
37 int sk_TYPE_push(STACK_OF(TYPE) *sk, const TYPE *ptr);
38 int sk_TYPE_unshift(STACK_OF(TYPE) *sk, const TYPE *ptr);
39 TYPE *sk_TYPE_pop(STACK_OF(TYPE) *sk);
40 TYPE *sk_TYPE_shift(STACK_OF(TYPE) *sk);
41 void sk_TYPE_pop_free(STACK_OF(TYPE) *sk, sk_TYPE_freefunc freefunc);
42 int sk_TYPE_insert(STACK_OF(TYPE) *sk, TYPE *ptr, int idx);
43 TYPE *sk_TYPE_set(STACK_OF(TYPE) *sk, int idx, const TYPE *ptr);
44 int sk_TYPE_find(STACK_OF(TYPE) *sk, TYPE *ptr);
45 int sk_TYPE_find_ex(STACK_OF(TYPE) *sk, TYPE *ptr);
46 void sk_TYPE_sort(const STACK_OF(TYPE) *sk);
47 int sk_TYPE_is_sorted(const STACK_OF(TYPE) *sk);
48 STACK_OF(TYPE) *sk_TYPE_dup(const STACK_OF(TYPE) *sk);
49 STACK_OF(TYPE) *sk_TYPE_deep_copy(const STACK_OF(TYPE) *sk,
50 sk_TYPE_copyfunc copyfunc,
51 sk_TYPE_freefunc freefunc);
52 sk_TYPE_compfunc (*sk_TYPE_set_cmp_func(STACK_OF(TYPE) *sk,
53 sk_TYPE_compfunc compare));
54 STACK_OF(TYPE) *sk_TYPE_new_reserve(sk_TYPE_compfunc compare, int n);
55
57 Applications can create and use their own stacks by placing any of the
58 macros described below in a header file. These macros define typesafe
59 inline functions that wrap around the utility OPENSSL_sk_ API. In the
60 description here, TYPE is used as a placeholder for any of the OpenSSL
61 datatypes, such as X509.
62
63 STACK_OF() returns the name for a stack of the specified TYPE.
64 DEFINE_STACK_OF() creates set of functions for a stack of TYPE. This
65 will mean that type TYPE is stored in each stack, the type is
66 referenced by STACK_OF(TYPE) and each function name begins with
67 sk_TYPE_. For example:
68
69 TYPE *sk_TYPE_value(STACK_OF(TYPE) *sk, int idx);
70
71 DEFINE_STACK_OF_CONST() is identical to DEFINE_STACK_OF() except each
72 element is constant. For example:
73
74 const TYPE *sk_TYPE_value(STACK_OF(TYPE) *sk, int idx);
75
76 DEFINE_SPECIAL_STACK_OF() defines a stack of TYPE but each function
77 uses FUNCNAME in the function name. For example:
78
79 TYPE *sk_FUNCNAME_value(STACK_OF(TYPE) *sk, int idx);
80
81 DEFINE_SPECIAL_STACK_OF_CONST() is similar except that each element is
82 constant:
83
84 const TYPE *sk_FUNCNAME_value(STACK_OF(TYPE) *sk, int idx);
85
86 sk_TYPE_num() returns the number of elements in sk or -1 if sk is NULL.
87
88 sk_TYPE_value() returns element idx in sk, where idx starts at zero. If
89 idx is out of range then NULL is returned.
90
91 sk_TYPE_new() allocates a new empty stack using comparison function
92 compare. If compare is NULL then no comparison function is used. This
93 function is equivalent to sk_TYPE_new_reserve(compare, 0).
94
95 sk_TYPE_new_null() allocates a new empty stack with no comparison
96 function. This function is equivalent to sk_TYPE_new_reserve(NULL, 0).
97
98 sk_TYPE_reserve() allocates additional memory in the sk structure such
99 that the next n calls to sk_TYPE_insert(), sk_TYPE_push() or
100 sk_TYPE_unshift() will not fail or cause memory to be allocated or
101 reallocated. If n is zero, any excess space allocated in the sk
102 structure is freed. On error sk is unchanged.
103
104 sk_TYPE_new_reserve() allocates a new stack. The new stack will have
105 additional memory allocated to hold n elements if n is positive. The
106 next n calls to sk_TYPE_insert(), sk_TYPE_push() or sk_TYPE_unshift()
107 will not fail or cause memory to be allocated or reallocated. If n is
108 zero or less than zero, no memory is allocated. sk_TYPE_new_reserve()
109 also sets the comparison function compare to the newly created stack.
110 If compare is NULL then no comparison function is used.
111
112 sk_TYPE_set_cmp_func() sets the comparison function of sk to compare.
113 The previous comparison function is returned or NULL if there was no
114 previous comparison function.
115
116 sk_TYPE_free() frees up the sk structure. It does not free up any
117 elements of sk. After this call sk is no longer valid.
118
119 sk_TYPE_zero() sets the number of elements in sk to zero. It does not
120 free sk so after this call sk is still valid.
121
122 sk_TYPE_pop_free() frees up all elements of sk and sk itself. The free
123 function freefunc() is called on each element to free it.
124
125 sk_TYPE_delete() deletes element i from sk. It returns the deleted
126 element or NULL if i is out of range.
127
128 sk_TYPE_delete_ptr() deletes element matching ptr from sk. It returns
129 the deleted element or NULL if no element matching ptr was found.
130
131 sk_TYPE_insert() inserts ptr into sk at position idx. Any existing
132 elements at or after idx are moved downwards. If idx is out of range
133 the new element is appended to sk. sk_TYPE_insert() either returns the
134 number of elements in sk after the new element is inserted or zero if
135 an error (such as memory allocation failure) occurred.
136
137 sk_TYPE_push() appends ptr to sk it is equivalent to:
138
139 sk_TYPE_insert(sk, ptr, -1);
140
141 sk_TYPE_unshift() inserts ptr at the start of sk it is equivalent to:
142
143 sk_TYPE_insert(sk, ptr, 0);
144
145 sk_TYPE_pop() returns and removes the last element from sk.
146
147 sk_TYPE_shift() returns and removes the first element from sk.
148
149 sk_TYPE_set() sets element idx of sk to ptr replacing the current
150 element. The new element value is returned or NULL if an error
151 occurred: this will only happen if sk is NULL or idx is out of range.
152
153 sk_TYPE_find() searches sk for the element ptr. In the case where no
154 comparison function has been specified, the function performs a linear
155 search for a pointer equal to ptr. The index of the first matching
156 element is returned or -1 if there is no match. In the case where a
157 comparison function has been specified, sk is sorted then
158 sk_TYPE_find() returns the index of a matching element or -1 if there
159 is no match. Note that, in this case, the matching element returned is
160 not guaranteed to be the first; the comparison function will usually
161 compare the values pointed to rather than the pointers themselves and
162 the order of elements in sk could change.
163
164 sk_TYPE_find_ex() operates like sk_TYPE_find() except when a comparison
165 function has been specified and no matching element is found. Instead
166 of returning -1, sk_TYPE_find_ex() returns the index of the element
167 either before or after the location where ptr would be if it were
168 present in sk.
169
170 sk_TYPE_sort() sorts sk using the supplied comparison function.
171
172 sk_TYPE_is_sorted() returns 1 if sk is sorted and 0 otherwise.
173
174 sk_TYPE_dup() returns a copy of sk. Note the pointers in the copy are
175 identical to the original.
176
177 sk_TYPE_deep_copy() returns a new stack where each element has been
178 copied. Copying is performed by the supplied copyfunc() and freeing by
179 freefunc(). The function freefunc() is only called if an error occurs.
180
182 Care should be taken when accessing stacks in multi-threaded
183 environments. Any operation which increases the size of a stack such
184 as sk_TYPE_insert() or sk_push() can "grow" the size of an internal
185 array and cause race conditions if the same stack is accessed in a
186 different thread. Operations such as sk_find() and sk_sort() can also
187 reorder the stack.
188
189 Any comparison function supplied should use a metric suitable for use
190 in a binary search operation. That is it should return zero, a positive
191 or negative value if a is equal to, greater than or less than b
192 respectively.
193
194 Care should be taken when checking the return values of the functions
195 sk_TYPE_find() and sk_TYPE_find_ex(). They return an index to the
196 matching element. In particular 0 indicates a matching first element.
197 A failed search is indicated by a -1 return value.
198
199 STACK_OF(), DEFINE_STACK_OF(), DEFINE_STACK_OF_CONST(), and
200 DEFINE_SPECIAL_STACK_OF() are implemented as macros.
201
202 The underlying utility OPENSSL_sk_ API should not be used directly. It
203 defines these functions: OPENSSL_sk_deep_copy(), OPENSSL_sk_delete(),
204 OPENSSL_sk_delete_ptr(), OPENSSL_sk_dup(), OPENSSL_sk_find(),
205 OPENSSL_sk_find_ex(), OPENSSL_sk_free(), OPENSSL_sk_insert(),
206 OPENSSL_sk_is_sorted(), OPENSSL_sk_new(), OPENSSL_sk_new_null(),
207 OPENSSL_sk_num(), OPENSSL_sk_pop(), OPENSSL_sk_pop_free(),
208 OPENSSL_sk_push(), OPENSSL_sk_reserve(), OPENSSL_sk_set(),
209 OPENSSL_sk_set_cmp_func(), OPENSSL_sk_shift(), OPENSSL_sk_sort(),
210 OPENSSL_sk_unshift(), OPENSSL_sk_value(), OPENSSL_sk_zero().
211
213 sk_TYPE_num() returns the number of elements in the stack or -1 if the
214 passed stack is NULL.
215
216 sk_TYPE_value() returns a pointer to a stack element or NULL if the
217 index is out of range.
218
219 sk_TYPE_new(), sk_TYPE_new_null() and sk_TYPE_new_reserve() return an
220 empty stack or NULL if an error occurs.
221
222 sk_TYPE_reserve() returns 1 on successful allocation of the required
223 memory or 0 on error.
224
225 sk_TYPE_set_cmp_func() returns the old comparison function or NULL if
226 there was no old comparison function.
227
228 sk_TYPE_free(), sk_TYPE_zero(), sk_TYPE_pop_free() and sk_TYPE_sort()
229 do not return values.
230
231 sk_TYPE_pop(), sk_TYPE_shift(), sk_TYPE_delete() and
232 sk_TYPE_delete_ptr() return a pointer to the deleted element or NULL on
233 error.
234
235 sk_TYPE_insert(), sk_TYPE_push() and sk_TYPE_unshift() return the total
236 number of elements in the stack and 0 if an error occurred.
237
238 sk_TYPE_set() returns a pointer to the replacement element or NULL on
239 error.
240
241 sk_TYPE_find() and sk_TYPE_find_ex() return an index to the found
242 element or -1 on error.
243
244 sk_TYPE_is_sorted() returns 1 if the stack is sorted and 0 if it is
245 not.
246
247 sk_TYPE_dup() and sk_TYPE_deep_copy() return a pointer to the copy of
248 the stack.
249
251 Before OpenSSL 1.1.0, this was implemented via macros and not inline
252 functions and was not a public API.
253
254 sk_TYPE_reserve() and sk_TYPE_new_reserve() were added in OpenSSL
255 1.1.1.
256
258 Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
259
260 Licensed under the OpenSSL license (the "License"). You may not use
261 this file except in compliance with the License. You can obtain a copy
262 in the file LICENSE in the source distribution or at
263 <https://www.openssl.org/source/license.html>.
264
265
266
2671.1.1g 2020-04-23 DEFINE_STACK_OF(3)