Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/platform/windows/broker/broker_request_validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// standard includes
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <iterator>
Expand All @@ -29,14 +30,14 @@ namespace lvh::windows::broker_validation {
LVH_WINDOWS_GAMEPAD_FLAG_SUPPORTS_ADAPTIVE_TRIGGERS;

template<typename Value, std::size_t Size>
bool all_zero(const Value (&values)[Size]) {
bool all_zero(const std::array<Value, Size> &values) {
return std::ranges::all_of(values, [](const auto value) {
return value == Value {};
});
}

template<std::size_t Size>
bool valid_c_string(const char (&value)[Size], bool allow_empty = true) {
bool valid_c_string(const std::array<char, Size> &value, bool allow_empty = true) {
const auto terminator = std::ranges::find(value, '\0');
if (terminator == std::end(value) || (!allow_empty && terminator == std::begin(value))) {
return false;
Expand All @@ -48,7 +49,7 @@ namespace lvh::windows::broker_validation {
}

template<std::size_t Size>
bool valid_sized_c_string(const char (&value)[Size], std::uint32_t size) {
bool valid_sized_c_string(const std::array<char, Size> &value, std::uint32_t size) {
if (size >= Size || value[size] != '\0') {
return false;
}
Expand Down
14 changes: 7 additions & 7 deletions src/platform/windows/broker/libvirtualhid_broker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,10 @@ namespace lvh::detail::windows_broker_service {
}

template<std::size_t Size>
void copy_c_string(char (&target)[Size], std::string_view value) {
void copy_c_string(std::array<char, Size> &target, std::string_view value) {
std::ranges::fill(target, '\0');
const auto count = std::min(value.size(), Size - 1U);
std::memcpy(target, value.data(), count);
std::memcpy(target.data(), value.data(), count);
}

std::string windows_error_message(DWORD error_code) {
Expand Down Expand Up @@ -1189,7 +1189,7 @@ namespace lvh::detail::windows_broker_service {
const LvhWindowsSessionToken &lhs,
const LvhWindowsSessionToken &rhs
) {
return std::memcmp(lhs.bytes, rhs.bytes, sizeof(lhs.bytes)) == 0;
return std::ranges::equal(lhs.bytes, rhs.bytes);
}

LvhWindowsDestroyDeviceRequest make_destroy_device_request(
Expand Down Expand Up @@ -1690,8 +1690,8 @@ namespace lvh::detail::windows_broker_service {
return response;
}

const std::string license_key {request.license_key};
const auto instance_name = request.instance_name[0] == '\0' ? default_instance_name() : std::string {request.instance_name};
const std::string license_key {request.license_key.data()};
const auto instance_name = request.instance_name[0] == '\0' ? default_instance_name() : std::string {request.instance_name.data()};
if (license_key.empty()) {
response.status = std::to_underlying(LvhWindowsBrokerStatusCode::invalid_argument);
copy_c_string(response.message, "License key is required.");
Expand Down Expand Up @@ -2237,7 +2237,7 @@ namespace lvh::detail::windows_broker_service {

std::pair<LvhWindowsBrokerStatusCode, bool> authorize_gamepad_create(
LvhWindowsBrokerLicenseStatus &license,
char (&message)[LVH_WINDOWS_BROKER_MAX_MESSAGE_SIZE]
std::array<char, LVH_WINDOWS_BROKER_MAX_MESSAGE_SIZE> &message
) {
{
std::lock_guard lock {mutex_};
Expand Down Expand Up @@ -2281,7 +2281,7 @@ namespace lvh::detail::windows_broker_service {

if (!license_allowed(*license_state_) || !license_time_is_current_locked()) {
fill_license_status_locked(license);
copy_c_string(message, license.message);
copy_c_string(message, license.message.data());
return {LvhWindowsBrokerStatusCode::license_invalid, false};
}

Expand Down
9 changes: 5 additions & 4 deletions src/platform/windows/control_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// standard includes
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
Expand Down Expand Up @@ -94,24 +95,24 @@ namespace lvh::detail::windows {
}

template<std::size_t Size>
std::uint32_t copy_string(char (&target)[Size], std::string_view source) {
std::uint32_t copy_string(std::array<char, Size> &target, std::string_view source) {
std::ranges::fill(target, '\0');

const auto copied = std::min(source.size(), Size - 1U);
if (copied > 0U) {
std::memcpy(target, source.data(), copied);
std::memcpy(target.data(), source.data(), copied);
}

return static_cast<std::uint32_t>(copied);
}

template<std::size_t Size>
std::uint32_t copy_bytes(std::uint8_t (&target)[Size], const std::vector<std::uint8_t> &source) {
std::uint32_t copy_bytes(std::array<std::uint8_t, Size> &target, const std::vector<std::uint8_t> &source) {
std::ranges::fill(target, std::uint8_t {});

const auto copied = std::min(source.size(), Size);
if (copied > 0U) {
std::memcpy(target, source.data(), copied);
std::memcpy(target.data(), source.data(), copied);
}

return static_cast<std::uint32_t>(copied);
Expand Down
33 changes: 18 additions & 15 deletions src/platform/windows/driver/libvirtualhid_umdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ namespace {

const auto descriptor_size = record->request.report_sizes.report_descriptor_size;
record->report_descriptor.assign(
record->request.report_descriptor,
record->request.report_descriptor + descriptor_size
record->request.report_descriptor.data(),
record->request.report_descriptor.data() + descriptor_size
);
record->hardware_ids = lvh::detail::windows::make_hardware_ids(record->request);

Expand Down Expand Up @@ -484,14 +484,14 @@ namespace {
}

bool session_token_matches(const DeviceRecord &record, const LvhWindowsSessionToken &session_token) {
return std::memcmp(record.session_token.bytes, session_token.bytes, sizeof(record.session_token.bytes)) == 0;
return std::ranges::equal(record.session_token.bytes, session_token.bytes);
}

NTSTATUS generate_session_token(LvhWindowsSessionToken &session_token) {
const auto status = BCryptGenRandom(
nullptr,
session_token.bytes,
static_cast<ULONG>(sizeof(session_token.bytes)),
session_token.bytes.data(),
static_cast<ULONG>(session_token.bytes.size()),
BCRYPT_USE_SYSTEM_PREFERRED_RNG
);
if (!NT_SUCCESS(status)) {
Expand Down Expand Up @@ -674,8 +674,8 @@ namespace {
const LvhWindowsSubmitInputReportRequest &request
) {
const auto report_id = record.request.hardware_ids.report_id;
const auto report_begin = request.report;
const auto report_end = request.report + request.report_size;
const auto report_begin = request.report.data();
const auto report_end = request.report.data() + request.report_size;
if (report_id == 0U) {
return {report_begin, report_end};
}
Expand Down Expand Up @@ -703,7 +703,7 @@ namespace {
}

if (payload_size > 0U) {
std::memcpy(event.report + report_id_size, packet.reportBuffer, payload_size);
std::memcpy(event.report.data() + report_id_size, packet.reportBuffer, payload_size);
}

event.report_size = static_cast<std::uint32_t>(report_id_size + payload_size);
Expand All @@ -714,7 +714,7 @@ namespace {
return;
}

auto reply = lvh::detail::windows::make_switch_pro_reply({event.report, event.report_size});
auto reply = lvh::detail::windows::make_switch_pro_reply({event.report.data(), event.report_size});
if (!reply.has_value()) {
return;
}
Expand Down Expand Up @@ -778,7 +778,7 @@ namespace {
std::lock_guard lock {record.mutex};
return record.generic_pid_feature_state.handle_set_feature(
static_cast<std::uint8_t>(report_id),
{event.report, event.report_size}
{event.report.data(), event.report_size}
);
}
return lvh::detail::windows::is_playstation_gamepad(record.request.gamepad_kind);
Expand All @@ -792,21 +792,24 @@ namespace {
std::lock_guard lock {record.mutex};
static_cast<void>(record.generic_pid_feature_state.handle_output_report(
static_cast<std::uint8_t>(event.report[0]),
{event.report, event.report_size}
{event.report.data(), event.report_size}
));
}

void set_device_path(std::uint64_t driver_device_id, char (&device_path)[LVH_WINDOWS_MAX_DEVICE_PATH_SIZE]) {
void set_device_path(
std::uint64_t driver_device_id,
std::array<char, LVH_WINDOWS_MAX_DEVICE_PATH_SIZE> &device_path
) {
constexpr auto path_prefix_size = sizeof(LVH_WINDOWS_CONTROL_DEVICE_PATH) - 1U;
constexpr auto separator_size = 1U;
static_assert(path_prefix_size + separator_size < LVH_WINDOWS_MAX_DEVICE_PATH_SIZE);

std::memcpy(device_path, LVH_WINDOWS_CONTROL_DEVICE_PATH, path_prefix_size);
std::memcpy(device_path.data(), LVH_WINDOWS_CONTROL_DEVICE_PATH, path_prefix_size);
device_path[path_prefix_size] = '#';

const auto output = std::to_chars(
device_path + path_prefix_size + separator_size,
device_path + sizeof(device_path) - 1U,
device_path.data() + path_prefix_size + separator_size,
device_path.data() + device_path.size() - 1U,
driver_device_id
);
if (output.ec == std::errc {}) {
Expand Down
Loading
Loading