MCPcopy Create free account
hub / github.com/Kenix3/libultraship / WriteDataToMpqFile

Function WriteDataToMpqFile

extern/StormLib/src/SFileAddFile.cpp:124–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

122// MPQ write data functions
123
124static DWORD WriteDataToMpqFile(
125 TMPQArchive * ha,
126 TMPQFile * hf,
127 LPBYTE pbFileData,
128 DWORD dwDataSize,
129 DWORD dwCompression)
130{
131 TFileEntry * pFileEntry = hf->pFileEntry;
132 ULONGLONG ByteOffset;
133 LPBYTE pbCompressed = NULL; // Compressed (target) data
134 LPBYTE pbToWrite = hf->pbFileSector; // Data to write to the file
135 DWORD dwErrCode = ERROR_SUCCESS;
136 int nCompressionLevel; // ADPCM compression level (only used for wave files)
137
138 // Make sure that the caller won't overrun the previously initiated file size
139 assert(hf->dwFilePos + dwDataSize <= pFileEntry->dwFileSize);
140 assert(hf->dwSectorCount != 0);
141 assert(hf->pbFileSector != NULL);
142 if((hf->dwFilePos + dwDataSize) > pFileEntry->dwFileSize)
143 return ERROR_DISK_FULL;
144
145 // Now write all data to the file sector buffer
146 if(dwErrCode == ERROR_SUCCESS)
147 {
148 DWORD dwBytesInSector = hf->dwFilePos % hf->dwSectorSize;
149 DWORD dwSectorIndex = hf->dwFilePos / hf->dwSectorSize;
150 DWORD dwBytesToCopy;
151
152 // Process all data.
153 while(dwDataSize != 0)
154 {
155 dwBytesToCopy = dwDataSize;
156
157 // Check for sector overflow
158 if(dwBytesToCopy > (hf->dwSectorSize - dwBytesInSector))
159 dwBytesToCopy = (hf->dwSectorSize - dwBytesInSector);
160
161 // Copy the data to the file sector
162 memcpy(hf->pbFileSector + dwBytesInSector, pbFileData, dwBytesToCopy);
163 dwBytesInSector += dwBytesToCopy;
164 pbFileData += dwBytesToCopy;
165 dwDataSize -= dwBytesToCopy;
166
167 // Update the file position
168 hf->dwFilePos += dwBytesToCopy;
169
170 // If the current sector is full, or if the file is already full,
171 // then write the data to the MPQ
172 if(dwBytesInSector >= hf->dwSectorSize || hf->dwFilePos >= pFileEntry->dwFileSize)
173 {
174 // Set the position in the file
175 ByteOffset = hf->RawFilePos + pFileEntry->dwCmpSize;
176
177 // Update MD5 and CRC32 of the file
178 if(hf->hctx != NULL)
179 md5_process((hash_state *)hf->hctx, hf->pbFileSector, dwBytesInSector);
180 hf->dwCrc32 = crc32(hf->dwCrc32, hf->pbFileSector, dwBytesInSector);
181

Callers 1

SFileAddFile_WriteFunction · 0.85

Calls 7

crc32Function · 0.85
SCompImplodeFunction · 0.85
SCompCompressFunction · 0.85
adler32Function · 0.85
EncryptMpqBlockFunction · 0.85
FileStream_WriteFunction · 0.85
GetLastErrorFunction · 0.85

Tested by

no test coverage detected