Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/stack.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_STACK_H
5 #define OPENSSL_HEADER_STACK_H
6
7 #include <limits.h>
8
9 #include <openssl/base.h>
10
11 #include <openssl/type_check.h>
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
17
18 // A stack, in OpenSSL, is an array of pointers. They are the most commonly
19 // used collection object.
20 //
21 // This file defines macros for type-safe use of the stack functions. A stack
22 // type is named like |STACK_OF(FOO)| and is accessed with functions named
23 // like |sk_FOO_*|. Note the stack will typically contain /pointers/ to |FOO|.
24 //
25 // The |DECLARE_STACK_OF| macro makes |STACK_OF(FOO)| available, and
26 // |DEFINE_STACK_OF| makes the corresponding functions available.
27
28
29 // Defining stacks.
30
31 // STACK_OF expands to the stack type for |type|.
32 #define STACK_OF(type) struct stack_st_##type
33
34 // DECLARE_STACK_OF declares the |STACK_OF(type)| type. It does not make the
35 // corresponding |sk_type_*| functions available. This macro should be used in
36 // files which only need the type.
37 #define DECLARE_STACK_OF(type) STACK_OF(type);
38
39 // DEFINE_NAMED_STACK_OF defines |STACK_OF(name)| to be a stack whose elements
40 // are |type| *. This macro makes the |sk_name_*| functions available.
41 //
42 // It is not necessary to use |DECLARE_STACK_OF| in files which use this macro.
43 #define DEFINE_NAMED_STACK_OF(name, type) \
44 BORINGSSL_DEFINE_STACK_OF_IMPL(name, type *, const type *) \
45 BORINGSSL_DEFINE_STACK_TRAITS(name, type, false)
46
47 // DEFINE_STACK_OF defines |STACK_OF(type)| to be a stack whose elements are
48 // |type| *. This macro makes the |sk_type_*| functions available.
49 //
50 // It is not necessary to use |DECLARE_STACK_OF| in files which use this macro.
51 #define DEFINE_STACK_OF(type) DEFINE_NAMED_STACK_OF(type, type)
52
53 // DEFINE_CONST_STACK_OF defines |STACK_OF(type)| to be a stack whose elements
54 // are const |type| *. This macro makes the |sk_type_*| functions available.
55 //
56 // It is not necessary to use |DECLARE_STACK_OF| in files which use this macro.
57 #define DEFINE_CONST_STACK_OF(type) \
58 BORINGSSL_DEFINE_STACK_OF_IMPL(type, const type *, const type *) \
59 BORINGSSL_DEFINE_STACK_TRAITS(type, const type, true)
60
61
62 // Using stacks.
63 //
64 // After the |DEFINE_STACK_OF| macro is used, the following functions are
65 // available.
66
67 #if 0 // Sample
68
69 // sk_SAMPLE_free_func is a callback to free an element in a stack.
70 typedef void (*sk_SAMPLE_free_func)(SAMPLE *);
71
72 // sk_SAMPLE_copy_func is a callback to copy an element in a stack. It should
73 // return the copy or NULL on error.
74 typedef SAMPLE *(*sk_SAMPLE_copy_func)(const SAMPLE *);
75
76 // sk_SAMPLE_cmp_func is a callback to compare |*a| to |*b|. It should return a
77 // value < 0, 0, or > 0 if |*a| is less than, equal to, or greater than |*b|,
78 // respectively. Note the extra indirection - the function is given a pointer
79 // to a pointer to the element. This is the |qsort|/|bsearch| comparison
80 // function applied to an array of |SAMPLE*|.
81 typedef int (*sk_SAMPLE_cmp_func)(const SAMPLE *const *a,
82 const SAMPLE *const *b);
83
84 // sk_SAMPLE_new creates a new, empty stack with the given comparison function,
85 // which may be NULL. It returns the new stack or NULL on allocation failure.
86 STACK_OF(SAMPLE) *sk_SAMPLE_new(sk_SAMPLE_cmp_func comp);
87
88 // sk_SAMPLE_new_null creates a new, empty stack. It returns the new stack or
89 // NULL on allocation failure.
90 STACK_OF(SAMPLE) *sk_SAMPLE_new_null(void);
91
92 // sk_SAMPLE_num returns the number of elements in |sk|. It is safe to cast this
93 // value to |int|. |sk| is guaranteed to have at most |INT_MAX| elements. If
94 // |sk| is NULL, it is treated as the empty list and this function returns zero.
95 size_t sk_SAMPLE_num(const STACK_OF(SAMPLE) *sk);
96
97 // sk_SAMPLE_zero resets |sk| to the empty state but does nothing to free the
98 // individual elements themselves.
99 void sk_SAMPLE_zero(STACK_OF(SAMPLE) *sk);
100
101 // sk_SAMPLE_value returns the |i|th pointer in |sk|, or NULL if |i| is out of
102 // range. If |sk| is NULL, it is treated as an empty list and the function
103 // returns NULL.
104 SAMPLE *sk_SAMPLE_value(const STACK_OF(SAMPLE) *sk, size_t i);
105
106 // sk_SAMPLE_set sets the |i|th pointer in |sk| to |p| and returns |p|. If |i|
107 // is out of range, it returns NULL.
108 SAMPLE *sk_SAMPLE_set(STACK_OF(SAMPLE) *sk, size_t i, SAMPLE *p);
109
110 // sk_SAMPLE_free frees |sk|, but does nothing to free the individual elements.
111 // Use |sk_SAMPLE_pop_free| to also free the elements.
112 void sk_SAMPLE_free(STACK_OF(SAMPLE) *sk);
113
114 // sk_SAMPLE_pop_free calls |free_func| on each element in |sk| and then
115 // frees the stack itself.
116 void sk_SAMPLE_pop_free(STACK_OF(SAMPLE) *sk, sk_SAMPLE_free_func free_func);
117
118 // sk_SAMPLE_insert inserts |p| into the stack at index |where|, moving existing
119 // elements if needed. It returns the length of the new stack, or zero on
120 // error. Ownership of |p| is taken by |sk|.
121 size_t sk_SAMPLE_insert(STACK_OF(SAMPLE) *sk, SAMPLE *p, size_t where);
122
123 // sk_SAMPLE_delete removes the pointer at index |where|, moving other elements
124 // down if needed. It returns the removed pointer, or NULL if |where| is out of
125 // range.
126 SAMPLE *sk_SAMPLE_delete(STACK_OF(SAMPLE) *sk, size_t where);
127
128 // sk_SAMPLE_delete_ptr removes, at most, one instance of |p| from |sk| based on
129 // pointer equality. If an instance of |p| is found then |p| is returned,
130 // otherwise it returns NULL.
131 SAMPLE *sk_SAMPLE_delete_ptr(STACK_OF(SAMPLE) *sk, const SAMPLE *p);
132
133 // sk_SAMPLE_delete_if_func is the callback function for |sk_SAMPLE_delete_if|.
134 // It should return one to remove |p| and zero to keep it.
135 typedef int (*sk_SAMPLE_delete_if_func)(SAMPLE *p, void *data);
136
137 // sk_SAMPLE_delete_if calls |func| with each element of |sk| and removes the
138 // entries where |func| returned one. This function does not free or return
139 // removed pointers so, if |sk| owns its contents, |func| should release the
140 // pointers prior to returning one.
141 void sk_SAMPLE_delete_if(STACK_OF(SAMPLE) *sk, sk_SAMPLE_delete_if_func func,
142 void *data);
143
144 // sk_SAMPLE_find find the first value in |sk| equal to |p|. |sk|'s comparison
145 // function determines equality, or pointer equality if |sk| has no comparison
146 // function.
147 //
148 // If the stack is sorted (see |sk_SAMPLE_sort|), this function uses a binary
149 // search. Otherwise it performs a linear search. If it finds a matching
150 // element, it returns the index of that element. Otherwise, it returns -1.
151 // If |sk| is NULL, it is treated as the empty list and the function returns
152 // zero.
153 //
154 // Note this differs from OpenSSL in that OpenSSL's version will implicitly
155 // sort |sk| if it has a comparison function defined.
156 int sk_SAMPLE_find(const STACK_OF(SAMPLE) *sk, const SAMPLE *p);
157
158 // sk_SAMPLE_find_awslc is like |sk_SAMPLE_find|, but if it finds a matching
159 // element, it writes the index to |*out_index| (if |out_index| is not NULL)
160 // and returns one. Otherwise, it returns zero.
161 int sk_SAMPLE_find_awslc(const STACK_OF(SAMPLE) *sk, size_t *out_index,
162 const SAMPLE *p);
163
164 // sk_SAMPLE_unshift inserts |p| as the first element of |sk| and takes
165 // ownership of |p|. It is equivalent to "sk_SAMPLE_insert(sk, p, 0)".
166 SAMPLE *sk_SAMPLE_unshift(STACK_OF(SAMPLE) *sk, SAMPLE *p);
167
168 // sk_SAMPLE_shift removes and returns the first element in |sk|, or NULL if
169 // |sk| is empty.
170 SAMPLE *sk_SAMPLE_shift(STACK_OF(SAMPLE) *sk);
171
172 // sk_SAMPLE_push appends |p| to |sk| and returns the length of the new stack,
173 // or 0 on allocation failure.
174 size_t sk_SAMPLE_push(STACK_OF(SAMPLE) *sk, SAMPLE *p);
175
176 // sk_SAMPLE_pop removes and returns the last element of |sk|, or NULL if |sk|
177 // is empty.
178 SAMPLE *sk_SAMPLE_pop(STACK_OF(SAMPLE) *sk);
179
180 // sk_SAMPLE_dup performs a shallow copy of a stack and returns the new stack,
181 // or NULL on error. Use |sk_SAMPLE_deep_copy| to also copy the elements.
182 STACK_OF(SAMPLE) *sk_SAMPLE_dup(const STACK_OF(SAMPLE) *sk);
183
184 // sk_SAMPLE_sort sorts the elements of |sk| into ascending order based on the
185 // comparison function. The stack maintains a "sorted" flag and sorting an
186 // already sorted stack is a no-op.
187 void sk_SAMPLE_sort(STACK_OF(SAMPLE) *sk);
188
189 // sk_SAMPLE_is_sorted returns one if |sk| is known to be sorted and zero
190 // otherwise.
191 int sk_SAMPLE_is_sorted(const STACK_OF(SAMPLE) *sk);
192
193 // sk_SAMPLE_set_cmp_func sets the comparison function to be used by |sk| and
194 // returns the previous one.
195 sk_SAMPLE_cmp_func sk_SAMPLE_set_cmp_func(STACK_OF(SAMPLE) *sk,
196 sk_SAMPLE_cmp_func comp);
197
198 // sk_SAMPLE_deep_copy performs a copy of |sk| and of each of the non-NULL
199 // elements in |sk| by using |copy_func|. If an error occurs, it calls
200 // |free_func| to free any copies already made and returns NULL.
201 STACK_OF(SAMPLE) *sk_SAMPLE_deep_copy(const STACK_OF(SAMPLE) *sk,
202 sk_SAMPLE_copy_func copy_func,
203 sk_SAMPLE_free_func free_func);
204
205 #endif // Sample
206
207
208 // Private functions.
209 //
210 // The |sk_*| functions generated above are implemented internally using the
211 // type-erased functions below. Callers should use the typed wrappers instead.
212 // When using the type-erased functions, callers are responsible for ensuring
213 // the underlying types are correct. Casting pointers to the wrong types will
214 // result in memory errors.
215
216 // OPENSSL_sk_free_func is a function that frees an element in a stack. Note its
217 // actual type is void (*)(T *) for some T. Low-level |sk_*| functions will be
218 // passed a type-specific wrapper to call it correctly.
219 typedef void (*OPENSSL_sk_free_func)(void *ptr);
220
221 // OPENSSL_sk_copy_func is a function that copies an element in a stack. Note
222 // its actual type is T *(*)(const T *) for some T. Low-level |sk_*| functions
223 // will be passed a type-specific wrapper to call it correctly.
224 typedef void *(*OPENSSL_sk_copy_func)(const void *ptr);
225
226 // OPENSSL_sk_cmp_func is a comparison function that returns a value < 0, 0 or >
227 // 0 if |*a| is less than, equal to or greater than |*b|, respectively. Note
228 // the extra indirection - the function is given a pointer to a pointer to the
229 // element. This differs from the usual qsort/bsearch comparison function.
230 //
231 // Note its actual type is |int (*)(const T *const *a, const T *const *b)|.
232 // Low-level |sk_*| functions will be passed a type-specific wrapper to call it
233 // correctly.
234 typedef int (*OPENSSL_sk_cmp_func)(const void *const *a, const void *const *b);
235
236 // OPENSSL_sk_delete_if_func is the generic version of
237 // |sk_SAMPLE_delete_if_func|.
238 typedef int (*OPENSSL_sk_delete_if_func)(void *obj, void *data);
239
240 // The following function types call the above type-erased signatures with the
241 // true types.
242 typedef void (*OPENSSL_sk_call_free_func)(OPENSSL_sk_free_func, void *);
243 typedef void *(*OPENSSL_sk_call_copy_func)(OPENSSL_sk_copy_func, const void *);
244 typedef int (*OPENSSL_sk_call_cmp_func)(OPENSSL_sk_cmp_func, const void *,
245 const void *);
246 typedef int (*OPENSSL_sk_call_delete_if_func)(OPENSSL_sk_delete_if_func, void *,
247 void *);
248
249 // An OPENSSL_STACK contains an array of pointers. It is not designed to be used
250 // directly, rather the wrapper macros should be used.
251 typedef struct stack_st OPENSSL_STACK;
252
253 // The following are raw stack functions. They implement the corresponding typed
254 // |sk_SAMPLE_*| functions generated by |DEFINE_STACK_OF|. Callers shouldn't be
255 // using them. Rather, callers should use the typed functions.
256 OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_cmp_func comp);
257 OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_new_null(void);
258 OPENSSL_EXPORT size_t OPENSSL_sk_num(const OPENSSL_STACK *sk);
259 OPENSSL_EXPORT void OPENSSL_sk_zero(OPENSSL_STACK *sk);
260 OPENSSL_EXPORT void *OPENSSL_sk_value(const OPENSSL_STACK *sk, size_t i);
261 OPENSSL_EXPORT void *OPENSSL_sk_set(OPENSSL_STACK *sk, size_t i, void *p);
262 OPENSSL_EXPORT void OPENSSL_sk_free(OPENSSL_STACK *sk);
263 OPENSSL_EXPORT void OPENSSL_sk_pop_free_ex(
264 OPENSSL_STACK *sk, OPENSSL_sk_call_free_func call_free_func,
265 OPENSSL_sk_free_func free_func);
266 OPENSSL_EXPORT size_t OPENSSL_sk_insert(OPENSSL_STACK *sk, void *p,
267 size_t where);
268 OPENSSL_EXPORT void *OPENSSL_sk_delete(OPENSSL_STACK *sk, size_t where);
269 OPENSSL_EXPORT void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *sk, const void *p);
270 OPENSSL_EXPORT void OPENSSL_sk_delete_if(
271 OPENSSL_STACK *sk, OPENSSL_sk_call_delete_if_func call_func,
272 OPENSSL_sk_delete_if_func func, void *data);
273 OPENSSL_EXPORT int OPENSSL_sk_find(const OPENSSL_STACK *sk, size_t *out_index,
274 const void *p,
275 OPENSSL_sk_call_cmp_func call_cmp_func);
276 OPENSSL_EXPORT int OPENSSL_sk_unshift(OPENSSL_STACK *sk, void *data);
277 OPENSSL_EXPORT void *OPENSSL_sk_shift(OPENSSL_STACK *sk);
278 OPENSSL_EXPORT size_t OPENSSL_sk_push(OPENSSL_STACK *sk, void *p);
279 OPENSSL_EXPORT void *OPENSSL_sk_pop(OPENSSL_STACK *sk);
280 OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk);
281 OPENSSL_EXPORT void OPENSSL_sk_sort(OPENSSL_STACK *sk,
282 OPENSSL_sk_call_cmp_func call_cmp_func);
283 OPENSSL_EXPORT int OPENSSL_sk_is_sorted(const OPENSSL_STACK *sk);
284 OPENSSL_EXPORT OPENSSL_sk_cmp_func
285 OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, OPENSSL_sk_cmp_func comp);
286 OPENSSL_EXPORT OPENSSL_STACK *OPENSSL_sk_deep_copy(
287 const OPENSSL_STACK *sk, OPENSSL_sk_call_copy_func call_copy_func,
288 OPENSSL_sk_copy_func copy_func, OPENSSL_sk_call_free_func call_free_func,
289 OPENSSL_sk_free_func free_func);
290
291
292 // Deprecated private functions (hidden).
293 //
294 // TODO(crbug.com/boringssl/499): Migrate callers to the typed wrappers, or at
295 // least the new names and remove the old ones.
296
297 typedef OPENSSL_STACK _STACK;
298
299 OPENSSL_INLINE OPENSSL_STACK *sk_new_null(void) {
300 return OPENSSL_sk_new_null();
301 }
302
303 OPENSSL_INLINE size_t sk_num(const OPENSSL_STACK *sk) {
304 return OPENSSL_sk_num(sk);
305 }
306
307 OPENSSL_INLINE void *sk_value(const OPENSSL_STACK *sk, size_t i) {
308 return OPENSSL_sk_value(sk, i);
309 }
310
311 OPENSSL_INLINE void sk_free(OPENSSL_STACK *sk) { OPENSSL_sk_free(sk); }
312
313 OPENSSL_INLINE size_t sk_push(OPENSSL_STACK *sk, void *p) {
314 return OPENSSL_sk_push(sk, p);
315 }
316
317 OPENSSL_INLINE void *sk_pop(OPENSSL_STACK *sk) { return OPENSSL_sk_pop(sk); }
318
319 // sk_pop_free behaves like |sk_pop_free_ex| but performs an invalid function
320 // pointer cast. It exists because some existing callers called |sk_pop_free|
321 // directly.
322 //
323 // TODO(davidben): Migrate callers to bssl::UniquePtr and remove this.
324 OPENSSL_EXPORT void sk_pop_free(OPENSSL_STACK *sk,
325 OPENSSL_sk_free_func free_func);
326
327
328 #if !defined(BORINGSSL_NO_CXX)
329 extern "C++" {
330 BSSL_NAMESPACE_BEGIN
331 namespace internal {
332 template <typename T>
333 struct StackTraits {};
334 }
335 BSSL_NAMESPACE_END
336 }
337
338 #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const) \
339 extern "C++" { \
340 BSSL_NAMESPACE_BEGIN \
341 namespace internal { \
342 template <> \
343 struct StackTraits<STACK_OF(name)> { \
344 static constexpr bool kIsStack = true; \
345 using Type = type; \
346 static constexpr bool kIsConst = is_const; \
347 }; \
348 } \
349 BSSL_NAMESPACE_END \
350 }
351
352 #else
353 #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const)
354 #endif
355
356 #define BORINGSSL_DEFINE_STACK_OF_IMPL(name, ptrtype, constptrtype) \
357 /* We disable MSVC C4191 in this macro, which warns when pointers are cast \
358 * to the wrong type. While the cast itself is valid, it is often a bug \
359 * because calling it through the cast is UB. However, we never actually \
360 * call functions as |OPENSSL_sk_cmp_func|. The type is just a type-erased \
361 * function pointer. (C does not guarantee function pointers fit in \
362 * |void*|, and GCC will warn on this.) Thus we just disable the false \
363 * positive warning. */ \
364 OPENSSL_MSVC_PRAGMA(warning(push)) \
365 OPENSSL_MSVC_PRAGMA(warning(disable : 4191)) \
366 OPENSSL_CLANG_PRAGMA("clang diagnostic push") \
367 OPENSSL_CLANG_PRAGMA("clang diagnostic ignored \"-Wunknown-warning-option\"") \
368 OPENSSL_CLANG_PRAGMA("clang diagnostic ignored \"-Wcast-function-type-strict\"") \
369 \
370 DECLARE_STACK_OF(name) \
371 \
372 typedef void (*sk_##name##_free_func)(ptrtype); \
373 typedef ptrtype (*sk_##name##_copy_func)(constptrtype); \
374 typedef int (*sk_##name##_cmp_func)(constptrtype const *, \
375 constptrtype const *); \
376 typedef int (*sk_##name##_delete_if_func)(ptrtype, void *); \
377 \
378 OPENSSL_INLINE void sk_##name##_call_free_func( \
379 OPENSSL_sk_free_func free_func, void *ptr) { \
380 ((sk_##name##_free_func)free_func)((ptrtype)ptr); \
381 } \
382 \
383 OPENSSL_INLINE void *sk_##name##_call_copy_func( \
384 OPENSSL_sk_copy_func copy_func, const void *ptr) { \
385 return (void *)((sk_##name##_copy_func)copy_func)((constptrtype)ptr); \
386 } \
387 \
388 OPENSSL_INLINE int sk_##name##_call_cmp_func(OPENSSL_sk_cmp_func cmp_func, \
389 const void *a, const void *b) { \
390 constptrtype a_ptr = (constptrtype)a; \
391 constptrtype b_ptr = (constptrtype)b; \
392 /* |cmp_func| expects an extra layer of pointers to match qsort. */ \
393 return ((sk_##name##_cmp_func)cmp_func)(&a_ptr, &b_ptr); \
394 } \
395 \
396 OPENSSL_INLINE int sk_##name##_call_delete_if_func( \
397 OPENSSL_sk_delete_if_func func, void *obj, void *data) { \
398 return ((sk_##name##_delete_if_func)func)((ptrtype)obj, data); \
399 } \
400 \
401 OPENSSL_INLINE STACK_OF(name) *sk_##name##_new(sk_##name##_cmp_func comp) { \
402 return (STACK_OF(name) *)OPENSSL_sk_new((OPENSSL_sk_cmp_func)comp); \
403 } \
404 \
405 OPENSSL_INLINE STACK_OF(name) *sk_##name##_new_null(void) { \
406 return (STACK_OF(name) *)OPENSSL_sk_new_null(); \
407 } \
408 \
409 OPENSSL_INLINE size_t sk_##name##_num(const STACK_OF(name) *sk) { \
410 return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
411 } \
412 \
413 OPENSSL_INLINE void sk_##name##_zero(STACK_OF(name) *sk) { \
414 OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
415 } \
416 \
417 OPENSSL_INLINE ptrtype sk_##name##_value(const STACK_OF(name) *sk, \
418 size_t i) { \
419 return (ptrtype)OPENSSL_sk_value((const OPENSSL_STACK *)sk, i); \
420 } \
421 \
422 OPENSSL_INLINE ptrtype sk_##name##_set(STACK_OF(name) *sk, size_t i, \
423 ptrtype p) { \
424 return (ptrtype)OPENSSL_sk_set((OPENSSL_STACK *)sk, i, (void *)p); \
425 } \
426 \
427 OPENSSL_INLINE void sk_##name##_free(STACK_OF(name) *sk) { \
428 OPENSSL_sk_free((OPENSSL_STACK *)sk); \
429 } \
430 \
431 OPENSSL_INLINE void sk_##name##_pop_free(STACK_OF(name) *sk, \
432 sk_##name##_free_func free_func) { \
433 OPENSSL_sk_pop_free_ex((OPENSSL_STACK *)sk, sk_##name##_call_free_func, \
434 (OPENSSL_sk_free_func)free_func); \
435 } \
436 \
437 OPENSSL_INLINE size_t sk_##name##_insert(STACK_OF(name) *sk, ptrtype p, \
438 size_t where) { \
439 return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (void *)p, where); \
440 } \
441 \
442 OPENSSL_INLINE ptrtype sk_##name##_delete(STACK_OF(name) *sk, \
443 size_t where) { \
444 return (ptrtype)OPENSSL_sk_delete((OPENSSL_STACK *)sk, where); \
445 } \
446 \
447 OPENSSL_INLINE ptrtype sk_##name##_delete_ptr(STACK_OF(name) *sk, \
448 constptrtype p) { \
449 return (ptrtype)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
450 (const void *)p); \
451 } \
452 \
453 OPENSSL_INLINE void sk_##name##_delete_if( \
454 STACK_OF(name) *sk, sk_##name##_delete_if_func func, void *data) { \
455 OPENSSL_sk_delete_if((OPENSSL_STACK *)sk, sk_##name##_call_delete_if_func, \
456 (OPENSSL_sk_delete_if_func)func, data); \
457 } \
458 \
459 /* use 3-arg sk_*_find_awslc when size_t-sized |out_index| needed */ \
460 OPENSSL_INLINE int sk_##name##_find_awslc(const STACK_OF(name) *sk, \
461 size_t *out_index, constptrtype p) { \
462 return OPENSSL_sk_find((const OPENSSL_STACK *)sk, out_index, \
463 (const void *)p, sk_##name##_call_cmp_func); \
464 } \
465 \
466 /* use 2-arg sk_*_find for OpenSSL compatibility */ \
467 OPENSSL_INLINE int sk_##name##_find(const STACK_OF(name) *sk, \
468 constptrtype p) { \
469 size_t out_index = 0; \
470 int ok = OPENSSL_sk_find((const OPENSSL_STACK *)sk, &out_index, \
471 (const void *)p, sk_##name##_call_cmp_func); \
472 /* return -1 if elt not found or elt index is invalid */ \
473 if (ok == 0 || out_index > INT_MAX) { \
474 return -1; \
475 } \
476 return (int) out_index; \
477 } \
478 \
479 OPENSSL_INLINE int sk_##name##_unshift(STACK_OF(name) *sk, ptrtype p) { \
480 return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (void *)p); \
481 } \
482 \
483 OPENSSL_INLINE ptrtype sk_##name##_shift(STACK_OF(name) *sk) { \
484 return (ptrtype)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
485 } \
486 \
487 OPENSSL_INLINE size_t sk_##name##_push(STACK_OF(name) *sk, ptrtype p) { \
488 return OPENSSL_sk_push((OPENSSL_STACK *)sk, (void *)p); \
489 } \
490 \
491 OPENSSL_INLINE ptrtype sk_##name##_pop(STACK_OF(name) *sk) { \
492 return (ptrtype)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
493 } \
494 \
495 OPENSSL_INLINE STACK_OF(name) *sk_##name##_dup(const STACK_OF(name) *sk) { \
496 return (STACK_OF(name) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
497 } \
498 \
499 OPENSSL_INLINE void sk_##name##_sort(STACK_OF(name) *sk) { \
500 OPENSSL_sk_sort((OPENSSL_STACK *)sk, sk_##name##_call_cmp_func); \
501 } \
502 \
503 OPENSSL_INLINE int sk_##name##_is_sorted(const STACK_OF(name) *sk) { \
504 return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
505 } \
506 \
507 OPENSSL_INLINE sk_##name##_cmp_func sk_##name##_set_cmp_func( \
508 STACK_OF(name) *sk, sk_##name##_cmp_func comp) { \
509 return (sk_##name##_cmp_func)OPENSSL_sk_set_cmp_func( \
510 (OPENSSL_STACK *)sk, (OPENSSL_sk_cmp_func)comp); \
511 } \
512 \
513 OPENSSL_INLINE STACK_OF(name) *sk_##name##_deep_copy( \
514 const STACK_OF(name) *sk, sk_##name##_copy_func copy_func, \
515 sk_##name##_free_func free_func) { \
516 return (STACK_OF(name) *)OPENSSL_sk_deep_copy( \
517 (const OPENSSL_STACK *)sk, sk_##name##_call_copy_func, \
518 (OPENSSL_sk_copy_func)copy_func, sk_##name##_call_free_func, \
519 (OPENSSL_sk_free_func)free_func); \
520 } \
521 \
522 OPENSSL_CLANG_PRAGMA("clang diagnostic pop") \
523 OPENSSL_MSVC_PRAGMA(warning(pop))
524
525
526 // Built-in stacks.
527
528 typedef char *OPENSSL_STRING;
529
530 DEFINE_STACK_OF(void)
531 DEFINE_NAMED_STACK_OF(OPENSSL_STRING, char)
532
533
534 #if defined(__cplusplus)
535 } // extern C
536 #endif
537
538 #if !defined(BORINGSSL_NO_CXX)
539 extern "C++" {
540
541 #include <type_traits>
542
543 BSSL_NAMESPACE_BEGIN
544
545 namespace internal {
546
547 // Stacks defined with |DEFINE_CONST_STACK_OF| are freed with |sk_free|.
548 template <typename Stack>
549 struct DeleterImpl<
550 Stack, typename std::enable_if<StackTraits<Stack>::kIsConst>::type> {
551 static void Free(Stack *sk) { OPENSSL_sk_free(reinterpret_cast<OPENSSL_STACK *>(sk)); }
552 };
553
554 // Stacks defined with |DEFINE_STACK_OF| are freed with |sk_pop_free| and the
555 // corresponding type's deleter.
556 template <typename Stack>
557 struct DeleterImpl<
558 Stack, typename std::enable_if<!StackTraits<Stack>::kIsConst>::type> {
559 static void Free(Stack *sk) {
560 // sk_FOO_pop_free is defined by macros and bound by name, so we cannot
561 // access it from C++ here.
562 using Type = typename StackTraits<Stack>::Type;
563 OPENSSL_sk_pop_free_ex(
564 reinterpret_cast<OPENSSL_STACK *>(sk),
565 [](OPENSSL_sk_free_func /* unused */, void *ptr) {
566 DeleterImpl<Type>::Free(reinterpret_cast<Type *>(ptr));
567 },
568 nullptr);
569 }
570 };
571
572 template <typename Stack>
573 class StackIteratorImpl {
574 public:
575 using Type = typename StackTraits<Stack>::Type;
576 // Iterators must be default-constructable.
577 StackIteratorImpl() : sk_(nullptr), idx_(0) {}
578 StackIteratorImpl(const Stack *sk, size_t idx) : sk_(sk), idx_(idx) {}
579
580 bool operator==(StackIteratorImpl other) const {
581 return sk_ == other.sk_ && idx_ == other.idx_;
582 }
583 bool operator!=(StackIteratorImpl other) const {
584 return !(*this == other);
585 }
586
587 Type *operator*() const {
588 return reinterpret_cast<Type *>(
589 OPENSSL_sk_value(reinterpret_cast<const OPENSSL_STACK *>(sk_), idx_));
590 }
591
592 StackIteratorImpl &operator++(/* prefix */) {
593 idx_++;
594 return *this;
595 }
596
597 StackIteratorImpl operator++(int /* postfix */) {
598 StackIteratorImpl copy(*this);
599 ++(*this);
600 return copy;
601 }
602
603 private:
604 const Stack *sk_;
605 size_t idx_;
606 };
607
608 template <typename Stack>
609 using StackIterator = typename std::enable_if<StackTraits<Stack>::kIsStack,
610 StackIteratorImpl<Stack>>::type;
611
612 } // namespace internal
613
614 // PushToStack pushes |elem| to |sk|. It returns true on success and false on
615 // allocation failure.
616 template <typename Stack>
617 inline
618 typename std::enable_if<!internal::StackTraits<Stack>::kIsConst, bool>::type
619 PushToStack(Stack *sk,
620 UniquePtr<typename internal::StackTraits<Stack>::Type> elem) {
621 if (!OPENSSL_sk_push(reinterpret_cast<OPENSSL_STACK *>(sk), elem.get())) {
622 return false;
623 }
624 // OPENSSL_sk_push takes ownership on success.
625 elem.release();
626 return true;
627 }
628
629 BSSL_NAMESPACE_END
630
631 // Define begin() and end() for stack types so C++ range for loops work.
632 template <typename Stack>
633 inline bssl::internal::StackIterator<Stack> begin(const Stack *sk) {
634 return bssl::internal::StackIterator<Stack>(sk, 0);
635 }
636
637 template <typename Stack>
638 inline bssl::internal::StackIterator<Stack> end(const Stack *sk) {
639 return bssl::internal::StackIterator<Stack>(
640 sk, OPENSSL_sk_num(reinterpret_cast<const OPENSSL_STACK *>(sk)));
641 }
642
643 } // extern C++
644 #endif
645
646 #endif // OPENSSL_HEADER_STACK_H