Merge pull request #2865 from babyhuey/feat/posixforwebkit-arc4random-buf
feat(libScePosixForWebKit): implement arc4random_buf
This commit is contained in:
commit
61a85df9d4
|
|
@ -244,4 +244,14 @@ if(BUILD_TESTING)
|
|||
set_tests_properties(savedata_native_write_replacement PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceSaveData.native>")
|
||||
endif()
|
||||
|
||||
add_test_executable(guest_posix_random_tests tests/GuestPosixRandom.cpp)
|
||||
target_include_directories(guest_posix_random_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_posix_random_tests PRIVATE libScePosixForWebKit)
|
||||
configure_windows_unwind(guest_posix_random_tests)
|
||||
add_test(NAME guest_posix_random COMMAND guest_posix_random_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_posix_random PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libScePosixForWebKit>")
|
||||
endif()
|
||||
endif()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
|
@ -19,4 +23,13 @@ char* APS5_VABI strcasestr_nid_postfix(const char* text, const char* needle) {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void APS5_VABI arc4random_buf_nid_postfix(void* buffer, std::size_t size) {
|
||||
static thread_local std::random_device device;
|
||||
auto* bytes = static_cast<unsigned char*>(buffer);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <source_location>
|
||||
|
||||
extern "C" void APS5_VABI arc4random_buf_nid_postfix(void*, std::size_t);
|
||||
|
||||
void Require(bool condition, std::source_location location = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
std::fprintf(stderr, "Posix random check failed at line %u\n", location.line());
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::array<unsigned char, 4099> first{};
|
||||
std::array<unsigned char, 4099> second{};
|
||||
first.fill(0xa5);
|
||||
arc4random_buf_nid_postfix(first.data(), 0);
|
||||
Require(std::all_of(first.begin(), first.end(), [](unsigned char value) { return value == 0xa5; }));
|
||||
for (const std::size_t size : {1u, 3u, 5u, 4098u}) {
|
||||
first.fill(0xa5);
|
||||
arc4random_buf_nid_postfix(first.data(), size);
|
||||
Require(std::all_of(first.begin() + static_cast<std::ptrdiff_t>(size), first.end(),
|
||||
[](unsigned char value) { return value == 0xa5; }));
|
||||
}
|
||||
arc4random_buf_nid_postfix(first.data(), first.size());
|
||||
arc4random_buf_nid_postfix(second.data(), second.size());
|
||||
Require(first != second);
|
||||
std::array<std::size_t, 256> counts{};
|
||||
for (const auto value : first) ++counts[value];
|
||||
Require(*std::max_element(counts.begin(), counts.end()) < 64);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue