code: Use std::span where appropriate (#6658)

* code: Use std::span when possible

* code: Prefix memcpy and memcmp with std::
This commit is contained in:
GPUCode 2023-07-07 01:52:40 +03:00 committed by GitHub
parent 4ccd9f24fb
commit cf9bb90ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
106 changed files with 362 additions and 329 deletions

View file

@ -843,14 +843,14 @@ void NWM_UDS::Unbind(Kernel::HLERequestContext& ctx) {
rb.Push<u32>(0);
}
ResultCode NWM_UDS::BeginHostingNetwork(const u8* network_info_buffer,
std::size_t network_info_size, std::vector<u8> passphrase) {
ResultCode NWM_UDS::BeginHostingNetwork(std::span<const u8> network_info_buffer,
std::vector<u8> passphrase) {
// TODO(Subv): Store the passphrase and verify it when attempting a connection.
{
std::lock_guard lock(connection_status_mutex);
network_info = {};
std::memcpy(&network_info, network_info_buffer, network_info_size);
std::memcpy(&network_info, network_info_buffer.data(), network_info_buffer.size());
// The real UDS module throws a fatal error if this assert fails.
ASSERT_MSG(network_info.max_nodes > 1, "Trying to host a network of only one member.");
@ -921,8 +921,7 @@ void NWM_UDS::BeginHostingNetwork(Kernel::HLERequestContext& ctx) {
ASSERT(passphrase.size() == passphrase_size);
LOG_DEBUG(Service_NWM, "called");
auto result = BeginHostingNetwork(network_info_buffer.data(), network_info_buffer.size(),
std::move(passphrase));
auto result = BeginHostingNetwork(network_info_buffer, std::move(passphrase));
LOG_DEBUG(Service_NWM, "An UDS network has been created.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -940,8 +939,7 @@ void NWM_UDS::BeginHostingNetworkDeprecated(Kernel::HLERequestContext& ctx) {
ASSERT(passphrase.size() == passphrase_size);
LOG_DEBUG(Service_NWM, "called");
auto result = BeginHostingNetwork(network_info_buffer.data(), network_info_buffer.size(),
std::move(passphrase));
auto result = BeginHostingNetwork(network_info_buffer, std::move(passphrase));
LOG_DEBUG(Service_NWM, "An UDS network has been created.");
IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
@ -1277,10 +1275,10 @@ private:
};
void NWM_UDS::ConnectToNetwork(Kernel::HLERequestContext& ctx, u16 command_id,
const u8* network_info_buffer, std::size_t network_info_size,
u8 connection_type, std::vector<u8> passphrase) {
std::span<const u8> network_info_buffer, u8 connection_type,
std::vector<u8> passphrase) {
network_info = {};
std::memcpy(&network_info, network_info_buffer, network_info_size);
std::memcpy(&network_info, network_info_buffer.data(), network_info_buffer.size());
// Start the connection sequence
StartConnectionSequence(network_info.host_mac_address);
@ -1304,8 +1302,7 @@ void NWM_UDS::ConnectToNetwork(Kernel::HLERequestContext& ctx) {
std::vector<u8> passphrase = rp.PopStaticBuffer();
ConnectToNetwork(ctx, 0x1E, network_info_buffer.data(), network_info_buffer.size(),
connection_type, std::move(passphrase));
ConnectToNetwork(ctx, 0x1E, network_info_buffer, connection_type, std::move(passphrase));
LOG_DEBUG(Service_NWM, "called");
}
@ -1322,8 +1319,7 @@ void NWM_UDS::ConnectToNetworkDeprecated(Kernel::HLERequestContext& ctx) {
std::vector<u8> passphrase = rp.PopStaticBuffer();
ConnectToNetwork(ctx, 0x09, network_info_buffer.data(), network_info_buffer.size(),
connection_type, std::move(passphrase));
ConnectToNetwork(ctx, 0x09, network_info_buffer, connection_type, std::move(passphrase));
LOG_DEBUG(Service_NWM, "called");
}

View file

@ -452,12 +452,12 @@ private:
u32 sharedmem_size, const NodeInfo& node, u16 version,
std::shared_ptr<Kernel::SharedMemory> sharedmem);
ResultCode BeginHostingNetwork(const u8* network_info_buffer, std::size_t network_info_size,
ResultCode BeginHostingNetwork(std::span<const u8> network_info_buffer,
std::vector<u8> passphrase);
void ConnectToNetwork(Kernel::HLERequestContext& ctx, u16 command_id,
const u8* network_info_buffer, std::size_t network_info_size,
u8 connection_type, std::vector<u8> passphrase);
std::span<const u8> network_info_buffer, u8 connection_type,
std::vector<u8> passphrase);
void BeaconBroadcastCallback(std::uintptr_t user_data, s64 cycles_late);

View file

@ -22,7 +22,7 @@ std::vector<u8> GenerateAuthenticationFrame(AuthenticationSeq seq) {
return data;
}
AuthenticationSeq GetAuthenticationSeqNumber(const std::vector<u8>& body) {
AuthenticationSeq GetAuthenticationSeqNumber(std::span<const u8> body) {
AuthenticationFrame frame;
std::memcpy(&frame, body.data(), sizeof(frame));
@ -74,9 +74,9 @@ std::vector<u8> GenerateAssocResponseFrame(AssocStatus status, u16 association_i
return data;
}
std::tuple<AssocStatus, u16> GetAssociationResult(const std::vector<u8>& body) {
std::tuple<AssocStatus, u16> GetAssociationResult(std::span<const u8> body) {
AssociationResponseFrame frame;
memcpy(&frame, body.data(), sizeof(frame));
std::memcpy(&frame, body.data(), sizeof(frame));
constexpr u16 AssociationIdMask = 0x3FFF;
return std::make_tuple(frame.status_code, frame.assoc_id & AssociationIdMask);

View file

@ -4,6 +4,7 @@
#pragma once
#include <span>
#include <tuple>
#include <vector>
#include "common/common_types.h"
@ -41,7 +42,7 @@ static_assert(sizeof(AssociationResponseFrame) == 6, "AssociationResponseFrame h
std::vector<u8> GenerateAuthenticationFrame(AuthenticationSeq seq);
/// Returns the sequence number from the body of an Authentication frame.
AuthenticationSeq GetAuthenticationSeqNumber(const std::vector<u8>& body);
AuthenticationSeq GetAuthenticationSeqNumber(std::span<const u8> body);
/// Generates an 802.11 association response frame with the specified status, association id and
/// network id, starting at the frame body.
@ -49,6 +50,6 @@ std::vector<u8> GenerateAssocResponseFrame(AssocStatus status, u16 association_i
/// Returns a tuple of (association status, association id) from the body of an AssociationResponse
/// frame.
std::tuple<AssocStatus, u16> GetAssociationResult(const std::vector<u8>& body);
std::tuple<AssocStatus, u16> GetAssociationResult(std::span<const u8> body);
} // namespace Service::NWM

View file

@ -28,7 +28,7 @@ static std::vector<u8> GenerateLLCHeader(EtherType protocol) {
header.protocol = protocol;
std::vector<u8> buffer(sizeof(header));
memcpy(buffer.data(), &header, sizeof(header));
std::memcpy(buffer.data(), &header, sizeof(header));
return buffer;
}
@ -53,7 +53,7 @@ static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 d
header.src_node_id = src_node_id;
std::vector<u8> buffer(sizeof(header));
memcpy(buffer.data(), &header, sizeof(header));
std::memcpy(buffer.data(), &header, sizeof(header));
return buffer;
}
@ -83,7 +83,7 @@ static std::array<u8, CryptoPP::Weak::MD5::DIGESTSIZE> GetDataCryptoCTR(
* @returns The key used for data frames crypto.
*/
[[maybe_unused]] static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(
const std::vector<u8>& passphrase, const NetworkInfo& network_info) {
std::span<const u8> passphrase, const NetworkInfo& network_info) {
// Calculate the MD5 hash of the input passphrase.
std::array<u8, CryptoPP::Weak::MD5::DIGESTSIZE> passphrase_hash;
CryptoPP::Weak::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(),
@ -158,9 +158,9 @@ static std::vector<u8> GenerateCCMPAAD(const MacAddress& sender, const MacAddres
* @returns The decrypted payload.
*/
[[maybe_unused]] static std::vector<u8> DecryptDataFrame(
const std::vector<u8>& encrypted_payload,
const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, const MacAddress& sender,
const MacAddress& receiver, const MacAddress& bssid, u16 sequence_number, u16 frame_control) {
std::span<const u8> encrypted_payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key,
const MacAddress& sender, const MacAddress& receiver, const MacAddress& bssid,
u16 sequence_number, u16 frame_control) {
// Reference: IEEE 802.11-2007
@ -218,7 +218,7 @@ static std::vector<u8> GenerateCCMPAAD(const MacAddress& sender, const MacAddres
* @returns The encrypted payload.
*/
[[maybe_unused]] static std::vector<u8> EncryptDataFrame(
const std::vector<u8>& payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key,
std::span<const u8> payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key,
const MacAddress& sender, const MacAddress& receiver, const MacAddress& bssid,
u16 sequence_number, u16 frame_control) {
// Reference: IEEE 802.11-2007
@ -266,7 +266,7 @@ static std::vector<u8> GenerateCCMPAAD(const MacAddress& sender, const MacAddres
return {};
}
std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node,
std::vector<u8> GenerateDataPayload(std::span<const u8> data, u8 channel, u16 dest_node,
u16 src_node, u16 sequence_number) {
std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData);
std::vector<u8> securedata_header = GenerateSecureDataHeader(
@ -277,7 +277,7 @@ std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16
return buffer;
}
SecureDataHeader ParseSecureDataHeader(const std::vector<u8>& data) {
SecureDataHeader ParseSecureDataHeader(std::span<const u8> data) {
SecureDataHeader header;
// Skip the LLC header
@ -308,20 +308,20 @@ std::vector<u8> GenerateEAPoLStartFrame(u16 association_id, const NodeInfo& node
return buffer;
}
EtherType GetFrameEtherType(const std::vector<u8>& frame) {
EtherType GetFrameEtherType(std::span<const u8> frame) {
LLCHeader header;
std::memcpy(&header, frame.data(), sizeof(header));
return header.protocol;
}
u16 GetEAPoLFrameType(const std::vector<u8>& frame) {
u16 GetEAPoLFrameType(std::span<const u8> frame) {
// Ignore the LLC header
u16_be eapol_type;
std::memcpy(&eapol_type, frame.data() + sizeof(LLCHeader), sizeof(eapol_type));
return eapol_type;
}
NodeInfo DeserializeNodeInfoFromFrame(const std::vector<u8>& frame) {
NodeInfo DeserializeNodeInfoFromFrame(std::span<const u8> frame) {
EAPoLStartPacket eapol_start;
// Skip the LLC header
@ -372,7 +372,7 @@ std::vector<u8> GenerateEAPoLLogoffFrame(const MacAddress& mac_address, u16 netw
return buffer;
}
EAPoLLogoffPacket ParseEAPoLLogoffFrame(const std::vector<u8>& frame) {
EAPoLLogoffPacket ParseEAPoLLogoffFrame(std::span<const u8> frame) {
EAPoLLogoffPacket eapol_logoff;
// Skip the LLC header

View file

@ -5,6 +5,7 @@
#pragma once
#include <array>
#include <span>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
@ -118,13 +119,13 @@ static_assert(sizeof(EAPoLLogoffPacket) == 0x298, "EAPoLLogoffPacket has the wro
* Generates an unencrypted 802.11 data payload.
* @returns The generated frame payload.
*/
std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node,
std::vector<u8> GenerateDataPayload(std::span<const u8> data, u8 channel, u16 dest_node,
u16 src_node, u16 sequence_number);
/*
* Returns the SecureDataHeader stored in an 802.11 data frame.
*/
SecureDataHeader ParseSecureDataHeader(const std::vector<u8>& data);
SecureDataHeader ParseSecureDataHeader(std::span<const u8> data);
/*
* Generates an unencrypted 802.11 data frame body with the EAPoL-Start format for UDS
@ -136,19 +137,19 @@ std::vector<u8> GenerateEAPoLStartFrame(u16 association_id, const NodeInfo& node
/*
* Returns the EtherType of the specified 802.11 frame.
*/
EtherType GetFrameEtherType(const std::vector<u8>& frame);
EtherType GetFrameEtherType(std::span<const u8> frame);
/*
* Returns the EAPoL type (Start / Logoff) of the specified 802.11 frame.
* Note: The frame *must* be an EAPoL frame.
*/
u16 GetEAPoLFrameType(const std::vector<u8>& frame);
u16 GetEAPoLFrameType(std::span<const u8> frame);
/*
* Returns a deserialized NodeInfo structure from the information inside an EAPoL-Start packet
* encapsulated in an 802.11 data frame.
*/
NodeInfo DeserializeNodeInfoFromFrame(const std::vector<u8>& frame);
NodeInfo DeserializeNodeInfoFromFrame(std::span<const u8> frame);
/*
* Returns a NodeInfo constructed from the data in the specified EAPoLNodeInfo.
@ -166,6 +167,6 @@ std::vector<u8> GenerateEAPoLLogoffFrame(const MacAddress& mac_address, u16 netw
/*
* Returns a EAPoLLogoffPacket representing the specified 802.11-encapsulated data frame.
*/
EAPoLLogoffPacket ParseEAPoLLogoffFrame(const std::vector<u8>& frame);
EAPoLLogoffPacket ParseEAPoLLogoffFrame(std::span<const u8> frame);
} // namespace Service::NWM