2016-12-17 13:48:49 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "DiscIO/TGCBlob.h"
|
|
|
|
|
2016-12-17 13:48:49 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-12-21 11:50:15 +00:00
|
|
|
#include <utility>
|
2016-12-17 13:48:49 +00:00
|
|
|
|
2017-01-15 20:46:32 +00:00
|
|
|
#include "Common/File.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2016-12-17 13:48:49 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
struct Interval
|
|
|
|
{
|
|
|
|
T start;
|
|
|
|
T length;
|
|
|
|
|
|
|
|
T End() const { return start + length; }
|
|
|
|
bool IsEmpty() const { return length == 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void SplitInterval(T split_point, Interval<T> interval, Interval<T>* out_1, Interval<T>* out_2)
|
|
|
|
{
|
|
|
|
if (interval.start < split_point)
|
|
|
|
*out_1 = {interval.start, std::min(interval.length, split_point - interval.start)};
|
|
|
|
else
|
|
|
|
*out_1 = {0, 0};
|
|
|
|
|
|
|
|
if (interval.End() > split_point)
|
|
|
|
*out_2 = {std::max(interval.start, split_point),
|
|
|
|
std::min(interval.length, interval.End() - split_point)};
|
|
|
|
else
|
|
|
|
*out_2 = {0, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 SubtractBE32(u32 minuend_be, u32 subtrahend_le)
|
|
|
|
{
|
|
|
|
return Common::swap32(Common::swap32(minuend_be) - subtrahend_le);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Replace8(u64 offset, u64 nbytes, u8* out_ptr, u64 replace_offset, u8 replace_value)
|
|
|
|
{
|
|
|
|
if (offset <= replace_offset && offset + nbytes > replace_offset)
|
|
|
|
out_ptr[replace_offset - offset] = replace_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Replace32(u64 offset, u64 nbytes, u8* out_ptr, u64 replace_offset, u32 replace_value)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < sizeof(u32); ++i)
|
|
|
|
Replace8(offset, nbytes, out_ptr, replace_offset + i, reinterpret_cast<u8*>(&replace_value)[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace DiscIO
|
|
|
|
{
|
2016-12-21 11:50:15 +00:00
|
|
|
std::unique_ptr<TGCFileReader> TGCFileReader::Create(File::IOFile file)
|
2016-12-17 13:48:49 +00:00
|
|
|
{
|
|
|
|
TGCHeader header;
|
2016-12-21 11:50:15 +00:00
|
|
|
if (file.Seek(0, SEEK_SET) && file.ReadArray(&header, 1) && header.magic == TGC_MAGIC)
|
2016-12-17 13:48:49 +00:00
|
|
|
return std::unique_ptr<TGCFileReader>(new TGCFileReader(std::move(file)));
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-12-21 11:50:15 +00:00
|
|
|
TGCFileReader::TGCFileReader(File::IOFile file) : m_file(std::move(file))
|
2016-12-17 13:48:49 +00:00
|
|
|
{
|
2016-12-21 11:50:15 +00:00
|
|
|
m_file.Seek(0, SEEK_SET);
|
2016-12-17 13:48:49 +00:00
|
|
|
m_file.ReadArray(&m_header, 1);
|
2017-10-08 09:43:48 +00:00
|
|
|
u32 header_size = Common::swap32(m_header.tgc_header_size);
|
2016-12-17 13:48:49 +00:00
|
|
|
m_size = m_file.GetSize();
|
2017-10-08 10:30:53 +00:00
|
|
|
m_file_area_shift = static_cast<s64>(Common::swap32(m_header.file_area_virtual_offset)) -
|
2017-10-08 09:43:48 +00:00
|
|
|
Common::swap32(m_header.file_area_real_offset) + header_size;
|
2016-12-17 13:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 TGCFileReader::GetDataSize() const
|
|
|
|
{
|
2017-10-08 09:43:48 +00:00
|
|
|
return m_size + Common::swap32(m_header.file_area_virtual_offset) -
|
|
|
|
Common::swap32(m_header.file_area_real_offset);
|
2016-12-17 13:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TGCFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2017-10-08 10:30:53 +00:00
|
|
|
Interval<u64> first_part = {0, 0};
|
|
|
|
Interval<u64> empty_part = {0, 0};
|
|
|
|
Interval<u64> file_part = {0, 0};
|
2016-12-17 13:48:49 +00:00
|
|
|
|
2017-10-08 09:43:48 +00:00
|
|
|
const u32 tgc_header_size = Common::swap32(m_header.tgc_header_size);
|
2017-10-08 10:16:10 +00:00
|
|
|
const u64 split_point = Common::swap32(m_header.file_area_real_offset) - tgc_header_size;
|
|
|
|
SplitInterval(split_point, Interval<u64>{offset, nbytes}, &first_part, &file_part);
|
2017-10-08 10:30:53 +00:00
|
|
|
if (m_file_area_shift > tgc_header_size)
|
|
|
|
{
|
|
|
|
SplitInterval(static_cast<u64>(m_file_area_shift - tgc_header_size), file_part, &empty_part,
|
|
|
|
&file_part);
|
|
|
|
}
|
2016-12-17 13:48:49 +00:00
|
|
|
|
2017-10-08 09:43:48 +00:00
|
|
|
// Offsets in the initial areas of the disc are unshifted
|
|
|
|
// (except for InternalRead's constant shift by tgc_header_size).
|
2016-12-17 13:48:49 +00:00
|
|
|
if (!first_part.IsEmpty())
|
|
|
|
{
|
|
|
|
if (!InternalRead(first_part.start, first_part.length, out_ptr + (first_part.start - offset)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-08 09:43:48 +00:00
|
|
|
// The data between the file area and the area that precedes it is treated as all zeroes.
|
|
|
|
// The game normally won't attempt to access this part of the virtual disc, but let's not return
|
|
|
|
// an error if it gets accessed, in case someone wants to copy or hash the whole virtual disc.
|
2016-12-17 13:48:49 +00:00
|
|
|
if (!empty_part.IsEmpty())
|
|
|
|
std::fill_n(out_ptr + (empty_part.start - offset), empty_part.length, 0);
|
|
|
|
|
2017-10-08 09:43:48 +00:00
|
|
|
// Offsets in the file area are shifted by m_file_area_shift.
|
|
|
|
if (!file_part.IsEmpty())
|
2016-12-17 13:48:49 +00:00
|
|
|
{
|
2017-10-08 09:43:48 +00:00
|
|
|
if (!InternalRead(file_part.start - m_file_area_shift, file_part.length,
|
|
|
|
out_ptr + (file_part.start - offset)))
|
2016-12-17 13:48:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TGCFileReader::InternalRead(u64 offset, u64 nbytes, u8* out_ptr)
|
|
|
|
{
|
2017-10-08 09:43:48 +00:00
|
|
|
const u32 tgc_header_size = Common::swap32(m_header.tgc_header_size);
|
2016-12-17 13:48:49 +00:00
|
|
|
|
2017-10-08 09:43:48 +00:00
|
|
|
if (m_file.Seek(offset + tgc_header_size, SEEK_SET) && m_file.ReadBytes(out_ptr, nbytes))
|
2016-12-17 13:48:49 +00:00
|
|
|
{
|
2017-10-08 09:43:48 +00:00
|
|
|
Replace32(offset, nbytes, out_ptr, 0x420,
|
|
|
|
SubtractBE32(m_header.dol_real_offset, tgc_header_size));
|
|
|
|
Replace32(offset, nbytes, out_ptr, 0x424,
|
|
|
|
SubtractBE32(m_header.fst_real_offset, tgc_header_size));
|
2016-12-17 13:48:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_file.Clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DiscIO
|