From 2496ecbaf6dacd7f146073635c020f7c40002793 Mon Sep 17 00:00:00 2001
From: Zak Kurka <zakkurka@gmail.com>
Date: Sat, 20 Oct 2018 13:09:05 -0500
Subject: [PATCH] Added IPS patching

---
 src/core/file_sys/ncch_container.cpp | 38 ++++++++++++++++++++++++++++
 src/core/file_sys/ncch_container.h   |  8 ++++++
 2 files changed, 46 insertions(+)

diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp
index 4e9b2efa7..c9be5bf80 100644
--- a/src/core/file_sys/ncch_container.cpp
+++ b/src/core/file_sys/ncch_container.cpp
@@ -481,6 +481,21 @@ Loader::ResultStatus NCCHContainer::LoadSectionExeFS(const char* name, std::vect
                     dec.ProcessData(&buffer[0], &buffer[0], section.size);
                 }
             }
+
+            std::string override_ips = filepath + ".exefsdir/code.ips";
+
+            if (FileUtil::Exists(override_ips) && strcmp(name, ".code") == 0) {
+                FileUtil::IOFile ips_file(override_ips, "rb");
+                size_t ips_file_size = ips_file.GetSize();
+                std::vector<u8> ips(ips_file_size);
+
+                if (ips_file.IsOpen() &&
+                    ips_file.ReadBytes(&ips[0], ips_file_size) == ips_file_size) {
+                    LOG_WARNING(Service_FS, "File {} patching code.bin", override_ips);
+                    ApplyIPS(ips, buffer);
+                }
+            }
+
             return Loader::ResultStatus::Success;
         }
     }
@@ -519,6 +534,29 @@ Loader::ResultStatus NCCHContainer::LoadOverrideExeFSSection(const char* name,
     return Loader::ResultStatus::ErrorNotUsed;
 }
 
+Loader::ResultStatus NCCHContainer::ApplyIPS(std::vector<u8>& ips, std::vector<u8>& buffer) {
+    u32 cursor = 5;
+    u32 patch_length = ips.size() - 3;
+    std::string ips_header(ips.begin(), ips.begin() + 5);
+
+    if (strcmp(ips_header.c_str(), "PATCH"))
+        return Loader::ResultStatus::Error;
+
+    while (cursor < patch_length) {
+        u32 offset = (ips[cursor]) << 16 | (ips[cursor + 1]) << 8 | (ips[cursor + 2]);
+        u32 length = (ips[cursor + 3]) << 8 | (ips[cursor + 4]);
+        cursor += 5;
+
+        for (int i = 0; i < length; i++) {
+            buffer[offset + i] = ips[cursor + i];
+        }
+
+        cursor += length;
+    }
+
+    return Loader::ResultStatus::Success;
+}
+
 Loader::ResultStatus NCCHContainer::ReadRomFS(std::shared_ptr<RomFSReader>& romfs_file) {
     Loader::ResultStatus result = Load();
     if (result != Loader::ResultStatus::Success)
diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h
index 79f87a6ef..8d6665e7a 100644
--- a/src/core/file_sys/ncch_container.h
+++ b/src/core/file_sys/ncch_container.h
@@ -249,6 +249,14 @@ public:
      */
     Loader::ResultStatus ReadRomFS(std::shared_ptr<RomFSReader>& romfs_file);
 
+    /**
+     * Attempts to patch a buffer using an IPS
+     * @param ips Vector of the patches to apply
+     * @param buffer Vector to patch data into
+     * @return ResultStatus result of function
+     */
+    Loader::ResultStatus ApplyIPS(std::vector<u8>& ips, std::vector<u8>& buffer);
+
     /**
      * Get the override RomFS of the NCCH container
      * Since the RomFS can be huge, we return a file reference instead of copying to a buffer