mirror of
https://github.com/ckolivas/lrzip.git
synced 2025-12-06 07:12:00 +01:00
Add function to get a stream of random numbers into a buffer from /dev/urandom if possible, and falling back to random() if not.
This commit is contained in:
parent
e26d0d1381
commit
0ffa041f36
20
util.c
20
util.c
|
|
@ -45,7 +45,11 @@
|
|||
# define PAGE_SIZE (4096)
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include "lrzip_private.h"
|
||||
#include "liblrzip.h"
|
||||
|
||||
static const char *infile = NULL;
|
||||
static char delete_infile = 0;
|
||||
|
|
@ -122,3 +126,19 @@ void round_to_page(i64 *size)
|
|||
if (unlikely(!*size))
|
||||
*size = PAGE_SIZE;
|
||||
}
|
||||
|
||||
void get_rand(uchar *buf, int len)
|
||||
{
|
||||
int fd, i;
|
||||
|
||||
fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
for (i = 0; i < len; i++)
|
||||
buf[i] = (uchar)random();
|
||||
} else {
|
||||
if (unlikely(read(fd, buf, len) != len))
|
||||
fatal("Failed to read fd in get_rand\n");
|
||||
if (unlikely(close(fd)))
|
||||
fatal("Failed to close fd in get_rand\n");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue