1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00

(trunk libT) #1384: make tr_cryptoRandInt() simpler to read

This commit is contained in:
Charles Kerr 2009-02-10 21:43:08 +00:00
parent 6fa7197105
commit c443159681

View file

@ -306,21 +306,31 @@ tr_cryptoHasTorrentHash( const tr_crypto * crypto )
} }
int int
tr_cryptoRandInt( int sup ) tr_cryptoRandInt( int upperBound )
{ {
int r; int noise;
int val;
RAND_pseudo_bytes ( (unsigned char *) &r, sizeof r ); if( RAND_pseudo_bytes ( (unsigned char *) &noise, sizeof noise ) >= 0 )
{
val = abs( noise ) % upperBound;
}
else /* fall back to a weaker implementation... */
{
val = tr_cryptoWeakRandInt( upperBound );
}
return (int) ( sup * ( abs( r ) / ( INT_MAX + 1.0 ) ) ); assert( val >= 0 );
assert( val < upperBound );
return val;
} }
int int
tr_cryptoWeakRandInt( int sup ) tr_cryptoWeakRandInt( int upperBound )
{ {
static int init = 0; static int init = 0;
assert( sup > 0 ); assert( upperBound > 0 );
if( !init ) if( !init )
{ {
@ -328,7 +338,7 @@ tr_cryptoWeakRandInt( int sup )
init = 1; init = 1;
} }
return rand( ) % sup; return rand( ) % upperBound;
} }
void void