mirror of
https://github.com/libretro/devilutionX.git
synced 2026-11-10 17:20:56 -10:00
Done with the following script:
```ruby
Dir["Source/**/*.{h,c,cc,cpp,hpp}"].each do |path|
v = File.read(path)
next if !v.include?("uint32_t") || v.include?("cstdint")
lines = v.lines
line_num = if lines[2].start_with?(" *")
lines.index { |l| l.start_with?(" */") } + 3
else
3
end
lines.insert(line_num, "#include <cstdint>\n")
File.write(path, lines.join(""))
end
```
then fixed-up manually
59 lines
1.3 KiB
C++
59 lines
1.3 KiB
C++
#include "platform/switch/random.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <sys/types.h>
|
|
|
|
#include <sodium.h>
|
|
|
|
extern "C" {
|
|
#include <switch/services/csrng.h>
|
|
#include <switch/sf/service.h>
|
|
}
|
|
|
|
static const char *randombytes_switchrandom_implementation_name()
|
|
{
|
|
return "switchrandom";
|
|
}
|
|
|
|
static bool randombytes_switchrandom_tryfill(void *const buf, const size_t size)
|
|
{
|
|
Result res;
|
|
Service *csrngService = csrngGetServiceSession();
|
|
if (!serviceIsActive(csrngService)) {
|
|
res = csrngInitialize();
|
|
if (!R_SUCCEEDED(res))
|
|
return false;
|
|
}
|
|
res = csrngGetRandomBytes(buf, size);
|
|
return R_SUCCEEDED(res);
|
|
}
|
|
|
|
static uint32_t randombytes_switchrandom()
|
|
{
|
|
uint32_t num;
|
|
if (!randombytes_switchrandom_tryfill(&num, sizeof(uint32_t)))
|
|
sodium_misuse();
|
|
return num;
|
|
}
|
|
|
|
static void randombytes_switchrandom_buf(void *const buf, const size_t size)
|
|
{
|
|
if (!randombytes_switchrandom_tryfill(buf, size))
|
|
sodium_misuse();
|
|
}
|
|
|
|
struct randombytes_implementation randombytes_switchrandom_implementation = {
|
|
randombytes_switchrandom_implementation_name,
|
|
randombytes_switchrandom,
|
|
nullptr,
|
|
nullptr,
|
|
randombytes_switchrandom_buf,
|
|
nullptr
|
|
};
|
|
|
|
void randombytes_switchrandom_init()
|
|
{
|
|
randombytes_set_implementation(&randombytes_switchrandom_implementation);
|
|
atexit(csrngExit);
|
|
}
|