Provide fallback implementation for getentropy()

Commit ba3396f changed getRandomBytes() implementation from c++ std
uniform distribution to getentropy(). The getentropy() function first
appeared in glibc 2.25. In order to provide backward compatibility for
older glibc (e.g. Android SDK <= 19) restore previous getRandomBytes()
implementation as a fallback.
pull/2000/head
Arkadiusz Bokowy 2022-11-06 23:19:07 +01:00
parent f4cbc7bb31
commit 71b1b4634f
1 changed files with 15 additions and 1 deletions

View File

@ -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<unsigned int*>(buffer);
auto dis = std::uniform_int_distribution<unsigned int>();
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