macros.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 \
  109. + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
  110. + ((OPENSSL_API_COMPAT >> 12) & 0xFF))
  111. # endif
  112. # endif
  113. /*
  114. * If OPENSSL_API_COMPAT wasn't given, we use default numbers to set
  115. * the API compatibility level.
  116. */
  117. # ifndef OPENSSL_API_LEVEL
  118. # if OPENSSL_CONFIGURED_API > 0
  119. # define OPENSSL_API_LEVEL (OPENSSL_CONFIGURED_API)
  120. # else
  121. # define OPENSSL_API_LEVEL \
  122. (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
  123. # endif
  124. # endif
  125. # if OPENSSL_API_LEVEL > OPENSSL_CONFIGURED_API
  126. # error "The requested API level higher than the configured API compatibility level"
  127. # endif
  128. /*
  129. * Check of sane values.
  130. */
  131. /* Can't go higher than the current version. */
  132. # if OPENSSL_API_LEVEL > (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
  133. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  134. # endif
  135. /* OpenSSL will have no version 2.y.z */
  136. # if OPENSSL_API_LEVEL < 30000 && OPENSSL_API_LEVEL >= 20000
  137. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  138. # endif
  139. /* Below 0.9.8 is unacceptably low */
  140. # if OPENSSL_API_LEVEL < 908
  141. # error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
  142. # endif
  143. /*
  144. * Define macros for deprecation and simulated removal purposes.
  145. *
  146. * The macros OSSL_DEPRECATED_{major}_{minor} are always defined for
  147. * all OpenSSL versions we care for. They can be used as attributes
  148. * in function declarations where appropriate.
  149. *
  150. * The macros OPENSSL_NO_DEPRECATED_{major}_{minor} are defined for
  151. * all OpenSSL versions up to or equal to the version given with
  152. * OPENSSL_API_COMPAT. They are used as guards around anything that's
  153. * deprecated up to that version, as an effect of the developer option
  154. * 'no-deprecated'.
  155. */
  156. # undef OPENSSL_NO_DEPRECATED_3_0
  157. # undef OPENSSL_NO_DEPRECATED_1_1_1
  158. # undef OPENSSL_NO_DEPRECATED_1_1_0
  159. # undef OPENSSL_NO_DEPRECATED_1_0_2
  160. # undef OPENSSL_NO_DEPRECATED_1_0_1
  161. # undef OPENSSL_NO_DEPRECATED_1_0_0
  162. # undef OPENSSL_NO_DEPRECATED_0_9_8
  163. # if OPENSSL_API_LEVEL >= 30000
  164. # ifndef OPENSSL_NO_DEPRECATED
  165. # define OSSL_DEPRECATEDIN_3_0 OSSL_DEPRECATED(3.0)
  166. # define OSSL_DEPRECATEDIN_3_0_FOR(msg) OSSL_DEPRECATED_FOR(3.0, msg)
  167. # else
  168. # define OPENSSL_NO_DEPRECATED_3_0
  169. # endif
  170. # else
  171. # define OSSL_DEPRECATEDIN_3_0
  172. # define OSSL_DEPRECATEDIN_3_0_FOR(msg)
  173. # endif
  174. # if OPENSSL_API_LEVEL >= 10101
  175. # ifndef OPENSSL_NO_DEPRECATED
  176. # define OSSL_DEPRECATEDIN_1_1_1 OSSL_DEPRECATED(1.1.1)
  177. # define OSSL_DEPRECATEDIN_1_1_1_FOR(msg) OSSL_DEPRECATED_FOR(1.1.1, msg)
  178. # else
  179. # define OPENSSL_NO_DEPRECATED_1_1_1
  180. # endif
  181. # else
  182. # define OSSL_DEPRECATEDIN_1_1_1
  183. # define OSSL_DEPRECATEDIN_1_1_1_FOR(msg)
  184. # endif
  185. # if OPENSSL_API_LEVEL >= 10100
  186. # ifndef OPENSSL_NO_DEPRECATED
  187. # define OSSL_DEPRECATEDIN_1_1_0 OSSL_DEPRECATED(1.1.0)
  188. # define OSSL_DEPRECATEDIN_1_1_0_FOR(msg) OSSL_DEPRECATED_FOR(1.1.0, msg)
  189. # else
  190. # define OPENSSL_NO_DEPRECATED_1_1_0
  191. # endif
  192. # else
  193. # define OSSL_DEPRECATEDIN_1_1_0
  194. # define OSSL_DEPRECATEDIN_1_1_0_FOR(msg)
  195. # endif
  196. # if OPENSSL_API_LEVEL >= 10002
  197. # ifndef OPENSSL_NO_DEPRECATED
  198. # define OSSL_DEPRECATEDIN_1_0_2 OSSL_DEPRECATED(1.0.2)
  199. # define OSSL_DEPRECATEDIN_1_0_2_FOR(msg) OSSL_DEPRECATED_FOR(1.0.2, msg)
  200. # else
  201. # define OPENSSL_NO_DEPRECATED_1_0_2
  202. # endif
  203. # else
  204. # define OSSL_DEPRECATEDIN_1_0_2
  205. # define OSSL_DEPRECATEDIN_1_0_2_FOR(msg)
  206. # endif
  207. # if OPENSSL_API_LEVEL >= 10001
  208. # ifndef OPENSSL_NO_DEPRECATED
  209. # define OSSL_DEPRECATEDIN_1_0_1 OSSL_DEPRECATED(1.0.1)
  210. # define OSSL_DEPRECATEDIN_1_0_1_FOR(msg) OSSL_DEPRECATED_FOR(1.0.1, msg)
  211. # else
  212. # define OPENSSL_NO_DEPRECATED_1_0_1
  213. # endif
  214. # else
  215. # define OSSL_DEPRECATEDIN_1_0_1
  216. # define OSSL_DEPRECATEDIN_1_0_1_FOR(msg)
  217. # endif
  218. # if OPENSSL_API_LEVEL >= 10000
  219. # ifndef OPENSSL_NO_DEPRECATED
  220. # define OSSL_DEPRECATEDIN_1_0_0 OSSL_DEPRECATED(1.0.0)
  221. # define OSSL_DEPRECATEDIN_1_0_0_FOR(msg) OSSL_DEPRECATED_FOR(1.0.0, msg)
  222. # else
  223. # define OPENSSL_NO_DEPRECATED_1_0_0
  224. # endif
  225. # else
  226. # define OSSL_DEPRECATEDIN_1_0_0
  227. # define OSSL_DEPRECATEDIN_1_0_0_FOR(msg)
  228. # endif
  229. # if OPENSSL_API_LEVEL >= 908
  230. # ifndef OPENSSL_NO_DEPRECATED
  231. # define OSSL_DEPRECATEDIN_0_9_8 OSSL_DEPRECATED(0.9.8)
  232. # define OSSL_DEPRECATEDIN_0_9_8_FOR(msg) OSSL_DEPRECATED_FOR(0.9.8, msg)
  233. # else
  234. # define OPENSSL_NO_DEPRECATED_0_9_8
  235. # endif
  236. # else
  237. # define OSSL_DEPRECATEDIN_0_9_8
  238. # define OSSL_DEPRECATEDIN_0_9_8_FOR(msg)
  239. # endif
  240. /*
  241. * Make our own variants of __FILE__ and __LINE__, depending on configuration
  242. */
  243. # ifndef OPENSSL_FILE
  244. # ifdef OPENSSL_NO_FILENAMES
  245. # define OPENSSL_FILE ""
  246. # define OPENSSL_LINE 0
  247. # else
  248. # define OPENSSL_FILE __FILE__
  249. # define OPENSSL_LINE __LINE__
  250. # endif
  251. # endif
  252. /*
  253. * __func__ was standardized in C99, so for any compiler that claims
  254. * to implement that language level or newer, we assume we can safely
  255. * use that symbol.
  256. *
  257. * GNU C also provides __FUNCTION__ since version 2, which predates
  258. * C99. We can, however, only use this if __STDC_VERSION__ exists,
  259. * as it's otherwise not allowed according to ISO C standards (C90).
  260. * (compiling with GNU C's -pedantic tells us so)
  261. *
  262. * If none of the above applies, we check if the compiler is MSVC,
  263. * and use __FUNCTION__ if that's the case.
  264. */
  265. # ifndef OPENSSL_FUNC
  266. # if defined(__STDC_VERSION__)
  267. # if __STDC_VERSION__ >= 199901L
  268. # define OPENSSL_FUNC __func__
  269. # elif defined(__GNUC__) && __GNUC__ >= 2
  270. # define OPENSSL_FUNC __FUNCTION__
  271. # endif
  272. # elif defined(_MSC_VER)
  273. # define OPENSSL_FUNC __FUNCTION__
  274. # endif
  275. /*
  276. * If all these possibilities are exhausted, we give up and use a
  277. * static string.
  278. */
  279. # ifndef OPENSSL_FUNC
  280. # define OPENSSL_FUNC "(unknown function)"
  281. # endif
  282. # endif
  283. #endif /* OPENSSL_MACROS_H */