safestack.h 7.9 KB

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