diff --git a/rand.c b/rand.c index 6bb72c35da..2770c30ec4 100644 --- a/rand.c +++ b/rand.c @@ -22,7 +22,11 @@ */ #include +#ifdef _WIN32 +#include +#else #include +#endif #include "rand.h" @@ -30,14 +34,22 @@ static FILE *frand = NULL; int finalize_rand(void) { +#ifdef _WIN32 + return 0; +#else if (!frand) return 0; int err = fclose(frand); frand = NULL; return err; +#endif } uint32_t random32(void) { +#ifdef _WIN32 + srand((unsigned)time(NULL)); + return ((rand() % 0xFF) | ((rand() % 0xFF) << 8) | ((rand() % 0xFF) << 16) | ((rand() % 0xFF) << 24)); +#else uint32_t r; size_t len = sizeof(r); if (!frand) { @@ -47,6 +59,7 @@ uint32_t random32(void) (void)len_read; assert(len_read == len); return r; +#endif } uint32_t random_uniform(uint32_t n) @@ -58,12 +71,20 @@ uint32_t random_uniform(uint32_t n) void random_buffer(uint8_t *buf, size_t len) { +#ifdef _WIN32 + srand((unsigned)time(NULL)); + size_t i; + for (i = 0; i < len; i++) { + buf[i] = rand() % 0xFF; + } +#else if (!frand) { frand = fopen("/dev/urandom", "r"); } size_t len_read = fread(buf, 1, len, frand); (void)len_read; assert(len_read == len); +#endif } void random_permute(char *str, size_t len)