mirror of
https://github.com/wiire-a/pixiewps.git
synced 2025-07-27 07:44:36 +02:00
Formatted random_r.c
This commit is contained in:
parent
22f96521fc
commit
cb615a1a08
100
src/random_r.c
100
src/random_r.c
@ -22,6 +22,10 @@
|
|||||||
* Rewritten to be reentrant by Ulrich Drepper, 1995
|
* Rewritten to be reentrant by Ulrich Drepper, 1995
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of pixiewps and was modified
|
||||||
|
*/
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -29,12 +33,12 @@
|
|||||||
/* #include <errno.h> */
|
/* #include <errno.h> */
|
||||||
|
|
||||||
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
||||||
# include <sys/param.h>
|
#include <sys/param.h>
|
||||||
# if defined(BSD) || defined(__APPLE__) && defined(__MACH__)
|
#if defined(BSD) || defined(__APPLE__) && defined(__MACH__)
|
||||||
/* Nothing to include */
|
/* Nothing to include */
|
||||||
# else
|
#else
|
||||||
# include <features.h>
|
#include <features.h>
|
||||||
# endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -82,65 +86,59 @@ struct m_random_data {
|
|||||||
dominant factor. With deg equal to seven, the period is actually much
|
dominant factor. With deg equal to seven, the period is actually much
|
||||||
longer than the 7*(2**7 - 1) predicted by this formula. */
|
longer than the 7*(2**7 - 1) predicted by this formula. */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* For each of the currently supported random number generators, we have a
|
/* For each of the currently supported random number generators, we have a
|
||||||
break value on the amount of state information (you need at least this many
|
break value on the amount of state information (you need at least this many
|
||||||
bytes of state info to support this random number generator), a degree for
|
bytes of state info to support this random number generator), a degree for
|
||||||
the polynomial (actually a trinomial) that the R.N.G. is based on, and
|
the polynomial (actually a trinomial) that the R.N.G. is based on, and
|
||||||
separation between the two lower order coefficients of the trinomial. */
|
separation between the two lower order coefficients of the trinomial. */
|
||||||
|
|
||||||
/* Linear congruential. */
|
/* Linear congruential */
|
||||||
#define TYPE_0 0
|
#define TYPE_0 0
|
||||||
#define BREAK_0 8
|
#define BREAK_0 8
|
||||||
#define DEG_0 0
|
#define DEG_0 0
|
||||||
#define SEP_0 0
|
#define SEP_0 0
|
||||||
|
|
||||||
/* x**7 + x**3 + 1. */
|
/* x**7 + x**3 + 1 */
|
||||||
#define TYPE_1 1
|
#define TYPE_1 1
|
||||||
#define BREAK_1 32
|
#define BREAK_1 32
|
||||||
#define DEG_1 7
|
#define DEG_1 7
|
||||||
#define SEP_1 3
|
#define SEP_1 3
|
||||||
|
|
||||||
/* x**15 + x + 1. */
|
/* x**15 + x + 1 */
|
||||||
#define TYPE_2 2
|
#define TYPE_2 2
|
||||||
#define BREAK_2 64
|
#define BREAK_2 64
|
||||||
#define DEG_2 15
|
#define DEG_2 15
|
||||||
#define SEP_2 1
|
#define SEP_2 1
|
||||||
|
|
||||||
/* x**31 + x**3 + 1. */
|
/* x**31 + x**3 + 1 */
|
||||||
#define TYPE_3 3
|
#define TYPE_3 3
|
||||||
#define BREAK_3 128
|
#define BREAK_3 128
|
||||||
#define DEG_3 31
|
#define DEG_3 31
|
||||||
#define SEP_3 3
|
#define SEP_3 3
|
||||||
|
|
||||||
/* x**63 + x + 1. */
|
/* x**63 + x + 1 */
|
||||||
#define TYPE_4 4
|
#define TYPE_4 4
|
||||||
#define BREAK_4 256
|
#define BREAK_4 256
|
||||||
#define DEG_4 63
|
#define DEG_4 63
|
||||||
#define SEP_4 1
|
#define SEP_4 1
|
||||||
|
|
||||||
|
|
||||||
/* Array versions of the above information to make code run faster.
|
/* Array versions of the above information to make code run faster.
|
||||||
Relies on fact that TYPE_i == i. */
|
Relies on fact that TYPE_i == i */
|
||||||
|
|
||||||
#define MAX_TYPES 5 /* Max number of types above. */
|
#define MAX_TYPES 5 /* Max number of types above */
|
||||||
|
|
||||||
struct m_random_poly_info
|
struct m_random_poly_info {
|
||||||
{
|
|
||||||
/* smallint seps[MAX_TYPES]; */
|
/* smallint seps[MAX_TYPES]; */
|
||||||
/* smallint degrees[MAX_TYPES]; */
|
/* smallint degrees[MAX_TYPES]; */
|
||||||
unsigned char seps[MAX_TYPES];
|
unsigned char seps[MAX_TYPES];
|
||||||
unsigned char degrees[MAX_TYPES];
|
unsigned char degrees[MAX_TYPES];
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct m_random_poly_info random_poly_info =
|
static const struct m_random_poly_info random_poly_info = {
|
||||||
{
|
{SEP_0, SEP_1, SEP_2, SEP_3, SEP_4},
|
||||||
{ SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 },
|
{DEG_0, DEG_1, DEG_2, DEG_3, DEG_4}
|
||||||
{ DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
|
/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
|
||||||
congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
|
congruential bit. Otherwise, we do our fancy trinomial stuff, which is the
|
||||||
same in all the other cases due to all the global variables that have been
|
same in all the other cases due to all the global variables that have been
|
||||||
@ -161,31 +159,26 @@ void m_random_r(struct m_random_data *buf, int32_t *result)
|
|||||||
|
|
||||||
state = buf->state;
|
state = buf->state;
|
||||||
|
|
||||||
if (buf->rand_type == TYPE_0)
|
if (buf->rand_type == TYPE_0) {
|
||||||
{
|
|
||||||
int32_t val = state[0];
|
int32_t val = state[0];
|
||||||
val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
|
val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
|
||||||
state[0] = val;
|
state[0] = val;
|
||||||
*result = val;
|
*result = val;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int32_t *fptr = buf->fptr;
|
int32_t *fptr = buf->fptr;
|
||||||
int32_t *rptr = buf->rptr;
|
int32_t *rptr = buf->rptr;
|
||||||
int32_t *end_ptr = buf->end_ptr;
|
int32_t *end_ptr = buf->end_ptr;
|
||||||
int32_t val;
|
int32_t val;
|
||||||
|
|
||||||
val = *fptr += *rptr;
|
val = *fptr += *rptr;
|
||||||
|
|
||||||
/* Chucking least random bit. */
|
/* Chucking least random bit. */
|
||||||
*result = (val >> 1) & 0x7fffffff;
|
*result = (val >> 1) & 0x7fffffff;
|
||||||
++fptr;
|
++fptr;
|
||||||
if (fptr >= end_ptr)
|
if (fptr >= end_ptr) {
|
||||||
{
|
|
||||||
fptr = state;
|
fptr = state;
|
||||||
++rptr;
|
++rptr;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
++rptr;
|
++rptr;
|
||||||
if (rptr >= end_ptr)
|
if (rptr >= end_ptr)
|
||||||
rptr = state;
|
rptr = state;
|
||||||
@ -195,13 +188,12 @@ void m_random_r(struct m_random_data *buf, int32_t *result)
|
|||||||
}
|
}
|
||||||
/* return 0; */
|
/* return 0; */
|
||||||
|
|
||||||
/* fail: */
|
/* fail: */
|
||||||
/* __set_errno (EINVAL); */
|
/* __set_errno (EINVAL); */
|
||||||
/* return -1; */
|
/* return -1; */
|
||||||
}
|
}
|
||||||
/* libc_hidden_def(random_r) */
|
/* libc_hidden_def(random_r) */
|
||||||
|
|
||||||
|
|
||||||
/* Initialize the random number generator based on the given seed. If the
|
/* Initialize the random number generator based on the given seed. If the
|
||||||
type is the trivial no-state-information type, just remember the seed.
|
type is the trivial no-state-information type, just remember the seed.
|
||||||
Otherwise, initializes state[] based on the given "seed" via a linear
|
Otherwise, initializes state[] based on the given "seed" via a linear
|
||||||
@ -210,7 +202,7 @@ void m_random_r(struct m_random_data *buf, int32_t *result)
|
|||||||
information a given number of times to get rid of any initial dependencies
|
information a given number of times to get rid of any initial dependencies
|
||||||
introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
|
introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
|
||||||
for default usage relies on values produced by this routine. */
|
for default usage relies on values produced by this routine. */
|
||||||
int m_srandom_r (unsigned int seed, struct m_random_data *buf)
|
int m_srandom_r(unsigned int seed, struct m_random_data *buf)
|
||||||
{
|
{
|
||||||
int type;
|
int type;
|
||||||
int32_t *state;
|
int32_t *state;
|
||||||
@ -221,14 +213,17 @@ int m_srandom_r (unsigned int seed, struct m_random_data *buf)
|
|||||||
|
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
type = buf->rand_type;
|
type = buf->rand_type;
|
||||||
if ((unsigned int) type >= MAX_TYPES)
|
if ((unsigned int)type >= MAX_TYPES)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
state = buf->state;
|
state = buf->state;
|
||||||
|
|
||||||
/* We must make sure the seed is not 0. Take arbitrarily 1 in this case. */
|
/* We must make sure the seed is not 0. Take arbitrarily 1 in this case. */
|
||||||
if (seed == 0)
|
if (seed == 0)
|
||||||
seed = 1;
|
seed = 1;
|
||||||
|
|
||||||
state[0] = seed;
|
state[0] = seed;
|
||||||
if (type == TYPE_0)
|
if (type == TYPE_0)
|
||||||
goto done;
|
goto done;
|
||||||
@ -236,8 +231,7 @@ int m_srandom_r (unsigned int seed, struct m_random_data *buf)
|
|||||||
dst = state;
|
dst = state;
|
||||||
word = seed;
|
word = seed;
|
||||||
kc = buf->rand_deg;
|
kc = buf->rand_deg;
|
||||||
for (i = 1; i < kc; ++i)
|
for (i = 1; i < kc; ++i) {
|
||||||
{
|
|
||||||
/* This does:
|
/* This does:
|
||||||
state[i] = (16807 * state[i - 1]) % 2147483647;
|
state[i] = (16807 * state[i - 1]) % 2147483647;
|
||||||
but avoids overflowing 31 bits. */
|
but avoids overflowing 31 bits. */
|
||||||
@ -252,10 +246,9 @@ int m_srandom_r (unsigned int seed, struct m_random_data *buf)
|
|||||||
buf->fptr = &state[buf->rand_sep];
|
buf->fptr = &state[buf->rand_sep];
|
||||||
buf->rptr = &state[0];
|
buf->rptr = &state[0];
|
||||||
kc *= 10;
|
kc *= 10;
|
||||||
while (--kc >= 0)
|
while (--kc >= 0) {
|
||||||
{
|
|
||||||
int32_t discard;
|
int32_t discard;
|
||||||
(void) m_random_r (buf, &discard);
|
(void)m_random_r(buf, &discard);
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
@ -266,7 +259,6 @@ fail:
|
|||||||
}
|
}
|
||||||
/* libc_hidden_def(srandom_r) */
|
/* libc_hidden_def(srandom_r) */
|
||||||
|
|
||||||
|
|
||||||
/* Initialize the state information in the given array of N bytes for
|
/* Initialize the state information in the given array of N bytes for
|
||||||
future random number generation. Based on the number of bytes we
|
future random number generation. Based on the number of bytes we
|
||||||
are given, and the break values for the different R.N.G.'s, we choose
|
are given, and the break values for the different R.N.G.'s, we choose
|
||||||
@ -278,7 +270,7 @@ fail:
|
|||||||
Note: The first thing we do is save the current state, if any, just like
|
Note: The first thing we do is save the current state, if any, just like
|
||||||
setstate so that it doesn't matter when initstate is called.
|
setstate so that it doesn't matter when initstate is called.
|
||||||
Returns a pointer to the old state. */
|
Returns a pointer to the old state. */
|
||||||
int m_initstate_r (unsigned int seed, char *arg_state, size_t n, struct m_random_data *buf)
|
int m_initstate_r(unsigned int seed, char *arg_state, size_t n, struct m_random_data *buf)
|
||||||
{
|
{
|
||||||
int type;
|
int type;
|
||||||
int degree;
|
int degree;
|
||||||
@ -290,10 +282,8 @@ int m_initstate_r (unsigned int seed, char *arg_state, size_t n, struct m_random
|
|||||||
|
|
||||||
if (n >= BREAK_3)
|
if (n >= BREAK_3)
|
||||||
type = n < BREAK_4 ? TYPE_3 : TYPE_4;
|
type = n < BREAK_4 ? TYPE_3 : TYPE_4;
|
||||||
else if (n < BREAK_1)
|
else if (n < BREAK_1) {
|
||||||
{
|
if (n < BREAK_0) {
|
||||||
if (n < BREAK_0)
|
|
||||||
{
|
|
||||||
/* __set_errno (EINVAL); */
|
/* __set_errno (EINVAL); */
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
@ -306,15 +296,16 @@ int m_initstate_r (unsigned int seed, char *arg_state, size_t n, struct m_random
|
|||||||
separation = random_poly_info.seps[type];
|
separation = random_poly_info.seps[type];
|
||||||
|
|
||||||
buf->rand_type = type;
|
buf->rand_type = type;
|
||||||
|
|
||||||
buf->rand_sep = separation;
|
buf->rand_sep = separation;
|
||||||
buf->rand_deg = degree;
|
buf->rand_deg = degree;
|
||||||
state = &((int32_t *) arg_state)[1]; /* First location. */
|
state = &((int32_t *)arg_state)[1]; /* First location. */
|
||||||
|
|
||||||
/* Must set END_PTR before srandom. */
|
/* Must set END_PTR before srandom. */
|
||||||
buf->end_ptr = &state[degree];
|
buf->end_ptr = &state[degree];
|
||||||
|
|
||||||
buf->state = state;
|
buf->state = state;
|
||||||
|
|
||||||
m_srandom_r (seed, buf);
|
m_srandom_r(seed, buf);
|
||||||
|
|
||||||
state[-1] = TYPE_0;
|
state[-1] = TYPE_0;
|
||||||
if (type != TYPE_0)
|
if (type != TYPE_0)
|
||||||
@ -328,7 +319,6 @@ fail:
|
|||||||
}
|
}
|
||||||
/* libc_hidden_def(initstate_r) */
|
/* libc_hidden_def(initstate_r) */
|
||||||
|
|
||||||
|
|
||||||
/* Restore the state from the given state array.
|
/* Restore the state from the given state array.
|
||||||
Note: It is important that we also remember the locations of the pointers
|
Note: It is important that we also remember the locations of the pointers
|
||||||
in the current state information, and restore the locations of the pointers
|
in the current state information, and restore the locations of the pointers
|
||||||
@ -337,9 +327,9 @@ fail:
|
|||||||
to the order in which things are done, it is OK to call setstate with the
|
to the order in which things are done, it is OK to call setstate with the
|
||||||
same state as the current state
|
same state as the current state
|
||||||
Returns a pointer to the old state information. */
|
Returns a pointer to the old state information. */
|
||||||
int m_setstate_r (char *arg_state, struct m_random_data *buf)
|
int m_setstate_r(char *arg_state, struct m_random_data *buf)
|
||||||
{
|
{
|
||||||
int32_t *new_state = 1 + (int32_t *) arg_state;
|
int32_t *new_state = 1 + (int32_t *)arg_state;
|
||||||
int type;
|
int type;
|
||||||
int old_type;
|
int old_type;
|
||||||
int32_t *old_state;
|
int32_t *old_state;
|
||||||
@ -364,13 +354,13 @@ int m_setstate_r (char *arg_state, struct m_random_data *buf)
|
|||||||
buf->rand_sep = separation = random_poly_info.seps[type];
|
buf->rand_sep = separation = random_poly_info.seps[type];
|
||||||
buf->rand_type = type;
|
buf->rand_type = type;
|
||||||
|
|
||||||
if (type != TYPE_0)
|
if (type != TYPE_0) {
|
||||||
{
|
|
||||||
int rear = new_state[-1] / MAX_TYPES;
|
int rear = new_state[-1] / MAX_TYPES;
|
||||||
buf->rptr = &new_state[rear];
|
buf->rptr = &new_state[rear];
|
||||||
buf->fptr = &new_state[(rear + separation) % degree];
|
buf->fptr = &new_state[(rear + separation) % degree];
|
||||||
}
|
}
|
||||||
buf->state = new_state;
|
buf->state = new_state;
|
||||||
|
|
||||||
/* Set end_ptr too. */
|
/* Set end_ptr too. */
|
||||||
buf->end_ptr = &new_state[degree];
|
buf->end_ptr = &new_state[degree];
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user