| 233 | } |
| 234 | |
| 235 | static DWORD ReadMpqFileSingleUnit(TMPQFile * hf, void * pvBuffer, DWORD dwFilePos, DWORD dwToRead, LPDWORD pdwBytesRead) |
| 236 | { |
| 237 | ULONGLONG RawFilePos = hf->RawFilePos; |
| 238 | TMPQArchive * ha = hf->ha; |
| 239 | TFileEntry * pFileEntry = hf->pFileEntry; |
| 240 | LPBYTE pbCompressed = NULL; |
| 241 | LPBYTE pbRawData; |
| 242 | DWORD dwErrCode = ERROR_SUCCESS; |
| 243 | |
| 244 | // If the file buffer is not allocated yet, do it. |
| 245 | if(hf->pbFileSector == NULL) |
| 246 | { |
| 247 | dwErrCode = AllocateSectorBuffer(hf); |
| 248 | if(dwErrCode != ERROR_SUCCESS || hf->pbFileSector == NULL) |
| 249 | return dwErrCode; |
| 250 | } |
| 251 | |
| 252 | // If the file is a patch file, adjust raw data offset |
| 253 | if(hf->pPatchInfo != NULL) |
| 254 | RawFilePos += hf->pPatchInfo->dwLength; |
| 255 | pbRawData = hf->pbFileSector; |
| 256 | |
| 257 | // If the file sector is not loaded yet, do it |
| 258 | if(hf->dwSectorOffs != 0) |
| 259 | { |
| 260 | // Is the file compressed? |
| 261 | if(pFileEntry->dwFlags & MPQ_FILE_COMPRESS_MASK) |
| 262 | { |
| 263 | // Allocate space for compressed data |
| 264 | pbCompressed = STORM_ALLOC(BYTE, pFileEntry->dwCmpSize); |
| 265 | if(pbCompressed == NULL) |
| 266 | return ERROR_NOT_ENOUGH_MEMORY; |
| 267 | pbRawData = pbCompressed; |
| 268 | } |
| 269 | |
| 270 | // Load the raw (compressed, encrypted) data |
| 271 | if(!FileStream_Read(ha->pStream, &RawFilePos, pbRawData, pFileEntry->dwCmpSize)) |
| 272 | { |
| 273 | STORM_FREE(pbCompressed); |
| 274 | return GetLastError(); |
| 275 | } |
| 276 | |
| 277 | // If the file is encrypted, we have to decrypt the data first |
| 278 | if(pFileEntry->dwFlags & MPQ_FILE_ENCRYPTED) |
| 279 | { |
| 280 | BSWAP_ARRAY32_UNSIGNED(pbRawData, pFileEntry->dwCmpSize); |
| 281 | DecryptMpqBlock(pbRawData, pFileEntry->dwCmpSize, hf->dwFileKey); |
| 282 | BSWAP_ARRAY32_UNSIGNED(pbRawData, pFileEntry->dwCmpSize); |
| 283 | } |
| 284 | |
| 285 | // If the file is compressed, we have to decompress it now |
| 286 | if(pFileEntry->dwFlags & MPQ_FILE_COMPRESS_MASK) |
| 287 | { |
| 288 | int cbOutBuffer = (int)hf->dwDataSize; |
| 289 | int cbInBuffer = (int)pFileEntry->dwCmpSize; |
| 290 | int nResult = 0; |
| 291 | |
| 292 | // |
no test coverage detected