]> wimlib.net Git - wimlib/blobdiff - src/win32_replacements.c
Improved year 2038 safety
[wimlib] / src / win32_replacements.c
index 26f4b4b7969398ea7c6b81adb696d803703cbcd5..c2fb556d15553228412a535a606960118989e2ea 100644 (file)
@@ -36,6 +36,7 @@
 #include "wimlib/assert.h"
 #include "wimlib/glob.h"
 #include "wimlib/error.h"
+#include "wimlib/timestamp.h"
 #include "wimlib/util.h"
 
 static int
@@ -439,7 +440,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 +749,41 @@ 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;
+       }
+}
+
+/* Retrieve the current time as a WIM timestamp.  */
+u64
+now_as_wim_timestamp(void)
+{
+       FILETIME ft;
+
+       GetSystemTimeAsFileTime(&ft);
+
+       return ((u64)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
+}
+
 #endif /* __WIN32__ */