Skip to content

Commit

Permalink
ChdFileReader: Migrate libchdr patch into PCSX2
Browse files Browse the repository at this point in the history
Added function didn't need to be in libchdr
  • Loading branch information
TheLastRar authored and F0bes committed Dec 17, 2024
1 parent 260380a commit 2f0b00a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 0 additions & 1 deletion 3rdparty/libchdr/include/libchdr/chd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);



Expand Down
21 changes: 0 additions & 21 deletions 3rdparty/libchdr/src/libchdr_chd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
***************************************************************************/
Expand Down
30 changes: 27 additions & 3 deletions pcsx2/CDVD/ChdFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2f0b00a

Please sign in to comment.