| 122 | // MPQ write data functions |
| 123 | |
| 124 | static 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 |
no test coverage detected