1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-04 16:01:14 -05:00
sdrangel/qrtplib/rtprandomrands.cpp

198 lines
4.1 KiB
C++
Raw Normal View History

2018-02-26 19:35:16 -05:00
/*
2018-02-27 17:05:46 -05:00
This file is a part of JRTPLIB
Copyright (c) 1999-2017 Jori Liesenborgs
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
Contact: jori.liesenborgs@gmail.com
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
This library was developed at the Expertise Centre for Digital Media
(http://www.edm.uhasselt.be), a research center of the Hasselt University
(http://www.uhasselt.be). The library is based upon work done for
my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
*/
2018-02-26 19:35:16 -05:00
#include "rtpconfig.h"
#ifdef RTP_HAVE_RAND_S
2018-02-27 17:05:46 -05:00
#define _CRT_RAND_S
2018-02-26 19:35:16 -05:00
#endif // RTP_HAVE_RAND_S
#include "rtprandomrands.h"
#include "rtperrors.h"
#include <stdlib.h>
#include <math.h>
// If compiling on VC 8 or later for full Windows, we'll attempt to use rand_s,
// which generates better random numbers. However, its only supported on Windows XP,
// Windows Server 2003, and later, so we'll do a run-time check to see if we can
// use it (it won't work on Windows 2000 SP4 for example).
namespace qrtplib
{
#ifndef RTP_HAVE_RAND_S
RTPRandomRandS::RTPRandomRandS()
{
2018-02-27 17:05:46 -05:00
initialized = false;
2018-02-26 19:35:16 -05:00
}
RTPRandomRandS::~RTPRandomRandS()
{
}
int RTPRandomRandS::Init()
{
2018-02-27 17:05:46 -05:00
return ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED;
2018-02-26 19:35:16 -05:00
}
uint8_t RTPRandomRandS::GetRandom8()
{
2018-02-27 17:05:46 -05:00
return 0;
2018-02-26 19:35:16 -05:00
}
uint16_t RTPRandomRandS::GetRandom16()
{
2018-02-27 17:05:46 -05:00
return 0;
2018-02-26 19:35:16 -05:00
}
uint32_t RTPRandomRandS::GetRandom32()
{
2018-02-27 17:05:46 -05:00
return 0;
2018-02-26 19:35:16 -05:00
}
double RTPRandomRandS::GetRandomDouble()
{
2018-02-27 17:05:46 -05:00
return 0;
2018-02-26 19:35:16 -05:00
}
#else
RTPRandomRandS::RTPRandomRandS()
{
2018-02-27 17:05:46 -05:00
initialized = false;
2018-02-26 19:35:16 -05:00
}
RTPRandomRandS::~RTPRandomRandS()
{
}
int RTPRandomRandS::Init()
{
2018-02-27 17:05:46 -05:00
if (initialized) // doesn't matter then
return 0;
HMODULE hAdvApi32 = LoadLibrary(TEXT("ADVAPI32.DLL"));
if(hAdvApi32 != NULL)
{
if(NULL != GetProcAddress( hAdvApi32, "SystemFunction036" )) // RtlGenRandom
{
initialized = true;
}
FreeLibrary(hAdvApi32);
hAdvApi32 = NULL;
}
if (!initialized)
return ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED;
return 0;
2018-02-26 19:35:16 -05:00
}
// rand_s gives a number between 0 and UINT_MAX. We'll assume that UINT_MAX is at least 8 bits
uint8_t RTPRandomRandS::GetRandom8()
{
2018-02-27 17:05:46 -05:00
if (!initialized)
return 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
unsigned int r;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
rand_s(&r);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return (uint8_t)(r&0xff);
2018-02-26 19:35:16 -05:00
}
uint16_t RTPRandomRandS::GetRandom16()
{
2018-02-27 17:05:46 -05:00
if (!initialized)
return 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
unsigned int r;
int shift = 0;
uint16_t x = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
for (int i = 0; i < 2; i++, shift += 8)
{
rand_s(&r);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
x ^= ((uint16_t)r << shift);
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return x;
2018-02-26 19:35:16 -05:00
}
uint32_t RTPRandomRandS::GetRandom32()
{
2018-02-27 17:05:46 -05:00
if (!initialized)
return 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
unsigned int r;
int shift = 0;
uint32_t x = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
for (int i = 0; i < 4; i++, shift += 8)
{
rand_s(&r);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
x ^= ((uint32_t)r << shift);
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return x;
2018-02-26 19:35:16 -05:00
}
double RTPRandomRandS::GetRandomDouble()
{
2018-02-27 17:05:46 -05:00
if (!initialized)
return 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
unsigned int r;
int shift = 0;
uint64_t x = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
for (int i = 0; i < 8; i++, shift += 8)
{
rand_s(&r);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
x ^= ((uint64_t)r << shift);
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
x &= 0x7fffffffffffffffULL;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int64_t x2 = (int64_t)x;
return RTPRANDOM_2POWMIN63*(double)x2;
2018-02-26 19:35:16 -05:00
}
#endif // RTP_HAVE_RAND_S
2018-02-27 17:05:46 -05:00
}
// end namespace
2018-02-26 19:35:16 -05:00