ips_layer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <map>
  6. #include <sstream>
  7. #include <string>
  8. #include <utility>
  9. #include "common/hex_util.h"
  10. #include "common/logging/log.h"
  11. #include "common/swap.h"
  12. #include "core/file_sys/ips_layer.h"
  13. #include "core/file_sys/vfs_vector.h"
  14. namespace FileSys {
  15. enum class IPSFileType {
  16. IPS,
  17. IPS32,
  18. Error,
  19. };
  20. constexpr std::array<std::pair<const char*, const char*>, 11> ESCAPE_CHARACTER_MAP{{
  21. {"\\a", "\a"},
  22. {"\\b", "\b"},
  23. {"\\f", "\f"},
  24. {"\\n", "\n"},
  25. {"\\r", "\r"},
  26. {"\\t", "\t"},
  27. {"\\v", "\v"},
  28. {"\\\\", "\\"},
  29. {"\\\'", "\'"},
  30. {"\\\"", "\""},
  31. {"\\\?", "\?"},
  32. }};
  33. static IPSFileType IdentifyMagic(const std::vector<u8>& magic) {
  34. if (magic.size() != 5) {
  35. return IPSFileType::Error;
  36. }
  37. static constexpr std::array<u8, 5> patch_magic{{'P', 'A', 'T', 'C', 'H'}};
  38. if (std::equal(magic.begin(), magic.end(), patch_magic.begin())) {
  39. return IPSFileType::IPS;
  40. }
  41. static constexpr std::array<u8, 5> ips32_magic{{'I', 'P', 'S', '3', '2'}};
  42. if (std::equal(magic.begin(), magic.end(), ips32_magic.begin())) {
  43. return IPSFileType::IPS32;
  44. }
  45. return IPSFileType::Error;
  46. }
  47. static bool IsEOF(IPSFileType type, const std::vector<u8>& data) {
  48. static constexpr std::array<u8, 3> eof{{'E', 'O', 'F'}};
  49. if (type == IPSFileType::IPS && std::equal(data.begin(), data.end(), eof.begin())) {
  50. return true;
  51. }
  52. static constexpr std::array<u8, 4> eeof{{'E', 'E', 'O', 'F'}};
  53. return type == IPSFileType::IPS32 && std::equal(data.begin(), data.end(), eeof.begin());
  54. }
  55. VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
  56. if (in == nullptr || ips == nullptr)
  57. return nullptr;
  58. const auto type = IdentifyMagic(ips->ReadBytes(0x5));
  59. if (type == IPSFileType::Error)
  60. return nullptr;
  61. auto in_data = in->ReadAllBytes();
  62. std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4);
  63. u64 offset = 5; // After header
  64. while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) {
  65. offset += temp.size();
  66. if (IsEOF(type, temp)) {
  67. break;
  68. }
  69. u32 real_offset{};
  70. if (type == IPSFileType::IPS32)
  71. real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3];
  72. else
  73. real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2];
  74. u16 data_size{};
  75. if (ips->ReadObject(&data_size, offset) != sizeof(u16))
  76. return nullptr;
  77. data_size = Common::swap16(data_size);
  78. offset += sizeof(u16);
  79. if (data_size == 0) { // RLE
  80. u16 rle_size{};
  81. if (ips->ReadObject(&rle_size, offset) != sizeof(u16))
  82. return nullptr;
  83. rle_size = Common::swap16(rle_size);
  84. offset += sizeof(u16);
  85. const auto data = ips->ReadByte(offset++);
  86. if (!data)
  87. return nullptr;
  88. if (real_offset + rle_size > in_data.size())
  89. rle_size = static_cast<u16>(in_data.size() - real_offset);
  90. std::memset(in_data.data() + real_offset, *data, rle_size);
  91. } else { // Standard Patch
  92. auto read = data_size;
  93. if (real_offset + read > in_data.size())
  94. read = static_cast<u16>(in_data.size() - real_offset);
  95. if (ips->Read(in_data.data() + real_offset, read, offset) != data_size)
  96. return nullptr;
  97. offset += data_size;
  98. }
  99. }
  100. if (!IsEOF(type, temp)) {
  101. return nullptr;
  102. }
  103. return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(),
  104. in->GetContainingDirectory());
  105. }
  106. struct IPSwitchCompiler::IPSwitchPatch {
  107. std::string name;
  108. bool enabled;
  109. std::map<u32, std::vector<u8>> records;
  110. };
  111. IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_) : patch_text(std::move(patch_text_)) {
  112. Parse();
  113. }
  114. IPSwitchCompiler::~IPSwitchCompiler() = default;
  115. std::array<u8, 32> IPSwitchCompiler::GetBuildID() const {
  116. return nso_build_id;
  117. }
  118. bool IPSwitchCompiler::IsValid() const {
  119. return valid;
  120. }
  121. static bool StartsWith(std::string_view base, std::string_view check) {
  122. return base.size() >= check.size() && base.substr(0, check.size()) == check;
  123. }
  124. static std::string EscapeStringSequences(std::string in) {
  125. for (const auto& seq : ESCAPE_CHARACTER_MAP) {
  126. for (auto index = in.find(seq.first); index != std::string::npos;
  127. index = in.find(seq.first, index)) {
  128. in.replace(index, std::strlen(seq.first), seq.second);
  129. index += std::strlen(seq.second);
  130. }
  131. }
  132. return in;
  133. }
  134. void IPSwitchCompiler::ParseFlag(const std::string& line) {
  135. if (StartsWith(line, "@flag offset_shift ")) {
  136. // Offset Shift Flag
  137. offset_shift = std::strtoll(line.substr(19).c_str(), nullptr, 0);
  138. } else if (StartsWith(line, "@little-endian")) {
  139. // Set values to read as little endian
  140. is_little_endian = true;
  141. } else if (StartsWith(line, "@big-endian")) {
  142. // Set values to read as big endian
  143. is_little_endian = false;
  144. } else if (StartsWith(line, "@flag print_values")) {
  145. // Force printing of applied values
  146. print_values = true;
  147. }
  148. }
  149. void IPSwitchCompiler::Parse() {
  150. const auto bytes = patch_text->ReadAllBytes();
  151. std::stringstream s;
  152. s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
  153. std::vector<std::string> lines;
  154. std::string stream_line;
  155. while (std::getline(s, stream_line)) {
  156. // Remove a trailing \r
  157. if (!stream_line.empty() && stream_line.back() == '\r')
  158. stream_line.pop_back();
  159. lines.push_back(std::move(stream_line));
  160. }
  161. for (std::size_t i = 0; i < lines.size(); ++i) {
  162. auto line = lines[i];
  163. // Remove midline comments
  164. std::size_t comment_index = std::string::npos;
  165. bool within_string = false;
  166. for (std::size_t k = 0; k < line.size(); ++k) {
  167. if (line[k] == '\"' && (k > 0 && line[k - 1] != '\\')) {
  168. within_string = !within_string;
  169. } else if (line[k] == '\\' && (k < line.size() - 1 && line[k + 1] == '\\')) {
  170. comment_index = k;
  171. break;
  172. }
  173. }
  174. if (!StartsWith(line, "//") && comment_index != std::string::npos) {
  175. last_comment = line.substr(comment_index + 2);
  176. line = line.substr(0, comment_index);
  177. }
  178. if (StartsWith(line, "@stop")) {
  179. // Force stop
  180. break;
  181. } else if (StartsWith(line, "@nsobid-")) {
  182. // NSO Build ID Specifier
  183. const auto raw_build_id = fmt::format("{:0<64}", line.substr(8));
  184. nso_build_id = Common::HexStringToArray<0x20>(raw_build_id);
  185. } else if (StartsWith(line, "#")) {
  186. // Mandatory Comment
  187. LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Forced output comment: {}",
  188. patch_text->GetName(), line.substr(1));
  189. } else if (StartsWith(line, "//")) {
  190. // Normal Comment
  191. last_comment = line.substr(2);
  192. if (last_comment.find_first_not_of(' ') == std::string::npos)
  193. continue;
  194. if (last_comment.find_first_not_of(' ') != 0)
  195. last_comment = last_comment.substr(last_comment.find_first_not_of(' '));
  196. } else if (StartsWith(line, "@enabled") || StartsWith(line, "@disabled")) {
  197. // Start of patch
  198. const auto enabled = StartsWith(line, "@enabled");
  199. if (i == 0)
  200. return;
  201. LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Parsing patch '{}' ({})",
  202. patch_text->GetName(), last_comment, line.substr(1));
  203. IPSwitchPatch patch{last_comment, enabled, {}};
  204. // Read rest of patch
  205. while (true) {
  206. if (i + 1 >= lines.size()) {
  207. break;
  208. }
  209. const auto& patch_line = lines[++i];
  210. // Start of new patch
  211. if (StartsWith(patch_line, "@enabled") || StartsWith(patch_line, "@disabled")) {
  212. --i;
  213. break;
  214. }
  215. // Check for a flag
  216. if (StartsWith(patch_line, "@")) {
  217. ParseFlag(patch_line);
  218. continue;
  219. }
  220. // 11 - 8 hex digit offset + space + minimum two digit overwrite val
  221. if (patch_line.length() < 11)
  222. break;
  223. auto offset = std::strtoul(patch_line.substr(0, 8).c_str(), nullptr, 16);
  224. offset += static_cast<unsigned long>(offset_shift);
  225. std::vector<u8> replace;
  226. // 9 - first char of replacement val
  227. if (patch_line[9] == '\"') {
  228. // string replacement
  229. auto end_index = patch_line.find('\"', 10);
  230. if (end_index == std::string::npos || end_index < 10)
  231. return;
  232. while (patch_line[end_index - 1] == '\\') {
  233. end_index = patch_line.find('\"', end_index + 1);
  234. if (end_index == std::string::npos || end_index < 10)
  235. return;
  236. }
  237. auto value = patch_line.substr(10, end_index - 10);
  238. value = EscapeStringSequences(value);
  239. replace.reserve(value.size());
  240. std::copy(value.begin(), value.end(), std::back_inserter(replace));
  241. } else {
  242. // hex replacement
  243. const auto value =
  244. patch_line.substr(9, patch_line.find_first_of(" /\r\n", 9) - 9);
  245. replace = Common::HexStringToVector(value, is_little_endian);
  246. }
  247. if (print_values) {
  248. LOG_INFO(Loader,
  249. "[IPSwitchCompiler ('{}')] - Patching value at offset 0x{:08X} "
  250. "with byte string '{}'",
  251. patch_text->GetName(), offset, Common::HexToString(replace));
  252. }
  253. patch.records.insert_or_assign(static_cast<u32>(offset), std::move(replace));
  254. }
  255. patches.push_back(std::move(patch));
  256. } else if (StartsWith(line, "@")) {
  257. ParseFlag(line);
  258. }
  259. }
  260. valid = true;
  261. }
  262. VirtualFile IPSwitchCompiler::Apply(const VirtualFile& in) const {
  263. if (in == nullptr || !valid)
  264. return nullptr;
  265. auto in_data = in->ReadAllBytes();
  266. for (const auto& patch : patches) {
  267. if (!patch.enabled)
  268. continue;
  269. for (const auto& record : patch.records) {
  270. if (record.first >= in_data.size())
  271. continue;
  272. auto replace_size = record.second.size();
  273. if (record.first + replace_size > in_data.size())
  274. replace_size = in_data.size() - record.first;
  275. for (std::size_t i = 0; i < replace_size; ++i)
  276. in_data[i + record.first] = record.second[i];
  277. }
  278. }
  279. return std::make_shared<VectorVfsFile>(std::move(in_data), in->GetName(),
  280. in->GetContainingDirectory());
  281. }
  282. } // namespace FileSys