]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
CryptoNG: replace drand48() with C++11 random number generator
authorAmos Jeffries <squid3@treenet.co.nz>
Sun, 8 Feb 2015 02:25:10 +0000 (18:25 -0800)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 8 Feb 2015 02:25:10 +0000 (18:25 -0800)
CREDITS
compat/Makefile.am
compat/drand48.c [deleted file]
compat/drand48.h [deleted file]
configure.ac
src/event.cc

diff --git a/CREDITS b/CREDITS
index 2938d3516befad9356eee0985bdf53af1a48fd4e..32bb09130571398b7dceb1d4ba942225db27907b 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -1748,12 +1748,6 @@ lib/getopt.c:
 
 ==============================================================================
 
-lib/drand48.c:
-
-From Linux libc-5.4.46.
-
-==============================================================================
-
 lib/radix.c:
 
  *  Adapted from HTSUtils.c in CERN httpd 3.0 (http://info.cern.ch/httpd/)
index 85a34dcd833a4b23b11f64d9c9c1886b678e6ec5..3edb18c0e4207ac45aefc098b16e0ed7b869615b 100644 (file)
@@ -28,7 +28,6 @@ libcompat_squid_la_SOURCES = \
        cppunit.h \
        debug.cc \
        debug.h \
-       drand48.h \
        eui64_aton.h \
        eui64_aton.c \
        fdsetsize.h \
diff --git a/compat/drand48.c b/compat/drand48.c
deleted file mode 100644 (file)
index 3b37a39..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
- *
- * Squid software is distributed under GPLv2+ license and includes
- * contributions from numerous individuals and organizations.
- * Please see the COPYING and CONTRIBUTORS files for details.
- */
-
-#include "squid.h"
-
-/* borrowed from libc/misc/drand48.c in Linux libc-5.4.46 this quick
- * hack by Martin Hamilton <martinh@gnu.org> to make Squid build on
- * Win32 with GNU-Win32 - sorry, folks! */
-
-#if !HAVE_DRAND48
-
-#define N   16
-#define MASK    ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
-#define LOW(x)  ((unsigned)(x) & MASK)
-#define HIGH(x) LOW((x) >> N)
-#define MUL(x, y, z)    { long l = (long)(x) * (long)(y); \
-        (z)[0] = LOW(l); (z)[1] = HIGH(l); }
-#define CARRY(x, y) ((long)(x) + (long)(y) > MASK)
-#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
-#define X0  0x330E
-#define X1  0xABCD
-#define X2  0x1234
-#define A0  0xE66D
-#define A1  0xDEEC
-#define A2  0x5
-#define C   0xB
-
-static void next(void);
-static unsigned x[3] = {X0, X1, X2}, a[3] = {A0, A1, A2}, c = C;
-
-double drand48(void);
-
-double
-drand48(void)
-{
-    static double two16m = 1.0 / (1L << N);
-    next();
-    return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
-}
-
-static void
-next(void)
-{
-    unsigned p[2], q[2], r[2], carry0, carry1;
-
-    MUL(a[0], x[0], p);
-    ADDEQU(p[0], c, carry0);
-    ADDEQU(p[1], carry0, carry1);
-    MUL(a[0], x[1], q);
-    ADDEQU(p[1], q[0], carry0);
-    MUL(a[1], x[0], r);
-    x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
-               a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
-    x[1] = LOW(p[1] + r[0]);
-    x[0] = LOW(p[0]);
-}
-
-#endif /* HAVE_DRAND48 */
-
diff --git a/compat/drand48.h b/compat/drand48.h
deleted file mode 100644 (file)
index 3ed4dda..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
- *
- * Squid software is distributed under GPLv2+ license and includes
- * contributions from numerous individuals and organizations.
- * Please see the COPYING and CONTRIBUTORS files for details.
- */
-
-#ifndef _SQUID_DRAND48_H
-#define _SQUID_DRAND48_H
-
-#if !HAVE_DRAND48
-#define HAVE_DRAND48 1
-SQUIDCEXTERN double drand48(void);
-#endif
-
-#endif
-
index 15268461c82d7d1cc72ab96356755fe0c270c126..2f16710dad2a5447329d09b04eaa9b8fab9ebdf2 100644 (file)
@@ -3371,7 +3371,6 @@ AC_CHECK_FUNCS(\
 )
 dnl ... and some we provide local replacements for
 AC_REPLACE_FUNCS(\
-       drand48 \
        initgroups \
        psignal \
        strerror \
index 534d62a0f21f98a4f287cfd1ae9ef111f9b1321a..ef37e5a217a908573cccac87faabf3bffc4c4710 100644 (file)
@@ -9,7 +9,6 @@
 /* DEBUG: section 41    Event Processing */
 
 #include "squid.h"
-#include "compat/drand48.h"
 #include "event.h"
 #include "mgr/Registration.h"
 #include "profiler/Profiler.h"
@@ -18,6 +17,7 @@
 #include "tools.h"
 
 #include <cmath>
+#include <random>
 
 /* The list of event processes */
 
@@ -112,12 +112,12 @@ void
 eventAddIsh(const char *name, EVH * func, void *arg, double delta_ish, int weight)
 {
     if (delta_ish >= 3.0) {
-        const double two_third = (2.0 * delta_ish) / 3.0;
-        delta_ish = two_third + (drand48() * two_third);
-        /*
-         * I'm sure drand48() isn't portable.  Tell me what function
-         * you have that returns a random double value in the range 0,1.
-         */
+        // Default seed is fine. We just need values random enough
+        // relative to each other to prevent waves of synchronised activity.
+        static std::mt19937 rng;
+        auto third = (delta_ish/3.0);
+        std::uniform_real_distribution<> thirdIsh(delta_ish - third, delta_ish + third);
+        delta_ish = thirdIsh(rng);
     }
 
     eventAdd(name, func, arg, delta_ish, weight);