2021-12-07 22:47:41 +01:00
|
|
|
#include <hex/helpers/patches.hpp>
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <string_view>
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PatchesGenerator : public hex::prv::Provider {
|
|
|
|
public:
|
|
|
|
explicit PatchesGenerator() = default;
|
|
|
|
~PatchesGenerator() override = default;
|
|
|
|
|
|
|
|
[[nodiscard]] bool isAvailable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isReadable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isWritable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isResizable() const override { return true; }
|
|
|
|
[[nodiscard]] bool isSavable() const override { return false; }
|
|
|
|
[[nodiscard]] bool isSavableAsRecent() const override { return false; }
|
|
|
|
|
|
|
|
[[nodiscard]] bool open() override { return true; }
|
|
|
|
void close() override { }
|
|
|
|
|
|
|
|
void readRaw(u64 offset, void *buffer, size_t size) override {
|
|
|
|
hex::unused(offset, buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeRaw(u64 offset, const void *buffer, size_t size) override {
|
|
|
|
for (u64 i = 0; i < size; i += 1)
|
|
|
|
this->m_patches[offset] = static_cast<const u8*>(buffer)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] size_t getActualSize() const override {
|
|
|
|
if (this->m_patches.empty())
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return this->m_patches.rbegin()->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resizeRaw(size_t newSize) override {
|
|
|
|
hex::unused(newSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertRaw(u64 offset, size_t size) override {
|
|
|
|
std::vector<std::pair<u64, u8>> patchesToMove;
|
|
|
|
|
|
|
|
for (auto &[address, value] : this->m_patches) {
|
|
|
|
if (address > offset)
|
|
|
|
patchesToMove.emplace_back(address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
this->m_patches.erase(address);
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
this->m_patches.insert({ address + size, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeRaw(u64 offset, size_t size) override {
|
|
|
|
std::vector<std::pair<u64, u8>> patchesToMove;
|
|
|
|
|
|
|
|
for (auto &[address, value] : this->m_patches) {
|
|
|
|
if (address > offset)
|
|
|
|
patchesToMove.emplace_back(address, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
this->m_patches.erase(address);
|
|
|
|
for (const auto &[address, value] : patchesToMove)
|
|
|
|
this->m_patches.insert({ address - size, value });
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string getName() const override {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] std::string getTypeName() const override { return ""; }
|
|
|
|
|
|
|
|
const std::map<u64, u8>& getPatches() const {
|
|
|
|
return this->m_patches;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::map<u64, u8> m_patches;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void pushStringBack(std::vector<u8> &buffer, const std::string &string) {
|
|
|
|
std::copy(string.begin(), string.end(), std::back_inserter(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void pushBytesBack(std::vector<u8> &buffer, T bytes) {
|
|
|
|
buffer.resize(buffer.size() + sizeof(T));
|
|
|
|
std::memcpy((&buffer.back() - sizeof(T)) + 1, &bytes, sizeof(T));
|
|
|
|
}
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
wolv::util::Expected<std::vector<u8>, IPSError> Patches::toIPSPatch() const {
|
2020-11-27 09:09:48 +01:00
|
|
|
std::vector<u8> result;
|
|
|
|
|
2022-01-09 20:16:05 +01:00
|
|
|
pushStringBack(result, "PATCH");
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
std::vector<u64> addresses;
|
|
|
|
std::vector<u8> values;
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
for (const auto &[address, value] : this->m_patches) {
|
2020-11-27 09:09:48 +01:00
|
|
|
addresses.push_back(address);
|
|
|
|
values.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u64> startAddress;
|
|
|
|
std::vector<u8> bytes;
|
|
|
|
for (u32 i = 0; i < addresses.size(); i++) {
|
|
|
|
if (!startAddress.has_value())
|
|
|
|
startAddress = addresses[i];
|
|
|
|
|
|
|
|
if (i != addresses.size() - 1 && addresses[i] == (addresses[i + 1] - 1)) {
|
|
|
|
bytes.push_back(values[i]);
|
|
|
|
} else {
|
|
|
|
bytes.push_back(values[i]);
|
|
|
|
|
2023-01-25 10:38:04 +01:00
|
|
|
if (bytes.size() > 0xFFFF)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::PatchTooLarge);
|
2023-01-25 10:38:04 +01:00
|
|
|
if (startAddress > 0xFFFF'FFFF)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::AddressOutOfRange);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 address = startAddress.value();
|
2022-01-24 20:53:17 +01:00
|
|
|
auto addressBytes = reinterpret_cast<u8 *>(&address);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
result.push_back(addressBytes[2]);
|
|
|
|
result.push_back(addressBytes[1]);
|
|
|
|
result.push_back(addressBytes[0]);
|
2020-11-27 09:09:48 +01:00
|
|
|
pushBytesBack<u16>(result, changeEndianess<u16>(bytes.size(), std::endian::big));
|
|
|
|
|
|
|
|
for (auto byte : bytes)
|
|
|
|
result.push_back(byte);
|
|
|
|
|
|
|
|
bytes.clear();
|
2022-01-24 20:53:17 +01:00
|
|
|
startAddress = {};
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:16:05 +01:00
|
|
|
pushStringBack(result, "EOF");
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
wolv::util::Expected<std::vector<u8>, IPSError> Patches::toIPS32Patch() const {
|
2020-11-27 09:09:48 +01:00
|
|
|
std::vector<u8> result;
|
|
|
|
|
2022-01-09 20:16:05 +01:00
|
|
|
pushStringBack(result, "IPS32");
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
std::vector<u64> addresses;
|
|
|
|
std::vector<u8> values;
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
for (const auto &[address, value] : this->m_patches) {
|
2020-11-27 09:09:48 +01:00
|
|
|
addresses.push_back(address);
|
|
|
|
values.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u64> startAddress;
|
|
|
|
std::vector<u8> bytes;
|
|
|
|
for (u32 i = 0; i < addresses.size(); i++) {
|
|
|
|
if (!startAddress.has_value())
|
|
|
|
startAddress = addresses[i];
|
|
|
|
|
|
|
|
if (i != addresses.size() - 1 && addresses[i] == (addresses[i + 1] - 1)) {
|
|
|
|
bytes.push_back(values[i]);
|
|
|
|
} else {
|
|
|
|
bytes.push_back(values[i]);
|
|
|
|
|
2023-01-25 10:38:04 +01:00
|
|
|
if (bytes.size() > 0xFFFF)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::PatchTooLarge);
|
2023-01-25 10:38:04 +01:00
|
|
|
if (startAddress > 0xFFFF'FFFF)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::AddressOutOfRange);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
u32 address = startAddress.value();
|
2022-01-24 20:53:17 +01:00
|
|
|
auto addressBytes = reinterpret_cast<u8 *>(&address);
|
2020-11-27 09:09:48 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
result.push_back(addressBytes[3]);
|
|
|
|
result.push_back(addressBytes[2]);
|
|
|
|
result.push_back(addressBytes[1]);
|
|
|
|
result.push_back(addressBytes[0]);
|
2020-11-27 09:09:48 +01:00
|
|
|
pushBytesBack<u16>(result, changeEndianess<u16>(bytes.size(), std::endian::big));
|
|
|
|
|
|
|
|
for (auto byte : bytes)
|
|
|
|
result.push_back(byte);
|
|
|
|
|
|
|
|
bytes.clear();
|
2022-01-24 20:53:17 +01:00
|
|
|
startAddress = {};
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:16:05 +01:00
|
|
|
pushStringBack(result, "EEOF");
|
2020-11-27 09:09:48 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
wolv::util::Expected<Patches, IPSError> Patches::fromProvider(hex::prv::Provider* provider) {
|
|
|
|
PatchesGenerator generator;
|
|
|
|
|
|
|
|
generator.getUndoStack().apply(provider->getUndoStack());
|
|
|
|
|
|
|
|
if (generator.getActualSize() > 0xFFFF'FFFF)
|
|
|
|
return wolv::util::Unexpected(IPSError::PatchTooLarge);
|
|
|
|
|
|
|
|
auto patches = generator.getPatches();
|
|
|
|
|
|
|
|
return Patches(std::move(patches));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wolv::util::Expected<Patches, IPSError> Patches::fromIPSPatch(const std::vector<u8> &ipsPatch) {
|
2020-11-29 15:09:36 +01:00
|
|
|
if (ipsPatch.size() < (5 + 3))
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchHeader);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
const char *header = "PATCH";
|
|
|
|
if (std::memcmp(ipsPatch.data(), header, 5) != 0)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchHeader);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
Patches result;
|
|
|
|
bool foundEOF = false;
|
|
|
|
|
|
|
|
u32 ipsOffset = 5;
|
|
|
|
while (ipsOffset < ipsPatch.size() - (5 + 3)) {
|
|
|
|
u32 offset = ipsPatch[ipsOffset + 2] | (ipsPatch[ipsOffset + 1] << 8) | (ipsPatch[ipsOffset + 0] << 16);
|
2022-02-01 22:09:44 +01:00
|
|
|
u16 size = ipsPatch[ipsOffset + 4] | (ipsPatch[ipsOffset + 3] << 8);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
ipsOffset += 5;
|
|
|
|
|
|
|
|
// Handle normal record
|
|
|
|
if (size > 0x0000) {
|
|
|
|
if (ipsOffset + size > ipsPatch.size() - 3)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchFormat);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
for (u16 i = 0; i < size; i++)
|
2023-11-25 12:43:48 +01:00
|
|
|
result.get()[offset + i] = ipsPatch[ipsOffset + i];
|
2020-11-29 15:09:36 +01:00
|
|
|
ipsOffset += size;
|
|
|
|
}
|
|
|
|
// Handle RLE record
|
|
|
|
else {
|
|
|
|
if (ipsOffset + 3 > ipsPatch.size() - 3)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchFormat);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
u16 rleSize = ipsPatch[ipsOffset + 0] | (ipsPatch[ipsOffset + 1] << 8);
|
|
|
|
|
|
|
|
ipsOffset += 2;
|
|
|
|
|
|
|
|
for (u16 i = 0; i < rleSize; i++)
|
2023-11-25 12:43:48 +01:00
|
|
|
result.get()[offset + i] = ipsPatch[ipsOffset + 0];
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
ipsOffset += 1;
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
const char *footer = "EOF";
|
|
|
|
if (std::memcmp(ipsPatch.data() + ipsOffset, footer, 3) == 0)
|
2020-11-29 15:09:36 +01:00
|
|
|
foundEOF = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundEOF)
|
|
|
|
return result;
|
|
|
|
else
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::MissingEOF);
|
2020-11-29 15:09:36 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 12:43:48 +01:00
|
|
|
wolv::util::Expected<Patches, IPSError> Patches::fromIPS32Patch(const std::vector<u8> &ipsPatch) {
|
2020-11-29 15:09:36 +01:00
|
|
|
if (ipsPatch.size() < (5 + 4))
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchHeader);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
const char *header = "IPS32";
|
|
|
|
if (std::memcmp(ipsPatch.data(), header, 5) != 0)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchHeader);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
Patches result;
|
|
|
|
bool foundEEOF = false;
|
|
|
|
|
|
|
|
u32 ipsOffset = 5;
|
|
|
|
while (ipsOffset < ipsPatch.size() - (5 + 4)) {
|
|
|
|
u32 offset = ipsPatch[ipsOffset + 3] | (ipsPatch[ipsOffset + 2] << 8) | (ipsPatch[ipsOffset + 1] << 16) | (ipsPatch[ipsOffset + 0] << 24);
|
2022-02-01 22:09:44 +01:00
|
|
|
u16 size = ipsPatch[ipsOffset + 5] | (ipsPatch[ipsOffset + 4] << 8);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
ipsOffset += 6;
|
|
|
|
|
|
|
|
// Handle normal record
|
|
|
|
if (size > 0x0000) {
|
|
|
|
if (ipsOffset + size > ipsPatch.size() - 3)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchFormat);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
for (u16 i = 0; i < size; i++)
|
2023-11-25 12:43:48 +01:00
|
|
|
result.get()[offset + i] = ipsPatch[ipsOffset + i];
|
2020-11-29 15:09:36 +01:00
|
|
|
ipsOffset += size;
|
|
|
|
}
|
|
|
|
// Handle RLE record
|
|
|
|
else {
|
|
|
|
if (ipsOffset + 3 > ipsPatch.size() - 3)
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::InvalidPatchFormat);
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
u16 rleSize = ipsPatch[ipsOffset + 0] | (ipsPatch[ipsOffset + 1] << 8);
|
|
|
|
|
|
|
|
ipsOffset += 2;
|
|
|
|
|
|
|
|
for (u16 i = 0; i < rleSize; i++)
|
2023-11-25 12:43:48 +01:00
|
|
|
result.get()[offset + i] = ipsPatch[ipsOffset + 0];
|
2020-11-29 15:09:36 +01:00
|
|
|
|
|
|
|
ipsOffset += 1;
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
const char *footer = "EEOF";
|
|
|
|
if (std::memcmp(ipsPatch.data() + ipsOffset, footer, 4) == 0)
|
2020-11-29 15:09:36 +01:00
|
|
|
foundEEOF = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundEEOF)
|
|
|
|
return result;
|
|
|
|
else
|
2023-11-02 08:53:46 +01:00
|
|
|
return wolv::util::Unexpected(IPSError::MissingEOF);
|
2020-11-29 15:09:36 +01:00
|
|
|
}
|
|
|
|
|
2020-11-27 09:09:48 +01:00
|
|
|
}
|