[libdispatch-dev] [PATCH] arc4random

Mark Heily mark at heily.com
Wed Nov 18 18:41:27 PST 2009


Here's a patch I sent a couple days ago that may have been overlooked.

Thanks,

  - Mark


Index: configure.ac
===================================================================
--- configure.ac    (revision 166)
+++ configure.ac    (working copy)
@@ -191,7 +191,7 @@
  AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]])
  AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]])
  AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include 
<sys/mount.h>]])
-AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time 
malloc_create_zone sysconf])
+AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time 
malloc_create_zone sysconf arc4random])

  AC_CHECK_DECLS([POSIX_SPAWN_START_SUSPENDED],
    [have_posix_spawn_start_suspended=true],
Index: testing/queue_finalizer.c
===================================================================
--- testing/queue_finalizer.c    (revision 166)
+++ testing/queue_finalizer.c    (working copy)
@@ -32,6 +32,35 @@

  void *ctxt_magic;

+/*
+* Based on ISC BIND lib/isc/random.c
+*/
+#ifndef HAVE_ARC4RANDOM
+static uint32_t
+arc4random(void)
+{
+    unsigned int pid;
+    static int arc4random_init;
+
+    /*
+     * The low bits of pid generally change faster.
+     * Xor them with the high bits of time which change slowly.
+     */
+    if (!arc4random_init) {
+        pid = getpid();
+        pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
+        srand(time(NULL) ^ pid);
+        arc4random_init = 1;
+    }
+
+    /*
+     * rand()'s lower bits are not random.
+     * rand()'s upper bit is zero.
+     */
+     return ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
+}
+#endif /* ! HAVE_ARC4RANDOM */
+
  static void
  finalizer(void *ctxt)
  {




More information about the libdispatch-dev mailing list