TGCBlob.cpp 4.0 KB

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