From 89410c164ad023d2dd24961b435733123e538d9d Mon Sep 17 00:00:00 2001 From: fadillzzz Date: Mon, 23 Aug 2021 03:24:35 +0700 Subject: [PATCH] fix(bps): Fixes BPS patch changing target size (#5829) --- src/core/file_sys/patch.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/core/file_sys/patch.cpp b/src/core/file_sys/patch.cpp index f4fb05ac2..725c6a48d 100644 --- a/src/core/file_sys/patch.cpp +++ b/src/core/file_sys/patch.cpp @@ -63,6 +63,7 @@ namespace Bps { // Realistically uint32s are more than enough for code patching. using Number = u32; +constexpr std::size_t MagicSize = 4; constexpr std::size_t FooterSize = 12; // The BPS format uses CRC32 checksums. @@ -149,7 +150,7 @@ public: : m_source{source}, m_target{target}, m_patch{patch} {} bool Apply() { - const auto magic = *m_patch.Read>(); + const auto magic = *m_patch.Read>(); if (std::string_view(magic.data(), magic.size()) != "BPS1") { LOG_ERROR(Service_FS, "Invalid BPS magic"); return false; @@ -257,10 +258,24 @@ private: } // namespace Bps bool ApplyBpsPatch(const std::vector& patch, std::vector& buffer) { + Bps::Stream patch_stream{patch.data(), patch.size()}; + + // Move the offset past the file format marker (i.e. "BPS1") + patch_stream.Seek(Bps::MagicSize); + + const Bps::Number source_size = patch_stream.ReadNumber(); + const Bps::Number target_size = patch_stream.ReadNumber(); + + if (target_size > source_size) { + LOG_INFO(Service_FS, "Resizing target to {}", target_size); + buffer.resize(target_size); + } + + patch_stream.Seek(0); + const std::vector source = buffer; Bps::Stream source_stream{source.data(), source.size()}; Bps::Stream target_stream{buffer.data(), buffer.size()}; - Bps::Stream patch_stream{patch.data(), patch.size()}; Bps::PatchApplier applier{source_stream, target_stream, patch_stream}; return applier.Apply(); }