Path: blob/main/contrib/expat/lib/random_rand_s.c
213651 views
/*1__ __ _2___\ \/ /_ __ __ _| |_3/ _ \\ /| '_ \ / _` | __|4| __// \| |_) | (_| | |_5\___/_/\_\ .__/ \__,_|\__|6|_| XML parser78Copyright (c) 2019 David Loffredo <[email protected]>9Copyright (c) 2019-2026 Sebastian Pipping <[email protected]>10Copyright (c) 2019 Ben Wagner <[email protected]>11Copyright (c) 2019 Vadim Zeitlin <[email protected]>12Copyright (c) 2026 Matthew Fernandez <[email protected]>13Licensed under the MIT license:1415Permission is hereby granted, free of charge, to any person obtaining16a copy of this software and associated documentation files (the17"Software"), to deal in the Software without restriction, including18without limitation the rights to use, copy, modify, merge, publish,19distribute, sublicense, and/or sell copies of the Software, and to permit20persons to whom the Software is furnished to do so, subject to the21following conditions:2223The above copyright notice and this permission notice shall be included24in all copies or substantial portions of the Software.2526THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,27EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF28MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN29NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,30DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR31OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE32USE OR OTHER DEALINGS IN THE SOFTWARE.33*/3435#include "random_rand_s.h"3637/* force stdlib to define rand_s() */38#if ! defined(_CRT_RAND_S)39# define _CRT_RAND_S40#endif4142// Workaround MinGW GCC trouble with recognizing `rand_s`, likely related43// to return type `error_t`; the symptom was:44// > error: implicit declaration of function ‘rand_s’45#if defined(__MINGW32__)46# include <errno.h>47#endif4849#include <stdlib.h> // for rand_s50#include <string.h> // for memcpy5152// Help clang-tidy out with prototype of function `rand_s`53#if defined(XML_CLANG_TIDY)54int rand_s(unsigned int *);55#endif5657/* Provide declaration of rand_s() for MinGW-32 (not 64, which has it),58as it didn't declare it in its header prior to version 5.3.0 of its59runtime package (mingwrt, containing stdlib.h). The upstream fix60was introduced at https://osdn.net/projects/mingw/ticket/39658 . */61#if defined(__MINGW32__) && defined(__MINGW32_VERSION) \62&& __MINGW32_VERSION < 5003000L && ! defined(__MINGW64_VERSION_MAJOR)63__declspec(dllimport) int rand_s(unsigned int *);64#endif6566/* Obtain entropy on Windows using the rand_s() function which67* generates cryptographically secure random numbers. Internally it68* uses RtlGenRandom API which is present in Windows XP and later.69*/70bool71writeRandomBytes_rand_s(void *target, size_t count) {72size_t bytesWrittenTotal = 0;7374while (bytesWrittenTotal < count) {75unsigned int random32 = 0;7677if (rand_s(&random32))78return false; /* failure */7980size_t toUse = count - bytesWrittenTotal;81if (toUse > sizeof(random32))82toUse = sizeof(random32);83memcpy((char *)target + bytesWrittenTotal, &random32, toUse);84bytesWrittenTotal += toUse;85}86return true; /* success */87}888990