]> wimlib.net Git - wimlib/blobdiff - src/util.c
v1.14.4
[wimlib] / src / util.c
index 47aedd428f79e5358e0feb83978a661cd55d5451..52af147d3f122fe5240cced3ea3484885017234b 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2012-2016 Eric Biggers
+ * Copyright 2012-2023 Eric Biggers
  *
  * This file is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free
@@ -16,7 +16,7 @@
  * details.
  *
  * You should have received a copy of the GNU Lesser General Public License
- * along with this file; if not, see http://www.gnu.org/licenses/.
+ * along with this file; if not, see https://www.gnu.org/licenses/.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -24,6 +24,7 @@
 #endif
 
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,6 +33,9 @@
 #  include <sys/types.h>
 #  include <sys/sysctl.h>
 #endif
+#ifdef HAVE_SYS_SYSCALL_H
+#  include <sys/syscall.h>
+#endif
 #include <unistd.h>
 
 #include "wimlib.h"
@@ -39,7 +43,6 @@
 #include "wimlib/error.h"
 #include "wimlib/timestamp.h"
 #include "wimlib/util.h"
-#include "wimlib/xml.h"
 
 /*******************
  * Memory allocation
@@ -102,7 +105,7 @@ wimlib_strdup(const char *str)
        return memdup(str, strlen(str) + 1);
 }
 
-#ifdef __WIN32__
+#ifdef _WIN32
 wchar_t *
 wimlib_wcsdup(const wchar_t *str)
 {
@@ -149,9 +152,6 @@ wimlib_set_memory_allocator(void *(*malloc_func)(size_t),
        wimlib_malloc_func  = malloc_func  ? malloc_func  : malloc;
        wimlib_free_func    = free_func    ? free_func    : free;
        wimlib_realloc_func = realloc_func ? realloc_func : realloc;
-
-       xml_set_memory_allocator(wimlib_malloc_func, wimlib_free_func,
-                                wimlib_realloc_func);
        return 0;
 }
 
@@ -166,43 +166,112 @@ void *mempcpy(void *dst, const void *src, size_t n)
 }
 #endif
 
-static bool seeded = false;
+/**************************
+ * Random number generation
+ **************************/
 
-static void
-seed_random(void)
+#ifndef _WIN32
+/*
+ * Generate @n cryptographically secure random bytes (thread-safe)
+ *
+ * This is the UNIX version.  It uses the Linux getrandom() system call if
+ * available; otherwise, it falls back to reading from /dev/urandom.
+ */
+void
+get_random_bytes(void *p, size_t n)
 {
-       srand(now_as_wim_timestamp());
-       seeded = true;
+       if (n == 0)
+               return;
+#ifdef __NR_getrandom
+       static bool getrandom_unavailable;
+
+       if (getrandom_unavailable)
+               goto try_dev_urandom;
+       do {
+               int res = syscall(__NR_getrandom, p, n, 0);
+               if (unlikely(res < 0)) {
+                       if (errno == ENOSYS) {
+                               getrandom_unavailable = true;
+                               goto try_dev_urandom;
+                       }
+                       if (errno == EINTR)
+                               continue;
+                       ERROR_WITH_ERRNO("getrandom() failed");
+                       wimlib_assert(0);
+                       res = 0;
+               }
+               p += res;
+               n -= res;
+       } while (n != 0);
+       return;
+
+try_dev_urandom:
+       ;
+#endif /* __NR_getrandom */
+       int fd = open("/dev/urandom", O_RDONLY);
+       if (fd < 0) {
+               ERROR_WITH_ERRNO("Unable to open /dev/urandom");
+               wimlib_assert(0);
+       }
+       do {
+               int res = read(fd, p, min(n, INT_MAX));
+               if (unlikely(res < 0)) {
+                       if (errno == EINTR)
+                               continue;
+                       ERROR_WITH_ERRNO("Error reading from /dev/urandom");
+                       wimlib_assert(0);
+                       res = 0;
+               }
+               p += res;
+               n -= res;
+       } while (n != 0);
+       close(fd);
 }
+#endif /* !_WIN32 */
 
-/* Fills @n characters pointed to by @p with random alphanumeric characters. */
+/*
+ * Generate @n cryptographically secure random alphanumeric characters
+ * (thread-safe)
+ *
+ * This is implemented on top of get_random_bytes().  For efficiency the calls
+ * to get_random_bytes() are batched.
+ */
 void
-randomize_char_array_with_alnum(tchar *p, size_t n)
+get_random_alnum_chars(tchar *p, size_t n)
 {
-       if (!seeded)
-               seed_random();
-       while (n--) {
-               int r = rand() % 62;
-               if (r < 26)
-                       *p++ = r + 'a';
-               else if (r < 52)
-                       *p++ = r - 26 + 'A';
+       u32 r[64];
+       int r_idx = 0;
+       int r_end = 0;
+
+       for (; n != 0; p++, n--) {
+               tchar x;
+
+               if (r_idx >= r_end) {
+                       r_idx = 0;
+                       r_end = min(n, ARRAY_LEN(r));
+                       get_random_bytes(r, r_end * sizeof(r[0]));
+               }
+
+               STATIC_ASSERT(sizeof(r[0]) == sizeof(u32));
+               while (unlikely(r[r_idx] >= UINT32_MAX - (UINT32_MAX % 62)))
+                       get_random_bytes(&r[r_idx], sizeof(r[0]));
+
+               x = r[r_idx++] % 62;
+
+               if (x < 26)
+                       *p = 'a' + x;
+               else if (x < 52)
+                       *p = 'A' + x - 26;
                else
-                       *p++ = r - 52 + '0';
+                       *p = '0' + x - 52;
        }
 }
 
-/* Fills @n bytes pointer to by @p with random numbers. */
-void
-randomize_byte_array(u8 *p, size_t n)
-{
-       if (!seeded)
-               seed_random();
-       while (n--)
-               *p++ = rand();
-}
+/************************
+ * System information
+ ************************/
 
-#ifndef __WIN32__
+#ifndef _WIN32
 unsigned
 get_available_cpus(void)
 {
@@ -213,9 +282,9 @@ get_available_cpus(void)
        }
        return n;
 }
-#endif /* !__WIN32__ */
+#endif /* !_WIN32 */
 
-#ifndef __WIN32__
+#ifndef _WIN32
 u64
 get_available_memory(void)
 {
@@ -238,4 +307,4 @@ default_size:
        WARNING("Failed to determine available memory; assuming 1 GiB");
        return (u64)1 << 30;
 }
-#endif /* !__WIN32__ */
+#endif /* !_WIN32 */