From b62978b5a101794345551f4b7722397d9d9d785b Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Thu, 26 Jul 2018 12:04:17 +0200
Subject: [PATCH 1/3] RomFS: add RomFSFile and GetRomFSFile

---
 src/core/hle/romfs.cpp | 24 +++++++++++++++++++++---
 src/core/hle/romfs.h   | 21 +++++++++++++++++++++
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/src/core/hle/romfs.cpp b/src/core/hle/romfs.cpp
index 3157df71d..8278b50de 100644
--- a/src/core/hle/romfs.cpp
+++ b/src/core/hle/romfs.cpp
@@ -53,7 +53,24 @@ static bool MatchName(const u8* buffer, u32 name_length, const std::u16string& n
     return name == std::u16string(name_buffer.begin(), name_buffer.end());
 }
 
+RomFSFile::RomFSFile() : data(nullptr), length(0) {}
+
+RomFSFile::RomFSFile(const u8* data, u64 length) : data(data), length(length) {}
+
+const u8* RomFSFile::Data() const {
+    return data;
+}
+
+u64 RomFSFile::Length() const {
+    return length;
+}
+
 const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path) {
+    RomFSFile file = GetFile(romfs, path);
+    return file.Data();
+}
+
+const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path) {
     constexpr u32 INVALID_FIELD = 0xFFFFFFFF;
 
     // Split path into directory names and file name
@@ -73,7 +90,7 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
         child_dir_offset = dir.first_child_dir_offset;
         while (true) {
             if (child_dir_offset == INVALID_FIELD) {
-                return nullptr;
+                return RomFSFile();
             }
             const u8* current_child_dir = romfs + header.dir_table_offset + child_dir_offset;
             std::memcpy(&dir, current_child_dir, sizeof(dir));
@@ -92,11 +109,12 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
         const u8* current_file = romfs + header.file_table_offset + file_offset;
         std::memcpy(&file, current_file, sizeof(file));
         if (MatchName(current_file + sizeof(file), file.name_length, file_name)) {
-            return romfs + header.data_offset + file.data_offset;
+            RomFSFile res(romfs + header.data_offset + file.data_offset, file.data_length);
+            return res;
         }
         file_offset = file.next_file_offset;
     }
-    return nullptr;
+    return RomFSFile();
 }
 
 } // namespace RomFS
diff --git a/src/core/hle/romfs.h b/src/core/hle/romfs.h
index ee9f29760..dfe2ee788 100644
--- a/src/core/hle/romfs.h
+++ b/src/core/hle/romfs.h
@@ -10,6 +10,18 @@
 
 namespace RomFS {
 
+class RomFSFile {
+public:
+    RomFSFile();
+    RomFSFile(const u8* data, u64 length);
+    const u8* Data() const;
+    u64 Length() const;
+
+private:
+    const u8* data;
+    u64 length;
+};
+
 /**
  * Gets the pointer to a file in a RomFS image.
  * @param romfs The pointer to the RomFS image
@@ -19,4 +31,13 @@ namespace RomFS {
  */
 const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path);
 
+/**
+ * Gets a RomFSFile class to a file in a RomFS image.
+ * @param romfs The pointer to the RomFS image
+ * @param path A vector containing the directory names and file name of the path to the file
+ * @return the RomFSFile to the file
+ * @todo reimplement this with a full RomFS manager
+ */
+const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path);
+
 } // namespace RomFS

From b1f8c2fe67b21c267db404e3fb4815482f5d3b20 Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Thu, 26 Jul 2018 12:59:31 +0200
Subject: [PATCH 2/3] Remove RomFS::GetFilePointer

---
 src/core/hle/romfs.cpp           | 8 +-------
 src/core/hle/romfs.h             | 9 ---------
 src/core/hle/service/apt/apt.cpp | 8 ++++----
 3 files changed, 5 insertions(+), 20 deletions(-)

diff --git a/src/core/hle/romfs.cpp b/src/core/hle/romfs.cpp
index 8278b50de..31064365d 100644
--- a/src/core/hle/romfs.cpp
+++ b/src/core/hle/romfs.cpp
@@ -65,11 +65,6 @@ u64 RomFSFile::Length() const {
     return length;
 }
 
