Home | History | Annotate | Line # | Download | only in openssl
      1  1.1  christos /*
      2  1.1  christos  * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the OpenSSL license (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #ifndef HEADER_SAFESTACK_H
     11  1.1  christos # define HEADER_SAFESTACK_H
     12  1.1  christos 
     13  1.1  christos # include <openssl/stack.h>
     14  1.1  christos # include <openssl/e_os2.h>
     15  1.1  christos 
     16  1.1  christos #ifdef __cplusplus
     17  1.1  christos extern "C" {
     18  1.1  christos #endif
     19  1.1  christos 
     20  1.1  christos # define STACK_OF(type) struct stack_st_##type
     21  1.1  christos 
     22  1.1  christos # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
     23  1.1  christos     STACK_OF(t1); \
     24  1.1  christos     typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
     25  1.1  christos     typedef void (*sk_##t1##_freefunc)(t3 *a); \
     26  1.1  christos     typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
     27  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
     28  1.1  christos     { \
     29  1.1  christos         return OPENSSL_sk_num((const OPENSSL_STACK *)(const void *)sk); \
     30  1.1  christos     } \
     31  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1)*sk, int idx) \
     32  1.1  christos     { \
     33  1.1  christos         return (t2 *)(void *)OPENSSL_sk_value((const OPENSSL_STACK *)(const void *)sk, idx); \
     34  1.1  christos     } \
     35  1.1  christos     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
     36  1.1  christos     { \
     37  1.1  christos         return (STACK_OF(t1) *)(void *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
     38  1.1  christos     } \
     39  1.1  christos     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
     40  1.1  christos     { \
     41  1.1  christos         return (STACK_OF(t1) *)(void *)OPENSSL_sk_new_null(); \
     42  1.1  christos     } \
     43  1.1  christos     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
     44  1.1  christos     { \
     45  1.1  christos         return (STACK_OF(t1) *)(void *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
     46  1.1  christos     } \
     47  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
     48  1.1  christos     { \
     49  1.1  christos         return OPENSSL_sk_reserve((OPENSSL_STACK *)(void *)sk, n); \
     50  1.1  christos     } \
     51  1.1  christos     static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
     52  1.1  christos     { \
     53  1.1  christos         OPENSSL_sk_free((OPENSSL_STACK *)(void *)sk); \
     54  1.1  christos     } \
     55  1.1  christos     static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
     56  1.1  christos     { \
     57  1.1  christos         OPENSSL_sk_zero((OPENSSL_STACK *)(void *)sk); \
     58  1.1  christos     } \
     59  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
     60  1.1  christos     { \
     61  1.1  christos         return (t2 *)(void *)OPENSSL_sk_delete((OPENSSL_STACK *)(void *)sk, i); \
     62  1.1  christos     } \
     63  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
     64  1.1  christos     { \
     65  1.1  christos         return (t2 *)(void *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)(void *)sk, \
     66  1.1  christos                                            (const void *)ptr); \
     67  1.1  christos     } \
     68  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
     69  1.1  christos     { \
     70  1.1  christos         return OPENSSL_sk_push((OPENSSL_STACK *)(void *)sk, (const void *)ptr); \
     71  1.1  christos     } \
     72  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
     73  1.1  christos     { \
     74  1.1  christos         return OPENSSL_sk_unshift((OPENSSL_STACK *)(void *)sk, (const void *)ptr); \
     75  1.1  christos     } \
     76  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
     77  1.1  christos     { \
     78  1.1  christos         return (t2 *)(void *)OPENSSL_sk_pop((OPENSSL_STACK *)(void *)sk); \
     79  1.1  christos     } \
     80  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
     81  1.1  christos     { \
     82  1.1  christos         return (t2 *)(void *)OPENSSL_sk_shift((OPENSSL_STACK *)(void *)sk); \
     83  1.1  christos     } \
     84  1.1  christos     static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
     85  1.1  christos     { \
     86  1.1  christos         OPENSSL_sk_pop_free((OPENSSL_STACK *)(void *)sk, (OPENSSL_sk_freefunc)freefunc); \
     87  1.1  christos     } \
     88  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
     89  1.1  christos     { \
     90  1.1  christos         return OPENSSL_sk_insert((OPENSSL_STACK *)(void *)sk, (const void *)ptr, idx); \
     91  1.1  christos     } \
     92  1.1  christos     static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
     93  1.1  christos     { \
     94  1.1  christos         return (t2 *)(void *)OPENSSL_sk_set((OPENSSL_STACK *)(void *)sk, idx, (const void *)ptr); \
     95  1.1  christos     } \
     96  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
     97  1.1  christos     { \
     98  1.1  christos         return OPENSSL_sk_find((OPENSSL_STACK *)(void *)sk, (const void *)ptr); \
     99  1.1  christos     } \
    100  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
    101  1.1  christos     { \
    102  1.1  christos         return OPENSSL_sk_find_ex((OPENSSL_STACK *)(void *)sk, (const void *)ptr); \
    103  1.1  christos     } \
    104  1.1  christos     static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
    105  1.1  christos     { \
    106  1.1  christos         OPENSSL_sk_sort((OPENSSL_STACK *)(void *)sk); \
    107  1.1  christos     } \
    108  1.1  christos     static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
    109  1.1  christos     { \
    110  1.1  christos         return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)(const void *)sk); \
    111  1.1  christos     } \
    112  1.1  christos     static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
    113  1.1  christos     { \
    114  1.1  christos         return (STACK_OF(t1) *)(void *)OPENSSL_sk_dup((const OPENSSL_STACK *)(const void *)sk); \
    115  1.1  christos     } \
    116  1.1  christos     static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
    117  1.1  christos                                                     sk_##t1##_copyfunc copyfunc, \
    118  1.1  christos                                                     sk_##t1##_freefunc freefunc) \
    119  1.1  christos     { \
    120  1.1  christos         return (STACK_OF(t1) *)(void *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)(const void *)sk, \
    121  1.1  christos                                             (OPENSSL_sk_copyfunc)copyfunc, \
    122  1.1  christos                                             (OPENSSL_sk_freefunc)freefunc); \
    123  1.1  christos     } \
    124  1.1  christos     static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
    125  1.1  christos     { \
    126  1.1  christos         return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)(void *)sk, (OPENSSL_sk_compfunc)compare); \
    127  1.1  christos     }
    128  1.1  christos 
    129  1.1  christos # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
    130  1.1  christos # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
    131  1.1  christos # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
    132  1.1  christos             SKM_DEFINE_STACK_OF(t1, const t2, t2)
    133  1.1  christos # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
    134  1.1  christos 
    135  1.1  christos /*-
    136  1.1  christos  * Strings are special: normally an lhash entry will point to a single
    137  1.1  christos  * (somewhat) mutable object. In the case of strings:
    138  1.1  christos  *
    139  1.1  christos  * a) Instead of a single char, there is an array of chars, NUL-terminated.
    140  1.1  christos  * b) The string may have be immutable.
    141  1.1  christos  *
    142  1.1  christos  * So, they need their own declarations. Especially important for
    143  1.1  christos  * type-checking tools, such as Deputy.
    144  1.1  christos  *
    145  1.1  christos  * In practice, however, it appears to be hard to have a const
    146  1.1  christos  * string. For now, I'm settling for dealing with the fact it is a
    147  1.1  christos  * string at all.
    148  1.1  christos  */
    149  1.1  christos typedef char *OPENSSL_STRING;
    150  1.1  christos typedef const char *OPENSSL_CSTRING;
    151  1.1  christos 
    152  1.1  christos /*-
    153  1.1  christos  * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
    154  1.1  christos  * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
    155  1.1  christos  * above, instead of a single char each entry is a NUL-terminated array of
    156  1.1  christos  * chars. So, we have to implement STRING specially for STACK_OF. This is
    157  1.1  christos  * dealt with in the autogenerated macros below.
    158  1.1  christos  */
    159  1.1  christos DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
    160  1.1  christos DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char)
    161  1.1  christos 
    162  1.1  christos /*
    163  1.1  christos  * Similarly, we sometimes use a block of characters, NOT nul-terminated.
    164  1.1  christos  * These should also be distinguished from "normal" stacks.
    165  1.1  christos  */
    166  1.1  christos typedef void *OPENSSL_BLOCK;
    167  1.1  christos DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
    168  1.1  christos 
    169  1.1  christos /*
    170  1.1  christos  * If called without higher optimization (min. -xO3) the Oracle Developer
    171  1.1  christos  * Studio compiler generates code for the defined (static inline) functions
    172  1.1  christos  * above.
    173  1.1  christos  * This would later lead to the linker complaining about missing symbols when
    174  1.1  christos  * this header file is included but the resulting object is not linked against
    175  1.1  christos  * the Crypto library (openssl#6912).
    176  1.1  christos  */
    177  1.1  christos # ifdef __SUNPRO_C
    178  1.1  christos #  pragma weak OPENSSL_sk_num
    179  1.1  christos #  pragma weak OPENSSL_sk_value
    180  1.1  christos #  pragma weak OPENSSL_sk_new
    181  1.1  christos #  pragma weak OPENSSL_sk_new_null
    182  1.1  christos #  pragma weak OPENSSL_sk_new_reserve
    183  1.1  christos #  pragma weak OPENSSL_sk_reserve
    184  1.1  christos #  pragma weak OPENSSL_sk_free
    185  1.1  christos #  pragma weak OPENSSL_sk_zero
    186  1.1  christos #  pragma weak OPENSSL_sk_delete
    187  1.1  christos #  pragma weak OPENSSL_sk_delete_ptr
    188  1.1  christos #  pragma weak OPENSSL_sk_push
    189  1.1  christos #  pragma weak OPENSSL_sk_unshift
    190  1.1  christos #  pragma weak OPENSSL_sk_pop
    191  1.1  christos #  pragma weak OPENSSL_sk_shift
    192  1.1  christos #  pragma weak OPENSSL_sk_pop_free
    193  1.1  christos #  pragma weak OPENSSL_sk_insert
    194  1.1  christos #  pragma weak OPENSSL_sk_set
    195  1.1  christos #  pragma weak OPENSSL_sk_find
    196  1.1  christos #  pragma weak OPENSSL_sk_find_ex
    197  1.1  christos #  pragma weak OPENSSL_sk_sort
    198  1.1  christos #  pragma weak OPENSSL_sk_is_sorted
    199  1.1  christos #  pragma weak OPENSSL_sk_dup
    200  1.1  christos #  pragma weak OPENSSL_sk_deep_copy
    201  1.1  christos #  pragma weak OPENSSL_sk_set_cmp_func
    202  1.1  christos # endif /* __SUNPRO_C */
    203  1.1  christos 
    204  1.1  christos # ifdef  __cplusplus
    205  1.1  christos }
    206  1.1  christos # endif
    207  1.1  christos #endif
    208