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

@ -296,11 +296,10 @@ ResultVal<ArchiveFormatInfo> ArchiveFactory_ExtSaveData::GetFormatInfo(const Pat
return info;
}
void ArchiveFactory_ExtSaveData::WriteIcon(const Path& path, const u8* icon_data,
std::size_t icon_size) {
void ArchiveFactory_ExtSaveData::WriteIcon(const Path& path, std::span<const u8> icon) {
std::string game_path = FileSys::GetExtSaveDataPath(GetMountPoint(), path);
FileUtil::IOFile icon_file(game_path + "icon", "wb");
icon_file.WriteBytes(icon_data, icon_size);
icon_file.WriteBytes(icon.data(), icon.size());
}
} // namespace FileSys

View file

@ -5,6 +5,7 @@
#pragma once
#include <memory>
#include <span>
#include <string>
#include <boost/serialization/export.hpp>
#include <boost/serialization/string.hpp>
@ -38,7 +39,7 @@ public:
* @param icon_data Binary data of the icon
* @param icon_size Size of the icon data
*/
void WriteIcon(const Path& path, const u8* icon_data, std::size_t icon_size);
void WriteIcon(const Path& path, std::span<const u8> icon);
private:
bool shared; ///< Whether this archive represents an ExtSaveData archive or a SharedExtSaveData

View file

@ -249,7 +249,7 @@ ResultVal<std::size_t> NCCHFile::Read(const u64 offset, const std::size_t length
std::size_t available_size = static_cast<std::size_t>(file_buffer.size() - offset);
std::size_t copy_size = std::min(length, available_size);
memcpy(buffer, file_buffer.data() + offset, copy_size);
std::memcpy(buffer, file_buffer.data() + offset, copy_size);
return copy_size;
}

View file

