summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/ips_layer.cpp
blob: 6bce138a82e431051dc8f1e33a8a89c4a076cde3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <sstream>
#include "common/assert.h"
#include "common/hex_util.h"
#include "common/swap.h"
#include "core/file_sys/ips_layer.h"
#include "core/file_sys/vfs_vector.h"

namespace FileSys {

enum class IPSFileType {
    IPS,
    IPS32,
    Error,
};

static IPSFileType IdentifyMagic(const std::vector<u8>& magic) {
    if (magic.size() != 5)
        return IPSFileType::Error;
    if (magic == std::vector<u8>{'P', 'A', 'T', 'C', 'H'})
        return IPSFileType::IPS;
    if (magic == std::vector<u8>{'I', 'P', 'S', '3', '2'})
        return IPSFileType::IPS32;
    return IPSFileType::Error;
}

VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) {
    if (in == nullptr || ips == nullptr)
        return nullptr;

    const auto type = IdentifyMagic(ips->ReadBytes(0x5));
    if (type == IPSFileType::Error)
        return nullptr;

    auto in_data = in->ReadAllBytes();

    std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4);
    u64 offset = 5; // After header
    while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) {
        offset += temp.size();
        if (type == IPSFileType::IPS32 && temp == std::vector<u8>{'E', 'E', 'O', 'F'} ||
            type == IPSFileType::IPS && temp == std::vector<u8>{'E', 'O', 'F'}) {
            break;
        }

        u32 real_offset{};
        if (type == IPSFileType::IPS32)
            real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3];
        else
            real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2];

        u16 data_size{};
        if (ips->ReadObject(&data_size, offset) != sizeof(u16))
            return nullptr;
        data_size = Common::swap16(data_size);
        offset += sizeof(u16);

        if (data_size == 0) { // RLE
            u16 rle_size{};
            if (ips->ReadObject(&rle_size, offset) != sizeof(u16))
                return nullptr;
            rle_size = Common::swap16(data_size);
            offset += sizeof(u16);

            const auto data = ips->ReadByte(offset++);
            if (data == boost::none)
                return nullptr;

            if (real_offset + rle_size > in_data.size())
                rle_size = in_data.size() - real_offset;
            std::memset(in_data.data() + real_offset, data.get(), rle_size);
        } else { // Standard Patch
            auto read = data_size;
            if (real_offset + read > in_data.size())
                read = in_data.size() - real_offset;
            if (ips->Read(in_data.data() + real_offset, read, offset) != data_size)
                return nullptr;
            offset += data_size;
        }
    }

    if (temp != std::vector<u8>{'E', 'E', 'O', 'F'} && temp != std::vector<u8>{'E', 'O', 'F'})
        return nullptr;
    return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory());
}

IPSwitchCompiler::IPSwitchCompiler(VirtualFile patch_text_)
    : valid(false), patch_text(std::move(patch_text_)), nso_build_id{}, is_little_endian(false),
      offset_shift(0), print_values(false) {
    Parse();
}

std::array<u8, 32> IPSwitchCompiler::GetBuildID() const {
    return nso_build_id;
}

bool IPSwitchCompiler::IsValid() const {
    return valid;
}

static bool StartsWith(const std::string& base, const std::string& check) {
    return base.size() >= check.size() && base.substr(0, check.size()) == check;
}

void IPSwitchCompiler::Parse() {
    const auto bytes = patch_text->ReadAllBytes();
    std::stringstream s;
    s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());

    std::vector<std::string> lines;
    std::string line;
    while (std::getline(s, line)) {
        // Remove a trailing \r
        if (!line.empty() && line[line.size() - 1] == '\r')
            line = line.substr(0, line.size() - 1);
        lines.push_back(line);
    }

    for (std::size_t i = 0; i < lines.size(); ++i) {
        auto line = lines[i];
        if (StartsWith(line, "@stop")) {
            // Force stop
            break;
        } else if (StartsWith(line, "@nsobid-")) {
            // NSO Build ID Specifier
            auto raw_build_id = line.substr(8);
            if (raw_build_id.size() != 0x40)
                raw_build_id.resize(0x40, '0');
            nso_build_id = Common::HexStringToArray<0x20>(raw_build_id);
        } else if (StartsWith(line, "@flag offset_shift ")) {
            // Offset Shift Flag
            offset_shift = std::stoull(line.substr(19), nullptr, 0);
        } else if (StartsWith(line, "#")) {
            // Mandatory Comment
            LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Forced output comment: {}",
                     patch_text->GetName(), line.substr(1));
        } else if (StartsWith(line, "@little-endian")) {
            // Set values to read as little endian
            is_little_endian = true;
        } else if (StartsWith(line, "@big-endian")) {
            // Set values to read as big endian
            is_little_endian = false;
        } else if (StartsWith(line, "@flag print_values")) {
            // Force printing of applied values
            print_values = true;
        } else if (StartsWith(line, "@enabled") || StartsWith(line, "@disabled")) {
            // Start of patch
            const auto enabled = StartsWith(line, "@enabled");
            if (i == 0)
                return;
            const auto name = lines[i - 1].substr(3);
            LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] Parsing patch '{}' ({})",
                     patch_text->GetName(), name, line.substr(1));

            IPSwitchPatch patch{name, enabled, {}};

            // Read rest of patch
            while (true) {
                if (i + 1 >= lines.size())
                    break;
                line = lines[++i];

                // 11 - 8 hex digit offset + space + minimum two digit overwrite val
                if (line.length() < 11)
                    break;
                auto offset = std::stoul(line.substr(0, 8), nullptr, 16);
                offset += offset_shift;

                std::vector<u8> replace;
                // 9 - first char of replacement val
                if (line[9] == '\"') {
                    // string replacement
                    const auto end_index = line.find_last_of('\"');
                    if (end_index == std::string::npos || end_index < 10)
                        return;
                    const auto value = line.substr(10, end_index - 10);
                    replace.reserve(value.size());
                    std::copy(value.begin(), value.end(), std::back_inserter(replace));
                } else {
                    // hex replacement
                    const auto value = line.substr(9);
                    replace.reserve(value.size() / 2);
                    replace = Common::HexStringToVector(value, is_little_endian);
                }

                if (print_values) {
                    LOG_INFO(Loader,
                             "[IPSwitchCompiler ('{}')]     - Patching value at offset 0x{:08X} "
                             "with byte string '{}'",
                             patch_text->GetName(), offset, Common::HexVectorToString(replace));
                }

                patch.records.emplace(offset, replace);
            }

            patches.push_back(std::move(patch));
        }
    }

    valid = true;
}

VirtualFile IPSwitchCompiler::Apply(const VirtualFile& in) const {
    if (in == nullptr || !valid)
        return nullptr;

    auto in_data = in->ReadAllBytes();

    for (const auto& patch : patches) {
        if (!patch.enabled)
            continue;

        for (const auto& record : patch.records) {
            if (record.first >= in_data.size())
                continue;
            auto replace_size = record.second.size();
            if (record.first + replace_size > in_data.size())
                replace_size = in_data.size() - record.first;
            for (std::size_t i = 0; i < replace_size; ++i)
                in_data[i + record.first] = record.second[i];
        }
    }

    return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory());
}

} // namespace FileSys