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

Function SaveExtTable

3rdParty/StormLib/src/SBaseFileTable.cpp:1087–1171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1085}
1086
1087static int SaveExtTable(
1088 TMPQArchive * ha,
1089 TMPQExtHeader * pExtTable,
1090 ULONGLONG ByteOffset,
1091 DWORD dwTableSize,
1092 unsigned char * md5,
1093 DWORD dwKey,
1094 bool bCompress,
1095 LPDWORD pcbTotalSize)
1096{
1097 ULONGLONG FileOffset;
1098 TMPQExtHeader * pCompressed = NULL;
1099 DWORD cbTotalSize = 0;
1100 int nError = ERROR_SUCCESS;
1101
1102 // Do we have to compress the table?
1103 if(bCompress)
1104 {
1105 int cbOutBuffer = (int)dwTableSize;
1106 int cbInBuffer = (int)dwTableSize;
1107
1108 // Allocate extra space for compressed table
1109 pCompressed = (TMPQExtHeader *)STORM_ALLOC(BYTE, dwTableSize);
1110 if(pCompressed == NULL)
1111 return ERROR_NOT_ENOUGH_MEMORY;
1112
1113 // Compress the table
1114 pCompressed->dwSignature = pExtTable->dwSignature;
1115 pCompressed->dwVersion = pExtTable->dwVersion;
1116 pCompressed->dwDataSize = pExtTable->dwDataSize;
1117 SCompCompress((pCompressed + 1), &cbOutBuffer, (pExtTable + 1), cbInBuffer, MPQ_COMPRESSION_ZLIB, 0, 0);
1118
1119 // If the compression failed, revert it. Otherwise, swap the tables
1120 if(cbOutBuffer >= cbInBuffer)
1121 {
1122 STORM_FREE(pCompressed);
1123 pCompressed = NULL;
1124 }
1125 else
1126 {
1127 pExtTable = pCompressed;
1128 }
1129 }
1130
1131 // Encrypt the table
1132 if(dwKey != 0)
1133 {
1134 BSWAP_ARRAY32_UNSIGNED(pExtTable + 1, pExtTable->dwDataSize);
1135 EncryptMpqBlock(pExtTable + 1, (DWORD)(dwTableSize - sizeof(TMPQExtHeader)), dwKey);
1136 BSWAP_ARRAY32_UNSIGNED(pExtTable + 1, pExtTable->dwDataSize);
1137 }
1138
1139 // Calculate the MD5 of the table after
1140 if(md5 != NULL)
1141 {
1142 CalculateDataBlockHash(pExtTable, dwTableSize, md5);
1143 }
1144

Callers 1

SaveMPQTablesFunction · 0.85

Calls 6

SCompCompressFunction · 0.85
EncryptMpqBlockFunction · 0.85
CalculateDataBlockHashFunction · 0.85
FileStream_WriteFunction · 0.85
GetLastErrorFunction · 0.85
WriteMemDataMD5Function · 0.85

Tested by

no test coverage detected