]> pd.if.org Git - uuid/commitdiff
Clean up of random number generator.
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 14 Sep 2012 06:52:55 +0000 (06:52 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 14 Sep 2012 06:52:55 +0000 (06:52 +0000)
rng.c

diff --git a/rng.c b/rng.c
index 89eed8964ef7d5a322da81c2faede52c93d0bb57..5a5155946829fe0c87781f86ae2d4822cd1bef9d 100644 (file)
--- a/rng.c
+++ b/rng.c
@@ -6,41 +6,37 @@
 
 #include <stdio.h>
 
-#define LTC_ARGCHK(x)
-#define TRY_URANDOM_FIRST 1
-
 #ifndef WIN32
-#define LTC_DEVRANDOM 1
+#define USE_DEVRANDOM 1
 #endif
 
-#ifdef LTC_DEVRANDOM
+#ifdef USE_DEVRANDOM
+#include <unistd.h>
 /* on *NIX read /dev/random */
 static unsigned long rng_nix(unsigned char *buf, unsigned long len) {
-#ifdef LTC_NO_FILE
-       return 0;
-#else
        FILE           *f;
-       unsigned long   x;
-#ifdef TRY_URANDOM_FIRST
+       size_t   x;
+
        f = fopen("/dev/urandom", "rb");
-       if (f == NULL)
-#endif
+       if (f == NULL) {
                f = fopen("/dev/random", "rb");
+       }
 
        if (f == NULL) {
                return 0;
        }
+
        /* disable buffering */
        if (setvbuf(f, NULL, _IONBF, 0) != 0) {
                fclose(f);
                return 0;
        }
-       x = (unsigned long) fread(buf, 1, (size_t) len, f);
+
+       x = fread(buf, 1, (size_t) len, f);
        fclose(f);
+
        return x;
-#endif
 }
-
 #endif
 
 /* TODO remove this.  It's too slow really (I think) */
@@ -115,21 +111,19 @@ static unsigned long rng_win32(unsigned char *buf, unsigned long len) {
 unsigned long   pd_uuid_rng_get_bytes(unsigned char *out, unsigned long outlen) {
        unsigned long   x;
 
-       LTC_ARGCHK(out != NULL);
+       if (!out) return 0;
 
-#if defined(LTC_DEVRANDOM)
+#if defined(USE_DEVRANDOM)
        x = rng_nix(out, outlen);
        if (x != 0) {
                return x;
        }
-#endif
-#ifdef WIN32
+#elif defined(WIN32)
        x = rng_win32(out, outlen);
        if (x != 0) {
                return x;
        }
-#endif
-#ifdef ANSI_RNG
+#elif defined(ANSI_RNG)
        x = rng_ansic(out, outlen);
        if (x != 0) {
                return x;