]> mj.ucw.cz Git - libucw.git/commitdiff
Random: Switched flags and buf_size arguments of strongrand (slightly nicer usage).
authorPavel Charvat <pchar@ucw.cz>
Wed, 23 Mar 2022 07:42:09 +0000 (07:42 +0000)
committerPavel Charvat <pchar@ucw.cz>
Wed, 23 Mar 2022 07:42:09 +0000 (07:42 +0000)
ucw/random-strong.c
ucw/random-test.c
ucw/random.h

index 760a6e65178c753f16caba80ee9fb508c90e3306..4391b736c89be1a3afe584767eb872123ebc6fa7 100644 (file)
@@ -246,7 +246,7 @@ static void strongrand_std_read_buffered(struct strongrand *sr, byte *ptr, int s
     }
 }
 
-static struct strongrand *strongrand_std_open_internal(int fd, const char *name, uint buf_size, uint flags)
+static struct strongrand *strongrand_std_open_internal(int fd, const char *name, uint flags, uint buf_size)
 {
   struct strongrand_std *srs = xmalloc(sizeof(*srs) + buf_size);
   bzero(srs, sizeof(*srs));
@@ -262,7 +262,7 @@ static struct strongrand *strongrand_std_open_internal(int fd, const char *name,
   return &srs->sr;
 }
 
-struct strongrand *strongrand_std_open(uint buf_size, uint flags)
+struct strongrand *strongrand_std_open(uint flags, uint buf_size)
 {
   const char *name;
   if (flags & STRONGRAND_STD_URANDOM)
@@ -277,9 +277,9 @@ struct strongrand *strongrand_std_open(uint buf_size, uint flags)
     }
 #ifdef CONFIG_UCW_GETRANDOM
   if (strongrand_getrandom_detect())
-    return strongrand_std_open_internal(STRONGRAND_FD_KERNEL_GETRANDOM, name, buf_size, flags);
+    return strongrand_std_open_internal(STRONGRAND_FD_KERNEL_GETRANDOM, name, flags, buf_size);
 #endif
-  struct strongrand *sr = strongrand_std_open_internal(STRONGRAND_FD_KERNEL_DEVICE, name, buf_size, flags);
+  struct strongrand *sr = strongrand_std_open_internal(STRONGRAND_FD_KERNEL_DEVICE, name, flags, buf_size);
   if (!(flags & STRONGRAND_STD_DELAYED))
     strongrand_std_open_device((struct strongrand_std *)sr);
   return sr;
index 2fd375956b1c2e08b8008aa551d39e0d72a4d867..59d54b6ca032a7a1d8da5225f11700e5f2d7f724 100644 (file)
@@ -88,7 +88,7 @@ static void test_strong(void)
   byte buf[100];
   for (uint j = 0; j < 2; j++)
     {
-      struct strongrand *r = strongrand_std_open((j == 0) ? 0 : 128, STRONGRAND_STD_URANDOM);
+      struct strongrand *r = strongrand_std_open(STRONGRAND_STD_URANDOM, (j == 0) ? 0 : 128);
       for (uint i = 1; i <= 100; i++)
        {
          strongrand_mem(r, buf, i);
@@ -216,19 +216,19 @@ static void bench_strong(void)
   struct strongrand *r;
 
   msg(L_INFO, "Benchmarking unbuffered strong random generator");
-  r = strongrand_std_open(0, STRONGRAND_STD_URANDOM);
+  r = strongrand_std_open(STRONGRAND_STD_URANDOM, 0);
   BENCH_SLOW(50, strongrand_mem(r, buf, 1));
   BENCH_SLOW(200, strongrand_mem(r, buf, 100));
   strongrand_close(r);
 
   msg(L_INFO, "Benchmarking buffered strong random generator");
-  r = strongrand_std_open(128, STRONGRAND_STD_URANDOM);
+  r = strongrand_std_open(STRONGRAND_STD_URANDOM, 128);
   BENCH_SLOW(50, strongrand_mem(r, buf, 1));
   BENCH_SLOW(200, strongrand_mem(r, buf, 100));
   strongrand_close(r);
 
   msg(L_INFO, "Benchmarking buffered strong random generator with autoreset");
-  r = strongrand_std_open(128, STRONGRAND_STD_URANDOM | STRONGRAND_STD_AUTO_RESET);
+  r = strongrand_std_open(STRONGRAND_STD_URANDOM | STRONGRAND_STD_AUTO_RESET, 128);
   BENCH_SLOW(50, strongrand_mem(r, buf, 1));
   BENCH_SLOW(200, strongrand_mem(r, buf, 100));
   strongrand_close(r);
index d4c510e3414bcc64d0b25b1f1099b590eee04916..dfe7494bd9ac547d2a03aacff2f9e5796dfccbfa 100644 (file)
@@ -157,7 +157,7 @@ void strongrand_mem(struct strongrand *c, void *ptr, size_t size);
  * -- STRONGRAND_STD_AUTO_RESET flag
  * -- no read since open or last reset (empty buffer)
  **/
-struct strongrand *strongrand_std_open(uint buf_size, uint flags);
+struct strongrand *strongrand_std_open(uint flags, uint buf_size);
 
 enum strongrand_std_flags {
   STRONGRAND_STD_URANDOM = 0x1,                // Use kernel's "urandom" source.