From f319538d981933260a92a4c425669364721ec9dc Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Mon, 15 Apr 2024 20:44:14 +0300 Subject: [PATCH] [CRT] Move rand_s into it's own file --- sdk/lib/crt/msvcrtex.cmake | 7 ++++ sdk/lib/crt/stdlib/rand.c | 45 ----------------------- sdk/lib/crt/stdlib/rand_s.c | 64 +++++++++++++++++++++++++++++++++ sdk/lib/crt/stdlib/stdlib.cmake | 1 + 4 files changed, 72 insertions(+), 45 deletions(-) create mode 100644 sdk/lib/crt/stdlib/rand_s.c diff --git a/sdk/lib/crt/msvcrtex.cmake b/sdk/lib/crt/msvcrtex.cmake index 4ecfd4db6f8..2d50c179a80 100644 --- a/sdk/lib/crt/msvcrtex.cmake +++ b/sdk/lib/crt/msvcrtex.cmake @@ -11,6 +11,13 @@ list(APPEND MSVCRTEX_SOURCE misc/ofmt_stub.c stdio/acrt_iob_func.c) +if(DLL_EXPORT_VERSION LESS 0x600) + list(APPEND MSVCRTEX_SOURCE + stdlib/_invalid_parameter.c + stdlib/rand_s.c + ) +endif() + if(CMAKE_C_COMPILER_ID STREQUAL "Clang") # Clang performs some optimizations requiring those funtions list(APPEND MSVCRTEX_SOURCE diff --git a/sdk/lib/crt/stdlib/rand.c b/sdk/lib/crt/stdlib/rand.c index b83be76960a..29fbc3ac3b3 100644 --- a/sdk/lib/crt/stdlib/rand.c +++ b/sdk/lib/crt/stdlib/rand.c @@ -26,48 +26,3 @@ 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; -} diff --git a/sdk/lib/crt/stdlib/rand_s.c b/sdk/lib/crt/stdlib/rand_s.c new file mode 100644 index 00000000000..26e2e4a5fcc --- /dev/null +++ b/sdk/lib/crt/stdlib/rand_s.c @@ -0,0 +1,64 @@ +/* + * PROJECT: ReactOS CRT library + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) + * PURPOSE: rand_s implementation + * COPYRIGHT: Copyright 2010 Sylvain Petreolle + * Copyright 2015 Christoph von Wittich + * Copyright 2015 Pierre Schweitzer + */ + +#include + +/********************************************************************* + * 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; +} + +// Small hack: import stub to allow GCC's stdc++ to link +#if defined(__GNUC__) && (DLL_EXPORT_VERSION < 0x600) +#ifdef WIN64 +const void* __imp_rand_s = rand_s; +#else +const void* _imp_rand_s = rand_s; +#endif +#endif diff --git a/sdk/lib/crt/stdlib/stdlib.cmake b/sdk/lib/crt/stdlib/stdlib.cmake index e4680908d14..4719ec21a39 100644 --- a/sdk/lib/crt/stdlib/stdlib.cmake +++ b/sdk/lib/crt/stdlib/stdlib.cmake @@ -29,6 +29,7 @@ list(APPEND CRT_STDLIB_SOURCE stdlib/obsol.c stdlib/putenv.c stdlib/rand.c + stdlib/rand_s.c stdlib/rot.c stdlib/senv.c stdlib/swab.c