Skip to content

Commit

Permalink
+ Fixed bug in opening SQP archives
Browse files Browse the repository at this point in the history
+ More efficient version of GetNearestPowerOfTwo
  • Loading branch information
Ladislav Zezula committed Jul 14, 2016
1 parent 7597fe1 commit f311254
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
28 changes: 14 additions & 14 deletions src/SBaseCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,20 @@ DWORD HashStringLower(const char * szFileName, DWORD dwHashType)

// Returns the nearest higher power of two.
// If the value is already a power of two, returns the same value
//static DWORD GetNearestPowerOfTwo(DWORD dwValue)
//{
// dwValue --;
//
// dwValue |= dwValue >> 1;
// dwValue |= dwValue >> 2;
// dwValue |= dwValue >> 4;
// dwValue |= dwValue >> 8;
// dwValue |= dwValue >> 16;
//
// return dwValue + 1;
//}
DWORD GetNearestPowerOfTwo(DWORD dwFileCount)
{
dwFileCount --;

DWORD GetHashTableSizeForFileCount(DWORD dwFileCount)
dwFileCount |= dwFileCount >> 1;
dwFileCount |= dwFileCount >> 2;
dwFileCount |= dwFileCount >> 4;
dwFileCount |= dwFileCount >> 8;
dwFileCount |= dwFileCount >> 16;

return dwFileCount + 1;
}
/*
DWORD GetNearestPowerOfTwo(DWORD dwFileCount)
{
DWORD dwPowerOfTwo = HASH_TABLE_SIZE_MIN;
Expand All @@ -284,7 +284,7 @@ DWORD GetHashTableSizeForFileCount(DWORD dwFileCount)
dwPowerOfTwo <<= 1;
return dwPowerOfTwo;
}

*/
//-----------------------------------------------------------------------------
// Calculates a Jenkin's Encrypting and decrypting MPQ file data

Expand Down
2 changes: 1 addition & 1 deletion src/SBaseFileTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ static TMPQHash * DefragmentHashTable(

// Calculate how many entries in the hash table we really need
dwFirstFreeEntry = (DWORD)(pTarget - pHashTable);
dwNewTableSize = GetHashTableSizeForFileCount(dwFirstFreeEntry);
dwNewTableSize = GetNearestPowerOfTwo(dwFirstFreeEntry);

// Fill the rest with entries that look like deleted
pHashTableEnd = pHashTable + dwNewTableSize;
Expand Down
4 changes: 2 additions & 2 deletions src/SBaseSubTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ TMPQHash * LoadSqpHashTable(TMPQArchive * ha)
if(pSqpHash->dwBlockIndex != HASH_ENTRY_FREE)
{
// Check block index against the size of the block table
if(pHeader->dwBlockTableSize <= MPQ_BLOCK_INDEX(pSqpHash) && MPQ_BLOCK_INDEX(pSqpHash) < HASH_ENTRY_DELETED)
if(pHeader->dwBlockTableSize <= MPQ_BLOCK_INDEX(pSqpHash) && pSqpHash->dwBlockIndex < HASH_ENTRY_DELETED)
nError = ERROR_FILE_CORRUPT;

// We do not support nonzero locale and platform ID
Expand Down Expand Up @@ -523,7 +523,7 @@ TMPQHash * LoadMpkHashTable(TMPQArchive * ha)
if(pMpkHash != NULL)
{
// Calculate the hash table size as if it was real MPQ hash table
pHeader->dwHashTableSize = GetHashTableSizeForFileCount(pHeader->dwHashTableSize);
pHeader->dwHashTableSize = GetNearestPowerOfTwo(pHeader->dwHashTableSize);
pHeader->HashTableSize64 = pHeader->dwHashTableSize * sizeof(TMPQHash);

// Now allocate table that will serve like a true MPQ hash table,
Expand Down
2 changes: 1 addition & 1 deletion src/SFileCompactArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ bool WINAPI SFileSetMaxFileCount(HANDLE hMpq, DWORD dwMaxFileCount)
if(nError == ERROR_SUCCESS)
{
// Calculate the hash table size for the new file limit
dwNewHashTableSize = GetHashTableSizeForFileCount(dwMaxFileCount);
dwNewHashTableSize = GetNearestPowerOfTwo(dwMaxFileCount);

// Rebuild both file tables
nError = RebuildFileTable(ha, dwNewHashTableSize);
Expand Down
2 changes: 1 addition & 1 deletion src/SFileCreateArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ bool WINAPI SFileCreateArchive2(const TCHAR * szMpqName, PSFILE_CREATE_MPQ pCrea
}

// If file count is not zero, initialize the hash table size
dwHashTableSize = GetHashTableSizeForFileCount(pCreateInfo->dwMaxFileCount + dwReservedFiles);
dwHashTableSize = GetNearestPowerOfTwo(pCreateInfo->dwMaxFileCount + dwReservedFiles);

// Retrieve the file size and round it up to 0x200 bytes
FileStream_GetSize(pStream, &MpqPos);
Expand Down
2 changes: 1 addition & 1 deletion src/StormCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ DWORD HashStringLower(const char * szFileName, DWORD dwHashType);

void InitializeMpqCryptography();

DWORD GetHashTableSizeForFileCount(DWORD dwFileCount);
DWORD GetNearestPowerOfTwo(DWORD dwFileCount);

bool IsPseudoFileName(const char * szFileName, LPDWORD pdwFileIndex);
ULONGLONG HashStringJenkins(const char * szFileName);
Expand Down
8 changes: 4 additions & 4 deletions test/StormTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4397,7 +4397,7 @@ int main(int argc, char * argv[])
// Open a MPK archive from Longwu online
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPx_2013_v1_LongwuOnline.mpk");

*/
// Open a SQP archive from War of the Immortals
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPx_2013_v1_WarOfTheImmortals.sqp", "ListFile_WarOfTheImmortals.txt");
Expand Down Expand Up @@ -4467,10 +4467,10 @@ int main(int argc, char * argv[])

if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2016_v1_SP_(4)Adrenaline.w3x");
*/

if(nError == ERROR_SUCCESS)
nError = TestOpenArchive("MPQ_2016_v1_ProtectedMap_1.4.w3x");
/*

// Open the multi-file archive with wrong prefix to see how StormLib deals with it
if(nError == ERROR_SUCCESS)
nError = TestOpenArchive_WillFail("flat-file://streaming/model.MPQ.0");
Expand Down Expand Up @@ -4673,7 +4673,7 @@ int main(int argc, char * argv[])
// Test replacing a file with zero size file
if(nError == ERROR_SUCCESS)
nError = TestModifyArchive_ReplaceFile("MPQ_2014_v4_Base.StormReplay", "AddFile-replay.message.events");
*/

#ifdef _MSC_VER
_CrtDumpMemoryLeaks();
#endif // _MSC_VER
Expand Down

0 comments on commit f311254

Please sign in to comment.