Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
| 57 | #ifndef OPENSSL_HEADER_STACK_H |
| 58 | #define OPENSSL_HEADER_STACK_H |
| 59 | |
| 60 | #include <openssl/base.h> |
| 61 | |
| 62 | #include <openssl/type_check.h> |
| 63 | |
| 64 | #if defined(__cplusplus) |
| 65 | extern "C" { |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /* A stack, in OpenSSL, is an array of pointers. They are the most commonly |
| 70 | * used collection object. |
| 71 | * |
| 72 | * This file defines macros for type safe use of the stack functions. A stack |
| 73 | * of a specific type of object has type |STACK_OF(type)|. This can be defined |
| 74 | * (once) with |DEFINE_STACK_OF(type)| and declared where needed with |
| 75 | * |DECLARE_STACK_OF(type)|. For example: |
| 76 | * |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 77 | * typedef struct foo_st { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | * int bar; |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 79 | * } FOO; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 80 | * |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 81 | * DEFINE_STACK_OF(FOO); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | * |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 83 | * Although note that the stack will contain /pointers/ to |FOO|. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 84 | * |
| 85 | * A macro will be defined for each of the sk_* functions below. For |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 86 | * STACK_OF(FOO), the macros would be sk_FOO_new, sk_FOO_pop etc. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
| 88 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 89 | /* stack_cmp_func is a comparison function that returns a value < 0, 0 or > 0 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | * if |*a| is less than, equal to or greater than |*b|, respectively. Note the |
| 91 | * extra indirection - the function is given a pointer to a pointer to the |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 92 | * element. This differs from the usual qsort/bsearch comparison function. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 93 | typedef int (*stack_cmp_func)(const void **a, const void **b); |
| 94 | |
| 95 | /* stack_st contains an array of pointers. It is not designed to be used |
| 96 | * directly, rather the wrapper macros should be used. */ |
| 97 | typedef struct stack_st { |
| 98 | /* num contains the number of valid pointers in |data|. */ |
| 99 | size_t num; |
| 100 | void **data; |
| 101 | /* sorted is non-zero if the values pointed to by |data| are in ascending |
| 102 | * order, based on |comp|. */ |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 103 | int sorted; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | /* num_alloc contains the number of pointers allocated in the buffer pointed |
| 105 | * to by |data|, which may be larger than |num|. */ |
| 106 | size_t num_alloc; |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 107 | /* comp is an optional comparison function. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 108 | stack_cmp_func comp; |
| 109 | } _STACK; |
| 110 | |
| 111 | |
| 112 | #define STACK_OF(type) struct stack_st_##type |
| 113 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | #define DECLARE_STACK_OF(type) STACK_OF(type); |
| 115 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | /* These are the raw stack functions, you shouldn't be using them. Rather you |
| 117 | * should be using the type stack macros implemented above. */ |
| 118 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 119 | /* sk_new creates a new, empty stack with the given comparison function, which |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | * may be zero. It returns the new stack or NULL on allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 121 | OPENSSL_EXPORT _STACK *sk_new(stack_cmp_func comp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | |
| 123 | /* sk_new_null creates a new, empty stack. It returns the new stack or NULL on |
| 124 | * allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 125 | OPENSSL_EXPORT _STACK *sk_new_null(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | |
| 127 | /* sk_num returns the number of elements in |s|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 128 | OPENSSL_EXPORT size_t sk_num(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
| 130 | /* sk_zero resets |sk| to the empty state but does nothing to free the |
| 131 | * individual elements themselves. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 132 | OPENSSL_EXPORT void sk_zero(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
| 134 | /* sk_value returns the |i|th pointer in |sk|, or NULL if |i| is out of |
| 135 | * range. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 136 | OPENSSL_EXPORT void *sk_value(const _STACK *sk, size_t i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | |
| 138 | /* sk_set sets the |i|th pointer in |sk| to |p| and returns |p|. If |i| is out |
| 139 | * of range, it returns NULL. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 140 | OPENSSL_EXPORT void *sk_set(_STACK *sk, size_t i, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | /* sk_free frees the given stack and array of pointers, but does nothing to |
| 143 | * free the individual elements. Also see |sk_pop_free|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 144 | OPENSSL_EXPORT void sk_free(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
| 146 | /* sk_pop_free calls |free_func| on each element in the stack and then frees |
| 147 | * the stack itself. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 148 | OPENSSL_EXPORT void sk_pop_free(_STACK *sk, void (*free_func)(void *)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
| 150 | /* sk_insert inserts |p| into the stack at index |where|, moving existing |
| 151 | * elements if needed. It returns the length of the new stack, or zero on |
| 152 | * error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 153 | OPENSSL_EXPORT size_t sk_insert(_STACK *sk, void *p, size_t where); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
| 155 | /* sk_delete removes the pointer at index |where|, moving other elements down |
| 156 | * if needed. It returns the removed pointer, or NULL if |where| is out of |
| 157 | * range. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 158 | OPENSSL_EXPORT void *sk_delete(_STACK *sk, size_t where); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | |
| 160 | /* sk_delete_ptr removes, at most, one instance of |p| from the stack based on |
| 161 | * pointer equality. If an instance of |p| is found then |p| is returned, |
| 162 | * otherwise it returns NULL. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 163 | OPENSSL_EXPORT void *sk_delete_ptr(_STACK *sk, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
Adam Langley | 0ed0cf6 | 2015-01-06 10:49:48 -0800 | [diff] [blame] | 165 | /* sk_find returns the first value in the stack equal to |p|. If a comparison |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | * function has been set on the stack, then equality is defined by it and the |
| 167 | * stack will be sorted if need be so that a binary search can be used. |
| 168 | * Otherwise pointer equality is used. If a matching element is found, its |
| 169 | * index is written to |*out_index| (if |out_index| is not NULL) and one is |
| 170 | * returned. Otherwise zero is returned. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 171 | OPENSSL_EXPORT int sk_find(_STACK *sk, size_t *out_index, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
| 173 | /* sk_shift removes and returns the first element in the stack, or returns NULL |
| 174 | * if the stack is empty. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 175 | OPENSSL_EXPORT void *sk_shift(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
| 177 | /* sk_push appends |p| to the stack and returns the length of the new stack, or |
| 178 | * 0 on allocation failure. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 179 | OPENSSL_EXPORT size_t sk_push(_STACK *sk, void *p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
| 181 | /* sk_pop returns and removes the last element on the stack, or NULL if the |
| 182 | * stack is empty. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 183 | OPENSSL_EXPORT void *sk_pop(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
| 185 | /* sk_dup performs a shallow copy of a stack and returns the new stack, or NULL |
| 186 | * on error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 187 | OPENSSL_EXPORT _STACK *sk_dup(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
| 189 | /* sk_sort sorts the elements of |sk| into ascending order based on the |
| 190 | * comparison function. The stack maintains a |sorted| flag and sorting an |
| 191 | * already sorted stack is a no-op. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 192 | OPENSSL_EXPORT void sk_sort(_STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
| 194 | /* sk_is_sorted returns one if |sk| is known to be sorted and zero |
| 195 | * otherwise. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 196 | OPENSSL_EXPORT int sk_is_sorted(const _STACK *sk); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
| 198 | /* sk_set_cmp_func sets the comparison function to be used by |sk| and returns |
| 199 | * the previous one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 200 | OPENSSL_EXPORT stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
Adam Langley | a1048a7 | 2015-02-11 14:27:21 -0800 | [diff] [blame] | 202 | /* sk_deep_copy performs a copy of |sk| and of each of the non-NULL elements in |
| 203 | * |sk| by using |copy_func|. If an error occurs, |free_func| is used to free |
| 204 | * any copies already made and NULL is returned. */ |
| 205 | OPENSSL_EXPORT _STACK *sk_deep_copy(const _STACK *sk, |
| 206 | void *(*copy_func)(void *), |
| 207 | void (*free_func)(void *)); |
| 208 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 210 | /* Defining stack types. |
| 211 | * |
| 212 | * This set of macros is used to emit the typed functions that act on a |
| 213 | * |STACK_OF(T)|. */ |
| 214 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 215 | #if !defined(BORINGSSL_NO_CXX) |
| 216 | extern "C++" { |
| 217 | namespace bssl { |
| 218 | namespace internal { |
| 219 | template <typename T> |
| 220 | struct StackTraits {}; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 225 | #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const) \ |
| 226 | extern "C++" { \ |
| 227 | namespace bssl { \ |
| 228 | namespace internal { \ |
| 229 | template <> \ |
| 230 | struct StackTraits<STACK_OF(name)> { \ |
| 231 | static constexpr bool kIsStack = true; \ |
| 232 | using Type = type; \ |
| 233 | static constexpr bool kIsConst = is_const; \ |
| 234 | }; \ |
| 235 | } \ |
| 236 | } \ |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | #else |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 240 | #define BORINGSSL_DEFINE_STACK_TRAITS(name, type, is_const) |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 241 | #endif |
| 242 | |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 243 | /* Stack functions must be tagged unused to support file-local stack types. |
| 244 | * Clang's -Wunused-function only allows unused static inline functions if they |
| 245 | * are defined in a header. */ |
| 246 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 247 | #define BORINGSSL_DEFINE_STACK_OF_IMPL(name, ptrtype, constptrtype) \ |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 248 | DECLARE_STACK_OF(name); \ |
| 249 | \ |
| 250 | typedef int (*stack_##name##_cmp_func)(constptrtype *a, constptrtype *b); \ |
| 251 | \ |
| 252 | static inline OPENSSL_UNUSED STACK_OF(name) * \ |
| 253 | sk_##name##_new(stack_##name##_cmp_func comp) { \ |
| 254 | return (STACK_OF(name) *)sk_new((stack_cmp_func)comp); \ |
| 255 | } \ |
| 256 | \ |
| 257 | static inline OPENSSL_UNUSED STACK_OF(name) *sk_##name##_new_null(void) { \ |
| 258 | return (STACK_OF(name) *)sk_new_null(); \ |
| 259 | } \ |
| 260 | \ |
| 261 | static inline OPENSSL_UNUSED size_t sk_##name##_num( \ |
| 262 | const STACK_OF(name) *sk) { \ |
| 263 | return sk_num((const _STACK *)sk); \ |
| 264 | } \ |
| 265 | \ |
| 266 | static inline OPENSSL_UNUSED void sk_##name##_zero(STACK_OF(name) *sk) { \ |
| 267 | sk_zero((_STACK *)sk); \ |
| 268 | } \ |
| 269 | \ |
| 270 | static inline OPENSSL_UNUSED ptrtype sk_##name##_value( \ |
| 271 | const STACK_OF(name) *sk, size_t i) { \ |
| 272 | return (ptrtype)sk_value((const _STACK *)sk, i); \ |
| 273 | } \ |
| 274 | \ |
| 275 | static inline OPENSSL_UNUSED ptrtype sk_##name##_set(STACK_OF(name) *sk, \ |
| 276 | size_t i, ptrtype p) { \ |
| 277 | return (ptrtype)sk_set((_STACK *)sk, i, (void *)p); \ |
| 278 | } \ |
| 279 | \ |
| 280 | static inline OPENSSL_UNUSED void sk_##name##_free(STACK_OF(name) *sk) { \ |
| 281 | sk_free((_STACK *)sk); \ |
| 282 | } \ |
| 283 | \ |
| 284 | static inline OPENSSL_UNUSED void sk_##name##_pop_free( \ |
| 285 | STACK_OF(name) *sk, void (*free_func)(ptrtype p)) { \ |
| 286 | sk_pop_free((_STACK *)sk, (void (*)(void *))free_func); \ |
| 287 | } \ |
| 288 | \ |
| 289 | static inline OPENSSL_UNUSED size_t sk_##name##_insert( \ |
| 290 | STACK_OF(name) *sk, ptrtype p, size_t where) { \ |
| 291 | return sk_insert((_STACK *)sk, (void *)p, where); \ |
| 292 | } \ |
| 293 | \ |
| 294 | static inline OPENSSL_UNUSED ptrtype sk_##name##_delete(STACK_OF(name) *sk, \ |
| 295 | size_t where) { \ |
| 296 | return (ptrtype)sk_delete((_STACK *)sk, where); \ |
| 297 | } \ |
| 298 | \ |
| 299 | static inline OPENSSL_UNUSED ptrtype sk_##name##_delete_ptr( \ |
| 300 | STACK_OF(name) *sk, ptrtype p) { \ |
| 301 | return (ptrtype)sk_delete_ptr((_STACK *)sk, (void *)p); \ |
| 302 | } \ |
| 303 | \ |
| 304 | static inline OPENSSL_UNUSED int sk_##name##_find( \ |
| 305 | STACK_OF(name) *sk, size_t *out_index, ptrtype p) { \ |
| 306 | return sk_find((_STACK *)sk, out_index, (void *)p); \ |
| 307 | } \ |
| 308 | \ |
| 309 | static inline OPENSSL_UNUSED ptrtype sk_##name##_shift(STACK_OF(name) *sk) { \ |
| 310 | return (ptrtype)sk_shift((_STACK *)sk); \ |
| 311 | } \ |
| 312 | \ |
| 313 | static inline OPENSSL_UNUSED size_t sk_##name##_push(STACK_OF(name) *sk, \ |
| 314 | ptrtype p) { \ |
| 315 | return sk_push((_STACK *)sk, (void *)p); \ |
| 316 | } \ |
| 317 | \ |
| 318 | static inline OPENSSL_UNUSED ptrtype sk_##name##_pop(STACK_OF(name) *sk) { \ |
| 319 | return (ptrtype)sk_pop((_STACK *)sk); \ |
| 320 | } \ |
| 321 | \ |
| 322 | static inline OPENSSL_UNUSED STACK_OF(name) * \ |
| 323 | sk_##name##_dup(const STACK_OF(name) *sk) { \ |
| 324 | return (STACK_OF(name) *)sk_dup((const _STACK *)sk); \ |
| 325 | } \ |
| 326 | \ |
| 327 | static inline OPENSSL_UNUSED void sk_##name##_sort(STACK_OF(name) *sk) { \ |
| 328 | sk_sort((_STACK *)sk); \ |
| 329 | } \ |
| 330 | \ |
| 331 | static inline OPENSSL_UNUSED int sk_##name##_is_sorted( \ |
| 332 | const STACK_OF(name) *sk) { \ |
| 333 | return sk_is_sorted((const _STACK *)sk); \ |
| 334 | } \ |
| 335 | \ |
| 336 | static inline OPENSSL_UNUSED stack_##name##_cmp_func \ |
| 337 | sk_##name##_set_cmp_func(STACK_OF(name) *sk, \ |
| 338 | stack_##name##_cmp_func comp) { \ |
| 339 | return (stack_##name##_cmp_func)sk_set_cmp_func((_STACK *)sk, \ |
| 340 | (stack_cmp_func)comp); \ |
| 341 | } \ |
| 342 | \ |
| 343 | static inline OPENSSL_UNUSED STACK_OF(name) * \ |
| 344 | sk_##name##_deep_copy(const STACK_OF(name) *sk, \ |
| 345 | ptrtype(*copy_func)(ptrtype), \ |
| 346 | void (*free_func)(ptrtype)) { \ |
| 347 | return (STACK_OF(name) *)sk_deep_copy((_STACK *)sk, \ |
| 348 | (void *(*)(void *))copy_func, \ |
| 349 | (void (*)(void *))free_func); \ |
| 350 | } |
| 351 | |
| 352 | /* DEFINE_STACK_OF defines |STACK_OF(type)| to be a stack whose elements are |
| 353 | * |type| *. */ |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 354 | #define DEFINE_STACK_OF(type) \ |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 355 | BORINGSSL_DEFINE_STACK_OF_IMPL(type, type *, const type *) \ |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 356 | BORINGSSL_DEFINE_STACK_TRAITS(type, type, false) |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 357 | |
| 358 | /* DEFINE_CONST_STACK_OF defines |STACK_OF(type)| to be a stack whose elements |
| 359 | * are const |type| *. */ |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 360 | #define DEFINE_CONST_STACK_OF(type) \ |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 361 | BORINGSSL_DEFINE_STACK_OF_IMPL(type, const type *, const type *) \ |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 362 | BORINGSSL_DEFINE_STACK_TRAITS(type, const type, true) |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 363 | |
| 364 | /* DEFINE_SPECIAL_STACK_OF defines |STACK_OF(type)| to be a stack whose elements |
| 365 | * are |type|, where |type| must be a typedef for a pointer. */ |
| 366 | #define DEFINE_SPECIAL_STACK_OF(type) \ |
| 367 | OPENSSL_COMPILE_ASSERT(sizeof(type) == sizeof(void *), \ |
| 368 | special_stack_of_non_pointer_##type); \ |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 369 | BORINGSSL_DEFINE_STACK_OF_IMPL(type, type, const type) |
David Benjamin | 01f8a8c | 2017-04-15 18:12:55 -0400 | [diff] [blame] | 370 | |
| 371 | |
| 372 | typedef char *OPENSSL_STRING; |
| 373 | |
| 374 | DEFINE_STACK_OF(void) |
| 375 | DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING) |
| 376 | |
| 377 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | #if defined(__cplusplus) |
| 379 | } /* extern C */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | #endif |
| 381 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 382 | #if !defined(BORINGSSL_NO_CXX) |
| 383 | extern "C++" { |
| 384 | |
| 385 | #include <type_traits> |
| 386 | |
| 387 | namespace bssl { |
| 388 | |
| 389 | namespace internal { |
| 390 | |
| 391 | // Stacks defined with |DEFINE_CONST_STACK_OF| are freed with |sk_free|. |
| 392 | template <typename Stack> |
| 393 | struct DeleterImpl< |
| 394 | Stack, typename std::enable_if<StackTraits<Stack>::kIsConst>::type> { |
| 395 | static void Free(Stack *sk) { sk_free(reinterpret_cast<_STACK *>(sk)); } |
| 396 | }; |
| 397 | |
| 398 | // Stacks defined with |DEFINE_STACK_OF| are freed with |sk_pop_free| and the |
| 399 | // corresponding type's deleter. |
| 400 | template <typename Stack> |
| 401 | struct DeleterImpl< |
| 402 | Stack, typename std::enable_if<!StackTraits<Stack>::kIsConst>::type> { |
| 403 | static void Free(Stack *sk) { |
| 404 | sk_pop_free( |
| 405 | reinterpret_cast<_STACK *>(sk), |
| 406 | reinterpret_cast<void (*)(void *)>( |
| 407 | DeleterImpl<typename StackTraits<Stack>::Type>::Free)); |
| 408 | } |
| 409 | }; |
| 410 | |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 411 | template <typename Stack> |
| 412 | class StackIteratorImpl { |
| 413 | public: |
| 414 | using Type = typename StackTraits<Stack>::Type; |
| 415 | // Iterators must be default-constructable. |
| 416 | StackIteratorImpl() : sk_(nullptr), idx_(0) {} |
| 417 | StackIteratorImpl(const Stack *sk, size_t idx) : sk_(sk), idx_(idx) {} |
| 418 | |
| 419 | bool operator==(StackIteratorImpl other) const { |
| 420 | return sk_ == other.sk_ && idx_ == other.idx_; |
| 421 | } |
| 422 | bool operator!=(StackIteratorImpl other) const { |
| 423 | return !(*this == other); |
| 424 | } |
| 425 | |
| 426 | Type *operator*() const { |
| 427 | return reinterpret_cast<Type *>( |
| 428 | sk_value(reinterpret_cast<const _STACK *>(sk_), idx_)); |
| 429 | } |
| 430 | |
| 431 | StackIteratorImpl &operator++(/* prefix */) { |
| 432 | idx_++; |
| 433 | return *this; |
| 434 | } |
| 435 | |
| 436 | StackIteratorImpl operator++(int /* postfix */) { |
| 437 | StackIteratorImpl copy(*this); |
| 438 | ++(*this); |
| 439 | return copy; |
| 440 | } |
| 441 | |
| 442 | private: |
| 443 | const Stack *sk_; |
| 444 | size_t idx_; |
| 445 | }; |
| 446 | |
| 447 | template <typename Stack> |
| 448 | using StackIterator = typename std::enable_if<StackTraits<Stack>::kIsStack, |
| 449 | StackIteratorImpl<Stack>>::type; |
| 450 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 451 | } // namespace internal |
| 452 | |
David Benjamin | 6e9321f | 2017-07-25 23:49:58 -0400 | [diff] [blame] | 453 | // PushToStack pushes |elem| to |sk|. It returns true on success and false on |
| 454 | // allocation failure. |
| 455 | template <typename Stack> |
| 456 | static inline |
| 457 | typename std::enable_if<!internal::StackTraits<Stack>::kIsConst, bool>::type |
| 458 | PushToStack(Stack *sk, |
| 459 | UniquePtr<typename internal::StackTraits<Stack>::Type> elem) { |
| 460 | if (!sk_push(reinterpret_cast<_STACK *>(sk), elem.get())) { |
| 461 | return false; |
| 462 | } |
| 463 | // sk_push takes ownership on success. |
| 464 | elem.release(); |
| 465 | return true; |
| 466 | } |
| 467 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 468 | } // namespace bssl |
| 469 | |
David Benjamin | ec78383 | 2017-07-25 23:23:03 -0400 | [diff] [blame] | 470 | // Define begin() and end() for stack types so C++ range for loops work. |
| 471 | template <typename Stack> |
| 472 | static inline bssl::internal::StackIterator<Stack> begin(const Stack *sk) { |
| 473 | return bssl::internal::StackIterator<Stack>(sk, 0); |
| 474 | } |
| 475 | |
| 476 | template <typename Stack> |
| 477 | static inline bssl::internal::StackIterator<Stack> end(const Stack *sk) { |
| 478 | return bssl::internal::StackIterator<Stack>( |
| 479 | sk, sk_num(reinterpret_cast<const _STACK *>(sk))); |
| 480 | } |
| 481 | |
David Benjamin | 0121953 | 2017-07-25 22:33:06 -0400 | [diff] [blame] | 482 | } // extern C++ |
| 483 | #endif |
| 484 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | #endif /* OPENSSL_HEADER_STACK_H */ |