2004-12-05 17:10:58 +01:00
|
|
|
/** $MirBSD: rnd.c,v 1.8 2004/12/05 16:10:58 tg Exp $ */
|
2004-08-27 16:55:45 +02:00
|
|
|
|
|
|
|
/*-
|
2004-05-24 21:06:55 +02:00
|
|
|
* Copyright (c) 2004
|
2004-08-27 16:55:45 +02:00
|
|
|
* Thorsten "mirabile" Glaser <tg@66h.42h.de>
|
2004-05-24 21:06:55 +02:00
|
|
|
*
|
|
|
|
* Licensee is hereby permitted to deal in this work without restric-
|
2004-12-05 17:10:58 +01:00
|
|
|
* tion, including unlimited rights to use, publicly perform, modify,
|
|
|
|
* merge, distribute, sell, give away or sublicence, provided all co-
|
|
|
|
* pyright notices above, these terms and the disclaimer are retained
|
|
|
|
* in all redistributions or reproduced in accompanying documentation
|
|
|
|
* or other materials provided with binary redistributions.
|
2004-05-24 21:06:55 +02:00
|
|
|
*
|
|
|
|
* Licensor hereby provides this work "AS IS" and WITHOUT WARRANTY of
|
|
|
|
* any kind, expressed or implied, to the maximum extent permitted by
|
|
|
|
* applicable law, but with the warranty of being written without ma-
|
2004-06-02 17:06:50 +02:00
|
|
|
* licious intent or gross negligence; in no event shall licensor, an
|
|
|
|
* author or contributor be held liable for any damage, direct, indi-
|
|
|
|
* rect or other, however caused, arising in any way out of the usage
|
2004-12-05 17:10:58 +01:00
|
|
|
* of this work, even if advised of the possibility of such damage.
|
2004-05-24 21:06:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sh.h"
|
|
|
|
#include "proto.h"
|
|
|
|
|
2004-12-05 17:10:58 +01:00
|
|
|
__RCSID("$MirBSD: rnd.c,v 1.8 2004/12/05 16:10:58 tg Exp $");
|
|
|
|
|
2004-05-24 21:06:55 +02:00
|
|
|
#ifndef HAVE_SRANDOM
|
|
|
|
#undef HAVE_RANDOM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef KSH
|
|
|
|
|
|
|
|
int rnd_state;
|
|
|
|
|
|
|
|
void
|
|
|
|
rnd_seed(long newval)
|
|
|
|
{
|
2004-05-25 00:05:25 +02:00
|
|
|
rnd_put(newval);
|
2004-05-24 21:06:55 +02:00
|
|
|
rnd_state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
rnd_get(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ARC4RANDOM
|
2004-08-07 01:03:57 +02:00
|
|
|
if (!rnd_state)
|
2004-05-24 21:06:55 +02:00
|
|
|
return arc4random() & 0x7FFF;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_RANDOM
|
|
|
|
return random() & 0x7FFF;
|
|
|
|
#else
|
|
|
|
return rand();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rnd_put(long newval)
|
|
|
|
{
|
2004-05-24 22:13:03 +02:00
|
|
|
long sv;
|
2004-05-24 21:06:55 +02:00
|
|
|
|
|
|
|
rnd_state = 1 | rnd_get();
|
2004-05-24 22:13:03 +02:00
|
|
|
sv = (rnd_get() << (newval & 7)) ^ newval;
|
2004-05-24 21:06:55 +02:00
|
|
|
|
|
|
|
#if defined(HAVE_ARC4RANDOM_PUSH)
|
|
|
|
arc4random_push(sv);
|
|
|
|
#elif defined(HAVE_ARC4RANDOM_ADDRANDOM)
|
2004-05-24 22:48:08 +02:00
|
|
|
arc4random_addrandom((char *)&sv, sizeof(sv));
|
2004-05-24 21:06:55 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARC4RANDOM
|
|
|
|
sv ^= arc4random();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_RANDOM
|
|
|
|
srandom(sv);
|
|
|
|
#else
|
|
|
|
srand(sv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (rnd_state) {
|
|
|
|
rnd_get();
|
|
|
|
rnd_state >>= 1;
|
|
|
|
}
|
|
|
|
rnd_state = 1;
|
|
|
|
}
|
|
|
|
#endif /* def KSH */
|