1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
% Copyright (C) 2009-2015 GraphicsMagick Group
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
% Random number generator.
%
% Currently based on George Marsaglia's multiply-with-carry generator.
% This is a k=2 generator with a period >2^60.
%
*/
#include "magick/studio.h"
#include "magick/random-private.h"
#include "magick/random.h"
#include "magick/semaphore.h"
#include "magick/tsd.h"
#include "magick/utility.h"
#if defined(HAVE_PTHREAD)
# define USE_POSIX_THREADS 1
#elif defined(MSWINDOWS)
# define USE_WIN32_THREADS 1
#endif
#if defined(USE_POSIX_THREADS)
# include <pthread.h>
#endif
#if defined(USE_WIN32_THREADS)
# include <windows.h>
#endif
#if defined(MSWINDOWS) && HAVE_CRYPTGENRANDOM && HAVE_WINCRYPT_H
# include <wincrypt.h>
#endif
static MagickBool initialized = MagickFalse;
static SemaphoreInfo *semaphore = (SemaphoreInfo *) 0;
static MagickTsdKey_t kernel_key = (MagickTsdKey_t) 0;
/*
Acquire the default random number kernel. Memory is owned by
library and should not be freed.
*/
MagickExport MagickRandomKernel* AcquireMagickRandomKernel()
{
MagickRandomKernel
*kernel;
if (!initialized)
InitializeMagickRandomGenerator();
kernel=(MagickRandomKernel *) MagickTsdGetSpecific(kernel_key);
if (kernel == (MagickRandomKernel *) NULL)
{
kernel=MagickAllocateAlignedMemory(MagickRandomKernel *,
MAGICK_CACHE_LINE_SIZE,
sizeof(MagickRandomKernel));
if (kernel == (MagickRandomKernel *) NULL)
MagickFatalError3(ResourceLimitFatalError,MemoryAllocationFailed,
UnableToAllocateRandomKernel);
InitializeMagickRandomKernel(kernel);
MagickTsdSetSpecific(kernel_key,(const void *) kernel);
}
return kernel;
}
/*
Initialize the random number generator system.
*/
void InitializeMagickRandomGenerator()
{
assert(semaphore == (SemaphoreInfo *) NULL);
semaphore=AllocateSemaphoreInfo();
if (!initialized)
{
MagickTsdKeyCreate2(&kernel_key,MagickFreeAligned);
initialized=MagickTrue;
}
}
/*
Destroy the random number generator system.
*/
void DestroyMagickRandomGenerator()
{
if (initialized)
{
MagickRandomKernel
*kernel;
/* FIXME: This only frees memory associated with one thread */
kernel=(MagickRandomKernel *) MagickTsdGetSpecific(kernel_key);
MagickFreeAlignedMemory(kernel);
(void) MagickTsdSetSpecific(kernel_key,kernel);
MagickTsdKeyDelete(kernel_key);
}
kernel_key=(MagickTsdKey_t) 0;
initialized=MagickFalse;
DestroySemaphoreInfo(&semaphore);
}
/*
Initialize the random kernel with suitable entropy
*/
MagickExport void
InitializeMagickRandomKernel(MagickRandomKernel *kernel)
{
MagickBool
done = MagickFalse;
kernel->z = 0U;
kernel->w = 0U;
#if defined(POSIX) && HAVE_DEV_URANDOM
/*
Read from /dev/urandom
MinGW's MSYS emulates /dev/random in its shell so a configure
test for it produces an invalid result.
*/
{
int
file;
if ((file=open("/dev/urandom",O_RDONLY | O_BINARY)) != -1)
{
if (read(file,(void *) kernel,sizeof(*kernel)) == sizeof(*kernel))
done=MagickTrue;
(void) close(file);
}
if (!done)
MagickFatalError(ResourceLimitFatalError,UnableToObtainRandomEntropy,
NULL);
}
#elif defined(MSWINDOWS) && HAVE_CRYPTGENRANDOM
/*
Use Windows advapi32.lib CryptGenRandom
Is claimed to be supported under Windows XP
*/
{
HCRYPTPROV
hProvider = 0;
if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
/* Returns zero (FALSE) on failure */
if (CryptGenRandom(hProvider, (DWORD) sizeof(*kernel), (BYTE *) kernel))
done=MagickTrue;
(void) CryptReleaseContext(hProvider, 0);
}
if (!done)
MagickFatalError(ResourceLimitFatalError,UnableToObtainRandomEntropy,
NULL);
}
#else
/*
Fallback implementation
*/
/*
Initial time of day.
*/
kernel->z ^= (magick_uint32_t) time(0);
/*
Let's hash with the address of kernel as well.
*/
kernel->z ^= (magick_uint32_t) ((magick_uintptr_t) kernel & 4294967295UL);
/*
Process ID
*/
kernel->w ^= (magick_uint32_t) getpid();
/*
Thread ID
*/
#if defined(USE_POSIX_THREADS)
{
union
{
pthread_t thread_id;
magick_uint32_t intval;
} pthread_union;
pthread_union.intval=0U;
pthread_union.thread_id=pthread_self();
kernel->w ^= pthread_union.intval;
}
#elif defined(USE_WIN32_THREADS)
kernel->w ^= ((magick_uint32_t) GetCurrentThreadId());
#endif
/*
It is quite likely that multiple threads will invoke this function
during the same second so we also tap into the default random
number generator to help produce a more random seed.
*/
kernel->w ^= (magick_uint32_t) rand();
#endif
}
/*
Generate a random integer value
*/
MagickExport magick_uint32_t MagickRandomInteger(void)
{
MagickRandomKernel
*kernel;
kernel=AcquireMagickRandomKernel();
return MagickRandomIntegerInlined(kernel);
}
/*
Generate a random double value
*/
MagickExport double MagickRandomReal(void)
{
MagickRandomKernel
*kernel;
kernel=AcquireMagickRandomKernel();
return MagickRandomRealInlined(kernel);
}
|