MCPcopy Create free account
hub / github.com/diasurgical/devilution / FindFreeMpqSpace

Function FindFreeMpqSpace

3rdParty/StormLib/src/SBaseCommon.cpp:777–813  ·  view source on GitHub ↗

Finds a free space in the MPQ where to store next data The free space begins beyond the file that is stored at the fuhrtest position in the MPQ. (listfile), (attributes) and (signature) are ignored, unless the MPQ is being flushed.

Source from the content-addressed store, hash-verified

775// position in the MPQ. (listfile), (attributes) and (signature) are ignored,
776// unless the MPQ is being flushed.
777ULONGLONG FindFreeMpqSpace(TMPQArchive * ha)
778{
779 TMPQHeader * pHeader = ha->pHeader;
780 TFileEntry * pFileTableEnd = ha->pFileTable + ha->dwFileTableSize;
781 TFileEntry * pFileEntry;
782 ULONGLONG FreeSpacePos = ha->pHeader->dwHeaderSize;
783 DWORD dwChunkCount;
784
785 // Parse the entire block table
786 for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
787 {
788 // Only take existing files with nonzero size
789 if((pFileEntry->dwFlags & MPQ_FILE_EXISTS) && (pFileEntry->dwCmpSize != 0))
790 {
791 // If we are not saving MPQ tables, ignore internal MPQ files
792 if((ha->dwFlags & MPQ_FLAG_SAVING_TABLES) == 0 && IsInternalMpqFileName(pFileEntry->szFileName))
793 continue;
794
795 // If the end of the file is bigger than current MPQ table pos, update it
796 if((pFileEntry->ByteOffset + pFileEntry->dwCmpSize) > FreeSpacePos)
797 {
798 // Get the end of the file data
799 FreeSpacePos = pFileEntry->ByteOffset + pFileEntry->dwCmpSize;
800
801 // Add the MD5 chunks, if present
802 if(pHeader->dwRawChunkSize != 0 && pFileEntry->dwCmpSize != 0)
803 {
804 dwChunkCount = ((pFileEntry->dwCmpSize - 1) / pHeader->dwRawChunkSize) + 1;
805 FreeSpacePos += dwChunkCount * MD5_DIGEST_SIZE;
806 }
807 }
808 }
809 }
810
811 // Give the free space position to the caller
812 return FreeSpacePos;
813}
814
815//-----------------------------------------------------------------------------
816// Common functions - MPQ File

Callers 2

SaveMPQTablesFunction · 0.85
CreateWritableHandleFunction · 0.85

Calls 1

IsInternalMpqFileNameFunction · 0.85

Tested by

no test coverage detected