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