File indexing completed on 2025-01-18 09:53:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <boost/config.hpp>
0012 #include <boost/throw_exception.hpp>
0013 #include <boost/predef/library/c/gnu.h>
0014 #include <cerrno>
0015 #include <cstddef>
0016
0017 #if !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM) && !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_DISABLE_LIBC_WRAPPER)
0018 #if BOOST_LIB_C_GNU >= BOOST_VERSION_NUMBER(2, 25, 0)
0019 #define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER
0020 #elif defined(__has_include)
0021 #if __has_include(<sys/random.h>)
0022 #define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER
0023 #endif
0024 #endif
0025 #endif
0026
0027 #if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER)
0028 #include <sys/random.h>
0029 #else
0030 #include <stddef.h> // ssize_t
0031 #endif
0032
0033 namespace boost {
0034 namespace uuids {
0035 namespace detail {
0036
0037 class random_provider_base
0038 {
0039 public:
0040
0041
0042
0043 void get_random_bytes(void *buf, std::size_t siz)
0044 {
0045 std::size_t offset = 0;
0046 while (offset < siz)
0047 {
0048 ssize_t sz = get_random(static_cast< char* >(buf) + offset, siz - offset, 0u);
0049
0050 if (BOOST_UNLIKELY(sz < 0))
0051 {
0052 int err = errno;
0053 if (err == EINTR)
0054 continue;
0055 BOOST_THROW_EXCEPTION(entropy_error(err, "getrandom"));
0056 }
0057
0058 offset += sz;
0059 }
0060 }
0061
0062 private:
0063 static ssize_t get_random(void *buf, std::size_t size, unsigned int flags)
0064 {
0065 #if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM)
0066 return BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM(buf, size, flags);
0067 #elif defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER)
0068 return ::getrandom(buf, size, flags);
0069 #else
0070 return ::syscall(SYS_getrandom, buf, size, flags);
0071 #endif
0072 }
0073 };
0074
0075 }
0076 }
0077 }