| 1382 | |
| 1383 | |
| 1384 | int WriteSectorChecksums(TMPQFile * hf) |
| 1385 | { |
| 1386 | TMPQArchive * ha = hf->ha; |
| 1387 | ULONGLONG RawFilePos; |
| 1388 | TFileEntry * pFileEntry = hf->pFileEntry; |
| 1389 | LPBYTE pbCompressed; |
| 1390 | DWORD dwCompressedSize = 0; |
| 1391 | DWORD dwCrcSize; |
| 1392 | int nOutSize; |
| 1393 | int nError = ERROR_SUCCESS; |
| 1394 | |
| 1395 | // The caller must make sure that this function is only called |
| 1396 | // when the following is true. |
| 1397 | assert(hf->pFileEntry->dwFlags & MPQ_FILE_SECTOR_CRC); |
| 1398 | assert(hf->SectorOffsets != NULL); |
| 1399 | assert(hf->SectorChksums != NULL); |
| 1400 | |
| 1401 | // If the MPQ has MD5 of each raw data chunk, |
| 1402 | // we leave sector offsets empty |
| 1403 | if(ha->pHeader->dwRawChunkSize != 0) |
| 1404 | { |
| 1405 | hf->SectorOffsets[hf->dwSectorCount + 1] = hf->SectorOffsets[hf->dwSectorCount]; |
| 1406 | return ERROR_SUCCESS; |
| 1407 | } |
| 1408 | |
| 1409 | // Calculate size of the checksum array |
| 1410 | dwCrcSize = hf->dwSectorCount * sizeof(DWORD); |
| 1411 | |
| 1412 | // Allocate buffer for compressed sector CRCs. |
| 1413 | pbCompressed = STORM_ALLOC(BYTE, dwCrcSize); |
| 1414 | if(pbCompressed == NULL) |
| 1415 | return ERROR_NOT_ENOUGH_MEMORY; |
| 1416 | |
| 1417 | // Perform the compression |
| 1418 | BSWAP_ARRAY32_UNSIGNED(hf->SectorChksums, dwCrcSize); |
| 1419 | |
| 1420 | nOutSize = (int)dwCrcSize; |
| 1421 | SCompCompress(pbCompressed, &nOutSize, hf->SectorChksums, (int)dwCrcSize, MPQ_COMPRESSION_ZLIB, 0, 0); |
| 1422 | dwCompressedSize = (DWORD)nOutSize; |
| 1423 | |
| 1424 | // Write the sector CRCs to the archive |
| 1425 | RawFilePos = hf->RawFilePos + hf->SectorOffsets[hf->dwSectorCount]; |
| 1426 | if(hf->pPatchInfo != NULL) |
| 1427 | RawFilePos += hf->pPatchInfo->dwLength; |
| 1428 | if(!FileStream_Write(ha->pStream, &RawFilePos, pbCompressed, dwCompressedSize)) |
| 1429 | nError = GetLastError(); |
| 1430 | |
| 1431 | // Not necessary, as the sector checksums |
| 1432 | // are going to be freed when this is done. |
| 1433 | // BSWAP_ARRAY32_UNSIGNED(hf->SectorChksums, dwCrcSize); |
| 1434 | |
| 1435 | // Store the sector CRCs |
| 1436 | hf->SectorOffsets[hf->dwSectorCount + 1] = hf->SectorOffsets[hf->dwSectorCount] + dwCompressedSize; |
| 1437 | pFileEntry->dwCmpSize += dwCompressedSize; |
| 1438 | STORM_FREE(pbCompressed); |
| 1439 | return nError; |
| 1440 | } |
| 1441 |
nothing calls this directly
no test coverage detected