#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) */
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;