You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

macros.h 9.5 kB

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