2 * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OPENSSL_SAFESTACK_H
11 # define OPENSSL_SAFESTACK_H
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 # define HEADER_SAFESTACK_H
19 # include <openssl/stack.h>
20 # include <openssl/e_os2.h>
26 # define STACK_OF(type) struct stack_st_##type
28 # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
30 typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
31 typedef void (*sk_##t1##_freefunc)(t3 *a); \
32 typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
33 static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
35 return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
37 static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
39 return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \
41 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
43 return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
45 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
47 return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
49 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
51 return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
53 static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
55 return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \
57 static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
59 OPENSSL_sk_free((OPENSSL_STACK *)sk); \
61 static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
63 OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
65 static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
67 return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
69 static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
71 return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
74 static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
76 return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
78 static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
80 return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
82 static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
84 return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
86 static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
88 return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
90 static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
92 OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
94 static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
96 return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
98 static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
100 return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
102 static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
104 return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
106 static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
108 return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
110 static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
112 OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
114 static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
116 return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
118 static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
120 return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
122 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
123 sk_##t1##_copyfunc copyfunc, \
124 sk_##t1##_freefunc freefunc) \
126 return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
127 (OPENSSL_sk_copyfunc)copyfunc, \
128 (OPENSSL_sk_freefunc)freefunc); \
130 static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
132 return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
135 # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
136 # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
137 # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
138 SKM_DEFINE_STACK_OF(t1, const t2, t2)
139 # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
142 * Strings are special: normally an lhash entry will point to a single
143 * (somewhat) mutable object. In the case of strings:
145 * a) Instead of a single char, there is an array of chars, NUL-terminated.
146 * b) The string may have be immutable.
148 * So, they need their own declarations. Especially important for
149 * type-checking tools, such as Deputy.
151 * In practice, however, it appears to be hard to have a const
152 * string. For now, I'm settling for dealing with the fact it is a
155 typedef char *OPENSSL_STRING;
156 typedef const char *OPENSSL_CSTRING;
159 * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
160 * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
161 * above, instead of a single char each entry is a NUL-terminated array of
162 * chars. So, we have to implement STRING specially for STACK_OF. This is
163 * dealt with in the autogenerated macros below.
165 DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
166 DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char)
169 * Similarly, we sometimes use a block of characters, NOT nul-terminated.
170 * These should also be distinguished from "normal" stacks.
172 typedef void *OPENSSL_BLOCK;
173 DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
176 * If called without higher optimization (min. -xO3) the Oracle Developer
177 * Studio compiler generates code for the defined (static inline) functions
179 * This would later lead to the linker complaining about missing symbols when
180 * this header file is included but the resulting object is not linked against
181 * the Crypto library (openssl#6912).
184 # pragma weak OPENSSL_sk_num
185 # pragma weak OPENSSL_sk_value
186 # pragma weak OPENSSL_sk_new
187 # pragma weak OPENSSL_sk_new_null
188 # pragma weak OPENSSL_sk_new_reserve
189 # pragma weak OPENSSL_sk_reserve
190 # pragma weak OPENSSL_sk_free
191 # pragma weak OPENSSL_sk_zero
192 # pragma weak OPENSSL_sk_delete
193 # pragma weak OPENSSL_sk_delete_ptr
194 # pragma weak OPENSSL_sk_push
195 # pragma weak OPENSSL_sk_unshift
196 # pragma weak OPENSSL_sk_pop
197 # pragma weak OPENSSL_sk_shift
198 # pragma weak OPENSSL_sk_pop_free
199 # pragma weak OPENSSL_sk_insert
200 # pragma weak OPENSSL_sk_set
201 # pragma weak OPENSSL_sk_find
202 # pragma weak OPENSSL_sk_find_ex
203 # pragma weak OPENSSL_sk_sort
204 # pragma weak OPENSSL_sk_is_sorted
205 # pragma weak OPENSSL_sk_dup
206 # pragma weak OPENSSL_sk_deep_copy
207 # pragma weak OPENSSL_sk_set_cmp_func
208 # endif /* __SUNPRO_C */