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