ObjFW  Check-in [2bcb2bd40f]

Overview
Comment:Use RtlGenRandom on Windows if available
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2bcb2bd40f5724ec50a6e810faa2f1445ee17e6a5c9555d62956a7336ca173c1
User & Date: js on 2024-04-21 19:15:47
Other Links: manifest | tags
Context
2024-04-21
19:55
Add missing NTAPI for RtlGenRandom check-in: f61f430bfb user: js tags: trunk
19:15
Use RtlGenRandom on Windows if available check-in: 2bcb2bd40f user: js tags: trunk
11:16
Don't use _wutime64 on Windows check-in: 7eb995ad11 user: js tags: trunk
Changes

Modified src/OFObject.m from [88eeb2453d] to [e25577ee55].

79
80
81
82
83
84
85




86
87
88
89
90
91
92
#if defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR)
extern id OFForward(id, SEL, ...);
extern struct Stret OFForward_stret(id, SEL, ...);
#else
# define OFForward OFMethodNotFound
# define OFForward_stret OFMethodNotFound_stret
#endif





struct PreIvars {
#ifdef OF_MSDOS
	ptrdiff_t offset;
#endif
	int retainCount;
#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)







>
>
>
>







79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#if defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR)
extern id OFForward(id, SEL, ...);
extern struct Stret OFForward_stret(id, SEL, ...);
#else
# define OFForward OFMethodNotFound
# define OFForward_stret OFMethodNotFound_stret
#endif

#ifdef OF_WINDOWS
BOOLEAN (*RtlGenRandomFuncPtr)(PVOID, ULONG);
#endif

struct PreIvars {
#ifdef OF_MSDOS
	ptrdiff_t offset;
#endif
	int retainCount;
#if !defined(OF_HAVE_ATOMIC_OPS) && !defined(OF_AMIGAOS)
188
189
190
191
192
193
194
195



196
197
198
199









200
201
202
203
204
205
206
	if (ptr == NULL)
		return;

	free((void *)((uintptr_t)ptr - offset));
}
#endif

#if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_GETRANDOM)



static void
initRandom(void)
{
	struct timeval tv;










# ifdef HAVE_RANDOM
	gettimeofday(&tv, NULL);
	srandom((unsigned)(tv.tv_sec ^ tv.tv_usec));
# else
	gettimeofday(&tv, NULL);
	srand((unsigned)(tv.tv_sec ^ tv.tv_usec));







|
>
>
>




>
>
>
>
>
>
>
>
>







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
	if (ptr == NULL)
		return;

	free((void *)((uintptr_t)ptr - offset));
}
#endif

#if (!defined(HAVE_ARC4RANDOM) && !defined(HAVE_GETRANDOM)) || \
    defined(OF_WINDOWS)
static OFOnceControl randomOnceControl = OFOnceControlInitValue;

static void
initRandom(void)
{
	struct timeval tv;

# ifdef OF_WINDOWS
	HANDLE handle;

	if ((handle = GetModuleHandleA("advapi32.dll")) != NULL &&
	    (RtlGenRandomFuncPtr = (BOOLEAN (*)(PVOID, ULONG))
	    GetProcAddress(handle, "SystemFunction036")) != NULL)
		return;
# endif

# ifdef HAVE_RANDOM
	gettimeofday(&tv, NULL);
	srandom((unsigned)(tv.tv_sec ^ tv.tv_usec));
# else
	gettimeofday(&tv, NULL);
	srand((unsigned)(tv.tv_sec ^ tv.tv_usec));
216
217
218
219
220
221
222

223







224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244










245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264










265
266
267
268
269
270
271
#elif defined(HAVE_GETRANDOM)
	uint16_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else

	static OFOnceControl onceControl = OFOnceControlInitValue;







	OFOnce(&onceControl, initRandom);
# ifdef HAVE_RANDOM
	return random() & 0xFFFF;
# else
	return rand() & 0xFFFF;
# endif
#endif
}

uint32_t
OFRandom32(void)
{
#if defined(HAVE_ARC4RANDOM)
	return arc4random();
#elif defined(HAVE_GETRANDOM)
	uint32_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else










	return ((uint32_t)OFRandom16() << 16) | OFRandom16();
#endif
}

uint64_t
OFRandom64(void)
{
#if defined(HAVE_ARC4RANDOM_BUF)
	uint64_t buffer;

	arc4random_buf(&buffer, sizeof(buffer));

	return buffer;
#elif defined(HAVE_GETRANDOM)
	uint64_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else










	return ((uint64_t)OFRandom32() << 32) | OFRandom32();
#endif
}

void
OFHashInit(unsigned long *hash)
{







>
|
>
>
>
>
>
>
>
|




















>
>
>
>
>
>
>
>
>
>




















>
>
>
>
>
>
>
>
>
>







232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#elif defined(HAVE_GETRANDOM)
	uint16_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
	OFOnce(&randomOnceControl, initRandom);

# ifdef OF_WINDOWS
	if (RtlGenRandomFuncPtr != NULL) {
		uint16_t buffer;
		OFEnsure(RtlGenRandomFuncPtr(&buffer, sizeof(buffer)));
		return buffer;
	}
# endif

# ifdef HAVE_RANDOM
	return random() & 0xFFFF;
# else
	return rand() & 0xFFFF;
# endif
#endif
}

uint32_t
OFRandom32(void)
{
#if defined(HAVE_ARC4RANDOM)
	return arc4random();
#elif defined(HAVE_GETRANDOM)
	uint32_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
# ifdef OF_WINDOWS
	OFOnce(&randomOnceControl, initRandom);

	if (RtlGenRandomFuncPtr != NULL) {
		uint32_t buffer;
		OFEnsure(RtlGenRandomFuncPtr(&buffer, sizeof(buffer)));
		return buffer;
	}
# endif

	return ((uint32_t)OFRandom16() << 16) | OFRandom16();
#endif
}

uint64_t
OFRandom64(void)
{
#if defined(HAVE_ARC4RANDOM_BUF)
	uint64_t buffer;

	arc4random_buf(&buffer, sizeof(buffer));

	return buffer;
#elif defined(HAVE_GETRANDOM)
	uint64_t buffer;

	OFEnsure(getrandom(&buffer, sizeof(buffer), 0) == sizeof(buffer));

	return buffer;
#else
# ifdef OF_WINDOWS
	OFOnce(&randomOnceControl, initRandom);

	if (RtlGenRandomFuncPtr != NULL) {
		uint64_t buffer;
		OFEnsure(RtlGenRandomFuncPtr(&buffer, sizeof(buffer)));
		return buffer;
	}
# endif

	return ((uint64_t)OFRandom32() << 32) | OFRandom32();
#endif
}

void
OFHashInit(unsigned long *hash)
{