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