mirror of https://github.com/aria2/aria2
Use std::ifstream instead of file descriptor for generateRandomDataUrandom()
Usually required random bytes are short, it is advantageous to use std::ifstream which is buffered I/O rather than read() syscall.pull/8/head
parent
46e59c4cc3
commit
b6f8a3dbb3
26
src/util.cc
26
src/util.cc
|
@ -1265,18 +1265,10 @@ void generateRandomDataRandom(unsigned char* data, size_t length)
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void generateRandomDataUrandom(unsigned char* data, size_t length, int fd)
|
void generateRandomDataUrandom
|
||||||
|
(unsigned char* data, size_t length, std::ifstream& devUrand)
|
||||||
{
|
{
|
||||||
while(length > 0) {
|
devUrand.read(reinterpret_cast<char*>(data), length);
|
||||||
ssize_t r;
|
|
||||||
while((r = read(fd, data, length)) == -1 && errno == EINTR);
|
|
||||||
if(r <= 0) {
|
|
||||||
generateRandomDataRandom(data, length);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
length -= r;
|
|
||||||
data += r;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
@ -1286,17 +1278,17 @@ void generateRandomData(unsigned char* data, size_t length)
|
||||||
generateRandomDataRandom(data, length);
|
generateRandomDataRandom(data, length);
|
||||||
#else // !__MINGW32__
|
#else // !__MINGW32__
|
||||||
static int method = -1;
|
static int method = -1;
|
||||||
static int fd;
|
static std::ifstream devUrand;
|
||||||
if(method == 0) {
|
if(method == 0) {
|
||||||
generateRandomDataUrandom(data, length, fd);
|
generateRandomDataUrandom(data, length, devUrand);
|
||||||
} else if(method == 1) {
|
} else if(method == 1) {
|
||||||
generateRandomDataRandom(data, length);
|
generateRandomDataRandom(data, length);
|
||||||
} else {
|
} else {
|
||||||
while((fd = open("/dev/urandom", O_RDONLY)) == -1 && errno == EINTR);
|
devUrand.open("/dev/urandom");
|
||||||
if(fd == -1) {
|
if(devUrand) {
|
||||||
method = 1;
|
|
||||||
} else {
|
|
||||||
method = 0;
|
method = 0;
|
||||||
|
} else {
|
||||||
|
method = 1;
|
||||||
}
|
}
|
||||||
generateRandomData(data, length);
|
generateRandomData(data, length);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue