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