random_provider_detect_platform.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright (c) 2017 James E. King III
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // https://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Platform-specific random entropy provider platform detection
  9. //
  10. #ifndef BOOST_UUID_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP
  11. #define BOOST_UUID_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP
  12. #include <boost/predef/library/c/cloudabi.h>
  13. #include <boost/predef/library/c/gnu.h>
  14. #include <boost/predef/os/bsd/open.h>
  15. #include <boost/predef/os/windows.h>
  16. // Note: Don't use Boost.Predef to detect Linux and Android as it may give different results depending on header inclusion order.
  17. // https://github.com/boostorg/predef/issues/81#issuecomment-413329061
  18. #if (defined(__linux__) || defined(__linux) || defined(linux)) && (!defined(__ANDROID__) || __ANDROID_API__ >= 28)
  19. #include <sys/syscall.h>
  20. #if defined(SYS_getrandom)
  21. #define BOOST_UUID_RANDOM_PROVIDER_HAS_GETRANDOM
  22. #endif // defined(SYS_getrandom)
  23. #endif
  24. // On Linux, getentropy is implemented via getrandom. If we know that getrandom is not supported by the kernel, getentropy
  25. // will certainly not work, even if libc provides a wrapper function for it. There is no reason, ever, to use getentropy on that platform.
  26. #if !defined(BOOST_UUID_RANDOM_PROVIDER_DISABLE_GETENTROPY) && (defined(__linux__) || defined(__linux) || defined(linux) || defined(__ANDROID__))
  27. #define BOOST_UUID_RANDOM_PROVIDER_DISABLE_GETENTROPY
  28. #endif
  29. //
  30. // Platform Detection - will load in the correct header and
  31. // will define the class <tt>random_provider_base</tt>.
  32. //
  33. #if BOOST_OS_BSD_OPEN >= BOOST_VERSION_NUMBER(2, 1, 0) || BOOST_LIB_C_CLOUDABI
  34. # define BOOST_UUID_RANDOM_PROVIDER_ARC4RANDOM
  35. # define BOOST_UUID_RANDOM_PROVIDER_NAME arc4random
  36. #elif BOOST_OS_WINDOWS
  37. # include <boost/winapi/config.hpp>
  38. # if BOOST_WINAPI_PARTITION_APP_SYSTEM && \
  39. !defined(BOOST_UUID_RANDOM_PROVIDER_FORCE_WINCRYPT) && \
  40. !defined(_WIN32_WCE) && \
  41. (BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6)
  42. # define BOOST_UUID_RANDOM_PROVIDER_BCRYPT
  43. # define BOOST_UUID_RANDOM_PROVIDER_NAME bcrypt
  44. # elif BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  45. # define BOOST_UUID_RANDOM_PROVIDER_WINCRYPT
  46. # define BOOST_UUID_RANDOM_PROVIDER_NAME wincrypt
  47. # else
  48. # error Unable to find a suitable windows entropy provider
  49. # endif
  50. #elif defined(BOOST_UUID_RANDOM_PROVIDER_HAS_GETRANDOM) && !defined(BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX) && !defined(BOOST_UUID_RANDOM_PROVIDER_DISABLE_GETRANDOM)
  51. # define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM
  52. # define BOOST_UUID_RANDOM_PROVIDER_NAME getrandom
  53. #elif BOOST_LIB_C_GNU >= BOOST_VERSION_NUMBER(2, 25, 0) && !defined(BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX) && !defined(BOOST_UUID_RANDOM_PROVIDER_DISABLE_GETENTROPY)
  54. # define BOOST_UUID_RANDOM_PROVIDER_GETENTROPY
  55. # define BOOST_UUID_RANDOM_PROVIDER_NAME getentropy
  56. #else
  57. # define BOOST_UUID_RANDOM_PROVIDER_POSIX
  58. # define BOOST_UUID_RANDOM_PROVIDER_NAME posix
  59. #endif
  60. #define BOOST_UUID_RANDOM_PROVIDER_STRINGIFY2(X) #X
  61. #define BOOST_UUID_RANDOM_PROVIDER_STRINGIFY(X) BOOST_UUID_RANDOM_PROVIDER_STRINGIFY2(X)
  62. #if defined(BOOST_UUID_RANDOM_PROVIDER_SHOW)
  63. #pragma message("BOOST_UUID_RANDOM_PROVIDER_NAME " BOOST_UUID_RANDOM_PROVIDER_STRINGIFY(BOOST_UUID_RANDOM_PROVIDER_NAME))
  64. #endif
  65. #endif // BOOST_UUID_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP