X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fwin32_replacements.c;h=2a16500da67964bd8e87b3e293a46023a439263b;hp=26f4b4b7969398ea7c6b81adb696d803703cbcd5;hb=90f1e04a2a143876a4413577b25db60b5ba0fe97;hpb=bb3005f9b9fff1333900d8635ad9aeda1021eb94 diff --git a/src/win32_replacements.c b/src/win32_replacements.c index 26f4b4b7..2a16500d 100644 --- a/src/win32_replacements.c +++ b/src/win32_replacements.c @@ -439,7 +439,7 @@ win32_rename_replacement(const wchar_t *srcpath, const wchar_t *dstpath) p = tmpname; p = wmempcpy(p, dstpath, dstlen); p = wmempcpy(p, orig_suffix, ARRAY_LEN(orig_suffix)); - randomize_char_array_with_alnum(p, num_rand_chars); + get_random_alnum_chars(p, num_rand_chars); p += num_rand_chars; *p = L'\0'; } @@ -748,4 +748,30 @@ win32_open_logfile(const wchar_t *path) return fp; } +#define RtlGenRandom SystemFunction036 +BOOLEAN WINAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength); + +/* + * Generate @n cryptographically secure random bytes (thread-safe) + * + * This is the Windows version. It uses RtlGenRandom() (actually called + * SystemFunction036) from advapi32.dll. + */ +void +get_random_bytes(void *p, size_t n) +{ + while (n != 0) { + u32 count = min(n, UINT32_MAX); + + if (!RtlGenRandom(p, count)) { + win32_error(GetLastError(), + L"RtlGenRandom() failed (count=%u)", count); + wimlib_assert(0); + count = 0; + } + p += count; + n -= count; + } +} + #endif /* __WIN32__ */