| 1020 | } |
| 1021 | |
| 1022 | static int SaveMpqTable( |
| 1023 | TMPQArchive * ha, |
| 1024 | void * pMpqTable, |
| 1025 | ULONGLONG ByteOffset, |
| 1026 | size_t Size, |
| 1027 | unsigned char * md5, |
| 1028 | DWORD dwKey, |
| 1029 | bool bCompress) |
| 1030 | { |
| 1031 | ULONGLONG FileOffset; |
| 1032 | void * pCompressed = NULL; |
| 1033 | int nError = ERROR_SUCCESS; |
| 1034 | |
| 1035 | // Do we have to compress the table? |
| 1036 | if(bCompress) |
| 1037 | { |
| 1038 | int cbOutBuffer = (int)Size; |
| 1039 | int cbInBuffer = (int)Size; |
| 1040 | |
| 1041 | // Allocate extra space for compressed table |
| 1042 | pCompressed = STORM_ALLOC(BYTE, Size); |
| 1043 | if(pCompressed == NULL) |
| 1044 | return ERROR_NOT_ENOUGH_MEMORY; |
| 1045 | |
| 1046 | // Compress the table |
| 1047 | SCompCompress(pCompressed, &cbOutBuffer, pMpqTable, cbInBuffer, MPQ_COMPRESSION_ZLIB, 0, 0); |
| 1048 | |
| 1049 | // If the compression failed, revert it. Otherwise, swap the tables |
| 1050 | if(cbOutBuffer >= cbInBuffer) |
| 1051 | { |
| 1052 | STORM_FREE(pCompressed); |
| 1053 | pCompressed = NULL; |
| 1054 | } |
| 1055 | else |
| 1056 | { |
| 1057 | pMpqTable = pCompressed; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // Encrypt the table |
| 1062 | if(dwKey != 0) |
| 1063 | { |
| 1064 | BSWAP_ARRAY32_UNSIGNED(pMpqTable, Size); |
| 1065 | EncryptMpqBlock(pMpqTable, (DWORD)Size, dwKey); |
| 1066 | BSWAP_ARRAY32_UNSIGNED(pMpqTable, Size); |
| 1067 | } |
| 1068 | |
| 1069 | // Calculate the MD5 |
| 1070 | if(md5 != NULL) |
| 1071 | { |
| 1072 | CalculateDataBlockHash(pMpqTable, (DWORD)Size, md5); |
| 1073 | } |
| 1074 | |
| 1075 | // Save the table to the MPQ |
| 1076 | BSWAP_ARRAY32_UNSIGNED(pMpqTable, Size); |
| 1077 | FileOffset = ha->MpqPos + ByteOffset; |
| 1078 | if(!FileStream_Write(ha->pStream, &FileOffset, pMpqTable, (DWORD)Size)) |
| 1079 | nError = GetLastError(); |
no test coverage detected