diff options
author | Ladislav Zezula <ladislav.zezula@avg.com> | 2014-08-25 12:59:01 +0200 |
---|---|---|
committer | Ladislav Zezula <ladislav.zezula@avg.com> | 2014-08-25 12:59:01 +0200 |
commit | d7044aecaeb2bbb33e0f5cc2080d5b2995bd79d5 (patch) | |
tree | e260ef9d1415bef05b0071af573512d90f93ae3b | |
parent | 2b5b7e977145c16f6ec23933c41678c382d22de3 (diff) |
+ Added support for newest Spazzler protector
-rw-r--r-- | StormLib.bat | 50 | ||||
-rw-r--r-- | StormLib.vcxproj | 2 | ||||
-rw-r--r-- | StormLib.vcxproj.filters | 2 | ||||
-rw-r--r-- | StormLib_dll.vcxproj | 2 | ||||
-rw-r--r-- | StormLib_dll.vcxproj.filters | 2 | ||||
-rw-r--r-- | StormLib_test.vcxproj | 2 | ||||
-rw-r--r-- | StormLib_test.vcxproj.filters | 2 | ||||
-rw-r--r-- | StormLib_v09.sln | 1 | ||||
-rw-r--r-- | StormLib_v11.sln | 1 | ||||
-rw-r--r-- | doc/History.txt | 138 | ||||
-rw-r--r-- | src/FileStream.cpp | 5876 | ||||
-rw-r--r-- | src/SBaseCommon.cpp | 54 | ||||
-rw-r--r-- | src/SBaseFileTable.cpp | 259 | ||||
-rw-r--r-- | src/SFileAttributes.cpp | 1142 | ||||
-rw-r--r-- | src/SFileOpenArchive.cpp | 17 | ||||
-rw-r--r-- | src/SFileVerify.cpp | 18 | ||||
-rw-r--r-- | src/huffman/HuffTree.xls | bin | 361472 -> 361470 bytes | |||
-rw-r--r-- | storm_dll/Storm_dll.bat | 18 | ||||
-rw-r--r-- | storm_dll/Storm_dll.sln | 10 | ||||
-rw-r--r-- | storm_dll/storm.dll | bin | 327680 -> 327531 bytes | |||
-rw-r--r-- | storm_dll/storm.lib | bin | 0 -> 5238 bytes | |||
-rw-r--r-- | storm_dll/storm_dll.h | 12 | ||||
-rw-r--r-- | storm_dll/storm_test.cpp | 36 | ||||
-rw-r--r-- | storm_dll/storm_test.vcproj | 351 | ||||
-rw-r--r-- | test/StormTest.cpp | 14 |
25 files changed, 4174 insertions, 3835 deletions
diff --git a/StormLib.bat b/StormLib.bat index 509a0ef..d675eac 100644 --- a/StormLib.bat +++ b/StormLib.bat @@ -1,25 +1,25 @@ -@echo off
-rem Post-build batch for StormLib project
-rem Called as StormLib.bat $(PlatformName) $(ConfigurationName)
-rem Example: StormLib.bat x64 Debug
-
-if not exist ..\aaa goto exit
-
-copy src\StormPort.h ..\aaa\inc
-copy src\StormLib.h ..\aaa\inc
-
-if x%1 == xWin32 goto PlatformWin32
-if x%1 == xx64 goto PlatformWin64
-goto exit
-
-:PlatformWin32
-copy .\bin\StormLib\%1\%2\*.lib ..\aaa\lib32
-goto exit
-
-:PlatformWin64
-copy .\bin\StormLib\%1\%2\*.lib ..\aaa\lib64
-goto exit
-
-:exit
-
-
+@echo off +rem Post-build batch for StormLib project +rem Called as StormLib.bat $(PlatformName) $(ConfigurationName) +rem Example: StormLib.bat x64 Debug + +if not exist ..\aaa goto exit + +copy src\StormPort.h ..\aaa\inc +copy src\StormLib.h ..\aaa\inc + +if x%1 == xWin32 goto PlatformWin32 +if x%1 == xx64 goto PlatformWin64 +goto exit + +:PlatformWin32 +copy .\bin\StormLib\%1\%2\*.lib ..\aaa\lib32 +goto exit + +:PlatformWin64 +copy .\bin\StormLib\%1\%2\*.lib ..\aaa\lib64 +goto exit + +:exit + + diff --git a/StormLib.vcxproj b/StormLib.vcxproj index e1b22aa..6121dae 100644 --- a/StormLib.vcxproj +++ b/StormLib.vcxproj @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="DebugAD|Win32"> diff --git a/StormLib.vcxproj.filters b/StormLib.vcxproj.filters index b80d6b2..14dbfb0 100644 --- a/StormLib.vcxproj.filters +++ b/StormLib.vcxproj.filters @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="Doc Files"> diff --git a/StormLib_dll.vcxproj b/StormLib_dll.vcxproj index 8f3eb4a..bd47710 100644 --- a/StormLib_dll.vcxproj +++ b/StormLib_dll.vcxproj @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> diff --git a/StormLib_dll.vcxproj.filters b/StormLib_dll.vcxproj.filters index c1fff27..f5dff48 100644 --- a/StormLib_dll.vcxproj.filters +++ b/StormLib_dll.vcxproj.filters @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="Doc Files"> diff --git a/StormLib_test.vcxproj b/StormLib_test.vcxproj index 055ba70..d5b34da 100644 --- a/StormLib_test.vcxproj +++ b/StormLib_test.vcxproj @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> diff --git a/StormLib_test.vcxproj.filters b/StormLib_test.vcxproj.filters index 25f2cdd..d50ab80 100644 --- a/StormLib_test.vcxproj.filters +++ b/StormLib_test.vcxproj.filters @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="Doc Files"> diff --git a/StormLib_v09.sln b/StormLib_v09.sln index e436152..66937b5 100644 --- a/StormLib_v09.sln +++ b/StormLib_v09.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StormLib", "StormLib.vcproj", "{78424708-1F6E-4D4B-920C-FB6D26847055}" diff --git a/StormLib_v11.sln b/StormLib_v11.sln index 49cfedb..f849427 100644 --- a/StormLib_v11.sln +++ b/StormLib_v11.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StormLib", "StormLib.vcxproj", "{78424708-1F6E-4D4B-920C-FB6D26847055}" diff --git a/doc/History.txt b/doc/History.txt index a4f5ba9..70e7550 100644 --- a/doc/History.txt +++ b/doc/History.txt @@ -1,69 +1,69 @@ -
- StormLib history
- ================
-
- Version 9.00
-
- - Support for streaming (master-mirror)
- - Support for multi-file MPQs used by some WoW versions
- - Opening maps protected by Spazzler protector
- - Opening maps protected by BOBA protector
-
- Version 8.02
-
- - Support for UNICODE encoding for on-disk files
- - Optimized file deleting
-
- Version 8.01
-
- - SFileFindFirstFile and SFileFindNextFile no longer find files that have
- patch file in the oldest MPQ in the patch chain
- - Write support for MPQs version 4
-
- Version 8.00
-
- - Updated support for protected maps from Warcraft III
-
- Version 7.11
-
- - Support for MPQs v 3.0 (WOW-Cataclysm BETA)
- - StormLib now deals properly with files that have MPQ_SECTOR_CHECKSUM missing,
- but have sector checksum entry present in the sector offset table
-
- Version 7.10
-
- - Support for partial MPQs ("interface.MPQ.part")
- - The only operation that is externally allowed to do with internal files
- ("(listfile)", "(attributes)" and "(signature)") is reading. Attempt to modify any of the file
- fails and GetLastError returns ERROR_INTERNAL_FILE
- - Fixed memory leak that has occured when writing more than one sector to the file at once
-
- Version 7.01
-
- - Support for adding files from memory
- - Fixed improper validation of handles to MPQ file and MPQ archive
- - Fixed bug where StormLib didn't save CRC32 of the file when added to archive
-
- Version 7.00
-
- - Properly deals with MPQs protected by w3xMaster
- - Major rewrite
- - Fixed support for (attributes)
- - Added file verification
- - Added MPQ signature verification
-
- Version 6.22
-
- - Properly deals with MPQs protected by w3xMaster
-
- Version 6.21
-
- - SFileRenameFile now properly re-crypts the file if necessary.
- - SFileFindFirstFile correctly deals with deleted files
-
- Version 6.20
-
- - Fixed lots of bugs when processing files with same names but different locales
- - Fixed bugs when repeately extracts the same file with MPQ_FILE_SINGLE_UNIT flag
- - Added SFileFlushArchive
- - Fixed issue opening AVI files renamed to MPQ using SFileCreateArchiveEx
+ + StormLib history + ================ + + Version 9.00 + + - Support for streaming (master-mirror) + - Support for multi-file MPQs used by some WoW versions + - Opening maps protected by Spazzler protector + - Opening maps protected by BOBA protector + + Version 8.02 + + - Support for UNICODE encoding for on-disk files + - Optimized file deleting + + Version 8.01 + + - SFileFindFirstFile and SFileFindNextFile no longer find files that have + patch file in the oldest MPQ in the patch chain + - Write support for MPQs version 4 + + Version 8.00 + + - Updated support for protected maps from Warcraft III + + Version 7.11 + + - Support for MPQs v 3.0 (WOW-Cataclysm BETA) + - StormLib now deals properly with files that have MPQ_SECTOR_CHECKSUM missing, + but have sector checksum entry present in the sector offset table + + Version 7.10 + + - Support for partial MPQs ("interface.MPQ.part") + - The only operation that is externally allowed to do with internal files + ("(listfile)", "(attributes)" and "(signature)") is reading. Attempt to modify any of the file + fails and GetLastError returns ERROR_INTERNAL_FILE + - Fixed memory leak that has occured when writing more than one sector to the file at once + + Version 7.01 + + - Support for adding files from memory + - Fixed improper validation of handles to MPQ file and MPQ archive + - Fixed bug where StormLib didn't save CRC32 of the file when added to archive + + Version 7.00 + + - Properly deals with MPQs protected by w3xMaster + - Major rewrite + - Fixed support for (attributes) + - Added file verification + - Added MPQ signature verification + + Version 6.22 + + - Properly deals with MPQs protected by w3xMaster + + Version 6.21 + + - SFileRenameFile now properly re-crypts the file if necessary. + - SFileFindFirstFile correctly deals with deleted files + + Version 6.20 + + - Fixed lots of bugs when processing files with same names but different locales + - Fixed bugs when repeately extracts the same file with MPQ_FILE_SINGLE_UNIT flag + - Added SFileFlushArchive + - Fixed issue opening AVI files renamed to MPQ using SFileCreateArchiveEx diff --git a/src/FileStream.cpp b/src/FileStream.cpp index 10f285e..2f6e599 100644 --- a/src/FileStream.cpp +++ b/src/FileStream.cpp @@ -1,2938 +1,2938 @@ -/*****************************************************************************/
-/* FileStream.cpp Copyright (c) Ladislav Zezula 2010 */
-/*---------------------------------------------------------------------------*/
-/* File stream support for StormLib */
-/* */
-/* Windows support: Written by Ladislav Zezula */
-/* Mac support: Written by Sam Wilkins */
-/* Linux support: Written by Sam Wilkins and Ivan Komissarov */
-/* Big-endian: Written & debugged by Sam Wilkins */
-/*---------------------------------------------------------------------------*/
-/* Date Ver Who Comment */
-/* -------- ---- --- ------- */
-/* 11.06.10 1.00 Lad Derived from StormPortMac.cpp and StormPortLinux.cpp */
-/*****************************************************************************/
-
-#define __STORMLIB_SELF__
-#include "StormLib.h"
-#include "StormCommon.h"
-#include "FileStream.h"
-
-#ifdef _MSC_VER
-#pragma comment(lib, "wininet.lib") // Internet functions for HTTP stream
-#pragma warning(disable: 4800) // 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
-#endif
-
-//-----------------------------------------------------------------------------
-// Local defines
-
-#ifndef INVALID_HANDLE_VALUE
-#define INVALID_HANDLE_VALUE ((HANDLE)-1)
-#endif
-
-//-----------------------------------------------------------------------------
-// Local functions - platform-specific functions
-
-#ifndef PLATFORM_WINDOWS
-static int nLastError = ERROR_SUCCESS;
-
-int GetLastError()
-{
- return nLastError;
-}
-
-void SetLastError(int nError)
-{
- nLastError = nError;
-}
-#endif
-
-static DWORD StringToInt(const char * szString)
-{
- DWORD dwValue = 0;
-
- while('0' <= szString[0] && szString[0] <= '9')
- {
- dwValue = (dwValue * 10) + (szString[0] - '9');
- szString++;
- }
-
- return dwValue;
-}
-
-//-----------------------------------------------------------------------------
-// Dummy init function
-
-static void BaseNone_Init(TFileStream *)
-{
- // Nothing here
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - base file support
-
-static bool BaseFile_Create(TFileStream * pStream)
-{
-#ifdef PLATFORM_WINDOWS
- {
- DWORD dwWriteShare = (pStream->dwFlags & STREAM_FLAG_WRITE_SHARE) ? FILE_SHARE_WRITE : 0;
-
- pStream->Base.File.hFile = CreateFile(pStream->szFileName,
- GENERIC_READ | GENERIC_WRITE,
- dwWriteShare | FILE_SHARE_READ,
- NULL,
- CREATE_ALWAYS,
- 0,
- NULL);
- if(pStream->Base.File.hFile == INVALID_HANDLE_VALUE)
- return false;
- }
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- {
- intptr_t handle;
-
- handle = open(pStream->szFileName, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if(handle == -1)
- {
- nLastError = errno;
- return false;
- }
-
- pStream->Base.File.hFile = (HANDLE)handle;
- }
-#endif
-
- // Reset the file size and position
- pStream->Base.File.FileSize = 0;
- pStream->Base.File.FilePos = 0;
- return true;
-}
-
-static bool BaseFile_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags)
-{
-#ifdef PLATFORM_WINDOWS
- {
- ULARGE_INTEGER FileSize;
- DWORD dwWriteAccess = (dwStreamFlags & STREAM_FLAG_READ_ONLY) ? 0 : FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES;
- DWORD dwWriteShare = (dwStreamFlags & STREAM_FLAG_WRITE_SHARE) ? FILE_SHARE_WRITE : 0;
-
- // Open the file
- pStream->Base.File.hFile = CreateFile(szFileName,
- FILE_READ_DATA | FILE_READ_ATTRIBUTES | dwWriteAccess,
- FILE_SHARE_READ | dwWriteShare,
- NULL,
- OPEN_EXISTING,
- 0,
- NULL);
- if(pStream->Base.File.hFile == INVALID_HANDLE_VALUE)
- return false;
-
- // Query the file size
- FileSize.LowPart = GetFileSize(pStream->Base.File.hFile, &FileSize.HighPart);
- pStream->Base.File.FileSize = FileSize.QuadPart;
-
- // Query last write time
- GetFileTime(pStream->Base.File.hFile, NULL, NULL, (LPFILETIME)&pStream->Base.File.FileTime);
- }
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- {
- struct stat64 fileinfo;
- int oflag = (dwStreamFlags & STREAM_FLAG_READ_ONLY) ? O_RDONLY : O_RDWR;
- intptr_t handle;
-
- // Open the file
- handle = open(szFileName, oflag | O_LARGEFILE);
- if(handle == -1)
- {
- nLastError = errno;
- return false;
- }
-
- // Get the file size
- if(fstat64(handle, &fileinfo) == -1)
- {
- nLastError = errno;
- return false;
- }
-
- // time_t is number of seconds since 1.1.1970, UTC.
- // 1 second = 10000000 (decimal) in FILETIME
- // Set the start to 1.1.1970 00:00:00
- pStream->Base.File.FileTime = 0x019DB1DED53E8000ULL + (10000000 * fileinfo.st_mtime);
- pStream->Base.File.FileSize = (ULONGLONG)fileinfo.st_size;
- pStream->Base.File.hFile = (HANDLE)handle;
- }
-#endif
-
- // Reset the file position
- pStream->Base.File.FilePos = 0;
- return true;
-}
-
-static bool BaseFile_Read(
- TFileStream * pStream, // Pointer to an open stream
- ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
- void * pvBuffer, // Pointer to data to be read
- DWORD dwBytesToRead) // Number of bytes to read from the file
-{
- ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.File.FilePos;
- DWORD dwBytesRead = 0; // Must be set by platform-specific code
-
-#ifdef PLATFORM_WINDOWS
- {
- // Note: StormLib no longer supports Windows 9x.
- // Thus, we can use the OVERLAPPED structure to specify
- // file offset to read from file. This allows us to skip
- // one system call to SetFilePointer
-
- // Update the byte offset
- pStream->Base.File.FilePos = ByteOffset;
-
- // Read the data
- if(dwBytesToRead != 0)
- {
- OVERLAPPED Overlapped;
-
- Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
- Overlapped.Offset = (DWORD)ByteOffset;
- Overlapped.hEvent = NULL;
- if(!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped))
- return false;
- }
- }
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- {
- ssize_t bytes_read;
-
- // If the byte offset is different from the current file position,
- // we have to update the file position xxx
- if(ByteOffset != pStream->Base.File.FilePos)
- {
- lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET);
- pStream->Base.File.FilePos = ByteOffset;
- }
-
- // Perform the read operation
- if(dwBytesToRead != 0)
- {
- bytes_read = read((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToRead);
- if(bytes_read == -1)
- {
- nLastError = errno;
- return false;
- }
-
- dwBytesRead = (DWORD)(size_t)bytes_read;
- }
- }
-#endif
-
- // Increment the current file position by number of bytes read
- // If the number of bytes read doesn't match to required amount, return false
- pStream->Base.File.FilePos = ByteOffset + dwBytesRead;
- if(dwBytesRead != dwBytesToRead)
- SetLastError(ERROR_HANDLE_EOF);
- return (dwBytesRead == dwBytesToRead);
-}
-
-/**
- * \a pStream Pointer to an open stream
- * \a pByteOffset Pointer to file byte offset. If NULL, writes to current position
- * \a pvBuffer Pointer to data to be written
- * \a dwBytesToWrite Number of bytes to write to the file
- */
-
-static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite)
-{
- ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.File.FilePos;
- DWORD dwBytesWritten = 0; // Must be set by platform-specific code
-
-#ifdef PLATFORM_WINDOWS
- {
- // Note: StormLib no longer supports Windows 9x.
- // Thus, we can use the OVERLAPPED structure to specify
- // file offset to read from file. This allows us to skip
- // one system call to SetFilePointer
-
- // Update the byte offset
- pStream->Base.File.FilePos = ByteOffset;
-
- // Read the data
- if(dwBytesToWrite != 0)
- {
- OVERLAPPED Overlapped;
-
- Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32);
- Overlapped.Offset = (DWORD)ByteOffset;
- Overlapped.hEvent = NULL;
- if(!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped))
- return false;
- }
- }
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- {
- ssize_t bytes_written;
-
- // If the byte offset is different from the current file position,
- // we have to update the file position
- if(ByteOffset != pStream->Base.File.FilePos)
- {
- lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET);
- pStream->Base.File.FilePos = ByteOffset;
- }
-
- // Perform the read operation
- bytes_written = write((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToWrite);
- if(bytes_written == -1)
- {
- nLastError = errno;
- return false;
- }
-
- dwBytesWritten = (DWORD)(size_t)bytes_written;
- }
-#endif
-
- // Increment the current file position by number of bytes read
- pStream->Base.File.FilePos = ByteOffset + dwBytesWritten;
-
- // Also modify the file size, if needed
- if(pStream->Base.File.FilePos > pStream->Base.File.FileSize)
- pStream->Base.File.FileSize = pStream->Base.File.FilePos;
-
- if(dwBytesWritten != dwBytesToWrite)
- SetLastError(ERROR_DISK_FULL);
- return (dwBytesWritten == dwBytesToWrite);
-}
-
-/**
- * \a pStream Pointer to an open stream
- * \a NewFileSize New size of the file
- */
-static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize)
-{
-#ifdef PLATFORM_WINDOWS
- {
- LONG FileSizeHi = (LONG)(NewFileSize >> 32);
- LONG FileSizeLo;
- DWORD dwNewPos;
- bool bResult;
-
- // Set the position at the new file size
- dwNewPos = SetFilePointer(pStream->Base.File.hFile, (LONG)NewFileSize, &FileSizeHi, FILE_BEGIN);
- if(dwNewPos == INVALID_SET_FILE_POINTER && GetLastError() != ERROR_SUCCESS)
- return false;
-
- // Set the current file pointer as the end of the file
- bResult = (bool)SetEndOfFile(pStream->Base.File.hFile);
- if(bResult)
- pStream->Base.File.FileSize = NewFileSize;
-
- // Restore the file position
- FileSizeHi = (LONG)(pStream->Base.File.FilePos >> 32);
- FileSizeLo = (LONG)(pStream->Base.File.FilePos);
- SetFilePointer(pStream->Base.File.hFile, FileSizeLo, &FileSizeHi, FILE_BEGIN);
- return bResult;
- }
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- {
- if(ftruncate64((intptr_t)pStream->Base.File.hFile, (off64_t)NewFileSize) == -1)
- {
- nLastError = errno;
- return false;
- }
-
- pStream->Base.File.FileSize = NewFileSize;
- return true;
- }
-#endif
-}
-
-// Gives the current file size
-static bool BaseFile_GetSize(TFileStream * pStream, ULONGLONG * pFileSize)
-{
- // Note: Used by all thre base providers.
- // Requires the TBaseData union to have the same layout for all three base providers
- *pFileSize = pStream->Base.File.FileSize;
- return true;
-}
-
-// Gives the current file position
-static bool BaseFile_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset)
-{
- // Note: Used by all thre base providers.
- // Requires the TBaseData union to have the same layout for all three base providers
- *pByteOffset = pStream->Base.File.FilePos;
- return true;
-}
-
-// Renames the file pointed by pStream so that it contains data from pNewStream
-static bool BaseFile_Replace(TFileStream * pStream, TFileStream * pNewStream)
-{
-#ifdef PLATFORM_WINDOWS
- // Delete the original stream file. Don't check the result value,
- // because if the file doesn't exist, it would fail
- DeleteFile(pStream->szFileName);
-
- // Rename the new file to the old stream's file
- return (bool)MoveFile(pNewStream->szFileName, pStream->szFileName);
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- // "rename" on Linux also works if the target file exists
- if(rename(pNewStream->szFileName, pStream->szFileName) == -1)
- {
- nLastError = errno;
- return false;
- }
-
- return true;
-#endif
-}
-
-static void BaseFile_Close(TFileStream * pStream)
-{
- if(pStream->Base.File.hFile != INVALID_HANDLE_VALUE)
- {
-#ifdef PLATFORM_WINDOWS
- CloseHandle(pStream->Base.File.hFile);
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- close((intptr_t)pStream->Base.File.hFile);
-#endif
- }
-
- // Also invalidate the handle
- pStream->Base.File.hFile = INVALID_HANDLE_VALUE;
-}
-
-// Initializes base functions for the disk file
-static void BaseFile_Init(TFileStream * pStream)
-{
- pStream->BaseCreate = BaseFile_Create;
- pStream->BaseOpen = BaseFile_Open;
- pStream->BaseRead = BaseFile_Read;
- pStream->BaseWrite = BaseFile_Write;
- pStream->BaseResize = BaseFile_Resize;
- pStream->BaseGetSize = BaseFile_GetSize;
- pStream->BaseGetPos = BaseFile_GetPos;
- pStream->BaseClose = BaseFile_Close;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - base memory-mapped file support
-
-static bool BaseMap_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags)
-{
-#ifdef PLATFORM_WINDOWS
-
- ULARGE_INTEGER FileSize;
- HANDLE hFile;
- HANDLE hMap;
- bool bResult = false;
-
- // Keep compiler happy
- dwStreamFlags = dwStreamFlags;
-
- // Open the file for read access
- hFile = CreateFile(szFileName, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
- if(hFile != INVALID_HANDLE_VALUE)
- {
- // Retrieve file size. Don't allow mapping file of a zero size.
- FileSize.LowPart = GetFileSize(hFile, &FileSize.HighPart);
- if(FileSize.QuadPart != 0)
- {
- // Now create mapping object
- hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
- if(hMap != NULL)
- {
- // Map the entire view into memory
- // Note that this operation will fail if the file can't fit
- // into usermode address space
- pStream->Base.Map.pbFile = (LPBYTE)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
- if(pStream->Base.Map.pbFile != NULL)
- {
- // Retrieve file time
- GetFileTime(hFile, NULL, NULL, (LPFILETIME)&pStream->Base.Map.FileTime);
-
- // Retrieve file size and position
- pStream->Base.Map.FileSize = FileSize.QuadPart;
- pStream->Base.Map.FilePos = 0;
- bResult = true;
- }
-
- // Close the map handle
- CloseHandle(hMap);
- }
- }
-
- // Close the file handle
- CloseHandle(hFile);
- }
-
- // If the file is not there and is not available for random access,
- // report error
- if(bResult == false)
- return false;
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- struct stat64 fileinfo;
- intptr_t handle;
- bool bResult = false;
-
- // Open the file
- handle = open(szFileName, O_RDONLY);
- if(handle != -1)
- {
- // Get the file size
- if(fstat64(handle, &fileinfo) != -1)
- {
- pStream->Base.Map.pbFile = (LPBYTE)mmap(NULL, (size_t)fileinfo.st_size, PROT_READ, MAP_PRIVATE, handle, 0);
- if(pStream->Base.Map.pbFile != NULL)
- {
- // time_t is number of seconds since 1.1.1970, UTC.
- // 1 second = 10000000 (decimal) in FILETIME
- // Set the start to 1.1.1970 00:00:00
- pStream->Base.Map.FileTime = 0x019DB1DED53E8000ULL + (10000000 * fileinfo.st_mtime);
- pStream->Base.Map.FileSize = (ULONGLONG)fileinfo.st_size;
- pStream->Base.Map.FilePos = 0;
- bResult = true;
- }
- }
- close(handle);
- }
-
- // Did the mapping fail?
- if(bResult == false)
- {
- nLastError = errno;
- return false;
- }
-#endif
-
- return true;
-}
-
-static bool BaseMap_Read(
- TFileStream * pStream, // Pointer to an open stream
- ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
- void * pvBuffer, // Pointer to data to be read
- DWORD dwBytesToRead) // Number of bytes to read from the file
-{
- ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.Map.FilePos;
-
- // Do we have to read anything at all?
- if(dwBytesToRead != 0)
- {
- // Don't allow reading past file size
- if((ByteOffset + dwBytesToRead) > pStream->Base.Map.FileSize)
- return false;
-
- // Copy the required data
- memcpy(pvBuffer, pStream->Base.Map.pbFile + (size_t)ByteOffset, dwBytesToRead);
- }
-
- // Move the current file position
- pStream->Base.Map.FilePos += dwBytesToRead;
- return true;
-}
-
-static void BaseMap_Close(TFileStream * pStream)
-{
-#ifdef PLATFORM_WINDOWS
- if(pStream->Base.Map.pbFile != NULL)
- UnmapViewOfFile(pStream->Base.Map.pbFile);
-#endif
-
-#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
- if(pStream->Base.Map.pbFile != NULL)
- munmap(pStream->Base.Map.pbFile, (size_t )pStream->Base.Map.FileSize);
-#endif
-
- pStream->Base.Map.pbFile = NULL;
-}
-
-// Initializes base functions for the mapped file
-static void BaseMap_Init(TFileStream * pStream)
-{
- // Supply the file stream functions
- pStream->BaseOpen = BaseMap_Open;
- pStream->BaseRead = BaseMap_Read;
- pStream->BaseGetSize = BaseFile_GetSize; // Reuse BaseFile function
- pStream->BaseGetPos = BaseFile_GetPos; // Reuse BaseFile function
- pStream->BaseClose = BaseMap_Close;
-
- // Mapped files are read-only
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - base HTTP file support
-
-static const TCHAR * BaseHttp_ExtractServerName(const TCHAR * szFileName, TCHAR * szServerName)
-{
- // Check for HTTP
- if(!_tcsnicmp(szFileName, _T("http://"), 7))
- szFileName += 7;
-
- // Cut off the server name
- if(szServerName != NULL)
- {
- while(szFileName[0] != 0 && szFileName[0] != _T('/'))
- *szServerName++ = *szFileName++;
- *szServerName = 0;
- }
- else
- {
- while(szFileName[0] != 0 && szFileName[0] != _T('/'))
- szFileName++;
- }
-
- // Return the remainder
- return szFileName;
-}
-
-static bool BaseHttp_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags)
-{
-#ifdef PLATFORM_WINDOWS
-
- HINTERNET hRequest;
- DWORD dwTemp = 0;
- bool bFileAvailable = false;
- int nError = ERROR_SUCCESS;
-
- // Keep compiler happy
- dwStreamFlags = dwStreamFlags;
-
- // Don't connect to the internet
- if(!InternetGetConnectedState(&dwTemp, 0))
- nError = GetLastError();
-
- // Initiate the connection to the internet
- if(nError == ERROR_SUCCESS)
- {
- pStream->Base.Http.hInternet = InternetOpen(_T("StormLib HTTP MPQ reader"),
- INTERNET_OPEN_TYPE_PRECONFIG,
- NULL,
- NULL,
- 0);
- if(pStream->Base.Http.hInternet == NULL)
- nError = GetLastError();
- }
-
- // Connect to the server
- if(nError == ERROR_SUCCESS)
- {
- TCHAR szServerName[MAX_PATH];
- DWORD dwFlags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_UI | INTERNET_FLAG_NO_CACHE_WRITE;
-
- // Initiate connection with the server
- szFileName = BaseHttp_ExtractServerName(szFileName, szServerName);
- pStream->Base.Http.hConnect = InternetConnect(pStream->Base.Http.hInternet,
- szServerName,
- INTERNET_DEFAULT_HTTP_PORT,
- NULL,
- NULL,
- INTERNET_SERVICE_HTTP,
- dwFlags,
- 0);
- if(pStream->Base.Http.hConnect == NULL)
- nError = GetLastError();
- }
-
- // Now try to query the file size
- if(nError == ERROR_SUCCESS)
- {
- // Open HTTP request to the file
- hRequest = HttpOpenRequest(pStream->Base.Http.hConnect, _T("GET"), szFileName, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
- if(hRequest != NULL)
- {
- if(HttpSendRequest(hRequest, NULL, 0, NULL, 0))
- {
- ULONGLONG FileTime = 0;
- DWORD dwFileSize = 0;
- DWORD dwDataSize;
- DWORD dwIndex = 0;
-
- // Check if the MPQ has Last Modified field
- dwDataSize = sizeof(ULONGLONG);
- if(HttpQueryInfo(hRequest, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME, &FileTime, &dwDataSize, &dwIndex))
- pStream->Base.Http.FileTime = FileTime;
-
- // Verify if the server supports random access
- dwDataSize = sizeof(DWORD);
- if(HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwFileSize, &dwDataSize, &dwIndex))
- {
- if(dwFileSize != 0)
- {
- pStream->Base.Http.FileSize = dwFileSize;
- pStream->Base.Http.FilePos = 0;
- bFileAvailable = true;
- }
- }
- }
- InternetCloseHandle(hRequest);
- }
- }
-
- // If the file is not there and is not available for random access,
- // report error
- if(bFileAvailable == false)
- {
- pStream->BaseClose(pStream);
- return false;
- }
-
- return true;
-
-#else
-
- // Not supported
- SetLastError(ERROR_NOT_SUPPORTED);
- pStream = pStream;
- return false;
-
-#endif
-}
-
-static bool BaseHttp_Read(
- TFileStream * pStream, // Pointer to an open stream
- ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
- void * pvBuffer, // Pointer to data to be read
- DWORD dwBytesToRead) // Number of bytes to read from the file
-{
-#ifdef PLATFORM_WINDOWS
- ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.Http.FilePos;
- DWORD dwTotalBytesRead = 0;
-
- // Do we have to read anything at all?
- if(dwBytesToRead != 0)
- {
- HINTERNET hRequest;
- LPCTSTR szFileName;
- LPBYTE pbBuffer = (LPBYTE)pvBuffer;
- TCHAR szRangeRequest[0x80];
- DWORD dwStartOffset = (DWORD)ByteOffset;
- DWORD dwEndOffset = dwStartOffset + dwBytesToRead;
-
- // Open HTTP request to the file
- szFileName = BaseHttp_ExtractServerName(pStream->szFileName, NULL);
- hRequest = HttpOpenRequest(pStream->Base.Http.hConnect, _T("GET"), szFileName, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
- if(hRequest != NULL)
- {
- // Add range request to the HTTP headers
- // http://www.clevercomponents.com/articles/article015/resuming.asp
- _stprintf(szRangeRequest, _T("Range: bytes=%u-%u"), (unsigned int)dwStartOffset, (unsigned int)dwEndOffset);
- HttpAddRequestHeaders(hRequest, szRangeRequest, 0xFFFFFFFF, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
-
- // Send the request to the server
- if(HttpSendRequest(hRequest, NULL, 0, NULL, 0))
- {
- while(dwTotalBytesRead < dwBytesToRead)
- {
- DWORD dwBlockBytesToRead = dwBytesToRead - dwTotalBytesRead;
- DWORD dwBlockBytesRead = 0;
-
- // Read the block from the file
- if(dwBlockBytesToRead > 0x200)
- dwBlockBytesToRead = 0x200;
- InternetReadFile(hRequest, pbBuffer, dwBlockBytesToRead, &dwBlockBytesRead);
-
- // Check for end
- if(dwBlockBytesRead == 0)
- break;
-
- // Move buffers
- dwTotalBytesRead += dwBlockBytesRead;
- pbBuffer += dwBlockBytesRead;
- }
- }
- InternetCloseHandle(hRequest);
- }
- }
-
- // Increment the current file position by number of bytes read
- pStream->Base.Http.FilePos = ByteOffset + dwTotalBytesRead;
-
- // If the number of bytes read doesn't match the required amount, return false
- if(dwTotalBytesRead != dwBytesToRead)
- SetLastError(ERROR_HANDLE_EOF);
- return (dwTotalBytesRead == dwBytesToRead);
-
-#else
-
- // Not supported
- pStream = pStream;
- pByteOffset = pByteOffset;
- pvBuffer = pvBuffer;
- dwBytesToRead = dwBytesToRead;
- SetLastError(ERROR_NOT_SUPPORTED);
- return false;
-
-#endif
-}
-
-static void BaseHttp_Close(TFileStream * pStream)
-{
-#ifdef PLATFORM_WINDOWS
- if(pStream->Base.Http.hConnect != NULL)
- InternetCloseHandle(pStream->Base.Http.hConnect);
- pStream->Base.Http.hConnect = NULL;
-
- if(pStream->Base.Http.hInternet != NULL)
- InternetCloseHandle(pStream->Base.Http.hInternet);
- pStream->Base.Http.hInternet = NULL;
-#else
- pStream = pStream;
-#endif
-}
-
-// Initializes base functions for the mapped file
-static void BaseHttp_Init(TFileStream * pStream)
-{
- // Supply the stream functions
- pStream->BaseOpen = BaseHttp_Open;
- pStream->BaseRead = BaseHttp_Read;
- pStream->BaseGetSize = BaseFile_GetSize; // Reuse BaseFile function
- pStream->BaseGetPos = BaseFile_GetPos; // Reuse BaseFile function
- pStream->BaseClose = BaseHttp_Close;
-
- // HTTP files are read-only
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - base block-based support
-
-// Generic function that loads blocks from the file
-// The function groups the block with the same availability,
-// so the called BlockRead can finish the request in a single system call
-static bool BlockStream_Read(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position
- void * pvBuffer, // Pointer to data to be read
- DWORD dwBytesToRead) // Number of bytes to read from the file
-{
- ULONGLONG BlockOffset0;
- ULONGLONG BlockOffset;
- ULONGLONG ByteOffset;
- ULONGLONG EndOffset;
- LPBYTE TransferBuffer;
- LPBYTE BlockBuffer;
- DWORD BlockBufferOffset; // Offset of the desired data in the block buffer
- DWORD BytesNeeded; // Number of bytes that really need to be read
- DWORD BlockSize = pStream->BlockSize;
- DWORD BlockCount;
- bool bPrevBlockAvailable;
- bool bCallbackCalled = false;
- bool bBlockAvailable;
- bool bResult = true;
-
- // The base block read function must be present
- assert(pStream->BlockRead != NULL);
-
- // NOP reading of zero bytes
- if(dwBytesToRead == 0)
- return true;
-
- // Get the current position in the stream
- ByteOffset = (pByteOffset != NULL) ? pByteOffset[0] : pStream->StreamPos;
- EndOffset = ByteOffset + dwBytesToRead;
- if(EndOffset > pStream->StreamSize)
- {
- SetLastError(ERROR_HANDLE_EOF);
- return false;
- }
-
- // Calculate the block parameters
- BlockOffset0 = BlockOffset = ByteOffset & ~((ULONGLONG)BlockSize - 1);
- BlockCount = (DWORD)(((EndOffset - BlockOffset) + (BlockSize - 1)) / BlockSize);
- BytesNeeded = (DWORD)(EndOffset - BlockOffset);
-
- // Remember where we have our data
- assert((BlockSize & (BlockSize - 1)) == 0);
- BlockBufferOffset = (DWORD)(ByteOffset & (BlockSize - 1));
-
- // Allocate buffer for reading blocks
- TransferBuffer = BlockBuffer = STORM_ALLOC(BYTE, (BlockCount * BlockSize));
- if(TransferBuffer == NULL)
- {
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
- return false;
- }
-
- // If all blocks are available, just read all blocks at once
- if(pStream->IsComplete == 0)
- {
- // Now parse the blocks and send the block read request
- // to all blocks with the same availability
- assert(pStream->BlockCheck != NULL);
- bPrevBlockAvailable = pStream->BlockCheck(pStream, BlockOffset);
-
- // Loop as long as we have something to read
- while(BlockOffset < EndOffset)
- {
- // Determine availability of the next block
- bBlockAvailable = pStream->BlockCheck(pStream, BlockOffset);
-
- // If the availability has changed, read all blocks up to this one
- if(bBlockAvailable != bPrevBlockAvailable)
- {
- // Call the file stream callback, if the block is not available
- if(pStream->pMaster && pStream->pfnCallback && bPrevBlockAvailable == false)
- {
- pStream->pfnCallback(pStream->UserData, BlockOffset0, (DWORD)(BlockOffset - BlockOffset0));
- bCallbackCalled = true;
- }
-
- // Load the continuous blocks with the same availability
- assert(BlockOffset > BlockOffset0);
- bResult = pStream->BlockRead(pStream, BlockOffset0, BlockOffset, BlockBuffer, BytesNeeded, bPrevBlockAvailable);
- if(!bResult)
- break;
-
- // Move the block offset
- BlockBuffer += (DWORD)(BlockOffset - BlockOffset0);
- BytesNeeded -= (DWORD)(BlockOffset - BlockOffset0);
- bPrevBlockAvailable = bBlockAvailable;
- BlockOffset0 = BlockOffset;
- }
-
- // Move to the block offset in the stream
- BlockOffset += BlockSize;
- }
-
- // If there is a block(s) remaining to be read, do it
- if(BlockOffset > BlockOffset0)
- {
- // Call the file stream callback, if the block is not available
- if(pStream->pMaster && pStream->pfnCallback && bPrevBlockAvailable == false)
- {
- pStream->pfnCallback(pStream->UserData, BlockOffset0, (DWORD)(BlockOffset - BlockOffset0));
- bCallbackCalled = true;
- }
-
- // Read the complete blocks from the file
- if(BlockOffset > pStream->StreamSize)
- BlockOffset = pStream->StreamSize;
- bResult = pStream->BlockRead(pStream, BlockOffset0, BlockOffset, BlockBuffer, BytesNeeded, bPrevBlockAvailable);
- }
- }
- else
- {
- // Read the complete blocks from the file
- if(EndOffset > pStream->StreamSize)
- EndOffset = pStream->StreamSize;
- bResult = pStream->BlockRead(pStream, BlockOffset, EndOffset, BlockBuffer, BytesNeeded, true);
- }
-
- // Now copy the data to the user buffer
- if(bResult)
- {
- memcpy(pvBuffer, TransferBuffer + BlockBufferOffset, dwBytesToRead);
- pStream->StreamPos = ByteOffset + dwBytesToRead;
- }
- else
- {
- // If the block read failed, set the last error
- SetLastError(ERROR_FILE_INCOMPLETE);
- }
-
- // Call the callback to indicate we are done
- if(bCallbackCalled)
- pStream->pfnCallback(pStream->UserData, 0, 0);
-
- // Free the block buffer and return
- STORM_FREE(TransferBuffer);
- return bResult;
-}
-
-static bool BlockStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize)
-{
- *pFileSize = pStream->StreamSize;
- return true;
-}
-
-static bool BlockStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset)
-{
- *pByteOffset = pStream->StreamPos;
- return true;
-}
-
-static void BlockStream_Close(TBlockStream * pStream)
-{
- // Free the data map, if any
- if(pStream->FileBitmap != NULL)
- STORM_FREE(pStream->FileBitmap);
- pStream->FileBitmap = NULL;
-
- // Call the base class for closing the stream
- pStream->BaseClose(pStream);
-}
-
-//-----------------------------------------------------------------------------
-// File stream allocation function
-
-static STREAM_INIT StreamBaseInit[4] =
-{
- BaseFile_Init,
- BaseMap_Init,
- BaseHttp_Init,
- BaseNone_Init
-};
-
-// This function allocates an empty structure for the file stream
-// The stream structure is created as flat block, variable length
-// The file name is placed after the end of the stream structure data
-static TFileStream * AllocateFileStream(
- const TCHAR * szFileName,
- size_t StreamSize,
- DWORD dwStreamFlags)
-{
- TFileStream * pMaster = NULL;
- TFileStream * pStream;
- const TCHAR * szNextFile = szFileName;
- size_t FileNameSize;
-
- // Sanity check
- assert(StreamSize != 0);
-
- // The caller can specify chain of files in the following form:
- // C:\archive.MPQ*http://www.server.com/MPQs/archive-server.MPQ
- // In that case, we use the part after "*" as master file name
- while(szNextFile[0] != 0 && szNextFile[0] != _T('*'))
- szNextFile++;
- FileNameSize = (size_t)((szNextFile - szFileName) * sizeof(TCHAR));
-
- // If we have a next file, we need to open it as master stream
- // Note that we don't care if the master stream exists or not,
- // If it doesn't, later attempts to read missing file block will fail
- if(szNextFile[0] == _T('*'))
- {
- // Don't allow another master file in the string
- if(_tcschr(szNextFile + 1, _T('*')) != NULL)
- {
- SetLastError(ERROR_INVALID_PARAMETER);
- return NULL;
- }
-
- // Open the master file
- pMaster = FileStream_OpenFile(szNextFile + 1, STREAM_FLAG_READ_ONLY);
- }
-
- // Allocate the stream structure for the given stream type
- pStream = (TFileStream *)STORM_ALLOC(BYTE, StreamSize + FileNameSize + sizeof(TCHAR));
- if(pStream != NULL)
- {
- // Zero the entire structure
- memset(pStream, 0, StreamSize);
- pStream->pMaster = pMaster;
- pStream->dwFlags = dwStreamFlags;
-
- // Initialize the file name
- pStream->szFileName = (TCHAR *)((BYTE *)pStream + StreamSize);
- memcpy(pStream->szFileName, szFileName, FileNameSize);
- pStream->szFileName[FileNameSize / sizeof(TCHAR)] = 0;
-
- // Initialize the stream functions
- StreamBaseInit[dwStreamFlags & 0x03](pStream);
- }
-
- return pStream;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - flat stream support
-
-static DWORD FlatStream_CheckFile(TBlockStream * pStream)
-{
- LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap;
- DWORD WholeByteCount = (pStream->BlockCount / 8);
- DWORD ExtraBitsCount = (pStream->BlockCount & 7);
- BYTE ExpectedValue;
-
- // Verify the whole bytes - their value must be 0xFF
- for(DWORD i = 0; i < WholeByteCount; i++)
- {
- if(FileBitmap[i] != 0xFF)
- return 0;
- }
-
- // If there are extra bits, calculate the mask
- if(ExtraBitsCount != 0)
- {
- ExpectedValue = (BYTE)((1 << ExtraBitsCount) - 1);
- if(FileBitmap[WholeByteCount] != ExpectedValue)
- return 0;
- }
-
- // Yes, the file is complete
- return 1;
-}
-
-static bool FlatStream_LoadBitmap(TBlockStream * pStream)
-{
- FILE_BITMAP_FOOTER Footer;
- ULONGLONG ByteOffset;
- LPBYTE FileBitmap;
- DWORD BlockCount;
- DWORD BitmapSize;
-
- // Do not load the bitmap if we should not have to
- if(!(pStream->dwFlags & STREAM_FLAG_USE_BITMAP))
- return false;
-
- // Only if the size is greater than size of bitmap footer
- if(pStream->Base.File.FileSize > sizeof(FILE_BITMAP_FOOTER))
- {
- // Load the bitmap footer
- ByteOffset = pStream->Base.File.FileSize - sizeof(FILE_BITMAP_FOOTER);
- if(pStream->BaseRead(pStream, &ByteOffset, &Footer, sizeof(FILE_BITMAP_FOOTER)))
- {
- // Make sure that the array is properly BSWAP-ed
- BSWAP_ARRAY32_UNSIGNED((LPDWORD)(&Footer), sizeof(FILE_BITMAP_FOOTER));
-
- // Verify if there is actually a footer
- if(Footer.Signature == ID_FILE_BITMAP_FOOTER && Footer.Version == 0x03)
- {
- // Get the offset of the bitmap, number of blocks and size of the bitmap
- ByteOffset = MAKE_OFFSET64(Footer.MapOffsetHi, Footer.MapOffsetLo);
- BlockCount = (DWORD)(((ByteOffset - 1) / Footer.BlockSize) + 1);
- BitmapSize = ((BlockCount + 7) / 8);
-
- // Check if the sizes match
- if(ByteOffset + BitmapSize + sizeof(FILE_BITMAP_FOOTER) == pStream->Base.File.FileSize)
- {
- // Allocate space for the bitmap
- FileBitmap = STORM_ALLOC(BYTE, BitmapSize);
- if(FileBitmap != NULL)
- {
- // Load the bitmap bits
- if(!pStream->BaseRead(pStream, &ByteOffset, FileBitmap, BitmapSize))
- {
- STORM_FREE(FileBitmap);
- return false;
- }
-
- // Update the stream size
- pStream->BuildNumber = Footer.BuildNumber;
- pStream->StreamSize = ByteOffset;
-
- // Fill the bitmap information
- pStream->FileBitmap = FileBitmap;
- pStream->BitmapSize = BitmapSize;
- pStream->BlockSize = Footer.BlockSize;
- pStream->BlockCount = BlockCount;
- pStream->IsComplete = FlatStream_CheckFile(pStream);
- return true;
- }
- }
- }
- }
- }
-
- return false;
-}
-
-static void FlatStream_UpdateBitmap(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG StartOffset,
- ULONGLONG EndOffset)
-{
- LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap;
- DWORD BlockIndex;
- DWORD BlockSize = pStream->BlockSize;
- DWORD ByteIndex;
- BYTE BitMask;
-
- // Sanity checks
- assert((StartOffset & (BlockSize - 1)) == 0);
- assert(FileBitmap != NULL);
-
- // Calculate the index of the block
- BlockIndex = (DWORD)(StartOffset / BlockSize);
- ByteIndex = (BlockIndex / 0x08);
- BitMask = (BYTE)(1 << (BlockIndex & 0x07));
-
- // Set all bits for the specified range
- while(StartOffset < EndOffset)
- {
- // Set the bit
- FileBitmap[ByteIndex] |= BitMask;
-
- // Move all
- StartOffset += BlockSize;
- ByteIndex += (BitMask >> 0x07);
- BitMask = (BitMask >> 0x07) | (BitMask << 0x01);
- }
-
- // Increment the bitmap update count
- pStream->IsModified = 1;
-}
-
-static bool FlatStream_BlockCheck(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG BlockOffset)
-{
- LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap;
- DWORD BlockIndex;
- BYTE BitMask;
-
- // Sanity checks
- assert((BlockOffset & (pStream->BlockSize - 1)) == 0);
- assert(FileBitmap != NULL);
-
- // Calculate the index of the block
- BlockIndex = (DWORD)(BlockOffset / pStream->BlockSize);
- BitMask = (BYTE)(1 << (BlockIndex & 0x07));
-
- // Check if the bit is present
- return (FileBitmap[BlockIndex / 0x08] & BitMask) ? true : false;
-}
-
-static bool FlatStream_BlockRead(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG StartOffset,
- ULONGLONG EndOffset,
- LPBYTE BlockBuffer,
- DWORD BytesNeeded,
- bool bAvailable)
-{
- DWORD BytesToRead = (DWORD)(EndOffset - StartOffset);
-
- // The starting offset must be aligned to size of the block
- assert(pStream->FileBitmap != NULL);
- assert((StartOffset & (pStream->BlockSize - 1)) == 0);
- assert(StartOffset < EndOffset);
-
- // If the blocks are not available, we need to load them from the master
- // and then save to the mirror
- if(bAvailable == false)
- {
- // If we have no master, we cannot satisfy read request
- if(pStream->pMaster == NULL)
- return false;
-
- // Load the blocks from the master stream
- // Note that we always have to read complete blocks
- // so they get properly stored to the mirror stream
- if(!FileStream_Read(pStream->pMaster, &StartOffset, BlockBuffer, BytesToRead))
- return false;
-
- // Store the loaded blocks to the mirror file.
- // Note that this operation is not required to succeed
- if(pStream->BaseWrite(pStream, &StartOffset, BlockBuffer, BytesToRead))
- FlatStream_UpdateBitmap(pStream, StartOffset, EndOffset);
-
- return true;
- }
- else
- {
- if(BytesToRead > BytesNeeded)
- BytesToRead = BytesNeeded;
- return pStream->BaseRead(pStream, &StartOffset, BlockBuffer, BytesToRead);
- }
-}
-
-static void FlatStream_Close(TBlockStream * pStream)
-{
- FILE_BITMAP_FOOTER Footer;
-
- if(pStream->FileBitmap && pStream->IsModified)
- {
- // Write the file bitmap
- pStream->BaseWrite(pStream, &pStream->StreamSize, pStream->FileBitmap, pStream->BitmapSize);
-
- // Prepare and write the file footer
- Footer.Signature = ID_FILE_BITMAP_FOOTER;
- Footer.Version = 3;
- Footer.BuildNumber = pStream->BuildNumber;
- Footer.MapOffsetLo = (DWORD)(pStream->StreamSize & 0xFFFFFFFF);
- Footer.MapOffsetHi = (DWORD)(pStream->StreamSize >> 0x20);
- Footer.BlockSize = pStream->BlockSize;
- BSWAP_ARRAY32_UNSIGNED(&Footer, sizeof(FILE_BITMAP_FOOTER));
- pStream->BaseWrite(pStream, NULL, &Footer, sizeof(FILE_BITMAP_FOOTER));
- }
-
- // Close the base class
- BlockStream_Close(pStream);
-}
-
-static bool FlatStream_CreateMirror(TBlockStream * pStream)
-{
- ULONGLONG MasterSize = 0;
- ULONGLONG MirrorSize = 0;
- LPBYTE FileBitmap = NULL;
- DWORD dwBitmapSize;
- DWORD dwBlockCount;
- bool bNeedCreateMirrorStream = true;
- bool bNeedResizeMirrorStream = true;
-
- // Do we have master function and base creation function?
- if(pStream->pMaster == NULL || pStream->BaseCreate == NULL)
- return false;
-
- // Retrieve the master file size, block count and bitmap size
- FileStream_GetSize(pStream->pMaster, &MasterSize);
- dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE);
- dwBitmapSize = (DWORD)((dwBlockCount + 7) / 8);
-
- // Setup stream size and position
- pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really???
- pStream->StreamSize = MasterSize;
- pStream->StreamPos = 0;
-
- // Open the base stream for write access
- if(pStream->BaseOpen(pStream, pStream->szFileName, 0))
- {
- // If the file open succeeded, check if the file size matches required size
- pStream->BaseGetSize(pStream, &MirrorSize);
- if(MirrorSize == MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER))
- {
- // Attempt to load an existing file bitmap
- if(FlatStream_LoadBitmap(pStream))
- return true;
-
- // We need to create new file bitmap
- bNeedResizeMirrorStream = false;
- }
-
- // We need to create mirror stream
- bNeedCreateMirrorStream = false;
- }
-
- // Create a new stream, if needed
- if(bNeedCreateMirrorStream)
- {
- if(!pStream->BaseCreate(pStream))
- return false;
- }
-
- // If we need to, then resize the mirror stream
- if(bNeedResizeMirrorStream)
- {
- if(!pStream->BaseResize(pStream, MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER)))
- return false;
- }
-
- // Allocate the bitmap array
- FileBitmap = STORM_ALLOC(BYTE, dwBitmapSize);
- if(FileBitmap == NULL)
- return false;
-
- // Initialize the bitmap
- memset(FileBitmap, 0, dwBitmapSize);
- pStream->FileBitmap = FileBitmap;
- pStream->BitmapSize = dwBitmapSize;
- pStream->BlockSize = DEFAULT_BLOCK_SIZE;
- pStream->BlockCount = dwBlockCount;
- pStream->IsComplete = 0;
- pStream->IsModified = 1;
-
- // Note: Don't write the stream bitmap right away.
- // Doing so would cause sparse file resize on NTFS,
- // which would take long time on larger files.
- return true;
-}
-
-static TFileStream * FlatStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags)
-{
- TBlockStream * pStream;
- ULONGLONG ByteOffset = 0;
-
- // Create new empty stream
- pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
- if(pStream == NULL)
- return NULL;
-
- // Do we have a master stream?
- if(pStream->pMaster != NULL)
- {
- if(!FlatStream_CreateMirror(pStream))
- {
- FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
- return NULL;
- }
- }
- else
- {
- // Attempt to open the base stream
- if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags))
- {
- FileStream_Close(pStream);
- return NULL;
- }
-
- // Load the bitmap, if required to
- if(dwStreamFlags & STREAM_FLAG_USE_BITMAP)
- FlatStream_LoadBitmap(pStream);
- }
-
- // If we have a stream bitmap, set the reading functions
- // which check presence of each file block
- if(pStream->FileBitmap != NULL)
- {
- // Set the stream position to zero. Stream size is already set
- assert(pStream->StreamSize != 0);
- pStream->StreamPos = 0;
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-
- // Supply the stream functions
- pStream->StreamRead = (STREAM_READ)BlockStream_Read;
- pStream->StreamGetSize = BlockStream_GetSize;
- pStream->StreamGetPos = BlockStream_GetPos;
- pStream->StreamClose = (STREAM_CLOSE)FlatStream_Close;
-
- // Supply the block functions
- pStream->BlockCheck = (BLOCK_CHECK)FlatStream_BlockCheck;
- pStream->BlockRead = (BLOCK_READ)FlatStream_BlockRead;
- }
- else
- {
- // Reset the base position to zero
- pStream->BaseRead(pStream, &ByteOffset, NULL, 0);
-
- // Setup stream size and position
- pStream->StreamSize = pStream->Base.File.FileSize;
- pStream->StreamPos = 0;
-
- // Set the base functions
- pStream->StreamRead = pStream->BaseRead;
- pStream->StreamWrite = pStream->BaseWrite;
- pStream->StreamResize = pStream->BaseResize;
- pStream->StreamGetSize = pStream->BaseGetSize;
- pStream->StreamGetPos = pStream->BaseGetPos;
- pStream->StreamClose = pStream->BaseClose;
- }
-
- return pStream;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - partial stream support
-
-static bool IsPartHeader(PPART_FILE_HEADER pPartHdr)
-{
- // Version number must be 2
- if(pPartHdr->PartialVersion == 2)
- {
- // GameBuildNumber must be an ASCII number
- if(isdigit(pPartHdr->GameBuildNumber[0]) && isdigit(pPartHdr->GameBuildNumber[1]) && isdigit(pPartHdr->GameBuildNumber[2]))
- {
- // Block size must be power of 2
- if((pPartHdr->BlockSize & (pPartHdr->BlockSize - 1)) == 0)
- return true;
- }
- }
-
- return false;
-}
-
-static DWORD PartStream_CheckFile(TBlockStream * pStream)
-{
- PPART_FILE_MAP_ENTRY FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap;
- DWORD dwBlockCount;
-
- // Get the number of blocks
- dwBlockCount = (DWORD)((pStream->StreamSize + pStream->BlockSize - 1) / pStream->BlockSize);
-
- // Check all blocks
- for(DWORD i = 0; i < dwBlockCount; i++, FileBitmap++)
- {
- // Few sanity checks
- assert(FileBitmap->LargeValueHi == 0);
- assert(FileBitmap->LargeValueLo == 0);
- assert(FileBitmap->Flags == 0 || FileBitmap->Flags == 3);
-
- // Check if this block is present
- if(FileBitmap->Flags != 3)
- return 0;
- }
-
- // Yes, the file is complete
- return 1;
-}
-
-static bool PartStream_LoadBitmap(TBlockStream * pStream)
-{
- PPART_FILE_MAP_ENTRY FileBitmap;
- PART_FILE_HEADER PartHdr;
- ULONGLONG ByteOffset = 0;
- ULONGLONG StreamSize = 0;
- DWORD BlockCount;
- DWORD BitmapSize;
-
- // Only if the size is greater than size of the bitmap header
- if(pStream->Base.File.FileSize > sizeof(PART_FILE_HEADER))
- {
- // Attempt to read PART file header
- if(pStream->BaseRead(pStream, &ByteOffset, &PartHdr, sizeof(PART_FILE_HEADER)))
- {
- // We need to swap PART file header on big-endian platforms
- BSWAP_ARRAY32_UNSIGNED(&PartHdr, sizeof(PART_FILE_HEADER));
-
- // Verify the PART file header
- if(IsPartHeader(&PartHdr))
- {
- // Get the number of blocks and size of one block
- StreamSize = MAKE_OFFSET64(PartHdr.FileSizeHi, PartHdr.FileSizeLo);
- ByteOffset = sizeof(PART_FILE_HEADER);
- BlockCount = (DWORD)((StreamSize + PartHdr.BlockSize - 1) / PartHdr.BlockSize);
- BitmapSize = BlockCount * sizeof(PART_FILE_MAP_ENTRY);
-
- // Check if sizes match
- if((ByteOffset + BitmapSize) < pStream->Base.File.FileSize)
- {
- // Allocate space for the array of PART_FILE_MAP_ENTRY
- FileBitmap = STORM_ALLOC(PART_FILE_MAP_ENTRY, BlockCount);
- if(FileBitmap != NULL)
- {
- // Load the block map
- if(!pStream->BaseRead(pStream, &ByteOffset, FileBitmap, BitmapSize))
- {
- STORM_FREE(FileBitmap);
- return false;
- }
-
- // Make sure that the byte order is correct
- BSWAP_ARRAY32_UNSIGNED(FileBitmap, BitmapSize);
-
- // Update the stream size
- pStream->BuildNumber = StringToInt(PartHdr.GameBuildNumber);
- pStream->StreamSize = StreamSize;
-
- // Fill the bitmap information
- pStream->FileBitmap = FileBitmap;
- pStream->BitmapSize = BitmapSize;
- pStream->BlockSize = PartHdr.BlockSize;
- pStream->BlockCount = BlockCount;
- pStream->IsComplete = PartStream_CheckFile(pStream);
- return true;
- }
- }
- }
- }
- }
-
- return false;
-}
-
-static void PartStream_UpdateBitmap(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG StartOffset,
- ULONGLONG EndOffset,
- ULONGLONG RealOffset)
-{
- PPART_FILE_MAP_ENTRY FileBitmap;
- DWORD BlockSize = pStream->BlockSize;
-
- // Sanity checks
- assert((StartOffset & (BlockSize - 1)) == 0);
- assert(pStream->FileBitmap != NULL);
-
- // Calculate the first entry in the block map
- FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + (StartOffset / BlockSize);
-
- // Set all bits for the specified range
- while(StartOffset < EndOffset)
- {
- // Set the bit
- FileBitmap->BlockOffsHi = (DWORD)(RealOffset >> 0x20);
- FileBitmap->BlockOffsLo = (DWORD)(RealOffset & 0xFFFFFFFF);
- FileBitmap->Flags = 3;
-
- // Move all
- StartOffset += BlockSize;
- RealOffset += BlockSize;
- FileBitmap++;
- }
-
- // Increment the bitmap update count
- pStream->IsModified = 1;
-}
-
-static bool PartStream_BlockCheck(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG BlockOffset)
-{
- PPART_FILE_MAP_ENTRY FileBitmap;
-
- // Sanity checks
- assert((BlockOffset & (pStream->BlockSize - 1)) == 0);
- assert(pStream->FileBitmap != NULL);
-
- // Calculate the block map entry
- FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + (BlockOffset / pStream->BlockSize);
-
- // Check if the flags are present
- return (FileBitmap->Flags & 0x03) ? true : false;
-}
-
-static bool PartStream_BlockRead(
- TBlockStream * pStream,
- ULONGLONG StartOffset,
- ULONGLONG EndOffset,
- LPBYTE BlockBuffer,
- DWORD BytesNeeded,
- bool bAvailable)
-{
- PPART_FILE_MAP_ENTRY FileBitmap;
- ULONGLONG ByteOffset;
- DWORD BytesToRead;
- DWORD BlockIndex = (DWORD)(StartOffset / pStream->BlockSize);
-
- // The starting offset must be aligned to size of the block
- assert(pStream->FileBitmap != NULL);
- assert((StartOffset & (pStream->BlockSize - 1)) == 0);
- assert(StartOffset < EndOffset);
-
- // If the blocks are not available, we need to load them from the master
- // and then save to the mirror
- if(bAvailable == false)
- {
- // If we have no master, we cannot satisfy read request
- if(pStream->pMaster == NULL)
- return false;
-
- // Load the blocks from the master stream
- // Note that we always have to read complete blocks
- // so they get properly stored to the mirror stream
- BytesToRead = (DWORD)(EndOffset - StartOffset);
- if(!FileStream_Read(pStream->pMaster, &StartOffset, BlockBuffer, BytesToRead))
- return false;
-
- // The loaded blocks are going to be stored to the end of the file
- // Note that this operation is not required to succeed
- if(pStream->BaseGetSize(pStream, &ByteOffset))
- {
- // Store the loaded blocks to the mirror file.
- if(pStream->BaseWrite(pStream, &ByteOffset, BlockBuffer, BytesToRead))
- {
- PartStream_UpdateBitmap(pStream, StartOffset, EndOffset, ByteOffset);
- }
- }
- }
- else
- {
- // Get the file map entry
- FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + BlockIndex;
-
- // Read all blocks
- while(StartOffset < EndOffset)
- {
- // Get the number of bytes to be read
- BytesToRead = (DWORD)(EndOffset - StartOffset);
- if(BytesToRead > pStream->BlockSize)
- BytesToRead = pStream->BlockSize;
- if(BytesToRead > BytesNeeded)
- BytesToRead = BytesNeeded;
-
- // Read the block
- ByteOffset = MAKE_OFFSET64(FileBitmap->BlockOffsHi, FileBitmap->BlockOffsLo);
- if(!pStream->BaseRead(pStream, &ByteOffset, BlockBuffer, BytesToRead))
- return false;
-
- // Move the pointers
- StartOffset += pStream->BlockSize;
- BlockBuffer += pStream->BlockSize;
- BytesNeeded -= pStream->BlockSize;
- FileBitmap++;
- }
- }
-
- return true;
-}
-
-static void PartStream_Close(TBlockStream * pStream)
-{
- PART_FILE_HEADER PartHeader;
- ULONGLONG ByteOffset = 0;
-
- if(pStream->FileBitmap && pStream->IsModified)
- {
- // Prepare the part file header
- memset(&PartHeader, 0, sizeof(PART_FILE_HEADER));
- PartHeader.PartialVersion = 2;
- PartHeader.FileSizeHi = (DWORD)(pStream->StreamSize >> 0x20);
- PartHeader.FileSizeLo = (DWORD)(pStream->StreamSize & 0xFFFFFFFF);
- PartHeader.BlockSize = pStream->BlockSize;
-
- // Make sure that the header is properly BSWAPed
- BSWAP_ARRAY32_UNSIGNED(&PartHeader, sizeof(PART_FILE_HEADER));
- sprintf(PartHeader.GameBuildNumber, "%u", (unsigned int)pStream->BuildNumber);
-
- // Write the part header
- pStream->BaseWrite(pStream, &ByteOffset, &PartHeader, sizeof(PART_FILE_HEADER));
-
- // Write the block bitmap
- BSWAP_ARRAY32_UNSIGNED(pStream->FileBitmap, pStream->BitmapSize);
- pStream->BaseWrite(pStream, NULL, pStream->FileBitmap, pStream->BitmapSize);
- }
-
- // Close the base class
- BlockStream_Close(pStream);
-}
-
-static bool PartStream_CreateMirror(TBlockStream * pStream)
-{
- ULONGLONG RemainingSize;
- ULONGLONG MasterSize = 0;
- ULONGLONG MirrorSize = 0;
- LPBYTE FileBitmap = NULL;
- DWORD dwBitmapSize;
- DWORD dwBlockCount;
- bool bNeedCreateMirrorStream = true;
- bool bNeedResizeMirrorStream = true;
-
- // Do we have master function and base creation function?
- if(pStream->pMaster == NULL || pStream->BaseCreate == NULL)
- return false;
-
- // Retrieve the master file size, block count and bitmap size
- FileStream_GetSize(pStream->pMaster, &MasterSize);
- dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE);
- dwBitmapSize = (DWORD)(dwBlockCount * sizeof(PART_FILE_MAP_ENTRY));
-
- // Setup stream size and position
- pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really???
- pStream->StreamSize = MasterSize;
- pStream->StreamPos = 0;
-
- // Open the base stream for write access
- if(pStream->BaseOpen(pStream, pStream->szFileName, 0))
- {
- // If the file open succeeded, check if the file size matches required size
- pStream->BaseGetSize(pStream, &MirrorSize);
- if(MirrorSize >= sizeof(PART_FILE_HEADER) + dwBitmapSize)
- {
- // Check if the remaining size is aligned to block
- RemainingSize = MirrorSize - sizeof(PART_FILE_HEADER) - dwBitmapSize;
- if((RemainingSize & (DEFAULT_BLOCK_SIZE - 1)) == 0 || RemainingSize == MasterSize)
- {
- // Attempt to load an existing file bitmap
- if(PartStream_LoadBitmap(pStream))
- return true;
- }
- }
-
- // We need to create mirror stream
- bNeedCreateMirrorStream = false;
- }
-
- // Create a new stream, if needed
- if(bNeedCreateMirrorStream)
- {
- if(!pStream->BaseCreate(pStream))
- return false;
- }
-
- // If we need to, then resize the mirror stream
- if(bNeedResizeMirrorStream)
- {
- if(!pStream->BaseResize(pStream, sizeof(PART_FILE_HEADER) + dwBitmapSize))
- return false;
- }
-
- // Allocate the bitmap array
- FileBitmap = STORM_ALLOC(BYTE, dwBitmapSize);
- if(FileBitmap == NULL)
- return false;
-
- // Initialize the bitmap
- memset(FileBitmap, 0, dwBitmapSize);
- pStream->FileBitmap = FileBitmap;
- pStream->BitmapSize = dwBitmapSize;
- pStream->BlockSize = DEFAULT_BLOCK_SIZE;
- pStream->BlockCount = dwBlockCount;
- pStream->IsComplete = 0;
- pStream->IsModified = 1;
-
- // Note: Don't write the stream bitmap right away.
- // Doing so would cause sparse file resize on NTFS,
- // which would take long time on larger files.
- return true;
-}
-
-
-static TFileStream * PartStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags)
-{
- TBlockStream * pStream;
-
- // Create new empty stream
- pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
- if(pStream == NULL)
- return NULL;
-
- // Do we have a master stream?
- if(pStream->pMaster != NULL)
- {
- if(!PartStream_CreateMirror(pStream))
- {
- FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
- return NULL;
- }
- }
- else
- {
- // Attempt to open the base stream
- if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags))
- {
- FileStream_Close(pStream);
- return NULL;
- }
-
- // Load the part stream block map
- if(!PartStream_LoadBitmap(pStream))
- {
- FileStream_Close(pStream);
- SetLastError(ERROR_BAD_FORMAT);
- return NULL;
- }
- }
-
- // Set the stream position to zero. Stream size is already set
- assert(pStream->StreamSize != 0);
- pStream->StreamPos = 0;
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-
- // Set new function pointers
- pStream->StreamRead = (STREAM_READ)BlockStream_Read;
- pStream->StreamGetPos = BlockStream_GetPos;
- pStream->StreamGetSize = BlockStream_GetSize;
- pStream->StreamClose = (STREAM_CLOSE)PartStream_Close;
-
- // Supply the block functions
- pStream->BlockCheck = (BLOCK_CHECK)PartStream_BlockCheck;
- pStream->BlockRead = (BLOCK_READ)PartStream_BlockRead;
- return pStream;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - MPQE stream support
-
-static const char * szKeyTemplate = "expand 32-byte k000000000000000000000000000000000000000000000000";
-
-static const char * AuthCodeArray[] =
-{
- // Starcraft II (Heart of the Swarm)
- // Authentication code URL: http://dist.blizzard.com/mediakey/hots-authenticationcode-bgdl.txt
- // -0C- -1C--08- -18--04- -14--00- -10-
- "S48B6CDTN5XEQAKQDJNDLJBJ73FDFM3U", // SC2 Heart of the Swarm-all : "expand 32-byte kQAKQ0000FM3UN5XE000073FD6CDT0000LJBJS48B0000DJND"
-
- // Diablo III: Agent.exe (1.0.0.954)
- // Address of decryption routine: 00502b00
- // Pointer to decryptor object: ECX
- // Pointer to key: ECX+0x5C
- // Authentication code URL: http://dist.blizzard.com/mediakey/d3-authenticationcode-enGB.txt
- // -0C- -1C--08- -18--04- -14--00- -10-
- "UCMXF6EJY352EFH4XFRXCFH2XC9MQRZK", // Diablo III Installer (deDE): "expand 32-byte kEFH40000QRZKY3520000XC9MF6EJ0000CFH2UCMX0000XFRX"
- "MMKVHY48RP7WXP4GHYBQ7SL9J9UNPHBP", // Diablo III Installer (enGB): "expand 32-byte kXP4G0000PHBPRP7W0000J9UNHY4800007SL9MMKV0000HYBQ"
- "8MXLWHQ7VGGLTZ9MQZQSFDCLJYET3CPP", // Diablo III Installer (enSG): "expand 32-byte kTZ9M00003CPPVGGL0000JYETWHQ70000FDCL8MXL0000QZQS"
- "EJ2R5TM6XFE2GUNG5QDGHKQ9UAKPWZSZ", // Diablo III Installer (enUS): "expand 32-byte kGUNG0000WZSZXFE20000UAKP5TM60000HKQ9EJ2R00005QDG"
- "PBGFBE42Z6LNK65UGJQ3WZVMCLP4HQQT", // Diablo III Installer (esES): "expand 32-byte kK65U0000HQQTZ6LN0000CLP4BE420000WZVMPBGF0000GJQ3"
- "X7SEJJS9TSGCW5P28EBSC47AJPEY8VU2", // Diablo III Installer (esMX): "expand 32-byte kW5P200008VU2TSGC0000JPEYJJS90000C47AX7SE00008EBS"
- "5KVBQA8VYE6XRY3DLGC5ZDE4XS4P7YA2", // Diablo III Installer (frFR): "expand 32-byte kRY3D00007YA2YE6X0000XS4PQA8V0000ZDE45KVB0000LGC5"
- "478JD2K56EVNVVY4XX8TDWYT5B8KB254", // Diablo III Installer (itIT): "expand 32-byte kVVY40000B2546EVN00005B8KD2K50000DWYT478J0000XX8T"
- "8TS4VNFQRZTN6YWHE9CHVDH9NVWD474A", // Diablo III Installer (koKR): "expand 32-byte k6YWH0000474ARZTN0000NVWDVNFQ0000VDH98TS40000E9CH"
- "LJ52Z32DF4LZ4ZJJXVKK3AZQA6GABLJB", // Diablo III Installer (plPL): "expand 32-byte k4ZJJ0000BLJBF4LZ0000A6GAZ32D00003AZQLJ520000XVKK"
- "K6BDHY2ECUE2545YKNLBJPVYWHE7XYAG", // Diablo III Installer (ptBR): "expand 32-byte k545Y0000XYAGCUE20000WHE7HY2E0000JPVYK6BD0000KNLB"
- "NDVW8GWLAYCRPGRNY8RT7ZZUQU63VLPR", // Diablo III Installer (ruRU): "expand 32-byte kXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
- "6VWCQTN8V3ZZMRUCZXV8A8CGUX2TAA8H", // Diablo III Installer (zhTW): "expand 32-byte kMRUC0000AA8HV3ZZ0000UX2TQTN80000A8CG6VWC0000ZXV8"
-// "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // Diablo III Installer (zhCN): "expand 32-byte kXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
-
- // Starcraft II (Wings of Liberty): Installer.exe (4.1.1.4219)
- // Address of decryption routine: 0053A3D0
- // Pointer to decryptor object: ECX
- // Pointer to key: ECX+0x5C
- // Authentication code URL: http://dist.blizzard.com/mediakey/sc2-authenticationcode-enUS.txt
- // -0C- -1C--08- -18--04- -14--00- -10-
- "Y45MD3CAK4KXSSXHYD9VY64Z8EKJ4XFX", // SC2 Wings of Liberty (deDE): "expand 32-byte kSSXH00004XFXK4KX00008EKJD3CA0000Y64ZY45M0000YD9V"
- "G8MN8UDG6NA2ANGY6A3DNY82HRGF29ZH", // SC2 Wings of Liberty (enGB): "expand 32-byte kANGY000029ZH6NA20000HRGF8UDG0000NY82G8MN00006A3D"
- "W9RRHLB2FDU9WW5B3ECEBLRSFWZSF7HW", // SC2 Wings of Liberty (enSG): "expand 32-byte kWW5B0000F7HWFDU90000FWZSHLB20000BLRSW9RR00003ECE"
- "3DH5RE5NVM5GTFD85LXGWT6FK859ETR5", // SC2 Wings of Liberty (enUS): "expand 32-byte kTFD80000ETR5VM5G0000K859RE5N0000WT6F3DH500005LXG"
- "8WLKUAXE94PFQU4Y249PAZ24N4R4XKTQ", // SC2 Wings of Liberty (esES): "expand 32-byte kQU4Y0000XKTQ94PF0000N4R4UAXE0000AZ248WLK0000249P"
- "A34DXX3VHGGXSQBRFE5UFFDXMF9G4G54", // SC2 Wings of Liberty (esMX): "expand 32-byte kSQBR00004G54HGGX0000MF9GXX3V0000FFDXA34D0000FE5U"
- "ZG7J9K938HJEFWPQUA768MA2PFER6EAJ", // SC2 Wings of Liberty (frFR): "expand 32-byte kFWPQ00006EAJ8HJE0000PFER9K9300008MA2ZG7J0000UA76"
- "NE7CUNNNTVAPXV7E3G2BSVBWGVMW8BL2", // SC2 Wings of Liberty (itIT): "expand 32-byte kXV7E00008BL2TVAP0000GVMWUNNN0000SVBWNE7C00003G2B"
- "3V9E2FTMBM9QQWK7U6MAMWAZWQDB838F", // SC2 Wings of Liberty (koKR): "expand 32-byte kQWK70000838FBM9Q0000WQDB2FTM0000MWAZ3V9E0000U6MA"
- "2NSFB8MELULJ83U6YHA3UP6K4MQD48L6", // SC2 Wings of Liberty (plPL): "expand 32-byte k83U6000048L6LULJ00004MQDB8ME0000UP6K2NSF0000YHA3"
- "QA2TZ9EWZ4CUU8BMB5WXCTY65F9CSW4E", // SC2 Wings of Liberty (ptBR): "expand 32-byte kU8BM0000SW4EZ4CU00005F9CZ9EW0000CTY6QA2T0000B5WX"
- "VHB378W64BAT9SH7D68VV9NLQDK9YEGT", // SC2 Wings of Liberty (ruRU): "expand 32-byte k9SH70000YEGT4BAT0000QDK978W60000V9NLVHB30000D68V"
- "U3NFQJV4M6GC7KBN9XQJ3BRDN3PLD9NE", // SC2 Wings of Liberty (zhTW): "expand 32-byte k7KBN0000D9NEM6GC0000N3PLQJV400003BRDU3NF00009XQJ"
-
- NULL
-};
-
-static DWORD Rol32(DWORD dwValue, DWORD dwRolCount)
-{
- DWORD dwShiftRight = 32 - dwRolCount;
-
- return (dwValue << dwRolCount) | (dwValue >> dwShiftRight);
-}
-
-static void CreateKeyFromAuthCode(
- LPBYTE pbKeyBuffer,
- const char * szAuthCode)
-{
- LPDWORD KeyPosition = (LPDWORD)(pbKeyBuffer + 0x10);
- LPDWORD AuthCode32 = (LPDWORD)szAuthCode;
-
- memcpy(pbKeyBuffer, szKeyTemplate, MPQE_CHUNK_SIZE);
- KeyPosition[0x00] = AuthCode32[0x03];
- KeyPosition[0x02] = AuthCode32[0x07];
- KeyPosition[0x03] = AuthCode32[0x02];
- KeyPosition[0x05] = AuthCode32[0x06];
- KeyPosition[0x06] = AuthCode32[0x01];
- KeyPosition[0x08] = AuthCode32[0x05];
- KeyPosition[0x09] = AuthCode32[0x00];
- KeyPosition[0x0B] = AuthCode32[0x04];
- BSWAP_ARRAY32_UNSIGNED(pbKeyBuffer, MPQE_CHUNK_SIZE);
-}
-
-static void DecryptFileChunk(
- DWORD * MpqData,
- LPBYTE pbKey,
- ULONGLONG ByteOffset,
- DWORD dwLength)
-{
- ULONGLONG ChunkOffset;
- DWORD KeyShuffled[0x10];
- DWORD KeyMirror[0x10];
- DWORD RoundCount = 0x14;
-
- // Prepare the key
- ChunkOffset = ByteOffset / MPQE_CHUNK_SIZE;
- memcpy(KeyMirror, pbKey, MPQE_CHUNK_SIZE);
- BSWAP_ARRAY32_UNSIGNED(KeyMirror, MPQE_CHUNK_SIZE);
- KeyMirror[0x05] = (DWORD)(ChunkOffset >> 32);
- KeyMirror[0x08] = (DWORD)(ChunkOffset);
-
- while(dwLength >= MPQE_CHUNK_SIZE)
- {
- // Shuffle the key - part 1
- KeyShuffled[0x0E] = KeyMirror[0x00];
- KeyShuffled[0x0C] = KeyMirror[0x01];
- KeyShuffled[0x05] = KeyMirror[0x02];
- KeyShuffled[0x0F] = KeyMirror[0x03];
- KeyShuffled[0x0A] = KeyMirror[0x04];
- KeyShuffled[0x07] = KeyMirror[0x05];
- KeyShuffled[0x0B] = KeyMirror[0x06];
- KeyShuffled[0x09] = KeyMirror[0x07];
- KeyShuffled[0x03] = KeyMirror[0x08];
- KeyShuffled[0x06] = KeyMirror[0x09];
- KeyShuffled[0x08] = KeyMirror[0x0A];
- KeyShuffled[0x0D] = KeyMirror[0x0B];
- KeyShuffled[0x02] = KeyMirror[0x0C];
- KeyShuffled[0x04] = KeyMirror[0x0D];
- KeyShuffled[0x01] = KeyMirror[0x0E];
- KeyShuffled[0x00] = KeyMirror[0x0F];
-
- // Shuffle the key - part 2
- for(DWORD i = 0; i < RoundCount; i += 2)
- {
- KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x02]), 0x07);
- KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0E]), 0x09);
- KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x0A]), 0x0D);
- KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x03]), 0x12);
-
- KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x04]), 0x07);
- KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x0C]), 0x09);
- KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x07]), 0x0D);
- KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x06]), 0x12);
-
- KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x01]), 0x07);
- KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x05]), 0x09);
- KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x0B]), 0x0D);
- KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x08]), 0x12);
-
- KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x00]), 0x07);
- KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x0F]), 0x09);
- KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x09]), 0x0D);
- KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x0D]), 0x12);
-
- KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x09]), 0x07);
- KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x0E]), 0x09);
- KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x04]), 0x0D);
- KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x08]), 0x12);
-
- KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x0A]), 0x07);
- KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x0C]), 0x09);
- KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x01]), 0x0D);
- KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0D]), 0x12);
-
- KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x07]), 0x07);
- KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x05]), 0x09);
- KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x00]), 0x0D);
- KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x03]), 0x12);
-
- KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x0B]), 0x07);
- KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x0F]), 0x09);
- KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x02]), 0x0D);
- KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x06]), 0x12);
- }
-
- // Decrypt one data chunk
- BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE);
- MpqData[0x00] = MpqData[0x00] ^ (KeyShuffled[0x0E] + KeyMirror[0x00]);
- MpqData[0x01] = MpqData[0x01] ^ (KeyShuffled[0x04] + KeyMirror[0x0D]);
- MpqData[0x02] = MpqData[0x02] ^ (KeyShuffled[0x08] + KeyMirror[0x0A]);
- MpqData[0x03] = MpqData[0x03] ^ (KeyShuffled[0x09] + KeyMirror[0x07]);
- MpqData[0x04] = MpqData[0x04] ^ (KeyShuffled[0x0A] + KeyMirror[0x04]);
- MpqData[0x05] = MpqData[0x05] ^ (KeyShuffled[0x0C] + KeyMirror[0x01]);
- MpqData[0x06] = MpqData[0x06] ^ (KeyShuffled[0x01] + KeyMirror[0x0E]);
- MpqData[0x07] = MpqData[0x07] ^ (KeyShuffled[0x0D] + KeyMirror[0x0B]);
- MpqData[0x08] = MpqData[0x08] ^ (KeyShuffled[0x03] + KeyMirror[0x08]);
- MpqData[0x09] = MpqData[0x09] ^ (KeyShuffled[0x07] + KeyMirror[0x05]);
- MpqData[0x0A] = MpqData[0x0A] ^ (KeyShuffled[0x05] + KeyMirror[0x02]);
- MpqData[0x0B] = MpqData[0x0B] ^ (KeyShuffled[0x00] + KeyMirror[0x0F]);
- MpqData[0x0C] = MpqData[0x0C] ^ (KeyShuffled[0x02] + KeyMirror[0x0C]);
- MpqData[0x0D] = MpqData[0x0D] ^ (KeyShuffled[0x06] + KeyMirror[0x09]);
- MpqData[0x0E] = MpqData[0x0E] ^ (KeyShuffled[0x0B] + KeyMirror[0x06]);
- MpqData[0x0F] = MpqData[0x0F] ^ (KeyShuffled[0x0F] + KeyMirror[0x03]);
- BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE);
-
- // Update byte offset in the key
- KeyMirror[0x08]++;
- if(KeyMirror[0x08] == 0)
- KeyMirror[0x05]++;
-
- // Move pointers and decrease number of bytes to decrypt
- MpqData += (MPQE_CHUNK_SIZE / sizeof(DWORD));
- dwLength -= MPQE_CHUNK_SIZE;
- }
-}
-
-static bool MpqeStream_DetectFileKey(TEncryptedStream * pStream)
-{
- ULONGLONG ByteOffset = 0;
- BYTE EncryptedHeader[MPQE_CHUNK_SIZE];
- BYTE FileHeader[MPQE_CHUNK_SIZE];
-
- // Read the first file chunk
- if(pStream->BaseRead(pStream, &ByteOffset, EncryptedHeader, sizeof(EncryptedHeader)))
- {
- // We just try all known keys one by one
- for(int i = 0; AuthCodeArray[i] != NULL; i++)
- {
- // Prepare they decryption key from game serial number
- CreateKeyFromAuthCode(pStream->Key, AuthCodeArray[i]);
-
- // Try to decrypt with the given key
- memcpy(FileHeader, EncryptedHeader, MPQE_CHUNK_SIZE);
- DecryptFileChunk((LPDWORD)FileHeader, pStream->Key, ByteOffset, MPQE_CHUNK_SIZE);
-
- // We check the decrypted data
- // All known encrypted MPQs have header at the begin of the file,
- // so we check for MPQ signature there.
- if(FileHeader[0] == 'M' && FileHeader[1] == 'P' && FileHeader[2] == 'Q')
- {
- // Update the stream size
- pStream->StreamSize = pStream->Base.File.FileSize;
-
- // Fill the block information
- pStream->BlockSize = MPQE_CHUNK_SIZE;
- pStream->BlockCount = (DWORD)(pStream->Base.File.FileSize + MPQE_CHUNK_SIZE - 1) / MPQE_CHUNK_SIZE;
- pStream->IsComplete = 1;
- return true;
- }
- }
- }
-
- // Key not found, sorry
- return false;
-}
-
-static bool MpqeStream_BlockRead(
- TEncryptedStream * pStream,
- ULONGLONG StartOffset,
- ULONGLONG EndOffset,
- LPBYTE BlockBuffer,
- DWORD BytesNeeded,
- bool bAvailable)
-{
- DWORD dwBytesToRead;
-
- assert((StartOffset & (pStream->BlockSize - 1)) == 0);
- assert(StartOffset < EndOffset);
- assert(bAvailable != false);
- BytesNeeded = BytesNeeded;
- bAvailable = bAvailable;
-
- // Read the file from the stream as-is
- // Limit the reading to number of blocks really needed
- dwBytesToRead = (DWORD)(EndOffset - StartOffset);
- if(!pStream->BaseRead(pStream, &StartOffset, BlockBuffer, dwBytesToRead))
- return false;
-
- // Decrypt the data
- dwBytesToRead = (dwBytesToRead + MPQE_CHUNK_SIZE - 1) & ~(MPQE_CHUNK_SIZE - 1);
- DecryptFileChunk((LPDWORD)BlockBuffer, pStream->Key, StartOffset, dwBytesToRead);
- return true;
-}
-
-static TFileStream * MpqeStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags)
-{
- TEncryptedStream * pStream;
-
- // Create new empty stream
- pStream = (TEncryptedStream *)AllocateFileStream(szFileName, sizeof(TEncryptedStream), dwStreamFlags);
- if(pStream == NULL)
- return NULL;
-
- // Attempt to open the base stream
- assert(pStream->BaseOpen != NULL);
- if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags))
- return NULL;
-
- // Determine the encryption key for the MPQ
- if(MpqeStream_DetectFileKey(pStream))
- {
- // Set the stream position and size
- assert(pStream->StreamSize != 0);
- pStream->StreamPos = 0;
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-
- // Set new function pointers
- pStream->StreamRead = (STREAM_READ)BlockStream_Read;
- pStream->StreamGetPos = BlockStream_GetPos;
- pStream->StreamGetSize = BlockStream_GetSize;
- pStream->StreamClose = pStream->BaseClose;
-
- // Supply the block functions
- pStream->BlockRead = (BLOCK_READ)MpqeStream_BlockRead;
- return pStream;
- }
-
- // Cleanup the stream and return
- FileStream_Close(pStream);
- SetLastError(ERROR_UNKNOWN_FILE_KEY);
- return NULL;
-}
-
-//-----------------------------------------------------------------------------
-// Local functions - Block4 stream support
-
-#define BLOCK4_BLOCK_SIZE 0x4000 // Size of one block
-#define BLOCK4_HASH_SIZE 0x20 // Size of MD5 hash that is after each block
-#define BLOCK4_MAX_BLOCKS 0x00002000 // Maximum amount of blocks per file
-#define BLOCK4_MAX_FSIZE 0x08040000 // Max size of one file
-
-static bool Block4Stream_BlockRead(
- TBlockStream * pStream, // Pointer to an open stream
- ULONGLONG StartOffset,
- ULONGLONG EndOffset,
- LPBYTE BlockBuffer,
- DWORD BytesNeeded,
- bool bAvailable)
-{
- TBaseProviderData * BaseArray = (TBaseProviderData *)pStream->FileBitmap;
- ULONGLONG ByteOffset;
- DWORD BytesToRead;
- DWORD StreamIndex;
- DWORD BlockIndex;
- bool bResult;
-
- // The starting offset must be aligned to size of the block
- assert(pStream->FileBitmap != NULL);
- assert((StartOffset & (pStream->BlockSize - 1)) == 0);
- assert(StartOffset < EndOffset);
- assert(bAvailable == true);
-
- // Keep compiler happy
- bAvailable = bAvailable;
- EndOffset = EndOffset;
-
- while(BytesNeeded != 0)
- {
- // Calculate the block index and the file index
- StreamIndex = (DWORD)((StartOffset / pStream->BlockSize) / BLOCK4_MAX_BLOCKS);
- BlockIndex = (DWORD)((StartOffset / pStream->BlockSize) % BLOCK4_MAX_BLOCKS);
- if(StreamIndex > pStream->BitmapSize)
- return false;
-
- // Calculate the block offset
- ByteOffset = ((ULONGLONG)BlockIndex * (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE));
- BytesToRead = STORMLIB_MIN(BytesNeeded, BLOCK4_BLOCK_SIZE);
-
- // Read from the base stream
- pStream->Base = BaseArray[StreamIndex];
- bResult = pStream->BaseRead(pStream, &ByteOffset, BlockBuffer, BytesToRead);
- BaseArray[StreamIndex] = pStream->Base;
-
- // Did the result succeed?
- if(bResult == false)
- return false;
-
- // Move pointers
- StartOffset += BytesToRead;
- BlockBuffer += BytesToRead;
- BytesNeeded -= BytesToRead;
- }
-
- return true;
-}
-
-
-static void Block4Stream_Close(TBlockStream * pStream)
-{
- TBaseProviderData * BaseArray = (TBaseProviderData *)pStream->FileBitmap;
-
- // If we have a non-zero count of base streams,
- // we have to close them all
- if(BaseArray != NULL)
- {
- // Close all base streams
- for(DWORD i = 0; i < pStream->BitmapSize; i++)
- {
- memcpy(&pStream->Base, BaseArray + i, sizeof(TBaseProviderData));
- pStream->BaseClose(pStream);
- }
- }
-
- // Free the data map, if any
- if(pStream->FileBitmap != NULL)
- STORM_FREE(pStream->FileBitmap);
- pStream->FileBitmap = NULL;
-
- // Do not call the BaseClose function,
- // we closed all handles already
- return;
-}
-
-static TFileStream * Block4Stream_Open(const TCHAR * szFileName, DWORD dwStreamFlags)
-{
- TBaseProviderData * NewBaseArray = NULL;
- ULONGLONG RemainderBlock;
- ULONGLONG BlockCount;
- ULONGLONG FileSize;
- TBlockStream * pStream;
- TCHAR * szNameBuff;
- size_t nNameLength;
- DWORD dwBaseFiles = 0;
- DWORD dwBaseFlags;
-
- // Create new empty stream
- pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
- if(pStream == NULL)
- return NULL;
-
- // Sanity check
- assert(pStream->BaseOpen != NULL);
-
- // Get the length of the file name without numeric suffix
- nNameLength = _tcslen(pStream->szFileName);
- if(pStream->szFileName[nNameLength - 2] == '.' && pStream->szFileName[nNameLength - 1] == '0')
- nNameLength -= 2;
- pStream->szFileName[nNameLength] = 0;
-
- // Supply the stream functions
- pStream->StreamRead = (STREAM_READ)BlockStream_Read;
- pStream->StreamGetSize = BlockStream_GetSize;
- pStream->StreamGetPos = BlockStream_GetPos;
- pStream->StreamClose = (STREAM_CLOSE)Block4Stream_Close;
- pStream->BlockRead = (BLOCK_READ)Block4Stream_BlockRead;
-
- // Allocate work space for numeric names
- szNameBuff = STORM_ALLOC(TCHAR, nNameLength + 4);
- if(szNameBuff != NULL)
- {
- // Set the base flags
- dwBaseFlags = (dwStreamFlags & STREAM_PROVIDERS_MASK) | STREAM_FLAG_READ_ONLY;
-
- // Go all suffixes from 0 to 30
- for(int nSuffix = 0; nSuffix < 30; nSuffix++)
- {
- // Open the n-th file
- _stprintf(szNameBuff, _T("%s.%u"), pStream->szFileName, nSuffix);
- if(!pStream->BaseOpen(pStream, szNameBuff, dwBaseFlags))
- break;
-
- // If the open succeeded, we re-allocate the base provider array
- NewBaseArray = STORM_ALLOC(TBaseProviderData, dwBaseFiles + 1);
- if(NewBaseArray == NULL)
- {
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
- return NULL;
- }
-
- // Copy the old base data array to the new base data array
- if(pStream->FileBitmap != NULL)
- {
- memcpy(NewBaseArray, pStream->FileBitmap, sizeof(TBaseProviderData) * dwBaseFiles);
- STORM_FREE(pStream->FileBitmap);
- }
-
- // Also copy the opened base array
- memcpy(NewBaseArray + dwBaseFiles, &pStream->Base, sizeof(TBaseProviderData));
- pStream->FileBitmap = NewBaseArray;
- dwBaseFiles++;
-
- // Get the size of the base stream
- pStream->BaseGetSize(pStream, &FileSize);
- assert(FileSize <= BLOCK4_MAX_FSIZE);
- RemainderBlock = FileSize % (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE);
- BlockCount = FileSize / (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE);
-
- // Increment the stream size and number of blocks
- pStream->StreamSize += (BlockCount * BLOCK4_BLOCK_SIZE);
- pStream->BlockCount += (DWORD)BlockCount;
-
- // Is this the last file?
- if(FileSize < BLOCK4_MAX_FSIZE)
- {
- if(RemainderBlock)
- {
- pStream->StreamSize += (RemainderBlock - BLOCK4_HASH_SIZE);
- pStream->BlockCount++;
- }
- break;
- }
- }
-
- // Fill the remainining block stream variables
- pStream->BitmapSize = dwBaseFiles;
- pStream->BlockSize = BLOCK4_BLOCK_SIZE;
- pStream->IsComplete = 1;
- pStream->IsModified = 0;
-
- // Fill the remaining stream variables
- pStream->StreamPos = 0;
- pStream->dwFlags |= STREAM_FLAG_READ_ONLY;
-
- STORM_FREE(szNameBuff);
- }
-
- // If we opened something, return success
- if(dwBaseFiles == 0)
- {
- FileStream_Close(pStream);
- SetLastError(ERROR_FILE_NOT_FOUND);
- pStream = NULL;
- }
-
- return pStream;
-}
-
-//-----------------------------------------------------------------------------
-// Public functions
-
-/**
- * This function creates a new file for read-write access
- *
- * - If the current platform supports file sharing,
- * the file must be created for read sharing (i.e. another application
- * can open the file for read, but not for write)
- * - If the file does not exist, the function must create new one
- * - If the file exists, the function must rewrite it and set to zero size
- * - The parameters of the function must be validate by the caller
- * - The function must initialize all stream function pointers in TFileStream
- * - If the function fails from any reason, it must close all handles
- * and free all memory that has been allocated in the process of stream creation,
- * including the TFileStream structure itself
- *
- * \a szFileName Name of the file to create
- */
-
-TFileStream * FileStream_CreateFile(
- const TCHAR * szFileName,
- DWORD dwStreamFlags)
-{
- TFileStream * pStream;
-
- // We only support creation of flat, local file
- if((dwStreamFlags & (STREAM_PROVIDERS_MASK)) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
- {
- SetLastError(ERROR_NOT_SUPPORTED);
- return NULL;
- }
-
- // Allocate file stream structure for flat stream
- pStream = AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags);
- if(pStream != NULL)
- {
- // Attempt to create the disk file
- if(BaseFile_Create(pStream))
- {
- // Fill the stream provider functions
- pStream->StreamRead = pStream->BaseRead;
- pStream->StreamWrite = pStream->BaseWrite;
- pStream->StreamResize = pStream->BaseResize;
- pStream->StreamGetSize = pStream->BaseGetSize;
- pStream->StreamGetPos = pStream->BaseGetPos;
- pStream->StreamClose = pStream->BaseClose;
- return pStream;
- }
-
- // File create failed, delete the stream
- STORM_FREE(pStream);
- pStream = NULL;
- }
-
- // Return the stream
- return pStream;
-}
-
-/**
- * This function opens an existing file for read or read-write access
- * - If the current platform supports file sharing,
- * the file must be open for read sharing (i.e. another application
- * can open the file for read, but not for write)
- * - If the file does not exist, the function must return NULL
- * - If the file exists but cannot be open, then function must return NULL
- * - The parameters of the function must be validate by the caller
- * - The function must initialize all stream function pointers in TFileStream
- * - If the function fails from any reason, it must close all handles
- * and free all memory that has been allocated in the process of stream creation,
- * including the TFileStream structure itself
- *
- * \a szFileName Name of the file to open
- * \a dwStreamFlags specifies the provider and base storage type
- */
-
-TFileStream * FileStream_OpenFile(
- const TCHAR * szFileName,
- DWORD dwStreamFlags)
-{
- DWORD dwProvider = dwStreamFlags & STREAM_PROVIDERS_MASK;
- size_t nPrefixLength = FileStream_Prefix(szFileName, &dwProvider);
-
- // Re-assemble the stream flags
- dwStreamFlags = (dwStreamFlags & STREAM_OPTIONS_MASK) | dwProvider;
- szFileName += nPrefixLength;
-
- // Perform provider-specific open
- switch(dwStreamFlags & STREAM_PROVIDER_MASK)
- {
- case STREAM_PROVIDER_FLAT:
- return FlatStream_Open(szFileName, dwStreamFlags);
-
- case STREAM_PROVIDER_PARTIAL:
- return PartStream_Open(szFileName, dwStreamFlags);
-
- case STREAM_PROVIDER_MPQE:
- return MpqeStream_Open(szFileName, dwStreamFlags);
-
- case STREAM_PROVIDER_BLOCK4:
- return Block4Stream_Open(szFileName, dwStreamFlags);
-
- default:
- SetLastError(ERROR_INVALID_PARAMETER);
- return NULL;
- }
-}
-
-/**
- * Returns the file name of the stream
- *
- * \a pStream Pointer to an open stream
- */
-const TCHAR * FileStream_GetFileName(TFileStream * pStream)
-{
- assert(pStream != NULL);
- return pStream->szFileName;
-}
-
-/**
- * Returns the length of the provider prefix. Returns zero if no prefix
- *
- * \a szFileName Pointer to a stream name (file, mapped file, URL)
- * \a pdwStreamProvider Pointer to a DWORD variable that receives stream provider (STREAM_PROVIDER_XXX)
- */
-
-size_t FileStream_Prefix(const TCHAR * szFileName, DWORD * pdwProvider)
-{
- size_t nPrefixLength1 = 0;
- size_t nPrefixLength2 = 0;
- DWORD dwProvider = 0;
-
- if(szFileName != NULL)
- {
- //
- // Determine the stream provider
- //
-
- if(!_tcsnicmp(szFileName, _T("flat-"), 5))
- {
- dwProvider |= STREAM_PROVIDER_FLAT;
- nPrefixLength1 = 5;
- }
-
- else if(!_tcsnicmp(szFileName, _T("part-"), 5))
- {
- dwProvider |= STREAM_PROVIDER_PARTIAL;
- nPrefixLength1 = 5;
- }
-
- else if(!_tcsnicmp(szFileName, _T("mpqe-"), 5))
- {
- dwProvider |= STREAM_PROVIDER_MPQE;
- nPrefixLength1 = 5;
- }
-
- else if(!_tcsnicmp(szFileName, _T("blk4-"), 5))
- {
- dwProvider |= STREAM_PROVIDER_BLOCK4;
- nPrefixLength1 = 5;
- }
-
- //
- // Determine the base provider
- //
-
- if(!_tcsnicmp(szFileName+nPrefixLength1, _T("file:"), 5))
- {
- dwProvider |= BASE_PROVIDER_FILE;
- nPrefixLength2 = 5;
- }
-
- else if(!_tcsnicmp(szFileName+nPrefixLength1, _T("map:"), 4))
- {
- dwProvider |= BASE_PROVIDER_MAP;
- nPrefixLength2 = 4;
- }
-
- else if(!_tcsnicmp(szFileName+nPrefixLength1, _T("http:"), 5))
- {
- dwProvider |= BASE_PROVIDER_HTTP;
- nPrefixLength2 = 5;
- }
-
- // Only accept stream provider if we recognized the base provider
- if(nPrefixLength2 != 0)
- {
- // It is also allowed to put "//" after the base provider, e.g. "file://", "http://"
- if(szFileName[nPrefixLength1+nPrefixLength2] == '/' && szFileName[nPrefixLength1+nPrefixLength2+1] == '/')
- nPrefixLength2 += 2;
-
- if(pdwProvider != NULL)
- *pdwProvider = dwProvider;
- return nPrefixLength1 + nPrefixLength2;
- }
- }
-
- return 0;
-}
-
-/**
- * Sets a download callback. Whenever the stream needs to download one or more blocks
- * from the server, the callback is called
- *
- * \a pStream Pointer to an open stream
- * \a pfnCallback Pointer to callback function
- * \a pvUserData Arbitrary user pointer passed to the download callback
- */
-
-bool FileStream_SetCallback(TFileStream * pStream, SFILE_DOWNLOAD_CALLBACK pfnCallback, void * pvUserData)
-{
- TBlockStream * pBlockStream = (TBlockStream *)pStream;
-
- if(pStream->BlockRead == NULL)
- {
- SetLastError(ERROR_NOT_SUPPORTED);
- return false;
- }
-
- pBlockStream->pfnCallback = pfnCallback;
- pBlockStream->UserData = pvUserData;
- return true;
-}
-
-/**
- * This function gives the block map. The 'pvBitmap' pointer must point to a buffer
- * of at least sizeof(STREAM_BLOCK_MAP) size. It can also have size of the complete
- * block map (i.e. sizeof(STREAM_BLOCK_MAP) + BitmapSize). In that case, the function
- * also copies the bit-based block map.
- *
- * \a pStream Pointer to an open stream
- * \a pvBitmap Pointer to buffer where the block map will be stored
- * \a cbBitmap Length of the buffer, of the block map
- * \a cbLengthNeeded Length of the bitmap, in bytes
- */
-
-bool FileStream_GetBitmap(TFileStream * pStream, void * pvBitmap, DWORD cbBitmap, DWORD * pcbLengthNeeded)
-{
- TStreamBitmap * pBitmap = (TStreamBitmap *)pvBitmap;
- TBlockStream * pBlockStream = (TBlockStream *)pStream;
- ULONGLONG BlockOffset;
- LPBYTE Bitmap = (LPBYTE)(pBitmap + 1);
- DWORD BitmapSize;
- DWORD BlockCount;
- DWORD BlockSize;
- bool bResult = false;
-
- // Retrieve the size of one block
- if(pStream->BlockCheck != NULL)
- {
- BlockCount = pBlockStream->BlockCount;
- BlockSize = pBlockStream->BlockSize;
- }
- else
- {
- BlockCount = (DWORD)((pStream->StreamSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE);
- BlockSize = DEFAULT_BLOCK_SIZE;
- }
-
- // Fill-in the variables
- BitmapSize = (BlockCount + 7) / 8;
-
- // Give the number of blocks
- if(pcbLengthNeeded != NULL)
- pcbLengthNeeded[0] = sizeof(TStreamBitmap) + BitmapSize;
-
- // If the length of the buffer is not enough
- if(pvBitmap != NULL && cbBitmap != 0)
- {
- // Give the STREAM_BLOCK_MAP structure
- if(cbBitmap >= sizeof(TStreamBitmap))
- {
- pBitmap->StreamSize = pStream->StreamSize;
- pBitmap->BitmapSize = BitmapSize;
- pBitmap->BlockCount = BlockCount;
- pBitmap->BlockSize = BlockSize;
- pBitmap->IsComplete = (pStream->BlockCheck != NULL) ? pBlockStream->IsComplete : 1;
- bResult = true;
- }
-
- // Give the block bitmap, if enough space
- if(cbBitmap >= sizeof(TStreamBitmap) + BitmapSize)
- {
- // Version with bitmap present
- if(pStream->BlockCheck != NULL)
- {
- DWORD ByteIndex = 0;
- BYTE BitMask = 0x01;
-
- // Initialize the map with zeros
- memset(Bitmap, 0, BitmapSize);
-
- // Fill the map
- for(BlockOffset = 0; BlockOffset < pStream->StreamSize; BlockOffset += BlockSize)
- {
- // Set the bit if the block is present
- if(pBlockStream->BlockCheck(pStream, BlockOffset))
- Bitmap[ByteIndex] |= BitMask;
-
- // Move bit position
- ByteIndex += (BitMask >> 0x07);
- BitMask = (BitMask >> 0x07) | (BitMask << 0x01);
- }
- }
- else
- {
- memset(Bitmap, 0xFF, BitmapSize);
- }
- }
- }
-
- // Set last error value and return
- if(bResult == false)
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- return bResult;
-}
-
-/**
- * Reads data from the stream
- *
- * - Returns true if the read operation succeeded and all bytes have been read
- * - Returns false if either read failed or not all bytes have been read
- * - If the pByteOffset is NULL, the function must read the data from the current file position
- * - The function can be called with dwBytesToRead = 0. In that case, pvBuffer is ignored
- * and the function just adjusts file pointer.
- *
- * \a pStream Pointer to an open stream
- * \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position
- * \a pvBuffer Pointer to data to be read
- * \a dwBytesToRead Number of bytes to read from the file
- *
- * \returns
- * - If the function reads the required amount of bytes, it returns true.
- * - If the function reads less than required bytes, it returns false and GetLastError() returns ERROR_HANDLE_EOF
- * - If the function fails, it reads false and GetLastError() returns an error code different from ERROR_HANDLE_EOF
- */
-bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead)
-{
- assert(pStream->StreamRead != NULL);
- return pStream->StreamRead(pStream, pByteOffset, pvBuffer, dwBytesToRead);
-}
-
-/**
- * This function writes data to the stream
- *
- * - Returns true if the write operation succeeded and all bytes have been written
- * - Returns false if either write failed or not all bytes have been written
- * - If the pByteOffset is NULL, the function must write the data to the current file position
- *
- * \a pStream Pointer to an open stream
- * \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position
- * \a pvBuffer Pointer to data to be written
- * \a dwBytesToWrite Number of bytes to write to the file
- */
-bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite)
-{
- if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
- {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
-
- assert(pStream->StreamWrite != NULL);
- return pStream->StreamWrite(pStream, pByteOffset, pvBuffer, dwBytesToWrite);
-}
-
-/**
- * Returns the size of a file
- *
- * \a pStream Pointer to an open stream
- * \a FileSize Pointer where to store the file size
- */
-bool FileStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize)
-{
- assert(pStream->StreamGetSize != NULL);
- return pStream->StreamGetSize(pStream, pFileSize);
-}
-
-/**
- * Sets the size of a file
- *
- * \a pStream Pointer to an open stream
- * \a NewFileSize File size to set
- */
-bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize)
-{
- if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
- {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
-
- assert(pStream->StreamResize != NULL);
- return pStream->StreamResize(pStream, NewFileSize);
-}
-
-/**
- * This function returns the current file position
- * \a pStream
- * \a pByteOffset
- */
-bool FileStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset)
-{
- assert(pStream->StreamGetPos != NULL);
- return pStream->StreamGetPos(pStream, pByteOffset);
-}
-
-/**
- * Returns the last write time of a file
- *
- * \a pStream Pointer to an open stream
- * \a pFileType Pointer where to store the file last write time
- */
-bool FileStream_GetTime(TFileStream * pStream, ULONGLONG * pFileTime)
-{
- // Just use the saved filetime value
- *pFileTime = pStream->Base.File.FileTime;
- return true;
-}
-
-/**
- * Returns the stream flags
- *
- * \a pStream Pointer to an open stream
- * \a pdwStreamFlags Pointer where to store the stream flags
- */
-bool FileStream_GetFlags(TFileStream * pStream, LPDWORD pdwStreamFlags)
-{
- *pdwStreamFlags = pStream->dwFlags;
- return true;
-}
-
-/**
- * Switches a stream with another. Used for final phase of archive compacting.
- * Performs these steps:
- *
- * 1) Closes the handle to the existing MPQ
- * 2) Renames the temporary MPQ to the original MPQ, overwrites existing one
- * 3) Opens the MPQ stores the handle and stream position to the new stream structure
- *
- * \a pStream Pointer to an open stream
- * \a pNewStream Temporary ("working") stream (created during archive compacting)
- */
-bool FileStream_Replace(TFileStream * pStream, TFileStream * pNewStream)
-{
- // Only supported on flat files
- if((pStream->dwFlags & STREAM_PROVIDERS_MASK) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE))
- {
- SetLastError(ERROR_NOT_SUPPORTED);
- return false;
- }
-
- // Not supported on read-only streams
- if(pStream->dwFlags & STREAM_FLAG_READ_ONLY)
- {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
-
- // Close both stream's base providers
- pNewStream->BaseClose(pNewStream);
- pStream->BaseClose(pStream);
-
- // Now we have to delete the (now closed) old file and rename the new file
- if(!BaseFile_Replace(pStream, pNewStream))
- return false;
-
- // Now open the base file again
- if(!BaseFile_Open(pStream, pStream->szFileName, pStream->dwFlags))
- return false;
-
- // Cleanup the new stream
- FileStream_Close(pNewStream);
- return true;
-}
-
-/**
- * This function closes an archive file and frees any data buffers
- * that have been allocated for stream management. The function must also
- * support partially allocated structure, i.e. one or more buffers
- * can be NULL, if there was an allocation failure during the process
- *
- * \a pStream Pointer to an open stream
- */
-void FileStream_Close(TFileStream * pStream)
-{
- // Check if the stream structure is allocated at all
- if(pStream != NULL)
- {
- // Free the master stream, if any
- if(pStream->pMaster != NULL)
- FileStream_Close(pStream->pMaster);
- pStream->pMaster = NULL;
-
- // Close the stream provider.
- if(pStream->StreamClose != NULL)
- pStream->StreamClose(pStream);
-
- // Also close base stream, if any
- else if(pStream->BaseClose != NULL)
- pStream->BaseClose(pStream);
-
- // Free the stream itself
- STORM_FREE(pStream);
- }
-}
-
-//-----------------------------------------------------------------------------
-// Utility functions (ANSI)
-
-const char * GetPlainFileName(const char * szFileName)
-{
- const char * szPlainName = szFileName;
-
- while(*szFileName != 0)
- {
- if(*szFileName == '\\' || *szFileName == '/')
- szPlainName = szFileName + 1;
- szFileName++;
- }
-
- return szPlainName;
-}
-
-void CopyFileName(char * szTarget, const char * szSource, size_t cchLength)
-{
- memcpy(szTarget, szSource, cchLength);
- szTarget[cchLength] = 0;
-}
-
-//-----------------------------------------------------------------------------
-// Utility functions (UNICODE) only exist in the ANSI version of the library
-// In ANSI builds, TCHAR = char, so we don't need these functions implemented
-
-#ifdef _UNICODE
-const TCHAR * GetPlainFileName(const TCHAR * szFileName)
-{
- const TCHAR * szPlainName = szFileName;
-
- while(*szFileName != 0)
- {
- if(*szFileName == '\\' || *szFileName == '/')
- szPlainName = szFileName + 1;
- szFileName++;
- }
-
- return szPlainName;
-}
-
-void CopyFileName(TCHAR * szTarget, const char * szSource, size_t cchLength)
-{
- mbstowcs(szTarget, szSource, cchLength);
- szTarget[cchLength] = 0;
-}
-
-void CopyFileName(char * szTarget, const TCHAR * szSource, size_t cchLength)
-{
- wcstombs(szTarget, szSource, cchLength);
- szTarget[cchLength] = 0;
-}
-#endif
-
-//-----------------------------------------------------------------------------
-// main - for testing purposes
-
-#ifdef __STORMLIB_TEST__
-int FileStream_Test(const TCHAR * szFileName, DWORD dwStreamFlags)
-{
- TFileStream * pStream;
- TMPQHeader MpqHeader;
- ULONGLONG FilePos;
- TMPQBlock * pBlock;
- TMPQHash * pHash;
-
- InitializeMpqCryptography();
-
- pStream = FileStream_OpenFile(szFileName, dwStreamFlags);
- if(pStream == NULL)
- return GetLastError();
-
- // Read the MPQ header
- FileStream_Read(pStream, NULL, &MpqHeader, MPQ_HEADER_SIZE_V2);
- if(MpqHeader.dwID != ID_MPQ)
- return ERROR_FILE_CORRUPT;
-
- // Read the hash table
- pHash = STORM_ALLOC(TMPQHash, MpqHeader.dwHashTableSize);
- if(pHash != NULL)
- {
- FilePos = MpqHeader.dwHashTablePos;
- FileStream_Read(pStream, &FilePos, pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash));
- DecryptMpqBlock(pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash), MPQ_KEY_HASH_TABLE);
- STORM_FREE(pHash);
- }
-
- // Read the block table
- pBlock = STORM_ALLOC(TMPQBlock, MpqHeader.dwBlockTableSize);
- if(pBlock != NULL)
- {
- FilePos = MpqHeader.dwBlockTablePos;
- FileStream_Read(pStream, &FilePos, pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock));
- DecryptMpqBlock(pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock), MPQ_KEY_BLOCK_TABLE);
- STORM_FREE(pBlock);
- }
-
- FileStream_Close(pStream);
- return ERROR_SUCCESS;
-}
-#endif
+/*****************************************************************************/ +/* FileStream.cpp Copyright (c) Ladislav Zezula 2010 */ +/*---------------------------------------------------------------------------*/ +/* File stream support for StormLib */ +/* */ +/* Windows support: Written by Ladislav Zezula */ +/* Mac support: Written by Sam Wilkins */ +/* Linux support: Written by Sam Wilkins and Ivan Komissarov */ +/* Big-endian: Written & debugged by Sam Wilkins */ +/*---------------------------------------------------------------------------*/ +/* Date Ver Who Comment */ +/* -------- ---- --- ------- */ +/* 11.06.10 1.00 Lad Derived from StormPortMac.cpp and StormPortLinux.cpp */ +/*****************************************************************************/ + +#define __STORMLIB_SELF__ +#include "StormLib.h" +#include "StormCommon.h" +#include "FileStream.h" + +#ifdef _MSC_VER +#pragma comment(lib, "wininet.lib") // Internet functions for HTTP stream +#pragma warning(disable: 4800) // 'BOOL' : forcing value to bool 'true' or 'false' (performance warning) +#endif + +//----------------------------------------------------------------------------- +// Local defines + +#ifndef INVALID_HANDLE_VALUE +#define INVALID_HANDLE_VALUE ((HANDLE)-1) +#endif + +//----------------------------------------------------------------------------- +// Local functions - platform-specific functions + +#ifndef PLATFORM_WINDOWS +static int nLastError = ERROR_SUCCESS; + +int GetLastError() +{ + return nLastError; +} + +void SetLastError(int nError) +{ + nLastError = nError; +} +#endif + +static DWORD StringToInt(const char * szString) +{ + DWORD dwValue = 0; + + while('0' <= szString[0] && szString[0] <= '9') + { + dwValue = (dwValue * 10) + (szString[0] - '9'); + szString++; + } + + return dwValue; +} + +//----------------------------------------------------------------------------- +// Dummy init function + +static void BaseNone_Init(TFileStream *) +{ + // Nothing here +} + +//----------------------------------------------------------------------------- +// Local functions - base file support + +static bool BaseFile_Create(TFileStream * pStream) +{ +#ifdef PLATFORM_WINDOWS + { + DWORD dwWriteShare = (pStream->dwFlags & STREAM_FLAG_WRITE_SHARE) ? FILE_SHARE_WRITE : 0; + + pStream->Base.File.hFile = CreateFile(pStream->szFileName, + GENERIC_READ | GENERIC_WRITE, + dwWriteShare | FILE_SHARE_READ, + NULL, + CREATE_ALWAYS, + 0, + NULL); + if(pStream->Base.File.hFile == INVALID_HANDLE_VALUE) + return false; + } +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + { + intptr_t handle; + + handle = open(pStream->szFileName, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if(handle == -1) + { + nLastError = errno; + return false; + } + + pStream->Base.File.hFile = (HANDLE)handle; + } +#endif + + // Reset the file size and position + pStream->Base.File.FileSize = 0; + pStream->Base.File.FilePos = 0; + return true; +} + +static bool BaseFile_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags) +{ +#ifdef PLATFORM_WINDOWS + { + ULARGE_INTEGER FileSize; + DWORD dwWriteAccess = (dwStreamFlags & STREAM_FLAG_READ_ONLY) ? 0 : FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES; + DWORD dwWriteShare = (dwStreamFlags & STREAM_FLAG_WRITE_SHARE) ? FILE_SHARE_WRITE : 0; + + // Open the file + pStream->Base.File.hFile = CreateFile(szFileName, + FILE_READ_DATA | FILE_READ_ATTRIBUTES | dwWriteAccess, + FILE_SHARE_READ | dwWriteShare, + NULL, + OPEN_EXISTING, + 0, + NULL); + if(pStream->Base.File.hFile == INVALID_HANDLE_VALUE) + return false; + + // Query the file size + FileSize.LowPart = GetFileSize(pStream->Base.File.hFile, &FileSize.HighPart); + pStream->Base.File.FileSize = FileSize.QuadPart; + + // Query last write time + GetFileTime(pStream->Base.File.hFile, NULL, NULL, (LPFILETIME)&pStream->Base.File.FileTime); + } +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + { + struct stat64 fileinfo; + int oflag = (dwStreamFlags & STREAM_FLAG_READ_ONLY) ? O_RDONLY : O_RDWR; + intptr_t handle; + + // Open the file + handle = open(szFileName, oflag | O_LARGEFILE); + if(handle == -1) + { + nLastError = errno; + return false; + } + + // Get the file size + if(fstat64(handle, &fileinfo) == -1) + { + nLastError = errno; + return false; + } + + // time_t is number of seconds since 1.1.1970, UTC. + // 1 second = 10000000 (decimal) in FILETIME + // Set the start to 1.1.1970 00:00:00 + pStream->Base.File.FileTime = 0x019DB1DED53E8000ULL + (10000000 * fileinfo.st_mtime); + pStream->Base.File.FileSize = (ULONGLONG)fileinfo.st_size; + pStream->Base.File.hFile = (HANDLE)handle; + } +#endif + + // Reset the file position + pStream->Base.File.FilePos = 0; + return true; +} + +static bool BaseFile_Read( + TFileStream * pStream, // Pointer to an open stream + ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position + void * pvBuffer, // Pointer to data to be read + DWORD dwBytesToRead) // Number of bytes to read from the file +{ + ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.File.FilePos; + DWORD dwBytesRead = 0; // Must be set by platform-specific code + +#ifdef PLATFORM_WINDOWS + { + // Note: StormLib no longer supports Windows 9x. + // Thus, we can use the OVERLAPPED structure to specify + // file offset to read from file. This allows us to skip + // one system call to SetFilePointer + + // Update the byte offset + pStream->Base.File.FilePos = ByteOffset; + + // Read the data + if(dwBytesToRead != 0) + { + OVERLAPPED Overlapped; + + Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32); + Overlapped.Offset = (DWORD)ByteOffset; + Overlapped.hEvent = NULL; + if(!ReadFile(pStream->Base.File.hFile, pvBuffer, dwBytesToRead, &dwBytesRead, &Overlapped)) + return false; + } + } +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + { + ssize_t bytes_read; + + // If the byte offset is different from the current file position, + // we have to update the file position xxx + if(ByteOffset != pStream->Base.File.FilePos) + { + lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET); + pStream->Base.File.FilePos = ByteOffset; + } + + // Perform the read operation + if(dwBytesToRead != 0) + { + bytes_read = read((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToRead); + if(bytes_read == -1) + { + nLastError = errno; + return false; + } + + dwBytesRead = (DWORD)(size_t)bytes_read; + } + } +#endif + + // Increment the current file position by number of bytes read + // If the number of bytes read doesn't match to required amount, return false + pStream->Base.File.FilePos = ByteOffset + dwBytesRead; + if(dwBytesRead != dwBytesToRead) + SetLastError(ERROR_HANDLE_EOF); + return (dwBytesRead == dwBytesToRead); +} + +/** + * \a pStream Pointer to an open stream + * \a pByteOffset Pointer to file byte offset. If NULL, writes to current position + * \a pvBuffer Pointer to data to be written + * \a dwBytesToWrite Number of bytes to write to the file + */ + +static bool BaseFile_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite) +{ + ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.File.FilePos; + DWORD dwBytesWritten = 0; // Must be set by platform-specific code + +#ifdef PLATFORM_WINDOWS + { + // Note: StormLib no longer supports Windows 9x. + // Thus, we can use the OVERLAPPED structure to specify + // file offset to read from file. This allows us to skip + // one system call to SetFilePointer + + // Update the byte offset + pStream->Base.File.FilePos = ByteOffset; + + // Read the data + if(dwBytesToWrite != 0) + { + OVERLAPPED Overlapped; + + Overlapped.OffsetHigh = (DWORD)(ByteOffset >> 32); + Overlapped.Offset = (DWORD)ByteOffset; + Overlapped.hEvent = NULL; + if(!WriteFile(pStream->Base.File.hFile, pvBuffer, dwBytesToWrite, &dwBytesWritten, &Overlapped)) + return false; + } + } +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + { + ssize_t bytes_written; + + // If the byte offset is different from the current file position, + // we have to update the file position + if(ByteOffset != pStream->Base.File.FilePos) + { + lseek64((intptr_t)pStream->Base.File.hFile, (off64_t)(ByteOffset), SEEK_SET); + pStream->Base.File.FilePos = ByteOffset; + } + + // Perform the read operation + bytes_written = write((intptr_t)pStream->Base.File.hFile, pvBuffer, (size_t)dwBytesToWrite); + if(bytes_written == -1) + { + nLastError = errno; + return false; + } + + dwBytesWritten = (DWORD)(size_t)bytes_written; + } +#endif + + // Increment the current file position by number of bytes read + pStream->Base.File.FilePos = ByteOffset + dwBytesWritten; + + // Also modify the file size, if needed + if(pStream->Base.File.FilePos > pStream->Base.File.FileSize) + pStream->Base.File.FileSize = pStream->Base.File.FilePos; + + if(dwBytesWritten != dwBytesToWrite) + SetLastError(ERROR_DISK_FULL); + return (dwBytesWritten == dwBytesToWrite); +} + +/** + * \a pStream Pointer to an open stream + * \a NewFileSize New size of the file + */ +static bool BaseFile_Resize(TFileStream * pStream, ULONGLONG NewFileSize) +{ +#ifdef PLATFORM_WINDOWS + { + LONG FileSizeHi = (LONG)(NewFileSize >> 32); + LONG FileSizeLo; + DWORD dwNewPos; + bool bResult; + + // Set the position at the new file size + dwNewPos = SetFilePointer(pStream->Base.File.hFile, (LONG)NewFileSize, &FileSizeHi, FILE_BEGIN); + if(dwNewPos == INVALID_SET_FILE_POINTER && GetLastError() != ERROR_SUCCESS) + return false; + + // Set the current file pointer as the end of the file + bResult = (bool)SetEndOfFile(pStream->Base.File.hFile); + if(bResult) + pStream->Base.File.FileSize = NewFileSize; + + // Restore the file position + FileSizeHi = (LONG)(pStream->Base.File.FilePos >> 32); + FileSizeLo = (LONG)(pStream->Base.File.FilePos); + SetFilePointer(pStream->Base.File.hFile, FileSizeLo, &FileSizeHi, FILE_BEGIN); + return bResult; + } +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + { + if(ftruncate64((intptr_t)pStream->Base.File.hFile, (off64_t)NewFileSize) == -1) + { + nLastError = errno; + return false; + } + + pStream->Base.File.FileSize = NewFileSize; + return true; + } +#endif +} + +// Gives the current file size +static bool BaseFile_GetSize(TFileStream * pStream, ULONGLONG * pFileSize) +{ + // Note: Used by all thre base providers. + // Requires the TBaseData union to have the same layout for all three base providers + *pFileSize = pStream->Base.File.FileSize; + return true; +} + +// Gives the current file position +static bool BaseFile_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset) +{ + // Note: Used by all thre base providers. + // Requires the TBaseData union to have the same layout for all three base providers + *pByteOffset = pStream->Base.File.FilePos; + return true; +} + +// Renames the file pointed by pStream so that it contains data from pNewStream +static bool BaseFile_Replace(TFileStream * pStream, TFileStream * pNewStream) +{ +#ifdef PLATFORM_WINDOWS + // Delete the original stream file. Don't check the result value, + // because if the file doesn't exist, it would fail + DeleteFile(pStream->szFileName); + + // Rename the new file to the old stream's file + return (bool)MoveFile(pNewStream->szFileName, pStream->szFileName); +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + // "rename" on Linux also works if the target file exists + if(rename(pNewStream->szFileName, pStream->szFileName) == -1) + { + nLastError = errno; + return false; + } + + return true; +#endif +} + +static void BaseFile_Close(TFileStream * pStream) +{ + if(pStream->Base.File.hFile != INVALID_HANDLE_VALUE) + { +#ifdef PLATFORM_WINDOWS + CloseHandle(pStream->Base.File.hFile); +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + close((intptr_t)pStream->Base.File.hFile); +#endif + } + + // Also invalidate the handle + pStream->Base.File.hFile = INVALID_HANDLE_VALUE; +} + +// Initializes base functions for the disk file +static void BaseFile_Init(TFileStream * pStream) +{ + pStream->BaseCreate = BaseFile_Create; + pStream->BaseOpen = BaseFile_Open; + pStream->BaseRead = BaseFile_Read; + pStream->BaseWrite = BaseFile_Write; + pStream->BaseResize = BaseFile_Resize; + pStream->BaseGetSize = BaseFile_GetSize; + pStream->BaseGetPos = BaseFile_GetPos; + pStream->BaseClose = BaseFile_Close; +} + +//----------------------------------------------------------------------------- +// Local functions - base memory-mapped file support + +static bool BaseMap_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags) +{ +#ifdef PLATFORM_WINDOWS + + ULARGE_INTEGER FileSize; + HANDLE hFile; + HANDLE hMap; + bool bResult = false; + + // Keep compiler happy + dwStreamFlags = dwStreamFlags; + + // Open the file for read access + hFile = CreateFile(szFileName, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + if(hFile != INVALID_HANDLE_VALUE) + { + // Retrieve file size. Don't allow mapping file of a zero size. + FileSize.LowPart = GetFileSize(hFile, &FileSize.HighPart); + if(FileSize.QuadPart != 0) + { + // Now create mapping object + hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); + if(hMap != NULL) + { + // Map the entire view into memory + // Note that this operation will fail if the file can't fit + // into usermode address space + pStream->Base.Map.pbFile = (LPBYTE)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); + if(pStream->Base.Map.pbFile != NULL) + { + // Retrieve file time + GetFileTime(hFile, NULL, NULL, (LPFILETIME)&pStream->Base.Map.FileTime); + + // Retrieve file size and position + pStream->Base.Map.FileSize = FileSize.QuadPart; + pStream->Base.Map.FilePos = 0; + bResult = true; + } + + // Close the map handle + CloseHandle(hMap); + } + } + + // Close the file handle + CloseHandle(hFile); + } + + // If the file is not there and is not available for random access, + // report error + if(bResult == false) + return false; +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + struct stat64 fileinfo; + intptr_t handle; + bool bResult = false; + + // Open the file + handle = open(szFileName, O_RDONLY); + if(handle != -1) + { + // Get the file size + if(fstat64(handle, &fileinfo) != -1) + { + pStream->Base.Map.pbFile = (LPBYTE)mmap(NULL, (size_t)fileinfo.st_size, PROT_READ, MAP_PRIVATE, handle, 0); + if(pStream->Base.Map.pbFile != NULL) + { + // time_t is number of seconds since 1.1.1970, UTC. + // 1 second = 10000000 (decimal) in FILETIME + // Set the start to 1.1.1970 00:00:00 + pStream->Base.Map.FileTime = 0x019DB1DED53E8000ULL + (10000000 * fileinfo.st_mtime); + pStream->Base.Map.FileSize = (ULONGLONG)fileinfo.st_size; + pStream->Base.Map.FilePos = 0; + bResult = true; + } + } + close(handle); + } + + // Did the mapping fail? + if(bResult == false) + { + nLastError = errno; + return false; + } +#endif + + return true; +} + +static bool BaseMap_Read( + TFileStream * pStream, // Pointer to an open stream + ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position + void * pvBuffer, // Pointer to data to be read + DWORD dwBytesToRead) // Number of bytes to read from the file +{ + ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.Map.FilePos; + + // Do we have to read anything at all? + if(dwBytesToRead != 0) + { + // Don't allow reading past file size + if((ByteOffset + dwBytesToRead) > pStream->Base.Map.FileSize) + return false; + + // Copy the required data + memcpy(pvBuffer, pStream->Base.Map.pbFile + (size_t)ByteOffset, dwBytesToRead); + } + + // Move the current file position + pStream->Base.Map.FilePos += dwBytesToRead; + return true; +} + +static void BaseMap_Close(TFileStream * pStream) +{ +#ifdef PLATFORM_WINDOWS + if(pStream->Base.Map.pbFile != NULL) + UnmapViewOfFile(pStream->Base.Map.pbFile); +#endif + +#if defined(PLATFORM_MAC) || defined(PLATFORM_LINUX) + if(pStream->Base.Map.pbFile != NULL) + munmap(pStream->Base.Map.pbFile, (size_t )pStream->Base.Map.FileSize); +#endif + + pStream->Base.Map.pbFile = NULL; +} + +// Initializes base functions for the mapped file +static void BaseMap_Init(TFileStream * pStream) +{ + // Supply the file stream functions + pStream->BaseOpen = BaseMap_Open; + pStream->BaseRead = BaseMap_Read; + pStream->BaseGetSize = BaseFile_GetSize; // Reuse BaseFile function + pStream->BaseGetPos = BaseFile_GetPos; // Reuse BaseFile function + pStream->BaseClose = BaseMap_Close; + + // Mapped files are read-only + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; +} + +//----------------------------------------------------------------------------- +// Local functions - base HTTP file support + +static const TCHAR * BaseHttp_ExtractServerName(const TCHAR * szFileName, TCHAR * szServerName) +{ + // Check for HTTP + if(!_tcsnicmp(szFileName, _T("http://"), 7)) + szFileName += 7; + + // Cut off the server name + if(szServerName != NULL) + { + while(szFileName[0] != 0 && szFileName[0] != _T('/')) + *szServerName++ = *szFileName++; + *szServerName = 0; + } + else + { + while(szFileName[0] != 0 && szFileName[0] != _T('/')) + szFileName++; + } + + // Return the remainder + return szFileName; +} + +static bool BaseHttp_Open(TFileStream * pStream, const TCHAR * szFileName, DWORD dwStreamFlags) +{ +#ifdef PLATFORM_WINDOWS + + HINTERNET hRequest; + DWORD dwTemp = 0; + bool bFileAvailable = false; + int nError = ERROR_SUCCESS; + + // Keep compiler happy + dwStreamFlags = dwStreamFlags; + + // Don't connect to the internet + if(!InternetGetConnectedState(&dwTemp, 0)) + nError = GetLastError(); + + // Initiate the connection to the internet + if(nError == ERROR_SUCCESS) + { + pStream->Base.Http.hInternet = InternetOpen(_T("StormLib HTTP MPQ reader"), + INTERNET_OPEN_TYPE_PRECONFIG, + NULL, + NULL, + 0); + if(pStream->Base.Http.hInternet == NULL) + nError = GetLastError(); + } + + // Connect to the server + if(nError == ERROR_SUCCESS) + { + TCHAR szServerName[MAX_PATH]; + DWORD dwFlags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_UI | INTERNET_FLAG_NO_CACHE_WRITE; + + // Initiate connection with the server + szFileName = BaseHttp_ExtractServerName(szFileName, szServerName); + pStream->Base.Http.hConnect = InternetConnect(pStream->Base.Http.hInternet, + szServerName, + INTERNET_DEFAULT_HTTP_PORT, + NULL, + NULL, + INTERNET_SERVICE_HTTP, + dwFlags, + 0); + if(pStream->Base.Http.hConnect == NULL) + nError = GetLastError(); + } + + // Now try to query the file size + if(nError == ERROR_SUCCESS) + { + // Open HTTP request to the file + hRequest = HttpOpenRequest(pStream->Base.Http.hConnect, _T("GET"), szFileName, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); + if(hRequest != NULL) + { + if(HttpSendRequest(hRequest, NULL, 0, NULL, 0)) + { + ULONGLONG FileTime = 0; + DWORD dwFileSize = 0; + DWORD dwDataSize; + DWORD dwIndex = 0; + + // Check if the MPQ has Last Modified field + dwDataSize = sizeof(ULONGLONG); + if(HttpQueryInfo(hRequest, HTTP_QUERY_LAST_MODIFIED | HTTP_QUERY_FLAG_SYSTEMTIME, &FileTime, &dwDataSize, &dwIndex)) + pStream->Base.Http.FileTime = FileTime; + + // Verify if the server supports random access + dwDataSize = sizeof(DWORD); + if(HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwFileSize, &dwDataSize, &dwIndex)) + { + if(dwFileSize != 0) + { + pStream->Base.Http.FileSize = dwFileSize; + pStream->Base.Http.FilePos = 0; + bFileAvailable = true; + } + } + } + InternetCloseHandle(hRequest); + } + } + + // If the file is not there and is not available for random access, + // report error + if(bFileAvailable == false) + { + pStream->BaseClose(pStream); + return false; + } + + return true; + +#else + + // Not supported + SetLastError(ERROR_NOT_SUPPORTED); + pStream = pStream; + return false; + +#endif +} + +static bool BaseHttp_Read( + TFileStream * pStream, // Pointer to an open stream + ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position + void * pvBuffer, // Pointer to data to be read + DWORD dwBytesToRead) // Number of bytes to read from the file +{ +#ifdef PLATFORM_WINDOWS + ULONGLONG ByteOffset = (pByteOffset != NULL) ? *pByteOffset : pStream->Base.Http.FilePos; + DWORD dwTotalBytesRead = 0; + + // Do we have to read anything at all? + if(dwBytesToRead != 0) + { + HINTERNET hRequest; + LPCTSTR szFileName; + LPBYTE pbBuffer = (LPBYTE)pvBuffer; + TCHAR szRangeRequest[0x80]; + DWORD dwStartOffset = (DWORD)ByteOffset; + DWORD dwEndOffset = dwStartOffset + dwBytesToRead; + + // Open HTTP request to the file + szFileName = BaseHttp_ExtractServerName(pStream->szFileName, NULL); + hRequest = HttpOpenRequest(pStream->Base.Http.hConnect, _T("GET"), szFileName, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0); + if(hRequest != NULL) + { + // Add range request to the HTTP headers + // http://www.clevercomponents.com/articles/article015/resuming.asp + _stprintf(szRangeRequest, _T("Range: bytes=%u-%u"), (unsigned int)dwStartOffset, (unsigned int)dwEndOffset); + HttpAddRequestHeaders(hRequest, szRangeRequest, 0xFFFFFFFF, HTTP_ADDREQ_FLAG_ADD_IF_NEW); + + // Send the request to the server + if(HttpSendRequest(hRequest, NULL, 0, NULL, 0)) + { + while(dwTotalBytesRead < dwBytesToRead) + { + DWORD dwBlockBytesToRead = dwBytesToRead - dwTotalBytesRead; + DWORD dwBlockBytesRead = 0; + + // Read the block from the file + if(dwBlockBytesToRead > 0x200) + dwBlockBytesToRead = 0x200; + InternetReadFile(hRequest, pbBuffer, dwBlockBytesToRead, &dwBlockBytesRead); + + // Check for end + if(dwBlockBytesRead == 0) + break; + + // Move buffers + dwTotalBytesRead += dwBlockBytesRead; + pbBuffer += dwBlockBytesRead; + } + } + InternetCloseHandle(hRequest); + } + } + + // Increment the current file position by number of bytes read + pStream->Base.Http.FilePos = ByteOffset + dwTotalBytesRead; + + // If the number of bytes read doesn't match the required amount, return false + if(dwTotalBytesRead != dwBytesToRead) + SetLastError(ERROR_HANDLE_EOF); + return (dwTotalBytesRead == dwBytesToRead); + +#else + + // Not supported + pStream = pStream; + pByteOffset = pByteOffset; + pvBuffer = pvBuffer; + dwBytesToRead = dwBytesToRead; + SetLastError(ERROR_NOT_SUPPORTED); + return false; + +#endif +} + +static void BaseHttp_Close(TFileStream * pStream) +{ +#ifdef PLATFORM_WINDOWS + if(pStream->Base.Http.hConnect != NULL) + InternetCloseHandle(pStream->Base.Http.hConnect); + pStream->Base.Http.hConnect = NULL; + + if(pStream->Base.Http.hInternet != NULL) + InternetCloseHandle(pStream->Base.Http.hInternet); + pStream->Base.Http.hInternet = NULL; +#else + pStream = pStream; +#endif +} + +// Initializes base functions for the mapped file +static void BaseHttp_Init(TFileStream * pStream) +{ + // Supply the stream functions + pStream->BaseOpen = BaseHttp_Open; + pStream->BaseRead = BaseHttp_Read; + pStream->BaseGetSize = BaseFile_GetSize; // Reuse BaseFile function + pStream->BaseGetPos = BaseFile_GetPos; // Reuse BaseFile function + pStream->BaseClose = BaseHttp_Close; + + // HTTP files are read-only + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; +} + +//----------------------------------------------------------------------------- +// Local functions - base block-based support + +// Generic function that loads blocks from the file +// The function groups the block with the same availability, +// so the called BlockRead can finish the request in a single system call +static bool BlockStream_Read( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG * pByteOffset, // Pointer to file byte offset. If NULL, it reads from the current position + void * pvBuffer, // Pointer to data to be read + DWORD dwBytesToRead) // Number of bytes to read from the file +{ + ULONGLONG BlockOffset0; + ULONGLONG BlockOffset; + ULONGLONG ByteOffset; + ULONGLONG EndOffset; + LPBYTE TransferBuffer; + LPBYTE BlockBuffer; + DWORD BlockBufferOffset; // Offset of the desired data in the block buffer + DWORD BytesNeeded; // Number of bytes that really need to be read + DWORD BlockSize = pStream->BlockSize; + DWORD BlockCount; + bool bPrevBlockAvailable; + bool bCallbackCalled = false; + bool bBlockAvailable; + bool bResult = true; + + // The base block read function must be present + assert(pStream->BlockRead != NULL); + + // NOP reading of zero bytes + if(dwBytesToRead == 0) + return true; + + // Get the current position in the stream + ByteOffset = (pByteOffset != NULL) ? pByteOffset[0] : pStream->StreamPos; + EndOffset = ByteOffset + dwBytesToRead; + if(EndOffset > pStream->StreamSize) + { + SetLastError(ERROR_HANDLE_EOF); + return false; + } + + // Calculate the block parameters + BlockOffset0 = BlockOffset = ByteOffset & ~((ULONGLONG)BlockSize - 1); + BlockCount = (DWORD)(((EndOffset - BlockOffset) + (BlockSize - 1)) / BlockSize); + BytesNeeded = (DWORD)(EndOffset - BlockOffset); + + // Remember where we have our data + assert((BlockSize & (BlockSize - 1)) == 0); + BlockBufferOffset = (DWORD)(ByteOffset & (BlockSize - 1)); + + // Allocate buffer for reading blocks + TransferBuffer = BlockBuffer = STORM_ALLOC(BYTE, (BlockCount * BlockSize)); + if(TransferBuffer == NULL) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return false; + } + + // If all blocks are available, just read all blocks at once + if(pStream->IsComplete == 0) + { + // Now parse the blocks and send the block read request + // to all blocks with the same availability + assert(pStream->BlockCheck != NULL); + bPrevBlockAvailable = pStream->BlockCheck(pStream, BlockOffset); + + // Loop as long as we have something to read + while(BlockOffset < EndOffset) + { + // Determine availability of the next block + bBlockAvailable = pStream->BlockCheck(pStream, BlockOffset); + + // If the availability has changed, read all blocks up to this one + if(bBlockAvailable != bPrevBlockAvailable) + { + // Call the file stream callback, if the block is not available + if(pStream->pMaster && pStream->pfnCallback && bPrevBlockAvailable == false) + { + pStream->pfnCallback(pStream->UserData, BlockOffset0, (DWORD)(BlockOffset - BlockOffset0)); + bCallbackCalled = true; + } + + // Load the continuous blocks with the same availability + assert(BlockOffset > BlockOffset0); + bResult = pStream->BlockRead(pStream, BlockOffset0, BlockOffset, BlockBuffer, BytesNeeded, bPrevBlockAvailable); + if(!bResult) + break; + + // Move the block offset + BlockBuffer += (DWORD)(BlockOffset - BlockOffset0); + BytesNeeded -= (DWORD)(BlockOffset - BlockOffset0); + bPrevBlockAvailable = bBlockAvailable; + BlockOffset0 = BlockOffset; + } + + // Move to the block offset in the stream + BlockOffset += BlockSize; + } + + // If there is a block(s) remaining to be read, do it + if(BlockOffset > BlockOffset0) + { + // Call the file stream callback, if the block is not available + if(pStream->pMaster && pStream->pfnCallback && bPrevBlockAvailable == false) + { + pStream->pfnCallback(pStream->UserData, BlockOffset0, (DWORD)(BlockOffset - BlockOffset0)); + bCallbackCalled = true; + } + + // Read the complete blocks from the file + if(BlockOffset > pStream->StreamSize) + BlockOffset = pStream->StreamSize; + bResult = pStream->BlockRead(pStream, BlockOffset0, BlockOffset, BlockBuffer, BytesNeeded, bPrevBlockAvailable); + } + } + else + { + // Read the complete blocks from the file + if(EndOffset > pStream->StreamSize) + EndOffset = pStream->StreamSize; + bResult = pStream->BlockRead(pStream, BlockOffset, EndOffset, BlockBuffer, BytesNeeded, true); + } + + // Now copy the data to the user buffer + if(bResult) + { + memcpy(pvBuffer, TransferBuffer + BlockBufferOffset, dwBytesToRead); + pStream->StreamPos = ByteOffset + dwBytesToRead; + } + else + { + // If the block read failed, set the last error + SetLastError(ERROR_FILE_INCOMPLETE); + } + + // Call the callback to indicate we are done + if(bCallbackCalled) + pStream->pfnCallback(pStream->UserData, 0, 0); + + // Free the block buffer and return + STORM_FREE(TransferBuffer); + return bResult; +} + +static bool BlockStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize) +{ + *pFileSize = pStream->StreamSize; + return true; +} + +static bool BlockStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset) +{ + *pByteOffset = pStream->StreamPos; + return true; +} + +static void BlockStream_Close(TBlockStream * pStream) +{ + // Free the data map, if any + if(pStream->FileBitmap != NULL) + STORM_FREE(pStream->FileBitmap); + pStream->FileBitmap = NULL; + + // Call the base class for closing the stream + pStream->BaseClose(pStream); +} + +//----------------------------------------------------------------------------- +// File stream allocation function + +static STREAM_INIT StreamBaseInit[4] = +{ + BaseFile_Init, + BaseMap_Init, + BaseHttp_Init, + BaseNone_Init +}; + +// This function allocates an empty structure for the file stream +// The stream structure is created as flat block, variable length +// The file name is placed after the end of the stream structure data +static TFileStream * AllocateFileStream( + const TCHAR * szFileName, + size_t StreamSize, + DWORD dwStreamFlags) +{ + TFileStream * pMaster = NULL; + TFileStream * pStream; + const TCHAR * szNextFile = szFileName; + size_t FileNameSize; + + // Sanity check + assert(StreamSize != 0); + + // The caller can specify chain of files in the following form: + // C:\archive.MPQ*http://www.server.com/MPQs/archive-server.MPQ + // In that case, we use the part after "*" as master file name + while(szNextFile[0] != 0 && szNextFile[0] != _T('*')) + szNextFile++; + FileNameSize = (size_t)((szNextFile - szFileName) * sizeof(TCHAR)); + + // If we have a next file, we need to open it as master stream + // Note that we don't care if the master stream exists or not, + // If it doesn't, later attempts to read missing file block will fail + if(szNextFile[0] == _T('*')) + { + // Don't allow another master file in the string + if(_tcschr(szNextFile + 1, _T('*')) != NULL) + { + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } + + // Open the master file + pMaster = FileStream_OpenFile(szNextFile + 1, STREAM_FLAG_READ_ONLY); + } + + // Allocate the stream structure for the given stream type + pStream = (TFileStream *)STORM_ALLOC(BYTE, StreamSize + FileNameSize + sizeof(TCHAR)); + if(pStream != NULL) + { + // Zero the entire structure + memset(pStream, 0, StreamSize); + pStream->pMaster = pMaster; + pStream->dwFlags = dwStreamFlags; + + // Initialize the file name + pStream->szFileName = (TCHAR *)((BYTE *)pStream + StreamSize); + memcpy(pStream->szFileName, szFileName, FileNameSize); + pStream->szFileName[FileNameSize / sizeof(TCHAR)] = 0; + + // Initialize the stream functions + StreamBaseInit[dwStreamFlags & 0x03](pStream); + } + + return pStream; +} + +//----------------------------------------------------------------------------- +// Local functions - flat stream support + +static DWORD FlatStream_CheckFile(TBlockStream * pStream) +{ + LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap; + DWORD WholeByteCount = (pStream->BlockCount / 8); + DWORD ExtraBitsCount = (pStream->BlockCount & 7); + BYTE ExpectedValue; + + // Verify the whole bytes - their value must be 0xFF + for(DWORD i = 0; i < WholeByteCount; i++) + { + if(FileBitmap[i] != 0xFF) + return 0; + } + + // If there are extra bits, calculate the mask + if(ExtraBitsCount != 0) + { + ExpectedValue = (BYTE)((1 << ExtraBitsCount) - 1); + if(FileBitmap[WholeByteCount] != ExpectedValue) + return 0; + } + + // Yes, the file is complete + return 1; +} + +static bool FlatStream_LoadBitmap(TBlockStream * pStream) +{ + FILE_BITMAP_FOOTER Footer; + ULONGLONG ByteOffset; + LPBYTE FileBitmap; + DWORD BlockCount; + DWORD BitmapSize; + + // Do not load the bitmap if we should not have to + if(!(pStream->dwFlags & STREAM_FLAG_USE_BITMAP)) + return false; + + // Only if the size is greater than size of bitmap footer + if(pStream->Base.File.FileSize > sizeof(FILE_BITMAP_FOOTER)) + { + // Load the bitmap footer + ByteOffset = pStream->Base.File.FileSize - sizeof(FILE_BITMAP_FOOTER); + if(pStream->BaseRead(pStream, &ByteOffset, &Footer, sizeof(FILE_BITMAP_FOOTER))) + { + // Make sure that the array is properly BSWAP-ed + BSWAP_ARRAY32_UNSIGNED((LPDWORD)(&Footer), sizeof(FILE_BITMAP_FOOTER)); + + // Verify if there is actually a footer + if(Footer.Signature == ID_FILE_BITMAP_FOOTER && Footer.Version == 0x03) + { + // Get the offset of the bitmap, number of blocks and size of the bitmap + ByteOffset = MAKE_OFFSET64(Footer.MapOffsetHi, Footer.MapOffsetLo); + BlockCount = (DWORD)(((ByteOffset - 1) / Footer.BlockSize) + 1); + BitmapSize = ((BlockCount + 7) / 8); + + // Check if the sizes match + if(ByteOffset + BitmapSize + sizeof(FILE_BITMAP_FOOTER) == pStream->Base.File.FileSize) + { + // Allocate space for the bitmap + FileBitmap = STORM_ALLOC(BYTE, BitmapSize); + if(FileBitmap != NULL) + { + // Load the bitmap bits + if(!pStream->BaseRead(pStream, &ByteOffset, FileBitmap, BitmapSize)) + { + STORM_FREE(FileBitmap); + return false; + } + + // Update the stream size + pStream->BuildNumber = Footer.BuildNumber; + pStream->StreamSize = ByteOffset; + + // Fill the bitmap information + pStream->FileBitmap = FileBitmap; + pStream->BitmapSize = BitmapSize; + pStream->BlockSize = Footer.BlockSize; + pStream->BlockCount = BlockCount; + pStream->IsComplete = FlatStream_CheckFile(pStream); + return true; + } + } + } + } + } + + return false; +} + +static void FlatStream_UpdateBitmap( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG StartOffset, + ULONGLONG EndOffset) +{ + LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap; + DWORD BlockIndex; + DWORD BlockSize = pStream->BlockSize; + DWORD ByteIndex; + BYTE BitMask; + + // Sanity checks + assert((StartOffset & (BlockSize - 1)) == 0); + assert(FileBitmap != NULL); + + // Calculate the index of the block + BlockIndex = (DWORD)(StartOffset / BlockSize); + ByteIndex = (BlockIndex / 0x08); + BitMask = (BYTE)(1 << (BlockIndex & 0x07)); + + // Set all bits for the specified range + while(StartOffset < EndOffset) + { + // Set the bit + FileBitmap[ByteIndex] |= BitMask; + + // Move all + StartOffset += BlockSize; + ByteIndex += (BitMask >> 0x07); + BitMask = (BitMask >> 0x07) | (BitMask << 0x01); + } + + // Increment the bitmap update count + pStream->IsModified = 1; +} + +static bool FlatStream_BlockCheck( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG BlockOffset) +{ + LPBYTE FileBitmap = (LPBYTE)pStream->FileBitmap; + DWORD BlockIndex; + BYTE BitMask; + + // Sanity checks + assert((BlockOffset & (pStream->BlockSize - 1)) == 0); + assert(FileBitmap != NULL); + + // Calculate the index of the block + BlockIndex = (DWORD)(BlockOffset / pStream->BlockSize); + BitMask = (BYTE)(1 << (BlockIndex & 0x07)); + + // Check if the bit is present + return (FileBitmap[BlockIndex / 0x08] & BitMask) ? true : false; +} + +static bool FlatStream_BlockRead( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG StartOffset, + ULONGLONG EndOffset, + LPBYTE BlockBuffer, + DWORD BytesNeeded, + bool bAvailable) +{ + DWORD BytesToRead = (DWORD)(EndOffset - StartOffset); + + // The starting offset must be aligned to size of the block + assert(pStream->FileBitmap != NULL); + assert((StartOffset & (pStream->BlockSize - 1)) == 0); + assert(StartOffset < EndOffset); + + // If the blocks are not available, we need to load them from the master + // and then save to the mirror + if(bAvailable == false) + { + // If we have no master, we cannot satisfy read request + if(pStream->pMaster == NULL) + return false; + + // Load the blocks from the master stream + // Note that we always have to read complete blocks + // so they get properly stored to the mirror stream + if(!FileStream_Read(pStream->pMaster, &StartOffset, BlockBuffer, BytesToRead)) + return false; + + // Store the loaded blocks to the mirror file. + // Note that this operation is not required to succeed + if(pStream->BaseWrite(pStream, &StartOffset, BlockBuffer, BytesToRead)) + FlatStream_UpdateBitmap(pStream, StartOffset, EndOffset); + + return true; + } + else + { + if(BytesToRead > BytesNeeded) + BytesToRead = BytesNeeded; + return pStream->BaseRead(pStream, &StartOffset, BlockBuffer, BytesToRead); + } +} + +static void FlatStream_Close(TBlockStream * pStream) +{ + FILE_BITMAP_FOOTER Footer; + + if(pStream->FileBitmap && pStream->IsModified) + { + // Write the file bitmap + pStream->BaseWrite(pStream, &pStream->StreamSize, pStream->FileBitmap, pStream->BitmapSize); + + // Prepare and write the file footer + Footer.Signature = ID_FILE_BITMAP_FOOTER; + Footer.Version = 3; + Footer.BuildNumber = pStream->BuildNumber; + Footer.MapOffsetLo = (DWORD)(pStream->StreamSize & 0xFFFFFFFF); + Footer.MapOffsetHi = (DWORD)(pStream->StreamSize >> 0x20); + Footer.BlockSize = pStream->BlockSize; + BSWAP_ARRAY32_UNSIGNED(&Footer, sizeof(FILE_BITMAP_FOOTER)); + pStream->BaseWrite(pStream, NULL, &Footer, sizeof(FILE_BITMAP_FOOTER)); + } + + // Close the base class + BlockStream_Close(pStream); +} + +static bool FlatStream_CreateMirror(TBlockStream * pStream) +{ + ULONGLONG MasterSize = 0; + ULONGLONG MirrorSize = 0; + LPBYTE FileBitmap = NULL; + DWORD dwBitmapSize; + DWORD dwBlockCount; + bool bNeedCreateMirrorStream = true; + bool bNeedResizeMirrorStream = true; + + // Do we have master function and base creation function? + if(pStream->pMaster == NULL || pStream->BaseCreate == NULL) + return false; + + // Retrieve the master file size, block count and bitmap size + FileStream_GetSize(pStream->pMaster, &MasterSize); + dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE); + dwBitmapSize = (DWORD)((dwBlockCount + 7) / 8); + + // Setup stream size and position + pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really??? + pStream->StreamSize = MasterSize; + pStream->StreamPos = 0; + + // Open the base stream for write access + if(pStream->BaseOpen(pStream, pStream->szFileName, 0)) + { + // If the file open succeeded, check if the file size matches required size + pStream->BaseGetSize(pStream, &MirrorSize); + if(MirrorSize == MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER)) + { + // Attempt to load an existing file bitmap + if(FlatStream_LoadBitmap(pStream)) + return true; + + // We need to create new file bitmap + bNeedResizeMirrorStream = false; + } + + // We need to create mirror stream + bNeedCreateMirrorStream = false; + } + + // Create a new stream, if needed + if(bNeedCreateMirrorStream) + { + if(!pStream->BaseCreate(pStream)) + return false; + } + + // If we need to, then resize the mirror stream + if(bNeedResizeMirrorStream) + { + if(!pStream->BaseResize(pStream, MasterSize + dwBitmapSize + sizeof(FILE_BITMAP_FOOTER))) + return false; + } + + // Allocate the bitmap array + FileBitmap = STORM_ALLOC(BYTE, dwBitmapSize); + if(FileBitmap == NULL) + return false; + + // Initialize the bitmap + memset(FileBitmap, 0, dwBitmapSize); + pStream->FileBitmap = FileBitmap; + pStream->BitmapSize = dwBitmapSize; + pStream->BlockSize = DEFAULT_BLOCK_SIZE; + pStream->BlockCount = dwBlockCount; + pStream->IsComplete = 0; + pStream->IsModified = 1; + + // Note: Don't write the stream bitmap right away. + // Doing so would cause sparse file resize on NTFS, + // which would take long time on larger files. + return true; +} + +static TFileStream * FlatStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags) +{ + TBlockStream * pStream; + ULONGLONG ByteOffset = 0; + + // Create new empty stream + pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags); + if(pStream == NULL) + return NULL; + + // Do we have a master stream? + if(pStream->pMaster != NULL) + { + if(!FlatStream_CreateMirror(pStream)) + { + FileStream_Close(pStream); + SetLastError(ERROR_FILE_NOT_FOUND); + return NULL; + } + } + else + { + // Attempt to open the base stream + if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags)) + { + FileStream_Close(pStream); + return NULL; + } + + // Load the bitmap, if required to + if(dwStreamFlags & STREAM_FLAG_USE_BITMAP) + FlatStream_LoadBitmap(pStream); + } + + // If we have a stream bitmap, set the reading functions + // which check presence of each file block + if(pStream->FileBitmap != NULL) + { + // Set the stream position to zero. Stream size is already set + assert(pStream->StreamSize != 0); + pStream->StreamPos = 0; + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; + + // Supply the stream functions + pStream->StreamRead = (STREAM_READ)BlockStream_Read; + pStream->StreamGetSize = BlockStream_GetSize; + pStream->StreamGetPos = BlockStream_GetPos; + pStream->StreamClose = (STREAM_CLOSE)FlatStream_Close; + + // Supply the block functions + pStream->BlockCheck = (BLOCK_CHECK)FlatStream_BlockCheck; + pStream->BlockRead = (BLOCK_READ)FlatStream_BlockRead; + } + else + { + // Reset the base position to zero + pStream->BaseRead(pStream, &ByteOffset, NULL, 0); + + // Setup stream size and position + pStream->StreamSize = pStream->Base.File.FileSize; + pStream->StreamPos = 0; + + // Set the base functions + pStream->StreamRead = pStream->BaseRead; + pStream->StreamWrite = pStream->BaseWrite; + pStream->StreamResize = pStream->BaseResize; + pStream->StreamGetSize = pStream->BaseGetSize; + pStream->StreamGetPos = pStream->BaseGetPos; + pStream->StreamClose = pStream->BaseClose; + } + + return pStream; +} + +//----------------------------------------------------------------------------- +// Local functions - partial stream support + +static bool IsPartHeader(PPART_FILE_HEADER pPartHdr) +{ + // Version number must be 2 + if(pPartHdr->PartialVersion == 2) + { + // GameBuildNumber must be an ASCII number + if(isdigit(pPartHdr->GameBuildNumber[0]) && isdigit(pPartHdr->GameBuildNumber[1]) && isdigit(pPartHdr->GameBuildNumber[2])) + { + // Block size must be power of 2 + if((pPartHdr->BlockSize & (pPartHdr->BlockSize - 1)) == 0) + return true; + } + } + + return false; +} + +static DWORD PartStream_CheckFile(TBlockStream * pStream) +{ + PPART_FILE_MAP_ENTRY FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap; + DWORD dwBlockCount; + + // Get the number of blocks + dwBlockCount = (DWORD)((pStream->StreamSize + pStream->BlockSize - 1) / pStream->BlockSize); + + // Check all blocks + for(DWORD i = 0; i < dwBlockCount; i++, FileBitmap++) + { + // Few sanity checks + assert(FileBitmap->LargeValueHi == 0); + assert(FileBitmap->LargeValueLo == 0); + assert(FileBitmap->Flags == 0 || FileBitmap->Flags == 3); + + // Check if this block is present + if(FileBitmap->Flags != 3) + return 0; + } + + // Yes, the file is complete + return 1; +} + +static bool PartStream_LoadBitmap(TBlockStream * pStream) +{ + PPART_FILE_MAP_ENTRY FileBitmap; + PART_FILE_HEADER PartHdr; + ULONGLONG ByteOffset = 0; + ULONGLONG StreamSize = 0; + DWORD BlockCount; + DWORD BitmapSize; + + // Only if the size is greater than size of the bitmap header + if(pStream->Base.File.FileSize > sizeof(PART_FILE_HEADER)) + { + // Attempt to read PART file header + if(pStream->BaseRead(pStream, &ByteOffset, &PartHdr, sizeof(PART_FILE_HEADER))) + { + // We need to swap PART file header on big-endian platforms + BSWAP_ARRAY32_UNSIGNED(&PartHdr, sizeof(PART_FILE_HEADER)); + + // Verify the PART file header + if(IsPartHeader(&PartHdr)) + { + // Get the number of blocks and size of one block + StreamSize = MAKE_OFFSET64(PartHdr.FileSizeHi, PartHdr.FileSizeLo); + ByteOffset = sizeof(PART_FILE_HEADER); + BlockCount = (DWORD)((StreamSize + PartHdr.BlockSize - 1) / PartHdr.BlockSize); + BitmapSize = BlockCount * sizeof(PART_FILE_MAP_ENTRY); + + // Check if sizes match + if((ByteOffset + BitmapSize) < pStream->Base.File.FileSize) + { + // Allocate space for the array of PART_FILE_MAP_ENTRY + FileBitmap = STORM_ALLOC(PART_FILE_MAP_ENTRY, BlockCount); + if(FileBitmap != NULL) + { + // Load the block map + if(!pStream->BaseRead(pStream, &ByteOffset, FileBitmap, BitmapSize)) + { + STORM_FREE(FileBitmap); + return false; + } + + // Make sure that the byte order is correct + BSWAP_ARRAY32_UNSIGNED(FileBitmap, BitmapSize); + + // Update the stream size + pStream->BuildNumber = StringToInt(PartHdr.GameBuildNumber); + pStream->StreamSize = StreamSize; + + // Fill the bitmap information + pStream->FileBitmap = FileBitmap; + pStream->BitmapSize = BitmapSize; + pStream->BlockSize = PartHdr.BlockSize; + pStream->BlockCount = BlockCount; + pStream->IsComplete = PartStream_CheckFile(pStream); + return true; + } + } + } + } + } + + return false; +} + +static void PartStream_UpdateBitmap( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG StartOffset, + ULONGLONG EndOffset, + ULONGLONG RealOffset) +{ + PPART_FILE_MAP_ENTRY FileBitmap; + DWORD BlockSize = pStream->BlockSize; + + // Sanity checks + assert((StartOffset & (BlockSize - 1)) == 0); + assert(pStream->FileBitmap != NULL); + + // Calculate the first entry in the block map + FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + (StartOffset / BlockSize); + + // Set all bits for the specified range + while(StartOffset < EndOffset) + { + // Set the bit + FileBitmap->BlockOffsHi = (DWORD)(RealOffset >> 0x20); + FileBitmap->BlockOffsLo = (DWORD)(RealOffset & 0xFFFFFFFF); + FileBitmap->Flags = 3; + + // Move all + StartOffset += BlockSize; + RealOffset += BlockSize; + FileBitmap++; + } + + // Increment the bitmap update count + pStream->IsModified = 1; +} + +static bool PartStream_BlockCheck( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG BlockOffset) +{ + PPART_FILE_MAP_ENTRY FileBitmap; + + // Sanity checks + assert((BlockOffset & (pStream->BlockSize - 1)) == 0); + assert(pStream->FileBitmap != NULL); + + // Calculate the block map entry + FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + (BlockOffset / pStream->BlockSize); + + // Check if the flags are present + return (FileBitmap->Flags & 0x03) ? true : false; +} + +static bool PartStream_BlockRead( + TBlockStream * pStream, + ULONGLONG StartOffset, + ULONGLONG EndOffset, + LPBYTE BlockBuffer, + DWORD BytesNeeded, + bool bAvailable) +{ + PPART_FILE_MAP_ENTRY FileBitmap; + ULONGLONG ByteOffset; + DWORD BytesToRead; + DWORD BlockIndex = (DWORD)(StartOffset / pStream->BlockSize); + + // The starting offset must be aligned to size of the block + assert(pStream->FileBitmap != NULL); + assert((StartOffset & (pStream->BlockSize - 1)) == 0); + assert(StartOffset < EndOffset); + + // If the blocks are not available, we need to load them from the master + // and then save to the mirror + if(bAvailable == false) + { + // If we have no master, we cannot satisfy read request + if(pStream->pMaster == NULL) + return false; + + // Load the blocks from the master stream + // Note that we always have to read complete blocks + // so they get properly stored to the mirror stream + BytesToRead = (DWORD)(EndOffset - StartOffset); + if(!FileStream_Read(pStream->pMaster, &StartOffset, BlockBuffer, BytesToRead)) + return false; + + // The loaded blocks are going to be stored to the end of the file + // Note that this operation is not required to succeed + if(pStream->BaseGetSize(pStream, &ByteOffset)) + { + // Store the loaded blocks to the mirror file. + if(pStream->BaseWrite(pStream, &ByteOffset, BlockBuffer, BytesToRead)) + { + PartStream_UpdateBitmap(pStream, StartOffset, EndOffset, ByteOffset); + } + } + } + else + { + // Get the file map entry + FileBitmap = (PPART_FILE_MAP_ENTRY)pStream->FileBitmap + BlockIndex; + + // Read all blocks + while(StartOffset < EndOffset) + { + // Get the number of bytes to be read + BytesToRead = (DWORD)(EndOffset - StartOffset); + if(BytesToRead > pStream->BlockSize) + BytesToRead = pStream->BlockSize; + if(BytesToRead > BytesNeeded) + BytesToRead = BytesNeeded; + + // Read the block + ByteOffset = MAKE_OFFSET64(FileBitmap->BlockOffsHi, FileBitmap->BlockOffsLo); + if(!pStream->BaseRead(pStream, &ByteOffset, BlockBuffer, BytesToRead)) + return false; + + // Move the pointers + StartOffset += pStream->BlockSize; + BlockBuffer += pStream->BlockSize; + BytesNeeded -= pStream->BlockSize; + FileBitmap++; + } + } + + return true; +} + +static void PartStream_Close(TBlockStream * pStream) +{ + PART_FILE_HEADER PartHeader; + ULONGLONG ByteOffset = 0; + + if(pStream->FileBitmap && pStream->IsModified) + { + // Prepare the part file header + memset(&PartHeader, 0, sizeof(PART_FILE_HEADER)); + PartHeader.PartialVersion = 2; + PartHeader.FileSizeHi = (DWORD)(pStream->StreamSize >> 0x20); + PartHeader.FileSizeLo = (DWORD)(pStream->StreamSize & 0xFFFFFFFF); + PartHeader.BlockSize = pStream->BlockSize; + + // Make sure that the header is properly BSWAPed + BSWAP_ARRAY32_UNSIGNED(&PartHeader, sizeof(PART_FILE_HEADER)); + sprintf(PartHeader.GameBuildNumber, "%u", (unsigned int)pStream->BuildNumber); + + // Write the part header + pStream->BaseWrite(pStream, &ByteOffset, &PartHeader, sizeof(PART_FILE_HEADER)); + + // Write the block bitmap + BSWAP_ARRAY32_UNSIGNED(pStream->FileBitmap, pStream->BitmapSize); + pStream->BaseWrite(pStream, NULL, pStream->FileBitmap, pStream->BitmapSize); + } + + // Close the base class + BlockStream_Close(pStream); +} + +static bool PartStream_CreateMirror(TBlockStream * pStream) +{ + ULONGLONG RemainingSize; + ULONGLONG MasterSize = 0; + ULONGLONG MirrorSize = 0; + LPBYTE FileBitmap = NULL; + DWORD dwBitmapSize; + DWORD dwBlockCount; + bool bNeedCreateMirrorStream = true; + bool bNeedResizeMirrorStream = true; + + // Do we have master function and base creation function? + if(pStream->pMaster == NULL || pStream->BaseCreate == NULL) + return false; + + // Retrieve the master file size, block count and bitmap size + FileStream_GetSize(pStream->pMaster, &MasterSize); + dwBlockCount = (DWORD)((MasterSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE); + dwBitmapSize = (DWORD)(dwBlockCount * sizeof(PART_FILE_MAP_ENTRY)); + + // Setup stream size and position + pStream->BuildNumber = DEFAULT_BUILD_NUMBER; // BUGBUG: Really??? + pStream->StreamSize = MasterSize; + pStream->StreamPos = 0; + + // Open the base stream for write access + if(pStream->BaseOpen(pStream, pStream->szFileName, 0)) + { + // If the file open succeeded, check if the file size matches required size + pStream->BaseGetSize(pStream, &MirrorSize); + if(MirrorSize >= sizeof(PART_FILE_HEADER) + dwBitmapSize) + { + // Check if the remaining size is aligned to block + RemainingSize = MirrorSize - sizeof(PART_FILE_HEADER) - dwBitmapSize; + if((RemainingSize & (DEFAULT_BLOCK_SIZE - 1)) == 0 || RemainingSize == MasterSize) + { + // Attempt to load an existing file bitmap + if(PartStream_LoadBitmap(pStream)) + return true; + } + } + + // We need to create mirror stream + bNeedCreateMirrorStream = false; + } + + // Create a new stream, if needed + if(bNeedCreateMirrorStream) + { + if(!pStream->BaseCreate(pStream)) + return false; + } + + // If we need to, then resize the mirror stream + if(bNeedResizeMirrorStream) + { + if(!pStream->BaseResize(pStream, sizeof(PART_FILE_HEADER) + dwBitmapSize)) + return false; + } + + // Allocate the bitmap array + FileBitmap = STORM_ALLOC(BYTE, dwBitmapSize); + if(FileBitmap == NULL) + return false; + + // Initialize the bitmap + memset(FileBitmap, 0, dwBitmapSize); + pStream->FileBitmap = FileBitmap; + pStream->BitmapSize = dwBitmapSize; + pStream->BlockSize = DEFAULT_BLOCK_SIZE; + pStream->BlockCount = dwBlockCount; + pStream->IsComplete = 0; + pStream->IsModified = 1; + + // Note: Don't write the stream bitmap right away. + // Doing so would cause sparse file resize on NTFS, + // which would take long time on larger files. + return true; +} + + +static TFileStream * PartStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags) +{ + TBlockStream * pStream; + + // Create new empty stream + pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags); + if(pStream == NULL) + return NULL; + + // Do we have a master stream? + if(pStream->pMaster != NULL) + { + if(!PartStream_CreateMirror(pStream)) + { + FileStream_Close(pStream); + SetLastError(ERROR_FILE_NOT_FOUND); + return NULL; + } + } + else + { + // Attempt to open the base stream + if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags)) + { + FileStream_Close(pStream); + return NULL; + } + + // Load the part stream block map + if(!PartStream_LoadBitmap(pStream)) + { + FileStream_Close(pStream); + SetLastError(ERROR_BAD_FORMAT); + return NULL; + } + } + + // Set the stream position to zero. Stream size is already set + assert(pStream->StreamSize != 0); + pStream->StreamPos = 0; + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; + + // Set new function pointers + pStream->StreamRead = (STREAM_READ)BlockStream_Read; + pStream->StreamGetPos = BlockStream_GetPos; + pStream->StreamGetSize = BlockStream_GetSize; + pStream->StreamClose = (STREAM_CLOSE)PartStream_Close; + + // Supply the block functions + pStream->BlockCheck = (BLOCK_CHECK)PartStream_BlockCheck; + pStream->BlockRead = (BLOCK_READ)PartStream_BlockRead; + return pStream; +} + +//----------------------------------------------------------------------------- +// Local functions - MPQE stream support + +static const char * szKeyTemplate = "expand 32-byte k000000000000000000000000000000000000000000000000"; + +static const char * AuthCodeArray[] = +{ + // Starcraft II (Heart of the Swarm) + // Authentication code URL: http://dist.blizzard.com/mediakey/hots-authenticationcode-bgdl.txt + // -0C- -1C--08- -18--04- -14--00- -10- + "S48B6CDTN5XEQAKQDJNDLJBJ73FDFM3U", // SC2 Heart of the Swarm-all : "expand 32-byte kQAKQ0000FM3UN5XE000073FD6CDT0000LJBJS48B0000DJND" + + // Diablo III: Agent.exe (1.0.0.954) + // Address of decryption routine: 00502b00 + // Pointer to decryptor object: ECX + // Pointer to key: ECX+0x5C + // Authentication code URL: http://dist.blizzard.com/mediakey/d3-authenticationcode-enGB.txt + // -0C- -1C--08- -18--04- -14--00- -10- + "UCMXF6EJY352EFH4XFRXCFH2XC9MQRZK", // Diablo III Installer (deDE): "expand 32-byte kEFH40000QRZKY3520000XC9MF6EJ0000CFH2UCMX0000XFRX" + "MMKVHY48RP7WXP4GHYBQ7SL9J9UNPHBP", // Diablo III Installer (enGB): "expand 32-byte kXP4G0000PHBPRP7W0000J9UNHY4800007SL9MMKV0000HYBQ" + "8MXLWHQ7VGGLTZ9MQZQSFDCLJYET3CPP", // Diablo III Installer (enSG): "expand 32-byte kTZ9M00003CPPVGGL0000JYETWHQ70000FDCL8MXL0000QZQS" + "EJ2R5TM6XFE2GUNG5QDGHKQ9UAKPWZSZ", // Diablo III Installer (enUS): "expand 32-byte kGUNG0000WZSZXFE20000UAKP5TM60000HKQ9EJ2R00005QDG" + "PBGFBE42Z6LNK65UGJQ3WZVMCLP4HQQT", // Diablo III Installer (esES): "expand 32-byte kK65U0000HQQTZ6LN0000CLP4BE420000WZVMPBGF0000GJQ3" + "X7SEJJS9TSGCW5P28EBSC47AJPEY8VU2", // Diablo III Installer (esMX): "expand 32-byte kW5P200008VU2TSGC0000JPEYJJS90000C47AX7SE00008EBS" + "5KVBQA8VYE6XRY3DLGC5ZDE4XS4P7YA2", // Diablo III Installer (frFR): "expand 32-byte kRY3D00007YA2YE6X0000XS4PQA8V0000ZDE45KVB0000LGC5" + "478JD2K56EVNVVY4XX8TDWYT5B8KB254", // Diablo III Installer (itIT): "expand 32-byte kVVY40000B2546EVN00005B8KD2K50000DWYT478J0000XX8T" + "8TS4VNFQRZTN6YWHE9CHVDH9NVWD474A", // Diablo III Installer (koKR): "expand 32-byte k6YWH0000474ARZTN0000NVWDVNFQ0000VDH98TS40000E9CH" + "LJ52Z32DF4LZ4ZJJXVKK3AZQA6GABLJB", // Diablo III Installer (plPL): "expand 32-byte k4ZJJ0000BLJBF4LZ0000A6GAZ32D00003AZQLJ520000XVKK" + "K6BDHY2ECUE2545YKNLBJPVYWHE7XYAG", // Diablo III Installer (ptBR): "expand 32-byte k545Y0000XYAGCUE20000WHE7HY2E0000JPVYK6BD0000KNLB" + "NDVW8GWLAYCRPGRNY8RT7ZZUQU63VLPR", // Diablo III Installer (ruRU): "expand 32-byte kXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + "6VWCQTN8V3ZZMRUCZXV8A8CGUX2TAA8H", // Diablo III Installer (zhTW): "expand 32-byte kMRUC0000AA8HV3ZZ0000UX2TQTN80000A8CG6VWC0000ZXV8" +// "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", // Diablo III Installer (zhCN): "expand 32-byte kXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + + // Starcraft II (Wings of Liberty): Installer.exe (4.1.1.4219) + // Address of decryption routine: 0053A3D0 + // Pointer to decryptor object: ECX + // Pointer to key: ECX+0x5C + // Authentication code URL: http://dist.blizzard.com/mediakey/sc2-authenticationcode-enUS.txt + // -0C- -1C--08- -18--04- -14--00- -10- + "Y45MD3CAK4KXSSXHYD9VY64Z8EKJ4XFX", // SC2 Wings of Liberty (deDE): "expand 32-byte kSSXH00004XFXK4KX00008EKJD3CA0000Y64ZY45M0000YD9V" + "G8MN8UDG6NA2ANGY6A3DNY82HRGF29ZH", // SC2 Wings of Liberty (enGB): "expand 32-byte kANGY000029ZH6NA20000HRGF8UDG0000NY82G8MN00006A3D" + "W9RRHLB2FDU9WW5B3ECEBLRSFWZSF7HW", // SC2 Wings of Liberty (enSG): "expand 32-byte kWW5B0000F7HWFDU90000FWZSHLB20000BLRSW9RR00003ECE" + "3DH5RE5NVM5GTFD85LXGWT6FK859ETR5", // SC2 Wings of Liberty (enUS): "expand 32-byte kTFD80000ETR5VM5G0000K859RE5N0000WT6F3DH500005LXG" + "8WLKUAXE94PFQU4Y249PAZ24N4R4XKTQ", // SC2 Wings of Liberty (esES): "expand 32-byte kQU4Y0000XKTQ94PF0000N4R4UAXE0000AZ248WLK0000249P" + "A34DXX3VHGGXSQBRFE5UFFDXMF9G4G54", // SC2 Wings of Liberty (esMX): "expand 32-byte kSQBR00004G54HGGX0000MF9GXX3V0000FFDXA34D0000FE5U" + "ZG7J9K938HJEFWPQUA768MA2PFER6EAJ", // SC2 Wings of Liberty (frFR): "expand 32-byte kFWPQ00006EAJ8HJE0000PFER9K9300008MA2ZG7J0000UA76" + "NE7CUNNNTVAPXV7E3G2BSVBWGVMW8BL2", // SC2 Wings of Liberty (itIT): "expand 32-byte kXV7E00008BL2TVAP0000GVMWUNNN0000SVBWNE7C00003G2B" + "3V9E2FTMBM9QQWK7U6MAMWAZWQDB838F", // SC2 Wings of Liberty (koKR): "expand 32-byte kQWK70000838FBM9Q0000WQDB2FTM0000MWAZ3V9E0000U6MA" + "2NSFB8MELULJ83U6YHA3UP6K4MQD48L6", // SC2 Wings of Liberty (plPL): "expand 32-byte k83U6000048L6LULJ00004MQDB8ME0000UP6K2NSF0000YHA3" + "QA2TZ9EWZ4CUU8BMB5WXCTY65F9CSW4E", // SC2 Wings of Liberty (ptBR): "expand 32-byte kU8BM0000SW4EZ4CU00005F9CZ9EW0000CTY6QA2T0000B5WX" + "VHB378W64BAT9SH7D68VV9NLQDK9YEGT", // SC2 Wings of Liberty (ruRU): "expand 32-byte k9SH70000YEGT4BAT0000QDK978W60000V9NLVHB30000D68V" + "U3NFQJV4M6GC7KBN9XQJ3BRDN3PLD9NE", // SC2 Wings of Liberty (zhTW): "expand 32-byte k7KBN0000D9NEM6GC0000N3PLQJV400003BRDU3NF00009XQJ" + + NULL +}; + +static DWORD Rol32(DWORD dwValue, DWORD dwRolCount) +{ + DWORD dwShiftRight = 32 - dwRolCount; + + return (dwValue << dwRolCount) | (dwValue >> dwShiftRight); +} + +static void CreateKeyFromAuthCode( + LPBYTE pbKeyBuffer, + const char * szAuthCode) +{ + LPDWORD KeyPosition = (LPDWORD)(pbKeyBuffer + 0x10); + LPDWORD AuthCode32 = (LPDWORD)szAuthCode; + + memcpy(pbKeyBuffer, szKeyTemplate, MPQE_CHUNK_SIZE); + KeyPosition[0x00] = AuthCode32[0x03]; + KeyPosition[0x02] = AuthCode32[0x07]; + KeyPosition[0x03] = AuthCode32[0x02]; + KeyPosition[0x05] = AuthCode32[0x06]; + KeyPosition[0x06] = AuthCode32[0x01]; + KeyPosition[0x08] = AuthCode32[0x05]; + KeyPosition[0x09] = AuthCode32[0x00]; + KeyPosition[0x0B] = AuthCode32[0x04]; + BSWAP_ARRAY32_UNSIGNED(pbKeyBuffer, MPQE_CHUNK_SIZE); +} + +static void DecryptFileChunk( + DWORD * MpqData, + LPBYTE pbKey, + ULONGLONG ByteOffset, + DWORD dwLength) +{ + ULONGLONG ChunkOffset; + DWORD KeyShuffled[0x10]; + DWORD KeyMirror[0x10]; + DWORD RoundCount = 0x14; + + // Prepare the key + ChunkOffset = ByteOffset / MPQE_CHUNK_SIZE; + memcpy(KeyMirror, pbKey, MPQE_CHUNK_SIZE); + BSWAP_ARRAY32_UNSIGNED(KeyMirror, MPQE_CHUNK_SIZE); + KeyMirror[0x05] = (DWORD)(ChunkOffset >> 32); + KeyMirror[0x08] = (DWORD)(ChunkOffset); + + while(dwLength >= MPQE_CHUNK_SIZE) + { + // Shuffle the key - part 1 + KeyShuffled[0x0E] = KeyMirror[0x00]; + KeyShuffled[0x0C] = KeyMirror[0x01]; + KeyShuffled[0x05] = KeyMirror[0x02]; + KeyShuffled[0x0F] = KeyMirror[0x03]; + KeyShuffled[0x0A] = KeyMirror[0x04]; + KeyShuffled[0x07] = KeyMirror[0x05]; + KeyShuffled[0x0B] = KeyMirror[0x06]; + KeyShuffled[0x09] = KeyMirror[0x07]; + KeyShuffled[0x03] = KeyMirror[0x08]; + KeyShuffled[0x06] = KeyMirror[0x09]; + KeyShuffled[0x08] = KeyMirror[0x0A]; + KeyShuffled[0x0D] = KeyMirror[0x0B]; + KeyShuffled[0x02] = KeyMirror[0x0C]; + KeyShuffled[0x04] = KeyMirror[0x0D]; + KeyShuffled[0x01] = KeyMirror[0x0E]; + KeyShuffled[0x00] = KeyMirror[0x0F]; + + // Shuffle the key - part 2 + for(DWORD i = 0; i < RoundCount; i += 2) + { + KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x02]), 0x07); + KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0E]), 0x09); + KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x0A]), 0x0D); + KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x03]), 0x12); + + KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x04]), 0x07); + KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x0C]), 0x09); + KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x07]), 0x0D); + KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x06]), 0x12); + + KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x01]), 0x07); + KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x05]), 0x09); + KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x0B]), 0x0D); + KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x08]), 0x12); + + KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x00]), 0x07); + KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x0F]), 0x09); + KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x09]), 0x0D); + KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x0D]), 0x12); + + KeyShuffled[0x04] = KeyShuffled[0x04] ^ Rol32((KeyShuffled[0x0E] + KeyShuffled[0x09]), 0x07); + KeyShuffled[0x08] = KeyShuffled[0x08] ^ Rol32((KeyShuffled[0x04] + KeyShuffled[0x0E]), 0x09); + KeyShuffled[0x09] = KeyShuffled[0x09] ^ Rol32((KeyShuffled[0x08] + KeyShuffled[0x04]), 0x0D); + KeyShuffled[0x0E] = KeyShuffled[0x0E] ^ Rol32((KeyShuffled[0x09] + KeyShuffled[0x08]), 0x12); + + KeyShuffled[0x01] = KeyShuffled[0x01] ^ Rol32((KeyShuffled[0x0C] + KeyShuffled[0x0A]), 0x07); + KeyShuffled[0x0D] = KeyShuffled[0x0D] ^ Rol32((KeyShuffled[0x01] + KeyShuffled[0x0C]), 0x09); + KeyShuffled[0x0A] = KeyShuffled[0x0A] ^ Rol32((KeyShuffled[0x0D] + KeyShuffled[0x01]), 0x0D); + KeyShuffled[0x0C] = KeyShuffled[0x0C] ^ Rol32((KeyShuffled[0x0A] + KeyShuffled[0x0D]), 0x12); + + KeyShuffled[0x00] = KeyShuffled[0x00] ^ Rol32((KeyShuffled[0x05] + KeyShuffled[0x07]), 0x07); + KeyShuffled[0x03] = KeyShuffled[0x03] ^ Rol32((KeyShuffled[0x00] + KeyShuffled[0x05]), 0x09); + KeyShuffled[0x07] = KeyShuffled[0x07] ^ Rol32((KeyShuffled[0x03] + KeyShuffled[0x00]), 0x0D); + KeyShuffled[0x05] = KeyShuffled[0x05] ^ Rol32((KeyShuffled[0x07] + KeyShuffled[0x03]), 0x12); + + KeyShuffled[0x02] = KeyShuffled[0x02] ^ Rol32((KeyShuffled[0x0F] + KeyShuffled[0x0B]), 0x07); + KeyShuffled[0x06] = KeyShuffled[0x06] ^ Rol32((KeyShuffled[0x02] + KeyShuffled[0x0F]), 0x09); + KeyShuffled[0x0B] = KeyShuffled[0x0B] ^ Rol32((KeyShuffled[0x06] + KeyShuffled[0x02]), 0x0D); + KeyShuffled[0x0F] = KeyShuffled[0x0F] ^ Rol32((KeyShuffled[0x0B] + KeyShuffled[0x06]), 0x12); + } + + // Decrypt one data chunk + BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE); + MpqData[0x00] = MpqData[0x00] ^ (KeyShuffled[0x0E] + KeyMirror[0x00]); + MpqData[0x01] = MpqData[0x01] ^ (KeyShuffled[0x04] + KeyMirror[0x0D]); + MpqData[0x02] = MpqData[0x02] ^ (KeyShuffled[0x08] + KeyMirror[0x0A]); + MpqData[0x03] = MpqData[0x03] ^ (KeyShuffled[0x09] + KeyMirror[0x07]); + MpqData[0x04] = MpqData[0x04] ^ (KeyShuffled[0x0A] + KeyMirror[0x04]); + MpqData[0x05] = MpqData[0x05] ^ (KeyShuffled[0x0C] + KeyMirror[0x01]); + MpqData[0x06] = MpqData[0x06] ^ (KeyShuffled[0x01] + KeyMirror[0x0E]); + MpqData[0x07] = MpqData[0x07] ^ (KeyShuffled[0x0D] + KeyMirror[0x0B]); + MpqData[0x08] = MpqData[0x08] ^ (KeyShuffled[0x03] + KeyMirror[0x08]); + MpqData[0x09] = MpqData[0x09] ^ (KeyShuffled[0x07] + KeyMirror[0x05]); + MpqData[0x0A] = MpqData[0x0A] ^ (KeyShuffled[0x05] + KeyMirror[0x02]); + MpqData[0x0B] = MpqData[0x0B] ^ (KeyShuffled[0x00] + KeyMirror[0x0F]); + MpqData[0x0C] = MpqData[0x0C] ^ (KeyShuffled[0x02] + KeyMirror[0x0C]); + MpqData[0x0D] = MpqData[0x0D] ^ (KeyShuffled[0x06] + KeyMirror[0x09]); + MpqData[0x0E] = MpqData[0x0E] ^ (KeyShuffled[0x0B] + KeyMirror[0x06]); + MpqData[0x0F] = MpqData[0x0F] ^ (KeyShuffled[0x0F] + KeyMirror[0x03]); + BSWAP_ARRAY32_UNSIGNED(MpqData, MPQE_CHUNK_SIZE); + + // Update byte offset in the key + KeyMirror[0x08]++; + if(KeyMirror[0x08] == 0) + KeyMirror[0x05]++; + + // Move pointers and decrease number of bytes to decrypt + MpqData += (MPQE_CHUNK_SIZE / sizeof(DWORD)); + dwLength -= MPQE_CHUNK_SIZE; + } +} + +static bool MpqeStream_DetectFileKey(TEncryptedStream * pStream) +{ + ULONGLONG ByteOffset = 0; + BYTE EncryptedHeader[MPQE_CHUNK_SIZE]; + BYTE FileHeader[MPQE_CHUNK_SIZE]; + + // Read the first file chunk + if(pStream->BaseRead(pStream, &ByteOffset, EncryptedHeader, sizeof(EncryptedHeader))) + { + // We just try all known keys one by one + for(int i = 0; AuthCodeArray[i] != NULL; i++) + { + // Prepare they decryption key from game serial number + CreateKeyFromAuthCode(pStream->Key, AuthCodeArray[i]); + + // Try to decrypt with the given key + memcpy(FileHeader, EncryptedHeader, MPQE_CHUNK_SIZE); + DecryptFileChunk((LPDWORD)FileHeader, pStream->Key, ByteOffset, MPQE_CHUNK_SIZE); + + // We check the decrypted data + // All known encrypted MPQs have header at the begin of the file, + // so we check for MPQ signature there. + if(FileHeader[0] == 'M' && FileHeader[1] == 'P' && FileHeader[2] == 'Q') + { + // Update the stream size + pStream->StreamSize = pStream->Base.File.FileSize; + + // Fill the block information + pStream->BlockSize = MPQE_CHUNK_SIZE; + pStream->BlockCount = (DWORD)(pStream->Base.File.FileSize + MPQE_CHUNK_SIZE - 1) / MPQE_CHUNK_SIZE; + pStream->IsComplete = 1; + return true; + } + } + } + + // Key not found, sorry + return false; +} + +static bool MpqeStream_BlockRead( + TEncryptedStream * pStream, + ULONGLONG StartOffset, + ULONGLONG EndOffset, + LPBYTE BlockBuffer, + DWORD BytesNeeded, + bool bAvailable) +{ + DWORD dwBytesToRead; + + assert((StartOffset & (pStream->BlockSize - 1)) == 0); + assert(StartOffset < EndOffset); + assert(bAvailable != false); + BytesNeeded = BytesNeeded; + bAvailable = bAvailable; + + // Read the file from the stream as-is + // Limit the reading to number of blocks really needed + dwBytesToRead = (DWORD)(EndOffset - StartOffset); + if(!pStream->BaseRead(pStream, &StartOffset, BlockBuffer, dwBytesToRead)) + return false; + + // Decrypt the data + dwBytesToRead = (dwBytesToRead + MPQE_CHUNK_SIZE - 1) & ~(MPQE_CHUNK_SIZE - 1); + DecryptFileChunk((LPDWORD)BlockBuffer, pStream->Key, StartOffset, dwBytesToRead); + return true; +} + +static TFileStream * MpqeStream_Open(const TCHAR * szFileName, DWORD dwStreamFlags) +{ + TEncryptedStream * pStream; + + // Create new empty stream + pStream = (TEncryptedStream *)AllocateFileStream(szFileName, sizeof(TEncryptedStream), dwStreamFlags); + if(pStream == NULL) + return NULL; + + // Attempt to open the base stream + assert(pStream->BaseOpen != NULL); + if(!pStream->BaseOpen(pStream, pStream->szFileName, dwStreamFlags)) + return NULL; + + // Determine the encryption key for the MPQ + if(MpqeStream_DetectFileKey(pStream)) + { + // Set the stream position and size + assert(pStream->StreamSize != 0); + pStream->StreamPos = 0; + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; + + // Set new function pointers + pStream->StreamRead = (STREAM_READ)BlockStream_Read; + pStream->StreamGetPos = BlockStream_GetPos; + pStream->StreamGetSize = BlockStream_GetSize; + pStream->StreamClose = pStream->BaseClose; + + // Supply the block functions + pStream->BlockRead = (BLOCK_READ)MpqeStream_BlockRead; + return pStream; + } + + // Cleanup the stream and return + FileStream_Close(pStream); + SetLastError(ERROR_UNKNOWN_FILE_KEY); + return NULL; +} + +//----------------------------------------------------------------------------- +// Local functions - Block4 stream support + +#define BLOCK4_BLOCK_SIZE 0x4000 // Size of one block +#define BLOCK4_HASH_SIZE 0x20 // Size of MD5 hash that is after each block +#define BLOCK4_MAX_BLOCKS 0x00002000 // Maximum amount of blocks per file +#define BLOCK4_MAX_FSIZE 0x08040000 // Max size of one file + +static bool Block4Stream_BlockRead( + TBlockStream * pStream, // Pointer to an open stream + ULONGLONG StartOffset, + ULONGLONG EndOffset, + LPBYTE BlockBuffer, + DWORD BytesNeeded, + bool bAvailable) +{ + TBaseProviderData * BaseArray = (TBaseProviderData *)pStream->FileBitmap; + ULONGLONG ByteOffset; + DWORD BytesToRead; + DWORD StreamIndex; + DWORD BlockIndex; + bool bResult; + + // The starting offset must be aligned to size of the block + assert(pStream->FileBitmap != NULL); + assert((StartOffset & (pStream->BlockSize - 1)) == 0); + assert(StartOffset < EndOffset); + assert(bAvailable == true); + + // Keep compiler happy + bAvailable = bAvailable; + EndOffset = EndOffset; + + while(BytesNeeded != 0) + { + // Calculate the block index and the file index + StreamIndex = (DWORD)((StartOffset / pStream->BlockSize) / BLOCK4_MAX_BLOCKS); + BlockIndex = (DWORD)((StartOffset / pStream->BlockSize) % BLOCK4_MAX_BLOCKS); + if(StreamIndex > pStream->BitmapSize) + return false; + + // Calculate the block offset + ByteOffset = ((ULONGLONG)BlockIndex * (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE)); + BytesToRead = STORMLIB_MIN(BytesNeeded, BLOCK4_BLOCK_SIZE); + + // Read from the base stream + pStream->Base = BaseArray[StreamIndex]; + bResult = pStream->BaseRead(pStream, &ByteOffset, BlockBuffer, BytesToRead); + BaseArray[StreamIndex] = pStream->Base; + + // Did the result succeed? + if(bResult == false) + return false; + + // Move pointers + StartOffset += BytesToRead; + BlockBuffer += BytesToRead; + BytesNeeded -= BytesToRead; + } + + return true; +} + + +static void Block4Stream_Close(TBlockStream * pStream) +{ + TBaseProviderData * BaseArray = (TBaseProviderData *)pStream->FileBitmap; + + // If we have a non-zero count of base streams, + // we have to close them all + if(BaseArray != NULL) + { + // Close all base streams + for(DWORD i = 0; i < pStream->BitmapSize; i++) + { + memcpy(&pStream->Base, BaseArray + i, sizeof(TBaseProviderData)); + pStream->BaseClose(pStream); + } + } + + // Free the data map, if any + if(pStream->FileBitmap != NULL) + STORM_FREE(pStream->FileBitmap); + pStream->FileBitmap = NULL; + + // Do not call the BaseClose function, + // we closed all handles already + return; +} + +static TFileStream * Block4Stream_Open(const TCHAR * szFileName, DWORD dwStreamFlags) +{ + TBaseProviderData * NewBaseArray = NULL; + ULONGLONG RemainderBlock; + ULONGLONG BlockCount; + ULONGLONG FileSize; + TBlockStream * pStream; + TCHAR * szNameBuff; + size_t nNameLength; + DWORD dwBaseFiles = 0; + DWORD dwBaseFlags; + + // Create new empty stream + pStream = (TBlockStream *)AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags); + if(pStream == NULL) + return NULL; + + // Sanity check + assert(pStream->BaseOpen != NULL); + + // Get the length of the file name without numeric suffix + nNameLength = _tcslen(pStream->szFileName); + if(pStream->szFileName[nNameLength - 2] == '.' && pStream->szFileName[nNameLength - 1] == '0') + nNameLength -= 2; + pStream->szFileName[nNameLength] = 0; + + // Supply the stream functions + pStream->StreamRead = (STREAM_READ)BlockStream_Read; + pStream->StreamGetSize = BlockStream_GetSize; + pStream->StreamGetPos = BlockStream_GetPos; + pStream->StreamClose = (STREAM_CLOSE)Block4Stream_Close; + pStream->BlockRead = (BLOCK_READ)Block4Stream_BlockRead; + + // Allocate work space for numeric names + szNameBuff = STORM_ALLOC(TCHAR, nNameLength + 4); + if(szNameBuff != NULL) + { + // Set the base flags + dwBaseFlags = (dwStreamFlags & STREAM_PROVIDERS_MASK) | STREAM_FLAG_READ_ONLY; + + // Go all suffixes from 0 to 30 + for(int nSuffix = 0; nSuffix < 30; nSuffix++) + { + // Open the n-th file + _stprintf(szNameBuff, _T("%s.%u"), pStream->szFileName, nSuffix); + if(!pStream->BaseOpen(pStream, szNameBuff, dwBaseFlags)) + break; + + // If the open succeeded, we re-allocate the base provider array + NewBaseArray = STORM_ALLOC(TBaseProviderData, dwBaseFiles + 1); + if(NewBaseArray == NULL) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return NULL; + } + + // Copy the old base data array to the new base data array + if(pStream->FileBitmap != NULL) + { + memcpy(NewBaseArray, pStream->FileBitmap, sizeof(TBaseProviderData) * dwBaseFiles); + STORM_FREE(pStream->FileBitmap); + } + + // Also copy the opened base array + memcpy(NewBaseArray + dwBaseFiles, &pStream->Base, sizeof(TBaseProviderData)); + pStream->FileBitmap = NewBaseArray; + dwBaseFiles++; + + // Get the size of the base stream + pStream->BaseGetSize(pStream, &FileSize); + assert(FileSize <= BLOCK4_MAX_FSIZE); + RemainderBlock = FileSize % (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE); + BlockCount = FileSize / (BLOCK4_BLOCK_SIZE + BLOCK4_HASH_SIZE); + + // Increment the stream size and number of blocks + pStream->StreamSize += (BlockCount * BLOCK4_BLOCK_SIZE); + pStream->BlockCount += (DWORD)BlockCount; + + // Is this the last file? + if(FileSize < BLOCK4_MAX_FSIZE) + { + if(RemainderBlock) + { + pStream->StreamSize += (RemainderBlock - BLOCK4_HASH_SIZE); + pStream->BlockCount++; + } + break; + } + } + + // Fill the remainining block stream variables + pStream->BitmapSize = dwBaseFiles; + pStream->BlockSize = BLOCK4_BLOCK_SIZE; + pStream->IsComplete = 1; + pStream->IsModified = 0; + + // Fill the remaining stream variables + pStream->StreamPos = 0; + pStream->dwFlags |= STREAM_FLAG_READ_ONLY; + + STORM_FREE(szNameBuff); + } + + // If we opened something, return success + if(dwBaseFiles == 0) + { + FileStream_Close(pStream); + SetLastError(ERROR_FILE_NOT_FOUND); + pStream = NULL; + } + + return pStream; +} + +//----------------------------------------------------------------------------- +// Public functions + +/** + * This function creates a new file for read-write access + * + * - If the current platform supports file sharing, + * the file must be created for read sharing (i.e. another application + * can open the file for read, but not for write) + * - If the file does not exist, the function must create new one + * - If the file exists, the function must rewrite it and set to zero size + * - The parameters of the function must be validate by the caller + * - The function must initialize all stream function pointers in TFileStream + * - If the function fails from any reason, it must close all handles + * and free all memory that has been allocated in the process of stream creation, + * including the TFileStream structure itself + * + * \a szFileName Name of the file to create + */ + +TFileStream * FileStream_CreateFile( + const TCHAR * szFileName, + DWORD dwStreamFlags) +{ + TFileStream * pStream; + + // We only support creation of flat, local file + if((dwStreamFlags & (STREAM_PROVIDERS_MASK)) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE)) + { + SetLastError(ERROR_NOT_SUPPORTED); + return NULL; + } + + // Allocate file stream structure for flat stream + pStream = AllocateFileStream(szFileName, sizeof(TBlockStream), dwStreamFlags); + if(pStream != NULL) + { + // Attempt to create the disk file + if(BaseFile_Create(pStream)) + { + // Fill the stream provider functions + pStream->StreamRead = pStream->BaseRead; + pStream->StreamWrite = pStream->BaseWrite; + pStream->StreamResize = pStream->BaseResize; + pStream->StreamGetSize = pStream->BaseGetSize; + pStream->StreamGetPos = pStream->BaseGetPos; + pStream->StreamClose = pStream->BaseClose; + return pStream; + } + + // File create failed, delete the stream + STORM_FREE(pStream); + pStream = NULL; + } + + // Return the stream + return pStream; +} + +/** + * This function opens an existing file for read or read-write access + * - If the current platform supports file sharing, + * the file must be open for read sharing (i.e. another application + * can open the file for read, but not for write) + * - If the file does not exist, the function must return NULL + * - If the file exists but cannot be open, then function must return NULL + * - The parameters of the function must be validate by the caller + * - The function must initialize all stream function pointers in TFileStream + * - If the function fails from any reason, it must close all handles + * and free all memory that has been allocated in the process of stream creation, + * including the TFileStream structure itself + * + * \a szFileName Name of the file to open + * \a dwStreamFlags specifies the provider and base storage type + */ + +TFileStream * FileStream_OpenFile( + const TCHAR * szFileName, + DWORD dwStreamFlags) +{ + DWORD dwProvider = dwStreamFlags & STREAM_PROVIDERS_MASK; + size_t nPrefixLength = FileStream_Prefix(szFileName, &dwProvider); + + // Re-assemble the stream flags + dwStreamFlags = (dwStreamFlags & STREAM_OPTIONS_MASK) | dwProvider; + szFileName += nPrefixLength; + + // Perform provider-specific open + switch(dwStreamFlags & STREAM_PROVIDER_MASK) + { + case STREAM_PROVIDER_FLAT: + return FlatStream_Open(szFileName, dwStreamFlags); + + case STREAM_PROVIDER_PARTIAL: + return PartStream_Open(szFileName, dwStreamFlags); + + case STREAM_PROVIDER_MPQE: + return MpqeStream_Open(szFileName, dwStreamFlags); + + case STREAM_PROVIDER_BLOCK4: + return Block4Stream_Open(szFileName, dwStreamFlags); + + default: + SetLastError(ERROR_INVALID_PARAMETER); + return NULL; + } +} + +/** + * Returns the file name of the stream + * + * \a pStream Pointer to an open stream + */ +const TCHAR * FileStream_GetFileName(TFileStream * pStream) +{ + assert(pStream != NULL); + return pStream->szFileName; +} + +/** + * Returns the length of the provider prefix. Returns zero if no prefix + * + * \a szFileName Pointer to a stream name (file, mapped file, URL) + * \a pdwStreamProvider Pointer to a DWORD variable that receives stream provider (STREAM_PROVIDER_XXX) + */ + +size_t FileStream_Prefix(const TCHAR * szFileName, DWORD * pdwProvider) +{ + size_t nPrefixLength1 = 0; + size_t nPrefixLength2 = 0; + DWORD dwProvider = 0; + + if(szFileName != NULL) + { + // + // Determine the stream provider + // + + if(!_tcsnicmp(szFileName, _T("flat-"), 5)) + { + dwProvider |= STREAM_PROVIDER_FLAT; + nPrefixLength1 = 5; + } + + else if(!_tcsnicmp(szFileName, _T("part-"), 5)) + { + dwProvider |= STREAM_PROVIDER_PARTIAL; + nPrefixLength1 = 5; + } + + else if(!_tcsnicmp(szFileName, _T("mpqe-"), 5)) + { + dwProvider |= STREAM_PROVIDER_MPQE; + nPrefixLength1 = 5; + } + + else if(!_tcsnicmp(szFileName, _T("blk4-"), 5)) + { + dwProvider |= STREAM_PROVIDER_BLOCK4; + nPrefixLength1 = 5; + } + + // + // Determine the base provider + // + + if(!_tcsnicmp(szFileName+nPrefixLength1, _T("file:"), 5)) + { + dwProvider |= BASE_PROVIDER_FILE; + nPrefixLength2 = 5; + } + + else if(!_tcsnicmp(szFileName+nPrefixLength1, _T("map:"), 4)) + { + dwProvider |= BASE_PROVIDER_MAP; + nPrefixLength2 = 4; + } + + else if(!_tcsnicmp(szFileName+nPrefixLength1, _T("http:"), 5)) + { + dwProvider |= BASE_PROVIDER_HTTP; + nPrefixLength2 = 5; + } + + // Only accept stream provider if we recognized the base provider + if(nPrefixLength2 != 0) + { + // It is also allowed to put "//" after the base provider, e.g. "file://", "http://" + if(szFileName[nPrefixLength1+nPrefixLength2] == '/' && szFileName[nPrefixLength1+nPrefixLength2+1] == '/') + nPrefixLength2 += 2; + + if(pdwProvider != NULL) + *pdwProvider = dwProvider; + return nPrefixLength1 + nPrefixLength2; + } + } + + return 0; +} + +/** + * Sets a download callback. Whenever the stream needs to download one or more blocks + * from the server, the callback is called + * + * \a pStream Pointer to an open stream + * \a pfnCallback Pointer to callback function + * \a pvUserData Arbitrary user pointer passed to the download callback + */ + +bool FileStream_SetCallback(TFileStream * pStream, SFILE_DOWNLOAD_CALLBACK pfnCallback, void * pvUserData) +{ + TBlockStream * pBlockStream = (TBlockStream *)pStream; + + if(pStream->BlockRead == NULL) + { + SetLastError(ERROR_NOT_SUPPORTED); + return false; + } + + pBlockStream->pfnCallback = pfnCallback; + pBlockStream->UserData = pvUserData; + return true; +} + +/** + * This function gives the block map. The 'pvBitmap' pointer must point to a buffer + * of at least sizeof(STREAM_BLOCK_MAP) size. It can also have size of the complete + * block map (i.e. sizeof(STREAM_BLOCK_MAP) + BitmapSize). In that case, the function + * also copies the bit-based block map. + * + * \a pStream Pointer to an open stream + * \a pvBitmap Pointer to buffer where the block map will be stored + * \a cbBitmap Length of the buffer, of the block map + * \a cbLengthNeeded Length of the bitmap, in bytes + */ + +bool FileStream_GetBitmap(TFileStream * pStream, void * pvBitmap, DWORD cbBitmap, DWORD * pcbLengthNeeded) +{ + TStreamBitmap * pBitmap = (TStreamBitmap *)pvBitmap; + TBlockStream * pBlockStream = (TBlockStream *)pStream; + ULONGLONG BlockOffset; + LPBYTE Bitmap = (LPBYTE)(pBitmap + 1); + DWORD BitmapSize; + DWORD BlockCount; + DWORD BlockSize; + bool bResult = false; + + // Retrieve the size of one block + if(pStream->BlockCheck != NULL) + { + BlockCount = pBlockStream->BlockCount; + BlockSize = pBlockStream->BlockSize; + } + else + { + BlockCount = (DWORD)((pStream->StreamSize + DEFAULT_BLOCK_SIZE - 1) / DEFAULT_BLOCK_SIZE); + BlockSize = DEFAULT_BLOCK_SIZE; + } + + // Fill-in the variables + BitmapSize = (BlockCount + 7) / 8; + + // Give the number of blocks + if(pcbLengthNeeded != NULL) + pcbLengthNeeded[0] = sizeof(TStreamBitmap) + BitmapSize; + + // If the length of the buffer is not enough + if(pvBitmap != NULL && cbBitmap != 0) + { + // Give the STREAM_BLOCK_MAP structure + if(cbBitmap >= sizeof(TStreamBitmap)) + { + pBitmap->StreamSize = pStream->StreamSize; + pBitmap->BitmapSize = BitmapSize; + pBitmap->BlockCount = BlockCount; + pBitmap->BlockSize = BlockSize; + pBitmap->IsComplete = (pStream->BlockCheck != NULL) ? pBlockStream->IsComplete : 1; + bResult = true; + } + + // Give the block bitmap, if enough space + if(cbBitmap >= sizeof(TStreamBitmap) + BitmapSize) + { + // Version with bitmap present + if(pStream->BlockCheck != NULL) + { + DWORD ByteIndex = 0; + BYTE BitMask = 0x01; + + // Initialize the map with zeros + memset(Bitmap, 0, BitmapSize); + + // Fill the map + for(BlockOffset = 0; BlockOffset < pStream->StreamSize; BlockOffset += BlockSize) + { + // Set the bit if the block is present + if(pBlockStream->BlockCheck(pStream, BlockOffset)) + Bitmap[ByteIndex] |= BitMask; + + // Move bit position + ByteIndex += (BitMask >> 0x07); + BitMask = (BitMask >> 0x07) | (BitMask << 0x01); + } + } + else + { + memset(Bitmap, 0xFF, BitmapSize); + } + } + } + + // Set last error value and return + if(bResult == false) + SetLastError(ERROR_INSUFFICIENT_BUFFER); + return bResult; +} + +/** + * Reads data from the stream + * + * - Returns true if the read operation succeeded and all bytes have been read + * - Returns false if either read failed or not all bytes have been read + * - If the pByteOffset is NULL, the function must read the data from the current file position + * - The function can be called with dwBytesToRead = 0. In that case, pvBuffer is ignored + * and the function just adjusts file pointer. + * + * \a pStream Pointer to an open stream + * \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position + * \a pvBuffer Pointer to data to be read + * \a dwBytesToRead Number of bytes to read from the file + * + * \returns + * - If the function reads the required amount of bytes, it returns true. + * - If the function reads less than required bytes, it returns false and GetLastError() returns ERROR_HANDLE_EOF + * - If the function fails, it reads false and GetLastError() returns an error code different from ERROR_HANDLE_EOF + */ +bool FileStream_Read(TFileStream * pStream, ULONGLONG * pByteOffset, void * pvBuffer, DWORD dwBytesToRead) +{ + assert(pStream->StreamRead != NULL); + return pStream->StreamRead(pStream, pByteOffset, pvBuffer, dwBytesToRead); +} + +/** + * This function writes data to the stream + * + * - Returns true if the write operation succeeded and all bytes have been written + * - Returns false if either write failed or not all bytes have been written + * - If the pByteOffset is NULL, the function must write the data to the current file position + * + * \a pStream Pointer to an open stream + * \a pByteOffset Pointer to file byte offset. If NULL, it reads from the current position + * \a pvBuffer Pointer to data to be written + * \a dwBytesToWrite Number of bytes to write to the file + */ +bool FileStream_Write(TFileStream * pStream, ULONGLONG * pByteOffset, const void * pvBuffer, DWORD dwBytesToWrite) +{ + if(pStream->dwFlags & STREAM_FLAG_READ_ONLY) + { + SetLastError(ERROR_ACCESS_DENIED); + return false; + } + + assert(pStream->StreamWrite != NULL); + return pStream->StreamWrite(pStream, pByteOffset, pvBuffer, dwBytesToWrite); +} + +/** + * Returns the size of a file + * + * \a pStream Pointer to an open stream + * \a FileSize Pointer where to store the file size + */ +bool FileStream_GetSize(TFileStream * pStream, ULONGLONG * pFileSize) +{ + assert(pStream->StreamGetSize != NULL); + return pStream->StreamGetSize(pStream, pFileSize); +} + +/** + * Sets the size of a file + * + * \a pStream Pointer to an open stream + * \a NewFileSize File size to set + */ +bool FileStream_SetSize(TFileStream * pStream, ULONGLONG NewFileSize) +{ + if(pStream->dwFlags & STREAM_FLAG_READ_ONLY) + { + SetLastError(ERROR_ACCESS_DENIED); + return false; + } + + assert(pStream->StreamResize != NULL); + return pStream->StreamResize(pStream, NewFileSize); +} + +/** + * This function returns the current file position + * \a pStream + * \a pByteOffset + */ +bool FileStream_GetPos(TFileStream * pStream, ULONGLONG * pByteOffset) +{ + assert(pStream->StreamGetPos != NULL); + return pStream->StreamGetPos(pStream, pByteOffset); +} + +/** + * Returns the last write time of a file + * + * \a pStream Pointer to an open stream + * \a pFileType Pointer where to store the file last write time + */ +bool FileStream_GetTime(TFileStream * pStream, ULONGLONG * pFileTime) +{ + // Just use the saved filetime value + *pFileTime = pStream->Base.File.FileTime; + return true; +} + +/** + * Returns the stream flags + * + * \a pStream Pointer to an open stream + * \a pdwStreamFlags Pointer where to store the stream flags + */ +bool FileStream_GetFlags(TFileStream * pStream, LPDWORD pdwStreamFlags) +{ + *pdwStreamFlags = pStream->dwFlags; + return true; +} + +/** + * Switches a stream with another. Used for final phase of archive compacting. + * Performs these steps: + * + * 1) Closes the handle to the existing MPQ + * 2) Renames the temporary MPQ to the original MPQ, overwrites existing one + * 3) Opens the MPQ stores the handle and stream position to the new stream structure + * + * \a pStream Pointer to an open stream + * \a pNewStream Temporary ("working") stream (created during archive compacting) + */ +bool FileStream_Replace(TFileStream * pStream, TFileStream * pNewStream) +{ + // Only supported on flat files + if((pStream->dwFlags & STREAM_PROVIDERS_MASK) != (STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE)) + { + SetLastError(ERROR_NOT_SUPPORTED); + return false; + } + + // Not supported on read-only streams + if(pStream->dwFlags & STREAM_FLAG_READ_ONLY) + { + SetLastError(ERROR_ACCESS_DENIED); + return false; + } + + // Close both stream's base providers + pNewStream->BaseClose(pNewStream); + pStream->BaseClose(pStream); + + // Now we have to delete the (now closed) old file and rename the new file + if(!BaseFile_Replace(pStream, pNewStream)) + return false; + + // Now open the base file again + if(!BaseFile_Open(pStream, pStream->szFileName, pStream->dwFlags)) + return false; + + // Cleanup the new stream + FileStream_Close(pNewStream); + return true; +} + +/** + * This function closes an archive file and frees any data buffers + * that have been allocated for stream management. The function must also + * support partially allocated structure, i.e. one or more buffers + * can be NULL, if there was an allocation failure during the process + * + * \a pStream Pointer to an open stream + */ +void FileStream_Close(TFileStream * pStream) +{ + // Check if the stream structure is allocated at all + if(pStream != NULL) + { + // Free the master stream, if any + if(pStream->pMaster != NULL) + FileStream_Close(pStream->pMaster); + pStream->pMaster = NULL; + + // Close the stream provider. + if(pStream->StreamClose != NULL) + pStream->StreamClose(pStream); + + // Also close base stream, if any + else if(pStream->BaseClose != NULL) + pStream->BaseClose(pStream); + + // Free the stream itself + STORM_FREE(pStream); + } +} + +//----------------------------------------------------------------------------- +// Utility functions (ANSI) + +const char * GetPlainFileName(const char * szFileName) +{ + const char * szPlainName = szFileName; + + while(*szFileName != 0) + { + if(*szFileName == '\\' || *szFileName == '/') + szPlainName = szFileName + 1; + szFileName++; + } + + return szPlainName; +} + +void CopyFileName(char * szTarget, const char * szSource, size_t cchLength) +{ + memcpy(szTarget, szSource, cchLength); + szTarget[cchLength] = 0; +} + +//----------------------------------------------------------------------------- +// Utility functions (UNICODE) only exist in the ANSI version of the library +// In ANSI builds, TCHAR = char, so we don't need these functions implemented + +#ifdef _UNICODE +const TCHAR * GetPlainFileName(const TCHAR * szFileName) +{ + const TCHAR * szPlainName = szFileName; + + while(*szFileName != 0) + { + if(*szFileName == '\\' || *szFileName == '/') + szPlainName = szFileName + 1; + szFileName++; + } + + return szPlainName; +} + +void CopyFileName(TCHAR * szTarget, const char * szSource, size_t cchLength) +{ + mbstowcs(szTarget, szSource, cchLength); + szTarget[cchLength] = 0; +} + +void CopyFileName(char * szTarget, const TCHAR * szSource, size_t cchLength) +{ + wcstombs(szTarget, szSource, cchLength); + szTarget[cchLength] = 0; +} +#endif + +//----------------------------------------------------------------------------- +// main - for testing purposes + +#ifdef __STORMLIB_TEST__ +int FileStream_Test(const TCHAR * szFileName, DWORD dwStreamFlags) +{ + TFileStream * pStream; + TMPQHeader MpqHeader; + ULONGLONG FilePos; + TMPQBlock * pBlock; + TMPQHash * pHash; + + InitializeMpqCryptography(); + + pStream = FileStream_OpenFile(szFileName, dwStreamFlags); + if(pStream == NULL) + return GetLastError(); + + // Read the MPQ header + FileStream_Read(pStream, NULL, &MpqHeader, MPQ_HEADER_SIZE_V2); + if(MpqHeader.dwID != ID_MPQ) + return ERROR_FILE_CORRUPT; + + // Read the hash table + pHash = STORM_ALLOC(TMPQHash, MpqHeader.dwHashTableSize); + if(pHash != NULL) + { + FilePos = MpqHeader.dwHashTablePos; + FileStream_Read(pStream, &FilePos, pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash)); + DecryptMpqBlock(pHash, MpqHeader.dwHashTableSize * sizeof(TMPQHash), MPQ_KEY_HASH_TABLE); + STORM_FREE(pHash); + } + + // Read the block table + pBlock = STORM_ALLOC(TMPQBlock, MpqHeader.dwBlockTableSize); + if(pBlock != NULL) + { + FilePos = MpqHeader.dwBlockTablePos; + FileStream_Read(pStream, &FilePos, pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock)); + DecryptMpqBlock(pBlock, MpqHeader.dwBlockTableSize * sizeof(TMPQBlock), MPQ_KEY_BLOCK_TABLE); + STORM_FREE(pBlock); + } + + FileStream_Close(pStream); + return ERROR_SUCCESS; +} +#endif diff --git a/src/SBaseCommon.cpp b/src/SBaseCommon.cpp index be7a74d..f363c28 100644 --- a/src/SBaseCommon.cpp +++ b/src/SBaseCommon.cpp @@ -708,6 +708,8 @@ TMPQFile * CreateFileHandle(TMPQArchive * ha, TFileEntry * pFileEntry) { // Set the raw position and MPQ position hf->RawFilePos = ha->MpqPos + pFileEntry->ByteOffset; + if(ha->pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1) + hf->RawFilePos = (DWORD)ha->MpqPos + (DWORD)pFileEntry->ByteOffset; hf->MpqFilePos = pFileEntry->ByteOffset; // Set the data size @@ -732,7 +734,6 @@ void * LoadMpqTable( LPBYTE pbMpqTable; LPBYTE pbToRead; DWORD dwBytesToRead = dwCompressedSize; - DWORD dwValidLength = dwTableSize; int nError = ERROR_SUCCESS; // Allocate the MPQ table @@ -751,6 +752,22 @@ void * LoadMpqTable( } } + // On archives v 1.0, Storm.dll does not actually check + // if the hash table was read entirely. Abused by Spazzler map protector + // which sets hash table size to 0x00100000 + if(ha->pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1 && (ha->dwFlags & MPQ_FLAG_MALFORMED)) + { + ULONGLONG FileSize = 0; + + // Cut the table size + FileStream_GetSize(ha->pStream, &FileSize); + if((ByteOffset + dwBytesToRead) > FileSize) + { + dwBytesToRead = (DWORD)(FileSize - ByteOffset); + memset(pbMpqTable + dwBytesToRead, 0, (dwTableSize - dwBytesToRead)); + } + } + // If everything succeeded, read the raw table form the MPQ if(FileStream_Read(ha->pStream, &ByteOffset, pbToRead, dwBytesToRead)) { @@ -774,10 +791,6 @@ void * LoadMpqTable( // Make sure that the table is properly byte-swapped BSWAP_ARRAY32_UNSIGNED(pbMpqTable, dwTableSize); - - // If the table was not fully readed, fill the rest with zeros - if(dwValidLength < dwTableSize) - memset(pbMpqTable + dwValidLength, 0, (dwTableSize - dwValidLength)); } else { @@ -805,25 +818,22 @@ void CalculateRawSectorOffset( TMPQFile * hf, DWORD dwSectorOffset) { + // Must be used for files within a MPQ + assert(hf->ha != NULL); + assert(hf->ha->pHeader != NULL); + // // Some MPQ protectors place the sector offset table after the actual file data. // Sector offsets in the sector offset table are negative. When added // to MPQ file offset from the block table entry, the result is a correct // position of the file data in the MPQ. // - // The position of sector table must be always within the MPQ, however. - // When a negative sector offset is found, we make sure that we make the addition - // just in 32-bits, and then add the MPQ offset. + // For MPQs version 1.0, the offset is purely 32-bit // - if(dwSectorOffset & 0x80000000) - { - RawFilePos = hf->ha->MpqPos + ((DWORD)hf->pFileEntry->ByteOffset + dwSectorOffset); - } - else - { - RawFilePos = hf->RawFilePos + dwSectorOffset; - } + RawFilePos = hf->RawFilePos + dwSectorOffset; + if(hf->ha->pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1) + RawFilePos = (DWORD)hf->ha->MpqPos + (DWORD)hf->pFileEntry->ByteOffset + dwSectorOffset; // We also have to add patch header size, if patch header is present if(hf->pPatchInfo != NULL) @@ -1044,11 +1054,13 @@ int AllocateSectorOffsets(TMPQFile * hf, bool bLoadFromFile) } // The sector size must not be bigger than compressed file size - if((dwSectorOffset1 - dwSectorOffset0) > pFileEntry->dwCmpSize) - { - bSectorOffsetTableCorrupt = true; - break; - } + // Edit: Yes, but apparently, in original Storm.dll, the compressed + // size is not checked anywhere +// if((dwSectorOffset1 - dwSectorOffset0) > pFileEntry->dwCmpSize) +// { +// bSectorOffsetTableCorrupt = true; +// break; +// } } // If data corruption detected, free the sector offset table diff --git a/src/SBaseFileTable.cpp b/src/SBaseFileTable.cpp index a2eb5a8..33e1472 100644 --- a/src/SBaseFileTable.cpp +++ b/src/SBaseFileTable.cpp @@ -56,30 +56,6 @@ static DWORD GetNecessaryBitCount(ULONGLONG MaxValue) return dwBitCount; } -static int CompareFilePositions(const void * p1, const void * p2) -{ - TMPQBlock * pBlock1 = *(TMPQBlock **)p1; - TMPQBlock * pBlock2 = *(TMPQBlock **)p2; - DWORD dwFileEnd1; - DWORD dwFileEnd2; - - // Compare file begins - if(pBlock1->dwFilePos < pBlock2->dwFilePos) - return -1; - if(pBlock1->dwFilePos > pBlock2->dwFilePos) - return +1; - - // If the files begin at the same position, compare their ends - dwFileEnd1 = pBlock1->dwFilePos + pBlock1->dwCSize; - dwFileEnd2 = pBlock2->dwFilePos + pBlock2->dwCSize; - if(dwFileEnd1 < dwFileEnd2) - return -1; - if(dwFileEnd1 > dwFileEnd2) - return +1; - - return 0; -} - //----------------------------------------------------------------------------- // Support functions for BIT_ARRAY @@ -240,27 +216,6 @@ void SetBits( //----------------------------------------------------------------------------- // Support for MPQ header -static DWORD GetMaxFileOffset32(TMPQArchive * ha) -{ - TMPQHeader * pHeader = ha->pHeader; - DWORD dwMaxFileOffset = ha->pHeader->dwArchiveSize; - - // We can call this only for malformed archives v 1.0 - assert(ha->pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1); - assert((ha->dwFlags & MPQ_FLAG_MALFORMED) != 0); - - // If the block table is at the end, decrement the limit by the block table - if(pHeader->dwBlockTablePos + (pHeader->dwBlockTableSize * sizeof(TMPQBlock)) >= dwMaxFileOffset) - dwMaxFileOffset = pHeader->dwBlockTablePos; - - // If the hash table is at the end, decrement the limit by the hash table - if(pHeader->dwHashTablePos + (pHeader->dwHashTableSize * sizeof(TMPQBlock)) >= dwMaxFileOffset) - dwMaxFileOffset = pHeader->dwHashTablePos; - - // Return what we found - return dwMaxFileOffset; -} - static ULONGLONG DetermineArchiveSize_V1( TMPQArchive * ha, TMPQHeader * pHeader, @@ -2049,78 +2004,40 @@ void InvalidateInternalFiles(TMPQArchive * ha) //----------------------------------------------------------------------------- // Support for file tables - hash table, block table, hi-block table -int CreateHashTable(TMPQArchive * ha, DWORD dwHashTableSize) -{ - TMPQHash * pHashTable; - - // Sanity checks - assert((dwHashTableSize & (dwHashTableSize - 1)) == 0); - assert(ha->pHashTable == NULL); - - // If the required hash table size is zero, don't create anything - if(dwHashTableSize == 0) - dwHashTableSize = HASH_TABLE_SIZE_DEFAULT; - - // Create the hash table - pHashTable = STORM_ALLOC(TMPQHash, dwHashTableSize); - if(pHashTable == NULL) - return ERROR_NOT_ENOUGH_MEMORY; - - // Fill it - memset(pHashTable, 0xFF, dwHashTableSize * sizeof(TMPQHash)); - ha->dwMaxFileCount = dwHashTableSize; - ha->pHashTable = pHashTable; - return ERROR_SUCCESS; -} - -TMPQHash * LoadHashTable(TMPQArchive * ha) +// Fixes the case when hash table size is set to arbitrary long value +static void FixHashTableSize(TMPQArchive * ha, ULONGLONG ByteOffset) { - TMPQHeader * pHeader = ha->pHeader; - ULONGLONG ByteOffset; - TMPQHash * pHashTable = NULL; - DWORD dwTableSize; - DWORD dwCmpSize; - - // If the MPQ has no hash table, do nothing - if(pHeader->dwHashTablePos == 0 && pHeader->wHashTablePosHi == 0) - return NULL; + ULONGLONG FileSize = 0; + DWORD dwFixedTableSize; + DWORD dwHashTableSize = ha->pHeader->dwHashTableSize; - // If the hash table size is zero, do nothing - if(pHeader->dwHashTableSize == 0) - return NULL; + // Spazzler protector abuses the fact that hash table size does not matter + // if the hash table is entirely filled with values different than 0xFFFFFFFF. + // It sets the hash table size to 0x00100000, which slows down file searching + // and adding a listfile. - // Load the hash table for MPQ variations - switch(ha->dwSubType) + // Only if the hash table size is correct + if((dwHashTableSize & (dwHashTableSize - 1)) == 0) { - case MPQ_SUBTYPE_MPQ: - - // Get the compressed and uncompressed hash table size - dwTableSize = pHeader->dwHashTableSize * sizeof(TMPQHash); - dwCmpSize = (DWORD)pHeader->HashTableSize64; - - // - // Load the table from the MPQ, with decompression - // - // Note: We will NOT check if the hash table is properly decrypted. - // Some MPQ protectors corrupt the hash table by rewriting part of it. - // Hash table, the way how it works, allows arbitrary values for unused entries. - // + // Retrieve the file size + FileStream_GetSize(ha->pStream, &FileSize); - ByteOffset = ha->MpqPos + MAKE_OFFSET64(pHeader->wHashTablePosHi, pHeader->dwHashTablePos); - pHashTable = (TMPQHash *)LoadMpqTable(ha, ByteOffset, dwCmpSize, dwTableSize, MPQ_KEY_HASH_TABLE); - break; + // Work as long as the size greater than file size + for(;;) + { + // Try the size of one half of the current + dwFixedTableSize = dwHashTableSize >> 1; + if(ByteOffset + (dwFixedTableSize * sizeof(TMPQHash)) <= FileSize) + break; - case MPQ_SUBTYPE_SQP: - pHashTable = LoadSqpHashTable(ha); - break; + // Cut the hash table size to half + dwHashTableSize = dwFixedTableSize; + } - case MPQ_SUBTYPE_MPK: - pHashTable = LoadMpkHashTable(ha); - break; + // Fix the hash table size + ha->pHeader->dwHashTableSize = dwHashTableSize; + ha->pHeader->HashTableSize64 = dwHashTableSize * sizeof(TMPQHash); } - - // Return the hash table - return pHashTable; } // This function fixes the scenario then dwBlockTableSize @@ -2174,64 +2091,90 @@ static void FixBlockTableSize( } } -// This function fixes the scenario then dwBlockTableSize -// is greater and goes into a MPQ file -static void FixCompressedFileSize( - TMPQArchive * ha, - TMPQBlock * pBlockTable) +int CreateHashTable(TMPQArchive * ha, DWORD dwHashTableSize) { - TMPQHeader * pHeader = ha->pHeader; - TMPQBlock ** SortTable; - TMPQBlock * pBlockTableEnd = pBlockTable + pHeader->dwBlockTableSize; - TMPQBlock * pBlock; - size_t nElements = 0; - size_t nIndex; - DWORD dwMaxFileOffs; + TMPQHash * pHashTable; - // Only perform this check on MPQs version 1.0 - assert(pHeader->dwHeaderSize == MPQ_HEADER_SIZE_V1); + // Sanity checks + assert((dwHashTableSize & (dwHashTableSize - 1)) == 0); + assert(ha->pHashTable == NULL); - // Allocate sort table for all entries - SortTable = STORM_ALLOC(TMPQBlock*, pHeader->dwBlockTableSize); - if(SortTable != NULL) - { - // Calculate the end of the archive - dwMaxFileOffs = GetMaxFileOffset32(ha); + // If the required hash table size is zero, don't create anything + if(dwHashTableSize == 0) + dwHashTableSize = HASH_TABLE_SIZE_DEFAULT; - // Put all blocks to a sort table - for(pBlock = pBlockTable; pBlock < pBlockTableEnd; pBlock++) - { - if(pBlock->dwFlags & MPQ_FILE_EXISTS) - SortTable[nElements++] = pBlock; - } + // Create the hash table + pHashTable = STORM_ALLOC(TMPQHash, dwHashTableSize); + if(pHashTable == NULL) + return ERROR_NOT_ENOUGH_MEMORY; - // Have we found at least one block? - if(nElements > 0) - { - // Sort the table - qsort(SortTable, nElements, sizeof(TMPQBlock *), CompareFilePositions); + // Fill it + memset(pHashTable, 0xFF, dwHashTableSize * sizeof(TMPQHash)); + ha->dwMaxFileCount = dwHashTableSize; + ha->pHashTable = pHashTable; + return ERROR_SUCCESS; +} + +TMPQHash * LoadHashTable(TMPQArchive * ha) +{ + TMPQHeader * pHeader = ha->pHeader; + ULONGLONG ByteOffset; + TMPQHash * pHashTable = NULL; + DWORD dwTableSize; + DWORD dwCmpSize; + + // If the MPQ has no hash table, do nothing + if(pHeader->dwHashTablePos == 0 && pHeader->wHashTablePosHi == 0) + return NULL; + + // If the hash table size is zero, do nothing + if(pHeader->dwHashTableSize == 0) + return NULL; + + // Load the hash table for MPQ variations + switch(ha->dwSubType) + { + case MPQ_SUBTYPE_MPQ: - // Walk the table and set all compressed sizes to they - // match the difference (dwFilePos1 - dwFilePos0) - for(nIndex = 0; nIndex < nElements - 1; nIndex++) + ByteOffset = ha->MpqPos + MAKE_OFFSET64(pHeader->wHashTablePosHi, pHeader->dwHashTablePos); + if((pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1) && (ha->dwFlags & MPQ_FLAG_MALFORMED)) { - TMPQBlock * pBlock1 = SortTable[nIndex + 1]; - TMPQBlock * pBlock0 = SortTable[nIndex]; - - pBlock0->dwCSize = (pBlock0->dwFlags & MPQ_FILE_COMPRESS_MASK) ? (pBlock1->dwFilePos - pBlock0->dwFilePos) : pBlock0->dwFSize; + // Calculate the hash table offset as 32-bit value + ByteOffset = (DWORD)ha->MpqPos + pHeader->dwHashTablePos; + + // Defense against map protectors that set the hash table size too big + FixHashTableSize(ha, ByteOffset); } - // Fix the last entry - pBlock = SortTable[nElements - 1]; - pBlock->dwCSize = (pBlock->dwFlags & MPQ_FILE_COMPRESS_MASK) ? (dwMaxFileOffs - pBlock->dwFilePos) : pBlock->dwFSize; - } + // Get the compressed and uncompressed hash table size + dwTableSize = pHeader->dwHashTableSize * sizeof(TMPQHash); + dwCmpSize = (DWORD)pHeader->HashTableSize64; + + // + // Load the table from the MPQ, with decompression + // + // Note: We will NOT check if the hash table is properly decrypted. + // Some MPQ protectors corrupt the hash table by rewriting part of it. + // Hash table, the way how it works, allows arbitrary values for unused entries. + // + + pHashTable = (TMPQHash *)LoadMpqTable(ha, ByteOffset, dwCmpSize, dwTableSize, MPQ_KEY_HASH_TABLE); + break; + + case MPQ_SUBTYPE_SQP: + pHashTable = LoadSqpHashTable(ha); + break; - STORM_FREE(SortTable); + case MPQ_SUBTYPE_MPK: + pHashTable = LoadMpkHashTable(ha); + break; } -} + // Return the hash table + return pHashTable; +} -TMPQBlock * LoadBlockTable(TMPQArchive * ha, bool bDontFixEntries) +TMPQBlock * LoadBlockTable(TMPQArchive * ha, bool /* bDontFixEntries */) { TMPQHeader * pHeader = ha->pHeader; TMPQBlock * pBlockTable = NULL; @@ -2278,9 +2221,11 @@ TMPQBlock * LoadBlockTable(TMPQArchive * ha, bool bDontFixEntries) if(pBlockTable != NULL) FixBlockTableSize(ha, pBlockTable); - // Defense against protectors that set invalid compressed size - if((ha->dwFlags & MPQ_FLAG_MALFORMED) && (bDontFixEntries == false)) - FixCompressedFileSize(ha, pBlockTable); + // + // Note: TMPQBlock::dwCSize file size can be completely invalid + // for compressed files and the file can still be read. + // Abused by some MPQ protectors + // } break; diff --git a/src/SFileAttributes.cpp b/src/SFileAttributes.cpp index e509d89..479363d 100644 --- a/src/SFileAttributes.cpp +++ b/src/SFileAttributes.cpp @@ -1,571 +1,571 @@ -/*****************************************************************************/
-/* SAttrFile.cpp Copyright (c) Ladislav Zezula 2007 */
-/*---------------------------------------------------------------------------*/
-/* Description: */
-/*---------------------------------------------------------------------------*/
-/* Date Ver Who Comment */
-/* -------- ---- --- ------- */
-/* 12.06.04 1.00 Lad The first version of SAttrFile.cpp */
-/*****************************************************************************/
-
-#define __STORMLIB_SELF__
-#include "StormLib.h"
-#include "StormCommon.h"
-
-//-----------------------------------------------------------------------------
-// Local structures
-
-typedef struct _MPQ_ATTRIBUTES_HEADER
-{
- DWORD dwVersion; // Version of the (attributes) file. Must be 100 (0x64)
- DWORD dwFlags; // See MPQ_ATTRIBUTE_XXXX
-
- // Followed by an array of CRC32
- // Followed by an array of file times
- // Followed by an array of MD5
- // Followed by an array of patch bits
-} MPQ_ATTRIBUTES_HEADER, *PMPQ_ATTRIBUTES_HEADER;
-
-//-----------------------------------------------------------------------------
-// Local functions
-
-static DWORD GetSizeOfAttributesFile(DWORD dwAttrFlags, DWORD dwFileTableSize)
-{
- DWORD cbAttrFile = sizeof(MPQ_ATTRIBUTES_HEADER);
-
- // Calculate size of the (attributes) file
- if(dwAttrFlags & MPQ_ATTRIBUTE_CRC32)
- cbAttrFile += dwFileTableSize * sizeof(DWORD);
- if(dwAttrFlags & MPQ_ATTRIBUTE_FILETIME)
- cbAttrFile += dwFileTableSize * sizeof(ULONGLONG);
- if(dwAttrFlags & MPQ_ATTRIBUTE_MD5)
- cbAttrFile += dwFileTableSize * MD5_DIGEST_SIZE;
-
- // The bit array has been created without the last bit belonging to (attributes)
- // When the number of files is a multiplier of 8 plus one, then the size of (attributes)
- // if 1 byte less than expected.
- // Example: wow-update-13164.MPQ: BlockTableSize = 0x62E1, but there's only 0xC5C bytes
- if(dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT)
- cbAttrFile += (dwFileTableSize + 6) / 8;
-
- return cbAttrFile;
-}
-
-static DWORD CheckSizeOfAttributesFile(DWORD cbAttrFile, DWORD dwAttrFlags, DWORD dwFileTableSize)
-{
- DWORD cbHeaderSize = sizeof(MPQ_ATTRIBUTES_HEADER);
- DWORD cbChecksumSize1 = 0;
- DWORD cbChecksumSize2 = 0;
- DWORD cbFileTimeSize1 = 0;
- DWORD cbFileTimeSize2 = 0;
- DWORD cbFileHashSize1 = 0;
- DWORD cbFileHashSize2 = 0;
- DWORD cbPatchBitSize1 = 0;
- DWORD cbPatchBitSize2 = 0;
- DWORD cbPatchBitSize3 = 0;
-
- //
- // Various variants with the patch bit
- //
- // interface.MPQ.part from WoW build 10958 has
- // the MPQ_ATTRIBUTE_PATCH_BIT set, but there's an array of DWORDs instead.
- // The array is filled with zeros, so we don't know what it should contain
- //
- // Zenith.SC2MAP has the MPQ_ATTRIBUTE_PATCH_BIT set, but the bit array is missing
- //
- // Elimination Tournament 2.w3x's (attributes) have one entry less
- //
- // There may be two variants: Either the (attributes) file has full
- // number of entries, or has one entry less
- //
-
- // Get the expected size of CRC32 array
- if(dwAttrFlags & MPQ_ATTRIBUTE_CRC32)
- {
- cbChecksumSize1 += dwFileTableSize * sizeof(DWORD);
- cbChecksumSize2 += cbChecksumSize1 - sizeof(DWORD);
- }
-
- // Get the expected size of FILETIME array
- if(dwAttrFlags & MPQ_ATTRIBUTE_FILETIME)
- {
- cbFileTimeSize1 += dwFileTableSize * sizeof(ULONGLONG);
- cbFileTimeSize2 += cbFileTimeSize1 - sizeof(ULONGLONG);
- }
-
- // Get the expected size of MD5 array
- if(dwAttrFlags & MPQ_ATTRIBUTE_MD5)
- {
- cbFileHashSize1 += dwFileTableSize * MD5_DIGEST_SIZE;
- cbFileHashSize2 += cbFileHashSize1 - MD5_DIGEST_SIZE;
- }
-
- // Get the expected size of patch bit array
- if(dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT)
- {
- cbPatchBitSize1 =
- cbPatchBitSize2 = ((dwFileTableSize + 6) / 8);
- cbPatchBitSize3 = dwFileTableSize * sizeof(DWORD);
- }
-
- // Check if the (attributes) file entry count is equal to our file table size
- if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1 + cbPatchBitSize1))
- return dwFileTableSize;
-
- // Check if the (attributes) file entry count is equal to our file table size minus one
- if(cbAttrFile == (cbHeaderSize + cbChecksumSize2 + cbFileTimeSize2 + cbFileHashSize2 + cbPatchBitSize2))
- return dwFileTableSize - 1;
-
- // Zenith.SC2MAP has the MPQ_ATTRIBUTE_PATCH_BIT set, but the bit array is missing
- if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1))
- return dwFileTableSize;
-
- // interface.MPQ.part (WoW build 10958) has the MPQ_ATTRIBUTE_PATCH_BIT set
- // but there's an array of DWORDs (filled with zeros) instead of array of bits
- if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1 + cbPatchBitSize3))
- return dwFileTableSize;
-
-#ifdef __STORMLIB_TEST__
- // Invalid size of the (attributes) file
- // Note that many MPQs, especially Warcraft III maps have the size of (attributes) invalid.
- // We only perform this check if this is the STORMLIB testprogram itself
-// assert(false);
-#endif
-
- return 0;
-}
-
-static int LoadAttributesFile(TMPQArchive * ha, LPBYTE pbAttrFile, DWORD cbAttrFile)
-{
- LPBYTE pbAttrFileEnd = pbAttrFile + cbAttrFile;
- LPBYTE pbAttrPtr = pbAttrFile;
- DWORD dwAttributesEntries = 0;
- DWORD i;
-
- // Load and verify the header
- if((pbAttrPtr + sizeof(MPQ_ATTRIBUTES_HEADER)) <= pbAttrFileEnd)
- {
- PMPQ_ATTRIBUTES_HEADER pAttrHeader = (PMPQ_ATTRIBUTES_HEADER)pbAttrPtr;
-
- // Verify the header version
- BSWAP_ARRAY32_UNSIGNED(pAttrHeader, sizeof(MPQ_ATTRIBUTES_HEADER));
- if(pAttrHeader->dwVersion != MPQ_ATTRIBUTES_V1)
- return ERROR_BAD_FORMAT;
-
- // Verify the flags
- if(pAttrHeader->dwFlags & ~MPQ_ATTRIBUTE_ALL)
- return ERROR_BAD_FORMAT;
-
- // Verify whether file size of (attributes) is expected
- dwAttributesEntries = CheckSizeOfAttributesFile(cbAttrFile, pAttrHeader->dwFlags, ha->pHeader->dwBlockTableSize);
- if(dwAttributesEntries == 0)
- return ERROR_BAD_FORMAT;
-
- ha->dwAttrFlags = pAttrHeader->dwFlags;
- pbAttrPtr = (LPBYTE)(pAttrHeader + 1);
- }
-
- // Load the CRC32 (if present)
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_CRC32)
- {
- LPDWORD ArrayCRC32 = (LPDWORD)pbAttrPtr;
- DWORD cbArraySize = dwAttributesEntries * sizeof(DWORD);
-
- // Verify if there's enough data
- if((pbAttrPtr + cbArraySize) > pbAttrFileEnd)
- return ERROR_FILE_CORRUPT;
-
- BSWAP_ARRAY32_UNSIGNED(ArrayCRC32, cbCRC32Size);
- for(i = 0; i < dwAttributesEntries; i++)
- ha->pFileTable[i].dwCrc32 = ArrayCRC32[i];
- pbAttrPtr += cbArraySize;
- }
-
- // Load the FILETIME (if present)
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_FILETIME)
- {
- ULONGLONG * ArrayFileTime = (ULONGLONG *)pbAttrPtr;
- DWORD cbArraySize = dwAttributesEntries * sizeof(ULONGLONG);
-
- // Verify if there's enough data
- if((pbAttrPtr + cbArraySize) > pbAttrFileEnd)
- return ERROR_FILE_CORRUPT;
-
- BSWAP_ARRAY64_UNSIGNED(ArrayFileTime, cbFileTimeSize);
- for(i = 0; i < dwAttributesEntries; i++)
- ha->pFileTable[i].FileTime = ArrayFileTime[i];
- pbAttrPtr += cbArraySize;
- }
-
- // Load the MD5 (if present)
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_MD5)
- {
- LPBYTE ArrayMd5 = pbAttrPtr;
- DWORD cbArraySize = dwAttributesEntries * MD5_DIGEST_SIZE;
-
- // Verify if there's enough data
- if((pbAttrPtr + cbArraySize) > pbAttrFileEnd)
- return ERROR_FILE_CORRUPT;
-
- for(i = 0; i < dwAttributesEntries; i++)
- {
- memcpy(ha->pFileTable[i].md5, ArrayMd5, MD5_DIGEST_SIZE);
- ArrayMd5 += MD5_DIGEST_SIZE;
- }
- pbAttrPtr += cbArraySize;
- }
-
- // Read the patch bit for each file (if present)
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT)
- {
- LPBYTE pbBitArray = pbAttrPtr;
- DWORD cbArraySize = (dwAttributesEntries + 7) / 8;
- DWORD dwByteIndex = 0;
- DWORD dwBitMask = 0x80;
-
- // Verify if there's enough data
- if((pbAttrPtr + cbArraySize) == pbAttrFileEnd)
- {
- for(i = 0; i < dwAttributesEntries; i++)
- {
- ha->pFileTable[i].dwFlags |= (pbBitArray[dwByteIndex] & dwBitMask) ? MPQ_FILE_PATCH_FILE : 0;
- dwByteIndex += (dwBitMask & 0x01);
- dwBitMask = (dwBitMask << 0x07) | (dwBitMask >> 0x01);
- }
- }
- }
-
- return ERROR_SUCCESS;
-}
-
-static LPBYTE CreateAttributesFile(TMPQArchive * ha, DWORD * pcbAttrFile)
-{
- PMPQ_ATTRIBUTES_HEADER pAttrHeader;
- TFileEntry * pFileTableEnd = ha->pFileTable + ha->dwFileTableSize;
- TFileEntry * pFileEntry;
- LPBYTE pbAttrFile;
- LPBYTE pbAttrPtr;
- size_t cbAttrFile;
- DWORD dwFinalEntries = ha->dwFileTableSize + ha->dwReservedFiles;
-
- // Check if we need patch bits in the (attributes) file
- for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
- {
- if(pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE)
- {
- ha->dwAttrFlags |= MPQ_ATTRIBUTE_PATCH_BIT;
- break;
- }
- }
-
- // Allocate the buffer for holding the entire (attributes)
- // Allodate 1 byte more (See GetSizeOfAttributesFile for more info)
- cbAttrFile = GetSizeOfAttributesFile(ha->dwAttrFlags, dwFinalEntries);
- pbAttrFile = pbAttrPtr = STORM_ALLOC(BYTE, cbAttrFile + 1);
- if(pbAttrFile != NULL)
- {
- // Make sure it's all zeroed
- memset(pbAttrFile, 0, cbAttrFile + 1);
-
- // Write the header of the (attributes) file
- pAttrHeader = (PMPQ_ATTRIBUTES_HEADER)pbAttrPtr;
- pAttrHeader->dwVersion = BSWAP_INT32_UNSIGNED(100);
- pAttrHeader->dwFlags = BSWAP_INT32_UNSIGNED((ha->dwAttrFlags & MPQ_ATTRIBUTE_ALL));
- pbAttrPtr = (LPBYTE)(pAttrHeader + 1);
-
- // Write the array of CRC32, if present
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_CRC32)
- {
- LPDWORD pArrayCRC32 = (LPDWORD)pbAttrPtr;
-
- // Copy from file table
- for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
- *pArrayCRC32++ = BSWAP_INT32_UNSIGNED(pFileEntry->dwCrc32);
-
- // Skip the reserved entries
- pbAttrPtr = (LPBYTE)(pArrayCRC32 + ha->dwReservedFiles);
- }
-
- // Write the array of file time
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_FILETIME)
- {
- ULONGLONG * pArrayFileTime = (ULONGLONG *)pbAttrPtr;
-
- // Copy from file table
- for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
- *pArrayFileTime++ = BSWAP_INT64_UNSIGNED(pFileEntry->FileTime);
-
- // Skip the reserved entries
- pbAttrPtr = (LPBYTE)(pArrayFileTime + ha->dwReservedFiles);
- }
-
- // Write the array of MD5s
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_MD5)
- {
- LPBYTE pbArrayMD5 = pbAttrPtr;
-
- // Copy from file table
- for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
- {
- memcpy(pbArrayMD5, pFileEntry->md5, MD5_DIGEST_SIZE);
- pbArrayMD5 += MD5_DIGEST_SIZE;
- }
-
- // Skip the reserved items
- pbAttrPtr = pbArrayMD5 + (ha->dwReservedFiles * MD5_DIGEST_SIZE);
- }
-
- // Write the array of patch bits
- if(ha->dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT)
- {
- LPBYTE pbBitArray = pbAttrPtr;
- DWORD dwByteIndex = 0;
- BYTE dwBitMask = 0x80;
-
- // Copy from file table
- for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++)
- {
- // Set the bit, if needed
- if(pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE)
- pbBitArray[dwByteIndex] |= dwBitMask;
-
- // Update bit index and bit mask
- dwByteIndex += (dwBitMask & 0x01);
- dwBitMask = (dwBitMask << 0x07) | (dwBitMask >> 0x01);
- }
-
- // Having incremented the bit array just by the number of items in the file table
- // will create the bit array one byte less of the number of files is a multiplier of 8).
- // Blizzard MPQs have the same feature.
-
- // Move past the bit array
- pbAttrPtr = (pbBitArray + dwByteIndex) + ((dwBitMask & 0x7F) ? 1 : 0);
- }
-
- // Now we expect that current position matches the estimated size
- // Note that if there is 1 extra bit above the byte size,
- // the table is actually 1 byte shorted in Blizzard MPQs. See GetSizeOfAttributesFile
- assert((size_t)(pbAttrPtr - pbAttrFile) == cbAttrFile);
- }
-
- // Give away the attributes file
- if(pcbAttrFile != NULL)
- *pcbAttrFile = (DWORD)cbAttrFile;
- return pbAttrFile;
-}
-
-//-----------------------------------------------------------------------------
-// Public functions (internal use by StormLib)
-
-int SAttrLoadAttributes(TMPQArchive * ha)
-{
- HANDLE hFile = NULL;
- LPBYTE pbAttrFile;
- DWORD dwBytesRead;
- DWORD cbAttrFile = 0;
- int nError = ERROR_FILE_CORRUPT;
-
- // File table must be initialized
- assert(ha->pFileTable != NULL);
-
- // Don't load the attributes file from malformer Warcraft III maps
- if(ha->dwFlags & MPQ_FLAG_MALFORMED)
- return ERROR_FILE_CORRUPT;
-
- // Attempt to open the "(attributes)" file.
- if(SFileOpenFileEx((HANDLE)ha, ATTRIBUTES_NAME, SFILE_OPEN_ANY_LOCALE, &hFile))
- {
- // Retrieve and check size of the (attributes) file
- cbAttrFile = SFileGetFileSize(hFile, NULL);
-
- // Size of the (attributes) might be 1 byte less than expected
- // See GetSizeOfAttributesFile for more info
- pbAttrFile = STORM_ALLOC(BYTE, cbAttrFile + 1);
- if(pbAttrFile != NULL)
- {
- // Set the last byte to 0 in case the size should be 1 byte greater
- pbAttrFile[cbAttrFile] = 0;
-
- // Load the entire file to memory
- SFileReadFile(hFile, pbAttrFile, cbAttrFile, &dwBytesRead, NULL);
- if(dwBytesRead == cbAttrFile)
- nError = LoadAttributesFile(ha, pbAttrFile, cbAttrFile);
-
- // Free the buffer
- STORM_FREE(pbAttrFile);
- }
-
- // Close the attributes file
- SFileCloseFile(hFile);
- }
-
- return nError;
-}
-
-// Saves the (attributes) to the MPQ
-int SAttrFileSaveToMpq(TMPQArchive * ha)
-{
- TMPQFile * hf = NULL;
- LPBYTE pbAttrFile;
- DWORD cbAttrFile = 0;
- int nError = ERROR_SUCCESS;
-
- // Only save the attributes if we should do so
- if(ha->dwFileFlags2 != 0)
- {
- // At this point, we expect to have at least one reserved entry in the file table
- assert(ha->dwFlags & MPQ_FLAG_ATTRIBUTES_INVALID);
- assert(ha->dwReservedFiles >= 1);
-
- // Create the raw data that is to be written to (attributes)
- // Note: Blizzard MPQs have entries for (listfile) and (attributes),
- // but they are filled empty
- pbAttrFile = CreateAttributesFile(ha, &cbAttrFile);
- if(pbAttrFile != NULL)
- {
- // We expect it to be nonzero size
- assert(cbAttrFile != 0);
-
- // Determine the real flags for (attributes)
- if(ha->dwFileFlags2 == MPQ_FILE_EXISTS)
- ha->dwFileFlags2 = GetDefaultSpecialFileFlags(cbAttrFile, ha->pHeader->wFormatVersion);
-
- // Create the attributes file in the MPQ
- nError = SFileAddFile_Init(ha, ATTRIBUTES_NAME,
- 0,
- cbAttrFile,
- LANG_NEUTRAL,
- ha->dwFileFlags2 | MPQ_FILE_REPLACEEXISTING,
- &hf);
-
- // Write the attributes file raw data to it
- if(nError == ERROR_SUCCESS)
- {
- // Write the content of the attributes file to the MPQ
- nError = SFileAddFile_Write(hf, pbAttrFile, cbAttrFile, MPQ_COMPRESSION_ZLIB);
- SFileAddFile_Finish(hf);
- }
-
- // Free the attributes buffer
- STORM_FREE(pbAttrFile);
- }
- else
- {
- // If the list file is empty, we assume ERROR_SUCCESS
- nError = (cbAttrFile == 0) ? ERROR_SUCCESS : ERROR_NOT_ENOUGH_MEMORY;
- }
-
- // If the save process succeeded, we clear the MPQ_FLAG_ATTRIBUTE_INVALID flag
- if(nError == ERROR_SUCCESS)
- {
- ha->dwFlags &= ~MPQ_FLAG_ATTRIBUTES_INVALID;
- ha->dwReservedFiles--;
- }
- }
-
- return nError;
-}
-
-//-----------------------------------------------------------------------------
-// Public functions
-
-DWORD WINAPI SFileGetAttributes(HANDLE hMpq)
-{
- TMPQArchive * ha = (TMPQArchive *)hMpq;
-
- // Verify the parameters
- if(!IsValidMpqHandle(hMpq))
- {
- SetLastError(ERROR_INVALID_PARAMETER);
- return SFILE_INVALID_ATTRIBUTES;
- }
-
- return ha->dwAttrFlags;
-}
-
-bool WINAPI SFileSetAttributes(HANDLE hMpq, DWORD dwFlags)
-{
- TMPQArchive * ha = (TMPQArchive *)hMpq;
-
- // Verify the parameters
- if(!IsValidMpqHandle(hMpq))
- {
- SetLastError(ERROR_INVALID_PARAMETER);
- return false;
- }
-
- // Not allowed when the archive is read-only
- if(ha->dwFlags & MPQ_FLAG_READ_ONLY)
- {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
-
- // Set the attributes
- InvalidateInternalFiles(ha);
- ha->dwAttrFlags = (dwFlags & MPQ_ATTRIBUTE_ALL);
- return true;
-}
-
-bool WINAPI SFileUpdateFileAttributes(HANDLE hMpq, const char * szFileName)
-{
- hash_state md5_state;
- TMPQArchive * ha = (TMPQArchive *)hMpq;
- TMPQFile * hf;
- BYTE Buffer[0x1000];
- HANDLE hFile = NULL;
- DWORD dwTotalBytes = 0;
- DWORD dwBytesRead;
- DWORD dwCrc32;
-
- // Verify the parameters
- if(!IsValidMpqHandle(ha))
- {
- SetLastError(ERROR_INVALID_PARAMETER);
- return false;
- }
-
- // Not allowed when the archive is read-only
- if(ha->dwFlags & MPQ_FLAG_READ_ONLY)
- {
- SetLastError(ERROR_ACCESS_DENIED);
- return false;
- }
-
- // Attempt to open the file
- if(!SFileOpenFileEx(hMpq, szFileName, SFILE_OPEN_BASE_FILE, &hFile))
- return false;
-
- // Get the file size
- hf = (TMPQFile *)hFile;
- dwTotalBytes = hf->pFileEntry->dwFileSize;
-
- // Initialize the CRC32 and MD5 contexts
- md5_init(&md5_state);
- dwCrc32 = crc32(0, Z_NULL, 0);
-
- // Go through entire file and calculate both CRC32 and MD5
- while(dwTotalBytes != 0)
- {
- // Read data from file
- SFileReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead, NULL);
- if(dwBytesRead == 0)
- break;
-
- // Update CRC32 and MD5
- dwCrc32 = crc32(dwCrc32, Buffer, dwBytesRead);
- md5_process(&md5_state, Buffer, dwBytesRead);
-
- // Decrement the total size
- dwTotalBytes -= dwBytesRead;
- }
-
- // Update both CRC32 and MD5
- hf->pFileEntry->dwCrc32 = dwCrc32;
- md5_done(&md5_state, hf->pFileEntry->md5);
-
- // Remember that we need to save the MPQ tables
- InvalidateInternalFiles(ha);
- SFileCloseFile(hFile);
- return true;
-}
+/*****************************************************************************/ +/* SAttrFile.cpp Copyright (c) Ladislav Zezula 2007 */ +/*---------------------------------------------------------------------------*/ +/* Description: */ +/*---------------------------------------------------------------------------*/ +/* Date Ver Who Comment */ +/* -------- ---- --- ------- */ +/* 12.06.04 1.00 Lad The first version of SAttrFile.cpp */ +/*****************************************************************************/ + +#define __STORMLIB_SELF__ +#include "StormLib.h" +#include "StormCommon.h" + +//----------------------------------------------------------------------------- +// Local structures + +typedef struct _MPQ_ATTRIBUTES_HEADER +{ + DWORD dwVersion; // Version of the (attributes) file. Must be 100 (0x64) + DWORD dwFlags; // See MPQ_ATTRIBUTE_XXXX + + // Followed by an array of CRC32 + // Followed by an array of file times + // Followed by an array of MD5 + // Followed by an array of patch bits +} MPQ_ATTRIBUTES_HEADER, *PMPQ_ATTRIBUTES_HEADER; + +//----------------------------------------------------------------------------- +// Local functions + +static DWORD GetSizeOfAttributesFile(DWORD dwAttrFlags, DWORD dwFileTableSize) +{ + DWORD cbAttrFile = sizeof(MPQ_ATTRIBUTES_HEADER); + + // Calculate size of the (attributes) file + if(dwAttrFlags & MPQ_ATTRIBUTE_CRC32) + cbAttrFile += dwFileTableSize * sizeof(DWORD); + if(dwAttrFlags & MPQ_ATTRIBUTE_FILETIME) + cbAttrFile += dwFileTableSize * sizeof(ULONGLONG); + if(dwAttrFlags & MPQ_ATTRIBUTE_MD5) + cbAttrFile += dwFileTableSize * MD5_DIGEST_SIZE; + + // The bit array has been created without the last bit belonging to (attributes) + // When the number of files is a multiplier of 8 plus one, then the size of (attributes) + // if 1 byte less than expected. + // Example: wow-update-13164.MPQ: BlockTableSize = 0x62E1, but there's only 0xC5C bytes + if(dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT) + cbAttrFile += (dwFileTableSize + 6) / 8; + + return cbAttrFile; +} + +static DWORD CheckSizeOfAttributesFile(DWORD cbAttrFile, DWORD dwAttrFlags, DWORD dwFileTableSize) +{ + DWORD cbHeaderSize = sizeof(MPQ_ATTRIBUTES_HEADER); + DWORD cbChecksumSize1 = 0; + DWORD cbChecksumSize2 = 0; + DWORD cbFileTimeSize1 = 0; + DWORD cbFileTimeSize2 = 0; + DWORD cbFileHashSize1 = 0; + DWORD cbFileHashSize2 = 0; + DWORD cbPatchBitSize1 = 0; + DWORD cbPatchBitSize2 = 0; + DWORD cbPatchBitSize3 = 0; + + // + // Various variants with the patch bit + // + // interface.MPQ.part from WoW build 10958 has + // the MPQ_ATTRIBUTE_PATCH_BIT set, but there's an array of DWORDs instead. + // The array is filled with zeros, so we don't know what it should contain + // + // Zenith.SC2MAP has the MPQ_ATTRIBUTE_PATCH_BIT set, but the bit array is missing + // + // Elimination Tournament 2.w3x's (attributes) have one entry less + // + // There may be two variants: Either the (attributes) file has full + // number of entries, or has one entry less + // + + // Get the expected size of CRC32 array + if(dwAttrFlags & MPQ_ATTRIBUTE_CRC32) + { + cbChecksumSize1 += dwFileTableSize * sizeof(DWORD); + cbChecksumSize2 += cbChecksumSize1 - sizeof(DWORD); + } + + // Get the expected size of FILETIME array + if(dwAttrFlags & MPQ_ATTRIBUTE_FILETIME) + { + cbFileTimeSize1 += dwFileTableSize * sizeof(ULONGLONG); + cbFileTimeSize2 += cbFileTimeSize1 - sizeof(ULONGLONG); + } + + // Get the expected size of MD5 array + if(dwAttrFlags & MPQ_ATTRIBUTE_MD5) + { + cbFileHashSize1 += dwFileTableSize * MD5_DIGEST_SIZE; + cbFileHashSize2 += cbFileHashSize1 - MD5_DIGEST_SIZE; + } + + // Get the expected size of patch bit array + if(dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT) + { + cbPatchBitSize1 = + cbPatchBitSize2 = ((dwFileTableSize + 6) / 8); + cbPatchBitSize3 = dwFileTableSize * sizeof(DWORD); + } + + // Check if the (attributes) file entry count is equal to our file table size + if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1 + cbPatchBitSize1)) + return dwFileTableSize; + + // Check if the (attributes) file entry count is equal to our file table size minus one + if(cbAttrFile == (cbHeaderSize + cbChecksumSize2 + cbFileTimeSize2 + cbFileHashSize2 + cbPatchBitSize2)) + return dwFileTableSize - 1; + + // Zenith.SC2MAP has the MPQ_ATTRIBUTE_PATCH_BIT set, but the bit array is missing + if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1)) + return dwFileTableSize; + + // interface.MPQ.part (WoW build 10958) has the MPQ_ATTRIBUTE_PATCH_BIT set + // but there's an array of DWORDs (filled with zeros) instead of array of bits + if(cbAttrFile == (cbHeaderSize + cbChecksumSize1 + cbFileTimeSize1 + cbFileHashSize1 + cbPatchBitSize3)) + return dwFileTableSize; + +#ifdef __STORMLIB_TEST__ + // Invalid size of the (attributes) file + // Note that many MPQs, especially Warcraft III maps have the size of (attributes) invalid. + // We only perform this check if this is the STORMLIB testprogram itself +// assert(false); +#endif + + return 0; +} + +static int LoadAttributesFile(TMPQArchive * ha, LPBYTE pbAttrFile, DWORD cbAttrFile) +{ + LPBYTE pbAttrFileEnd = pbAttrFile + cbAttrFile; + LPBYTE pbAttrPtr = pbAttrFile; + DWORD dwAttributesEntries = 0; + DWORD i; + + // Load and verify the header + if((pbAttrPtr + sizeof(MPQ_ATTRIBUTES_HEADER)) <= pbAttrFileEnd) + { + PMPQ_ATTRIBUTES_HEADER pAttrHeader = (PMPQ_ATTRIBUTES_HEADER)pbAttrPtr; + + // Verify the header version + BSWAP_ARRAY32_UNSIGNED(pAttrHeader, sizeof(MPQ_ATTRIBUTES_HEADER)); + if(pAttrHeader->dwVersion != MPQ_ATTRIBUTES_V1) + return ERROR_BAD_FORMAT; + + // Verify the flags + if(pAttrHeader->dwFlags & ~MPQ_ATTRIBUTE_ALL) + return ERROR_BAD_FORMAT; + + // Verify whether file size of (attributes) is expected + dwAttributesEntries = CheckSizeOfAttributesFile(cbAttrFile, pAttrHeader->dwFlags, ha->pHeader->dwBlockTableSize); + if(dwAttributesEntries == 0) + return ERROR_BAD_FORMAT; + + ha->dwAttrFlags = pAttrHeader->dwFlags; + pbAttrPtr = (LPBYTE)(pAttrHeader + 1); + } + + // Load the CRC32 (if present) + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_CRC32) + { + LPDWORD ArrayCRC32 = (LPDWORD)pbAttrPtr; + DWORD cbArraySize = dwAttributesEntries * sizeof(DWORD); + + // Verify if there's enough data + if((pbAttrPtr + cbArraySize) > pbAttrFileEnd) + return ERROR_FILE_CORRUPT; + + BSWAP_ARRAY32_UNSIGNED(ArrayCRC32, cbCRC32Size); + for(i = 0; i < dwAttributesEntries; i++) + ha->pFileTable[i].dwCrc32 = ArrayCRC32[i]; + pbAttrPtr += cbArraySize; + } + + // Load the FILETIME (if present) + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_FILETIME) + { + ULONGLONG * ArrayFileTime = (ULONGLONG *)pbAttrPtr; + DWORD cbArraySize = dwAttributesEntries * sizeof(ULONGLONG); + + // Verify if there's enough data + if((pbAttrPtr + cbArraySize) > pbAttrFileEnd) + return ERROR_FILE_CORRUPT; + + BSWAP_ARRAY64_UNSIGNED(ArrayFileTime, cbFileTimeSize); + for(i = 0; i < dwAttributesEntries; i++) + ha->pFileTable[i].FileTime = ArrayFileTime[i]; + pbAttrPtr += cbArraySize; + } + + // Load the MD5 (if present) + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_MD5) + { + LPBYTE ArrayMd5 = pbAttrPtr; + DWORD cbArraySize = dwAttributesEntries * MD5_DIGEST_SIZE; + + // Verify if there's enough data + if((pbAttrPtr + cbArraySize) > pbAttrFileEnd) + return ERROR_FILE_CORRUPT; + + for(i = 0; i < dwAttributesEntries; i++) + { + memcpy(ha->pFileTable[i].md5, ArrayMd5, MD5_DIGEST_SIZE); + ArrayMd5 += MD5_DIGEST_SIZE; + } + pbAttrPtr += cbArraySize; + } + + // Read the patch bit for each file (if present) + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT) + { + LPBYTE pbBitArray = pbAttrPtr; + DWORD cbArraySize = (dwAttributesEntries + 7) / 8; + DWORD dwByteIndex = 0; + DWORD dwBitMask = 0x80; + + // Verify if there's enough data + if((pbAttrPtr + cbArraySize) == pbAttrFileEnd) + { + for(i = 0; i < dwAttributesEntries; i++) + { + ha->pFileTable[i].dwFlags |= (pbBitArray[dwByteIndex] & dwBitMask) ? MPQ_FILE_PATCH_FILE : 0; + dwByteIndex += (dwBitMask & 0x01); + dwBitMask = (dwBitMask << 0x07) | (dwBitMask >> 0x01); + } + } + } + + return ERROR_SUCCESS; +} + +static LPBYTE CreateAttributesFile(TMPQArchive * ha, DWORD * pcbAttrFile) +{ + PMPQ_ATTRIBUTES_HEADER pAttrHeader; + TFileEntry * pFileTableEnd = ha->pFileTable + ha->dwFileTableSize; + TFileEntry * pFileEntry; + LPBYTE pbAttrFile; + LPBYTE pbAttrPtr; + size_t cbAttrFile; + DWORD dwFinalEntries = ha->dwFileTableSize + ha->dwReservedFiles; + + // Check if we need patch bits in the (attributes) file + for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++) + { + if(pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE) + { + ha->dwAttrFlags |= MPQ_ATTRIBUTE_PATCH_BIT; + break; + } + } + + // Allocate the buffer for holding the entire (attributes) + // Allodate 1 byte more (See GetSizeOfAttributesFile for more info) + cbAttrFile = GetSizeOfAttributesFile(ha->dwAttrFlags, dwFinalEntries); + pbAttrFile = pbAttrPtr = STORM_ALLOC(BYTE, cbAttrFile + 1); + if(pbAttrFile != NULL) + { + // Make sure it's all zeroed + memset(pbAttrFile, 0, cbAttrFile + 1); + + // Write the header of the (attributes) file + pAttrHeader = (PMPQ_ATTRIBUTES_HEADER)pbAttrPtr; + pAttrHeader->dwVersion = BSWAP_INT32_UNSIGNED(100); + pAttrHeader->dwFlags = BSWAP_INT32_UNSIGNED((ha->dwAttrFlags & MPQ_ATTRIBUTE_ALL)); + pbAttrPtr = (LPBYTE)(pAttrHeader + 1); + + // Write the array of CRC32, if present + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_CRC32) + { + LPDWORD pArrayCRC32 = (LPDWORD)pbAttrPtr; + + // Copy from file table + for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++) + *pArrayCRC32++ = BSWAP_INT32_UNSIGNED(pFileEntry->dwCrc32); + + // Skip the reserved entries + pbAttrPtr = (LPBYTE)(pArrayCRC32 + ha->dwReservedFiles); + } + + // Write the array of file time + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_FILETIME) + { + ULONGLONG * pArrayFileTime = (ULONGLONG *)pbAttrPtr; + + // Copy from file table + for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++) + *pArrayFileTime++ = BSWAP_INT64_UNSIGNED(pFileEntry->FileTime); + + // Skip the reserved entries + pbAttrPtr = (LPBYTE)(pArrayFileTime + ha->dwReservedFiles); + } + + // Write the array of MD5s + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_MD5) + { + LPBYTE pbArrayMD5 = pbAttrPtr; + + // Copy from file table + for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++) + { + memcpy(pbArrayMD5, pFileEntry->md5, MD5_DIGEST_SIZE); + pbArrayMD5 += MD5_DIGEST_SIZE; + } + + // Skip the reserved items + pbAttrPtr = pbArrayMD5 + (ha->dwReservedFiles * MD5_DIGEST_SIZE); + } + + // Write the array of patch bits + if(ha->dwAttrFlags & MPQ_ATTRIBUTE_PATCH_BIT) + { + LPBYTE pbBitArray = pbAttrPtr; + DWORD dwByteIndex = 0; + BYTE dwBitMask = 0x80; + + // Copy from file table + for(pFileEntry = ha->pFileTable; pFileEntry < pFileTableEnd; pFileEntry++) + { + // Set the bit, if needed + if(pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE) + pbBitArray[dwByteIndex] |= dwBitMask; + + // Update bit index and bit mask + dwByteIndex += (dwBitMask & 0x01); + dwBitMask = (dwBitMask << 0x07) | (dwBitMask >> 0x01); + } + + // Having incremented the bit array just by the number of items in the file table + // will create the bit array one byte less of the number of files is a multiplier of 8). + // Blizzard MPQs have the same feature. + + // Move past the bit array + pbAttrPtr = (pbBitArray + dwByteIndex) + ((dwBitMask & 0x7F) ? 1 : 0); + } + + // Now we expect that current position matches the estimated size + // Note that if there is 1 extra bit above the byte size, + // the table is actually 1 byte shorted in Blizzard MPQs. See GetSizeOfAttributesFile + assert((size_t)(pbAttrPtr - pbAttrFile) == cbAttrFile); + } + + // Give away the attributes file + if(pcbAttrFile != NULL) + *pcbAttrFile = (DWORD)cbAttrFile; + return pbAttrFile; +} + +//----------------------------------------------------------------------------- +// Public functions (internal use by StormLib) + +int SAttrLoadAttributes(TMPQArchive * ha) +{ + HANDLE hFile = NULL; + LPBYTE pbAttrFile; + DWORD dwBytesRead; + DWORD cbAttrFile = 0; + int nError = ERROR_FILE_CORRUPT; + + // File table must be initialized + assert(ha->pFileTable != NULL); + + // Don't load the attributes file from malformer Warcraft III maps + if(ha->dwFlags & MPQ_FLAG_MALFORMED) + return ERROR_FILE_CORRUPT; + + // Attempt to open the "(attributes)" file. + if(SFileOpenFileEx((HANDLE)ha, ATTRIBUTES_NAME, SFILE_OPEN_ANY_LOCALE, &hFile)) + { + // Retrieve and check size of the (attributes) file + cbAttrFile = SFileGetFileSize(hFile, NULL); + + // Size of the (attributes) might be 1 byte less than expected + // See GetSizeOfAttributesFile for more info + pbAttrFile = STORM_ALLOC(BYTE, cbAttrFile + 1); + if(pbAttrFile != NULL) + { + // Set the last byte to 0 in case the size should be 1 byte greater + pbAttrFile[cbAttrFile] = 0; + + // Load the entire file to memory + SFileReadFile(hFile, pbAttrFile, cbAttrFile, &dwBytesRead, NULL); + if(dwBytesRead == cbAttrFile) + nError = LoadAttributesFile(ha, pbAttrFile, cbAttrFile); + + // Free the buffer + STORM_FREE(pbAttrFile); + } + + // Close the attributes file + SFileCloseFile(hFile); + } + + return nError; +} + +// Saves the (attributes) to the MPQ +int SAttrFileSaveToMpq(TMPQArchive * ha) +{ + TMPQFile * hf = NULL; + LPBYTE pbAttrFile; + DWORD cbAttrFile = 0; + int nError = ERROR_SUCCESS; + + // Only save the attributes if we should do so + if(ha->dwFileFlags2 != 0) + { + // At this point, we expect to have at least one reserved entry in the file table + assert(ha->dwFlags & MPQ_FLAG_ATTRIBUTES_INVALID); + assert(ha->dwReservedFiles >= 1); + + // Create the raw data that is to be written to (attributes) + // Note: Blizzard MPQs have entries for (listfile) and (attributes), + // but they are filled empty + pbAttrFile = CreateAttributesFile(ha, &cbAttrFile); + if(pbAttrFile != NULL) + { + // We expect it to be nonzero size + assert(cbAttrFile != 0); + + // Determine the real flags for (attributes) + if(ha->dwFileFlags2 == MPQ_FILE_EXISTS) + ha->dwFileFlags2 = GetDefaultSpecialFileFlags(cbAttrFile, ha->pHeader->wFormatVersion); + + // Create the attributes file in the MPQ + nError = SFileAddFile_Init(ha, ATTRIBUTES_NAME, + 0, + cbAttrFile, + LANG_NEUTRAL, + ha->dwFileFlags2 | MPQ_FILE_REPLACEEXISTING, + &hf); + + // Write the attributes file raw data to it + if(nError == ERROR_SUCCESS) + { + // Write the content of the attributes file to the MPQ + nError = SFileAddFile_Write(hf, pbAttrFile, cbAttrFile, MPQ_COMPRESSION_ZLIB); + SFileAddFile_Finish(hf); + } + + // Free the attributes buffer + STORM_FREE(pbAttrFile); + } + else + { + // If the list file is empty, we assume ERROR_SUCCESS + nError = (cbAttrFile == 0) ? ERROR_SUCCESS : ERROR_NOT_ENOUGH_MEMORY; + } + + // If the save process succeeded, we clear the MPQ_FLAG_ATTRIBUTE_INVALID flag + if(nError == ERROR_SUCCESS) + { + ha->dwFlags &= ~MPQ_FLAG_ATTRIBUTES_INVALID; + ha->dwReservedFiles--; + } + } + + return nError; +} + +//----------------------------------------------------------------------------- +// Public functions + +DWORD WINAPI SFileGetAttributes(HANDLE hMpq) +{ + TMPQArchive * ha = (TMPQArchive *)hMpq; + + // Verify the parameters + if(!IsValidMpqHandle(hMpq)) + { + SetLastError(ERROR_INVALID_PARAMETER); + return SFILE_INVALID_ATTRIBUTES; + } + + return ha->dwAttrFlags; +} + +bool WINAPI SFileSetAttributes(HANDLE hMpq, DWORD dwFlags) +{ + TMPQArchive * ha = (TMPQArchive *)hMpq; + + // Verify the parameters + if(!IsValidMpqHandle(hMpq)) + { + SetLastError(ERROR_INVALID_PARAMETER); + return false; + } + + // Not allowed when the archive is read-only + if(ha->dwFlags & MPQ_FLAG_READ_ONLY) + { + SetLastError(ERROR_ACCESS_DENIED); + return false; + } + + // Set the attributes + InvalidateInternalFiles(ha); + ha->dwAttrFlags = (dwFlags & MPQ_ATTRIBUTE_ALL); + return true; +} + +bool WINAPI SFileUpdateFileAttributes(HANDLE hMpq, const char * szFileName) +{ + hash_state md5_state; + TMPQArchive * ha = (TMPQArchive *)hMpq; + TMPQFile * hf; + BYTE Buffer[0x1000]; + HANDLE hFile = NULL; + DWORD dwTotalBytes = 0; + DWORD dwBytesRead; + DWORD dwCrc32; + + // Verify the parameters + if(!IsValidMpqHandle(ha)) + { + SetLastError(ERROR_INVALID_PARAMETER); + return false; + } + + // Not allowed when the archive is read-only + if(ha->dwFlags & MPQ_FLAG_READ_ONLY) + { + SetLastError(ERROR_ACCESS_DENIED); + return false; + } + + // Attempt to open the file + if(!SFileOpenFileEx(hMpq, szFileName, SFILE_OPEN_BASE_FILE, &hFile)) + return false; + + // Get the file size + hf = (TMPQFile *)hFile; + dwTotalBytes = hf->pFileEntry->dwFileSize; + + // Initialize the CRC32 and MD5 contexts + md5_init(&md5_state); + dwCrc32 = crc32(0, Z_NULL, 0); + + // Go through entire file and calculate both CRC32 and MD5 + while(dwTotalBytes != 0) + { + // Read data from file + SFileReadFile(hFile, Buffer, sizeof(Buffer), &dwBytesRead, NULL); + if(dwBytesRead == 0) + break; + + // Update CRC32 and MD5 + dwCrc32 = crc32(dwCrc32, Buffer, dwBytesRead); + md5_process(&md5_state, Buffer, dwBytesRead); + + // Decrement the total size + dwTotalBytes -= dwBytesRead; + } + + // Update both CRC32 and MD5 + hf->pFileEntry->dwCrc32 = dwCrc32; + md5_done(&md5_state, hf->pFileEntry->md5); + + // Remember that we need to save the MPQ tables + InvalidateInternalFiles(ha); + SFileCloseFile(hFile); + return true; +} diff --git a/src/SFileOpenArchive.cpp b/src/SFileOpenArchive.cpp index 719aefe..98d8766 100644 --- a/src/SFileOpenArchive.cpp +++ b/src/SFileOpenArchive.cpp @@ -84,6 +84,9 @@ static int VerifyMpqTablePositions(TMPQArchive * ha, ULONGLONG FileSize) if(pHeader->wHashTablePosHi || pHeader->dwHashTablePos) { ByteOffset = ha->MpqPos + MAKE_OFFSET64(pHeader->wHashTablePosHi, pHeader->dwHashTablePos); + if((pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1) && (ha->dwFlags & MPQ_FLAG_MALFORMED)) + ByteOffset = (DWORD)ha->MpqPos + pHeader->dwHashTablePos; + if(ByteOffset > FileSize) return ERROR_BAD_FORMAT; } @@ -91,18 +94,12 @@ static int VerifyMpqTablePositions(TMPQArchive * ha, ULONGLONG FileSize) // Check the begin of block table if(pHeader->wBlockTablePosHi || pHeader->dwBlockTablePos) { + ByteOffset = ha->MpqPos + MAKE_OFFSET64(pHeader->wBlockTablePosHi, pHeader->dwBlockTablePos); if((pHeader->wFormatVersion == MPQ_FORMAT_VERSION_1) && (ha->dwFlags & MPQ_FLAG_MALFORMED)) - { ByteOffset = (DWORD)ha->MpqPos + pHeader->dwBlockTablePos; - if(ByteOffset > FileSize) - return ERROR_BAD_FORMAT; - } - else - { - ByteOffset = ha->MpqPos + MAKE_OFFSET64(pHeader->wBlockTablePosHi, pHeader->dwBlockTablePos); - if(ByteOffset > FileSize) - return ERROR_BAD_FORMAT; - } + + if(ByteOffset > FileSize) + return ERROR_BAD_FORMAT; } // Check the begin of hi-block table diff --git a/src/SFileVerify.cpp b/src/SFileVerify.cpp index 5030d66..8d7e950 100644 --- a/src/SFileVerify.cpp +++ b/src/SFileVerify.cpp @@ -26,15 +26,15 @@ // Known Blizzard public keys // Created by Jean-Francois Roy using OpenSSL -static const char * szBlizzardWeakPrivateKey =
- "-----BEGIN PRIVATE KEY-----"
- "MIIBOQIBAAJBAJJidwS/uILMBSO5DLGsBFknIXWWjQJe2kfdfEk3G/j66w4KkhZ1"
- "V61Rt4zLaMVCYpDun7FLwRjkMDSepO1q2DcCAwEAAQJANtiztVDMJh2hE1hjPDKy"
- "UmEJ9U/aN3gomuKOjbQbQ/bWWcM/WfhSVHmPqtqh/bQI2UXFr0rnXngeteZHLr/b"
- "8QIhAMuWriSKGMACw18/rVVfUrThs915odKBH1Alr3vMVVzZAiEAuBHPSQkgwcb6"
- "L4MWaiKuOzq08mSyNqPeN8oSy18q848CIHeMn+3s+eOmu7su1UYQl6yH7OrdBd1q"
- "3UxfFNEJiAbhAiAqxdCyOxHGlbM7aS3DOg3cq5ayoN2cvtV7h1R4t8OmVwIgF+5z"
- "/6vkzBUsZhd8Nwyis+MeQYH0rpFpMKdTlqmPF2Q="
+static const char * szBlizzardWeakPrivateKey = + "-----BEGIN PRIVATE KEY-----" + "MIIBOQIBAAJBAJJidwS/uILMBSO5DLGsBFknIXWWjQJe2kfdfEk3G/j66w4KkhZ1" + "V61Rt4zLaMVCYpDun7FLwRjkMDSepO1q2DcCAwEAAQJANtiztVDMJh2hE1hjPDKy" + "UmEJ9U/aN3gomuKOjbQbQ/bWWcM/WfhSVHmPqtqh/bQI2UXFr0rnXngeteZHLr/b" + "8QIhAMuWriSKGMACw18/rVVfUrThs915odKBH1Alr3vMVVzZAiEAuBHPSQkgwcb6" + "L4MWaiKuOzq08mSyNqPeN8oSy18q848CIHeMn+3s+eOmu7su1UYQl6yH7OrdBd1q" + "3UxfFNEJiAbhAiAqxdCyOxHGlbM7aS3DOg3cq5ayoN2cvtV7h1R4t8OmVwIgF+5z" + "/6vkzBUsZhd8Nwyis+MeQYH0rpFpMKdTlqmPF2Q=" "-----END PRIVATE KEY-----"; static const char * szBlizzardWeakPublicKey = diff --git a/src/huffman/HuffTree.xls b/src/huffman/HuffTree.xls Binary files differindex 68e0664..95a85a4 100644 --- a/src/huffman/HuffTree.xls +++ b/src/huffman/HuffTree.xls diff --git a/storm_dll/Storm_dll.bat b/storm_dll/Storm_dll.bat deleted file mode 100644 index 466a3d3..0000000 --- a/storm_dll/Storm_dll.bat +++ /dev/null @@ -1,18 +0,0 @@ -@echo off -rem Post-build batch for StormDll project -rem Called as StormDll.bat $(PlatformName) $(ConfigurationName) -rem Example: StormDll.bat x64 Debug - -if x%1 == xWin32 goto PlatformWin32 -if x%1 == xx64 goto PlatformWin64 -goto exit - -:PlatformWin32 -copy .\bin\Storm_dll\%1\%2\*.lib . -goto exit - -:PlatformWin64 -copy .\bin\Storm_dll\%1\%2\*.lib . -goto exit - -:exit diff --git a/storm_dll/Storm_dll.sln b/storm_dll/Storm_dll.sln index 7f1616a..3b323fa 100644 --- a/storm_dll/Storm_dll.sln +++ b/storm_dll/Storm_dll.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Storm_dll", "Storm_dll.vcproj", "{BD600973-C6FA-4CE3-8821-67F6418B7F9C}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PhotoSort", "Storm_test.vcproj", "{5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -19,6 +21,14 @@ Global {BD600973-C6FA-4CE3-8821-67F6418B7F9C}.Release|Win32.Build.0 = Release|Win32 {BD600973-C6FA-4CE3-8821-67F6418B7F9C}.Release|x64.ActiveCfg = Release|x64 {BD600973-C6FA-4CE3-8821-67F6418B7F9C}.Release|x64.Build.0 = Release|x64 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Debug|Win32.ActiveCfg = Debug|Win32 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Debug|Win32.Build.0 = Debug|Win32 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Debug|x64.ActiveCfg = Debug|x64 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Debug|x64.Build.0 = Debug|x64 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Release|Win32.ActiveCfg = Release|Win32 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Release|Win32.Build.0 = Release|Win32 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Release|x64.ActiveCfg = Release|x64 + {5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/storm_dll/storm.dll b/storm_dll/storm.dll Binary files differindex d4daca0..966c226 100644 --- a/storm_dll/storm.dll +++ b/storm_dll/storm.dll diff --git a/storm_dll/storm.lib b/storm_dll/storm.lib Binary files differnew file mode 100644 index 0000000..299a6d4 --- /dev/null +++ b/storm_dll/storm.lib diff --git a/storm_dll/storm_dll.h b/storm_dll/storm_dll.h index 6d67820..8625bd0 100644 --- a/storm_dll/storm_dll.h +++ b/storm_dll/storm_dll.h @@ -29,8 +29,7 @@ #define SCOMP(Name) SComp##Name #endif - -// Just in case anyone is still using C out there +// Make sure the functions are exported as C functions #ifdef __cplusplus extern "C" { #endif @@ -46,7 +45,7 @@ DWORD WINAPI SFILE(GetFileSize)(HANDLE hFile, LPDWORD lpFileSizeHigh); BOOL WINAPI SFILE(GetFileArchive)(HANDLE hFile, HANDLE *hMPQ); BOOL WINAPI SFILE(GetFileName)(HANDLE hFile, LPCSTR lpBuffer, DWORD dwBufferLength); DWORD WINAPI SFILE(SetFilePointer)(HANDLE hFile, long lDistanceToMove, PLONG lplDistanceToMoveHigh, DWORD dwMoveMethod); -BOOL WINAPI SFILE(ReadFile)(HANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped); +BOOL WINAPI SFILE(ReadFile)(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped); LCID WINAPI SFILE(SetLocale)(LCID nNewLocale); BOOL WINAPI SFILE(GetBasePath)(LPCSTR lpBuffer, DWORD dwBufferLength); BOOL WINAPI SFILE(SetBasePath)(LPCSTR lpNewBasePath); @@ -55,13 +54,12 @@ BOOL WINAPI SFILE(SetBasePath)(LPCSTR lpNewBasePath); BOOL WINAPI SCOMP(Compress) (char * pbOutBuffer, int * pdwOutLength, char * pbInBuffer, int dwInLength, int uCmp, int uCmpType, int nCmpLevel); BOOL WINAPI SCOMP(Decompress)(char * pbOutBuffer, int * pdwOutLength, char * pbInBuffer, int dwInLength); +#ifdef __cplusplus +} +#endif #if defined(_MSC_VER) && !defined(BUILDING_STORM_CPP) #pragma comment(lib, "Storm.lib") // Force linking Storm.lib and thus Storm.dll #endif -#ifdef __cplusplus -} -#endif - #endif // __STORM_H__ diff --git a/storm_dll/storm_test.cpp b/storm_dll/storm_test.cpp new file mode 100644 index 0000000..a14b550 --- /dev/null +++ b/storm_dll/storm_test.cpp @@ -0,0 +1,36 @@ +/*****************************************************************************/ +/* Storm_test.cpp Copyright (c) Ladislav Zezula 2014 */ +/*---------------------------------------------------------------------------*/ +/* Test module for storm.dll (original Blizzard MPQ dynalic library */ +/*---------------------------------------------------------------------------*/ +/* Date Ver Who Comment */ +/* -------- ---- --- ------- */ +/* 24.08.14 1.00 Lad The first version of Storm_test.cpp */ +/*****************************************************************************/ + +#define _CRT_NON_CONFORMING_SWPRINTFS +#define _CRT_SECURE_NO_DEPRECATE +#include <stdio.h> + +#ifdef _MSC_VER +#include <crtdbg.h> +#endif + +#define STORM_ALTERNATE_NAMES +#include "storm_dll.h" // Header file for Storm.dll + +//----------------------------------------------------------------------------- +// Main + +int main() +{ + HANDLE hMpq = NULL; + + _asm int 3; + if(StormOpenArchive("E:\\Multimedia\\MPQs\\1995 - Test MPQs\\MPQ_2014_v1_ProtectedMap_Spazzler3.w3x", 0, 0, &hMpq)) + { + StormCloseArchive(hMpq); + } + + return 0; +} diff --git a/storm_dll/storm_test.vcproj b/storm_dll/storm_test.vcproj new file mode 100644 index 0000000..571ad8e --- /dev/null +++ b/storm_dll/storm_test.vcproj @@ -0,0 +1,351 @@ +<?xml version="1.0" encoding="windows-1250"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9,00" + Name="Storm_test" + ProjectGUID="{5B0E680A-EC4E-402E-AE0D-18ADD2DC6EBE}" + RootNamespace="Storm_test" + Keyword="Win32Proj" + TargetFrameworkVersion="131072" + > + <Platforms> + <Platform + Name="Win32" + /> + <Platform + Name="x64" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../aaa/inc;exif" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="4" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/Storm_test.exe" + LinkIncremental="2" + AdditionalLibraryDirectories="../aaa/lib32" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/Storm_test.pdb" + SubSystem="1" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Debug|x64" + OutputDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="../aaa/inc" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="4" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/Storm_test.exe" + LinkIncremental="2" + AdditionalLibraryDirectories="../aaa/lib64" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/Storm_test.pdb" + SubSystem="1" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="../aaa/inc" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="4" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/Storm_test.exe" + LinkIncremental="1" + AdditionalLibraryDirectories="../aaa/lib32" + GenerateDebugInformation="true" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + CommandLine="" + /> + </Configuration> + <Configuration + Name="Release|x64" + OutputDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + IntermediateDirectory=".\bin\$(PlatformName)\$(ConfigurationName)" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TargetEnvironment="3" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="../aaa/inc" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="4" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/Storm_test.exe" + LinkIncremental="1" + AdditionalLibraryDirectories="../aaa/lib64" + GenerateDebugInformation="true" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="17" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + CommandLine="" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\Storm_test.cpp" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/test/StormTest.cpp b/test/StormTest.cpp index bac7c2a..dda622d 100644 --- a/test/StormTest.cpp +++ b/test/StormTest.cpp @@ -1436,6 +1436,13 @@ static TFileData * LoadMpqFile(TLogHelper * pLogger, HANDLE hMpq, const char * s nError = pLogger->PrintError("Failed to query the file size"); } + // Spazzler protector: Creates fake files with size of 0x7FFFE7CA + if(nError == ERROR_SUCCESS) + { + if(dwFileSizeLo > 0x1FFFFFFF) + nError = ERROR_FILE_CORRUPT; + } + // Allocate buffer for the file content if(nError == ERROR_SUCCESS) { @@ -3556,7 +3563,7 @@ int main(int argc, char * argv[]) // Search all testing archives and verify their SHA1 hash // if(nError == ERROR_SUCCESS) // nError = FindFiles(ForEachFile_VerifyFileChecksum, szMpqSubDir); - +/* // Test reading linear file without bitmap if(nError == ERROR_SUCCESS) nError = TestFileStreamOperations("MPQ_2013_v4_alternate-original.MPQ", 0); @@ -3608,7 +3615,7 @@ int main(int argc, char * argv[]) // Open a stream, paired with remote master (takes hell lot of time!) // if(nError == ERROR_SUCCESS) // nError = TestReadFile_MasterMirror("MPQ_2013_v4_alternate-downloaded.MPQ", "http://www.zezula.net\\mpqs\\alternate.zip", false); - +*/ // Search in listfile if(nError == ERROR_SUCCESS) nError = TestSearchListFile("ListFile_Blizzard.txt"); @@ -3664,6 +3671,9 @@ int main(int argc, char * argv[]) if(nError == ERROR_SUCCESS) nError = TestOpenArchive("MPQ_2014_v1_ProtectedMap_Spazzler2.w3x"); + if(nError == ERROR_SUCCESS) + nError = TestOpenArchive("MPQ_2014_v1_ProtectedMap_Spazzler3.w3x"); + // Open an Warcraft III map locked by the BOBA protector if(nError == ERROR_SUCCESS) nError = TestOpenArchive("MPQ_2002_v1_ProtectedMap_BOBA.w3m"); |