summaryrefslogtreecommitdiff
path: root/src/core/file_sys
diff options
context:
space:
mode:
authorGravatar Lioncash2018-10-09 13:47:59 -0400
committerGravatar Lioncash2018-10-09 14:10:22 -0400
commit465175cdf58fecf9098c702feaf8d000f0952973 (patch)
tree6e55dc8c602b06c0a25447428e3f7d42f9c271ba /src/core/file_sys
parentips_layer: Remove unnecessary explicit std::pair constructor in std::array (diff)
downloadyuzu-465175cdf58fecf9098c702feaf8d000f0952973.tar.gz
yuzu-465175cdf58fecf9098c702feaf8d000f0952973.tar.xz
yuzu-465175cdf58fecf9098c702feaf8d000f0952973.zip
ips_layer: Avoid constructing std::vector instances where not necessary
We can just compare the existing std::vector instance with a constexpr std::array containing the desired match. This is lighter resource-wise, as we don't need to allocate on the heap.
Diffstat (limited to 'src/core/file_sys')
-rw-r--r--src/core/file_sys/ips_layer.cpp31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp
index 90f91f230..6c072d0a3 100644
--- a/src/core/file_sys/ips_layer.cpp
+++ b/src/core/file_sys/ips_layer.cpp
@@ -2,6 +2,7 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
5#include <cstring> 6#include <cstring>
6#include <map> 7#include <map>
7#include <sstream> 8#include <sstream>
@@ -37,15 +38,33 @@ constexpr std::array<std::pair<const char*, const char*>, 11> ESCAPE_CHARACTER_M
37}}; 38}};
38 39
39static IPSFileType IdentifyMagic(const std::vector<u8>& magic) { 40static IPSFileType IdentifyMagic(const std::vector<u8>& magic) {
40 if (magic.size() != 5) 41 if (magic.size() != 5) {
41 return IPSFileType::Error; 42 return IPSFileType::Error;
42 if (magic == std::vector<u8>{'P', 'A', 'T', 'C', 'H'}) 43 }
44
45 constexpr std::array<u8, 5> patch_magic{{'P', 'A', 'T', 'C', 'H'}};
46 if (std::equal(magic.begin(), magic.end(), patch_magic.begin())) {
43 return IPSFileType::IPS; 47 return IPSFileType::IPS;
44 if (magic == std::vector<u8>{'I', 'P', 'S', '3', '2'}) 48 }
49
50 constexpr std::array<u8, 5> ips32_magic{{'I', 'P', 'S', '3', '2'}};
51 if (std::equal(magic.begin(), magic.end(), ips32_magic.begin())) {
45 return IPSFileType::IPS32; 52 return IPSFileType::IPS32;
53 }
54
46 return IPSFileType::Error; 55 return IPSFileType::Error;
47} 56}
48 57
58static bool IsEOF(IPSFileType type, const std::vector<u8>& data) {
59 constexpr std::array<u8, 3> eof{{'E', 'O', 'F'}};
60 if (type == IPSFileType::IPS && std::equal(data.begin(), data.end(), eof.begin())) {
61 return true;
62 }
63
64 constexpr std::array<u8, 4> eeof{{'E', 'E', 'O', 'F'}};
65 return type == IPSFileType::IPS32 && std::equal(data.begin(), data.end(), eeof.begin());
66}
67
49VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { 68VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
50 if (in == nullptr || ips == nullptr) 69 if (in == nullptr || ips == nullptr)
51 return nullptr; 70 return nullptr;
@@ -60,8 +79,7 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
60 u64 offset = 5; // After header 79 u64 offset = 5; // After header
61 while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) { 80 while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) {
62 offset += temp.size(); 81 offset += temp.size();
63 if (type == IPSFileType::IPS32 && temp == std::vector<u8>{'E', 'E', 'O', 'F'} || 82 if (IsEOF(type, temp)) {
64 type == IPSFileType::IPS && temp == std::vector<u8>{'E', 'O', 'F'}) {
65 break; 83 break;
66 } 84 }
67 85
@@ -101,8 +119,9 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
101 } 119 }
102 } 120 }
103 121
104 if (temp != std::vector<u8>{'E', 'E', 'O', 'F'} && temp != std::vector<u8>{'E', 'O', 'F'}) 122 if (!IsEOF(type, temp)) {
105 return nullptr; 123 return nullptr;
124 }
106 125
107 return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(), 126 return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(),
108 in->GetContainingDirectory()); 127 in->GetContainingDirectory());