| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/TGCBlob.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <memory> |
| 8 | #include <type_traits> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "Common/BitUtils.h" |
| 13 | #include "Common/Swap.h" |
| 14 | |
| 15 | namespace |
| 16 | { |
| 17 | u32 SubtractBE32(u32 minuend_be, u32 subtrahend_le) |
| 18 | { |
| 19 | return Common::swap32(Common::swap32(minuend_be) - subtrahend_le); |
| 20 | } |
| 21 | |
| 22 | void Replace(u64 offset, u64 size, u8* out_ptr, u64 replace_offset, u64 replace_size, |
| 23 | const u8* replace_ptr) |
| 24 | { |
| 25 | const u64 replace_start = std::max(offset, replace_offset); |
| 26 | const u64 replace_end = std::min(offset + size, replace_offset + replace_size); |
| 27 | |
| 28 | if (replace_end > replace_start) |
| 29 | { |
| 30 | std::copy_n(replace_ptr + (replace_start - replace_offset), replace_end - replace_start, |
| 31 | out_ptr + (replace_start - offset)); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | template <typename T> |
| 36 | void Replace(u64 offset, u64 size, u8* out_ptr, u64 replace_offset, const T& replace_value) |
| 37 | { |
| 38 | static_assert(std::is_trivially_copyable_v<T>); |
| 39 | |
| 40 | const u8* replace_ptr = reinterpret_cast<const u8*>(&replace_value); |
| 41 | Replace(offset, size, out_ptr, replace_offset, sizeof(T), replace_ptr); |
| 42 | } |
| 43 | } // namespace |
| 44 | |
| 45 | namespace DiscIO |
| 46 | { |
| 47 | std::unique_ptr<TGCFileReader> TGCFileReader::Create(File::DirectIOFile file) |
| 48 | { |
| 49 | TGCHeader header; |
| 50 | if (file.OffsetRead(0, Common::AsWritableU8Span(header)) && header.magic == TGC_MAGIC) |
| 51 | { |
| 52 | return std::unique_ptr<TGCFileReader>(new TGCFileReader(std::move(file))); |
| 53 | } |
| 54 | |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | TGCFileReader::TGCFileReader(File::DirectIOFile file) : m_file(std::move(file)) |
| 59 | { |
| 60 | m_file.OffsetRead(0, Common::AsWritableU8Span(m_header)); |
| 61 | |
| 62 | m_size = m_file.GetSize(); |
| 63 | |
| 64 | const u32 fst_offset = Common::swap32(m_header.fst_real_offset); |
| 65 | const u32 fst_size = Common::swap32(m_header.fst_size); |
| 66 | m_fst.resize(fst_size); |
| 67 | if (!m_file.OffsetRead(fst_offset, m_fst)) |
| 68 | { |
| 69 | m_fst.clear(); |
| 70 | } |
| 71 | |
| 72 | constexpr size_t FST_ENTRY_SIZE = 12; |
| 73 | if (m_fst.size() < FST_ENTRY_SIZE) |
| 74 | return; |
| 75 | |
| 76 | // This calculation can overflow, but this is not a problem, because in that case |
| 77 | // the old_offset + file_area_shift calculation later also overflows, cancelling it out |
| 78 | const u32 file_area_shift = Common::swap32(m_header.file_area_real_offset) - |
| 79 | Common::swap32(m_header.file_area_virtual_offset) - |
| 80 | Common::swap32(m_header.tgc_header_size); |
| 81 | |
| 82 | const size_t claimed_fst_entries = Common::swap32(m_fst.data() + 8); |
| 83 | const size_t fst_entries = std::min(claimed_fst_entries, m_fst.size() / FST_ENTRY_SIZE); |
| 84 | for (size_t i = 0; i < fst_entries; ++i) |
| 85 | { |
| 86 | // If this is a file (as opposed to a directory)... |
| 87 | if (m_fst[i * FST_ENTRY_SIZE] == 0) |
| 88 | { |
| 89 | // ...change its offset |
| 90 | const u32 old_offset = Common::swap32(m_fst.data() + i * FST_ENTRY_SIZE + 4); |
| 91 | const u32 new_offset = Common::swap32(old_offset + file_area_shift); |
| 92 | Replace<u32>(0, m_fst.size(), m_fst.data(), i * FST_ENTRY_SIZE + 4, new_offset); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | std::unique_ptr<BlobReader> TGCFileReader::CopyReader() const |
| 98 | { |
| 99 | return Create(m_file); |
| 100 | } |
| 101 | |
| 102 | u64 TGCFileReader::GetDataSize() const |
| 103 | { |
| 104 | return m_size - Common::swap32(m_header.tgc_header_size); |
| 105 | } |
| 106 | |
| 107 | bool TGCFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) |
| 108 | { |
| 109 | const u32 tgc_header_size = Common::swap32(m_header.tgc_header_size); |
| 110 | |
| 111 | if (m_file.OffsetRead(offset + tgc_header_size, out_ptr, nbytes)) |
| 112 | { |
| 113 | const u32 replacement_dol_offset = SubtractBE32(m_header.dol_real_offset, tgc_header_size); |
| 114 | const u32 replacement_fst_offset = SubtractBE32(m_header.fst_real_offset, tgc_header_size); |
| 115 | |
| 116 | Replace<u32>(offset, nbytes, out_ptr, 0x0420, replacement_dol_offset); |
| 117 | Replace<u32>(offset, nbytes, out_ptr, 0x0424, replacement_fst_offset); |
| 118 | Replace(offset, nbytes, out_ptr, Common::swap32(replacement_fst_offset), m_fst.size(), |
| 119 | m_fst.data()); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | } // namespace DiscIO |