safestack.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright 1999-2016 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_inline int sk_##t1##_num(const STACK_OF(t1) *sk) \
  23. { \
  24. return OPENSSL_sk_num((const OPENSSL_STACK *)sk); \
  25. } \
  26. static 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_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_inline STACK_OF(t1) *sk_##t1##_new_null(void) \
  35. { \
  36. return (STACK_OF(t1) *)OPENSSL_sk_new_null(); \
  37. } \
  38. static ossl_inline void sk_##t1##_free(STACK_OF(t1) *sk) \
  39. { \
  40. OPENSSL_sk_free((OPENSSL_STACK *)sk); \
  41. } \
  42. static ossl_inline void sk_##t1##_zero(STACK_OF(t1) *sk) \
  43. { \
  44. OPENSSL_sk_zero((OPENSSL_STACK *)sk); \
  45. } \
  46. static ossl_inline t2 *sk_##t1##_delete(STACK_OF(t1) *sk, int i) \
  47. { \
  48. return (t2 *)OPENSSL_sk_delete((OPENSSL_STACK *)sk, i); \
  49. } \
  50. static ossl_inline t2 *sk_##t1##_delete_ptr(STACK_OF(t1) *sk, t2 *ptr) \
  51. { \
  52. return (t2 *)OPENSSL_sk_delete_ptr((OPENSSL_STACK *)sk, \
  53. (const void *)ptr); \
  54. } \
  55. static ossl_inline int sk_##t1##_push(STACK_OF(t1) *sk, t2 *ptr) \
  56. { \
  57. return OPENSSL_sk_push((OPENSSL_STACK *)sk, (const void *)ptr); \
  58. } \
  59. static ossl_inline int sk_##t1##_unshift(STACK_OF(t1) *sk, t2 *ptr) \
  60. { \
  61. return OPENSSL_sk_unshift((OPENSSL_STACK *)sk, (const void *)ptr); \
  62. } \
  63. static ossl_inline t2 *sk_##t1##_pop(STACK_OF(t1) *sk) \
  64. { \
  65. return (t2 *)OPENSSL_sk_pop((OPENSSL_STACK *)sk); \
  66. } \
  67. static ossl_inline t2 *sk_##t1##_shift(STACK_OF(t1) *sk) \
  68. { \
  69. return (t2 *)OPENSSL_sk_shift((OPENSSL_STACK *)sk); \
  70. } \
  71. static ossl_inline void sk_##t1##_pop_free(STACK_OF(t1) *sk, sk_##t1##_freefunc freefunc) \
  72. { \
  73. OPENSSL_sk_pop_free((OPENSSL_STACK *)sk, (OPENSSL_sk_freefunc)freefunc); \
  74. } \
  75. static ossl_inline int sk_##t1##_insert(STACK_OF(t1) *sk, t2 *ptr, int idx) \
  76. { \
  77. return OPENSSL_sk_insert((OPENSSL_STACK *)sk, (const void *)ptr, idx); \
  78. } \
  79. static ossl_inline t2 *sk_##t1##_set(STACK_OF(t1) *sk, int idx, t2 *ptr) \
  80. { \
  81. return (t2 *)OPENSSL_sk_set((OPENSSL_STACK *)sk, idx, (const void *)ptr); \
  82. } \
  83. static ossl_inline int sk_##t1##_find(STACK_OF(t1) *sk, t2 *ptr) \
  84. { \
  85. return OPENSSL_sk_find((OPENSSL_STACK *)sk, (const void *)ptr); \
  86. } \
  87. static ossl_inline int sk_##t1##_find_ex(STACK_OF(t1) *sk, t2 *ptr) \
  88. { \
  89. return OPENSSL_sk_find_ex((OPENSSL_STACK *)sk, (const void *)ptr); \
  90. } \
  91. static ossl_inline void sk_##t1##_sort(STACK_OF(t1) *sk) \
  92. { \
  93. OPENSSL_sk_sort((OPENSSL_STACK *)sk); \
  94. } \
  95. static ossl_inline int sk_##t1##_is_sorted(const STACK_OF(t1) *sk) \
  96. { \
  97. return OPENSSL_sk_is_sorted((const OPENSSL_STACK *)sk); \
  98. } \
  99. static ossl_inline STACK_OF(t1) * sk_##t1##_dup(const STACK_OF(t1) *sk) \
  100. { \
  101. return (STACK_OF(t1) *)OPENSSL_sk_dup((const OPENSSL_STACK *)sk); \
  102. } \
  103. static ossl_inline STACK_OF(t1) *sk_##t1##_deep_copy(const STACK_OF(t1) *sk, \
  104. sk_##t1##_copyfunc copyfunc, \
  105. sk_##t1##_freefunc freefunc) \
  106. { \
  107. return (STACK_OF(t1) *)OPENSSL_sk_deep_copy((const OPENSSL_STACK *)sk, \
  108. (OPENSSL_sk_copyfunc)copyfunc, \
  109. (OPENSSL_sk_freefunc)freefunc); \
  110. } \
  111. static ossl_inline sk_##t1##_compfunc sk_##t1##_set_cmp_func(STACK_OF(t1) *sk, sk_##t1##_compfunc compare) \
  112. { \
  113. return (sk_##t1##_compfunc)OPENSSL_sk_set_cmp_func((OPENSSL_STACK *)sk, (OPENSSL_sk_compfunc)compare); \
  114. }
  115. # define DEFINE_SPECIAL_STACK_OF(t1, t2) SKM_DEFINE_STACK_OF(t1, t2, t2)
  116. # define DEFINE_STACK_OF(t) SKM_DEFINE_STACK_OF(t, t, t)
  117. # define DEFINE_SPECIAL_STACK_OF_CONST(t1, t2) \
  118. SKM_DEFINE_STACK_OF(t1, const t2, t2)
  119. # define DEFINE_STACK_OF_CONST(t) SKM_DEFINE_STACK_OF(t, const t, t)
  120. /*-
  121. * Strings are special: normally an lhash entry will point to a single
  122. * (somewhat) mutable object. In the case of strings:
  123. *
  124. * a) Instead of a single char, there is an array of chars, NUL-terminated.
  125. * b) The string may have be immutable.
  126. *
  127. * So, they need their own declarations. Especially important for
  128. * type-checking tools, such as Deputy.
  129. *
  130. * In practice, however, it appears to be hard to have a const
  131. * string. For now, I'm settling for dealing with the fact it is a
  132. * string at all.
  133. */
  134. typedef char *OPENSSL_STRING;
  135. typedef const char *OPENSSL_CSTRING;
  136. /*-
  137. * Confusingly, LHASH_OF(STRING) deals with char ** throughout, but
  138. * STACK_OF(STRING) is really more like STACK_OF(char), only, as mentioned
  139. * above, instead of a single char each entry is a NUL-terminated array of
  140. * chars. So, we have to implement STRING specially for STACK_OF. This is
  141. * dealt with in the autogenerated macros below.
  142. */
  143. DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char)
  144. DEFINE_SPECIAL_STACK_OF_CONST(OPENSSL_CSTRING, char)
  145. /*
  146. * Similarly, we sometimes use a block of characters, NOT nul-terminated.
  147. * These should also be distinguished from "normal" stacks.
  148. */
  149. typedef void *OPENSSL_BLOCK;
  150. DEFINE_SPECIAL_STACK_OF(OPENSSL_BLOCK, void)
  151. # ifdef __cplusplus
  152. }
  153. # endif
  154. #endif