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