Debian Codegraph @

Source/Core/DiscIO/Volume.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DiscIO/Volume.h"
5
6 #include <cstring>
7 #include <functional>
8 #include <map>
9 #include <memory>
10 #include <optional>
11 #include <span>
12 #include <string>
13 #include <type_traits>
14 #include <utility>
15 #include <vector>
16
17 #include "Common/CommonTypes.h"
18 #include "Common/Crypto/SHA1.h"
19 #include "Common/StringUtil.h"
20
21 #include "Core/Config/MainSettings.h"
22 #include "Core/IOS/ES/Formats.h"
23
24 #include "DiscIO/Blob.h"
25 #include "DiscIO/CachedBlob.h"
26 #include "DiscIO/DiscUtils.h"
27 #include "DiscIO/Enums.h"
28 #include "DiscIO/VolumeDisc.h"
29 #include "DiscIO/VolumeGC.h"
30 #include "DiscIO/VolumeWad.h"
31 #include "DiscIO/VolumeWii.h"
32
33 namespace DiscIO
34 {
35 const IOS::ES::TicketReader Volume::INVALID_TICKET{};
36 const IOS::ES::TMDReader Volume::INVALID_TMD{};
37 const std::vector<u8> Volume::INVALID_CERT_CHAIN{};
38
39 std::string Volume::DecodeString(std::span<const char> data) const
40 {
41 // strnlen to trim null bytes
42 std::string string(data.data(), strnlen(data.data(), data.size()));
43 return GetRegion() == Region::NTSC_J ? SHIFTJISToUTF8(string) : CP1252ToUTF8(string);
44 }
45
46 std::string Volume::FilterGameID(std::span<const char> data)
47 {
48 std::string string(data.data(), data.size());
49
50 // We don't want game IDs to contain characters that are unprintable or might cause path
51 // traversal. Game IDs normally only contain ASCII uppercase letters and numbers,
52 // but GNHE5d contains a lowercase letter, so let's allow all ASCII letters and numbers.
53 std::ranges::replace_if(string, std::not_fn(Common::IsAlnum), '-');
54
55 return string;
56 }
57
58 template <typename T>
59 static void AddToSyncHash(Common::SHA1::Context* context, const T& data)
60 {
61 static_assert(std::is_trivially_copyable_v<T>);
62 context->Update(reinterpret_cast<const u8*>(&data), sizeof(data));
63 }
64
65 void Volume::ReadAndAddToSyncHash(Common::SHA1::Context* context, u64 offset, u64 length,
66 const Partition& partition) const
67 {
68 std::vector<u8> buffer(length);
69 if (Read(offset, length, buffer.data(), partition))
70 context->Update(buffer);
71 }
72
73 void Volume::AddTMDToSyncHash(Common::SHA1::Context* context, const Partition& partition) const
74 {
75 // We want to hash some important parts of the TMD, but nothing that changes when fakesigning.
76 // (Fakesigned WADs are very popular, and we don't want people with properly signed WADs to
77 // unnecessarily be at a disadvantage due to most netplay partners having fakesigned WADs.)
78
79 const IOS::ES::TMDReader& tmd = GetTMD(partition);
80 if (!tmd.IsValid())
81 return;
82
83 AddToSyncHash(context, tmd.GetIOSId());
84 AddToSyncHash(context, tmd.GetTitleId());
85 AddToSyncHash(context, tmd.GetTitleFlags());
86 AddToSyncHash(context, tmd.GetGroupId());
87 AddToSyncHash(context, tmd.GetRegion());
88 AddToSyncHash(context, tmd.GetTitleVersion());
89 AddToSyncHash(context, tmd.GetBootIndex());
90
91 for (const IOS::ES::Content& content : tmd.GetContents())
92 AddToSyncHash(context, content);
93 }
94
95 std::map<Language, std::string> Volume::ReadWiiNames(std::span<const char16_t> data)
96 {
97 std::map<Language, std::string> names;
98 for (size_t i = 0; i < NUMBER_OF_LANGUAGES; ++i)
99 {
100 const size_t name_start = NAME_CHARS_LENGTH * i;
101 if (name_start + NAME_CHARS_LENGTH <= data.size())
102 {
103 const std::string name = UTF16BEToUTF8(data.data() + name_start, NAME_CHARS_LENGTH);
104 if (!name.empty())
105 names[static_cast<Language>(i)] = name;
106 }
107 }
108 return names;
109 }
110
111 template <typename T = std::identity>
112 static std::unique_ptr<VolumeDisc> TryCreateDisc(std::unique_ptr<BlobReader>& reader,
113 const T& reader_adapter_factory = {})
114 {
115 if (!reader)
116 return nullptr;
117
118 // `reader_adapter_factory` is used *after* successful magic word read.
119 // This prevents `CachedBlobReader` from showing warnings when failing to scrub a .dol file.
120
121 if (reader->ReadSwapped<u32>(0x18) == WII_DISC_MAGIC)
122 return std::make_unique<VolumeWii>(reader_adapter_factory(std::move(reader)));
123
124 if (reader->ReadSwapped<u32>(0x1C) == GAMECUBE_DISC_MAGIC)
125 return std::make_unique<VolumeGC>(reader_adapter_factory(std::move(reader)));
126
127 // No known magic words found
128 return nullptr;
129 }
130
131 std::unique_ptr<VolumeDisc> CreateDisc(std::unique_ptr<BlobReader> reader)
132 {
133 return TryCreateDisc(reader);
134 }
135
136 std::unique_ptr<VolumeDisc> CreateDisc(const std::string& path)
137 {
138 return CreateDisc(CreateBlobReader(path));
139 }
140
141 std::unique_ptr<VolumeDisc> CreateDiscForCore(const std::string& path)
142 {
143 auto reader = CreateBlobReader(path);
144
145 if (Config::Get(Config::MAIN_LOAD_GAME_INTO_MEMORY))
146 return TryCreateDisc(reader, CreateScrubbingCachedBlobReader);
147
148 return TryCreateDisc(reader);
149 }
150
151 static std::unique_ptr<VolumeWAD> TryCreateWAD(std::unique_ptr<BlobReader>& reader)
152 {
153 if (!reader)
154 return nullptr;
155
156 // Check for WAD
157 // 0x206962 for boot2 wads
158 const std::optional<u32> wad_magic = reader->ReadSwapped<u32>(0x02);
159 if (wad_magic == u32(0x00204973) || wad_magic == u32(0x00206962))
160 return std::make_unique<VolumeWAD>(std::move(reader));
161
162 // No known magic words found
163 return nullptr;
164 }
165
166 std::unique_ptr<VolumeWAD> CreateWAD(std::unique_ptr<BlobReader> reader)
167 {
168 return TryCreateWAD(reader);
169 }
170
171 std::unique_ptr<VolumeWAD> CreateWAD(const std::string& path)
172 {
173 return CreateWAD(CreateBlobReader(path));
174 }
175
176 std::unique_ptr<Volume> CreateVolume(std::unique_ptr<BlobReader> reader)
177 {
178 std::unique_ptr<VolumeDisc> disc = TryCreateDisc(reader);
179 if (disc)
180 return disc;
181
182 std::unique_ptr<VolumeWAD> wad = TryCreateWAD(reader);
183 if (wad)
184 return wad;
185
186 return nullptr;
187 }
188
189 std::unique_ptr<Volume> CreateVolume(const std::string& path)
190 {
191 return CreateVolume(CreateBlobReader(path));
192 }
193 } // namespace DiscIO