X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/9f8f8b1e1d553774865f2c177191c359c3dc652c..2b7a0f47ea3ecea7b50c3052e854eae0d306da80:/sbr/m_rand.c diff --git a/sbr/m_rand.c b/sbr/m_rand.c index 4d248886..1accdc89 100644 --- a/sbr/m_rand.c +++ b/sbr/m_rand.c @@ -1,27 +1,35 @@ -/* - * m_rand -- provides pseudorandom bytes +/* m_rand.c -- provides pseudorandom bytes * * This code is Copyright (c) 2012, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. */ -#include /* for abs(), srand(), rand() */ -#include /* for fopen(), fread(), fclose() */ -#include /* for getpid() */ -#include /* for time() */ +#include +#include +#include +#include +#include -static int seeded = 0; +#include "config.h" +#include "m_rand.h" + +#if !HAVE_ARC4RANDOM +static bool seeded = false; +#endif int -m_rand (unsigned char *buf, size_t n) { +m_rand (unsigned char *buf, size_t n) +{ +#if !HAVE_ARC4RANDOM if (! seeded) { FILE *devurandom; unsigned int seed; if ((devurandom = fopen ("/dev/urandom", "r"))) { - if (fread (&seed, sizeof (seed), 1, devurandom) == 1) seeded = 1; + if (fread (&seed, sizeof (seed), 1, devurandom) == 1) + seeded = true; fclose (devurandom); } @@ -31,7 +39,7 @@ m_rand (unsigned char *buf, size_t n) { arXiv:1005.4117v1 [physics.comp-ph], 22 May 2010, p. 19. time() and getpid() shouldn't fail on POSIX platforms. */ seed = abs ((int) ((time (0) * 181 * ((getpid ()-83) * 359)) % 104729)); - seeded = 1; + seeded = true; } srand (seed); @@ -46,6 +54,9 @@ m_rand (unsigned char *buf, size_t n) { *buf++ = *rndp++; } } +#else + arc4random_buf(buf, n); +#endif return 0; }