diff --git a/Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h b/Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h index 9023890..9f4c02e 100644 --- a/Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h +++ b/Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h @@ -2,6 +2,7 @@ #define ENGINE_SOURCE_BLIZZARDCORE_INCLUDE_BLIZZARD_UTIL_DIGEST_H #include +#include namespace Blizzard { namespace Util { @@ -11,9 +12,42 @@ struct Digest { /* 0000 */ uint8_t m_data[N]; bool operator<(const Digest& other) const; + bool operator==(const Digest& other) const; + bool operator!=(const Digest& other) const; }; } } +// VERIFY: 182eff5b manual +// OFFSET: 0x00429540 +template +bool Blizzard::Util::Digest::operator<(const Digest& other) const { + for (int32_t i = 0; i < N; i++) { + if (this->m_data[i] < other.m_data[i]) { + return true; + } + + if (this->m_data[i] > other.m_data[i]) { + return false; + } + } + + return false; +} + +// VERIFY: 06cb648f manual +// OFFSET: 0x00668750 +template +bool Blizzard::Util::Digest::operator==(const Digest& other) const { + return memcmp(this->m_data, other.m_data, N) == 0; +} + +// VERIFY: 951b079f manual +// OFFSET: 0x004221A0 +template +bool Blizzard::Util::Digest::operator!=(const Digest& other) const { + return memcmp(this->m_data, other.m_data, N) != 0; +} + #endif diff --git a/Engine/Source/BlizzardCore/Include/Blizzard/Util/HashMap.h b/Engine/Source/BlizzardCore/Include/Blizzard/Util/HashMap.h index 2d2dcc3..dae2166 100644 --- a/Engine/Source/BlizzardCore/Include/Blizzard/Util/HashMap.h +++ b/Engine/Source/BlizzardCore/Include/Blizzard/Util/HashMap.h @@ -1,4 +1,81 @@ #ifndef ENGINE_SOURCE_BLIZZARDCORE_INCLUDE_BLIZZARD_UTIL_HASHMAP_H #define ENGINE_SOURCE_BLIZZARDCORE_INCLUDE_BLIZZARD_UTIL_HASHMAP_H +#include +#include "Engine/Source/Storm/H/storm.h" + +namespace Blizzard { +namespace Util { + +template +class HashMap { + public: + struct Entry { + /* 0000 */ K m_key; + /* 0008 */ V m_value; + }; + + /* 0000 */ Entry* m_entries; + /* 0004 */ uint32_t m_count; + /* 0008 */ uint32_t m_capacity; + + ~HashMap(); + bool Find(const K& key, uint32_t* index); + bool Get(const K& key, V* value); +}; + +} +} + +// INLINED +template +Blizzard::Util::HashMap::~HashMap() { + for (uint32_t i = 0; i < this->m_capacity; i++) { + if (this->m_entries[i].m_key.m_lo) { + this->m_entries[i].m_key.m_lo = 0; + this->m_entries[i].m_key.m_hi = 0; + } + } + + this->m_count = 0; + + if (this->m_entries) { + SMemFree(this->m_entries, __FILE__, __LINE__, 0); + this->m_entries = nullptr; + this->m_capacity = 0; + } +} + +// VERIFY: 23f50510 manual +// OFFSET: 0x00433BA0 +template +bool Blizzard::Util::HashMap::Find(const K& key, uint32_t* index) { + if (!this->m_capacity) { + return false; + } + + for (*index = key.m_lo & (this->m_capacity - 1); this->m_entries[*index].m_key.m_lo; *index = (*index + 1) & (this->m_capacity - 1)) { + if (this->m_entries[*index].m_key.m_lo == key.m_lo && this->m_entries[*index].m_key.m_hi == key.m_hi) { + return true; + } + } + + return false; +} + +// VERIFY: d994337d manual +// OFFSET: 0x00435EA0 +template +bool Blizzard::Util::HashMap::Get(const K& key, V* value) { + uint32_t index; + + if (!this->Find(key, &index)) { + return false; + } + + *value = this->m_entries[index].m_value; + + return true; +} + #endif diff --git a/Engine/Source/BlizzardCore/Include/Blizzard/Util/MD5.h b/Engine/Source/BlizzardCore/Include/Blizzard/Util/MD5.h new file mode 100644 index 0000000..c9912fb --- /dev/null +++ b/Engine/Source/BlizzardCore/Include/Blizzard/Util/MD5.h @@ -0,0 +1,24 @@ +#ifndef ENGINE_SOURCE_BLIZZARDCORE_INCLUDE_BLIZZARD_UTIL_MD5_H +#define ENGINE_SOURCE_BLIZZARDCORE_INCLUDE_BLIZZARD_UTIL_MD5_H + +#include + +namespace Blizzard { +namespace Util { + +class MD5 { + public: + /* 0000 */ uint32_t m_count[2]; + /* 0008 */ uint32_t m_state[4]; + /* 0018 */ uint8_t m_buffer[64]; + + MD5(); + void Finish(uint8_t* digest); + void Process(const uint8_t* block); + void Process(const void* data, uint32_t size); +}; + +} +} + +#endif diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.cpp b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.cpp new file mode 100644 index 0000000..56c9bd9 --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.cpp @@ -0,0 +1,881 @@ +#include +#include +#include +#include +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnitContainer.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQEncrypt.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SComp.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.h" +#include "Engine/Source/BlizzardCore/Source/System/File/File.h" +#include "Engine/Source/BlizzardCore/Source/System/Hardware/Hardware.h" +#include "Engine/Source/BlizzardCore/Source/System/Lock/Lock.h" +#include "Engine/Source/BlizzardCore/Source/System/String/String.h" +#include "ThirdParty/zlib/contrib/blast/blast.h" +#include "ThirdParty/zlib/zlib.h" + +// VERIFY: 4faeb939 manual +// OFFSET: 0x00431DB0 +IOMpqUnit::IOMpqUnit(System_SFile::ARCHIVEREC* archive, const char* name, bool ownsMemory, IOUnit* next) : IOUnit(next, ownsMemory), unk_0010(0), m_archive(archive), m_name((name ? static_cast(strlen(name)) : 0) + 1) { + Blizzard::String::Copy(this->m_name.m_data, name, this->m_name.m_count); + + if (this->m_archive) { + this->m_archive->m_internalRefs.Increment(); + } +} + +// VERIFY: a7d3cb7f auto +// OFFSET: 0x00429630 +uint32_t IOMpqUnit::GetType() { + return 'mpq '; +} + +// INLINED +IOMpqUnit::~IOMpqUnit() { +} + +// VERIFY: 915c8823 auto +// OFFSET: 0x00454060 +bool IOMpqUnit::Close() { + if (this->m_archive) { + this->m_archive->RemoveInternalArchiveRef(); + } + + return true; +} + +System_SFile::ARCHIVEREC* IOMpqUnit::GetArchive() { + return this->m_archive; +} + +const char* IOMpqUnit::GetName() { + return this->m_name.m_data; +} + +// VERIFY: 48195ff4 manual +// OFFSET: 0x00437640 +IOMpqArchiveUnit::IOMpqArchiveUnit(System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, bool ownsMemory, IOUnit* next) : IOMpqUnit(archive, name, ownsMemory, next) { + this->m_blockIndex = blockIndex; + this->m_blockEntry = archive->GetBlockEntry(blockIndex); + + if (this->m_blockEntry.m_md5Check) { + System_SFile::s_signatures.GetSignature(name, &this->m_blockEntry.m_md5); + } +} + +// VERIFY: 8184d4ce auto +// OFFSET: 0x00431EB0 +bool IOMpqArchiveUnit::GetFileSize(int64_t* size) { + *size = this->m_blockEntry.m_fileSize; + return true; +} + +bool IOMpqArchiveUnit::SetFileSize(int64_t size) { + return false; +} + +// INLINED +IOMpqArchiveUnit::~IOMpqArchiveUnit() { +} + +// VERIFY: 31dccac3 manual +// OFFSET: 0x00431EF0 +const char* IOMpqArchiveUnit::GetPath() { + return this->GetName(); +} + +// VERIFY: 0c8748b4 auto +// OFFSET: 0x00431ED0 +bool IOMpqArchiveUnit::GetCompressedSize(int64_t* size) { + *size = this->m_blockEntry.m_compressedSize; + return true; +} + +// VERIFY: 3f0e5717 auto +// OFFSET: 0x00431F00 +IOUnit* IOMpqArchiveUnit::GetBaseUnit() { + return this->m_archive->m_file; +} + +// VERIFY: 1be0c5bf auto +// OFFSET: 0x00431F10 +int64_t IOMpqArchiveUnit::GetOffset() { + return this->m_blockEntry.m_offset; +} + +// VERIFY: e6452137 auto +// OFFSET: 0x00431F20 +uint64_t IOMpqArchiveUnit::GetFileTime() { + return this->m_blockEntry.m_fileTime; +} + +// VERIFY: fa7d866e auto +// OFFSET: 0x00431F30 +uint32_t IOMpqArchiveUnit::GetCrc() { + return this->m_blockEntry.m_crc; +} + +// VERIFY: c2fc05db auto +// OFFSET: 0x00431F40 +const Blizzard::Util::Digest<16>* IOMpqArchiveUnit::GetMd5() { + return &this->m_blockEntry.m_md5; +} + +// VERIFY: 9e9495a5 auto +// OFFSET: 0x00431F50 +bool IOMpqArchiveUnit::HasMd5Check() { + return this->m_blockEntry.m_md5Check; +} + +// VERIFY: 636246fc auto +// OFFSET: 0x00431F80 +void IOMpqArchiveUnit::DisableMd5Check() { + this->m_archive->SetMD5Check(this->m_blockIndex, false); + this->m_blockEntry.m_md5Check = false; +} + +// VERIFY: 5faa24df auto +// OFFSET: 0x00431F60 +uint32_t IOMpqArchiveUnit::GetFlags() { + return this->m_blockEntry.m_flags; +} + +// VERIFY: 22f28fcc auto +// OFFSET: 0x00431FA0 +bool IOMpqArchiveUnit::MarkDeleted() { + this->m_blockEntry.m_flags |= 0x2000000; + this->m_archive->SetBlockEntry(this->m_blockIndex, this->m_blockEntry); + return true; +} + +// VERIFY: f5430f38 auto +// OFFSET: 0x00431F70 +bool IOMpqArchiveUnit::IsEncrypted() { + return (this->m_blockEntry.m_flags & 0xF0000) != 0; +} + +// VERIFY: 41863b5d manual +// OFFSET: 0x004396E0 +IOMpqArchiveReadUnit* IOMpqArchiveReadUnit::Create(IOUnitContainer* container, System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, IOUnit* next) { + if (!next) { + return nullptr; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOMpqArchiveReadUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOMpqArchiveReadUnit(archive, name, blockIndex, ownsMemory, next); +} + +// INLINED +IOMpqArchiveReadUnit::IOMpqArchiveReadUnit(System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, bool ownsMemory, IOUnit* next) : IOMpqArchiveUnit(archive, name, blockIndex, ownsMemory, next) { +} + +bool IOMpqArchiveReadUnit::Write(const void* buffer, int64_t offset, uint32_t count) { + return false; +} + +// VERIFY: 6194de2c manual +// OFFSET: 0x00435380 +IOMpqDiskUnit* IOMpqDiskUnit::Create(IOUnitContainer* container, System_SFile::ARCHIVEREC* archive, const char* name, const char* path, IOUnit* next) { + if (!next) { + return nullptr; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOMpqDiskUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOMpqDiskUnit(archive, name, path, ownsMemory, next); +} + +// VERIFY: 27a64d53 manual +// OFFSET: 0x00432050 +IOMpqDiskUnit::IOMpqDiskUnit(System_SFile::ARCHIVEREC* archive, const char* name, const char* path, bool ownsMemory, IOUnit* next) : IOMpqUnit(archive, name, ownsMemory, next), m_path((path ? static_cast(strlen(path)) : 0) + 1) { + Blizzard::String::Copy(this->m_path.m_data, path, this->m_path.m_count); +} + +// INLINED +IOMpqDiskUnit::~IOMpqDiskUnit() { +} + +// VERIFY: deabac3d auto +// OFFSET: 0x00432130 +const char* IOMpqDiskUnit::GetPath() { + return this->m_path.m_data; +} + +bool IOMpqDiskUnit::GetCompressedSize(int64_t* size) { + return this->m_next->GetFileSize(size); +} + +IOUnit* IOMpqDiskUnit::GetBaseUnit() { + return this->m_next; +} + +int64_t IOMpqDiskUnit::GetOffset() { + return 0; +} + +uint64_t IOMpqDiskUnit::GetFileTime() { + return 0; +} + +uint32_t IOMpqDiskUnit::GetCrc() { + return 0; +} + +// VERIFY: 70a1b3db auto +// OFFSET: 0x00432120 +const Blizzard::Util::Digest<16>* IOMpqDiskUnit::GetMd5() { + return &System_SFile::s_emptyMd5; +} + +bool IOMpqDiskUnit::HasMd5Check() { + return false; +} + +void IOMpqDiskUnit::DisableMd5Check() { +} + +uint32_t IOMpqDiskUnit::GetFlags() { + return 0; +} + +bool IOMpqDiskUnit::MarkDeleted() { + return false; +} + +bool IOMpqDiskUnit::IsEncrypted() { + return false; +} + +// VERIFY: 36098579 manual +// OFFSET: 0x004351E0 +IOUnit* IOMpqReadUnit::Create(IOUnitContainer* container, uint32_t fileSize, uint32_t compressedSize, uint32_t sectorSize, uint32_t flags, uint32_t key, IOUnit* next) { + if (!next) { + return nullptr; + } + + if (!fileSize) { + return next; + } + + uint32_t sectorCount = (fileSize + sectorSize - 1) / sectorSize; + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOMpqReadUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOMpqReadUnit(fileSize, compressedSize, sectorCount, sectorSize, flags, key, ownsMemory, next); +} + +// VERIFY: cd8765a0 manual +// OFFSET: 0x00435290 +IOUnit* IOMpqReadUnit::CreateSingle(IOUnitContainer* container, uint32_t fileSize, uint32_t compressedSize, uint32_t flags, uint32_t key, IOUnit* next) { + if (!next) { + return nullptr; + } + + if (!fileSize) { + return next; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOMpqReadUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOMpqReadUnit(fileSize, compressedSize, flags, key, ownsMemory, next); +} + +// VERIFY: a2649e4d manual +// OFFSET: 0x00459B90 +bool IOMpqReadUnit::DecompressBlocks(IOMpqReadUnit* unit, uint32_t offset, uint32_t size, uint8_t* source, uint32_t first, uint32_t count, uint8_t* dest) { + uint32_t sector = first; + bool hasChecksums = unit->m_checksums.m_count != 0; + uint32_t end = first + count; + + if (first >= end) { + return true; + } + + while (true) { + uint32_t compressedSize = unit->m_sectorOffsets.m_data[sector + 1] - unit->m_sectorOffsets.m_data[sector]; + uint32_t sectorSize = sector >= unit->m_sectorOffsets.m_count - 3 ? unit->m_lastSectorSize : unit->m_sectorSize; + + if (unit->m_flags & 0xF0000) { + System_MPQEncrypt::Decrypt(reinterpret_cast(source), compressedSize & ~3u, sector + unit->m_key); + } + + if (hasChecksums) { + uint32_t checksum = unit->m_checksums.m_data[sector]; + + if (checksum) { + uint32_t adler = adler32(0, source, compressedSize); + + if (!adler) { + adler = -1; + } + + if (adler != checksum) { + return false; + } + } + } + + uint32_t outSize = sectorSize; + uint32_t method = unit->m_flags & 0xFF00; + + if (method == 0x100) { + System_SComp::PkwareBuffer buffer; + buffer.m_output = dest; + buffer.m_outputPos = 0; + buffer.m_outputSize = sectorSize; + buffer.m_input = source; + buffer.m_inputSize = compressedSize; + + if (blast(System_SComp::PkwareBufferRead, &buffer, System_SComp::PkwareBufferWrite, &buffer)) { + return false; + } + + outSize = buffer.m_outputPos; + } else if (method != 0x200 || !System_SComp::Decompress(dest, &outSize, source, compressedSize)) { + return false; + } + + if (sectorSize != outSize) { + return false; + } + + dest += outSize; + source += compressedSize; + + if (++sector >= end) { + return true; + } + } +} + +// VERIFY: 6ceb9959 auto +// OFFSET: 0x0045D860 +void IOMpqReadUnit::DecompressSectors(void* param) { + SectorJob* job = static_cast(param); + uint32_t sector = job->m_first; + uint8_t* scratch = job->m_scratch; + uint32_t none = 0; + + job->m_success = false; + job->m_scratchLeft = job->m_scratchSize; + job->m_destCursor = job->m_dest; + + while (true) { + uint32_t compressed = job->m_unit->CountCompressed(sector, job->m_end); + + if (compressed) { + if (!job->m_unit->DecompressRun(sector, compressed, &job->m_destCursor, &scratch, &job->m_scratchLeft)) { + return; + } + + sector += compressed; + + if (sector >= job->m_end) { + break; + } + } + + uint32_t uncompressed = job->m_unit->CountUncompressed(sector, job->m_end); + + if (uncompressed) { + if (!job->m_unit->VerifyRun(sector, uncompressed, &job->m_destCursor, nullptr, &none)) { + return; + } + + sector += uncompressed; + + if (sector >= job->m_end) { + break; + } + } + } + + job->m_success = true; +} + +// VERIFY: 2652cd24 auto +// OFFSET: 0x004342F0 +void IOMpqReadUnit::ReadSectors(SectorJob* job) { + uint32_t sector = job->m_first; + uint8_t* scratch = job->m_scratch; + uint32_t none = 0; + + job->m_success = false; + job->m_scratchLeft = job->m_scratchSize; + job->m_destCursor = job->m_dest; + + while (true) { + uint32_t compressed = job->m_unit->CountCompressed(sector, job->m_end); + + if (compressed) { + if (!job->m_unit->ReadRun(sector, compressed, &job->m_destCursor, &scratch, &job->m_scratchLeft)) { + return; + } + + sector += compressed; + + if (sector >= job->m_end) { + break; + } + } + + uint32_t uncompressed = job->m_unit->CountUncompressed(sector, job->m_end); + + if (uncompressed) { + if (!job->m_unit->ReadRun(sector, uncompressed, &job->m_destCursor, nullptr, &none)) { + return; + } + + sector += uncompressed; + + if (sector >= job->m_end) { + break; + } + } + } + + job->m_success = true; +} + +// VERIFY: 26ab9193 manual +// OFFSET: 0x0042BAC0 +bool IOMpqReadUnit::VerifyBlocks(IOMpqReadUnit* unit, uint32_t offset, uint32_t size, uint8_t* source, uint32_t first, uint32_t count, uint8_t* dest) { + bool hasChecksums = unit->m_checksums.m_count != 0; + + if (!(unit->m_flags & 0xF0000) && !hasChecksums) { + return true; + } + + uint32_t sector = first; + + if (first >= first + count) { + return true; + } + + while (true) { + uint32_t sectorSize = sector >= unit->m_sectorOffsets.m_count - 3 ? unit->m_lastSectorSize : unit->m_sectorSize; + + if (unit->m_flags & 0xF0000) { + System_MPQEncrypt::Decrypt(reinterpret_cast(dest), sectorSize & ~3u, sector + unit->m_key); + } + + if (hasChecksums) { + uint32_t checksum = unit->m_checksums.m_data[sector]; + + if (checksum) { + uint32_t adler = adler32(0, dest, sectorSize); + + if (!adler) { + adler = -1; + } + + if (adler != checksum) { + return false; + } + } + } + + dest += sectorSize; + + if (++sector >= first + count) { + return true; + } + } +} + +// VERIFY: 7713b1e2 manual +// OFFSET: 0x00430EB0 +IOMpqReadUnit::IOMpqReadUnit(uint32_t fileSize, uint32_t compressedSize, uint32_t sectorCount, uint32_t sectorSize, uint32_t flags, uint32_t key, bool ownsMemory, IOUnit* next) : IOUnit(next, ownsMemory), m_sectorOffsets(sectorCount + 2), m_checksums(0) { + this->m_key = key; + this->m_flags = flags; + this->m_fileSize = fileSize; + this->m_sectorSize = sectorSize; + this->m_lastSectorSize = fileSize % sectorSize; + + if (!this->m_lastSectorSize) { + this->m_lastSectorSize = sectorSize; + } + + this->m_sectorOffsets.m_data[0] = -3; + this->m_sectorOffsets.m_data[1] = compressedSize; +} + +// VERIFY: faed398b manual +// OFFSET: 0x00430FD0 +IOMpqReadUnit::IOMpqReadUnit(uint32_t fileSize, uint32_t compressedSize, uint32_t flags, uint32_t key, bool ownsMemory, IOUnit* next) : IOUnit(next, ownsMemory), m_sectorOffsets(3), m_checksums(0) { + this->m_sectorSize = fileSize; + this->m_lastSectorSize = fileSize; + this->m_flags = flags; + this->m_fileSize = fileSize; + this->m_key = key; + this->m_sectorOffsets.m_data[0] = 0; + this->m_sectorOffsets.m_data[1] = compressedSize; +} + +// VERIFY: b5e816a0 auto +// OFFSET: 0x00429770 +uint32_t IOMpqReadUnit::GetType() { + return 'mpqR'; +} + +// VERIFY: 04968570 manual +// OFFSET: 0x0045D900 +bool IOMpqReadUnit::Read(void* buffer, int64_t offset, uint32_t count, uint32_t* bytesRead) { + if (!count) { + return true; + } + + uint32_t state = this->m_sectorOffsets.m_data[0]; + + if (state >= 0xFFFFFFFD) { + if (state == 0xFFFFFFFF) { + return false; + } + + if (!this->LoadSectorTable()) { + Blizzard::File::AddToLastErrorStack(0, "LoadSectorTable failed", 4); + this->m_sectorOffsets.m_data[0] = state + 1; + return false; + } + } + + uint32_t start = static_cast(offset); + + if (start + count > this->m_fileSize) { + if (!bytesRead) { + return false; + } + + count = this->m_fileSize - start; + } + + uint32_t first = start / this->m_sectorSize; + uint32_t sectors = (this->m_sectorSize + start + count - 1) / this->m_sectorSize - first; + uint32_t workerCount; + + if (sectors >= 8) { + workerCount = sectors >> 2; + Blizzard::Hardware::GetProcessorFeatures(); + workerCount = std::min(std::min(workerCount, System_Hardware::s_processorFeatures.m_processorCount), 8u) - 1; + } else { + workerCount = 0; + } + + if (System_SFile::s_threadingMode == 1) { + workerCount >>= 1; + } else if (System_SFile::s_threadingMode == 2) { + workerCount = 0; + } + + System_MPQGlobals::Worker* workers[7]; + System_MPQGlobals::GetWorkerPool()->Acquire(workerCount, workers); + + uint32_t perJob = sectors / (workerCount + 1); + uint32_t extra = sectors % (workerCount + 1); + bool dispatched[7] = {}; + Blizzard::Memory::TempBuffer scratch(0x20000); + uint8_t* scratchPtr = scratch.m_data; + uint32_t scratchSize = 0x20000; + uint8_t* dest = static_cast(buffer); + SectorJob jobs[8]; + + for (uint32_t i = 0;; i++) { + SectorJob* job = &jobs[i]; + job->m_unit = this; + job->m_first = first; + job->m_end = first + perJob + (i < extra); + job->m_scratch = scratchPtr; + job->m_scratchSize = scratchSize; + job->m_dest = dest; + + ReadSectors(job); + + first = job->m_end; + scratchSize = job->m_scratchLeft; + scratchPtr = scratch.m_buffer + 0x20000 - scratchSize; + dest = job->m_destCursor; + + if (job->m_success) { + if (i >= workerCount) { + DecompressSectors(job); + } else { + System_MPQGlobals::Worker* worker = workers[i]; + + if (!worker->m_proc) { + worker->m_proc = DecompressSectors; + worker->m_param = job; + Blizzard::Lock::EventSet(worker->m_start.m_event); + } + + dispatched[i] = true; + } + } + + if (i + 1 > workerCount) { + break; + } + } + + for (uint32_t i = 0; i < workerCount; i++) { + if (dispatched[i]) { + Blizzard::Lock::EventWaitForever(workers[i]->m_done.m_event); + } + } + + System_MPQGlobals::GetWorkerPool()->Release(workerCount, workers); + + for (uint32_t i = 0; jobs[i].m_success;) { + if (++i > workerCount) { + if (bytesRead) { + *bytesRead = count; + } + + return true; + } + } + + Blizzard::File::AddToLastErrorStack(0, "ReadSectors/DecompressData failed", 4); + + return false; +} + +bool IOMpqReadUnit::Write(const void* buffer, int64_t offset, uint32_t count) { + return false; +} + +bool IOMpqReadUnit::Flush() { + return true; +} + +// VERIFY: 630ff2f3 auto +// OFFSET: 0x00426890 +bool IOMpqReadUnit::GetFileSize(int64_t* size) { + *size = this->m_fileSize; + return true; +} + +bool IOMpqReadUnit::SetFileSize(int64_t size) { + return false; +} + +// INLINED +IOMpqReadUnit::~IOMpqReadUnit() { +} + +bool IOMpqReadUnit::Close() { + return true; +} + +// VERIFY: a824d722 manual +// OFFSET: 0x0042FB60 +uint32_t IOMpqReadUnit::CountCompressed(uint32_t first, uint32_t end) { + uint32_t* offsets = this->m_sectorOffsets.m_data; + uint32_t previous = offsets[first]; + uint32_t sector = first; + + if (first < end) { + uint32_t last = this->m_sectorOffsets.m_count - 3; + + do { + uint32_t next = offsets[sector + 1]; + uint32_t sectorSize = sector >= last ? this->m_lastSectorSize : this->m_sectorSize; + + if (next - previous == sectorSize) { + break; + } + + sector++; + previous = next; + } while (sector < end); + } + + return sector - first; +} + +// VERIFY: fc6c70df manual +// OFFSET: 0x0042FC60 +uint32_t IOMpqReadUnit::CountUncompressed(uint32_t first, uint32_t end) { + uint32_t* offsets = this->m_sectorOffsets.m_data; + uint32_t previous = offsets[first]; + uint32_t sector = first; + + if (first < end) { + uint32_t last = this->m_sectorOffsets.m_count - 3; + + do { + uint32_t next = offsets[sector + 1]; + uint32_t sectorSize = sector >= last ? this->m_lastSectorSize : this->m_sectorSize; + + if (next - previous != sectorSize) { + break; + } + + sector++; + previous = next; + } while (sector < end); + } + + return sector - first; +} + +// VERIFY: 15ce3f88 manual +// OFFSET: 0x0045B4E0 +bool IOMpqReadUnit::DecompressRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft) { + uint32_t start = this->m_sectorOffsets.m_data[first]; + uint32_t size = this->m_sectorOffsets.m_data[first + count] - start; + uint32_t lastSize = first + count - 1 >= this->m_sectorOffsets.m_count - 3 ? this->m_lastSectorSize : this->m_sectorSize; + uint32_t total = lastSize + this->m_sectorSize * (count - 1); + + if (*scratchLeft < size) { + if (!DecompressBlocks(this, start, size, *dest + total - size, first, count, *dest)) { + return false; + } + + *dest += total; + return true; + } + + if (!DecompressBlocks(this, start, size, *scratch, first, count, *dest)) { + return false; + } + + *scratch += size; + *scratchLeft -= size; + *dest += total; + return true; +} + +// VERIFY: d7e23bc5 manual +// OFFSET: 0x00459A00 +bool IOMpqReadUnit::LoadSectorTable() { + uint32_t entries = this->m_sectorOffsets.m_count - 1; + uint32_t tableSize = entries * 4; + uint32_t compressedSize = this->m_sectorOffsets.m_data[1]; + uint32_t bytesRead = 0; + + if (!this->m_next->Read(this->m_sectorOffsets.m_data, 0, tableSize + 4, &bytesRead) || bytesRead < tableSize) { + this->m_sectorOffsets.m_data[1] = compressedSize; + return false; + } + + bytesRead &= ~3u; + + if (this->m_flags & 0xF0000) { + System_MPQEncrypt::Decrypt(this->m_sectorOffsets.m_data, bytesRead, this->m_key - 1); + } + + for (uint32_t i = 1; i < entries; i++) { + if (this->m_sectorOffsets.m_data[i] <= this->m_sectorOffsets.m_data[i - 1] || this->m_sectorOffsets.m_data[i] > compressedSize) { + this->m_sectorOffsets.m_data[1] = compressedSize; + return false; + } + } + + uint32_t* offsets = this->m_sectorOffsets.m_data; + + if (offsets[0] - tableSize >= 4) { + uint32_t checksumOffset = offsets[entries - 1]; + uint32_t checksumSize = offsets[entries] - checksumOffset; + uint32_t expected = tableSize - 4; + + if (checksumSize) { + if (checksumSize > expected) { + this->m_sectorOffsets.m_data[1] = compressedSize; + return false; + } + + this->m_checksums.Resize(entries - 1); + + if (!this->m_next->Read(this->m_checksums.m_data, checksumOffset, checksumSize, nullptr)) { + this->m_sectorOffsets.m_data[1] = compressedSize; + return false; + } + + uint32_t outSize = expected; + + if (!System_SComp::Decompress(this->m_checksums.m_data, &outSize, this->m_checksums.m_data, checksumSize) || expected != outSize) { + this->m_sectorOffsets.m_data[1] = compressedSize; + return false; + } + } + } + + return true; +} + +// VERIFY: 78cbfb13 manual +// OFFSET: 0x0042FBC0 +bool IOMpqReadUnit::ReadRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft) { + uint32_t start = this->m_sectorOffsets.m_data[first]; + uint32_t size = this->m_sectorOffsets.m_data[first + count] - start; + uint32_t lastSize = first + count - 1 >= this->m_sectorOffsets.m_count - 3 ? this->m_lastSectorSize : this->m_sectorSize; + uint32_t total = lastSize + this->m_sectorSize * (count - 1); + + if (*scratchLeft < size) { + if (!this->m_next->Read(*dest + total - size, start, size, nullptr)) { + return false; + } + + *dest += total; + return true; + } + + if (!this->m_next->Read(*scratch, start, size, nullptr)) { + return false; + } + + *scratch += size; + *scratchLeft -= size; + *dest += total; + return true; +} + +// VERIFY: ce01556e manual +// OFFSET: 0x0042FCC0 +bool IOMpqReadUnit::VerifyRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft) { + uint32_t start = this->m_sectorOffsets.m_data[first]; + uint32_t size = this->m_sectorOffsets.m_data[first + count] - start; + uint32_t lastSize = first + count - 1 >= this->m_sectorOffsets.m_count - 3 ? this->m_lastSectorSize : this->m_sectorSize; + uint32_t total = lastSize + this->m_sectorSize * (count - 1); + + if (*scratchLeft < size) { + if (!VerifyBlocks(this, start, size, *dest + total - size, first, count, *dest)) { + return false; + } + + *dest += total; + return true; + } + + if (!VerifyBlocks(this, start, size, *scratch, first, count, *dest)) { + return false; + } + + *scratch += size; + *scratchLeft -= size; + *dest += total; + return true; +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.h b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.h new file mode 100644 index 0000000..60d09ab --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.h @@ -0,0 +1,142 @@ +#ifndef ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_IOMPQUNIT_H +#define ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_IOMPQUNIT_H + +#include +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h" +#include "Engine/Source/BlizzardCore/Source/System/Memory/TempBuffer.h" + +class IOMpqUnit : public IOUnit { + public: + /* 0010 */ uint32_t unk_0010; + /* 0014 */ System_SFile::ARCHIVEREC* m_archive; + /* 0018 */ Blizzard::Memory::TempBuffer m_name; + + virtual uint32_t GetType(); + virtual ~IOMpqUnit(); + virtual bool Close(); + virtual System_SFile::ARCHIVEREC* GetArchive(); + virtual const char* GetName(); + virtual const char* GetPath() = 0; + virtual bool GetCompressedSize(int64_t* size) = 0; + virtual IOUnit* GetBaseUnit() = 0; + virtual int64_t GetOffset() = 0; + virtual uint64_t GetFileTime() = 0; + virtual uint32_t GetCrc() = 0; + virtual const Blizzard::Util::Digest<16>* GetMd5() = 0; + virtual bool HasMd5Check() = 0; + virtual void DisableMd5Check() = 0; + virtual uint32_t GetFlags() = 0; + virtual bool MarkDeleted() = 0; + virtual bool IsEncrypted() = 0; + + IOMpqUnit(System_SFile::ARCHIVEREC* archive, const char* name, bool ownsMemory, IOUnit* next); +}; + +class IOMpqArchiveUnit : public IOMpqUnit { + public: + /* 0124 */ uint32_t m_blockIndex; + /* 0128 */ System_SFile::BLOCKENTRY m_blockEntry; + + virtual bool GetFileSize(int64_t* size); + virtual bool SetFileSize(int64_t size); + virtual ~IOMpqArchiveUnit(); + virtual const char* GetPath(); + virtual bool GetCompressedSize(int64_t* size); + virtual IOUnit* GetBaseUnit(); + virtual int64_t GetOffset(); + virtual uint64_t GetFileTime(); + virtual uint32_t GetCrc(); + virtual const Blizzard::Util::Digest<16>* GetMd5(); + virtual bool HasMd5Check(); + virtual void DisableMd5Check(); + virtual uint32_t GetFlags(); + virtual bool MarkDeleted(); + virtual bool IsEncrypted(); + + IOMpqArchiveUnit(System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, bool ownsMemory, IOUnit* next); +}; + +class IOMpqArchiveReadUnit : public IOMpqArchiveUnit { + public: + static IOMpqArchiveReadUnit* Create(IOUnitContainer* container, System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, IOUnit* next); + + virtual bool Write(const void* buffer, int64_t offset, uint32_t count); + + IOMpqArchiveReadUnit(System_SFile::ARCHIVEREC* archive, const char* name, uint32_t blockIndex, bool ownsMemory, IOUnit* next); +}; + +class IOMpqDiskUnit : public IOMpqUnit { + public: + static IOMpqDiskUnit* Create(IOUnitContainer* container, System_SFile::ARCHIVEREC* archive, const char* name, const char* path, IOUnit* next); + + /* 0124 */ Blizzard::Memory::TempBuffer m_path; + + virtual ~IOMpqDiskUnit(); + virtual const char* GetPath(); + virtual bool GetCompressedSize(int64_t* size); + virtual IOUnit* GetBaseUnit(); + virtual int64_t GetOffset(); + virtual uint64_t GetFileTime(); + virtual uint32_t GetCrc(); + virtual const Blizzard::Util::Digest<16>* GetMd5(); + virtual bool HasMd5Check(); + virtual void DisableMd5Check(); + virtual uint32_t GetFlags(); + virtual bool MarkDeleted(); + virtual bool IsEncrypted(); + + IOMpqDiskUnit(System_SFile::ARCHIVEREC* archive, const char* name, const char* path, bool ownsMemory, IOUnit* next); +}; + +class IOMpqReadUnit : public IOUnit { + public: + struct SectorJob { + /* 0000 */ IOMpqReadUnit* m_unit; + /* 0004 */ uint32_t m_first; + /* 0008 */ uint32_t m_end; + /* 000C */ uint8_t* m_scratch; + /* 0010 */ uint32_t m_scratchSize; + /* 0014 */ uint8_t* m_dest; + /* 0018 */ uint32_t m_scratchLeft; + /* 001C */ uint8_t* m_destCursor; + /* 0020 */ bool m_success; + /* 0021 */ uint8_t unk_0021[3]; + }; + + static IOUnit* Create(IOUnitContainer* container, uint32_t fileSize, uint32_t compressedSize, uint32_t sectorSize, uint32_t flags, uint32_t key, IOUnit* next); + static IOUnit* CreateSingle(IOUnitContainer* container, uint32_t fileSize, uint32_t compressedSize, uint32_t flags, uint32_t key, IOUnit* next); + static bool DecompressBlocks(IOMpqReadUnit* unit, uint32_t offset, uint32_t size, uint8_t* source, uint32_t first, uint32_t count, uint8_t* dest); + static void DecompressSectors(void* param); + static void ReadSectors(SectorJob* job); + static bool VerifyBlocks(IOMpqReadUnit* unit, uint32_t offset, uint32_t size, uint8_t* source, uint32_t first, uint32_t count, uint8_t* dest); + + /* 0010 */ Blizzard::Memory::TempBuffer m_sectorOffsets; + /* 0040 */ Blizzard::Memory::TempBuffer m_checksums; + /* 0068 */ uint32_t m_sectorSize; + /* 006C */ uint32_t m_lastSectorSize; + /* 0070 */ uint32_t m_flags; + /* 0074 */ uint32_t m_key; + /* 0078 */ uint32_t m_fileSize; + + virtual uint32_t GetType(); + virtual bool Read(void* buffer, int64_t offset, uint32_t count, uint32_t* bytesRead); + virtual bool Write(const void* buffer, int64_t offset, uint32_t count); + virtual bool Flush(); + virtual bool GetFileSize(int64_t* size); + virtual bool SetFileSize(int64_t size); + virtual ~IOMpqReadUnit(); + virtual bool Close(); + + IOMpqReadUnit(uint32_t fileSize, uint32_t compressedSize, uint32_t sectorCount, uint32_t sectorSize, uint32_t flags, uint32_t key, bool ownsMemory, IOUnit* next); + IOMpqReadUnit(uint32_t fileSize, uint32_t compressedSize, uint32_t flags, uint32_t key, bool ownsMemory, IOUnit* next); + uint32_t CountCompressed(uint32_t first, uint32_t end); + uint32_t CountUncompressed(uint32_t first, uint32_t end); + bool DecompressRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft); + bool LoadSectorTable(); + bool ReadRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft); + bool VerifyRun(uint32_t first, uint32_t count, uint8_t** dest, uint8_t** scratch, uint32_t* scratchLeft); +}; + +#endif diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.cpp b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.cpp index 0e59493..2c0655c 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.cpp +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.cpp @@ -15,7 +15,7 @@ bool System_MPQGlobals::ArchiveLess::operator()(const System_SFile::ARCHIVEREC* return a->m_priority > b->m_priority; } -// VERIFY: df235665 manual +// VERIFY: 4886183c manual // OFFSET: 0x0044FFF0 System_MPQGlobals::Globals::Globals() { this->m_mopaq.m_directAccess = 0; @@ -24,9 +24,9 @@ System_MPQGlobals::Globals::Globals() { this->m_mopaq.m_basePath[0] = '\0'; } - this->m_mopaq.unk_0108 = 0; - this->m_mopaq.unk_010C = 0; - this->m_mopaq.unk_0110 = 0; + this->m_mopaq.m_openCallback = nullptr; + this->m_mopaq.m_openCallbackParam = nullptr; + this->m_mopaq.m_md5Mode = 0; } // VERIFY: e5b01062 manual @@ -58,3 +58,68 @@ Blizzard::Lock::RMutex* System_MPQGlobals::GetMopaqGlobalsLock() { System_MPQGlobals::WorkerPool* System_MPQGlobals::GetWorkerPool() { return &s_globals.Get()->m_workerPool; } + +// VERIFY: af26db8a manual +// OFFSET: 0x0042BA60 +uint32_t System_MPQGlobals::Worker::ThreadProc(void* param) { + Worker* worker = static_cast(param); + + Blizzard::Lock::EventWaitForever(worker->m_start.m_event); + + while (worker->m_proc) { + worker->m_proc(worker->m_param); + worker->m_proc = nullptr; + worker->m_param = nullptr; + Blizzard::Lock::EventSet(worker->m_done.m_event); + Blizzard::Lock::EventWaitForever(worker->m_start.m_event); + } + + return 0; +} + +// VERIFY: ab9a819e manual +// OFFSET: 0x00451200 +System_MPQGlobals::Worker::Worker() { + this->m_proc = nullptr; + this->m_param = nullptr; + Blizzard::Thread::Create(ThreadProc, this, &this->m_thread, "", 0, 0x40000); +} + +// VERIFY: 7d61b910 manual +// OFFSET: 0x00451A30 +void System_MPQGlobals::WorkerPool::Acquire(uint32_t count, Worker** workers) { + uint32_t i = 0; + + if (!count) { + return; + } + + Blizzard::Lock::MutexEnter(this->m_mutex.m_mutex); + + for (; i < count && !this->m_workers.empty(); i++) { + workers[i] = this->m_workers.back(); + this->m_workers.pop_back(); + } + + for (; i < count; i++) { + workers[i] = new Worker(); + } + + Blizzard::Lock::MutexLeave(this->m_mutex.m_mutex); +} + +// VERIFY: d764e194 manual +// OFFSET: 0x00446060 +void System_MPQGlobals::WorkerPool::Release(uint32_t count, Worker** workers) { + if (!count) { + return; + } + + Blizzard::Lock::MutexEnter(this->m_mutex.m_mutex); + + do { + this->m_workers.push_back(*workers++); + } while (--count); + + Blizzard::Lock::MutexLeave(this->m_mutex.m_mutex); +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h index 4042321..54bec54 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h @@ -8,6 +8,7 @@ #include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h" #include "Engine/Source/BlizzardCore/Source/System/Lock/Lock.h" #include "Engine/Source/BlizzardCore/Source/System/Memory/AllocSTL.h" +#include "Engine/Source/BlizzardCore/Source/System/Thread/Thread.h" namespace System_MPQGlobals { @@ -20,15 +21,32 @@ typedef std::set m_workers; + /* 0018 */ std::vector m_workers; + + void Acquire(uint32_t count, Worker** workers); + void Release(uint32_t count, Worker** workers); }; class Globals { diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.cpp b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.cpp index 04e5157..7fedeca 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.cpp +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.cpp @@ -1,10 +1,26 @@ +#include #include "Engine/Source/BlizzardCore/SFile2-Core.h" -#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOFileUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnitContainer.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/IOMpqUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQEncrypt.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h" #include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h" +#include "Engine/Source/BlizzardCore/Source/System/Lock/Lock.h" +#include "Engine/Source/BlizzardCore/Source/System/Log/Log.h" +#include "Engine/Source/BlizzardCore/Source/System/String/Path.h" +#include "Engine/Source/Storm/H/storm.h" // STATIC: 0x00B38344 Blizzard::Thread::LocalObject System_SFile::s_rawReadInfo; +// STATIC: 0x00B3858C +uint32_t System_SFile::s_threadingMode; + // INLINED System_SFile::RawReadInfo::RawReadInfo() { this->m_retryNative = false; @@ -15,3 +31,209 @@ void STORMAPI SFile::CreateOverlapped(SOVERLAPPED* overlapped) { void STORMAPI SFile::DestroyOverlapped(SOVERLAPPED* overlapped) { } + +// VERIFY: 65f10e31 auto +// OFFSET: 0x00426E20 +bool System_SFile::CheckCrc(void* context, uint32_t crc) { + return crc == static_cast(context)->m_blockEntry.m_crc; +} + +// VERIFY: dc1be6b1 auto +// OFFSET: 0x00426FD0 +void Blizzard::Mopaq::SFileSetThreadingMode(uint32_t mode) { + System_SFile::s_threadingMode = mode; +} + +// VERIFY: 94d8b66b manual +// OFFSET: 0x0042ED90 +IOUnit* System_SFile::GetLockedUnit(IOUnit* file) { + if (!file || file->GetType() != 'mpq ') { + return nullptr; + } + + IOUnit* lock = file->m_next; + + if (!lock || lock->GetType() != 'lock') { + return nullptr; + } + + return lock->m_next; +} + +// VERIFY: df584044 auto +// OFFSET: 0x0042EDD0 +bool System_SFile::InsertLockedUnit(IOUnit* file, IOUnit* unit) { + if (!file || !unit || file->GetType() != 'mpq ') { + return false; + } + + return IOLockUnit::ReplaceNext(file->m_next, unit); +} + +// VERIFY: 07c1b49c manual +// OFFSET: 0x00437310 +IOUnit* System_SFile::OpenFileUnit(IOUnitContainer* container, const char* path, int32_t mode) { + if (mode & 0x40) { + return System_Mopaq::OpenDiskFile(container, path, mode); + } + + return IOFileUnit::Create(container, path, mode); +} + +// VERIFY: 66befaed auto +// OFFSET: 0x00437340 +IOUnit* System_SFile::CreateRangeUnit(IOUnitContainer* container, int64_t start, int64_t end, IOUnit* next) { + return IORangeUnit::Create(container, start, end, next); +} + +// VERIFY: ed823991 manual +// OFFSET: 0x004609B0 +int32_t Blizzard::Mopaq::SFileOpenFileEx(System_SFile::ARCHIVEREC* handle, const char* name, const Blizzard::Util::Digest<16>& md5, uint32_t flags, IOMpqUnit** result) { + IOMpqUnit* file = nullptr; + *result = nullptr; + System_SFile::ARCHIVEREC* archive = nullptr; + uint32_t error = 104; + uint32_t blockIndex; + char path[260]; + int32_t found = GetFileBlockEntry(handle, name, md5, flags, nullptr, &archive, &blockIndex, nullptr, path); + + Blizzard::Log::Printf(3, "Logs/Profiling_File_SFileOpenFileEx.log", "%s\n", name); + + switch (found) { + case 0: + case 4: { + error = 2; + goto fail; + } + + case 1: { + System_SFile::BLOCKENTRY entry = archive->GetBlockEntry(blockIndex); + + if (!(entry.m_flags & 0x80000000)) { + error = 1006; + goto fail; + } + + IOChecksumUnit* checksum = nullptr; + + if (entry.m_flags & 0xFFF00) { + uint32_t fileSize = entry.m_fileSize; + uint32_t key = 0; + + if (entry.m_flags & 0xF0000) { + key = System_MPQEncrypt::Hash(Blizzard::String::FindFilename(name), 3); + + if (entry.m_flags & 0x20000) { + key = fileSize ^ (key + static_cast(entry.m_offset) - static_cast(archive->m_archiveOffset)); + } + } + + archive->m_file->m_refCount.Increment(); + + if (entry.m_flags & 0x1000000) { + IOUnitContainer container(sizeof(IOMpqArchiveReadUnit), sizeof(IOCrcUnit), sizeof(IOAlignUnit), sizeof(IOLockUnit), sizeof(IOMpqReadUnit), sizeof(IORangeUnit), 0); + IOUnit* range = System_SFile::CreateRangeUnit(&container, entry.m_offset, entry.m_offset + entry.m_compressedSize, archive->m_file); + IOUnit* read = IOMpqReadUnit::CreateSingle(&container, fileSize, entry.m_compressedSize, entry.m_flags, key, range); + IOUnit* crc = IOCrcUnit::Create(&container, fileSize, entry.m_crc ? System_SFile::CheckCrc : nullptr, false, &checksum, read); + IOUnit* align = IOAlignUnit::Create(&container, 0, fileSize, 1, false, false, 1, crc); + IOUnit* lock = IOLockUnit::Create(&container, align); + file = IOMpqArchiveReadUnit::Create(&container, archive, name, blockIndex, lock); + } else { + bool checkCrc = !(entry.m_flags & 0x4000000) && entry.m_crc; + IOUnitContainer container(sizeof(IOMpqArchiveReadUnit), sizeof(IOCrcUnit), sizeof(IOAlignUnit), sizeof(IOLockUnit), sizeof(IOMpqReadUnit), sizeof(IORangeUnit), 0); + IOUnit* range = System_SFile::CreateRangeUnit(&container, entry.m_offset, entry.m_offset + entry.m_compressedSize, archive->m_file); + IOUnit* read = IOMpqReadUnit::Create(&container, fileSize, entry.m_compressedSize, archive->m_sectorSize, entry.m_flags, key, range); + IOUnit* crc = IOCrcUnit::Create(&container, fileSize, checkCrc ? System_SFile::CheckCrc : nullptr, false, &checksum, read); + IOUnit* align = IOAlignUnit::Create(&container, 0, archive->m_sectorSize, 1, true, true, 1, crc); + IOUnit* lock = IOLockUnit::Create(&container, align); + file = IOMpqArchiveReadUnit::Create(&container, archive, name, blockIndex, lock); + } + } else { + if (entry.m_compressedSize != entry.m_fileSize) { + error = 1006; + goto fail; + } + + archive->m_file->m_refCount.Increment(); + + IOUnitContainer container(sizeof(IOMpqArchiveReadUnit), sizeof(IOCrcUnit), sizeof(IOLockUnit), sizeof(IORangeUnit), 0); + IOUnit* range = System_SFile::CreateRangeUnit(&container, entry.m_offset, entry.m_offset + entry.m_fileSize, archive->m_file); + IOUnit* crc = IOCrcUnit::Create(&container, entry.m_fileSize, entry.m_crc ? System_SFile::CheckCrc : nullptr, false, &checksum, range); + IOUnit* lock = IOLockUnit::Create(&container, crc); + file = IOMpqArchiveReadUnit::Create(&container, archive, name, blockIndex, lock); + } + + if (checksum) { + checksum->SetContext(file); + } + + found = 1; + break; + } + + case 2: + case 3: { + IOUnitContainer container(sizeof(IOMpqDiskUnit), sizeof(IOUnbufferedFileUnit), 0); + IOUnit* disk = System_SFile::OpenFileUnit(&container, path, (((flags & 4) | 2) >> 1) | 0x1004); + file = IOMpqDiskUnit::Create(&container, archive, name, path, disk); + found = 2; + break; + } + + default: { + goto fail; + } + } + + if (!file || (archive && !archive->RemoveInternalArchiveRef())) { + goto fail; + } + + if (found) { + void (*callback)(const char* name, void* param); + void* param; + + { + Blizzard::Lock::ScopedMutex lock(System_MPQGlobals::GetMopaqGlobalsLock()->m_mutex); + System_MPQGlobals::MopaqGlobals* globals = System_MPQGlobals::GetMopaqGlobals(); + callback = globals->m_openCallback; + param = globals->m_openCallbackParam; + } + + if (callback) { + callback(name, param); + } + + if (System_MPQGlobals::GetMopaqGlobals()->m_md5Mode == 1 && file->HasMd5Check()) { + int64_t size; + file->GetFileSize(&size); + + IOUnit* inner = System_SFile::GetLockedUnit(file); + IOUnit* md5Unit = IOMd5Unit::Create(nullptr, size, System_MPQGlobals::GetMopaqGlobals()->m_md5Callback, true, nullptr, inner); + static_cast(md5Unit)->SetContext(file); + + bool inserted = System_SFile::InsertLockedUnit(file, md5Unit); + + if (!inner || !inserted) { + inner->m_refCount.Increment(); + md5Unit->Release(); + found = 0; + } + } + } + + *result = file; + return found; + +fail: + System_SFile::s_lastError = error; + found = 0; + + if (file) { + file->Release(); + file = nullptr; + } + + *result = file; + return found; +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h index d829d4c..6b68b52 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h @@ -1,10 +1,18 @@ #ifndef ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_SFILE_H #define ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_SFILE_H +#include +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h" #include "Engine/Source/BlizzardCore/Source/System/Thread/ThreadLocalStorage.h" +class IOMpqUnit; +class IOUnit; +class IOUnitContainer; + namespace System_SFile { +class ARCHIVEREC; + struct RawReadInfo { /* 0000 */ bool m_retryNative; @@ -12,7 +20,23 @@ struct RawReadInfo { }; extern Blizzard::Thread::LocalObject s_rawReadInfo; +extern uint32_t s_threadingMode; + +bool CheckCrc(void* context, uint32_t crc); +IOUnit* CreateRangeUnit(IOUnitContainer* container, int64_t start, int64_t end, IOUnit* next); +IOUnit* GetLockedUnit(IOUnit* file); +bool InsertLockedUnit(IOUnit* file, IOUnit* unit); +IOUnit* OpenFileUnit(IOUnitContainer* container, const char* path, int32_t mode); } +namespace Blizzard { +namespace Mopaq { + +int32_t SFileOpenFileEx(System_SFile::ARCHIVEREC* handle, const char* name, const Blizzard::Util::Digest<16>& md5, uint32_t flags, IOMpqUnit** result); +void SFileSetThreadingMode(uint32_t mode); + +} +} + #endif diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.cpp b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.cpp index 5fb84e9..57a458b 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.cpp +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.cpp @@ -1213,3 +1213,361 @@ hashError: InitEmptyTables(this, 1); } + +// VERIFY: 30fc5178 manual +// OFFSET: 0x0042C3F0 +System_SFile::BLOCKENTRY System_SFile::ARCHIVEREC::GetBlockEntry(uint32_t index) { + BlockEntryTable* blocks = this->m_blockEntryTable; + + if (!blocks) { + return this->m_blockTable[index]; + } + + BLOCKENTRY entry; + blocks->UnpackEntry(index, &entry); + + if (this->m_flags & 0x80) { + entry.m_crc = this->m_blockEntryTable->m_crcs[index]; + } + + if (this->m_flags & 0x40) { + entry.m_md5 = this->m_blockEntryTable->m_md5s[index]; + } + + if (this->m_flags & 0x100) { + entry.m_fileTime = this->m_blockEntryTable->m_fileTimes[index]; + } + + return entry; +} + +// VERIFY: 913cd4bd manual +// OFFSET: 0x0042C7E0 +uint32_t System_SFile::SearchHashTable(ARCHIVEREC* archive, const char* name, bool matchNeutral, bool* deleted) { + archive->InitializeTables(); + + uint16_t locale = archive->m_locale; + uint8_t platform = archive->m_platform; + std::vector& table = archive->m_hashTable; + uint32_t mask = static_cast(table.size()) - 1; + uint32_t index = System_MPQEncrypt::Hash(name, 0) & mask; + uint32_t found = -1; + HASHENTRY* entry = &table[index]; + + if (entry->m_blockIndex == -1) { + return -1; + } + + uint32_t hashA = System_MPQEncrypt::Hash(name, 1); + uint32_t hashB = System_MPQEncrypt::Hash(name, 2); + uint32_t start = index; + + while (true) { + if (entry->m_hashA == hashA && entry->m_hashB == hashB && entry->m_blockIndex != -2) { + if (entry->m_locale == locale && entry->m_platform == platform) { + found = index; + break; + } + + if (matchNeutral && (!entry->m_locale || entry->m_locale == locale) && (!entry->m_platform || entry->m_platform == platform)) { + found = index; + } + } + + index = (index + 1) & mask; + + if (index == start) { + break; + } + + entry = &table[index]; + + if (entry->m_blockIndex == -1) { + break; + } + } + + if (found != -1 && deleted) { + *deleted = (archive->GetBlockEntry(table[found].m_blockIndex).m_flags & 0x2000000) != 0; + } + + return found; +} + +// VERIFY: a62931d4 manual +// OFFSET: 0x00454120 +bool System_SFile::CheckFileExistsOnDisk(const char* name, uint32_t flags, char* result) { + char buffer[260]; + + if (!(flags & 3)) { + return false; + } + + if (!result) { + result = buffer; + } + + if (flags & 2) { + ConvertRelativePathName(Blizzard::String::FindFilename(name), result); + + if (Blizzard::File::Exists(result) == 1) { + return true; + } + } + + if (!(flags & 1)) { + return false; + } + + if (Blizzard::File::GetRootChars(name)) { + Blizzard::String::Copy(result, name, 260); + } else { + ConvertRelativePathName(name, result); + } + + return Blizzard::File::Exists(result) == 1; +} + +// VERIFY: 138e567c manual +// OFFSET: 0x0045BED0 +void System_SFile::ARCHIVEREC::BuildBlockKeys() { + if (!this->m_tablesInitialized) { + this->InitializeTables(); + } + + if (this->m_blockKeysBuilt) { + return; + } + + ArchivePtr archive(GetArchivePtr(this, true)); + + if (this->m_blockKeys.empty()) { + for (uint32_t i = 0; i < this->m_header.m_blockTableSize; i++) { + this->m_blockKeys.insert(std::make_pair(this->m_blockTable[i].m_md5, i)); + } + + this->m_blockKeysBuilt = true; + } +} + +// VERIFY: 520c5718 manual +// OFFSET: 0x0045C880 +uint32_t System_SFile::FindBlockEntry(ARCHIVEREC* archive, const Blizzard::Util::Digest<16>& md5, bool* deleted) { + archive->BuildBlockKeys(); + + std::map, uint32_t>::iterator it = archive->m_blockKeys.find(md5); + + if (it == archive->m_blockKeys.end()) { + return -1; + } + + *deleted = (archive->GetBlockEntry(it->second).m_flags & 0x2000000) != 0; + + return it->second; +} + +// VERIFY: 962c3879 manual +// OFFSET: 0x0045F7D0 +int32_t FindFileInOneArchive(System_SFile::ARCHIVEREC* archive, const char* name, const uint64_t& hash, const Blizzard::Util::Digest<16>& md5, uint32_t flags, System_SFile::FileLookup* lookup) { + if (!(flags & 0x10) && (archive->m_externalRefs.m_value <= 0 || archive->unk_0200 > 0)) { + return 0; + } + + bool deleted = false; + + if (md5 != System_SFile::s_emptyMd5) { + lookup->m_blockIndex = System_SFile::FindBlockEntry(archive, md5, &deleted); + } + + if (name && lookup->m_blockIndex == -1 && md5 == System_SFile::s_emptyMd5) { + char path[260]; + uint32_t prefix; + bool prefixed = false; + + if (archive->m_directPath.empty()) { + prefix = 0; + } else { + prefix = Blizzard::String::Copy(path, archive->m_directPath.c_str(), 260); + Blizzard::String::Copy(&path[prefix], name, 260 - prefix); + name = path; + prefixed = true; + } + + if (!archive->m_file) { + if (!lookup->m_checkedDisk || prefix) { + if (System_SFile::CheckFileExistsOnDisk(name, 1, lookup->m_path)) { + lookup->m_archive = archive; + return 2; + } + + if (!prefix) { + lookup->m_checkedDisk = true; + } + } + + return 0; + } + + if (archive->m_parent) { + archive = archive->m_parent; + } + + if (prefix && (flags & 1)) { + if (System_SFile::CheckFileExistsOnDisk(name, 1, lookup->m_path)) { + return 3; + } + } + + archive->InitializeTables(); + + if (archive->m_hashTable.empty()) { + uint64_t key = prefixed ? System_SFile::HashEntryTable::CalculateHash(name) : hash; + lookup->m_blockIndex = archive->m_hashEntryTable->GetEntry(key, archive->m_blockEntryTable); + + if (lookup->m_blockIndex == -1) { + return 0; + } + + deleted = (archive->GetBlockEntry(lookup->m_blockIndex).m_flags & 0x2000000) != 0; + } else { + lookup->m_hashIndex = System_SFile::SearchHashTable(archive, name, true, &deleted); + + if (lookup->m_hashIndex != -1) { + lookup->m_blockIndex = archive->m_hashTable[lookup->m_hashIndex].m_blockIndex; + } + } + } + + if (deleted) { + return 4; + } + + if (lookup->m_blockIndex == -1) { + return 0; + } + + lookup->m_archive = archive; + + return 1; +} + +// VERIFY: b9bc5aa5 manual +// OFFSET: 0x0045FA10 +int32_t GetFileBlockEntry(System_SFile::ARCHIVEREC* handle, const char* name, const Blizzard::Util::Digest<16>& md5, uint32_t flags, System_SFile::ARCHIVEREC** archiveOut, System_SFile::ARCHIVEREC** recordOut, uint32_t* blockIndex, uint32_t* hashIndex, char* path) { + if (archiveOut) { + *archiveOut = nullptr; + } + + if (recordOut) { + *recordOut = nullptr; + } + + if (blockIndex) { + *blockIndex = 0; + } + + if (hashIndex) { + *hashIndex = -1; + } + + System_SFile::ArchivePtr archive(System_SFile::GetArchivePtr(handle, true)); + + if (handle && !archive.m_archive) { + return 0; + } + + System_SFile::FileLookup lookup; + lookup.m_hashIndex = -1; + lookup.m_blockIndex = -1; + int32_t result = 0; + lookup.m_checkedDisk = false; + lookup.m_archive = nullptr; + lookup.m_path = path; + + if (flags & 3) { + lookup.m_checkedDisk = true; + + if (System_SFile::CheckFileExistsOnDisk(name, flags, path)) { + result = 3; + } + } + + if (!(flags & 8)) { + if (!result) { + Blizzard::Lock::ScopedMutex lock(System_MPQGlobals::GetArchiveLock()->m_mutex); + uint64_t hash = System_SFile::HashEntryTable::CalculateHash(name); + + if (archive.m_archive) { + result = FindFileInOneArchive(archive.m_archive, name, hash, md5, flags, &lookup); + } else { + System_MPQGlobals::ArchiveList* list = System_MPQGlobals::GetArchiveList(); + + for (System_MPQGlobals::ArchiveList::iterator it = list->begin(); it != list->end(); ++it) { + result = FindFileInOneArchive(*it, name, hash, md5, flags, &lookup); + + if (result) { + handle = *it; + break; + } + } + } + } + + if (result == 1 || result == 2) { + if (recordOut) { + lookup.m_archive->m_internalRefs.Increment(); + *recordOut = lookup.m_archive; + } + + if (archiveOut) { + *archiveOut = handle; + } + + if (result == 1) { + if (hashIndex) { + *hashIndex = lookup.m_hashIndex; + } + + if (blockIndex) { + *blockIndex = lookup.m_blockIndex; + } + } + } + } + + return result; +} + +// VERIFY: adbb7274 manual +// OFFSET: 0x00460EA0 +int32_t Blizzard::Mopaq::SFileFileExists(System_SFile::ARCHIVEREC* handle, const char* name, uint32_t flags, System_SFile::ARCHIVEREC** archiveOut) { + Blizzard::Util::Digest<16> md5 = System_SFile::s_emptyMd5; + int32_t result = GetFileBlockEntry(handle, name, md5, flags, archiveOut, nullptr, nullptr, nullptr, nullptr); + + if (result == 4) { + return 0; + } + + return result; +} + +// VERIFY: 2e816731 manual +// OFFSET: 0x0042C680 +void System_SFile::ARCHIVEREC::SetMD5Check(uint32_t index, bool enabled) { + BlockEntryTable* blocks = this->m_blockEntryTable; + + if (blocks) { + if ((this->m_flags & 0x200) && (blocks->m_tableFlags & 0x200)) { + uint8_t bit = enabled; + blocks->m_entries->WriteBits(blocks->m_md5Bit + index * blocks->m_entryBits, blocks->m_md5Bits, &bit, 1); + } + } else { + this->m_blockTable[index].m_md5Check = enabled; + } +} + +// VERIFY: 42f50f1a manual +// OFFSET: 0x00431A20 +void System_SFile::ARCHIVEREC::SetBlockEntry(uint32_t index, const BLOCKENTRY& entry) { + this->m_blockTable[index] = entry; +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h index b039f8a..704de2e 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileArchives.h @@ -19,8 +19,8 @@ struct BLOCKENTRY { /* 0008 */ uint32_t m_compressedSize; /* 000C */ uint32_t m_fileSize; /* 0010 */ uint32_t m_flags; - /* 0014 */ uint32_t m_crc; - /* 0018 */ uint64_t m_fileTime; + /* 0014 */ uint64_t m_fileTime; + /* 001C */ uint32_t m_crc; /* 0020 */ Blizzard::Util::Digest<16> m_md5; /* 0030 */ bool m_md5Check; }; @@ -156,7 +156,9 @@ class ARCHIVEREC { /* 01CC */ std::map, uint32_t> m_blockKeys; /* 01D8 */ bool m_blockKeysBuilt; /* 01D9 */ bool m_tablesInitialized; - /* 01DA */ uint8_t unk_01DA[6]; + /* 01DA */ uint16_t m_locale; + /* 01DC */ uint8_t m_platform; + /* 01DD */ uint8_t unk_01DD[3]; /* 01E0 */ uint32_t unk_01E0; /* 01E4 */ std::vector unk_01E4; /* 01F4 */ uint32_t unk_01F4; @@ -174,13 +176,26 @@ class ARCHIVEREC { ARCHIVEREC(); ~ARCHIVEREC(); + void BuildBlockKeys(); void BuildBlockTable(); void ConvertToNewHashTableFormat(); bool FillNewHashTable(int32_t hashBits); + BLOCKENTRY GetBlockEntry(uint32_t index); void InitializeTables(); void PackBlockTable(); bool ReadAdditionalAttributes(); bool RemoveInternalArchiveRef(); + void SetBlockEntry(uint32_t index, const BLOCKENTRY& entry); + void SetMD5Check(uint32_t index, bool enabled); +}; + +struct FileLookup { + /* 0000 */ bool m_checkedDisk; + /* 0001 */ uint8_t unk_0001[3]; + /* 0004 */ ARCHIVEREC* m_archive; + /* 0008 */ uint32_t m_hashIndex; + /* 000C */ uint32_t m_blockIndex; + /* 0010 */ char* m_path; }; class ArchivePtr { @@ -207,19 +222,26 @@ extern Blizzard::Util::Digest<16> s_emptyMd5; extern Blizzard::Lock::Atomic s_nextArchiveId; extern uint32_t s_lastError; +bool CheckFileExistsOnDisk(const char* name, uint32_t flags, char* result); void ConvertRelativePathName(const char* path, char* result); +uint32_t FindBlockEntry(ARCHIVEREC* archive, const Blizzard::Util::Digest<16>& md5, bool* deleted); ARCHIVEREC* GetArchivePtr(ARCHIVEREC* handle, bool addRef); bool InitEmptyTables(ARCHIVEREC* archive, uint32_t hashCount); bool OpenDirectPathAsArchive(const char* path, int32_t priority, uint32_t flags, ARCHIVEREC** result); bool s_OpenArchive(ARCHIVEREC* archive, uint32_t flags, ARCHIVEREC** result); +uint32_t SearchHashTable(ARCHIVEREC* archive, const char* name, bool matchNeutral, bool* deleted); } +int32_t FindFileInOneArchive(System_SFile::ARCHIVEREC* archive, const char* name, const uint64_t& hash, const Blizzard::Util::Digest<16>& md5, uint32_t flags, System_SFile::FileLookup* lookup); +int32_t GetFileBlockEntry(System_SFile::ARCHIVEREC* handle, const char* name, const Blizzard::Util::Digest<16>& md5, uint32_t flags, System_SFile::ARCHIVEREC** archiveOut, System_SFile::ARCHIVEREC** recordOut, uint32_t* blockIndex, uint32_t* hashIndex, char* path); + namespace Blizzard { namespace Mopaq { bool SFileCloseArchive(System_SFile::ARCHIVEREC* handle); bool SFileEnableDirectAccess(uint32_t enable); +int32_t SFileFileExists(System_SFile::ARCHIVEREC* handle, const char* name, uint32_t flags, System_SFile::ARCHIVEREC** archiveOut); uint32_t SFileGetLastError(); bool SFileOpenArchive(const char* path, int32_t priority, uint32_t flags, System_SFile::ARCHIVEREC** result); bool SFileOpenArchive(IOUnit* file, const char* name, int32_t priority, uint32_t flags, System_SFile::ARCHIVEREC** result); diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.cpp b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.cpp new file mode 100644 index 0000000..a4cf259 --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Util/Hash.h" +#include "Engine/Source/BlizzardCore/Source/System/String/String.h" + +// STATIC: 0x00B38338 +System_SFile::SignatureTable System_SFile::s_signatures; + +// VERIFY: 2da0c4e0 manual +// OFFSET: 0x0042ECC0 +System_SFile::SignatureKey::SignatureKey(const char* name) { + uint32_t pc = 2; + uint32_t pb = 1; + Blizzard::Util::hashlittle2(name, name ? strlen(name) : 0, &pc, &pb); + + uint64_t hash = pc + (static_cast(pb) << 32); + this->m_lo = static_cast(hash) | 0x3F; + this->m_hi = static_cast(hash >> 32); +} + +// VERIFY: 7f796dd8 manual +// OFFSET: 0x00437470 +bool System_SFile::SignatureTable::GetSignature(const char* name, Blizzard::Util::Digest<16>* md5) { + if (!md5) { + return false; + } + + char lower[260]; + Blizzard::String::Copy(lower, name, sizeof(lower)); + + for (char* p = lower; *p; p++) { + *p = static_cast(tolower(*p)); + } + + SignatureKey key(lower); + + return this->Get(key, md5); +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.h b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.h new file mode 100644 index 0000000..2a33845 --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFileSignatures.h @@ -0,0 +1,26 @@ +#ifndef ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_SFILESIGNATURES_H +#define ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_MOPAQ_SFILESIGNATURES_H + +#include +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/Digest.h" +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/HashMap.h" + +namespace System_SFile { + +struct SignatureKey { + /* 0000 */ uint32_t m_lo; + /* 0004 */ uint32_t m_hi; + + SignatureKey(const char* name); +}; + +class SignatureTable : public Blizzard::Util::HashMap> { + public: + bool GetSignature(const char* name, Blizzard::Util::Digest<16>* md5); +}; + +extern SignatureTable s_signatures; + +} + +#endif diff --git a/Engine/Source/BlizzardCore/Source/Packages/Util/MD5.cpp b/Engine/Source/BlizzardCore/Source/Packages/Util/MD5.cpp new file mode 100644 index 0000000..a3727f9 --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/Util/MD5.cpp @@ -0,0 +1,173 @@ +#include +#include +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/MD5.h" + +#define MD5_ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define MD5_F(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define MD5_G(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define MD5_H(x, y, z) ((x) ^ (y) ^ (z)) +#define MD5_I(x, y, z) ((y) ^ ((x) | ~(z))) +#define MD5_STEP(f, a, b, c, d, k, s, ti) t = a + f(b, c, d) + x[k] + (ti); a = MD5_ROTATE_LEFT(t, s) + b + +// STATIC: 0x00A505D8 +static const uint8_t s_padding[64] = { 0x80 }; + +// INLINED +Blizzard::Util::MD5::MD5() { + this->m_count[0] = 0; + this->m_count[1] = 0; + this->m_state[0] = 0x67452301; + this->m_state[1] = 0xEFCDAB89; + this->m_state[2] = 0x98BADCFE; + this->m_state[3] = 0x10325476; +} + +// VERIFY: 488235a5 manual +// OFFSET: 0x0042B280 +void Blizzard::Util::MD5::Finish(uint8_t* digest) { + uint8_t length[8]; + + for (int32_t i = 0; i < 8; i++) { + length[i] = static_cast(this->m_count[i >> 2] >> ((i & 3) << 3)); + } + + this->Process(s_padding, ((55 - (this->m_count[0] >> 3)) & 63) + 1); + this->Process(length, 8); + + for (int32_t i = 0; i < 16; i++) { + digest[i] = static_cast(this->m_state[i >> 2] >> ((i & 3) << 3)); + } +} + +// VERIFY: cf6eed7b manual +// OFFSET: 0x004250D0 +void Blizzard::Util::MD5::Process(const uint8_t* block) { + uint32_t a = this->m_state[0]; + uint32_t b = this->m_state[1]; + uint32_t c = this->m_state[2]; + uint32_t d = this->m_state[3]; + uint32_t t; + const uint32_t* x; + uint32_t buffer[16]; + + if (!(reinterpret_cast(block) & 3)) { + x = reinterpret_cast(block); + } else { + memcpy(buffer, block, sizeof(buffer)); + x = buffer; + } + + MD5_STEP(MD5_F, a, b, c, d, 0, 7, 0xD76AA478); + MD5_STEP(MD5_F, d, a, b, c, 1, 12, 0xE8C7B756); + MD5_STEP(MD5_F, c, d, a, b, 2, 17, 0x242070DB); + MD5_STEP(MD5_F, b, c, d, a, 3, 22, 0xC1BDCEEE); + MD5_STEP(MD5_F, a, b, c, d, 4, 7, 0xF57C0FAF); + MD5_STEP(MD5_F, d, a, b, c, 5, 12, 0x4787C62A); + MD5_STEP(MD5_F, c, d, a, b, 6, 17, 0xA8304613); + MD5_STEP(MD5_F, b, c, d, a, 7, 22, 0xFD469501); + MD5_STEP(MD5_F, a, b, c, d, 8, 7, 0x698098D8); + MD5_STEP(MD5_F, d, a, b, c, 9, 12, 0x8B44F7AF); + MD5_STEP(MD5_F, c, d, a, b, 10, 17, 0xFFFF5BB1); + MD5_STEP(MD5_F, b, c, d, a, 11, 22, 0x895CD7BE); + MD5_STEP(MD5_F, a, b, c, d, 12, 7, 0x6B901122); + MD5_STEP(MD5_F, d, a, b, c, 13, 12, 0xFD987193); + MD5_STEP(MD5_F, c, d, a, b, 14, 17, 0xA679438E); + MD5_STEP(MD5_F, b, c, d, a, 15, 22, 0x49B40821); + + MD5_STEP(MD5_G, a, b, c, d, 1, 5, 0xF61E2562); + MD5_STEP(MD5_G, d, a, b, c, 6, 9, 0xC040B340); + MD5_STEP(MD5_G, c, d, a, b, 11, 14, 0x265E5A51); + MD5_STEP(MD5_G, b, c, d, a, 0, 20, 0xE9B6C7AA); + MD5_STEP(MD5_G, a, b, c, d, 5, 5, 0xD62F105D); + MD5_STEP(MD5_G, d, a, b, c, 10, 9, 0x02441453); + MD5_STEP(MD5_G, c, d, a, b, 15, 14, 0xD8A1E681); + MD5_STEP(MD5_G, b, c, d, a, 4, 20, 0xE7D3FBC8); + MD5_STEP(MD5_G, a, b, c, d, 9, 5, 0x21E1CDE6); + MD5_STEP(MD5_G, d, a, b, c, 14, 9, 0xC33707D6); + MD5_STEP(MD5_G, c, d, a, b, 3, 14, 0xF4D50D87); + MD5_STEP(MD5_G, b, c, d, a, 8, 20, 0x455A14ED); + MD5_STEP(MD5_G, a, b, c, d, 13, 5, 0xA9E3E905); + MD5_STEP(MD5_G, d, a, b, c, 2, 9, 0xFCEFA3F8); + MD5_STEP(MD5_G, c, d, a, b, 7, 14, 0x676F02D9); + MD5_STEP(MD5_G, b, c, d, a, 12, 20, 0x8D2A4C8A); + + MD5_STEP(MD5_H, a, b, c, d, 5, 4, 0xFFFA3942); + MD5_STEP(MD5_H, d, a, b, c, 8, 11, 0x8771F681); + MD5_STEP(MD5_H, c, d, a, b, 11, 16, 0x6D9D6122); + MD5_STEP(MD5_H, b, c, d, a, 14, 23, 0xFDE5380C); + MD5_STEP(MD5_H, a, b, c, d, 1, 4, 0xA4BEEA44); + MD5_STEP(MD5_H, d, a, b, c, 4, 11, 0x4BDECFA9); + MD5_STEP(MD5_H, c, d, a, b, 7, 16, 0xF6BB4B60); + MD5_STEP(MD5_H, b, c, d, a, 10, 23, 0xBEBFBC70); + MD5_STEP(MD5_H, a, b, c, d, 13, 4, 0x289B7EC6); + MD5_STEP(MD5_H, d, a, b, c, 0, 11, 0xEAA127FA); + MD5_STEP(MD5_H, c, d, a, b, 3, 16, 0xD4EF3085); + MD5_STEP(MD5_H, b, c, d, a, 6, 23, 0x04881D05); + MD5_STEP(MD5_H, a, b, c, d, 9, 4, 0xD9D4D039); + MD5_STEP(MD5_H, d, a, b, c, 12, 11, 0xE6DB99E5); + MD5_STEP(MD5_H, c, d, a, b, 15, 16, 0x1FA27CF8); + MD5_STEP(MD5_H, b, c, d, a, 2, 23, 0xC4AC5665); + + MD5_STEP(MD5_I, a, b, c, d, 0, 6, 0xF4292244); + MD5_STEP(MD5_I, d, a, b, c, 7, 10, 0x432AFF97); + MD5_STEP(MD5_I, c, d, a, b, 14, 15, 0xAB9423A7); + MD5_STEP(MD5_I, b, c, d, a, 5, 21, 0xFC93A039); + MD5_STEP(MD5_I, a, b, c, d, 12, 6, 0x655B59C3); + MD5_STEP(MD5_I, d, a, b, c, 3, 10, 0x8F0CCC92); + MD5_STEP(MD5_I, c, d, a, b, 10, 15, 0xFFEFF47D); + MD5_STEP(MD5_I, b, c, d, a, 1, 21, 0x85845DD1); + MD5_STEP(MD5_I, a, b, c, d, 8, 6, 0x6FA87E4F); + MD5_STEP(MD5_I, d, a, b, c, 15, 10, 0xFE2CE6E0); + MD5_STEP(MD5_I, c, d, a, b, 6, 15, 0xA3014314); + MD5_STEP(MD5_I, b, c, d, a, 13, 21, 0x4E0811A1); + MD5_STEP(MD5_I, a, b, c, d, 4, 6, 0xF7537E82); + MD5_STEP(MD5_I, d, a, b, c, 11, 10, 0xBD3AF235); + MD5_STEP(MD5_I, c, d, a, b, 2, 15, 0x2AD7D2BB); + MD5_STEP(MD5_I, b, c, d, a, 9, 21, 0xEB86D391); + + this->m_state[0] += a; + this->m_state[1] += b; + this->m_state[2] += c; + this->m_state[3] += d; +} + +// VERIFY: d4e30e4c manual +// OFFSET: 0x0042B150 +void Blizzard::Util::MD5::Process(const void* data, uint32_t size) { + const uint8_t* p = static_cast(data); + uint32_t left = size; + uint32_t offset = (this->m_count[0] >> 3) & 63; + uint32_t bits = size << 3; + + if (!size) { + return; + } + + this->m_count[1] += size >> 29; + this->m_count[0] += bits; + + if (this->m_count[0] < bits) { + this->m_count[1]++; + } + + if (offset) { + uint32_t copy = offset + size > 64 ? 64 - offset : size; + memcpy(this->m_buffer + offset, p, copy); + + if (offset + copy < 64) { + return; + } + + p += copy; + left -= copy; + this->Process(this->m_buffer); + } + + for (; left >= 64; p += 64, left -= 64) { + this->Process(p); + } + + if (left) { + memcpy(this->m_buffer, p, left); + } +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.cpp b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.cpp index 6bf4283..7974ffe 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.cpp +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.cpp @@ -1,7 +1,9 @@ #include #include #include +#include #include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnitContainer.h" #include "Engine/Source/BlizzardCore/Source/System/File/File.h" #include "Engine/Source/Storm/H/storm.h" @@ -407,3 +409,37 @@ bool IOAlignUnit::Close() { SMemFree(this->m_buffer, __FILE__, __LINE__, 0); return result; } + +// VERIFY: c897ffe8 manual +// OFFSET: 0x00434B90 +IOAlignUnit* IOAlignUnit::Create(IOUnitContainer* container, int64_t base, uint32_t blockSize, uint32_t blockCount, bool largeTransfers, bool exactSize, uint32_t bufferAlign, bool skipCleanOnWrite, IOUnit* next) { + if (!next) { + return nullptr; + } + + int64_t size; + + if (!next->GetFileSize(&size)) { + next->Release(); + return nullptr; + } + + if (!blockSize) { + blockSize = 1; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOAlignUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOAlignUnit(base, blockSize, blockCount, largeTransfers, exactSize, bufferAlign, skipCleanOnWrite, size, ownsMemory, next); +} + +// VERIFY: 5e8cfd60 auto +// OFFSET: 0x00434C60 +IOAlignUnit* IOAlignUnit::Create(IOUnitContainer* container, int64_t base, uint32_t blockSize, uint32_t blockCount, bool largeTransfers, bool exactSize, uint32_t bufferAlign, IOUnit* next) { + return IOAlignUnit::Create(container, base, blockSize, blockCount, largeTransfers, exactSize, bufferAlign, true, next); +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h index 76e469d..c742f43 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOAlignUnit.h @@ -40,6 +40,9 @@ class IOAlignUnit : public IOUnit { virtual ~IOAlignUnit(); virtual bool Close(); + static IOAlignUnit* Create(IOUnitContainer* container, int64_t base, uint32_t blockSize, uint32_t blockCount, bool largeTransfers, bool exactSize, uint32_t bufferAlign, bool skipCleanOnWrite, IOUnit* next); + static IOAlignUnit* Create(IOUnitContainer* container, int64_t base, uint32_t blockSize, uint32_t blockCount, bool largeTransfers, bool exactSize, uint32_t bufferAlign, IOUnit* next); + IOAlignUnit(int64_t base, uint32_t blockSize, uint32_t blockCount, bool largeTransfers, bool exactSize, uint32_t bufferAlign, bool skipCleanOnWrite, int64_t size, bool ownsMemory, IOUnit* next); void AllocateBuffer(); bool CleanCache(); diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.cpp b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.cpp new file mode 100644 index 0000000..0664ffb --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.cpp @@ -0,0 +1,263 @@ +#include +#include +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnitContainer.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/MPQGlobals.h" +#include "Engine/Source/BlizzardCore/Source/System/File/File.h" +#include "Engine/Source/BlizzardCore/Source/System/Lock/Lock.h" +#include "ThirdParty/zlib/zlib.h" + +// VERIFY: 759a2ec7 manual +// OFFSET: 0x00426820 +void IOChecksumUnit::ChecksumJob(void* param) { + Job* job = static_cast(param); + job->m_unit->Checksum(job->m_data, job->m_count); + job->m_unit->m_position += job->m_count; +} + +// INLINED +IOChecksumUnit::IOChecksumUnit(int64_t size, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next) : IOUnit(next, ownsMemory) { + this->m_position = 0; + this->m_size = size; + this->m_context = nullptr; + this->m_readToEnd = readToEnd; + + if (self) { + *self = this; + } +} + +// VERIFY: b072b424 auto +// OFFSET: 0x00429620 +uint32_t IOChecksumUnit::GetType() { + return 'crc '; +} + +// VERIFY: 875dcaaa manual +// OFFSET: 0x00457ED0 +bool IOChecksumUnit::Read(void* buffer, int64_t offset, uint32_t count, uint32_t* bytesRead) { + if (!this->Clamp(offset, &count, bytesRead) || !this->m_next->Read(buffer, offset, count, bytesRead)) { + return false; + } + + return this->Update(static_cast(buffer), offset, bytesRead ? *bytesRead : count); +} + +// VERIFY: 750c7f6b manual +// OFFSET: 0x00457F40 +bool IOChecksumUnit::Write(const void* buffer, int64_t offset, uint32_t count) { + if (!this->Clamp(offset, &count, nullptr)) { + return false; + } + + bool result; + Job job; + job.m_unit = this; + + if (this->GetRange(static_cast(buffer), offset, count, &job.m_data, &job.m_count)) { + System_MPQGlobals::Worker* worker; + System_MPQGlobals::GetWorkerPool()->Acquire(1, &worker); + + if (!worker->m_proc) { + worker->m_proc = ChecksumJob; + worker->m_param = &job; + Blizzard::Lock::EventSet(worker->m_start.m_event); + } + + result = this->m_next->Write(buffer, offset, count); + Blizzard::Lock::EventWaitForever(worker->m_done.m_event); + System_MPQGlobals::GetWorkerPool()->Release(1, &worker); + } else { + result = this->m_next->Write(buffer, offset, count); + } + + if (!result) { + return false; + } + + if (this->m_position == this->m_size) { + return this->Finish(); + } + + return true; +} + +// VERIFY: d6c9da29 manual +// OFFSET: 0x00426850 +bool IOChecksumUnit::SetFileSize(int64_t size) { + if (size <= this->m_size) { + return this->m_next->SetFileSize(size); + } + + return false; +} + +// INLINED +IOChecksumUnit::~IOChecksumUnit() { +} + +// VERIFY: 6d8f8a39 manual +// OFFSET: 0x00430E00 +bool IOChecksumUnit::Close() { + if (!this->m_readToEnd) { + return true; + } + + if (this->m_position >= this->m_size) { + return true; + } + + uint32_t count = static_cast(this->m_size - this->m_position); + uint8_t* buffer = count ? new uint8_t[count] : nullptr; + bool result = this->Read(buffer, this->m_position, count, nullptr); + + if (buffer) { + delete[] buffer; + } + + return result; +} + +void IOChecksumUnit::SetContext(void* context) { + this->m_context = context; +} + +// VERIFY: 80e5336f manual +// OFFSET: 0x00457E50 +bool IOChecksumUnit::Clamp(int64_t offset, uint32_t* count, uint32_t* bytesRead) { + if (offset + *count > this->m_size) { + if (offset >= this->m_size) { + Blizzard::File::SetLastError(10); + Blizzard::File::AddToLastErrorStack(10, "IOChecksumUnit::Clamp - Range Error", 1); + return false; + } + + if (!bytesRead) { + return false; + } + + *count = static_cast(this->m_size - offset); + } + + return true; +} + +// VERIFY: 7f568e1b manual +// OFFSET: 0x00426740 +bool IOChecksumUnit::GetRange(const uint8_t* data, int64_t offset, uint32_t count, const uint8_t** rangeData, uint32_t* rangeCount) { + if (offset > this->m_position) { + return false; + } + + if (offset + count <= this->m_position) { + return false; + } + + if (offset < this->m_position) { + uint32_t skip = static_cast(this->m_position - offset); + data += skip; + count -= skip; + } + + *rangeData = data; + *rangeCount = count; + + return true; +} + +// VERIFY: 1fd2bcd5 manual +// OFFSET: 0x004267B0 +bool IOChecksumUnit::Update(const uint8_t* data, int64_t offset, uint32_t count) { + if (this->GetRange(data, offset, count, &data, &count)) { + this->Checksum(data, count); + this->m_position += count; + + if (this->m_position == this->m_size) { + return this->Finish(); + } + } + + return true; +} + +// VERIFY: d67d5826 manual +// OFFSET: 0x004350C0 +IOUnit* IOCrcUnit::Create(IOUnitContainer* container, int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, IOUnit* next) { + if (!next) { + return nullptr; + } + + if (!callback) { + return next; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOCrcUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOCrcUnit(size, callback, readToEnd, self, ownsMemory, next); +} + +// VERIFY: 543472ca manual +// OFFSET: 0x00430C90 +IOCrcUnit::IOCrcUnit(int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next) : IOChecksumUnit(size, readToEnd, self, ownsMemory, next) { + this->m_callback = callback; + this->m_crc = 0; +} + +// VERIFY: 81fee3cc auto +// OFFSET: 0x00430CF0 +void IOCrcUnit::Checksum(const uint8_t* data, uint32_t count) { + this->m_crc = crc32(this->m_crc, data, count); +} + +// VERIFY: 83438dc2 auto +// OFFSET: 0x00430D20 +bool IOCrcUnit::Finish() { + return this->m_callback(this->m_context, this->m_crc); +} + +// VERIFY: 16498305 manual +// OFFSET: 0x00435180 +IOUnit* IOMd5Unit::Create(IOUnitContainer* container, int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, IOUnit* next) { + if (!next) { + return nullptr; + } + + if (!callback) { + return next; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IOMd5Unit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IOMd5Unit(size, callback, readToEnd, self, ownsMemory, next); +} + +// VERIFY: 71e51316 manual +// OFFSET: 0x00430D40 +IOMd5Unit::IOMd5Unit(int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next) : IOChecksumUnit(size, readToEnd, self, ownsMemory, next) { + this->m_callback = callback; +} + +// VERIFY: 0e82453d auto +// OFFSET: 0x00430DC0 +void IOMd5Unit::Checksum(const uint8_t* data, uint32_t count) { + this->m_md5.Process(data, count); +} + +// VERIFY: 7776d404 auto +// OFFSET: 0x00430DD0 +bool IOMd5Unit::Finish() { + uint8_t digest[16]; + this->m_md5.Finish(digest); + + return this->m_callback(this->m_context, digest); +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.h b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.h new file mode 100644 index 0000000..0c28143 --- /dev/null +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOChecksumUnit.h @@ -0,0 +1,70 @@ +#ifndef ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_IOUNIT_IOCHECKSUMUNIT_H +#define ENGINE_SOURCE_BLIZZARDCORE_SOURCE_PACKAGES_IOUNIT_IOCHECKSUMUNIT_H + +#include +#include "Engine/Source/BlizzardCore/Include/Blizzard/Util/MD5.h" +#include "Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h" + +class IOChecksumUnit : public IOUnit { + public: + struct Job { + /* 0000 */ IOChecksumUnit* m_unit; + /* 0004 */ const uint8_t* m_data; + /* 0008 */ uint32_t m_count; + }; + + static void ChecksumJob(void* param); + + /* 0010 */ int64_t m_position; + /* 0018 */ int64_t m_size; + /* 0020 */ void* m_context; + /* 0024 */ bool m_readToEnd; + /* 0025 */ uint8_t unk_0025[3]; + + virtual uint32_t GetType(); + virtual bool Read(void* buffer, int64_t offset, uint32_t count, uint32_t* bytesRead); + virtual bool Write(const void* buffer, int64_t offset, uint32_t count); + virtual bool SetFileSize(int64_t size); + virtual ~IOChecksumUnit(); + virtual bool Close(); + virtual void SetContext(void* context); + virtual void Checksum(const uint8_t* data, uint32_t count) = 0; + virtual bool Finish() = 0; + + IOChecksumUnit(int64_t size, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next); + bool Clamp(int64_t offset, uint32_t* count, uint32_t* bytesRead); + bool GetRange(const uint8_t* data, int64_t offset, uint32_t count, const uint8_t** rangeData, uint32_t* rangeCount); + bool Update(const uint8_t* data, int64_t offset, uint32_t count); +}; + +class IOCrcUnit : public IOChecksumUnit { + public: + typedef bool (*Callback)(void* context, uint32_t crc); + + static IOUnit* Create(IOUnitContainer* container, int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, IOUnit* next); + + /* 0028 */ Callback m_callback; + /* 002C */ uint32_t m_crc; + + virtual void Checksum(const uint8_t* data, uint32_t count); + virtual bool Finish(); + + IOCrcUnit(int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next); +}; + +class IOMd5Unit : public IOChecksumUnit { + public: + typedef bool (*Callback)(void* context, const uint8_t* md5); + + static IOUnit* Create(IOUnitContainer* container, int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, IOUnit* next); + + /* 0028 */ Callback m_callback; + /* 002C */ Blizzard::Util::MD5 m_md5; + + virtual void Checksum(const uint8_t* data, uint32_t count); + virtual bool Finish(); + + IOMd5Unit(int64_t size, Callback callback, bool readToEnd, IOChecksumUnit** self, bool ownsMemory, IOUnit* next); +}; + +#endif diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.cpp b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.cpp index 6291571..1dbdd76 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.cpp +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.cpp @@ -250,3 +250,37 @@ void* IOLockUnit::Win32Handle() { Blizzard::Lock::ScopedMutex lock(*this->m_mutex); return this->m_next->Win32Handle(); } + +// VERIFY: 4926450f manual +// OFFSET: 0x00435050 +IORangeUnit* IORangeUnit::Create(IOUnitContainer* container, int64_t start, int64_t end, IOUnit* next) { + if (!next) { + return nullptr; + } + + if (start > end) { + next->Release(); + return nullptr; + } + + bool ownsMemory; + void* memory = IOUnitContainer::Alloc(container, sizeof(IORangeUnit), &ownsMemory); + + if (!memory) { + return nullptr; + } + + return new (memory) IORangeUnit(start, end, ownsMemory, next); +} + +// VERIFY: dfd9ee20 manual +// OFFSET: 0x00429A20 +bool IOLockUnit::ReplaceNext(IOUnit* lock, IOUnit* next) { + if (!lock || !next || lock->GetType() != 'lock' || next->m_next != lock->m_next) { + return false; + } + + lock->m_next = next; + + return true; +} diff --git a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h index c3ad9e4..5086bca 100644 --- a/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h +++ b/Engine/Source/BlizzardCore/Source/Packages/iounit/IOUnit.h @@ -29,6 +29,8 @@ class IOUnit { class IORangeUnit : public IOUnit { public: + static IORangeUnit* Create(IOUnitContainer* container, int64_t start, int64_t end, IOUnit* next); + /* 0010 */ int64_t m_start; /* 0018 */ int64_t m_end; @@ -46,6 +48,7 @@ class IOLockUnit : public IOUnit { public: static IOLockUnit* Create(IOUnitContainer* container, uint32_t flags, IOUnit* next); static IOLockUnit* Create(IOUnitContainer* container, IOUnit* next); + static bool ReplaceNext(IOUnit* lock, IOUnit* next); /* 0010 */ Blizzard::Lock::Mutex* m_mutex; /* 0014 */ Blizzard::Lock::Mutex m_ownMutex; diff --git a/Engine/Source/BlizzardCore/Source/System/Memory/TempBuffer.h b/Engine/Source/BlizzardCore/Source/System/Memory/TempBuffer.h index c0561ed..eecc5a4 100644 --- a/Engine/Source/BlizzardCore/Source/System/Memory/TempBuffer.h +++ b/Engine/Source/BlizzardCore/Source/System/Memory/TempBuffer.h @@ -38,7 +38,7 @@ Blizzard::Memory::TempBuffer::~TempBuffer() { } // VERIFY: 019cc2c0 manual -// OFFSET: 0x0042EC20 +// OFFSET: [0x0042EC20, 0x0042EAF0] template void Blizzard::Memory::TempBuffer::Resize(uint32_t count) { if (count > N) {