2 * {- join("\n * ", @autowarntext) -}
4 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
13 use OpenSSL::stackhash qw(generate_stack_string_macros
14 generate_stack_const_string_macros
15 generate_stack_block_macros);
18 #ifndef OPENSSL_SAFESTACK_H
19 # define OPENSSL_SAFESTACK_H
22 # include <openssl/macros.h>
23 # ifndef OPENSSL_NO_DEPRECATED_3_0
24 # define HEADER_SAFESTACK_H
27 # include <openssl/stack.h>
28 # include <openssl/e_os2.h>
34 # define STACK_OF(type) struct stack_st_##type
36 /* Helper macro for internal use */
37 # define SKM_DEFINE_STACK_OF_INTERNAL(t1, t2, t3) \
39 typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
40 typedef void (*sk_##t1##_freefunc)(t3 *a); \
41 typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
42 static ossl_unused ossl_inline t2 *ossl_check_##t1##_type(t2 *ptr) \
46 static ossl_unused ossl_inline const OPENSSL_STACK *ossl_check_const_##t1##_sk_type(const STACK_OF(t1) *sk) \
48 return (const OPENSSL_STACK *)sk; \
50 static ossl_unused ossl_inline OPENSSL_STACK *ossl_check_##t1##_sk_type(STACK_OF(t1) *sk) \
52 return (OPENSSL_STACK *)sk; \
54 static ossl_unused ossl_inline OPENSSL_sk_compfunc ossl_check_##t1##_compfunc_type(sk_##t1##_compfunc cmp) \
56 return (OPENSSL_sk_compfunc)cmp; \
58 static ossl_unused ossl_inline OPENSSL_sk_copyfunc ossl_check_##t1##_copyfunc_type(sk_##t1##_copyfunc cpy) \
60 return (OPENSSL_sk_copyfunc)cpy; \
62 static ossl_unused ossl_inline OPENSSL_sk_freefunc ossl_check_##t1##_freefunc_type(sk_##t1##_freefunc fr) \
64 return (OPENSSL_sk_freefunc)fr; \
67 # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
69 typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
70 typedef void (*sk_##t1##_freefunc)(t3 *a); \
71 typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
72 static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
74 return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
76 static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
78 return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \
80 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
82 return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
84 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
86 return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
88 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
90 return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
92 static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
94 return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \
96 static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
98 OPENSSL_sk_free((OPENSSL_STACK *)sk); \
100 static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
102 OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
104 static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
106 return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
108 static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
110 return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
111 (const void *)ptr); \
113 static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
115 return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
117 static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
119 return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
121 static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
123 return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
125 static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
127 return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
129 static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
131 OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
133 static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
135 return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
137 static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
139 return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
141 static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
143 return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
145 static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
147 return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
149 static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
151 OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
153 static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
155 return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
157 static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
159 return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
161 static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
162 sk_##t1##_copyfunc copyfunc, \
163 sk_##t1##_freefunc freefunc) \
165 return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
166 (OPENSSL_sk_copyfunc)copyfunc, \
167 (OPENSSL_sk_freefunc)freefunc); \
169 static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
171 return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
174 # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
175 # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
176 # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
177 # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
178 SKM_DEFINE_STACK_OF(t1, const t2, t2)
181 * Strings are special: normally an lhash entry will point to a single
182 * (somewhat) mutable object. In the case of strings:
184 * a) Instead of a single char, there is an array of chars, NUL-terminated.
185 * b) The string may have be immutable.
187 * So, they need their own declarations. Especially important for
188 * type-checking tools, such as Deputy.
190 * In practice, however, it appears to be hard to have a const
191 * string. For now, I'm settling for dealing with the fact it is a
194 typedef char *OPENSSL_STRING;
195 typedef const char *OPENSSL_CSTRING;
198 * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
199 * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
200 * above, instead of a single char each entry is a NUL-terminated array of
201 * chars. So, we have to implement STRING specially for STACK_OF. This is
202 * dealt with in the autogenerated macros below.
205 generate_stack_string_macros()
206 .generate_stack_const_string_macros();
209 #if !defined(OPENSSL_NO_DEPRECATED_3_0)
211 * This is not used by OpenSSL. A block of bytes, NOT nul-terminated.
212 * These should also be distinguished from "normal" stacks.
214 typedef void *OPENSSL_BLOCK;
216 generate_stack_block_macros();