2020-05-27 10:25:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-27 10:25:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Platform.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
#if defined(__serenity__)
|
|
|
|
# include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-17 20:25:13 +00:00
|
|
|
#if defined(__unix__)
|
2020-05-27 10:25:46 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2021-09-12 16:06:33 +00:00
|
|
|
#if defined(AK_OS_MACOS)
|
2020-05-29 20:22:01 +00:00
|
|
|
# include <sys/random.h>
|
|
|
|
#endif
|
|
|
|
|
2020-05-27 10:25:46 +00:00
|
|
|
namespace AK {
|
|
|
|
|
2020-12-20 23:09:48 +00:00
|
|
|
inline void fill_with_random([[maybe_unused]] void* buffer, [[maybe_unused]] size_t length)
|
2020-05-27 10:25:46 +00:00
|
|
|
{
|
|
|
|
#if defined(__serenity__)
|
|
|
|
arc4random_buf(buffer, length);
|
2020-11-27 22:57:02 +00:00
|
|
|
#elif defined(OSS_FUZZ)
|
2021-09-12 16:06:33 +00:00
|
|
|
#elif defined(__unix__) or defined(AK_OS_MACOS)
|
2020-12-20 23:09:48 +00:00
|
|
|
[[maybe_unused]] int rc = getentropy(buffer, length);
|
2020-05-27 10:25:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_random()
|
|
|
|
{
|
|
|
|
T t;
|
|
|
|
fill_with_random(&t, sizeof(T));
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:16:06 +00:00
|
|
|
u32 get_random_uniform(u32 max_bounds);
|
|
|
|
|
2020-05-27 10:25:46 +00:00
|
|
|
}
|
2020-10-16 19:59:53 +00:00
|
|
|
|
|
|
|
using AK::fill_with_random;
|
|
|
|
using AK::get_random;
|
2021-05-14 15:16:06 +00:00
|
|
|
using AK::get_random_uniform;
|