aboutsummaryrefslogtreecommitdiff
path: root/libntp/ntp_random.c
diff options
context:
space:
mode:
Diffstat (limited to 'libntp/ntp_random.c')
-rw-r--r--libntp/ntp_random.c58
1 files changed, 37 insertions, 21 deletions
diff --git a/libntp/ntp_random.c b/libntp/ntp_random.c
index c982a0d0ac1a..0ada44f256b0 100644
--- a/libntp/ntp_random.c
+++ b/libntp/ntp_random.c
@@ -45,7 +45,7 @@ static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95";
#endif
#include <stdio.h>
-#include <ntp_types.h>
+#include <l_stdlib.h>
#include <ntp_random.h>
#include <ntp_unixtime.h>
@@ -140,15 +140,8 @@ static char sccsid[] = "@(#)random.c 8.2 (Berkeley) 5/19/95";
#define DEG_4 63
#define SEP_4 1
-/*
- * Array versions of the above information to make code run faster --
- * relies on fact that TYPE_i == i.
- */
#define MAX_TYPES 5 /* max number of types above */
-static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
-static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
-
/*
* Initially, everything is set up as if from:
*
@@ -163,7 +156,7 @@ static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
* MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
*/
-static long randtbl[DEG_3 + 1] = {
+static unsigned long randtbl[DEG_3 + 1] = {
TYPE_3,
#ifdef USE_WEAK_SEEDING
/* Historic implementation compatibility */
@@ -198,8 +191,8 @@ static long randtbl[DEG_3 + 1] = {
* in the initialization of randtbl) because the state table pointer is set
* to point to randtbl[1] (as explained below).
*/
-static long *fptr = &randtbl[SEP_3 + 1];
-static long *rptr = &randtbl[1];
+static unsigned long *fptr = &randtbl[SEP_3 + 1];
+static unsigned long *rptr = &randtbl[1];
/*
* The following things are the pointer to the state information table, the
@@ -211,11 +204,11 @@ static long *rptr = &randtbl[1];
* this is more efficient than indexing every time to find the address of
* the last element to see if the front and rear pointers have wrapped.
*/
-static long *state = &randtbl[1];
+static unsigned long *state = &randtbl[1];
static long rand_type = TYPE_3;
static long rand_deg = DEG_3;
static long rand_sep = SEP_3;
-static long *end_ptr = &randtbl[DEG_3 + 1];
+static unsigned long *end_ptr = &randtbl[DEG_3 + 1];
static inline long good_rand (long);
@@ -268,19 +261,22 @@ ntp_srandom(
unsigned long x
)
{
- register long i;
+ long i;
- if (rand_type == TYPE_0)
+ if (rand_type == TYPE_0) {
state[0] = x;
- else {
+ } else {
state[0] = x;
for (i = 1; i < rand_deg; i++)
state[i] = good_rand(state[i - 1]);
fptr = &state[rand_sep];
rptr = &state[0];
for (i = 0; i < 10 * rand_deg; i++)
- (void)ntp_random();
+ x = ntp_random();
}
+
+ /* seed the likely faster (and poorer) rand() as well */
+ srand((u_int)x);
}
/*
@@ -307,6 +303,24 @@ ntp_srandomdev( void )
}
#endif
+
+/*
+ * ntp_initstate() and ntp_setstate() are unused in our codebase and
+ * trigger warnings due to casting to a more-strictly-aligned pointer
+ * on alignment-sensitive platforms. #ifdef them away to save noise,
+ * build time, and binary space, but retain the code in case we find a
+ * use.
+ */
+#ifdef COMPILE_UNUSED_FUNCTIONS
+/*
+ * Array versions of the above information to make code run faster --
+ * relies on fact that TYPE_i == i.
+ */
+#define MAX_TYPES 5 /* max number of types above */
+
+static long degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
+static long seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
+
/*
* initstate:
*
@@ -370,7 +384,7 @@ ntp_initstate(
rand_deg = DEG_4;
rand_sep = SEP_4;
}
- state = (long *) (long_arg_state + 1); /* first location */
+ state = (unsigned long *) (long_arg_state + 1); /* first location */
end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
ntp_srandom(seed);
if (rand_type == TYPE_0)
@@ -404,7 +418,7 @@ ntp_setstate(
char *arg_state /* pointer to state array */
)
{
- register long *new_state = (long *) arg_state;
+ register unsigned long *new_state = (unsigned long *) arg_state;
register long type = new_state[0] % MAX_TYPES;
register long rear = new_state[0] / MAX_TYPES;
char *ostate = (char *)(&state[-1]);
@@ -427,7 +441,7 @@ ntp_setstate(
(void)fprintf(stderr,
"random: state info corrupted; not changed.\n");
}
- state = (long *) (new_state + 1);
+ state = (new_state + 1);
if (rand_type != TYPE_0) {
rptr = &state[rear];
fptr = &state[(rear + rand_sep) % rand_deg];
@@ -435,6 +449,8 @@ ntp_setstate(
end_ptr = &state[rand_deg]; /* set end_ptr too */
return(ostate);
}
+#endif /* COMPILE_UNUSED_FUNCTIONS */
+
/*
* random:
@@ -457,7 +473,7 @@ long
ntp_random( void )
{
register long i;
- register long *f, *r;
+ register unsigned long *f, *r;
if (rand_type == TYPE_0) {
i = state[0];