2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-09-30 04:13:15 +02:00
|
|
|
|
2018-10-09 19:47:59 +02:00
|
|
|
#include <algorithm>
|
2018-10-09 19:39:23 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
2018-10-01 00:13:22 +02:00
|
|
|
#include <sstream>
|
2018-10-09 19:39:23 +02:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
2018-10-01 00:13:22 +02:00
|
|
|
#include "common/hex_util.h"
|
2018-10-09 19:39:23 +02:00
|
|
|
#include "common/logging/log.h"
|
2018-09-30 04:13:15 +02:00
|
|
|
#include "common/swap.h"
|
|
|
|
#include "core/file_sys/ips_layer.h"
|
|
|
|
#include "core/file_sys/vfs_vector.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
enum class IPSFileType {
|
|
|
|
IPS,
|
|
|
|
IPS32,
|
|
|
|
Error,
|
|
|
|
};
|
|
|
|
|
2018-10-09 19:42:34 +02:00
|
|
|
constexpr std::array<std::pair<const char*, const char*>, 11> ESCAPE_CHARACTER_MAP{{
|
|
|
|
{"\\a", "\a"},
|
|
|
|
{"\\b", "\b"},
|
|
|
|
{"\\f", "\f"},
|
|
|
|
{"\\n", "\n"},
|
|
|
|
{"\\r", "\r"},
|
|
|
|
{"\\t", "\t"},
|
|
|
|
{"\\v", "\v"},
|
|
|
|
{"\\\\", "\\"},
|
|
|
|
{"\\\'", "\'"},
|
|
|
|
{"\\\"", "\""},
|
|
|
|
{"\\\?", "\?"},
|
|
|
|
}};
|
2018-10-01 14:31:34 +02:00
|
|
|
|
2018-09-30 04:13:15 +02:00
|
|
|
static IPSFileType IdentifyMagic(const std::vector<u8>& magic) {
|
2018-10-09 19:47:59 +02:00
|
|
|
if (magic.size() != 5) {
|
2018-09-30 04:13:15 +02:00
|
|
|
return IPSFileType::Error;
|
2018-10-09 19:47:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:13:47 +01:00
|
|
|
static constexpr std::array<u8, 5> patch_magic{{'P', 'A', 'T', 'C', 'H'}};
|
2018-10-09 19:47:59 +02:00
|
|
|
if (std::equal(magic.begin(), magic.end(), patch_magic.begin())) {
|
2018-09-30 04:13:15 +02:00
|
|
|
return IPSFileType::IPS;
|
2018-10-09 19:47:59 +02:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:13:47 +01:00
|
|
|
static constexpr std::array<u8, 5> ips32_magic{{'I', 'P', 'S', '3', '2'}};
|
2018-10-09 19:47:59 +02:00
|
|
|
if (std::equal(magic.begin(), magic.end(), ips32_magic.begin())) {
|
2018-09-30 04:13:15 +02:00
|
|
|
return IPSFileType::IPS32;
|
2018-10-09 19:47:59 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 04:13:15 +02:00
|
|
|
return IPSFileType::Error;
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:47:59 +02:00
|
|
|
static bool IsEOF(IPSFileType type, const std::vector<u8>& data) {
|
2023-02-14 17:13:47 +01:00
|
|
|
static constexpr std::array<u8, 3> eof{{'E', 'O', 'F'}};
|
2018-10-09 19:47:59 +02:00
|
|
|
if (type == IPSFileType::IPS && std::equal(data.begin(), data.end(), eof.begin())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:13:47 +01:00
|
|
|
static constexpr std::array<u8, 4> eeof{{'E', 'E', 'O', 'F'}};
|
2018-10-09 19:47:59 +02:00
|
|
|
return type == IPSFileType::IPS32 && std::equal(data.begin(), data.end(), eeof.begin());
|
|
|
|
}
|
|
|
|
|
2018-09-30 04:13:15 +02:00
|
|
|
VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
|
2020-10-21 04:07:39 +02:00
|
|
|
if (in == nullptr || ips == nullptr)
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto type = IdentifyMagic(ips->ReadBytes(0x5));
|
2020-10-21 04:07:39 +02:00
|
|
|
if (type == IPSFileType::Error)
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto in_data = in->ReadAllBytes();
|
2023-12-28 07:15:29 +01:00
|
|
|
if (in_data.size() == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-09-30 04:13:15 +02:00
|
|
|
|
|
|
|
std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4);
|
|
|
|
u64 offset = 5; // After header
|
|
|
|
while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) {
|
|
|
|
offset += temp.size();
|
2018-10-09 19:47:59 +02:00
|
|
|
if (IsEOF(type, temp)) {
|
2018-09-30 04:13:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 real_offset{};
|
2020-10-21 04:07:39 +02:00
|
|
|
if (type == IPSFileType::IPS32)
|
|
|
|
real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3];
|
|
|
|
else
|
|
|
|
real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2];
|
2018-09-30 04:13:15 +02:00
|
|
|
|
2023-12-28 07:15:29 +01:00
|
|
|
if (real_offset > in_data.size()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-09-30 04:13:15 +02:00
|
|
|
u16 data_size{};
|
2020-10-21 04:07:39 +02:00
|
|
|
if (ips->ReadObject(&data_size, offset) != sizeof(u16))
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
|
|
|
data_size = Common::swap16(data_size);
|
|
|
|
offset += sizeof(u16);
|
|
|
|
|
|
|
|
if (data_size == 0) { // RLE
|
|
|
|
u16 rle_size{};
|
2020-10-21 04:07:39 +02:00
|
|
|
if (ips->ReadObject(&rle_size, offset) != sizeof(u16))
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
2018-10-25 20:23:56 +02:00
|
|
|
rle_size = Common::swap16(rle_size);
|
2018-09-30 04:13:15 +02:00
|
|
|
offset += sizeof(u16);
|
|
|
|
|
|
|
|
const auto data = ips->ReadByte(offset++);
|
2020-10-21 04:07:39 +02:00
|
|
|
if (!data)
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2020-10-21 04:07:39 +02:00
|
|
|
if (real_offset + rle_size > in_data.size())
|
2018-10-09 19:18:20 +02:00
|
|
|
rle_size = static_cast<u16>(in_data.size() - real_offset);
|
2018-10-30 05:03:25 +01:00
|
|
|
std::memset(in_data.data() + real_offset, *data, rle_size);
|
2018-09-30 04:13:15 +02:00
|
|
|
} else { // Standard Patch
|
|
|
|
auto read = data_size;
|
2020-10-21 04:07:39 +02:00
|
|
|
if (real_offset + read > in_data.size())
|
2018-10-09 19:18:20 +02:00
|
|
|
read = static_cast<u16>(in_data.size() - real_offset);
|
2020-10-21 04:07:39 +02:00
|
|
|
if (ips->Read(in_data.data() + real_offset, read, offset) != data_size)
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
|
|
|
offset += data_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:47:59 +02:00
|
|
|
if (!IsEOF(type, temp)) {
|
2018-09-30 04:13:15 +02:00
|
|
|
return nullptr;
|
2018-10-09 19:47:59 +02:00
|
|
|
}
|
2018-10-09 19:34:38 +02:00
|
|
|
|
|
|
|
return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(),
|
|
|
|
in->GetContainingDirectory());
|
2018-09-30 04:13:15 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:39:23 +02:00
|
|
|
struct IPSwitchCompiler::IPSwitchPatch {
|
|
|
|
std::string name;
|
|
|
|
bool enabled;
|
|
|
|
std::map<u32, std::vector<u8>> records;
|
|
|
|
};
|
|
|
|
|
2018-10-01 23:17:08 +02:00
|
|
|
IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) : patch_text(std::move(patch_text_)) {
|
2018-10-01 00:13:22 +02:00
|
|
|
Parse();
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:17:08 +02:00
|
|
|
IPSwitchCompiler::~IPSwitchCompiler() = default;
|
|
|
|
|
2018-10-01 00:13:22 +02:00
|
|
|
std::array<u8, 32> IPSwitchCompiler::GetBuildID() const {
|
|
|
|
return nso_build_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPSwitchCompiler::IsValid() const {
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:17:08 +02:00
|
|
|
static bool StartsWith(std::string_view base, std::string_view check) {
|
2018-10-01 00:13:22 +02:00
|
|
|
return base.size() >= check.size() && base.substr(0, check.size()) == check;
|
|
|
|
}
|
|
|
|
|
2018-10-01 14:31:34 +02:00
|
|
|
static std::string EscapeStringSequences(std::string in) {
|
|
|
|
for (const auto& seq : ESCAPE_CHARACTER_MAP) {
|
|
|
|
for (auto index = in.find(seq.first); index != std::string::npos;
|
|
|
|
index = in.find(seq.first, index)) {
|
|
|
|
in.replace(index, std::strlen(seq.first), seq.second);
|
|
|
|
index += std::strlen(seq.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:23:18 +02:00
|
|
|
void IPSwitchCompiler::ParseFlag(const std::string& line) {
|
|
|
|
if (StartsWith(line, "@flag offset_shift ")) {
|
|
|
|
// Offset Shift Flag
|
2023-09-13 19:32:48 +02:00
|
|
|
offset_shift = std::strtoll(line.substr(19).c_str(), nullptr, 0);
|
2018-10-04 18:23:18 +02:00
|
|
|
} else if (StartsWith(line, "@little-endian")) {
|
|
|
|
// Set values to read as little endian
|
|
|
|
is_little_endian = true;
|
|
|
|
} else if (StartsWith(line, "@big-endian")) {
|
|
|
|
// Set values to read as big endian
|
|
|
|
is_little_endian = false;
|
|
|
|
} else if (StartsWith(line, "@flag print_values")) {
|
|
|
|
// Force printing of applied values
|
|
|
|
print_values = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 00:13:22 +02:00
|
|
|
void IPSwitchCompiler::Parse() {
|
|
|
|
const auto bytes = patch_text->ReadAllBytes();
|
|
|
|
std::stringstream s;
|
2020-10-21 04:07:39 +02:00
|
|
|
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
|
2018-10-01 00:13:22 +02:00
|
|
|
|
|
|
|
std::vector<std::string> lines;
|
2018-10-01 23:17:08 +02:00
|
|
|
std::string stream_line;
|
|
|
|
while (std::getline(s, stream_line)) {
|
2018-10-01 00:13:22 +02:00
|
|
|
// Remove a trailing \r
|
2020-10-21 04:07:39 +02:00
|
|
|
if (!stream_line.empty() && stream_line.back() == '\r')
|
2018-10-01 23:17:08 +02:00
|
|
|
stream_line.pop_back();
|
|
|
|
lines.push_back(std::move(stream_line));
|
2018-10-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < lines.size(); ++i) {
|
|
|
|
auto line = lines[i];
|
2018-10-01 14:31:34 +02:00
|
|
|
|
|
|
|
// Remove midline comments
|
2018-10-04 18:23:18 +02:00
|
|
|
std::size_t comment_index = std::string::npos;
|
|
|
|
bool within_string = false;
|
|
|
|
for (std::size_t k = 0; k < line.size(); ++k) {
|
|
|
|
if (line[k] == '\"' && (k > 0 && line[k - 1] != '\\')) {
|
|
|
|
within_string = !within_string;
|
|
|
|
} else if (line[k] == '\\' && (k < line.size() - 1 && line[k + 1] == '\\')) {
|
|
|
|
comment_index = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:17:08 +02:00
|
|
|
if (!StartsWith(line, "//") && comment_index != std::string::npos) {
|
|
|
|
last_comment = line.substr(comment_index + 2);
|
|
|
|
line = line.substr(0, comment_index);
|
2018-10-01 14:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 00:13:22 +02:00
|
|
|
if (StartsWith(line, "@stop")) {
|
|
|
|
// Force stop
|
|
|
|
break;
|
|
|
|
} else if (StartsWith(line, "@nsobid-")) {
|
|
|
|
// NSO Build ID Specifier
|
2022-08-21 18:28:36 +02:00
|
|
|
const auto raw_build_id = fmt::format("{:0<64}", line.substr(8));
|
2018-10-01 00:13:22 +02:00
|
|
|
nso_build_id = Common::HexStringToArray<0x20>(raw_build_id);
|
|
|
|
} else if (StartsWith(line, "#")) {
|
|
|
|
// Mandatory Comment
|
|
|
|
LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Forced output comment: {}",
|
|
|
|
patch_text->GetName(), line.substr(1));
|
2018-10-01 14:31:34 +02:00
|
|
|
} else if (StartsWith(line, "//")) {
|
|
|
|
// Normal Comment
|
|
|
|
last_comment = line.substr(2);
|
|
|
|
if (last_comment.find_first_not_of(' ') == std::string::npos)
|
|
|
|
continue;
|
|
|
|
if (last_comment.find_first_not_of(' ') != 0)
|
|
|
|
last_comment = last_comment.substr(last_comment.find_first_not_of(' '));
|
2018-10-01 00:13:22 +02:00
|
|
|
} else if (StartsWith(line, "@enabled") || StartsWith(line, "@disabled")) {
|
|
|
|
// Start of patch
|
|
|
|
const auto enabled = StartsWith(line, "@enabled");
|
|
|
|
if (i == 0)
|
|
|
|
return;
|
|
|
|
LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Parsing patch '{}' ({})",
|
2018-10-01 14:31:34 +02:00
|
|
|
patch_text->GetName(), last_comment, line.substr(1));
|
2018-10-01 00:13:22 +02:00
|
|
|
|
2018-10-01 14:31:34 +02:00
|
|
|
IPSwitchPatch patch{last_comment, enabled, {}};
|
2018-10-01 00:13:22 +02:00
|
|
|
|
|
|
|
// Read rest of patch
|
|
|
|
while (true) {
|
2020-09-22 22:38:43 +02:00
|
|
|
if (i + 1 >= lines.size()) {
|
2018-10-01 00:13:22 +02:00
|
|
|
break;
|
2020-09-22 22:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto& patch_line = lines[++i];
|
2018-10-01 00:13:22 +02:00
|
|
|
|
2018-10-04 18:23:18 +02:00
|
|
|
// Start of new patch
|
|
|
|
if (StartsWith(patch_line, "@enabled") || StartsWith(patch_line, "@disabled")) {
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a flag
|
|
|
|
if (StartsWith(patch_line, "@")) {
|
|
|
|
ParseFlag(patch_line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-01 00:13:22 +02:00
|
|
|
// 11 - 8 hex digit offset + space + minimum two digit overwrite val
|
2018-10-01 23:17:08 +02:00
|
|
|
if (patch_line.length() < 11)
|
2018-10-01 00:13:22 +02:00
|
|
|
break;
|
2023-09-13 19:32:48 +02:00
|
|
|
auto offset = std::strtoul(patch_line.substr(0, 8).c_str(), nullptr, 16);
|
2018-10-09 19:18:20 +02:00
|
|
|
offset += static_cast<unsigned long>(offset_shift);
|
2018-10-01 00:13:22 +02:00
|
|
|
|
|
|
|
std::vector<u8> replace;
|
|
|
|
// 9 - first char of replacement val
|
2018-10-01 23:17:08 +02:00
|
|
|
if (patch_line[9] == '\"') {
|
2018-10-01 00:13:22 +02:00
|
|
|
// string replacement
|
2018-10-04 18:23:18 +02:00
|
|
|
auto end_index = patch_line.find('\"', 10);
|
2018-10-01 00:13:22 +02:00
|
|
|
if (end_index == std::string::npos || end_index < 10)
|
|
|
|
return;
|
2018-10-04 18:23:18 +02:00
|
|
|
while (patch_line[end_index - 1] == '\\') {
|
|
|
|
end_index = patch_line.find('\"', end_index + 1);
|
|
|
|
if (end_index == std::string::npos || end_index < 10)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:17:08 +02:00
|
|
|
auto value = patch_line.substr(10, end_index - 10);
|
2018-10-01 14:31:34 +02:00
|
|
|
value = EscapeStringSequences(value);
|
2018-10-01 00:13:22 +02:00
|
|
|
replace.reserve(value.size());
|
|
|
|
std::copy(value.begin(), value.end(), std::back_inserter(replace));
|
|
|
|
} else {
|
|
|
|
// hex replacement
|
2022-08-12 16:19:44 +02:00
|
|
|
const auto value =
|
|
|
|
patch_line.substr(9, patch_line.find_first_of(" /\r\n", 9) - 9);
|
2018-10-01 00:13:22 +02:00
|
|
|
replace = Common::HexStringToVector(value, is_little_endian);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_values) {
|
|
|
|
LOG_INFO(Loader,
|
|
|
|
"[IPSwitchCompiler ('{}')] - Patching value at offset 0x{:08X} "
|
|
|
|
"with byte string '{}'",
|
2019-06-12 23:27:06 +02:00
|
|
|
patch_text->GetName(), offset, Common::HexToString(replace));
|
2018-10-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:10:50 +02:00
|
|
|
patch.records.insert_or_assign(static_cast<u32>(offset), std::move(replace));
|
2018-10-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
patches.push_back(std::move(patch));
|
2018-10-04 18:23:18 +02:00
|
|
|
} else if (StartsWith(line, "@")) {
|
|
|
|
ParseFlag(line);
|
2018-10-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile IPSwitchCompiler::Apply(const VirtualFile& in) const {
|
|
|
|
if (in == nullptr || !valid)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto in_data = in->ReadAllBytes();
|
|
|
|
|
|
|
|
for (const auto& patch : patches) {
|
|
|
|
if (!patch.enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const auto& record : patch.records) {
|
|
|
|
if (record.first >= in_data.size())
|
|
|
|
continue;
|
|
|
|
auto replace_size = record.second.size();
|
|
|
|
if (record.first + replace_size > in_data.size())
|
|
|
|
replace_size = in_data.size() - record.first;
|
|
|
|
for (std::size_t i = 0; i < replace_size; ++i)
|
|
|
|
in_data[i + record.first] = record.second[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:34:38 +02:00
|
|
|
return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(),
|
|
|
|
in->GetContainingDirectory());
|
2018-10-01 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 04:13:15 +02:00
|
|
|
} // namespace FileSys
|