@ -108,7 +108,7 @@ Loader::ResultStatus CIAContainer::Load(const std::string& filepath) {
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::Load(const std::vector<u8>& file_data) {
Loader::ResultStatus CIAContainer::Load(std::span<const u8> file_data) {
Loader::ResultStatus result = LoadHeader(file_data);
if (result != Loader::ResultStatus::Success)
return result;
@ -133,30 +133,29 @@ Loader::ResultStatus CIAContainer::Load(const std::vector<u8>& file_data) {
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::LoadHeader(const std::vector<u8>& header_data,
std::size_t offset) {
if (header_data.size() - offset < sizeof(Header))
Loader::ResultStatus CIAContainer::LoadHeader(std::span<const u8> header_data, std::size_t offset) {
if (header_data.size() - offset < sizeof(Header)) {
return Loader::ResultStatus::Error;
}
std::memcpy(&cia_header, header_data.data(), sizeof(Header));
return Loader::ResultStatus::Success;
}
Loader::ResultStatus CIAContainer::LoadTicket(const std::vector<u8>& ticket_data,
std::size_t offset) {
Loader::ResultStatus CIAContainer::LoadTicket(std::span<const u8> ticket_data, std::size_t offset) {
return cia_ticket.Load(ticket_data, offset);
}
Loader::ResultStatus CIAContainer::LoadTitleMetadata(const std::vector<u8>& tmd_data,
Loader::ResultStatus CIAContainer::LoadTitleMetadata(std::span<const u8> tmd_data,
std::size_t offset) {
return cia_tmd.Load(tmd_data, offset);
}
Loader::ResultStatus CIAContainer::LoadMetadata(const std::vector<u8>& meta_data,
std::size_t offset) {
if (meta_data.size() - offset < sizeof(Metadata))
Loader::ResultStatus CIAContainer::LoadMetadata(std::span<const u8> meta_data, std::size_t offset) {
if (meta_data.size() - offset < sizeof(Metadata)) {
return Loader::ResultStatus::Error;
}
std::memcpy(&cia_metadata, meta_data.data(), sizeof(Metadata));

View file

@ -6,8 +6,8 @@
#include <array>
#include <memory>
#include <span>
#include <string>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/ticket.h"
@ -39,13 +39,13 @@ public:
// Load whole CIAs outright
Loader::ResultStatus Load(const FileBackend& backend);
Loader::ResultStatus Load(const std::string& filepath);
Loader::ResultStatus Load(const std::vector<u8>& header_data);
Loader::ResultStatus Load(std::span<const u8> header_data);
// Load parts of CIAs (for CIAs streamed in)
Loader::ResultStatus LoadHeader(const std::vector<u8>& header_data, std::size_t offset = 0);
Loader::ResultStatus LoadTicket(const std::vector<u8>& ticket_data, std::size_t offset = 0);
Loader::ResultStatus LoadTitleMetadata(const std::vector<u8>& tmd_data, std::size_t offset = 0);
Loader::ResultStatus LoadMetadata(const std::vector<u8>& meta_data, std::size_t offset = 0);
Loader::ResultStatus LoadHeader(std::span<const u8> header_data, std::size_t offset = 0);
Loader::ResultStatus LoadTicket(std::span<const u8> ticket_data, std::size_t offset = 0);
Loader::ResultStatus LoadTitleMetadata(std::span<const u8> tmd_data, std::size_t offset = 0);
Loader::ResultStatus LoadMetadata(std::span<const u8> meta_data, std::size_t offset = 0);
const Ticket& GetTicket() const;
const TitleMetadata& GetTitleMetadata() const;

View file

@ -4,6 +4,7 @@
#include <cstring>
#include <memory>
#include <span>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/sha.h>
@ -36,10 +37,10 @@ u64 GetModId(u64 program_id) {
* @param size Size of compressed buffer
* @return Size of decompressed buffer
*/
static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
static std::size_t LZSS_GetDecompressedSize(std::span<const u8> buffer) {
u32 offset_size;
std::memcpy(&offset_size, buffer + size - sizeof(u32), sizeof(u32));
return offset_size + size;
std::memcpy(&offset_size, buffer.data() + buffer.size() - sizeof(u32), sizeof(u32));
return offset_size + buffer.size();
}
/**
@ -50,19 +51,18 @@ static u32 LZSS_GetDecompressedSize(const u8* buffer, u32 size) {
* @param decompressed_size Size of decompressed buffer
* @return True on success, otherwise false
*/
static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decompressed,
u32 decompressed_size) {
const u8* footer = compressed + compressed_size - 8;
static bool LZSS_Decompress(std::span<const u8> compressed, std::span<u8> decompressed) {
const u8* footer = compressed.data() + compressed.size() - 8;
u32 buffer_top_and_bottom;
std::memcpy(&buffer_top_and_bottom, footer, sizeof(u32));
u32 out = decompressed_size;
u32 index = compressed_size - ((buffer_top_and_bottom >> 24) & 0xFF);
u32 stop_index = compressed_size - (buffer_top_and_bottom & 0xFFFFFF);
size_t out = decompressed.size();
size_t index = compressed.size() - ((buffer_top_and_bottom >> 24) & 0xFF);
size_t stop_index = compressed.size() - (buffer_top_and_bottom & 0xFFFFFF);
memset(decompressed, 0, decompressed_size);
memcpy(decompressed, compressed, compressed_size);
std::memset(decompressed.data(), 0, decompressed.size());
std::memcpy(decompressed.data(), compressed.data(), compressed.size());
while (index > stop_index) {
u8 control = compressed[--index];
@ -92,7 +92,7 @@ static bool LZSS_Decompress(const u8* compressed, u32 compressed_size, u8* decom
for (unsigned j = 0; j < segment_size; j++) {
// Check if compression is out of bounds
if (out + segment_offset >= decompressed_size)
if (out + segment_offset >= decompressed.size())
return false;
u8 data = decompressed[out + segment_offset];
@ -538,14 +538,9 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
if (strcmp(section.name, ".code") == 0 && is_compressed) {
// Section is compressed, read compressed .code section...
std::unique_ptr<u8[]> temp_buffer;
try {
temp_buffer.reset(new u8[section.size]);
} catch (std::bad_alloc&) {
return Loader::ResultStatus::ErrorMemoryAllocationFailed;
}
if (exefs_file.ReadBytes(&temp_buffer[0], section.size) != section.size)
std::vector<u8> temp_buffer(section.size);
if (exefs_file.ReadBytes(temp_buffer.data(), temp_buffer.size()) !=
temp_buffer.size())
return Loader::ResultStatus::Error;
if (is_encrypted) {
@ -553,11 +548,10 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
}
// Decompress .code section...
u32 decompressed_size = LZSS_GetDecompressedSize(&temp_buffer[0], section.size);
buffer.resize(decompressed_size);
if (!LZSS_Decompress(&temp_buffer[0], section.size, buffer.data(),
decompressed_size))
buffer.resize(LZSS_GetDecompressedSize(temp_buffer));
if (!LZSS_Decompress(temp_buffer, buffer)) {
return Loader::ResultStatus::ErrorInvalidFormat;
}
} else {
// Section is uncompressed...
buffer.resize(section.size);

View file

@ -142,8 +142,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Load(
}
exe_load_func.push_back(instruction);
}
memcpy(exe_load_args, header.infos.builtin_load_exe_args,
sizeof(_3gx_Infos::builtin_load_exe_args));
std::memcpy(exe_load_args, header.infos.builtin_load_exe_args,
sizeof(_3gx_Infos::builtin_load_exe_args));
}
// Load code sections
@ -245,8 +245,8 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(
plugin_header.plgldr_reply = plg_context.plg_reply;
plugin_header.is_default_plugin = plg_context.is_default_path;
if (plg_context.use_user_load_parameters) {
memcpy(plugin_header.config, plg_context.user_load_parameters.config,
sizeof(PluginHeader::config));
std::memcpy(plugin_header.config, plg_context.user_load_parameters.config,
sizeof(PluginHeader::config));
}
kernel.memory.WriteBlock(process, _3GX_exe_load_addr, &plugin_header, sizeof(PluginHeader));
@ -286,8 +286,7 @@ Loader::ResultStatus FileSys::Plugin3GXLoader::Map(
}
void FileSys::Plugin3GXLoader::MapBootloader(Kernel::Process& process, Kernel::KernelSystem& kernel,
u32 memory_offset,
const std::vector<u32>& exe_load_func,
u32 memory_offset, std::span<const u32> exe_load_func,
const u32_le* exe_load_args, u32 checksum_size,
u32 exe_checksum, bool no_flash) {
@ -296,7 +295,8 @@ void FileSys::Plugin3GXLoader::MapBootloader(Kernel::Process& process, Kernel::K
sizeof(u32) * 2);
std::array<u32_le, g_plugin_loader_bootloader.size() / sizeof(u32)> bootloader;
memcpy(bootloader.data(), g_plugin_loader_bootloader.data(), g_plugin_loader_bootloader.size());
std::memcpy(bootloader.data(), g_plugin_loader_bootloader.data(),
g_plugin_loader_bootloader.size());
for (auto it = bootloader.begin(); it < bootloader.end(); it++) {
switch (static_cast<u32>(*it)) {

View file

@ -21,9 +21,10 @@
#pragma once
#include <core/file_sys/archive_backend.h>
#include <span>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/archive_backend.h"
#include "core/hle/kernel/process.h"
#include "core/hle/service/plgldr/plgldr.h"
@ -71,7 +72,7 @@ private:
static constexpr size_t bootloader_memory_size = 0x1000;
static void MapBootloader(Kernel::Process& process, Kernel::KernelSystem& kernel,
u32 memory_offset, const std::vector<u32>& exe_load_func,
u32 memory_offset, std::span<const u32> exe_load_func,
const u32_le* exe_load_args, u32 checksum_size, u32 exe_checksum,
bool no_flash);

View file

@ -13,7 +13,7 @@
namespace FileSys {
Loader::ResultStatus Ticket::Load(const std::vector<u8> file_data, std::size_t offset) {
Loader::ResultStatus Ticket::Load(std::span<const u8> file_data, std::size_t offset) {
std::size_t total_size = static_cast<std::size_t>(file_data.size() - offset);
if (total_size < sizeof(u32))
return Loader::ResultStatus::Error;
@ -35,8 +35,8 @@ Loader::ResultStatus Ticket::Load(const std::vector<u8> file_data, std::size_t o
// Read signature + ticket body
ticket_signature.resize(signature_size);
memcpy(ticket_signature.data(), &file_data[offset + sizeof(u32)], signature_size);
memcpy(&ticket_body, &file_data[offset + body_start], sizeof(Body));
std::memcpy(ticket_signature.data(), &file_data[offset + sizeof(u32)], signature_size);
std::memcpy(&ticket_body, &file_data[offset + body_start], sizeof(Body));
return Loader::ResultStatus::Success;
}

View file

@ -6,6 +6,7 @@
#include <array>
#include <optional>
#include <span>
#include <string>
#include <vector>
#include "common/common_funcs.h"
@ -47,7 +48,7 @@ public:
static_assert(sizeof(Body) == 0x210, "Ticket body structure size is wrong");
#pragma pack(pop)
Loader::ResultStatus Load(const std::vector<u8> file_data, std::size_t offset = 0);
Loader::ResultStatus Load(std::span<const u8> file_data, std::size_t offset = 0);
std::optional<std::array<u8, 16>> GetTitleKey() const;
private:

View file

@ -29,12 +29,13 @@ Loader::ResultStatus TitleMetadata::Load(const std::string& file_path) {
return result;
}
Loader::ResultStatus TitleMetadata::Load(const std::vector<u8>& file_data, std::size_t offset) {
Loader::ResultStatus TitleMetadata::Load(std::span<const u8> file_data, std::size_t offset) {
std::size_t total_size = static_cast<std::size_t>(file_data.size() - offset);
if (total_size < sizeof(u32_be))
if (total_size < sizeof(u32_be)) {
return Loader::ResultStatus::Error;
}
memcpy(&signature_type, &file_data[offset], sizeof(u32_be));
std::memcpy(&signature_type, &file_data[offset], sizeof(u32_be));
// Signature lengths are variable, and the body follows the signature
u32 signature_size = GetSignatureSize(signature_type);
@ -46,13 +47,14 @@ Loader::ResultStatus TitleMetadata::Load(const std::vector<u8>& file_data, std::
std::size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
std::size_t body_end = body_start + sizeof(Body);
if (total_size < body_end)
if (total_size < body_end) {
return Loader::ResultStatus::Error;
}
// Read signature + TMD body, then load the amount of ContentChunks specified
tmd_signature.resize(signature_size);
memcpy(tmd_signature.data(), &file_data[offset + sizeof(u32_be)], signature_size);
memcpy(&tmd_body, &file_data[offset + body_start], sizeof(TitleMetadata::Body));
std::memcpy(tmd_signature.data(), &file_data[offset + sizeof(u32_be)], signature_size);
std::memcpy(&tmd_body, &file_data[offset + body_start], sizeof(TitleMetadata::Body));
std::size_t expected_size =
body_start + sizeof(Body) + static_cast<u16>(tmd_body.content_count) * sizeof(ContentChunk);
@ -65,8 +67,8 @@ Loader::ResultStatus TitleMetadata::Load(const std::vector<u8>& file_data, std::
for (u16 i = 0; i < tmd_body.content_count; i++) {
ContentChunk chunk;
memcpy(&chunk, &file_data[offset + body_end + (i * sizeof(ContentChunk))],
sizeof(ContentChunk));
std::memcpy(&chunk, &file_data[offset + body_end + (i * sizeof(ContentChunk))],
sizeof(ContentChunk));
tmd_chunks.push_back(chunk);
}

View file

@ -5,6 +5,7 @@
#pragma once
#include <array>
#include <span>
#include <string>
#include <vector>
#include "common/common_types.h"
@ -82,7 +83,7 @@ public:
#pragma pack(pop)
Loader::ResultStatus Load(const std::string& file_path);
Loader::ResultStatus Load(const std::vector<u8>& file_data, std::size_t offset = 0);
Loader::ResultStatus Load(std::span<const u8> file_data, std::size_t offset = 0);
Loader::ResultStatus Save(const std::string& file_path);
u64 GetTitleID() const;