diff --git a/configure.ac b/configure.ac index 10acaba027..a105f3c6a2 100644 --- a/configure.ac +++ b/configure.ac @@ -911,6 +911,9 @@ AC_CHECK_FUNCS([gettimeofday], AC_CHECK_FUNCS([strptime], [AM_CONDITIONAL([HAVE_STRPTIME], true)], [AM_CONDITIONAL([HAVE_STRPTIME], false)]) +AC_CHECK_FUNCS([getentropy], + [AM_CONDITIONAL([HAVE_GETENTROPY], true)], + [AM_CONDITIONAL([HAVE_GETENTROPY], false)]) AC_CHECK_FUNCS([daemon], [have_daemon=yes]) AM_CONDITIONAL([HAVE_DAEMON], [test "x$have_daemon" = "xyes"]) diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc index 8dbef82fa9..7748ba0d6b 100644 --- a/src/SimpleRandomizer.cc +++ b/src/SimpleRandomizer.cc @@ -106,6 +106,20 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len) auto iter = len / blocklen; auto p = buf; +#if !HAVE_GETENTROPY + auto getentropy = [this](void *buffer, size_t length) { + auto buf = reinterpret_cast(buffer); + auto dis = std::uniform_int_distribution(); + for (size_t q = length / sizeof(unsigned int); q > 0; --q, ++buf) { + *buf = dis(gen_); + } + const size_t r = length % sizeof(unsigned int); + auto last = dis(gen_); + memcpy(buf, &last, r); + return 0; + }; +#endif // !HAVE_GETENTROPY + for (size_t i = 0; i < iter; ++i) { auto rv = getentropy(p, blocklen); if (rv != 0) { @@ -128,7 +142,7 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len) assert(0); abort(); } -#endif // ! __MINGW32__ +#endif // !__MINGW32__ && !__APPLE__ } } // namespace aria2