Skip to content

Commit 7845991

Browse files
committed
implement glib_random_bytes for msvc
1 parent a20d7be commit 7845991

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

qemu/util/guest-random.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
#include "qemu/guest-random.h"
1616
#include "crypto/random.h"
1717
//#include "sysemu/replay.h"
18-
18+
#include <stdlib.h>
19+
#include <string.h>
1920

2021
#ifndef _MSC_VER
2122
static __thread GRand *thread_rand;
23+
#else
24+
static bool initialized = false;
2225
#endif
2326
static bool deterministic = true;
2427

25-
2628
static int glib_random_bytes(void *buf, size_t len)
2729
{
2830
#ifndef _MSC_VER
@@ -43,6 +45,26 @@ static int glib_random_bytes(void *buf, size_t len)
4345
x = g_rand_int(rand);
4446
__builtin_memcpy(buf + i, &x, i - len);
4547
}
48+
#else
49+
uint32_t x;
50+
size_t i;
51+
52+
if (!initialized) {
53+
if (deterministic) {
54+
srand(0);
55+
} else {
56+
srand(time(NULL));
57+
}
58+
59+
for (i = 0; i + 4 <= len; i += 4) {
60+
x = rand();
61+
memcpy(buf + i, &x, 4);
62+
}
63+
if (i < len) {
64+
x = rand();
65+
memcpy(buf + i, &x, i - len);
66+
}
67+
}
4668
#endif
4769
return 0;
4870
}

0 commit comments

Comments
 (0)