aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLadislav Zezula <ladislav.zezula@avast.com>2021-12-26 11:36:55 +0100
committerLadislav Zezula <ladislav.zezula@avast.com>2021-12-26 11:36:55 +0100
commita5ef7a850aac844abf2e3859e26617ed596d4624 (patch)
tree92c30943d8df469bc2ee372c1c022c6dfa9fb95e
parentfb9b3d4ba173d3ff4c70b0e404f6b4ebee864ce9 (diff)
Fixed assert on some platforms
-rw-r--r--src/SBaseCommon.cpp2
-rw-r--r--src/SFileAddFile.cpp21
-rw-r--r--src/StormLib.h4
-rw-r--r--test/stormlib-test.txt2
4 files changed, 15 insertions, 14 deletions
diff --git a/src/SBaseCommon.cpp b/src/SBaseCommon.cpp
index 2defdab..5b81bad 100644
--- a/src/SBaseCommon.cpp
+++ b/src/SBaseCommon.cpp
@@ -1681,6 +1681,8 @@ void FreeFileHandle(TMPQFile *& hf)
STORM_FREE(hf->SectorOffsets);
if(hf->SectorChksums != NULL)
STORM_FREE(hf->SectorChksums);
+ if(hf->hctx != NULL)
+ STORM_FREE(hf->hctx);
if(hf->pbFileSector != NULL)
STORM_FREE(hf->pbFileSector);
if(hf->pStream != NULL)
diff --git a/src/SFileAddFile.cpp b/src/SFileAddFile.cpp
index 9c7331b..f71434e 100644
--- a/src/SFileAddFile.cpp
+++ b/src/SFileAddFile.cpp
@@ -80,7 +80,7 @@ static bool IsWaveFile_16BitsPerAdpcmSample(
return false;
}
-static int FillWritableHandle(
+static DWORD FillWritableHandle(
TMPQArchive * ha,
TMPQFile * hf,
ULONGLONG FileTime,
@@ -99,14 +99,13 @@ static int FillWritableHandle(
pFileEntry->dwCmpSize = 0;
pFileEntry->dwFlags = dwFlags | MPQ_FILE_EXISTS;
- // Initialize the file time, CRC32 and MD5
- assert(sizeof(hf->hctx) >= sizeof(hash_state));
- memset(pFileEntry->md5, 0, MD5_DIGEST_SIZE);
- md5_init((hash_state *)hf->hctx);
- pFileEntry->dwCrc32 = crc32(0, Z_NULL, 0);
+ // Initialize hashing of the file
+ if((hf->hctx = STORM_ALLOC(hash_state, 1)) != NULL)
+ md5_init((hash_state *)hf->hctx);
- // If the caller gave us a file time, use it.
+ // Fill-in file time and CRC
pFileEntry->FileTime = FileTime;
+ pFileEntry->dwCrc32 = crc32(0, Z_NULL, 0);
// Mark the archive as modified
ha->dwFlags |= MPQ_FLAG_CHANGED;
@@ -175,8 +174,9 @@ static DWORD WriteDataToMpqFile(
// Set the position in the file
ByteOffset = hf->RawFilePos + pFileEntry->dwCmpSize;
- // Update CRC32 and MD5 of the file
- md5_process((hash_state *)hf->hctx, hf->pbFileSector, dwBytesInSector);
+ // Update MD5 and CRC32 of the file
+ if(hf->hctx != NULL)
+ md5_process((hash_state *)hf->hctx, hf->pbFileSector, dwBytesInSector);
hf->dwCrc32 = crc32(hf->dwCrc32, hf->pbFileSector, dwBytesInSector);
// Compress the file sector, if needed
@@ -662,7 +662,8 @@ DWORD SFileAddFile_Write(TMPQFile * hf, const void * pvData, DWORD dwSize, DWORD
pFileEntry->dwCrc32 = hf->dwCrc32;
// Finish calculating MD5
- md5_done((hash_state *)hf->hctx, pFileEntry->md5);
+ if(hf->hctx != NULL)
+ md5_done((hash_state *)hf->hctx, pFileEntry->md5);
// If we also have sector checksums, write them to the file
if(hf->SectorChksums != NULL)
diff --git a/src/StormLib.h b/src/StormLib.h
index 1335f0e..4854704 100644
--- a/src/StormLib.h
+++ b/src/StormLib.h
@@ -171,8 +171,6 @@ extern "C" {
#define HET_ENTRY_DELETED 0x80 // NameHash1 value for a deleted entry
#define HET_ENTRY_FREE 0x00 // NameHash1 value for free entry
-#define HASH_STATE_SIZE 0x60 // Size of LibTomCrypt's hash_state structure
-
// Values for SFileOpenArchive
#define SFILE_OPEN_HARD_DISK_FILE 2 // Open the archive on HDD
#define SFILE_OPEN_CDROM_FILE 3 // Open the archive only if it is on CDROM
@@ -888,7 +886,7 @@ typedef struct _TMPQFile
DWORD dwSectorOffs; // File position of currently loaded file sector
DWORD dwSectorSize; // Size of the file sector. For single unit files, this is equal to the file size
- unsigned char hctx[HASH_STATE_SIZE]; // Hash state for MD5. Used when saving file to MPQ
+ void * hctx; // Hash state for MD5. Used when saving file to MPQ
DWORD dwCrc32; // CRC32 value, used when saving file to MPQ
DWORD dwAddFileError; // Result of the "Add File" operations
diff --git a/test/stormlib-test.txt b/test/stormlib-test.txt
index a8441d2..34917cc 100644
--- a/test/stormlib-test.txt
+++ b/test/stormlib-test.txt
@@ -1,4 +1,4 @@
-Microsoft Windows [Version 10.0.19043.1415]
+Microsoft Windows [Version 10.0.19044.1415]
(c) Microsoft Corporation. All rights reserved.
E:\Ladik\Appdir\StormLib>StormLib_test.exe