Files
AnyPS5/core/libs/prx/libSceRandom/Export.cpp
T
PacukaandClaude Opus 5.5 d1c25097e3 feat(random): implement sceRandomGetRandomNumber
Fill the buffer from std::random_device and reject a null buffer or a
size above SCE_RANDOM_MAX_SIZE (64) with SCE_RANDOM_ERROR_INVALID, as
the system library does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 12:14:46 +02:00

30 lines
793 B
C++

#include <cstdint>
#include <cstddef>
#include <cstring>
#include <algorithm>
#include <random>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
namespace {
constexpr int SCE_RANDOM_ERROR_INVALID = static_cast<int>(0x817C0016);
constexpr std::size_t SCE_RANDOM_MAX_SIZE = 64;
}
extern "C" {
int APS5_VABI sceRandomGetRandomNumber(void* buf, size_t size) {
if (!buf || size > SCE_RANDOM_MAX_SIZE) return SCE_RANDOM_ERROR_INVALID;
static thread_local std::random_device device;
auto* bytes = static_cast<unsigned char*>(buf);
for (std::size_t offset = 0; offset < size; offset += sizeof(unsigned int)) {
const unsigned int value = device();
std::memcpy(bytes + offset, &value, std::min(sizeof(value), size - offset));
}
return 0;
}
}