From cea19fd659496bcdf09a12b163ce490c1fa71ff7 Mon Sep 17 00:00:00 2001
From: TheKoopaKingdom <thekoopakingdom@gmail.com>
Date: Tue, 23 May 2017 19:46:30 -0400
Subject: [PATCH] Moved whitelist checks from FS_User to the Archive_NCCH
 handler.

---
 src/core/file_sys/archive_ncch.cpp  | 36 ++++++++++++++++++-
 src/core/hle/service/fs/fs_user.cpp | 54 ++---------------------------
 2 files changed, 37 insertions(+), 53 deletions(-)

diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp
index bf4e0916b7..84950f8717 100644
--- a/src/core/file_sys/archive_ncch.cpp
+++ b/src/core/file_sys/archive_ncch.cpp
@@ -9,6 +9,7 @@
 #include "common/file_util.h"
 #include "common/logging/log.h"
 #include "common/string_util.h"
+#include "core/core.h"
 #include "core/file_sys/archive_ncch.h"
 #include "core/file_sys/ivfc_archive.h"
 #include "core/hle/service/fs/archive.h"
@@ -33,10 +34,43 @@ ArchiveFactory_NCCH::ArchiveFactory_NCCH(const std::string& nand_directory)
 ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_NCCH::Open(const Path& path) {
     auto vec = path.AsBinary();
     const u32* data = reinterpret_cast<u32*>(vec.data());
-    std::string file_path = GetNCCHPath(mount_point, data[1], data[0]);
+    u32 high = data[1];
+    u32 low = data[0];
+    std::string file_path = GetNCCHPath(mount_point, high, low);
     auto file = std::make_shared<FileUtil::IOFile>(file_path, "rb");
 
     if (!file->IsOpen()) {
+        // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
+        const u32 shared_data_archive = 0x0004009B;
+        const u32 system_data_archive = 0x000400DB;
+
+        // Low Title IDs.
+        const u32 mii_data = 0x00010202;
+        const u32 region_manifest = 0x00010402;
+        const u32 ng_word_list = 0x00010302;
+
+        LOG_DEBUG(Service_FS, "Full Path: %s. Category: 0x%X. Path: 0x%X.", path.DebugStr().c_str(),
+                  high, low);
+
+        if (high == shared_data_archive) {
+            if (low == mii_data) {
+                LOG_ERROR(Service_FS, "Failed to get a handle for shared data archive: Mii data. ");
+                Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
+                                                      "Mii data");
+            } else if (low == region_manifest) {
+                LOG_ERROR(Service_FS,
+                          "Failed to get a handle for shared data archive: region manifes");
+                Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
+                                                      "Region manifest");
+            }
+        } else if (high == system_data_archive) {
+            if (low == ng_word_list) {
+                LOG_ERROR(Service_FS,
+                          "Failed to get a handle for system data archive: NG bad word list.");
+                Core::System::GetInstance().SetStatus(Core::System::ResultStatus::ErrorSystemFiles,
+                                                      "NG bad word list");
+            }
+        }
         return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
                           ErrorLevel::Status);
     }
diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
index c65d46238b..c1825e9c8b 100644
--- a/src/core/hle/service/fs/fs_user.cpp
+++ b/src/core/hle/service/fs/fs_user.cpp
@@ -130,61 +130,11 @@ static void OpenFileDirectly(Service::Interface* self) {
 
     ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id, archive_path);
     if (archive_handle.Failed()) {
-        cmd_buff[1] = archive_handle.Code().raw;
-        cmd_buff[3] = 0;
-
-        if (static_cast<FS::ArchiveIdCode>(archive_id) == ArchiveIdCode::NCCH) {
-            // High Title ID of the archive: The category (https://3dbrew.org/wiki/Title_list).
-            // (Note: The values there are big endian, these must be little endian.)
-            const std::vector<u8> shared_data_archive = {0x9B, 0x00, 0x04, 0x00};
-            const std::vector<u8> system_data_archive = {0xDB, 0x00, 0x04, 0x00};
-
-            // Low Title IDs.
-            const std::vector<u8> mii_data = {0x02, 0x02, 0x01, 0x00};
-            const std::vector<u8> region_manifest = {0x02, 0x04, 0x01, 0x00};
-            const std::vector<u8> ng_word_list = {0x02, 0x03, 0x01, 0x00};
-
-            // Make a copy of the binary path because reusing AsBinary() for creating category
-            // results in bad_alloc being thrown.
-            std::vector<u8> binary_archive_path = archive_path.AsBinary();
-            std::vector<u8> category(binary_archive_path.begin() + 4,
-                                     binary_archive_path.begin() + 8);
-            std::vector<u8> path(binary_archive_path.begin(), binary_archive_path.begin() + 4);
-
-            if (category == shared_data_archive) {
-                if (path == mii_data) {
-                    LOG_ERROR(Service_FS,
-                              "Failed to get a handle for shared data archive: Mii data. "
-                              "Archive ID=0x%08X Archive Path=%s",
-                              static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
-                    Core::System::GetInstance().SetStatus(
-                        Core::System::ResultStatus::ErrorSystemFiles, "Mii data");
-                    return;
-                } else if (path == region_manifest) {
-                    LOG_ERROR(Service_FS,
-                              "Failed to get a handle for shared data archive: region manifest. "
-                              "Archive ID=0x%08X Archive Path=%s",
-                              static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
-                    Core::System::GetInstance().SetStatus(
-                        Core::System::ResultStatus::ErrorSystemFiles, "Region manifest");
-                    return;
-                }
-            } else if (category == system_data_archive) {
-                if (path == ng_word_list) {
-                    LOG_ERROR(Service_FS,
-                              "Failed to get a handle for system data archive: NG bad word list. "
-                              "Archive ID=0x%08X Archive Path=%s",
-                              static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
-                    Core::System::GetInstance().SetStatus(
-                        Core::System::ResultStatus::ErrorSystemFiles, "NG bad word list");
-                    return;
-                }
-            }
-        }
-
         LOG_ERROR(Service_FS,
                   "Failed to get a handle for archive archive_id=0x%08X archive_path=%s",
                   static_cast<u32>(archive_id), archive_path.DebugStr().c_str());
+        cmd_buff[1] = archive_handle.Code().raw;
+        cmd_buff[3] = 0;
         return;
     }
     SCOPE_EXIT({ CloseArchive(*archive_handle); });