From 2aad23c443d7286a42f99e55c13ad165817963da Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Mon, 16 Dec 2024 18:46:21 +0000 Subject: [PATCH] ChdFileReader: Migrate libchdr patch into PCSX2 Added function didn't need to be in libchdr --- 3rdparty/libchdr/include/libchdr/chd.h | 1 - 3rdparty/libchdr/src/libchdr_chd.c | 21 ------------------ pcsx2/CDVD/ChdFileReader.cpp | 30 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/3rdparty/libchdr/include/libchdr/chd.h b/3rdparty/libchdr/include/libchdr/chd.h index d1e7a81dd16bc..1092d40b4327f 100644 --- a/3rdparty/libchdr/include/libchdr/chd.h +++ b/3rdparty/libchdr/include/libchdr/chd.h @@ -407,7 +407,6 @@ CHD_EXPORT const chd_header *chd_get_header(chd_file *chd); CHD_EXPORT chd_error chd_read_header_core_file(core_file *file, chd_header *header); CHD_EXPORT chd_error chd_read_header_file(FILE *file, chd_header *header); CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header); -CHD_EXPORT bool chd_is_matching_parent(const chd_header* header, const chd_header* parent_header); diff --git a/3rdparty/libchdr/src/libchdr_chd.c b/3rdparty/libchdr/src/libchdr_chd.c index cac9720a172e1..aa1ec8b9dc67c 100644 --- a/3rdparty/libchdr/src/libchdr_chd.c +++ b/3rdparty/libchdr/src/libchdr_chd.c @@ -2281,27 +2281,6 @@ CHD_EXPORT chd_error chd_read_header(const char *filename, chd_header *header) return err; } -CHD_EXPORT bool chd_is_matching_parent(const chd_header* header, const chd_header* parent_header) -{ - /* check MD5 if it isn't empty */ - if (memcmp(nullmd5, header->parentmd5, sizeof(header->parentmd5)) != 0 && - memcmp(nullmd5, parent_header->md5, sizeof(parent_header->md5)) != 0 && - memcmp(parent_header->md5, header->parentmd5, sizeof(header->parentmd5)) != 0) - { - return false; - } - - /* check SHA1 if it isn't empty */ - if (memcmp(nullsha1, header->parentsha1, sizeof(header->parentsha1)) != 0 && - memcmp(nullsha1, parent_header->sha1, sizeof(parent_header->sha1)) != 0 && - memcmp(parent_header->sha1, header->parentsha1, sizeof(header->parentsha1)) != 0) - { - return false; - } - - return true; -} - /*************************************************************************** CORE DATA READ/WRITE ***************************************************************************/ diff --git a/pcsx2/CDVD/ChdFileReader.cpp b/pcsx2/CDVD/ChdFileReader.cpp index fbf2c642aa6b0..b58095c2897e4 100644 --- a/pcsx2/CDVD/ChdFileReader.cpp +++ b/pcsx2/CDVD/ChdFileReader.cpp @@ -95,6 +95,30 @@ ChdFileReader::~ChdFileReader() pxAssert(!ChdFile); } +static bool IsHeaderParentCHD(const chd_header& header, const chd_header& parent_header) +{ + static const u8 nullmd5[CHD_MD5_BYTES]{}; + static const u8 nullsha1[CHD_SHA1_BYTES]{}; + + // Check MD5 if it isn't empty. + if (std::memcmp(nullmd5, header.parentmd5, CHD_MD5_BYTES) != 0 && + std::memcmp(nullmd5, parent_header.md5, CHD_MD5_BYTES) != 0 && + std::memcmp(parent_header.md5, header.parentmd5, CHD_MD5_BYTES) != 0) + { + return false; + } + + // Check SHA1 if it isn't empty. + if (std::memcmp(nullsha1, header.parentsha1, CHD_SHA1_BYTES) != 0 && + std::memcmp(nullsha1, parent_header.sha1, CHD_SHA1_BYTES) != 0 && + std::memcmp(parent_header.sha1, header.parentsha1, CHD_SHA1_BYTES) != 0) + { + return false; + } + + return true; +} + static chd_file* OpenCHD(const std::string& filename, FileSystem::ManagedCFilePtr fp, Error* error, u32 recursion_level) { chd_file* chd; @@ -144,14 +168,14 @@ static chd_file* OpenCHD(const std::string& filename, FileSystem::ManagedCFilePt if (!StringUtil::compareNoCase(parent_dir, Path::GetDirectory(it->first))) continue; - if (!chd_is_matching_parent(&header, &it->second)) + if (!IsHeaderParentCHD(header, it->second)) continue; // Re-check the header, it might have changed since we last opened. chd_header parent_header; auto parent_fp = FileSystem::OpenManagedSharedCFile(it->first.c_str(), "rb", FileSystem::FileShareMode::DenyWrite); if (parent_fp && chd_read_header_file(parent_fp.get(), &parent_header) == CHDERR_NONE && - chd_is_matching_parent(&header, &parent_header)) + IsHeaderParentCHD(header, parent_header)) { // Need to take a copy of the string, because the parent might add to the list and invalidate the iterator. const std::string filename_to_open = it->first; @@ -192,7 +216,7 @@ static chd_file* OpenCHD(const std::string& filename, FileSystem::ManagedCFilePt else s_chd_hash_cache.emplace_back(fd.FileName, parent_header); - if (!chd_is_matching_parent(&header, &parent_header)) + if (!IsHeaderParentCHD(header, parent_header)) continue; // Match! Open this one.