Cleanup RsRandom

Rename RSRandom -> RsRandom for consistency
Cleanup documentation to use Doxygen format
Solve a bunch of compiler warning (there are still a lot)
This commit is contained in:
Gioacchino Mazzurco 2019-04-28 14:06:52 +02:00
parent 3feb048c37
commit 6ca85ca7b6
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
2 changed files with 61 additions and 48 deletions

View File

@ -19,35 +19,39 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * * along with this program. If not, see <https://www.gnu.org/licenses/>. *
* * * *
*******************************************************************************/ *******************************************************************************/
#include "rsrandom.h"
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include "rsrandom.h"
#include <openssl/rand.h> #include <openssl/rand.h>
uint32_t RSRandom::index = RSRandom::N ; uint32_t RsRandom::index = RsRandom::N;
std::vector<uint32_t> RSRandom::MT(RSRandom::N,0u) ; std::vector<uint32_t> RsRandom::MT(RsRandom::N,0u);
RsMutex RSRandom::rndMtx("RSRandom") ; RsMutex RsRandom::rndMtx("RsRandom");
// According to our tests (cyril+thunder), on both Windows and Linux does /* According to our tests (cyril+thunder), on both Windows and Linux does
// RAND_bytes init itself automatically at first call, from system-based * RAND_bytes init itself automatically at first call, from system-based
// unpredictable values, so that seeding is not even needed. * unpredictable values, so that seeding is not even needed.
// This call still adds some randomness (not much actually, but it's always good to * This call still adds some randomness (not much actually, but it's always good
// have anyway) * to have anyway) */
//
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
#include "util/rstime.h" # include "util/rstime.h"
#ifdef WIN_PTHREADS_H # ifdef WIN_PTHREADS_H
static bool auto_seed = RSRandom::seed( (time(NULL) + ((uint32_t) pthread_self())*0x1293fe)^0x18e34a12 ) ; static bool auto_seed = RSRandom::seed(
#else (time(nullptr) +
static bool auto_seed = RSRandom::seed( (time(NULL) + ((uint32_t) pthread_self().p)*0x1293fe)^0x18e34a12 ) ; static_cast<uint32_t>(pthread_self()) *0x1293fe)^0x18e34a12 );
#endif # else // def WIN_PTHREADS_H
static bool auto_seed = RSRandom::seed(
(time(nullptr) +
static_cast<uint32_t>(pthread_self().p)*0x1293fe)^0x18e34a12 );
# endif // def WIN_PTHREADS_H
#endif #endif
bool RSRandom::seed(uint32_t s) bool RsRandom::seed(uint32_t s)
{ {
RsStackMutex mtx(rndMtx) ; RS_STACK_MUTEX(rndMtx);
MT.resize(N,0) ; // because MT might not be already resized MT.resize(N,0) ; // because MT might not be already resized
@ -66,22 +70,22 @@ bool RSRandom::seed(uint32_t s)
return true ; return true ;
} }
void RSRandom::random_bytes(unsigned char *data,uint32_t size) void RsRandom::random_bytes(unsigned char *data,uint32_t size)
{ {
RAND_bytes(data,size) ; RAND_bytes(data,size) ;
} }
void RSRandom::locked_next_state() void RsRandom::locked_next_state()
{ {
RAND_bytes((unsigned char *)&MT[0],N*sizeof(uint32_t)) ; RAND_bytes((unsigned char *)&MT[0],N*sizeof(uint32_t)) ;
index = 0 ; index = 0 ;
} }
uint32_t RSRandom::random_u32() uint32_t RsRandom::random_u32()
{ {
uint32_t y; uint32_t y;
{ {
RsStackMutex mtx(rndMtx) ; RS_STACK_MUTEX(rndMtx);
index++ ; index++ ;
@ -102,22 +106,22 @@ uint32_t RSRandom::random_u32()
return y; return y;
} }
uint64_t RSRandom::random_u64() uint64_t RsRandom::random_u64()
{ {
return ((uint64_t)random_u32() << 32ul) + random_u32() ; return ((uint64_t)random_u32() << 32ul) + random_u32() ;
} }
float RSRandom::random_f32() float RsRandom::random_f32()
{ {
return random_u32() / (float)(~(uint32_t)0) ; return random_u32() / (float)(~(uint32_t)0) ;
} }
double RSRandom::random_f64() double RsRandom::random_f64()
{ {
return random_u64() / (double)(~(uint64_t)0) ; return random_u64() / (double)(~(uint64_t)0) ;
} }
std::string RSRandom::random_alphaNumericString(uint32_t len) std::string RsRandom::random_alphaNumericString(uint32_t len)
{ {
std::string s = "" ; std::string s = "" ;

View File

@ -21,34 +21,43 @@
*******************************************************************************/ *******************************************************************************/
#pragma once #pragma once
// RSRandom contains a random number generator that is
// - thread safe
// - system independant
// - fast
// - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number generator
#include <vector> #include <vector>
#include <util/rsthreads.h> #include <cstdint>
class RSRandom #include "util/rsthreads.h"
#include "util/rsdeprecate.h"
/**
* RsRandom provide a random number generator that is
* - thread safe
* - platform independent
* - fast
* - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number
* generator
*/
class RsRandom
{ {
public: public:
static uint32_t random_u32() ; static uint32_t random_u32();
static uint64_t random_u64() ; static uint64_t random_u64();
static float random_f32() ; static float random_f32();
static double random_f64() ; static double random_f64();
static bool seed(uint32_t s) ; static bool seed(uint32_t s);
static std::string random_alphaNumericString(uint32_t length) ; static std::string random_alphaNumericString(uint32_t length);
static void random_bytes(unsigned char *data,uint32_t length) ; static void random_bytes(uint8_t* data, uint32_t length);
private: private:
static RsMutex rndMtx ; static RsMutex rndMtx;
static const uint32_t N = 1024; static const uint32_t N = 1024;
static void locked_next_state() ; static void locked_next_state();
static uint32_t index ; static uint32_t index;
static std::vector<uint32_t> MT ; static std::vector<uint32_t> MT;
}; };
/// @deprecated this alias is provided only for code retro-compatibility
using RSRandom RS_DEPRECATED_FOR(RsRandom) = RsRandom;