safestack.h 8.1 KB

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