SDRAngel
4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
qrtplib
rtprandomrands.cpp
Go to the documentation of this file.
1
/*
2
3
This file is a part of JRTPLIB
4
Copyright (c) 1999-2017 Jori Liesenborgs
5
6
Contact: jori.liesenborgs@gmail.com
7
8
This library was developed at the Expertise Centre for Digital Media
9
(http://www.edm.uhasselt.be), a research center of the Hasselt University
10
(http://www.uhasselt.be). The library is based upon work done for
11
my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12
13
Permission is hereby granted, free of charge, to any person obtaining a
14
copy of this software and associated documentation files (the "Software"),
15
to deal in the Software without restriction, including without limitation
16
the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
and/or sell copies of the Software, and to permit persons to whom the
18
Software is furnished to do so, subject to the following conditions:
19
20
The above copyright notice and this permission notice shall be included
21
in all copies or substantial portions of the Software.
22
23
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29
IN THE SOFTWARE.
30
31
*/
32
33
#include "
rtpconfig.h
"
34
#ifdef RTP_HAVE_RAND_S
35
#define _CRT_RAND_S
36
#endif // RTP_HAVE_RAND_S
37
38
#include "
rtprandomrands.h
"
39
#include "
rtperrors.h
"
40
#include <stdlib.h>
41
#include <
math.h
>
42
43
// If compiling on VC 8 or later for full Windows, we'll attempt to use rand_s,
44
// which generates better random numbers. However, its only supported on Windows XP,
45
// Windows Server 2003, and later, so we'll do a run-time check to see if we can
46
// use it (it won't work on Windows 2000 SP4 for example).
47
48
namespace
qrtplib
49
{
50
51
#ifndef RTP_HAVE_RAND_S
52
53
RTPRandomRandS::RTPRandomRandS
()
54
{
55
initialized
=
false
;
56
}
57
58
RTPRandomRandS::~RTPRandomRandS
()
59
{
60
}
61
62
int
RTPRandomRandS::Init
()
63
{
64
return
ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED
;
65
}
66
67
uint8_t
RTPRandomRandS::GetRandom8
()
68
{
69
return
0;
70
}
71
72
uint16_t
RTPRandomRandS::GetRandom16
()
73
{
74
return
0;
75
}
76
77
uint32_t
RTPRandomRandS::GetRandom32
()
78
{
79
return
0;
80
}
81
82
double
RTPRandomRandS::GetRandomDouble
()
83
{
84
return
0;
85
}
86
87
#else
88
89
RTPRandomRandS::RTPRandomRandS
()
90
{
91
initialized
=
false
;
92
}
93
94
RTPRandomRandS::~RTPRandomRandS
()
95
{
96
}
97
98
int
RTPRandomRandS::Init
()
99
{
100
if
(
initialized
)
// doesn't matter then
101
return
0;
102
103
HMODULE hAdvApi32 = LoadLibrary(TEXT(
"ADVAPI32.DLL"
));
104
if
(hAdvApi32 != NULL)
105
{
106
if
(NULL != GetProcAddress( hAdvApi32,
"SystemFunction036"
))
// RtlGenRandom
107
{
108
initialized
=
true
;
109
}
110
FreeLibrary(hAdvApi32);
111
hAdvApi32 = NULL;
112
}
113
114
if
(!
initialized
)
115
return
ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED
;
116
return
0;
117
}
118
119
// rand_s gives a number between 0 and UINT_MAX. We'll assume that UINT_MAX is at least 8 bits
120
121
uint8_t
RTPRandomRandS::GetRandom8
()
122
{
123
if
(!
initialized
)
124
return
0;
125
126
unsigned
int
r;
127
128
rand_s(&r);
129
130
return
(
uint8_t
)(r&0xff);
131
}
132
133
uint16_t
RTPRandomRandS::GetRandom16
()
134
{
135
if
(!
initialized
)
136
return
0;
137
138
unsigned
int
r;
139
int
shift = 0;
140
uint16_t
x = 0;
141
142
for
(
int
i
= 0;
i
< 2;
i
++, shift += 8)
143
{
144
rand_s(&r);
145
146
x ^= ((
uint16_t
)r << shift);
147
}
148
149
return
x;
150
}
151
152
uint32_t
RTPRandomRandS::GetRandom32
()
153
{
154
if
(!
initialized
)
155
return
0;
156
157
unsigned
int
r;
158
int
shift = 0;
159
uint32_t
x = 0;
160
161
for
(
int
i
= 0;
i
< 4;
i
++, shift += 8)
162
{
163
rand_s(&r);
164
165
x ^= ((
uint32_t
)r << shift);
166
}
167
168
return
x;
169
}
170
171
double
RTPRandomRandS::GetRandomDouble
()
172
{
173
if
(!
initialized
)
174
return
0;
175
176
unsigned
int
r;
177
int
shift = 0;
178
uint64_t
x = 0;
179
180
for
(
int
i
= 0;
i
< 8;
i
++, shift += 8)
181
{
182
rand_s(&r);
183
184
x ^= ((
uint64_t
)r << shift);
185
}
186
187
x &= 0x7fffffffffffffffULL;
188
189
int64_t
x2 = (
int64_t
)x;
190
return
RTPRANDOM_2POWMIN63
*(double)x2;
191
}
192
193
#endif // RTP_HAVE_RAND_S
194
195
}
196
// end namespace
197
math.h
rtperrors.h
RTPRANDOM_2POWMIN63
#define RTPRANDOM_2POWMIN63
Definition:
rtprandom.h:47
qrtplib::RTPRandomRandS::Init
int Init()
Definition:
rtprandomrands.cpp:62
int64_t
__int64 int64_t
Definition:
rtptypes_win.h:47
uint32_t
unsigned int uint32_t
Definition:
rtptypes_win.h:46
rtprandomrands.h
qrtplib::RTPRandomRandS::initialized
bool initialized
Definition:
rtprandomrands.h:66
uint8_t
unsigned char uint8_t
Definition:
rtptypes_win.h:42
uint16_t
unsigned short uint16_t
Definition:
rtptypes_win.h:44
qrtplib::RTPRandomRandS::GetRandom16
uint16_t GetRandom16()
Definition:
rtprandomrands.cpp:72
i
int32_t i
Definition:
decimators.h:244
qrtplib::RTPRandomRandS::~RTPRandomRandS
~RTPRandomRandS()
Definition:
rtprandomrands.cpp:58
qrtplib::RTPRandomRandS::GetRandomDouble
double GetRandomDouble()
Definition:
rtprandomrands.cpp:82
rtpconfig.h
qrtplib::RTPRandomRandS::GetRandom32
uint32_t GetRandom32()
Definition:
rtprandomrands.cpp:77
ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED
#define ERR_RTP_RTPRANDOMRANDS_NOTSUPPORTED
Definition:
rtperrors.h:190
qrtplib
Definition:
rtcpapppacket.cpp:35
qrtplib::RTPRandomRandS::GetRandom8
uint8_t GetRandom8()
Definition:
rtprandomrands.cpp:67
qrtplib::RTPRandomRandS::RTPRandomRandS
RTPRandomRandS()
Definition:
rtprandomrands.cpp:53
uint64_t
unsigned __int64 uint64_t
Definition:
rtptypes_win.h:48
Generated on Fri Aug 2 2019 17:56:32 for SDRAngel by
1.8.13