-const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path) {
-    RomFSFile file = GetFile(romfs, path);
-    return file.Data();
-}
-
 const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path) {
     constexpr u32 INVALID_FIELD = 0xFFFFFFFF;
 
@@ -109,8 +104,7 @@ const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path
         const u8* current_file = romfs + header.file_table_offset + file_offset;
         std::memcpy(&file, current_file, sizeof(file));
         if (MatchName(current_file + sizeof(file), file.name_length, file_name)) {
-            RomFSFile res(romfs + header.data_offset + file.data_offset, file.data_length);
-            return res;
+            return RomFSFile(romfs + header.data_offset + file.data_offset, file.data_length);
         }
         file_offset = file.next_file_offset;
     }
diff --git a/src/core/hle/romfs.h b/src/core/hle/romfs.h
index dfe2ee788..a6f5b17a1 100644
--- a/src/core/hle/romfs.h
+++ b/src/core/hle/romfs.h
@@ -22,15 +22,6 @@ private:
     u64 length;
 };
 
-/**
- * Gets the pointer to a file in a RomFS image.
- * @param romfs The pointer to the RomFS image
- * @param path A vector containing the directory names and file name of the path to the file
- * @return the pointer to the file
- * @todo reimplement this with a full RomFS manager
- */
-const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path);
-
 /**
  * Gets a RomFSFile class to a file in a RomFS image.
  * @param romfs The pointer to the RomFS image
diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp
index 98c263b64..b30f42541 100644
--- a/src/core/hle/service/apt/apt.cpp
+++ b/src/core/hle/service/apt/apt.cpp
@@ -143,9 +143,9 @@ bool Module::LoadSharedFont() {
 
     const char16_t* file_name[4] = {u"cbf_std.bcfnt.lz", u"cbf_zh-Hans-CN.bcfnt.lz",
                                     u"cbf_ko-Hang-KR.bcfnt.lz", u"cbf_zh-Hant-TW.bcfnt.lz"};
-    const u8* font_file =
-        RomFS::GetFilePointer(romfs_buffer.data(), {file_name[font_region_code - 1]});
-    if (font_file == nullptr)
+    const RomFS::RomFSFile font_file =
+        RomFS::GetFile(romfs_buffer.data(), {file_name[font_region_code - 1]});
+    if (font_file.Data() == nullptr)
         return false;
 
     struct {
@@ -159,7 +159,7 @@ bool Module::LoadSharedFont() {
     shared_font_header.status = 2; // successfully loaded
     shared_font_header.region = font_region_code;
     shared_font_header.decompressed_size =
-        DecompressLZ11(font_file, shared_font_mem->GetPointer(0x80));
+        DecompressLZ11(font_file.Data(), shared_font_mem->GetPointer(0x80));
     std::memcpy(shared_font_mem->GetPointer(), &shared_font_header, sizeof(shared_font_header));
     *shared_font_mem->GetPointer(0x83) = 'U'; // Change the magic from "CFNT" to "CFNU"
 

From 123c0886e6f7bd70f5bfea8353bbea7ccf46ac86 Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Thu, 26 Jul 2018 13:10:56 +0200
Subject: [PATCH 3/3] RomFSFile: Default constructor

---
 src/core/hle/romfs.cpp | 2 --
 src/core/hle/romfs.h   | 6 +++---
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/core/hle/romfs.cpp b/src/core/hle/romfs.cpp
index 31064365d..efd0ec48e 100644
--- a/src/core/hle/romfs.cpp
+++ b/src/core/hle/romfs.cpp
@@ -53,8 +53,6 @@ static bool MatchName(const u8* buffer, u32 name_length, const std::u16string& n
     return name == std::u16string(name_buffer.begin(), name_buffer.end());
 }
 
-RomFSFile::RomFSFile() : data(nullptr), length(0) {}
-
 RomFSFile::RomFSFile(const u8* data, u64 length) : data(data), length(length) {}
 
 const u8* RomFSFile::Data() const {
diff --git a/src/core/hle/romfs.h b/src/core/hle/romfs.h
index a6f5b17a1..41f1d4700 100644
--- a/src/core/hle/romfs.h
+++ b/src/core/hle/romfs.h
@@ -12,14 +12,14 @@ namespace RomFS {
 
 class RomFSFile {
 public:
-    RomFSFile();
+    RomFSFile() = default;
     RomFSFile(const u8* data, u64 length);
     const u8* Data() const;
     u64 Length() const;
 
 private:
-    const u8* data;
-    u64 length;
+    const u8* data = nullptr;
+    u64 length = 0;
 };
 
 /**