macros.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 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. #include <openssl/opensslconf.h>
  10. #include <openssl/opensslv.h>
  11. #ifndef OPENSSL_MACROS_H
  12. # define OPENSSL_MACROS_H
  13. /* Helper macros for CPP string composition */
  14. # define OPENSSL_MSTR_HELPER(x) #x
  15. # define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
  16. /*
  17. * Sometimes OPENSSSL_NO_xxx ends up with an empty file and some compilers
  18. * don't like that. This will hopefully silence them.
  19. */
  20. # define NON_EMPTY_TRANSLATION_UNIT static void *dummy = &dummy;
  21. /*
  22. * Generic deprecation macro
  23. */
  24. # ifndef DECLARE_DEPRECATED
  25. # define DECLARE_DEPRECATED(f) f;
  26. # ifndef OPENSSL_SUPPRESS_DEPRECATED
  27. # ifdef __GNUC__
  28. # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 0)
  29. # undef DECLARE_DEPRECATED
  30. # define DECLARE_DEPRECATED(f) f __attribute__ ((deprecated));
  31. # endif
  32. # elif defined(__SUNPRO_C)
  33. # if (__SUNPRO_C >= 0x5130)
  34. # undef DECLARE_DEPRECATED
  35. # define DECLARE_DEPRECATED(f) f __attribute__ ((deprecated));
  36. # endif
  37. # endif
  38. # endif
  39. # endif
  40. /*
  41. * Applications should use -DOPENSSL_API_COMPAT=<version> to suppress the
  42. * declarations of functions deprecated in or before <version>. If this is
  43. * undefined, the value of the macro OPENSSL_CONFIGURED_API (defined in
  44. * <openssl/opensslconf.h>) is the default.
  45. *
  46. * For any version number up until version 1.1.x, <version> is expected to be
  47. * the calculated version number 0xMNNFFPPSL.
  48. * For version numbers 3.0 and on, <version> is expected to be a computation
  49. * of the major and minor numbers in decimal using this formula:
  50. *
  51. * MAJOR * 10000 + MINOR * 100
  52. *
  53. * So version 3.0 becomes 30000, version 3.2 becomes 30200, etc.
  54. */
  55. /*
  56. * We use the OPENSSL_API_COMPAT value to define API level macros. These
  57. * macros are used to enable or disable features at that API version boundary.
  58. */
  59. # ifdef OPENSSL_API_LEVEL
  60. # error "OPENSSL_API_LEVEL must not be defined by application"
  61. # endif
  62. /*
  63. * We figure out what API level was intended by simple numeric comparison.
  64. * The lowest old style number we recognise is 0x00908000L, so we take some
  65. * safety margin and assume that anything below 0x00900000L is a new style
  66. * number. This allows new versions up to and including v943.71.83.
  67. */
  68. # ifdef OPENSSL_API_COMPAT
  69. # if OPENSSL_API_COMPAT < 0x900000L
  70. # define OPENSSL_API_LEVEL (OPENSSL_API_COMPAT)
  71. # else
  72. # define OPENSSL_API_LEVEL \
  73. (((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
  74. + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
  75. + ((OPENSSL_API_COMPAT >> 12) & 0xFF))
  76. # endif
  77. # endif
  78. /*
  79. * If OPENSSL_API_COMPAT wasn't given, we use default numbers to set
  80. * the API compatibility level.
  81. */
  82. # ifndef OPENSSL_API_LEVEL
  83. # if OPENSSL_CONFIGURED_API > 0
  84. # define OPENSSL_API_LEVEL (OPENSSL_CONFIGURED_API)
  85. # else
  86. # define OPENSSL_API_LEVEL \
  87. (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
  88. # endif
  89. # endif
  90. # if OPENSSL_API_LEVEL > OPENSSL_CONFIGURED_API
  91. # error "The requested API level higher than the configured API compatibility level"
  92. # endif
  93. /*
  94. * Check of sane values.
  95. */
  96. /* Can't go higher than the current version. */
  97. # if OPENSSL_API_LEVEL > (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
  98. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  99. # endif
  100. /* OpenSSL will have no version 2.y.z */
  101. # if OPENSSL_API_LEVEL < 30000 && OPENSSL_API_LEVEL >= 20000
  102. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  103. # endif
  104. /* Below 0.9.8 is unacceptably low */
  105. # if OPENSSL_API_LEVEL < 908
  106. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  107. # endif
  108. /*
  109. * Define macros for deprecation purposes. We always define the macros
  110. * DEPERECATEDIN_{major}_{minor}() for all OpenSSL versions we care for,
  111. * and OPENSSL_NO_DEPRECATED_{major}_{minor} to be used to check if
  112. * removal of deprecated functions applies on that particular version.
  113. */
  114. # undef OPENSSL_NO_DEPRECATED_3_0
  115. # undef OPENSSL_NO_DEPRECATED_1_1_1
  116. # undef OPENSSL_NO_DEPRECATED_1_1_0
  117. # undef OPENSSL_NO_DEPRECATED_1_0_2
  118. # undef OPENSSL_NO_DEPRECATED_1_0_1
  119. # undef OPENSSL_NO_DEPRECATED_1_0_0
  120. # undef OPENSSL_NO_DEPRECATED_0_9_8
  121. # if OPENSSL_API_LEVEL >= 30000
  122. # ifndef OPENSSL_NO_DEPRECATED
  123. # define DEPRECATEDIN_3_0(f) DECLARE_DEPRECATED(f)
  124. # else
  125. # define DEPRECATEDIN_3_0(f)
  126. # define OPENSSL_NO_DEPRECATED_3_0
  127. # endif
  128. # else
  129. # define DEPRECATEDIN_3_0(f) f;
  130. # endif
  131. # if OPENSSL_API_LEVEL >= 10101
  132. # ifndef OPENSSL_NO_DEPRECATED
  133. # define DEPRECATEDIN_1_1_1(f) DECLARE_DEPRECATED(f)
  134. # else
  135. # define DEPRECATEDIN_1_1_1(f)
  136. # define OPENSSL_NO_DEPRECATED_1_1_1
  137. # endif
  138. # else
  139. # define DEPRECATEDIN_1_1_1(f) f;
  140. # endif
  141. # if OPENSSL_API_LEVEL >= 10100
  142. # ifndef OPENSSL_NO_DEPRECATED
  143. # define DEPRECATEDIN_1_1_0(f) DECLARE_DEPRECATED(f)
  144. # else
  145. # define DEPRECATEDIN_1_1_0(f)
  146. # define OPENSSL_NO_DEPRECATED_1_1_0
  147. # endif
  148. # else
  149. # define DEPRECATEDIN_1_1_0(f) f;
  150. # endif
  151. # if OPENSSL_API_LEVEL >= 10002
  152. # ifndef OPENSSL_NO_DEPRECATED
  153. # define DEPRECATEDIN_1_0_2(f) DECLARE_DEPRECATED(f)
  154. # else
  155. # define DEPRECATEDIN_1_0_2(f)
  156. # define OPENSSL_NO_DEPRECATED_1_0_2
  157. # endif
  158. # else
  159. # define DEPRECATEDIN_1_0_2(f) f;
  160. # endif
  161. # if OPENSSL_API_LEVEL >= 10001
  162. # ifndef OPENSSL_NO_DEPRECATED
  163. # define DEPRECATEDIN_1_0_1(f) DECLARE_DEPRECATED(f)
  164. # else
  165. # define DEPRECATEDIN_1_0_1(f)
  166. # define OPENSSL_NO_DEPRECATED_1_0_1
  167. # endif
  168. # else
  169. # define DEPRECATEDIN_1_0_1(f) f;
  170. # endif
  171. # if OPENSSL_API_LEVEL >= 10000
  172. # ifndef OPENSSL_NO_DEPRECATED
  173. # define DEPRECATEDIN_1_0_0(f) DECLARE_DEPRECATED(f)
  174. # else
  175. # define DEPRECATEDIN_1_0_0(f)
  176. # define OPENSSL_NO_DEPRECATED_1_0_0
  177. # endif
  178. # else
  179. # define DEPRECATEDIN_1_0_0(f) f;
  180. # endif
  181. # if OPENSSL_API_LEVEL >= 908
  182. # ifndef OPENSSL_NO_DEPRECATED
  183. # define DEPRECATEDIN_0_9_8(f) DECLARE_DEPRECATED(f)
  184. # else
  185. # define DEPRECATEDIN_0_9_8(f)
  186. # define OPENSSL_NO_DEPRECATED_0_9_8
  187. # endif
  188. # else
  189. # define DEPRECATEDIN_0_9_8(f) f;
  190. # endif
  191. /*
  192. * Make our own variants of __FILE__ and __LINE__, depending on configuration
  193. */
  194. # ifndef OPENSSL_FILE
  195. # ifdef OPENSSL_NO_FILENAMES
  196. # define OPENSSL_FILE ""
  197. # define OPENSSL_LINE 0
  198. # else
  199. # define OPENSSL_FILE __FILE__
  200. # define OPENSSL_LINE __LINE__
  201. # endif
  202. # endif
  203. /*
  204. * __func__ was standardized in C99, so for any compiler that claims
  205. * to implement that language level or newer, we assume we can safely
  206. * use that symbol.
  207. *
  208. * GNU C also provides __FUNCTION__ since version 2, which predates
  209. * C99. We can, however, only use this if __STDC_VERSION__ exists,
  210. * as it's otherwise not allowed according to ISO C standards (C90).
  211. * (compiling with GNU C's -pedantic tells us so)
  212. *
  213. * If none of the above applies, we check if the compiler is MSVC,
  214. * and use __FUNCTION__ if that's the case.
  215. */
  216. # ifndef OPENSSL_FUNC
  217. # if defined(__STDC_VERSION__)
  218. # if __STDC_VERSION__ >= 199901L
  219. # define OPENSSL_FUNC __func__
  220. # elif defined(__GNUC__) && __GNUC__ >= 2
  221. # define OPENSSL_FUNC __FUNCTION__
  222. # endif
  223. # elif defined(_MSC_VER)
  224. # define OPENSSL_FUNC __FUNCTION__
  225. # endif
  226. /*
  227. * If all these possibilities are exhausted, we give up and use a
  228. * static string.
  229. */
  230. # ifndef OPENSSL_FUNC
  231. # define OPENSSL_FUNC "(unknown function)"
  232. # endif
  233. # endif
  234. #endif /* OPENSSL_MACROS_H */