[CRT] Move rand to stdlib, where it belongs

This commit is contained in:
Timo Kreuzer 2024-04-11 19:49:42 +03:00
parent d55f49978d
commit 43beb913da
4 changed files with 8 additions and 7 deletions

73
sdk/lib/crt/stdlib/rand.c Normal file
View file

@ -0,0 +1,73 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <precomp.h>
#include <ntsecapi.h>
#include <internal/tls.h>
/*
* @implemented
*/
int
rand(void)
{
thread_data_t *data = msvcrt_get_thread_data();
/* this is the algorithm used by MSVC, according to
* http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators */
data->random_seed = data->random_seed * 214013 + 2531011;
return (data->random_seed >> 16) & RAND_MAX;
}
/*
* @implemented
*/
void
srand(unsigned int seed)
{
thread_data_t *data = msvcrt_get_thread_data();
data->random_seed = seed;
}
/*********************************************************************
* rand_s (MSVCRT.@)
*/
int CDECL rand_s(unsigned int *pval)
{
BOOLEAN (WINAPI *pSystemFunction036)(PVOID, ULONG); // RtlGenRandom
HINSTANCE hadvapi32;
if (!pval)
{
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
*_errno() = EINVAL;
return EINVAL;
}
*pval = 0;
hadvapi32 = LoadLibraryA("advapi32.dll");
if (!hadvapi32)
{
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
*_errno() = EINVAL;
return EINVAL;
}
pSystemFunction036 = (void*)GetProcAddress(hadvapi32, "SystemFunction036");
if (!pSystemFunction036)
{
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
*_errno() = EINVAL;
FreeLibrary(hadvapi32);
return EINVAL;
}
if (!pSystemFunction036(pval, sizeof(*pval)))
{
_invalid_parameter(NULL,_CRT_WIDE("rand_s"),_CRT_WIDE(__FILE__),__LINE__, 0);
*_errno() = EINVAL;
FreeLibrary(hadvapi32);
return EINVAL;
}
FreeLibrary(hadvapi32);
return 0;
}

View file

@ -0,0 +1,29 @@
#include <stdlib.h>
#if defined(__GNUC__)
static unsigned long long next = 0;
#else
static unsigned __int64 next = 0;
#endif
/*
* @implemented
*/
int __cdecl rand(void)
{
#if defined(__GNUC__)
next = next * 0x5deece66dLL + 11;
#else
next = next * 0x5deece66di64 + 11;
#endif
return (int)((next >> 16) & RAND_MAX);
}
/*
* @implemented
*/
void __cdecl srand(unsigned seed)
{
next = seed;
}

View file

@ -1,10 +1,15 @@
list(APPEND LIBCNTPR_STDLIB_SOURCE
list(APPEND COMMON_STDLIB_SOURCE
stdlib/qsort.c
)
list(APPEND LIBCNTPR_STDLIB_SOURCE
${COMMON_STDLIB_SOURCE}
stdlib/rand_nt.c
)
list(APPEND CRT_STDLIB_SOURCE
${LIBCNTPR_STDLIB_SOURCE}
${COMMON_STDLIB_SOURCE}
stdlib/_exit.c
stdlib/_set_abort_behavior.c
stdlib/abort.c
@ -22,6 +27,7 @@ list(APPEND CRT_STDLIB_SOURCE
stdlib/mbstowcs.c
stdlib/obsol.c
stdlib/putenv.c
stdlib/rand.c
stdlib/rot.c
stdlib/senv.c
stdlib/swab.c