safestack.h.in 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * {- join("\n * ", @autowarntext) -}
  3. *
  4. * Copyright 1999-2021 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. use OpenSSL::stackhash qw(generate_stack_string_macros
  13. generate_stack_const_string_macros
  14. generate_stack_block_macros);
  15. -}
  16. #ifndef OPENSSL_SAFESTACK_H
  17. # define OPENSSL_SAFESTACK_H
  18. # pragma once
  19. # include <openssl/macros.h>
  20. # ifndef OPENSSL_NO_DEPRECATED_3_0
  21. # define HEADER_SAFESTACK_H
  22. # endif
  23. # include <openssl/stack.h>
  24. # include <openssl/e_os2.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. # define STACK_OF(type) struct stack_st_##type
  29. /* Helper macro for internal use */
  30. # define SKM_DEFINE_STACK_OF_INTERNAL(t1, t2, t3) \
  31. STACK_OF(t1); \
  32. typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
  33. typedef void (*sk_##t1##_freefunc)(t3 *a); \
  34. typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
  35. static ossl_unused ossl_inline t2 *ossl_check_##t1##_type(t2 *ptr) \
  36. { \
  37. return ptr; \
  38. } \
  39. static ossl_unused ossl_inline const OPENSSL_STACK *ossl_check_const_##t1##_sk_type(const STACK_OF(t1) *sk) \
  40. { \
  41. return (const OPENSSL_STACK *)sk; \
  42. } \
  43. static ossl_unused ossl_inline OPENSSL_STACK *ossl_check_##t1##_sk_type(STACK_OF(t1) *sk) \
  44. { \
  45. return (OPENSSL_STACK *)sk; \
  46. } \
  47. static ossl_unused ossl_inline OPENSSL_sk_compfunc ossl_check_##t1##_compfunc_type(sk_##t1##_compfunc cmp) \
  48. { \
  49. return (OPENSSL_sk_compfunc)cmp; \
  50. } \
  51. static ossl_unused ossl_inline OPENSSL_sk_copyfunc ossl_check_##t1##_copyfunc_type(sk_##t1##_copyfunc cpy) \
  52. { \
  53. return (OPENSSL_sk_copyfunc)cpy; \
  54. } \
  55. static ossl_unused ossl_inline OPENSSL_sk_freefunc ossl_check_##t1##_freefunc_type(sk_##t1##_freefunc fr) \
  56. { \
  57. return (OPENSSL_sk_freefunc)fr; \
  58. }
  59. # define SKM_DEFINE_STACK_OF(t1, t2, t3) \
  60. STACK_OF(t1); \
  61. typedef int (*sk_##t1##_compfunc)(const t3 * const *a, const t3 *const *b); \
  62. typedef void (*sk_##t1##_freefunc)(t3 *a); \
  63. typedef t3 * (*sk_##t1##_copyfunc)(const t3 *a); \
  64. static ossl_unused ossl_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
  65. { \
  66. return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
  67. } \
  68. static ossl_unused ossl_inline t2 *sk_##t1##_value(const STACK_OF(t1) *sk, int idx) \
  69. { \
  70. return (t2 *)OPENSSL_sk_value((const OPENSSL_STACK *)sk, idx); \
  71. } \
  72. static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new(sk_##t1##_compfunc compare) \
  73. { \
  74. return (STACK_OF(t1) *)OPENSSL_sk_new((OPENSSL_sk_compfunc)compare); \
  75. } \
  76. static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
  77. { \
  78. return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
  79. } \
  80. static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_new_reserve(sk_##t1##_compfunc compare, int n) \
  81. { \
  82. return (STACK_OF(t1) *)OPENSSL_sk_new_reserve((OPENSSL_sk_compfunc)compare, n); \
  83. } \
  84. static ossl_unused ossl_inline int sk_##t1##_reserve(STACK_OF(t1) *sk, int n) \
  85. { \
  86. return OPENSSL_sk_reserve((OPENSSL_STACK *)sk, n); \
  87. } \
  88. static ossl_unused ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
  89. { \
  90. OPENSSL_sk_free((OPENSSL_STACK *)sk); \
  91. } \
  92. static ossl_unused ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
  93. { \
  94. OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
  95. } \
  96. static ossl_unused ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
  97. { \
  98. return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
  99. } \
  100. static ossl_unused ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
  101. { \
  102. return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
  103. (const void *)ptr); \
  104. } \
  105. static ossl_unused ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
  106. { \
  107. return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
  108. } \
  109. static ossl_unused ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
  110. { \
  111. return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
  112. } \
  113. static ossl_unused ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
  114. { \
  115. return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
  116. } \
  117. static ossl_unused ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
  118. { \
  119. return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
  120. } \
  121. static ossl_unused ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
  122. { \
  123. OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
  124. } \
  125. static ossl_unused ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
  126. { \
  127. return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
  128. } \
  129. static ossl_unused ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
  130. { \
  131. return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
  132. } \
  133. static ossl_unused ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
  134. { \
  135. return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
  136. } \
  137. static ossl_unused ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
  138. { \
  139. return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
  140. } \
  141. static ossl_unused ossl_inline int sk_##t1##_find_all(STACK_OF(t1) *sk, t2 *ptr, int *pnum) \
  142. { \
  143. return OPENSSL_sk_find_all((OPENSSL_STACK *)sk, (const void *)ptr, pnum); \
  144. } \
  145. static ossl_unused ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
  146. { \
  147. OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
  148. } \
  149. static ossl_unused ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
  150. { \
  151. return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
  152. } \
  153. static ossl_unused ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
  154. { \
  155. return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
  156. } \
  157. static ossl_unused ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
  158. sk_##t1##_copyfunc copyfunc, \
  159. sk_##t1##_freefunc freefunc) \
  160. { \
  161. return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
  162. (OPENSSL_sk_copyfunc)copyfunc, \
  163. (OPENSSL_sk_freefunc)freefunc); \
  164. } \
  165. static ossl_unused ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
  166. { \
  167. return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
  168. }
  169. # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
  170. # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
  171. # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
  172. # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
  173. SKM_DEFINE_STACK_OF(t1, const t2, t2)
  174. /*-
  175. * Strings are special: normally an lhash entry will point to a single
  176. * (somewhat) mutable object. In the case of strings:
  177. *
  178. * a) Instead of a single char, there is an array of chars, NUL-terminated.
  179. * b) The string may have be immutable.
  180. *
  181. * So, they need their own declarations. Especially important for
  182. * type-checking tools, such as Deputy.
  183. *
  184. * In practice, however, it appears to be hard to have a const
  185. * string. For now, I'm settling for dealing with the fact it is a
  186. * string at all.
  187. */
  188. typedef char *OPENSSL_STRING;
  189. typedef const char *OPENSSL_CSTRING;
  190. /*-
  191. * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
  192. * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
  193. * above, instead of a single char each entry is a NUL-terminated array of
  194. * chars. So, we have to implement STRING specially for STACK_OF. This is
  195. * dealt with in the autogenerated macros below.
  196. */
  197. {-
  198. generate_stack_string_macros()
  199. .generate_stack_const_string_macros();
  200. -}
  201. #if !defined(OPENSSL_NO_DEPRECATED_3_0)
  202. /*
  203. * This is not used by OpenSSL. A block of bytes, NOT nul-terminated.
  204. * These should also be distinguished from "normal" stacks.
  205. */
  206. typedef void *OPENSSL_BLOCK;
  207. {-
  208. generate_stack_block_macros();
  209. -}
  210. #endif
  211. # ifdef __cplusplus
  212. }
  213. # endif
  214. #endif