diff options
| author | JosJuice <josjuice@gmail.com> | 2016-12-17 14:48:49 +0100 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2016-12-18 18:57:36 +0100 |
| commit | 9bd514ed1cd02cc0b8c8f22affecaae1892da15f (patch) | |
| tree | dd9195dd1cef94686cc2f4d38b876135de0eeef1 /Source/Core/DiscIO/TGCBlob.cpp | |
| parent | 49384208cfce23d6b9ff12e4a16b1fb98da7735e (diff) | |
Add TGC disc image compatibility
Diffstat (limited to 'Source/Core/DiscIO/TGCBlob.cpp')
| -rw-r--r-- | Source/Core/DiscIO/TGCBlob.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/Source/Core/DiscIO/TGCBlob.cpp b/Source/Core/DiscIO/TGCBlob.cpp new file mode 100644 index 0000000000..85a5a854aa --- /dev/null +++ b/Source/Core/DiscIO/TGCBlob.cpp @@ -0,0 +1,141 @@ +// Copyright 2016 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include <memory> +#include <string> + +#include "Common/CommonFuncs.h" +#include "Common/FileUtil.h" +#include "DiscIO/TGCBlob.h" + +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 +{ +bool IsTGCBlob(const std::string& path) +{ + File::IOFile file(path, "rb"); + + TGCHeader header; + return (file.ReadArray(&header, 1) && header.magic == Common::swap32(0xAE0F38A2)); +} + +std::unique_ptr<TGCFileReader> TGCFileReader::Create(const std::string& path) +{ + if (IsTGCBlob(path)) + { + File::IOFile file(path, "rb"); + return std::unique_ptr<TGCFileReader>(new TGCFileReader(std::move(file))); + } + + return nullptr; +} + +TGCFileReader::TGCFileReader(File::IOFile&& file) : m_file(std::move(file)) +{ + m_file.ReadArray(&m_header, 1); + u32 header_size = Common::swap32(m_header.header_size); + m_size = m_file.GetSize(); + m_file_offset = Common::swap32(m_header.unknown_important_2) - + Common::swap32(m_header.unknown_important_1) + header_size; +} + +u64 TGCFileReader::GetDataSize() const +{ + return m_size + Common::swap32(m_header.unknown_important_2) - + Common::swap32(m_header.unknown_important_1); +} + +bool TGCFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) +{ + Interval<u64> first_part; + Interval<u64> empty_part; + Interval<u64> last_part; + + u32 header_size = Common::swap32(m_header.header_size); + SplitInterval(m_file_offset, Interval<u64>{offset, nbytes}, &first_part, &last_part); + SplitInterval(m_size - header_size, first_part, &first_part, &empty_part); + + // Offsets before m_file_offset are read as usual + if (!first_part.IsEmpty()) + { + if (!InternalRead(first_part.start, first_part.length, out_ptr + (first_part.start - offset))) + return false; + } + + // If any offset before m_file_offset isn't actually in the file, + // treat it as 0x00 bytes (so e.g. MD5 calculation of the whole disc won't fail) + if (!empty_part.IsEmpty()) + std::fill_n(out_ptr + (empty_part.start - offset), empty_part.length, 0); + + // Offsets after m_file_offset are read as if they are (offset - m_file_offset) + if (!last_part.IsEmpty()) + { + if (!InternalRead(last_part.start - m_file_offset, last_part.length, + out_ptr + (last_part.start - offset))) + return false; + } + + return true; +} + +bool TGCFileReader::InternalRead(u64 offset, u64 nbytes, u8* out_ptr) +{ + u32 header_size = Common::swap32(m_header.header_size); + + if (m_file.Seek(offset + header_size, SEEK_SET) && m_file.ReadBytes(out_ptr, nbytes)) + { + Replace32(offset, nbytes, out_ptr, 0x420, SubtractBE32(m_header.dol_offset, header_size)); + Replace32(offset, nbytes, out_ptr, 0x424, SubtractBE32(m_header.fst_offset, header_size)); + return true; + } + + m_file.Clear(); + return false; +} + +} // namespace DiscIO |
