Current News
Archived News
Search News
Discussion Forum


Old Forum
Install Programs More Downloads...
Troubleshooting
Source Code
Format Specs.
Misc. Information
Non-SF Stuff
Links




1 /* License information for this code is in license.txt */
3 #define WIN32_LEAN_AND_MEAN
5 // Includes
6 #include <windows.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10 #ifdef _USRDLL
11 #include <shellapi.h>
12 #endif
14 #include "../SComp/SComp.h"
16 #include "SFmpqInternal.h"
17 #include "MpqCrypt.h"
18 #include "MpqHashTable.h"
19 #include "MpqBlockTable.h"
20 #include "SFmpqapi.h"
21 #include "SFUtil.h"
22 #include "SFTypes.h"
24 struct SFMPQAPIMODULE {
25         SFMPQAPIMODULE();
26         ~SFMPQAPIMODULE();
27 } SFMpqApi;
29 #ifndef _WIN32
30 #define SFMPQ_STATIC
31 #endif
33 #if defined(SFMPQ_STATIC) || !defined(SFMPQAPI_EXPORTS)
34 #define DllMain SFMpqMain
36 struct SFMPQLIBMODULE {
37         SFMPQLIBMODULE();
38         ~SFMPQLIBMODULE();
39 } SFMpqLib;
41 BOOL APIENTRY DllMain(HINSTANCE hInstDLL, DWORD  ul_reason_for_call, LPVOID lpReserved);
43 SFMPQLIBMODULE::SFMPQLIBMODULE()
44 {
45         SFMpqMain(0,DLL_PROCESS_ATTACH,0);
46 }
48 SFMPQLIBMODULE::~SFMPQLIBMODULE()
49 {
50         SFMpqDestroy();
51         SFMpqMain(0,DLL_PROCESS_DETACH,0);
52 }
54 #endif
56 LCID LocaleID = 0;
57 WORD wCreationVersion = 0;
58 BOOL SFMpqInit = FALSE;
59 HINSTANCE hStorm = 0;
60 #ifdef _USRDLL
61 HINSTANCE hSFMpqInstance = 0;
62 #endif
64 #define SFMPQ_CURRENT_VERSION {1,0,8,1}
66 SFMPQVERSION SFMpqVersion = SFMPQ_CURRENT_VERSION;
67 LPCSTR SFMpqVersionString = "ShadowFlare MPQ API Library v1.08";
68 float MpqVersion = 2.00;
70 // Class to simulate _alloca for cross-compiler compatibility
71 class TempAlloc {
72 public:
73         TempAlloc();
74         ~TempAlloc();
75         TempAlloc(unsigned long int AllocSize);
76         void * Alloc(unsigned long int AllocSize);
77         void *lpAllocAddress;
78 };
80 LPCSTR ID_MPQ = "MPQ\x1A";
81 LPCSTR ID_BN3 = "BN3\x1A";
82 LPCSTR ID_RIFF = "RIFF";
83 LPCSTR ID_WAVE = "WAVEfmt ";
84 LPCSTR INTERNAL_LISTFILE = "(listfile)";
85 LPCSTR INTERNAL_FILES = "(attributes)\n(listfile)\n(signature)";
86 LPCSTR Storm_dll = "Storm.dll";
87 LPCSTR UNKNOWN_OUT = "~unknowns\\unknown_%08x";
88 LPCSTR UNKNOWN_IN = "~unknowns\\unknown_%x";
89 LPCSTR UNKNOWN_CMP = "~unknowns\\unknown_";
90 #define UNKNOWN_LEN 18
92 LCID availLocales[7] = {0x0000,0x0407,0x0409,0x040A,0x040C,0x0410,0x0416};
93 #define nLocales 7
95 #define MAX_MPQ_PATH 260;
97 MPQARCHIVE **lpOpenMpq = 0;
98 DWORD dwOpenMpqCount = 0;
99 MPQARCHIVE * FirstLastMpq[2] = {0,0};
100 MPQFILE **lpOpenFile = 0;
101 DWORD dwOpenFileCount = 0;
102 MPQFILE * FirstLastFile[2] = {0,0};
104 char StormBasePath[MAX_PATH+1];
106 typedef BOOL (WINAPI* funcSCompCompress)(LPVOID lpvDestinationMem, LPDWORD lpdwCompressedSize, LPVOID lpvSourceMem, DWORD dwDecompressedSize, DWORD dwCompressionType, DWORD dwCompressionSubType, DWORD dwWAVQuality);
107 typedef BOOL (WINAPI* funcSCompDecompress)(LPVOID lpvDestinationMem, LPDWORD lpdwDecompressedSize, LPVOID lpvSourceMem, DWORD dwCompressedSize);
108 funcSCompCompress stormSCompCompress = 0;
109 funcSCompDecompress stormSCompDecompress = 0;
111 void LoadStorm();
112 void FreeStorm();
114 BOOL WINAPI MpqOpenArchiveEx(LPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags, MPQHANDLE *hMPQ, DWORD dwFlags2, DWORD dwMaximumFilesInArchive, DWORD dwBlockSize);
115 UInt64 WINAPI SFileFindMpqHeaderEx(HANDLE hFile, UInt64 qwStart = 0, UInt64 qwLength = 0);
116 DWORD GetFullPath(LPCSTR lpFileName, char *lpBuffer, DWORD dwBufferLength);
117 MPQHANDLE GetHashTableEntry(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID FileLocale);
118 MPQHANDLE GetHashTableEntryOfHash(MPQHANDLE hMPQ, DWORD dwTablePos, DWORD dwNameHashA, DWORD dwNameHashB, LCID FileLocale);
119 MPQHANDLE GetFreeHashTableEntry(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID FileLocale, BOOL ReturnExisting);
120 BOOL SearchOpenArchives(LPCSTR lpFileName, MPQHANDLE *hMPQ, MPQHANDLE *hFile);
121 void SortOpenArchivesByPriority();
122 DWORD GetHandleType(MPQHANDLE hFile);
123 BOOL AddToInternalListing(MPQHANDLE hMPQ, LPCSTR lpFileName);
124 BOOL RemoveFromInternalListing(MPQHANDLE hMPQ, LPCSTR lpFileName);
125 DWORD DetectFileSeedEx(MPQARCHIVE * mpqOpenArc, HASHTABLEENTRY * lpHashEntry, LPCSTR * lplpFileName);
127 BOOL APIENTRY DllMain( HINSTANCE hInstDLL, 
128                        DWORD  ul_reason_for_call, 
129                        LPVOID lpReserved
130                                          )
132 #ifdef _WIN32
133         char *lpExeName,*buffer;DWORD slen;
134 #endif
135         DWORD dwLastCount;
137 #ifdef _USRDLL
138         hSFMpqInstance = hInstDLL;
139 #endif
141     switch (ul_reason_for_call)
142         {
143                 case DLL_PROCESS_ATTACH:
144                         InitCryptTable();
145                         dwHashTableKey = HashString("(hash table)",HASH_KEY);
146                         dwBlockTableKey = HashString("(block table)",HASH_KEY);
147 #ifdef _WIN32
148                         lpExeName = (char *)SFAlloc(MAX_PATH+1);
149                         if (lpExeName) {
150                                 slen = GetModuleFileName(0,lpExeName,MAX_PATH);
151                                 lpExeName[slen] = 0;
152                                 buffer = lpExeName+strlen(lpExeName);
153                                 while (*buffer!='\\') {*buffer = 0;buffer--;}
154                                 SFileSetBasePath(lpExeName);
155                                 SFFree(lpExeName);
156                         }
157 #else
158                         SFileSetBasePath("./");
159 #endif
160                         break;
161                 case DLL_THREAD_ATTACH:
162                         break;
163                 case DLL_THREAD_DETACH:
164                         break;
165                 case DLL_PROCESS_DETACH:
166                         if (lpOpenFile) {
167                                 while (dwOpenFileCount>0) {
168                                         dwLastCount = dwOpenFileCount;
169                                         SFileCloseFile((MPQHANDLE)lpOpenFile[dwOpenFileCount-1]);
170                                         if (dwLastCount==dwOpenFileCount) dwOpenFileCount--;
171                                 }
173                                 SFFree(lpOpenFile);
174                         }
176                         if (lpOpenMpq) {
177                                 while (dwOpenMpqCount>0) {
178                                         dwLastCount = dwOpenMpqCount;
179                                         SFileCloseArchive((MPQHANDLE)lpOpenMpq[dwOpenMpqCount-1]);
180                                         if (dwLastCount==dwOpenMpqCount) dwOpenMpqCount--;
181                                 }
183                                 SFFree(lpOpenMpq);
184                         }
185                         break;
186     }
188     return TRUE;
191 TempAlloc::TempAlloc()
193         lpAllocAddress = 0;
196 TempAlloc::TempAlloc(unsigned long int AllocSize)
198         lpAllocAddress = SFAlloc(AllocSize);
201 TempAlloc::~TempAlloc()
203         if (lpAllocAddress) {
204                 SFFree(lpAllocAddress);
205                 lpAllocAddress = 0;
206         }
209 void * TempAlloc::Alloc(unsigned long int AllocSize)
211         lpAllocAddress = SFAlloc(AllocSize);
212         return lpAllocAddress;
215 SFMPQAPIMODULE::SFMPQAPIMODULE()
217         //LoadStorm();
220 SFMPQAPIMODULE::~SFMPQAPIMODULE()
222         FreeStorm();
225 BOOL SFMPQAPI WINAPI SFileDestroy() {
226         return TRUE;
229 void SFMPQAPI WINAPI StormDestroy() {
232 void SFMPQAPI WINAPI SFMpqDestroy() {
233         //FreeStorm();
236 BOOL SFMPQAPI WINAPI MpqInitialize()
238         return (SFMpqInit=TRUE);
241 LPCSTR SFMPQAPI WINAPI MpqGetVersionString()
243         return SFMpqVersionString;
246 float SFMPQAPI WINAPI MpqGetVersion()
248         return MpqVersion;
251 LPCSTR SFMPQAPI WINAPI SFMpqGetVersionString()
253         return SFMpqVersionString;
256 DWORD  SFMPQAPI WINAPI SFMpqGetVersionString2(LPCSTR lpBuffer, DWORD dwBufferLength)
258         if (!lpBuffer) {
259                 SetLastError(ERROR_INVALID_PARAMETER);
260                 return strlen(SFMpqVersionString)+1;
261         }
263         DWORD slen = strlen(SFMpqVersionString)+1;
265         if (dwBufferLength>=slen) memcpy((void *)lpBuffer,SFMpqVersionString,slen);
266         else memcpy((void *)lpBuffer,SFMpqVersionString,dwBufferLength);
268         return slen;
271 SFMPQVERSION SFMPQAPI WINAPI SFMpqGetVersion()
273         return SFMpqVersion;
276 void SFMPQAPI WINAPI AboutSFMpq()
278 #ifdef _USRDLL
279         if (hSFMpqInstance) {
280                 char szAboutPage[MAX_PATH+13];
281                 strcpy(szAboutPage,"res://");
282                 GetModuleFileName(hSFMpqInstance,szAboutPage+6,MAX_PATH);
283                 strcat(szAboutPage,"/about");
284                 ShellExecute(0,0,szAboutPage,0,0,SW_SHOWNORMAL);
285         }
286 #endif
289 BOOL MpqWriteHeaders(MPQARCHIVE *mpqOpenArc)
291         DWORD nIOLen;
292         BOOL bReturnVal = FALSE;
294         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart, FILE_BEGIN);
295         bReturnVal = WriteFile(mpqOpenArc->hFile, &mpqOpenArc->MpqHeader, sizeof(MPQHEADER), &nIOLen, 0);
297         if (bReturnVal && mpqOpenArc->MpqHeader.wVersion > 0) {
298                 bReturnVal = WriteFile(mpqOpenArc->hFile, &mpqOpenArc->MpqHeader_Ex, sizeof(MPQHEADER_EX), &nIOLen, 0);
299         }
301         return bReturnVal;
304 BOOL MpqWriteNewArchive(MPQARCHIVE *mpqOpenArc)
306         DWORD nIOLen;
308         memcpy(&mpqOpenArc->MpqHeader.dwMPQID,ID_MPQ,4);
309         mpqOpenArc->MpqHeader.wVersion = wCreationVersion;
311         if (mpqOpenArc->MpqHeader.wVersion == 0)
312                 mpqOpenArc->MpqHeader.dwHeaderSize = sizeof(MPQHEADER);
313         else
314                 mpqOpenArc->MpqHeader.dwHeaderSize = sizeof(MPQHEADER) + sizeof(MPQHEADER_EX);
315         mpqOpenArc->dwHeaderSize = mpqOpenArc->MpqHeader.dwHeaderSize;
317         if (mpqOpenArc->MpqHeader.wBlockSize & 0xFFFF0000)
318                 mpqOpenArc->MpqHeader.wBlockSize = DEFAULT_BLOCK_SIZE;
319         DWORD i;
320         for (i=1;i<mpqOpenArc->MpqHeader.dwHashTableSize;i*=2) {}
321         mpqOpenArc->MpqHeader.dwHashTableSize = i;
322         mpqOpenArc->MpqHeader.dwBlockTableSize = 0;
323         mpqOpenArc->MpqHeader.dwHashTableOffset = mpqOpenArc->dwHeaderSize;
324         mpqOpenArc->MpqHeader.dwBlockTableOffset = mpqOpenArc->dwHeaderSize + mpqOpenArc->MpqHeader.dwHashTableSize * sizeof(HASHTABLEENTRY);
325         mpqOpenArc->MpqHeader_Ex.qwExtendedBlockOffsetTable = 0;
326         mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh = 0;
327         mpqOpenArc->MpqHeader_Ex.wBlockTableOffsetHigh = 0;
328         mpqOpenArc->MpqHeader.dwMPQSize = mpqOpenArc->dwHeaderSize + mpqOpenArc->MpqHeader.dwHashTableSize * sizeof(HASHTABLEENTRY);
330         if (!MpqWriteHeaders(mpqOpenArc))
331                 return FALSE;
333         mpqOpenArc->lpHashTable = (HASHTABLEENTRY *)SFAlloc(sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize);
334         if (!mpqOpenArc->lpHashTable) {
335                 return FALSE;
336         }
337         memset(mpqOpenArc->lpHashTable, 0xFF, sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize);
338         EncryptData((LPBYTE)mpqOpenArc->lpHashTable, sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize, dwHashTableKey);
339         if (!WriteFile(mpqOpenArc->hFile, mpqOpenArc->lpHashTable, sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize, &nIOLen, 0)) {
340                 return FALSE;
341         }
343         return TRUE;
346 BOOL MpqReadExistingArchive(MPQARCHIVE *mpqOpenArc)
348         DWORD nIOLen;
350         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart, FILE_BEGIN);
351         if (!ReadFile(mpqOpenArc->hFile, &mpqOpenArc->MpqHeader, sizeof(MPQHEADER), &nIOLen, 0))
352                 return FALSE;
354         if (mpqOpenArc->MpqHeader.wVersion > 0) {
355                 mpqOpenArc->dwHeaderSize = sizeof(MPQHEADER) + sizeof(MPQHEADER_EX);
356                 if (!ReadFile(mpqOpenArc->hFile, &mpqOpenArc->MpqHeader_Ex, sizeof(MPQHEADER_EX), &nIOLen, 0))
357                         return FALSE;
358         }
359         else {
360                 mpqOpenArc->dwHeaderSize = sizeof(MPQHEADER);
361                 memset(&mpqOpenArc->MpqHeader_Ex, 0, sizeof(MPQHEADER_EX));
362         }
364         mpqOpenArc->lpHashTable = (HASHTABLEENTRY *)SFAlloc(sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize);
365         if (!mpqOpenArc->lpHashTable)
366                 return FALSE;
368         if (mpqOpenArc->MpqHeader.dwBlockTableSize) {
369                 mpqOpenArc->lpBlockTable = (BLOCKTABLEENTRY *)SFAlloc((sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)) * mpqOpenArc->MpqHeader.dwBlockTableSize);
370                 mpqOpenArc->lpFileOffsetsHigh = (UInt16 *)(mpqOpenArc->lpBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize);
371                 if (!mpqOpenArc->lpBlockTable)
372                         return FALSE;
373         }
375         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->MpqHeader.dwHashTableOffset, mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh), FILE_BEGIN);
376         if (!ReadFile(mpqOpenArc->hFile, mpqOpenArc->lpHashTable, sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize, &nIOLen, 0))
377                 return FALSE;
379         if (mpqOpenArc->MpqHeader.dwBlockTableSize) {
380                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->MpqHeader.dwBlockTableOffset, mpqOpenArc->MpqHeader_Ex.wBlockTableOffsetHigh), FILE_BEGIN);
381                 if (!ReadFile(mpqOpenArc->hFile, mpqOpenArc->lpBlockTable, sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize, &nIOLen, 0))
382                         return FALSE;
384                 if (mpqOpenArc->MpqHeader_Ex.qwExtendedBlockOffsetTable) {
385                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + mpqOpenArc->MpqHeader_Ex.qwExtendedBlockOffsetTable, FILE_BEGIN);
386                         if (!ReadFile(mpqOpenArc->hFile, mpqOpenArc->lpFileOffsetsHigh, sizeof(WORD) * mpqOpenArc->MpqHeader.dwBlockTableSize, &nIOLen, 0))
387                                 return FALSE;
388                 }
389         }
391         return TRUE;
394 BOOL MpqOpenArchiveInit(MPQARCHIVE *mpqOpenArc, LPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags1, DWORD dwFlags2, DWORD dwExtraFlags)
396         /*mpqOpenArc->lpFileName = (char *)SFAlloc(strlen(lpFileName)+1);
397         if(!mpqOpenArc->lpFileName)
398                 return FALSE;*/
399         DecryptData((LPBYTE)mpqOpenArc->lpHashTable,sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize,dwHashTableKey);
400         if (mpqOpenArc->lpBlockTable) DecryptData((LPBYTE)mpqOpenArc->lpBlockTable,sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize,dwBlockTableKey);
401         mpqOpenArc->lpFileName = mpqOpenArc->szFileName;
402         strncpy(mpqOpenArc->lpFileName,lpFileName,259);
403         if (FirstLastMpq[1]) FirstLastMpq[1]->lpNextArc = mpqOpenArc;
404         mpqOpenArc->lpNextArc = (MPQARCHIVE *)FirstLastMpq;
405         mpqOpenArc->lpPrevArc = (MPQARCHIVE *)FirstLastMpq[1];
406         if (!FirstLastMpq[0]) {
407                 mpqOpenArc->lpPrevArc = (MPQARCHIVE *)0xEAFC5E23;
408                 FirstLastMpq[0] = mpqOpenArc;
409         }
410         FirstLastMpq[1] = mpqOpenArc;
411         mpqOpenArc->dwFlags1 = dwFlags1;
412         mpqOpenArc->dwPriority = dwPriority;
413         mpqOpenArc->lpLastReadFile = 0;
414         mpqOpenArc->dwUnk = 0;
415         mpqOpenArc->dwBlockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
416         mpqOpenArc->lpLastReadBlock = 0;
417         mpqOpenArc->dwBufferSize = 0;
418         mpqOpenArc->qwMPQEnd = mpqOpenArc->qwMPQStart + mpqOpenArc->MpqHeader.dwMPQSize;
419         mpqOpenArc->lpMPQHeader = &mpqOpenArc->MpqHeader;
420         mpqOpenArc->qwReadOffset = mpqOpenArc->qwMPQEnd;
421         mpqOpenArc->dwRefCount = 1;
422         mpqOpenArc->dwFlags = dwFlags2;
423         mpqOpenArc->dwExtraFlags = dwExtraFlags;
425         return TRUE;
428 BOOL WINAPI MpqOpenArchiveEx(LPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags, MPQHANDLE *hMPQ, DWORD dwFlags2, DWORD dwMaximumFilesInArchive, DWORD dwBlockSize)
430         if (!lpFileName || !hMPQ) {
431                 SetLastError(ERROR_INVALID_PARAMETER);
432                 if (hMPQ) *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
433                 return FALSE;
434         }
435         *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
436         if (!*lpFileName) {
437                 SetLastError(ERROR_INVALID_PARAMETER);
438                 return FALSE;
439         }
441         DWORD dwFlags1 = 0;
443 #ifdef _WIN32
444         char RootPath[4];
445         memset(RootPath,0,4);
446         if (memcmp(lpFileName+1,":\\",2)==0) memcpy(RootPath,lpFileName,3);
447         else {
448                 char CurDir[MAX_PATH];
449                 GetCurrentDirectory(MAX_PATH,CurDir);
450                 memcpy(RootPath,CurDir,3);
451         }
452         if (GetDriveType(RootPath)==DRIVE_CDROM)
453                 dwFlags1 |= 2;
454         else if (dwFlags&2)
455                 dwFlags1 |= 1;
456         if (dwFlags & SFILE_OPEN_CD_ROM_FILE)
457                 if (!(dwFlags1&2)) {return FALSE;}
458 #endif
460         DWORD dwCreateFlags,dwAccessFlags,dwShareFlags;
461         if (dwFlags2 & MOAU_OPEN_ALWAYS) dwCreateFlags = OPEN_ALWAYS;
462         else if (dwFlags2 & MOAU_CREATE_ALWAYS) dwCreateFlags = CREATE_ALWAYS;
463         else if (dwFlags2 & MOAU_OPEN_EXISTING) dwCreateFlags = OPEN_EXISTING;
464         else dwCreateFlags = CREATE_NEW;
465         if (dwFlags2 & MOAU_READ_ONLY) {
466                 if (!(dwFlags2 & MOAU_OPEN_EXISTING)) {
467                         SetLastError(MPQ_ERROR_BAD_OPEN_MODE);
468                         return FALSE;
469                 }
470                 dwAccessFlags = GENERIC_READ;
471                 if (dwFlags2 & SFILE_OPEN_ALLOW_WRITE) dwFlags2 = dwFlags2^SFILE_OPEN_ALLOW_WRITE;
472         }
473         else {
474                 dwAccessFlags = GENERIC_READ|GENERIC_WRITE;
475                 dwFlags2 = dwFlags2|SFILE_OPEN_ALLOW_WRITE;
476         }
477         if (dwAccessFlags & GENERIC_WRITE)
478                 dwShareFlags = 0;
479         else
480                 dwShareFlags = FILE_SHARE_READ;
481         HANDLE hFile;
482         hFile = CreateFile(lpFileName,dwAccessFlags,dwShareFlags,0,dwCreateFlags,0,0);
483         TempAlloc NewAlloc;
484         if (hFile==INVALID_HANDLE_VALUE) {
485                 DWORD namelen = GetFullPath(lpFileName,0,0);
486                 char *namebuffer = (char *)NewAlloc.Alloc(namelen);
487                 GetFullPath(lpFileName,namebuffer,namelen);
488                 lpFileName = namebuffer;
489                 hFile = CreateFile(lpFileName,dwAccessFlags,dwShareFlags,0,dwCreateFlags,0,0);
490         }
491         if (hFile!=INVALID_HANDLE_VALUE)
492         {
493                 MPQARCHIVE **lpnOpenMpq = (MPQARCHIVE **)SFAlloc(sizeof(MPQARCHIVE *) * (dwOpenMpqCount+1));
494                 if (!lpnOpenMpq) {
495                         CloseHandle(hFile);
496                         return FALSE;
497                 }
498                 MPQARCHIVE *mpqOpenArc;
499                 if (SFGetFileSize(hFile)==0 && !(dwFlags2 & MOAU_READ_ONLY))
500                 {
501                         mpqOpenArc = (MPQARCHIVE *)SFAlloc(sizeof(MPQARCHIVE));
502                         if (!mpqOpenArc) {
503                                 SFFree(lpnOpenMpq);
504                                 CloseHandle(hFile);
505                                 return FALSE;
506                         }
508                         mpqOpenArc->hFile = hFile;
509                         mpqOpenArc->qwMPQStart = 0;
510                         mpqOpenArc->MpqHeader.dwHashTableSize = dwMaximumFilesInArchive;
511                         mpqOpenArc->MpqHeader.wBlockSize = (WORD)dwBlockSize;
513                         if (!MpqWriteNewArchive(mpqOpenArc)) {
514                                 if (mpqOpenArc->lpHashTable) SFFree(mpqOpenArc->lpHashTable);
515                                 SFFree(mpqOpenArc);
516                                 SFFree(lpnOpenMpq);
517                                 CloseHandle(hFile);
518                                 return FALSE;
519                         }
520                 }
521                 else
522                 {
523                         UInt64 qwMpqStart = SFileFindMpqHeaderEx(hFile);
524                         if (qwMpqStart == (UInt64)-1) {
525                                 SFFree(lpnOpenMpq);
526                                 CloseHandle(hFile);
527                                 SetLastError(MPQ_ERROR_MPQ_INVALID);
528                                 return FALSE;
529                         }
530                         mpqOpenArc = (MPQARCHIVE *)SFAlloc(sizeof(MPQARCHIVE));
531                         if (!mpqOpenArc) {
532                                 SFFree(lpnOpenMpq);
533                                 CloseHandle(hFile);
534                                 return FALSE;
535                         }
537                         mpqOpenArc->hFile = hFile;
538                         mpqOpenArc->qwMPQStart = qwMpqStart;
540                         if (!MpqReadExistingArchive(mpqOpenArc)) {
541                                 if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
542                                 if (mpqOpenArc->lpHashTable) SFFree(mpqOpenArc->lpHashTable);
543                                 SFFree(mpqOpenArc);
544                                 SFFree(lpnOpenMpq);
545                                 CloseHandle(hFile);
546                                 return FALSE;
547                         }
548                 }
550                 if (!MpqOpenArchiveInit(mpqOpenArc, lpFileName, dwPriority, dwFlags1, dwFlags2, 0)) {
551                         if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
552                         SFFree(mpqOpenArc->lpHashTable);
553                         SFFree(mpqOpenArc);
554                         SFFree(lpnOpenMpq);
555                         CloseHandle(hFile);
556                         return FALSE;
557                 }
559                 memcpy(lpnOpenMpq,lpOpenMpq,sizeof(MPQARCHIVE *) * dwOpenMpqCount);
560                 lpnOpenMpq[dwOpenMpqCount] = mpqOpenArc;
561                 if (lpOpenMpq) SFFree(lpOpenMpq);
562                 lpOpenMpq = lpnOpenMpq;
563                 dwOpenMpqCount++;
564                 if (dwOpenMpqCount>1) SortOpenArchivesByPriority();
565                 *hMPQ = (MPQHANDLE)mpqOpenArc;
567                 return TRUE;
568         }
569         else {
570                 if (dwFlags2&MOAU_OPEN_EXISTING) SetLastError(ERROR_FILE_NOT_FOUND);
571                 else if (dwFlags2&MOAU_CREATE_NEW) SetLastError(ERROR_ALREADY_EXISTS);
572                 *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
573                 return FALSE;
574         }
577 BOOL SFMPQAPI WINAPI SFileOpenFileAsArchive(MPQHANDLE hSourceMPQ, LPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags, MPQHANDLE *hMPQ)
579         if (!lpFileName || !hMPQ) {
580                 SetLastError(ERROR_INVALID_PARAMETER);
581                 if (hMPQ) *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
582                 return FALSE;
583         }
584         *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
585         if (!*lpFileName) {
586                 SetLastError(ERROR_INVALID_PARAMETER);
587                 return FALSE;
588         }
590         HANDLE hFile;
591         if (SFileOpenFileEx(hSourceMPQ,lpFileName,1,&hFile))
592         {
593                 MPQFILE mpqArcFile;
594                 mpqArcFile = *(MPQFILE *)hFile;
595                 SFileCloseFile(hFile);
596                 if (mpqArcFile.hFile != INVALID_HANDLE_VALUE)
597                         return SFileOpenArchive(lpFileName,dwPriority,dwFlags,hMPQ);
598                 hFile = mpqArcFile.lpParentArc->hFile;
599                 MPQARCHIVE **lpnOpenMpq = (MPQARCHIVE **)SFAlloc(sizeof(MPQARCHIVE *) * (dwOpenMpqCount+1));
600                 if (!lpnOpenMpq) {
601                         return FALSE;
602                 }
603                 MPQARCHIVE *mpqOpenArc;
604                 UInt64 qwMpqStart = mpqArcFile.lpParentArc->qwMPQStart + MakeUInt64(mpqArcFile.lpBlockEntry->dwFileOffset, mpqArcFile.wFileOffsetHigh);
605                 UInt64 flen = mpqArcFile.lpBlockEntry->dwFullSize;
606                 qwMpqStart = SFileFindMpqHeaderEx(hFile, qwMpqStart, flen);
607                 if (qwMpqStart == (UInt64)-1) {
608                         SFFree(lpnOpenMpq);
609                         SetLastError(MPQ_ERROR_MPQ_INVALID);
610                         return FALSE;
611                 }
612                 mpqOpenArc = (MPQARCHIVE *)SFAlloc(sizeof(MPQARCHIVE));
613                 if (!mpqOpenArc) {
614                         SFFree(lpnOpenMpq);
615                         return FALSE;
616                 }
618                 mpqOpenArc->hFile = hFile;
619                 mpqOpenArc->qwMPQStart = qwMpqStart;
621                 if (!MpqReadExistingArchive(mpqOpenArc)) {
622                         if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
623                         if (mpqOpenArc->lpHashTable) SFFree(mpqOpenArc->lpHashTable);
624                         SFFree(mpqOpenArc);
625                         SFFree(lpnOpenMpq);
626                         return FALSE;
627                 }
629                 if (!MpqOpenArchiveInit(mpqOpenArc, lpFileName, dwPriority, 0, dwFlags, 1)) {
630                         if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
631                         SFFree(mpqOpenArc->lpHashTable);
632                         SFFree(mpqOpenArc);
633                         SFFree(lpnOpenMpq);
634                         return FALSE;
635                 }
637                 memcpy(lpnOpenMpq,lpOpenMpq,sizeof(MPQARCHIVE *) * dwOpenMpqCount);
638                 lpnOpenMpq[dwOpenMpqCount] = mpqOpenArc;
639                 if (lpOpenMpq) SFFree(lpOpenMpq);
640                 lpOpenMpq = lpnOpenMpq;
641                 dwOpenMpqCount++;
642                 if (dwOpenMpqCount>1) SortOpenArchivesByPriority();
643                 *hMPQ = (MPQHANDLE)mpqOpenArc;
645                 return TRUE;
646         }
647         else {
648                 *hMPQ = (MPQHANDLE)INVALID_HANDLE_VALUE;
649                 return FALSE;
650         }
653 BOOL SFMPQAPI WINAPI SFileOpenArchive(LPCSTR lpFileName, DWORD dwPriority, DWORD dwFlags, MPQHANDLE *hMPQ)
655         if (dwFlags & SFILE_OPEN_ALLOW_WRITE) {
656                 return MpqOpenArchiveEx(lpFileName,dwPriority,dwFlags ^ SFILE_OPEN_ALLOW_WRITE,hMPQ,MOAU_OPEN_EXISTING,0,USE_DEFAULT_BLOCK_SIZE);
657         }
658         else {
659                 return MpqOpenArchiveEx(lpFileName,dwPriority,dwFlags,hMPQ,MOAU_OPEN_EXISTING|MOAU_READ_ONLY,0,USE_DEFAULT_BLOCK_SIZE);
660         }
663 BOOL SFMPQAPI WINAPI SFileCloseArchive(MPQHANDLE hMPQ)
665         if (hMPQ==0) {
666                 SetLastError(ERROR_INVALID_PARAMETER);
667                 return FALSE;
668         }
670         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
671         if (dwOpenMpqCount==0) return FALSE;
672         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_MPQ,4)!=0 && memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)!=0) return FALSE;
673         if (!(mpqOpenArc->dwExtraFlags & 1)) CloseHandle(mpqOpenArc->hFile);
674         //SFFree(mpqOpenArc->lpFileName);
675         SFFree(mpqOpenArc->lpHashTable);
676         if(mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
677         if ((DWORD)mpqOpenArc->lpNextArc!=(DWORD)FirstLastMpq)
678                 mpqOpenArc->lpNextArc->lpPrevArc = mpqOpenArc->lpPrevArc;
679         if ((DWORD)mpqOpenArc->lpPrevArc!=0xEAFC5E23)
680                 mpqOpenArc->lpPrevArc->lpNextArc = mpqOpenArc->lpNextArc;
681         if (mpqOpenArc==FirstLastMpq[0])
682                 FirstLastMpq[0] = mpqOpenArc->lpNextArc;
683         if ((DWORD)FirstLastMpq[0]==(DWORD)FirstLastMpq)
684                 FirstLastMpq[0] = 0;
685         if (mpqOpenArc==FirstLastMpq[1])
686                 FirstLastMpq[1] = mpqOpenArc->lpPrevArc;
687         if ((DWORD)FirstLastMpq[1]==0xEAFC5E23)
688                 FirstLastMpq[1] = 0;
689         SFFree(mpqOpenArc);
690         if (!lpOpenMpq) return TRUE;
691         if (dwOpenMpqCount-1==0) {
692                 SFFree(lpOpenMpq);
693                 lpOpenMpq = 0;
694                 dwOpenMpqCount--;
695                 return TRUE;
696         }
697         else if (dwOpenMpqCount==0) return FALSE;
698         MPQARCHIVE **lpnOpenMpq = (MPQARCHIVE **)SFAlloc(sizeof(MPQARCHIVE *) * (dwOpenMpqCount-1));
699         if (lpnOpenMpq) {
700                 for (DWORD i=0,j=0;i<dwOpenMpqCount;i++) {
701                         if ((MPQHANDLE)lpOpenMpq[i]!=hMPQ) {
702                                 memcpy(&lpnOpenMpq[j],&lpOpenMpq[i],sizeof(MPQARCHIVE *));
703                                 j++;
704                         }
705                 }
706                 SFFree(lpOpenMpq);
707                 lpOpenMpq = lpnOpenMpq;
708                 dwOpenMpqCount--;
709         }
710         else {
711                 for (DWORD i=0,j=0;i<dwOpenMpqCount;i++) {
712                         if ((MPQHANDLE)lpOpenMpq[i]==hMPQ) {
713                                 lpOpenMpq[i] = (MPQARCHIVE *)0;
714                         }
715                         else {
716                                 if (i!=j) memcpy(&lpOpenMpq[j],&lpOpenMpq[i],sizeof(MPQARCHIVE *));
717                                 j++;
718                         }
719                 }
720                 dwOpenMpqCount--;
721         }
722         return TRUE;
725 BOOL SFMPQAPI WINAPI SFileGetArchiveName(MPQHANDLE hMPQ, LPCSTR lpBuffer, DWORD dwBufferLength)
727         if (hMPQ==0 || lpBuffer==0) {
728                 SetLastError(ERROR_INVALID_PARAMETER);
729                 return FALSE;
730         }
732         DWORD dwhType = GetHandleType(hMPQ);
733         char *lpFileName;
734         if (dwhType==SFILE_TYPE_MPQ) lpFileName = ((MPQARCHIVE *)hMPQ)->lpFileName;
735         else if (dwhType==SFILE_TYPE_FILE) lpFileName = ((MPQFILE *)hMPQ)->lpFileName;
736         else return FALSE;
737         DWORD slen = strlen(lpFileName)+1;
738         if (dwBufferLength>=slen) memcpy((void *)lpBuffer,lpFileName,slen);
739         else memcpy((void *)lpBuffer,lpFileName,dwBufferLength);
740         return TRUE;
743 BOOL SFMPQAPI WINAPI SFileOpenFile(LPCSTR lpFileName, MPQHANDLE *hFile)
745         return SFileOpenFileEx(0,lpFileName,SFILE_SEARCH_CURRENT_ONLY,hFile);
748 BOOL WINAPI IsHexDigit(char nChar)
750         switch (nChar) {
751                 case '0':
752                 case '1':
753                 case '2':
754                 case '3':
755                 case '4':
756                 case '5':
757                 case '6':
758                 case '7':
759                 case '8':
760                 case '9':
761                 case 'A':
762                 case 'a':
763                 case 'B':
764                 case 'b':
765                 case 'C':
766                 case 'c':
767                 case 'D':
768                 case 'd':
769                 case 'E':
770                 case 'e':
771                 case 'F':
772                 case 'f':
773                         return TRUE;
774                 default:
775                         return FALSE;
776         }
777         return FALSE;
780 BOOL SFMPQAPI WINAPI SFileOpenFileEx(MPQHANDLE hMPQ, LPCSTR lpFileName, DWORD dwSearchScope, MPQHANDLE *hFile)
782         if (!lpFileName || !hFile) {
783                 SetLastError(ERROR_INVALID_PARAMETER);
784                 return FALSE;
785         }
786         if (!*lpFileName) {
787                 SetLastError(ERROR_INVALID_PARAMETER);
788                 return FALSE;
789         }
791         /*char szFileName[260];
792         if (hMPQ!=0) {
793                 if ((DWORD)lpFileName<((MPQARCHIVE *)hMPQ)->MpqHeader.dwHashTableSize) {
794                         sprintf(szFileName,UNKNOWN_OUT,(DWORD)lpFileName);
795                         lpFileName = szFileName;
796                         dwSearchScope = SFILE_SEARCH_CURRENT_ONLY;
797                 }
798         }
799         else {
800                 for (DWORD i=0;i<dwOpenMpqCount;i++) {
801                         if ((DWORD)lpFileName<lpOpenMpq[i]->MpqHeader.dwHashTableSize) {
802                                 SetLastError(ERROR_INVALID_PARAMETER);
803                                 return FALSE;
804                         }
805                 }
806         }*/
808         MPQFILE **lpnOpenFile = (MPQFILE **)SFAlloc(sizeof(MPQFILE *) * (dwOpenFileCount+1));
809         if (!lpnOpenFile) return FALSE;
811         MPQFILE *mpqOpenFile = (MPQFILE *)SFAlloc(sizeof(MPQFILE));
812         if (!mpqOpenFile) {
813                 SFFree(lpnOpenFile);
814                 return FALSE;
815         }
816         BOOL bFileOnDisk=FALSE;
817         MPQHANDLE hnMPQ,hnFile;
818         TempAlloc NewAlloc;
819         if (dwSearchScope==SFILE_SEARCH_CURRENT_ONLY) {
820                 if (hMPQ) {
821                         if (memicmp(lpFileName,UNKNOWN_CMP,UNKNOWN_LEN)==0 && IsHexDigit(lpFileName[UNKNOWN_LEN])) {
822                                 DWORD dwHashIndex;
823                                 sscanf(lpFileName,UNKNOWN_IN,&dwHashIndex);
824                                 hnFile = (HANDLE)&((MPQARCHIVE *)hMPQ)->lpHashTable[dwHashIndex];
825                                 if ((((HASHTABLEENTRY *)hnFile)->dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE)
826                                 {
827                                         SFFree(mpqOpenFile);
828                                         SFFree(lpnOpenFile);
829                                         SetLastError(ERROR_FILE_NOT_FOUND);
830                                         return FALSE;
831                                 }
832                         }
833                         else hnFile = GetHashTableEntry(hMPQ,lpFileName,LocaleID);
834                         hnMPQ = hMPQ;
835                 }
836                 else {
837                         SearchOpenArchives(lpFileName,&hnMPQ,&hnFile);
838                 }
839         }
840         else {
841                 SearchOpenArchives(lpFileName,&hnMPQ,&hnFile);
842                 if (!hnFile) {
843                         hnMPQ = 0;
844                         DWORD namelen = GetFullPath(lpFileName,0,0);
845                         char *namebuffer = (char *)NewAlloc.Alloc(namelen);
846                         GetFullPath(lpFileName,namebuffer,namelen);
847                         lpFileName = namebuffer;
848                         hnFile = CreateFile(lpFileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
849                         if (hnFile==INVALID_HANDLE_VALUE) hnFile=0;
850                         else bFileOnDisk = TRUE;
851                 }
852         }
853         if (hnMPQ) {
854                 if(!((MPQARCHIVE *)hnMPQ)->lpBlockTable) {
855                         SFFree(mpqOpenFile);
856                         SFFree(lpnOpenFile);
857                         SetLastError(ERROR_FILE_NOT_FOUND);
858                         return FALSE;
859                 }
860         }
861         if (!hnFile) {
862                 SFFree(mpqOpenFile);
863                 SFFree(lpnOpenFile);
864                 SetLastError(ERROR_FILE_NOT_FOUND);
865                 return FALSE;
866         }
867         mpqOpenFile->lpFileName = (char *)SFAlloc(strlen(lpFileName)+1);
868         if (!mpqOpenFile->lpFileName) {
869                 SFFree(mpqOpenFile);
870                 SFFree(lpnOpenFile);
871                 return FALSE;
872         }
873         if (hnMPQ) {
874                 if (!(((MPQARCHIVE *)hnMPQ)->lpBlockTable[((HASHTABLEENTRY *)hnFile)->dwBlockTableIndex].dwFlags & MAFA_EXISTS)) {
875                         SFFree(mpqOpenFile->lpFileName);
876                         SFFree(mpqOpenFile);
877                         SFFree(lpnOpenFile);
878                         SetLastError(ERROR_FILE_INVALID);
879                         return FALSE;
880                 }
881         }
882         strcpy(mpqOpenFile->lpFileName,lpFileName);
883         if (!bFileOnDisk) {
884                 mpqOpenFile->lpParentArc = (MPQARCHIVE *)hnMPQ;
885                 mpqOpenFile->hFile = INVALID_HANDLE_VALUE;
886                 mpqOpenFile->lpHashEntry = (HASHTABLEENTRY *)hnFile;
887                 mpqOpenFile->lpBlockEntry = &((MPQARCHIVE *)hnMPQ)->lpBlockTable[mpqOpenFile->lpHashEntry->dwBlockTableIndex];
888                 mpqOpenFile->wFileOffsetHigh = ((MPQARCHIVE *)hnMPQ)->lpFileOffsetsHigh[mpqOpenFile->lpHashEntry->dwBlockTableIndex];
889                 mpqOpenFile->lpParentArc->dwRefCount++;
890                 if (mpqOpenFile->lpBlockEntry->dwFlags&MAFA_ENCRYPT) {
891                         LPSTR lpOldNameBuffer = mpqOpenFile->lpFileName;
892                         if (memicmp(mpqOpenFile->lpFileName,UNKNOWN_CMP,UNKNOWN_LEN)==0 && IsHexDigit(mpqOpenFile->lpFileName[UNKNOWN_LEN]))
893                                 mpqOpenFile->lpFileName = 0;
894                         mpqOpenFile->dwCryptKey = DetectFileSeedEx(mpqOpenFile->lpParentArc,mpqOpenFile->lpHashEntry,(LPCSTR *)&mpqOpenFile->lpFileName);
895                         if (memicmp(lpFileName,UNKNOWN_CMP,UNKNOWN_LEN)==0 && IsHexDigit(lpFileName[UNKNOWN_LEN])) {
896                                 if (!mpqOpenFile->lpFileName)
897                                         mpqOpenFile->lpFileName = lpOldNameBuffer;
898                                 else
899                                         SFFree(lpOldNameBuffer);
900                         }
901                         if (mpqOpenFile->dwCryptKey==0) {
902                                 SFFree(mpqOpenFile->lpFileName);
903                                 SFFree(mpqOpenFile);
904                                 SFFree(lpnOpenFile);
905                                 SetLastError(ERROR_FILE_INVALID);
906                                 return FALSE;
907                         }
908                 }
909         }
910         else {
911                 mpqOpenFile->hFile = hnFile;
912         }
913         strncpy(mpqOpenFile->szFileName,mpqOpenFile->lpFileName,259);
914         *hFile = (MPQHANDLE)mpqOpenFile;
915         memcpy(lpnOpenFile,lpOpenFile,sizeof(MPQFILE *) * dwOpenFileCount);
916         lpnOpenFile[dwOpenFileCount] = mpqOpenFile;
917         if (lpOpenFile) SFFree(lpOpenFile);
918         lpOpenFile = lpnOpenFile;
919         dwOpenFileCount++;
920         return TRUE;
923 BOOL SFMPQAPI WINAPI SFileCloseFile(MPQHANDLE hFile)
925         if (!hFile) {
926                 SetLastError(ERROR_INVALID_PARAMETER);
927                 return FALSE;
928         }
930         MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
932         if (dwOpenFileCount==0) return FALSE;
933         SFFree(mpqOpenFile->lpFileName);
934         if (mpqOpenFile->lpdwBlockOffsets) SFFree(mpqOpenFile->lpdwBlockOffsets);
935         if (mpqOpenFile->hFile == INVALID_HANDLE_VALUE) mpqOpenFile->lpParentArc->dwRefCount--;
937         SFFree(mpqOpenFile);
938         if (lpOpenFile==0) return TRUE;
939         if (dwOpenFileCount-1==0) {
940                 SFFree(lpOpenFile);
941                 lpOpenFile = (MPQFILE **)0;
942                 dwOpenFileCount--;
943                 return TRUE;
944         }
945         else if (dwOpenFileCount==0) return FALSE;
946         MPQFILE **lpnOpenFile = (MPQFILE **)SFAlloc(sizeof(MPQFILE *) * (dwOpenFileCount-1));
947         if (lpnOpenFile) {
948                 for (DWORD i=0,j=0;i<dwOpenFileCount;i++) {
949                         if ((MPQHANDLE)lpOpenFile[i]!=hFile) {
950                                 memcpy(&lpnOpenFile[j],&lpOpenFile[i],sizeof(MPQFILE *));
951                                 j++;
952                         }
953                 }
954                 SFFree(lpOpenFile);
955                 lpOpenFile = lpnOpenFile;
956                 dwOpenFileCount--;
957         }
958         else {
959                 for (DWORD i=0,j=0;i<dwOpenFileCount;i++) {
960                         if ((MPQHANDLE)lpOpenFile[i]==hFile) {
961                                 lpOpenFile[i] = (MPQFILE *)0;
962                         }
963                         else {
964                                 if (i!=j) memcpy(&lpOpenFile[j],&lpOpenFile[i],sizeof(MPQFILE *));
965                                 j++;
966                         }
967                 }
968                 dwOpenFileCount--;
969         }
970         return TRUE;
973 DWORD SFMPQAPI WINAPI SFileGetFileSize(MPQHANDLE hFile, LPDWORD lpFileSizeHigh)
975         if (!hFile) {
976                 SetLastError(ERROR_INVALID_PARAMETER);
977                 return (DWORD)-1;
978         }
980         MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
981         if (mpqOpenFile->hFile == INVALID_HANDLE_VALUE) {
982                 if (lpFileSizeHigh) *lpFileSizeHigh = 0;
983                 return mpqOpenFile->lpBlockEntry->dwFullSize;
984         }
985         else return GetFileSize(mpqOpenFile->hFile,lpFileSizeHigh);
988 BOOL SFMPQAPI WINAPI SFileGetFileArchive(MPQHANDLE hFile, MPQHANDLE *hMPQ)
990         if (!hFile || !hMPQ) {
991                 SetLastError(ERROR_INVALID_PARAMETER);
992                 return FALSE;
993         }
995         if (((MPQFILE *)hFile)->hFile == INVALID_HANDLE_VALUE) {
996                 *hMPQ = (MPQHANDLE)((MPQFILE *)hFile)->lpParentArc;
997                 return TRUE;
998         }
999         else {
1000                 SetLastError(ERROR_INVALID_PARAMETER);
1001                 return FALSE;
1002         }
1005 BOOL SFMPQAPI WINAPI SFileGetFileName(MPQHANDLE hFile, LPCSTR lpBuffer, DWORD dwBufferLength)
1007         return SFileGetArchiveName(hFile,lpBuffer,dwBufferLength);
1010 DWORD SFMPQAPI WINAPI SFileGetFileInfo(MPQHANDLE hFile, DWORD dwInfoType)
1012         if (!hFile) {
1013                 SetLastError(ERROR_INVALID_PARAMETER);
1014                 return (DWORD)-1;
1015         }
1016         if (dwInfoType==0) {
1017                 SetLastError(ERROR_UNKNOWN_PROPERTY);
1018                 return (DWORD)-1;
1019         }
1021         DWORD dwhType = GetHandleType(hFile);
1022         if (dwhType==SFILE_TYPE_FILE)
1023         {
1024                 MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
1025                 if (mpqOpenFile->hFile == INVALID_HANDLE_VALUE) {
1026                         MPQARCHIVE *mpqOpenArc = mpqOpenFile->lpParentArc;
1027                         switch (dwInfoType) {
1028                         case SFILE_INFO_BLOCK_SIZE:
1029                                 return mpqOpenArc->MpqHeader.wBlockSize;
1030                         case SFILE_INFO_HASH_TABLE_SIZE:
1031                                 return mpqOpenArc->MpqHeader.dwHashTableSize;
1032                         case SFILE_INFO_NUM_FILES:
1033                                 return mpqOpenArc->MpqHeader.dwBlockTableSize;
1034                         case SFILE_INFO_TYPE:
1035                                 return SFILE_TYPE_FILE;
1036                         case SFILE_INFO_SIZE:
1037                                 return mpqOpenFile->lpBlockEntry->dwFullSize;
1038                         case SFILE_INFO_COMPRESSED_SIZE:
1039                                 return mpqOpenFile->lpBlockEntry->dwCompressedSize;
1040                         case SFILE_INFO_FLAGS:
1041                                 return mpqOpenFile->lpBlockEntry->dwFlags;
1042                         case SFILE_INFO_PARENT:
1043                                 return (DWORD)mpqOpenFile->lpParentArc;
1044                         case SFILE_INFO_POSITION:
1045                                 return mpqOpenFile->dwFilePointer;
1046                         case SFILE_INFO_LOCALEID:
1047                                 return mpqOpenFile->lpHashEntry->lcLocale;
1048                         case SFILE_INFO_PRIORITY:
1049                                 return mpqOpenArc->dwPriority;
1050                         case SFILE_INFO_HASH_INDEX:
1051                                 return ((UIntPtr)mpqOpenFile->lpHashEntry-(UIntPtr)mpqOpenArc->lpHashTable)/sizeof(HASHTABLEENTRY);
1052                         case SFILE_INFO_BLOCK_INDEX:
1053                                 return mpqOpenFile->lpHashEntry->dwBlockTableIndex;
1054                         default:
1055                                 SetLastError(ERROR_UNKNOWN_PROPERTY);
1056                                 return (DWORD)-1;
1057                         }
1058                 }
1059                 else {
1060                         switch (dwInfoType) {
1061                         case SFILE_INFO_TYPE:
1062                                 return SFILE_TYPE_FILE;
1063                         case SFILE_INFO_SIZE:
1064                                 return SFGetFileSize(mpqOpenFile->hFile);
1065                         case SFILE_INFO_COMPRESSED_SIZE:
1066                                 return SFGetFileSize(mpqOpenFile->hFile);
1067 #ifdef _WIN32
1068                         case SFILE_INFO_FLAGS:
1069                                 return GetFileAttributes(mpqOpenFile->lpFileName);
1070 #endif
1071                         case SFILE_INFO_POSITION:
1072                                 return SFSetFilePointer(mpqOpenFile->hFile,0,FILE_CURRENT);
1073                         default:
1074                                 SetLastError(ERROR_UNKNOWN_PROPERTY);
1075                                 return (DWORD)-1;
1077                         }
1078                 }
1079         }
1080         else if (dwhType==SFILE_TYPE_MPQ)
1081         {
1082                 MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hFile;
1083                 switch (dwInfoType) {
1084                 case SFILE_INFO_BLOCK_SIZE:
1085                         return mpqOpenArc->MpqHeader.wBlockSize;
1086                 case SFILE_INFO_HASH_TABLE_SIZE:
1087                         return mpqOpenArc->MpqHeader.dwHashTableSize;
1088                 case SFILE_INFO_NUM_FILES:
1089                         return mpqOpenArc->MpqHeader.dwBlockTableSize;
1090                 case SFILE_INFO_TYPE:
1091                         return SFILE_TYPE_MPQ;
1092                 case SFILE_INFO_SIZE:
1093                         return mpqOpenArc->MpqHeader.dwMPQSize;
1094                 case SFILE_INFO_PRIORITY:
1095                         return mpqOpenArc->dwPriority;
1096                 default:
1097                         SetLastError(ERROR_UNKNOWN_PROPERTY);
1098                         return (DWORD)-1;
1099                 }
1100         }
1101         SetLastError(ERROR_INVALID_PARAMETER);
1102         return (DWORD)-1;
1105 DWORD SFMPQAPI WINAPI SFileSetFilePointer(MPQHANDLE hFile, long lDistanceToMove, PLONG lplDistanceToMoveHigh, DWORD dwMoveMethod)
1107         if (!hFile) {
1108                 SetLastError(ERROR_INVALID_PARAMETER);
1109                 return (DWORD)-1;
1110         }
1112         MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
1113         if (mpqOpenFile->hFile == INVALID_HANDLE_VALUE) {
1114                 long fsz = mpqOpenFile->lpBlockEntry->dwFullSize;
1115                 long cpos = mpqOpenFile->dwFilePointer;
1116                 switch (dwMoveMethod) {
1117                 case FILE_BEGIN:
1118                         if (lDistanceToMove<0 || lDistanceToMove>fsz) return (DWORD)-1;
1119                         mpqOpenFile->dwFilePointer = lDistanceToMove;
1120                         break;
1121                 case FILE_CURRENT:
1122                         if (lDistanceToMove<cpos || cpos+lDistanceToMove>fsz) return (DWORD)-1;
1123                         mpqOpenFile->dwFilePointer += lDistanceToMove;
1124                         break;
1126                 case FILE_END:
1127                         if (lDistanceToMove<fsz || lDistanceToMove>0) return (DWORD)-1;
1128                         mpqOpenFile->dwFilePointer = fsz+lDistanceToMove;
1129                         break;
1130                 default:
1131                         if (lDistanceToMove<0 || lDistanceToMove>fsz) return (DWORD)-1;
1132                         mpqOpenFile->dwFilePointer = lDistanceToMove;
1133                 }
1134                 if (lplDistanceToMoveHigh!=0) *lplDistanceToMoveHigh = 0;
1135                 return mpqOpenFile->dwFilePointer;
1136         }
1137         else return SetFilePointer(mpqOpenFile->hFile,lDistanceToMove,lplDistanceToMoveHigh,dwMoveMethod);
1141 BOOL SFMPQAPI WINAPI SFileReadFile(MPQHANDLE hFile,LPVOID lpBuffer,DWORD nNumberOfBytesToRead,LPDWORD lpNumberOfBytesRead,LPOVERLAPPED lpOverlapped)
1143         if (!hFile || !lpBuffer) {
1144                 SetLastError(ERROR_INVALID_PARAMETER);
1145                 return FALSE;
1146         }
1148         MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
1149         if (mpqOpenFile->hFile != INVALID_HANDLE_VALUE) {
1150                 DWORD tsz=0;
1151                 BOOL RetVal = ReadFile(mpqOpenFile->hFile,lpBuffer,nNumberOfBytesToRead,&tsz,lpOverlapped);
1152                 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = tsz;
1153                 return RetVal;
1154         }
1155         if (lpOverlapped)
1156                 if (lpOverlapped->Internal || lpOverlapped->InternalHigh || lpOverlapped->Offset || lpOverlapped->OffsetHigh || lpOverlapped->hEvent)
1157                         SFileSetFilePointer(hFile,lpOverlapped->Offset,(long *)&lpOverlapped->OffsetHigh,FILE_BEGIN);
1158         if (nNumberOfBytesToRead==0) {
1159                 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1160                 if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1161                 return TRUE;
1162         }
1163         MPQARCHIVE *mpqOpenArc = mpqOpenFile->lpParentArc;
1165         if (mpqOpenFile->dwFilePointer>mpqOpenFile->lpBlockEntry->dwFullSize) return FALSE;
1166         if (mpqOpenFile->dwFilePointer==mpqOpenFile->lpBlockEntry->dwFullSize)
1167         {
1168                 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1169                 if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1170                 return TRUE;
1171         }
1172         DWORD nBytesRead,TotalBytesRead=0;
1173         DWORD BlockIndex = mpqOpenFile->lpHashEntry->dwBlockTableIndex;
1174         if (mpqOpenFile->dwFilePointer+nNumberOfBytesToRead>mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize) nNumberOfBytesToRead = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize-mpqOpenFile->dwFilePointer;
1175         DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
1176         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_SINGLEBLOCK)
1177                 blockSize = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
1178         DWORD dwBlockStart = mpqOpenFile->dwFilePointer - (mpqOpenFile->dwFilePointer % blockSize);
1179     DWORD blockNum = dwBlockStart / blockSize;
1180     DWORD nBlocks  = (mpqOpenFile->dwFilePointer+nNumberOfBytesToRead) / blockSize;
1181     if((mpqOpenFile->dwFilePointer+nNumberOfBytesToRead) % blockSize)
1182                 nBlocks++;
1183         DWORD TotalBlocks = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize / blockSize;
1184         if(mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize)
1185                 TotalBlocks++;
1186         DWORD dwBlockPos = mpqOpenFile->dwFilePointer % blockSize;
1187         DWORD dwBlockPosEnd = (mpqOpenFile->dwFilePointer+nNumberOfBytesToRead) % blockSize;
1188         if (dwBlockPosEnd==0) dwBlockPosEnd = blockSize;
1189         void *blkBuffer = SFAlloc(blockSize);
1190         if (!blkBuffer) {
1191                 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1192                 if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1193                 return FALSE;
1194         }
1195         DWORD nBufferCount;
1196         for (nBufferCount = 1; nBufferCount < nBlocks - blockNum && blockSize * nBufferCount < 65536; nBufferCount *= 2) {}
1197         if (nBufferCount > nBlocks - blockNum) nBufferCount = nBlocks - blockNum;
1198         void *blkLargeBuffer = NULL;
1199         if (nBufferCount > 1) {
1200                 blkLargeBuffer = SFAlloc(blockSize * nBufferCount);
1201                 if (!blkLargeBuffer) {
1202                         SFFree(blkBuffer);
1203                         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1204                         if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1205                         return FALSE;
1206                 }
1207         }
1208         DWORD dwCryptKey = mpqOpenFile->dwCryptKey;
1209         //if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_ENCRYPT) dwCryptKey = HashString(mpqOpenFile->lpFileName,HASH_KEY);
1210         //if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_MODCRYPTKEY) dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
1211         DWORD HeaderLength=0;
1212         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
1213         {
1214                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]), FILE_BEGIN);
1215                 ReadFile(mpqOpenArc->hFile,&HeaderLength,4,&nBytesRead,0);
1216         }
1217         DWORD i;
1218         if (!mpqOpenFile->lpdwBlockOffsets)
1219         {
1220                 DWORD *dwBlockPtrTable = (DWORD *)SFAlloc((TotalBlocks+1)*4);
1221                 if (!dwBlockPtrTable) {
1223                         if (blkLargeBuffer) SFFree(blkLargeBuffer);
1224                         SFFree(blkBuffer);
1225                         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1226                         if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1227                         return FALSE;
1228                 }
1229                 if (((mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS) || (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS2)) && !(mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_SINGLEBLOCK))
1230                 {
1231                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength, FILE_BEGIN);
1232                         ReadFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&nBytesRead,0);
1233                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_ENCRYPT) {
1234                                 DecryptData((LPBYTE)dwBlockPtrTable,(TotalBlocks+1)*4,dwCryptKey-1);
1235                         }
1236                 }
1237                 else
1238                 {
1239                         for (i=0;i<TotalBlocks+1;i++) {
1240                                 if (i<TotalBlocks) dwBlockPtrTable[i] = i * blockSize;
1241                                 else dwBlockPtrTable[i] = mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize - HeaderLength;
1242                         }
1243                 }
1244                 mpqOpenFile->lpdwBlockOffsets = dwBlockPtrTable;
1245         }
1246         BYTE *compbuffer = NULL;
1247         if ((mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS) || (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS2)) {
1248                 compbuffer = (BYTE *)SFAlloc(blockSize+3);
1249                 if (!compbuffer) {
1250                         if (blkLargeBuffer) SFFree(blkLargeBuffer);
1251                         SFFree(blkBuffer);
1252                         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = 0;
1253                         if (lpOverlapped) lpOverlapped->InternalHigh = 0;
1254                         return FALSE;
1255                 }
1256         }
1257         DWORD blk=0,blki=0;
1258         for (i=blockNum;i<nBlocks;i++) {
1259                 if (blk==0 && blkLargeBuffer) {
1260                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength + mpqOpenFile->lpdwBlockOffsets[i], FILE_BEGIN);
1261                         blki=i;
1262                         if (i+nBufferCount>nBlocks) {
1263                                 if (ReadFile(mpqOpenArc->hFile,blkLargeBuffer,mpqOpenFile->lpdwBlockOffsets[nBlocks]-mpqOpenFile->lpdwBlockOffsets[i],&nBytesRead,0)==0) {
1264                                         if (compbuffer) SFFree(compbuffer);
1265                                         SFFree(blkLargeBuffer);
1266                                         SFFree(blkBuffer);
1267                                         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = TotalBytesRead;
1268                                         if (lpOverlapped) lpOverlapped->InternalHigh = TotalBytesRead;
1269                                         return FALSE;
1270                                 }
1272                         }
1273                         else {
1274                                 if (ReadFile(mpqOpenArc->hFile,blkLargeBuffer,mpqOpenFile->lpdwBlockOffsets[i+nBufferCount]-mpqOpenFile->lpdwBlockOffsets[i],&nBytesRead,0)==0) {
1275                                         if (compbuffer) SFFree(compbuffer);
1276                                         SFFree(blkLargeBuffer);
1277                                         SFFree(blkBuffer);
1278                                         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = TotalBytesRead;
1279                                         if (lpOverlapped) lpOverlapped->InternalHigh = TotalBytesRead;
1280                                         return FALSE;
1281                                 }
1282                         }
1283                 }
1284                 if (blkLargeBuffer) {
1285                         memcpy(blkBuffer,((char *)blkLargeBuffer)+(mpqOpenFile->lpdwBlockOffsets[blki+blk]-mpqOpenFile->lpdwBlockOffsets[blki]),mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]);
1286                 }
1287                 else {
1288                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength + mpqOpenFile->lpdwBlockOffsets[i], FILE_BEGIN);
1289                         blki=i;
1290                         if (ReadFile(mpqOpenArc->hFile,blkBuffer,mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i],&nBytesRead,0)==0) {
1291                                 if (compbuffer) SFFree(compbuffer);
1292                                 SFFree(blkBuffer);
1293                                 if (lpNumberOfBytesRead) *lpNumberOfBytesRead = TotalBytesRead;
1294                                 if (lpOverlapped) lpOverlapped->InternalHigh = TotalBytesRead;
1295                                 return FALSE;
1296                         }
1297                 }
1298                 if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_ENCRYPT)
1299                 {
1300                         DecryptData((LPBYTE)blkBuffer,mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i],dwCryptKey+i);
1301                 }
1302                 if (i==TotalBlocks-1 && (mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize)!=0) blockSize=mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize;
1303                 DWORD outLength=blockSize;
1304                 if ((mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i])!=blockSize)
1305                 {
1306                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS)
1307                         {
1308                                 BYTE *compdata = compbuffer;
1309                                 memcpy(compdata,(char *)blkBuffer,mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]);
1310                                 if (compdata[0]&UNSUPPORTED_DECOMPRESSION) {
1311                                         LoadStorm();
1312                                         if (stormSCompDecompress)
1313                                                 stormSCompDecompress(blkBuffer, &outLength, compdata, mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]);
1314                                 }
1315                                 else {
1316                                         SCompDecompress(blkBuffer, &outLength, compdata, mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]);
1317                                 }
1318                         }
1319                         else if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS2)
1320                         {
1321                                 BYTE *compdata = compbuffer;
1322                                 compdata[0] = 0x08;
1323                                 memcpy(compdata+1,(char *)blkBuffer,mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]);
1324                                 SCompDecompress(blkBuffer, &outLength, compdata, mpqOpenFile->lpdwBlockOffsets[i+1]-mpqOpenFile->lpdwBlockOffsets[i]+1);
1325                         }
1326                 }
1327                 if (i==blockNum) {
1328                         if (nNumberOfBytesToRead > blockSize-dwBlockPos) {
1329                                 memcpy(lpBuffer,(char *)blkBuffer+dwBlockPos,blockSize-dwBlockPos);
1330                                 TotalBytesRead+=blockSize-dwBlockPos;
1331                                 mpqOpenFile->dwFilePointer += blockSize-dwBlockPos;
1332                                 nNumberOfBytesToRead-=blockSize-dwBlockPos;
1333                         }
1334                         else {
1335                                 memcpy(lpBuffer,(char *)blkBuffer+dwBlockPos,nNumberOfBytesToRead);
1336                                 TotalBytesRead+=nNumberOfBytesToRead;
1337                                 mpqOpenFile->dwFilePointer += nNumberOfBytesToRead;
1338                                 nNumberOfBytesToRead-=nNumberOfBytesToRead;
1339                         }
1340                 }
1341                 else if (i==nBlocks-1) {
1342                         if (nNumberOfBytesToRead > dwBlockPosEnd) {
1343                                 memcpy((char *)lpBuffer+TotalBytesRead,blkBuffer,dwBlockPosEnd);
1344                                 TotalBytesRead+=dwBlockPosEnd;
1345                                 mpqOpenFile->dwFilePointer += dwBlockPosEnd;
1346                                 nNumberOfBytesToRead-=dwBlockPosEnd;
1347                         }
1348                         else {
1349                                 memcpy((char *)lpBuffer+TotalBytesRead,blkBuffer,nNumberOfBytesToRead);
1350                                 TotalBytesRead+=nNumberOfBytesToRead;
1351                                 mpqOpenFile->dwFilePointer += nNumberOfBytesToRead;
1352                                 nNumberOfBytesToRead-=nNumberOfBytesToRead;
1353                         }
1354                 }
1355                 else {
1356                         if (nNumberOfBytesToRead > blockSize) {
1357                                 memcpy((char *)lpBuffer+TotalBytesRead,blkBuffer,blockSize);
1358                                 TotalBytesRead+=blockSize;
1359                                 mpqOpenFile->dwFilePointer += blockSize;
1360                                 nNumberOfBytesToRead-=blockSize;
1361                         }
1362                         else {
1363                                 memcpy((char *)lpBuffer+TotalBytesRead,blkBuffer,nNumberOfBytesToRead);
1364                                 TotalBytesRead+=nNumberOfBytesToRead;
1365                                 mpqOpenFile->dwFilePointer += nNumberOfBytesToRead;
1366                                 nNumberOfBytesToRead-=nNumberOfBytesToRead;
1367                         }
1368                 }
1369                 blk = (blk+1) % nBufferCount;
1370         }
1371         if (compbuffer) SFFree(compbuffer);
1372         if (blkLargeBuffer) SFFree(blkLargeBuffer);
1373         SFFree(blkBuffer);
1374         if (lpNumberOfBytesRead) *lpNumberOfBytesRead = TotalBytesRead;
1375         if (lpOverlapped) lpOverlapped->InternalHigh = TotalBytesRead;
1376         return TRUE;
1379 LCID SFMPQAPI WINAPI SFileSetLocale(LCID nNewLocale)
1381         return (LocaleID = nNewLocale);
1384 BOOL SFMPQAPI WINAPI SFileGetBasePath(LPCSTR lpBuffer, DWORD dwBufferLength)
1386         if (lpBuffer==0) {
1387                 SetLastError(ERROR_INVALID_PARAMETER);
1388                 return FALSE;
1389         }
1391         DWORD slen = strlen(StormBasePath)+1;
1392         if (dwBufferLength>=slen) memcpy((void *)lpBuffer,StormBasePath,slen);
1393         else memcpy((void *)lpBuffer,StormBasePath,dwBufferLength);
1394         return TRUE;
1397 BOOL SFMPQAPI WINAPI SFileSetBasePath(LPCSTR lpNewBasePath)
1399         if (!lpNewBasePath) {
1400                 SetLastError(ERROR_INVALID_PARAMETER);
1401                 return FALSE;
1402         }
1403         if (!*lpNewBasePath) {
1404                 SetLastError(ERROR_INVALID_PARAMETER);
1405                 return FALSE;
1406         }
1408         DWORD slen = strlen(lpNewBasePath)+1;
1409         if (slen>MAX_PATH+1) return FALSE;
1410         else if (slen==MAX_PATH+1)
1411                 if (lpNewBasePath[MAX_PATH-1]!='\\' && lpNewBasePath[MAX_PATH-1]!='/') return FALSE;
1412         TempAlloc NewAlloc;
1413         char *buffer = (char *)NewAlloc.Alloc(slen+1);
1414         memcpy(buffer,lpNewBasePath,slen);
1415         char *npath;
1416 #ifdef _WIN32
1417         for (npath=buffer;npath[0]!=0;npath++)
1418                 if (npath[0]=='/') npath[0]='\\';
1419         if (npath[-1]!='\\') {
1420                 npath[0]='\\';
1421                 npath[1]=0;
1422                 slen+=1;
1423         }
1424 #else
1425         for (npath=buffer;npath[0]!=0;npath++)
1426                 if (npath[0]=='\\') npath[0]='/';
1427         if (npath[-1]!='/') {
1428                 npath[0]='/';
1429                 npath[1]=0;
1430                 slen+=1;
1431         }
1432 #endif
1433 #ifdef _WIN32
1434         DWORD attrib = GetFileAttributes(buffer);
1435         if ((attrib&FILE_ATTRIBUTE_DIRECTORY)==0 || attrib==0xFFFFFFFF) return FALSE;
1436 #endif
1438         memcpy(StormBasePath,buffer,slen);
1439         return TRUE;
1442 BOOL SFMPQAPI WINAPI SFileSetArchivePriority(MPQHANDLE hMPQ, DWORD dwPriority)
1444         if (!hMPQ) {
1445                 SetLastError(ERROR_INVALID_PARAMETER);
1446                 return FALSE;
1447         }
1449         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
1450         mpqOpenArc->dwPriority = dwPriority;
1451         if (dwOpenMpqCount>1) SortOpenArchivesByPriority();
1452         return TRUE;
1455 int StringICompare(const void *arg1,const void *arg2)
1457         return stricmp(*(char **)arg1,*(char **)arg2);
1460 BOOL SFMPQAPI WINAPI SFileListFiles(MPQHANDLE hMPQ, LPCSTR lpFileLists, FILELISTENTRY *lpListBuffer, DWORD dwFlags)
1462         if (!hMPQ || !lpListBuffer) {
1463                 SetLastError(ERROR_INVALID_PARAMETER);
1464                 return FALSE;
1465         }
1467         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
1468         DWORD i,tsz;
1470         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
1471         {
1472                 for (i=0;i<mpqOpenArc->MpqHeader.dwHashTableSize;i++) {
1473                         lpListBuffer[i].dwFileExists = 0;
1474                         lpListBuffer[i].szFileName[0] = 0;
1475                         if ((mpqOpenArc->lpHashTable[i].dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE && mpqOpenArc->lpHashTable[i].dwBlockTableIndex < mpqOpenArc->MpqHeader.dwBlockTableSize) {
1476                                 lpListBuffer[i].lcLocale = mpqOpenArc->lpHashTable[i].lcLocale;
1477                                 DWORD dwBlockIndex = mpqOpenArc->lpHashTable[i].dwBlockTableIndex;
1478                                 lpListBuffer[i].dwCompressedSize = mpqOpenArc->lpBlockTable[dwBlockIndex].dwCompressedSize;
1479                                 lpListBuffer[i].dwFullSize = mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize;
1480                                 lpListBuffer[i].dwFlags = mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags;
1481                                 if (dwFlags & SFILE_LIST_FLAG_UNKNOWN)
1482                                         lpListBuffer[i].dwFileExists = 1;
1483                                 else
1484                                         lpListBuffer[i].dwFileExists=0xFFFFFFFF;
1485                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[dwBlockIndex]) + 0x40, FILE_BEGIN);
1486                                 ReadFile(mpqOpenArc->hFile,lpListBuffer[i].szFileName,260,&tsz,0);
1488                                 if (mpqOpenArc->lpHashTable[i].dwNameHashA==HashString(lpListBuffer[i].szFileName,HASH_NAME_A) && mpqOpenArc->lpHashTable[i].dwNameHashB==HashString(lpListBuffer[i].szFileName,HASH_NAME_B)) {
1489                                         if (dwFlags&SFILE_LIST_ONLY_UNKNOWN && !(dwFlags&SFILE_LIST_ONLY_KNOWN)) {
1490                                                 lpListBuffer[i].dwFileExists = 0;
1491                                         }
1492                                 }
1493                                 else {
1494                                         sprintf(lpListBuffer[i].szFileName,UNKNOWN_OUT,i);
1495                                         if (dwFlags & SFILE_LIST_FLAG_UNKNOWN) {
1496                                                 lpListBuffer[i].dwFileExists |= 2;
1497                                         }
1499                                         if (dwFlags&SFILE_LIST_ONLY_KNOWN) {
1500                                                 lpListBuffer[i].dwFileExists = 0;
1501                                         }
1502                                 }
1503                         }
1504                 }
1506                 return TRUE;
1507         }
1509         char **lpNameBuffers=0;
1511         char **lpNames=0,szNull[1]={0},*lpFLCopy;
1512         DWORD *lpdwNameHashA=0,*lpdwNameHashB=0;
1513         DWORD dwListNameHashA,dwListNameHashB;
1514         DWORD j;
1515         lpFLCopy = (char *)lpFileLists;
1516         DWORD dwExtLists=0,dwIntLists=0;
1517         if (!lpFLCopy) lpFLCopy=szNull;
1518         char *lpLines;
1519         for (lpLines=lpFLCopy;lpLines;lpLines=nextline(lpLines)) {
1520                 if (!*lpLines) break;
1521                 dwExtLists++;
1522         }
1523         dwListNameHashA = HashString(INTERNAL_LISTFILE,HASH_NAME_A);
1524         dwListNameHashB = HashString(INTERNAL_LISTFILE,HASH_NAME_B);
1525         for (i=0;i<mpqOpenArc->MpqHeader.dwHashTableSize;i++) {
1526                 if (mpqOpenArc->lpHashTable[i].dwNameHashA==dwListNameHashA && mpqOpenArc->lpHashTable[i].dwNameHashB==dwListNameHashB && (mpqOpenArc->lpHashTable[i].dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE) {
1527                         dwIntLists++;
1528                 }
1529         }
1530         lpNameBuffers = (char **)SFAlloc((1+dwExtLists+dwIntLists)*sizeof(char *));
1531         if (dwExtLists) {
1532                 lpFLCopy = strdup(lpFLCopy);
1533                 i=0;
1534                 for (lpLines=lpFLCopy;lpLines;lpLines=nextline(lpLines)) {
1535                         if (!*lpLines) break;
1536                         lpNameBuffers[i+1] = lpLines;
1537                         i++;
1538                 }
1539                 for (i=0;i<dwExtLists;i++) {
1540                         lpNameBuffers[i+1][strlnlen(lpNameBuffers[i+1])]=0;
1541                 }
1542                 qsort(lpNameBuffers+1,dwExtLists,sizeof(char *),StringICompare);
1543                 for (i=0;i<dwExtLists-1;i++) {
1544                         if (stricmp(lpNameBuffers[i+1],lpNameBuffers[i+2])==0) {
1545                                 memmove(&lpNameBuffers[i+1],&lpNameBuffers[i+2],(dwExtLists-(i+1))*sizeof(char *));
1546                                 dwExtLists--;
1547                         }
1548                 }
1549         }
1550         if (dwIntLists) {
1551                 dwIntLists = 0;
1552                 for (i=0;i<mpqOpenArc->MpqHeader.dwHashTableSize;i++) {
1553                         if (mpqOpenArc->lpHashTable[i].dwNameHashA==dwListNameHashA && mpqOpenArc->lpHashTable[i].dwNameHashB==dwListNameHashB && (mpqOpenArc->lpHashTable[i].dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE) {
1554                                 lpNameBuffers[1+dwExtLists+dwIntLists] = (char *)i;
1555                                 dwIntLists++;
1556                         }
1557                 }
1558         }
1559         DWORD dwLines=0,dwOldLines=0,dwTotalLines=0;
1560         DWORD fsz;
1561         HANDLE hFile;
1562         DWORD dwListCryptKey = HashString(INTERNAL_LISTFILE,HASH_KEY);
1563         for (i=0;i<1+dwExtLists+dwIntLists;i++) {
1564                 if (i==0) {
1565                         fsz = strlen(INTERNAL_FILES);
1566                         lpNameBuffers[i] = (char *)SFAlloc(fsz+1);
1567                         memcpy(lpNameBuffers[i],INTERNAL_FILES,fsz);
1568                         lpNameBuffers[i][fsz]=0;
1569                 }
1570                 else if (i<1+dwExtLists) {
1571                         if (!(dwFlags&SFILE_LIST_MEMORY_LIST)) {
1572                                 hFile = CreateFile(lpNameBuffers[i],GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
1573                                 if (hFile!=INVALID_HANDLE_VALUE) {
1574                                         fsz = SFGetFileSize(hFile);
1575                                         SFSetFilePointer(hFile,0,FILE_BEGIN);
1576                                         lpNameBuffers[i] = (char *)SFAlloc(fsz+1);
1577                                         ReadFile(hFile,lpNameBuffers[i],fsz,&tsz,0);
1578                                         CloseHandle(hFile);
1579                                         lpNameBuffers[i][fsz]=0;
1580                                 }
1581                                 else lpNameBuffers[i]=0;
1582                         }
1583                         else {
1584                                 dwTotalLines++;
1585                                 continue;
1586                         }
1587                 }
1588                 else {
1589                         MPQFILE thisFile;
1590                         memset(&thisFile,0,sizeof(MPQFILE));
1591                         thisFile.lpParentArc = (MPQARCHIVE *)hMPQ;
1592                         thisFile.hFile = INVALID_HANDLE_VALUE;
1593                         thisFile.lpHashEntry = &mpqOpenArc->lpHashTable[(DWORD)lpNameBuffers[i]];
1594                         thisFile.lpBlockEntry = &mpqOpenArc->lpBlockTable[thisFile.lpHashEntry->dwBlockTableIndex];
1595                         thisFile.lpFileName = thisFile.szFileName;
1596                         strcpy(thisFile.lpFileName,INTERNAL_LISTFILE);
1597                         thisFile.dwFilePointer = 0;
1598                         thisFile.lpdwBlockOffsets = 0;
1599                         BLOCKTABLEENTRY *lpBlockEntry = thisFile.lpBlockEntry;
1600                         if (lpBlockEntry->dwFlags & MAFA_ENCRYPT) {
1601                                 thisFile.dwCryptKey = dwListCryptKey;
1602                                 if (lpBlockEntry->dwFlags & MAFA_MODCRYPTKEY)
1603                                         thisFile.dwCryptKey = (thisFile.dwCryptKey + lpBlockEntry->dwFileOffset) ^ lpBlockEntry->dwFullSize;
1604                         }
1605                         fsz = lpBlockEntry->dwFullSize;
1606                         lpNameBuffers[i] = (char *)SFAlloc(fsz+1);
1607                         SFileReadFile(&thisFile,lpNameBuffers[i],fsz,0,0);
1608                         if (thisFile.lpdwBlockOffsets) SFFree(thisFile.lpdwBlockOffsets);
1609                         lpNameBuffers[i][fsz]=0;
1610                 }
1611                 for (lpLines=lpNameBuffers[i];lpLines;lpLines=nextline(lpLines)) {
1612                         if (!*lpLines) break;
1613                         dwTotalLines++;
1614                 }
1615         }
1616         lpNames = (char **)SFAlloc(dwTotalLines*sizeof(char *));
1617         for (i=0;i<1+dwExtLists+dwIntLists;i++) {
1618                 dwOldLines=dwLines;
1619                 for (lpLines=lpNameBuffers[i];lpLines;lpLines=nextline(lpLines)) {
1620                         if (!*lpLines) break;
1621                         lpNames[dwLines] = lpLines;
1622                         dwLines++;
1623                 }
1624                 for (j=dwOldLines;j<dwLines;j++) {
1625                         lpNames[j][strlnlen(lpNames[j])]=0;
1626                 }
1627         }
1628         qsort(lpNames,dwTotalLines,sizeof(char *),StringICompare);
1629         for (i=0;i<dwTotalLines-1;i++) {
1630                 if (stricmp(lpNames[i],lpNames[i+1])==0) {
1631                         memmove(&lpNames[i],&lpNames[i+1],(dwTotalLines-(i+1))*sizeof(char *));
1632                         dwTotalLines--;
1633                 }
1634         }
1635         if (dwTotalLines) {
1636                 lpdwNameHashA = (DWORD *)SFAlloc(dwTotalLines*sizeof(DWORD));
1637                 lpdwNameHashB = (DWORD *)SFAlloc(dwTotalLines*sizeof(DWORD));
1638                 for (i=0;i<dwTotalLines;i++) {
1639                         lpdwNameHashA[i] = HashString(lpNames[i],HASH_NAME_A);
1640                         lpdwNameHashB[i] = HashString(lpNames[i],HASH_NAME_B);
1641                 }
1642         }
1643         for (i=0;i<mpqOpenArc->MpqHeader.dwHashTableSize;i++) {
1644                 lpListBuffer[i].dwFileExists = 0;
1645                 lpListBuffer[i].szFileName[0] = 0;
1646                 if ((mpqOpenArc->lpHashTable[i].dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE && mpqOpenArc->lpHashTable[i].dwBlockTableIndex < mpqOpenArc->MpqHeader.dwBlockTableSize) {
1647                         lpListBuffer[i].lcLocale = mpqOpenArc->lpHashTable[i].lcLocale;
1648                         DWORD dwBlockIndex = mpqOpenArc->lpHashTable[i].dwBlockTableIndex;
1649                         lpListBuffer[i].dwCompressedSize = mpqOpenArc->lpBlockTable[dwBlockIndex].dwCompressedSize;
1650                         lpListBuffer[i].dwFullSize = mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize;
1651                         lpListBuffer[i].dwFlags = mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags;
1652                         if (dwFlags & SFILE_LIST_FLAG_UNKNOWN)
1653                                 lpListBuffer[i].dwFileExists = 1;
1654                         else
1655                                 lpListBuffer[i].dwFileExists=0xFFFFFFFF;
1656                         for (j=0;j<dwTotalLines;j++) {
1657                                 if (mpqOpenArc->lpHashTable[i].dwNameHashA==lpdwNameHashA[j] && mpqOpenArc->lpHashTable[i].dwNameHashB==lpdwNameHashB[j]) {
1658                                         strncpy(lpListBuffer[i].szFileName,lpNames[j],260);
1659                                         if (dwFlags&SFILE_LIST_ONLY_UNKNOWN && !(dwFlags&SFILE_LIST_ONLY_KNOWN)) {
1660                                                 lpListBuffer[i].dwFileExists = 0;
1661                                         }
1662                                         break;
1663                                 }
1664                                 if (j+1==dwTotalLines) {
1665                                         sprintf(lpListBuffer[i].szFileName,UNKNOWN_OUT,i);
1666                                         if (dwFlags & SFILE_LIST_FLAG_UNKNOWN) {
1667                                                 lpListBuffer[i].dwFileExists |= 2;
1668                                         }
1670                                         if (dwFlags&SFILE_LIST_ONLY_KNOWN) {
1671                                                 lpListBuffer[i].dwFileExists = 0;
1672                                         }
1673                                 }
1674                         }
1675                 }
1676         }
1677         if (lpNameBuffers) {
1678                 for (i=0;i<1+dwExtLists+dwIntLists;i++) {
1679                         if (i>=1 && i<1+dwExtLists) {
1680                                 if ((dwFlags&SFILE_LIST_MEMORY_LIST)) {
1681                                         continue;
1682                                 }
1683                         }
1684                         if (lpNameBuffers[i]) SFFree(lpNameBuffers[i]);
1685                 }
1686                 SFFree(lpNameBuffers);
1687         }
1688         if (dwExtLists) SFFree(lpFLCopy);
1689         if (lpdwNameHashA) SFFree(lpdwNameHashA);
1690         if (lpdwNameHashB) SFFree(lpdwNameHashB);
1691         return TRUE;
1694 MPQHANDLE SFMPQAPI WINAPI MpqOpenArchiveForUpdate(LPCSTR lpFileName, DWORD dwFlags, DWORD dwMaximumFilesInArchive)
1696         MPQHANDLE hMPQ;
1698         MpqOpenArchiveEx(lpFileName,0,0,&hMPQ,dwFlags,dwMaximumFilesInArchive,USE_DEFAULT_BLOCK_SIZE);
1699         return hMPQ;
1702 MPQHANDLE SFMPQAPI WINAPI MpqOpenArchiveForUpdateEx(LPCSTR lpFileName, DWORD dwFlags, DWORD dwMaximumFilesInArchive, DWORD dwBlockSize)
1704         MPQHANDLE hMPQ;
1706         MpqOpenArchiveEx(lpFileName,0,0,&hMPQ,dwFlags,dwMaximumFilesInArchive,dwBlockSize);
1707         return hMPQ;
1710 DWORD SFMPQAPI WINAPI MpqCloseUpdatedArchive(MPQHANDLE hMPQ, DWORD dwUnknown2)
1712         return SFileCloseArchive(hMPQ);
1715 BOOL SFMPQAPI WINAPI MpqAddFileToArchiveEx(MPQHANDLE hMPQ, LPCSTR lpSourceFileName, LPCSTR lpDestFileName, DWORD dwFlags, DWORD dwCompressionType, DWORD dwCompressLevel)
1717         if (!hMPQ || !lpSourceFileName || !lpDestFileName) {
1718                 SetLastError(ERROR_INVALID_PARAMETER);
1719                 return FALSE;
1720         }
1721         if (!*lpSourceFileName || !*lpDestFileName) {
1722                 SetLastError(ERROR_INVALID_PARAMETER);
1723                 return FALSE;
1724         }
1726         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
1727                 SetLastError(ERROR_ACCESS_DENIED);
1728                 return FALSE;
1729         }
1730         BOOL ReplaceExisting;
1731         if (dwFlags&MAFA_REPLACE_EXISTING) {
1732                 dwFlags = dwFlags^MAFA_REPLACE_EXISTING;
1733                 ReplaceExisting = TRUE;
1734         }
1735         else ReplaceExisting = FALSE;
1736         MPQHANDLE hFile = GetFreeHashTableEntry(hMPQ,lpDestFileName,LocaleID,ReplaceExisting);
1737         if (!hFile) return FALSE;
1738         HANDLE haFile = CreateFile(lpSourceFileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
1739         if (haFile==INVALID_HANDLE_VALUE) return FALSE;
1740         DWORD fsz = SFGetFileSize(haFile),tsz;
1741         DWORD ucfsz = fsz;
1742         BOOL IsBNcache = FALSE;char *buffer,*hbuffer;
1743         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
1744         DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
1745         DWORD TotalBlocks = fsz / blockSize;
1746         if(fsz % blockSize)
1747                 TotalBlocks++;
1748         DWORD ptsz = 0;
1749         if (dwFlags&MAFA_COMPRESS || dwFlags&MAFA_COMPRESS2) ptsz = (TotalBlocks+1)*4;
1750         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
1751         {
1752                 IsBNcache = TRUE;
1753                 hbuffer = (char *)SFAlloc(fsz+324+ptsz);
1754                 if (!hbuffer) {
1755                         CloseHandle(haFile);
1756                         return FALSE;
1757                 }
1758                 hbuffer[0] = (char)0x44;
1759                 hbuffer[1] = (char)0x01;
1760                 DWORD lpFileNameLength = strlen(lpDestFileName);
1761                 if (lpFileNameLength<=260) memcpy(hbuffer+64,lpDestFileName,lpFileNameLength);
1762                 else strncpy(hbuffer+64,lpDestFileName,260);
1763                 buffer = hbuffer+324;
1764         }
1765         else buffer = (char *)SFAlloc(fsz+ptsz);
1766         if (!buffer && fsz!=0) {
1767                 CloseHandle(haFile);
1768                 return FALSE;
1769         }
1770         if (fsz!=0) {
1771                 if (ReadFile(haFile,buffer,fsz,&tsz,0)==0) {
1772                         SFFree(buffer);
1773                         CloseHandle(haFile);
1774                         return FALSE;
1775                 }
1776         }
1777         else {
1778                 if (dwFlags&MAFA_COMPRESS) dwFlags = dwFlags^MAFA_COMPRESS;
1779                 if (dwFlags&MAFA_COMPRESS2) dwFlags = dwFlags^MAFA_COMPRESS2;
1780                 if (dwFlags&MAFA_ENCRYPT) dwFlags = dwFlags^MAFA_ENCRYPT;
1781                 if (dwFlags&MAFA_MODCRYPTKEY) dwFlags = dwFlags^MAFA_MODCRYPTKEY;
1782         }
1783         CloseHandle(haFile);
1784         DWORD i;
1785         DWORD *dwBlkpt = (DWORD *)SFAlloc((TotalBlocks+1)*4);
1786         if (!dwBlkpt) {
1787                 SFFree(buffer);
1788                 return FALSE;
1789         }
1790         if ((dwFlags & MAFA_COMPRESS) || (dwFlags & MAFA_COMPRESS2))
1791         {
1792                 DWORD dwCompressionSubType = 0;
1794                 if ((dwCompressionType&0x40 || dwCompressionType&0x80) && (memcmp(buffer,ID_RIFF,4)!=0 || memcmp(buffer+8,ID_WAVE,8)!=0 || buffer[20]!=1 || buffer[34]!=16)) {
1795                         dwCompressionType = 0x08;
1796                         dwCompressLevel = 0;
1797                 }
1798                 if (dwCompressionType&0x40 || dwCompressionType&0x80) {
1799                         switch (dwCompressLevel) {
1800                                 case MAWA_QUALITY_HIGH:
1801                                         dwCompressLevel = 1;
1802                                         break;
1803                                 case MAWA_QUALITY_LOW:
1804                                         dwCompressLevel = 3;
1805                                         break;
1806                                 default:
1807                                         dwCompressLevel = 0;
1808                         }
1809                 }
1810                 else if (dwCompressionType&0x01 || dwCompressionType&0x08 || ((dwFlags & MAFA_COMPRESS)!=MAFA_COMPRESS && (dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2)) {
1811                         dwCompressionSubType = dwCompressLevel;
1812                         dwCompressLevel = 0;
1813                 }
1814                 else if (dwCompressionType&0x02) {
1815                         dwCompressionSubType = 1;
1816                 }
1818                 DWORD LastOffset=ptsz;
1819                 BYTE *compbuffer = (BYTE *)SFAlloc(blockSize);
1820                 if (!compbuffer) {
1821                         SFFree(dwBlkpt);
1822                         SFFree(buffer);
1823                         return FALSE;
1824                 }
1825                 char *outbuffer = (char *)SFAlloc(blockSize);
1826                 if (!outbuffer) {
1827                         SFFree(compbuffer);
1828                         SFFree(dwBlkpt);
1829                         SFFree(buffer);
1830                         return FALSE;
1831                 }
1832                 char *newbuffer = (char *)SFAlloc(fsz+ptsz);
1833                 if (!newbuffer) {
1834                         SFFree(outbuffer);
1835                         SFFree(compbuffer);
1836                         SFFree(dwBlkpt);
1837                         SFFree(buffer);
1838                         return FALSE;
1839                 }
1840                 DWORD CurPos=0;
1841                 for (i=0;i<TotalBlocks;i++) {
1842                         dwBlkpt[i] = LastOffset;
1843                         if (i==TotalBlocks-1 && (ucfsz % blockSize)!=0) blockSize=ucfsz % blockSize;
1844                         DWORD outLength=blockSize;
1845                         BYTE *compdata = compbuffer;
1846                         char *oldoutbuffer = outbuffer;
1847                         if (dwFlags & MAFA_COMPRESS)
1848                         {
1849                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
1850                                 if (i==0 && (dwCompressionType&0x40 || dwCompressionType&0x80)) {
1851                                         if (SCompCompress(outbuffer, &outLength, compdata, blockSize, 0x08, 0, 0)==0)
1852                                                 outLength=blockSize;
1853                                 }
1854                                 else
1855                                 {
1856                                         if (dwCompressionType&UNSUPPORTED_COMPRESSION) {
1857                                                 LoadStorm();
1858                                                 if (stormSCompCompress) {
1859                                                         if (stormSCompCompress(outbuffer, &outLength, compdata, blockSize, dwCompressionType, dwCompressionSubType, dwCompressLevel)==0)
1860                                                                 outLength=blockSize;
1861                                                 }
1862                                         }
1863                                         else {
1864                                                 if (SCompCompress(outbuffer, &outLength, compdata, blockSize, dwCompressionType, dwCompressionSubType, dwCompressLevel)==0)
1865                                                         outLength=blockSize;
1866                                         }
1867                                 }
1868                         }
1869                         else if (dwFlags & MAFA_COMPRESS2)
1870                         {
1871                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
1872                                 if (SCompCompress(outbuffer, &outLength, compdata, blockSize, 0x08, dwCompressionSubType, 0)==0) {
1873                                         outLength=blockSize;
1874                                 }
1875                                 else {
1876                                         outbuffer++;
1877                                         outLength--;
1878                                 }
1879                         }
1880                         else
1881                         {
1882                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
1883                         }
1884                         if (outLength>=blockSize)
1885                         {
1886                                 memcpy((char *)newbuffer+LastOffset,compdata,blockSize);
1887                                 LastOffset+=blockSize;
1888                         }
1889                         else {
1890                                 memcpy((char *)newbuffer+LastOffset,outbuffer,outLength);
1891                                 LastOffset+=outLength;
1892                         }
1893                         outbuffer = oldoutbuffer;
1894                         CurPos += blockSize;
1895                 }
1896                 fsz = LastOffset;
1897                 dwBlkpt[TotalBlocks] = fsz;
1898                 memcpy(newbuffer,dwBlkpt,ptsz);
1900                 SFFree(outbuffer);
1901                 SFFree(compbuffer);
1902                 memcpy(buffer,newbuffer,fsz);
1903                 SFFree(newbuffer);
1904         }
1905         else
1906         {
1907                 for (i=0;i<TotalBlocks+1;i++) {
1908                         if (i<TotalBlocks) dwBlkpt[i] = i * blockSize;
1909                         else dwBlkpt[i] = ucfsz;
1910                 }
1911         }
1912         if (IsBNcache==TRUE)
1913         {
1914                 buffer = hbuffer;
1915                 fsz += 324;
1916         }
1917         HASHTABLEENTRY *hashEntry = (HASHTABLEENTRY *)hFile;
1918         BOOL IsNewBlockEntry = FALSE;DWORD OldBlockTableSize = sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize;
1919         if ((hashEntry->dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE)
1920         {
1921                 BLOCKTABLEENTRY *lpnBlockTable;
1922                 lpnBlockTable = (BLOCKTABLEENTRY *)SFAlloc((sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)) * (mpqOpenArc->MpqHeader.dwBlockTableSize + 1));
1923                 if(!lpnBlockTable) {
1924                         SFFree(buffer);
1925                         return FALSE;
1926                 }
1927                 if (mpqOpenArc->lpBlockTable) {
1928                         memcpy(lpnBlockTable,mpqOpenArc->lpBlockTable,sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize);
1929                         memcpy(lpnBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize + 1, mpqOpenArc->lpFileOffsetsHigh, sizeof(UInt16) * mpqOpenArc->MpqHeader.dwBlockTableSize);
1930                 }
1931                 mpqOpenArc->MpqHeader.dwBlockTableSize++;
1932                 if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
1933                 mpqOpenArc->lpBlockTable = lpnBlockTable;
1934                 mpqOpenArc->lpFileOffsetsHigh = (UInt16 *)(mpqOpenArc->lpBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize);
1935                 IsNewBlockEntry = TRUE;
1936         }
1937         DWORD BlockIndex = mpqOpenArc->MpqHeader.dwBlockTableSize - 1;
1938         if ((hashEntry->dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE)
1939         {
1940                 hashEntry->dwNameHashA = HashString(lpDestFileName,HASH_NAME_A);
1941                 hashEntry->dwNameHashB = HashString(lpDestFileName,HASH_NAME_B);
1942                 hashEntry->lcLocale = LocaleID;
1943                 hashEntry->dwBlockTableIndex = BlockIndex;
1944                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = 0;
1946         }
1947         else
1948         {
1949                 BlockIndex = hashEntry->dwBlockTableIndex;
1950         }
1951         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset==0 || mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize<fsz)
1952         {
1953                 if (mpqOpenArc->MpqHeader.dwHashTableOffset+(mpqOpenArc->MpqHeader.dwHashTableSize * sizeof(HASHTABLEENTRY))+OldBlockTableSize==mpqOpenArc->MpqHeader.dwMPQSize)
1954                 {
1955                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwHashTableOffset)
1956                         {
1957                                 mpqOpenArc->MpqHeader.dwHashTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
1958                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
1959                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
1960                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
1961                         }
1962                         else
1963                         {
1964                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwHashTableOffset;
1965                                 mpqOpenArc->MpqHeader.dwHashTableOffset += fsz;
1966                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz;
1967                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz;
1968                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
1969                         }
1970                 }
1971                 else if (mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize==mpqOpenArc->MpqHeader.dwMPQSize)
1972                 {
1973                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwBlockTableOffset)
1974                         {
1975                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
1976                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
1977                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
1978                         }
1979                         else
1980                         {
1981                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwBlockTableOffset;
1982                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz;
1983                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz;
1984                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
1985                         }
1986                 }
1987                 else
1988                 {
1989                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwMPQSize)
1990                         {
1991                                 mpqOpenArc->MpqHeader.dwBlockTableOffset = mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+fsz;
1992                                 mpqOpenArc->MpqHeader.dwMPQSize = mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize;
1993                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
1994                         }
1995                         else
1996                         {
1997                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwMPQSize;
1998                                 mpqOpenArc->MpqHeader.dwBlockTableOffset = mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+fsz;
1999                                 mpqOpenArc->MpqHeader.dwMPQSize = mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize;
2000                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2001                         }
2002                 }
2003         }
2004         mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize = fsz;
2005         mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize = ucfsz;
2006         mpqOpenArc->lpBlockTable[BlockIndex].dwFlags = dwFlags|MAFA_EXISTS;
2007         MpqWriteHeaders(mpqOpenArc);
2008         if (dwFlags & MAFA_ENCRYPT) {
2009                 DWORD dwCryptKey;
2010                 if (dwFlags&MAFA_ENCRYPT) dwCryptKey = HashString(lpDestFileName,HASH_KEY);
2011                 if (dwFlags&MAFA_MODCRYPTKEY) dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
2012                 DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2013                 DWORD TotalBlocks = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize / blockSize;
2014                 if(mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize)
2015                         TotalBlocks++;
2016                 DWORD dwBlockEnd = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize;
2017                 if (dwBlockEnd==0) dwBlockEnd = blockSize;
2018                 if ((dwFlags & MAFA_COMPRESS)==MAFA_COMPRESS || (dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2) {
2019                         EncryptData((LPBYTE)buffer,ptsz,dwCryptKey-1);
2020                 }
2021                 for (DWORD i=0;i<TotalBlocks;i++) {
2022                         EncryptData((LPBYTE)buffer+dwBlkpt[i],dwBlkpt[i+1]-dwBlkpt[i],dwCryptKey);
2023                         dwCryptKey++;
2024                 }
2025         }
2026         SFFree(dwBlkpt);
2027         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]), FILE_BEGIN);
2028         WriteFile(mpqOpenArc->hFile,buffer,fsz,&tsz,0);
2029         SFFree(buffer);
2030         WriteHashTable(mpqOpenArc);
2031         WriteBlockTable(mpqOpenArc);
2032         AddToInternalListing(hMPQ,lpDestFileName);
2034         return TRUE;
2037 BOOL SFMPQAPI WINAPI MpqAddFileToArchive(MPQHANDLE hMPQ, LPCSTR lpSourceFileName, LPCSTR lpDestFileName, DWORD dwFlags)
2039         return MpqAddFileToArchiveEx(hMPQ,lpSourceFileName,lpDestFileName,dwFlags,0x08,0);
2042 BOOL SFMPQAPI WINAPI MpqAddWaveToArchive(MPQHANDLE hMPQ, LPCSTR lpSourceFileName, LPCSTR lpDestFileName, DWORD dwFlags, DWORD dwQuality)
2044         return MpqAddFileToArchiveEx(hMPQ,lpSourceFileName,lpDestFileName,dwFlags,0x81,dwQuality);
2047 BOOL SFMPQAPI WINAPI MpqAddFileFromBufferEx(MPQHANDLE hMPQ, LPVOID lpBuffer, DWORD dwLength, LPCSTR lpFileName, DWORD dwFlags, DWORD dwCompressionType, DWORD dwCompressLevel)
2049         if (!hMPQ || !lpBuffer || !lpFileName) {
2050                 SetLastError(ERROR_INVALID_PARAMETER);
2051                 return FALSE;
2052         }
2053         if (!*lpFileName) {
2054                 SetLastError(ERROR_INVALID_PARAMETER);
2055                 return FALSE;
2056         }
2058         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
2059                 SetLastError(ERROR_ACCESS_DENIED);
2060                 return FALSE;
2061         }
2062         BOOL ReplaceExisting;
2063         if (dwFlags&MAFA_REPLACE_EXISTING) {
2064                 dwFlags = dwFlags^MAFA_REPLACE_EXISTING;
2065                 ReplaceExisting = TRUE;
2067         }
2068         else ReplaceExisting = FALSE;
2069         MPQHANDLE hFile = GetFreeHashTableEntry(hMPQ,lpFileName,LocaleID,ReplaceExisting);
2070         if (hFile==0) return FALSE;
2071         DWORD fsz = dwLength,tsz;
2072         DWORD ucfsz = fsz;
2073         BOOL IsBNcache = FALSE;char *buffer,*hbuffer;
2074         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
2075         DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2076         DWORD TotalBlocks = fsz / blockSize;
2077         if(fsz % blockSize)
2078                 TotalBlocks++;
2079         DWORD ptsz = 0;
2080         if (dwFlags&MAFA_COMPRESS || dwFlags&MAFA_COMPRESS2) ptsz = (TotalBlocks+1)*4;
2081         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
2082         {
2083                 IsBNcache = TRUE;
2084                 hbuffer = (char *)SFAlloc(fsz+324+ptsz);
2085                 if (hbuffer==0) {
2086                         return FALSE;
2087                 }
2088                 hbuffer[0] = (char)0x44;
2089                 hbuffer[1] = (char)0x01;
2090                 DWORD lpFileNameLength = strlen(lpFileName);
2091                 if (lpFileNameLength<=260) memcpy(hbuffer+64,lpFileName,lpFileNameLength);
2092                 else strncpy(hbuffer+64,lpFileName,260);
2093                 buffer = hbuffer+324;
2094         }
2095         else buffer = (char *)SFAlloc(fsz+ptsz);
2096         if (buffer==0 && fsz!=0) {
2097                 return FALSE;
2098         }
2099         if (fsz!=0) memcpy(buffer,lpBuffer,fsz);
2100         else {
2101                 if (dwFlags&MAFA_COMPRESS) dwFlags = dwFlags^MAFA_COMPRESS;
2102                 if (dwFlags&MAFA_COMPRESS2) dwFlags = dwFlags^MAFA_COMPRESS2;
2103                 if (dwFlags&MAFA_ENCRYPT) dwFlags = dwFlags^MAFA_ENCRYPT;
2104                 if (dwFlags&MAFA_MODCRYPTKEY) dwFlags = dwFlags^MAFA_MODCRYPTKEY;
2105         }
2106         DWORD i;
2107         DWORD *dwBlkpt = (DWORD *)SFAlloc((TotalBlocks+1)*4);
2109         if (dwBlkpt==0) {
2110                 SFFree(buffer);
2111                 return FALSE;
2112         }
2113         if ((dwFlags & MAFA_COMPRESS)==MAFA_COMPRESS || (dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2)
2114         {
2115                 DWORD dwCompressionSubType = 0;
2117                 if ((dwCompressionType&0x40 || dwCompressionType&0x80) && (memcmp(buffer,ID_RIFF,4)!=0 || memcmp(buffer+8,ID_WAVE,8)!=0 || buffer[20]!=1 || buffer[34]!=16)) {
2118                         dwCompressionType = 0x08;
2119                         dwCompressLevel = 0;
2120                 }
2121                 if (dwCompressionType&0x40 || dwCompressionType&0x80) {
2122                         switch (dwCompressLevel) {
2123                                 case MAWA_QUALITY_HIGH:
2124                                         dwCompressLevel = 1;
2125                                         break;
2126                                 case MAWA_QUALITY_LOW:
2127                                         dwCompressLevel = 3;
2128                                         break;
2129                                 default:
2130                                         dwCompressLevel = 0;
2131                         }
2132                 }
2133                 else if (dwCompressionType&0x01 || dwCompressionType&0x08 || ((dwFlags & MAFA_COMPRESS)!=MAFA_COMPRESS && (dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2)) {
2134                         dwCompressionSubType = dwCompressLevel;
2135                         dwCompressLevel = 0;
2136                 }
2137                 else if (dwCompressionType&0x02) {
2138                         dwCompressionSubType = 1;
2139                 }
2141                 DWORD LastOffset=ptsz;
2142                 BYTE *compbuffer = (BYTE *)SFAlloc(blockSize);
2143                 if (compbuffer==0) {
2144                         SFFree(dwBlkpt);
2145                         SFFree(buffer);
2146                         return FALSE;
2147                 }
2149                 char *outbuffer = (char *)SFAlloc(blockSize);
2150                 if (outbuffer==0) {
2151                         SFFree(compbuffer);
2152                         SFFree(dwBlkpt);
2153                         SFFree(buffer);
2154                         return FALSE;
2155                 }
2156                 char *newbuffer = (char *)SFAlloc(fsz+ptsz);
2157                 if (newbuffer==0) {
2158                         SFFree(outbuffer);
2159                         SFFree(compbuffer);
2160                         SFFree(dwBlkpt);
2161                         SFFree(buffer);
2162                         return FALSE;
2163                 }
2164                 DWORD CurPos=0;
2165                 for (i=0;i<TotalBlocks;i++) {
2166                         dwBlkpt[i] = LastOffset;
2167                         if (i==TotalBlocks-1 && (ucfsz % blockSize)!=0) blockSize=ucfsz % blockSize;
2168                         DWORD outLength=blockSize;
2169                         BYTE *compdata = compbuffer;
2170                         if (dwFlags & MAFA_COMPRESS)
2171                         {
2172                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
2173                                 if (i==0 && (dwCompressionType&0x40 || dwCompressionType&0x80)) {
2174                                         if (SCompCompress(outbuffer, &outLength, compdata, blockSize, 0x08, 0, 0)==0)
2175                                                 outLength=blockSize;
2176                                 }
2177                                 else
2178                                 {
2179                                         if (dwCompressionType&UNSUPPORTED_COMPRESSION) {
2180                                                 LoadStorm();
2181                                                 if (stormSCompCompress!=0) {
2182                                                         if (stormSCompCompress(outbuffer, &outLength, compdata, blockSize, dwCompressionType, dwCompressionSubType, dwCompressLevel)==0)
2183                                                                 outLength=blockSize;
2184                                                 }
2185                                         }
2186                                         else {
2187                                                 if (SCompCompress(outbuffer, &outLength, compdata, blockSize, dwCompressionType, dwCompressionSubType, dwCompressLevel)==0)
2188                                                         outLength=blockSize;
2189                                         }
2190                                 }
2191                         }
2192                         else if (dwFlags & MAFA_COMPRESS2)
2193                         {
2194                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
2195                                 if (SCompCompress(outbuffer, &outLength, compdata, blockSize, 0x08, dwCompressionSubType, 0)==0) {
2196                                         outLength=blockSize;
2197                                 }
2198                                 else {
2199                                         outbuffer++;
2200                                         outLength--;
2201                                 }
2202                         }
2203                         else
2204                         {
2205                                 memcpy(compdata,(char *)buffer+CurPos,blockSize);
2206                         }
2207                         if (outLength>=blockSize)
2208                         {
2209                                 memcpy((char *)newbuffer+LastOffset,compdata,blockSize);
2210                                 LastOffset+=blockSize;
2211                         }
2212                         else {
2213                                 memcpy((char *)newbuffer+LastOffset,outbuffer,outLength);
2214                                 LastOffset+=outLength;
2215                         }
2216                         CurPos += blockSize;
2217                 }
2218                 fsz = LastOffset;
2219                 dwBlkpt[TotalBlocks] = fsz;
2220                 memcpy(newbuffer,dwBlkpt,ptsz);
2221                 SFFree(outbuffer);
2222                 SFFree(compbuffer);
2223                 memcpy(buffer,newbuffer,fsz);
2224                 SFFree(newbuffer);
2225         }
2226         else
2227         {
2228                 for (i=0;i<TotalBlocks+1;i++) {
2229                         if (i<TotalBlocks) dwBlkpt[i] = i * blockSize;
2230                         else dwBlkpt[i] = ucfsz;
2231                 }
2232         }
2233         if (IsBNcache==TRUE)
2234         {
2235                 buffer = hbuffer;
2236                 fsz += 324;
2237         }
2238         HASHTABLEENTRY *hashEntry = (HASHTABLEENTRY *)hFile;
2240         BOOL IsNewBlockEntry = FALSE;DWORD OldBlockTableSize = sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize;
2241         if ((hashEntry->dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE)
2242         {
2243                 BLOCKTABLEENTRY *lpnBlockTable;
2244                 lpnBlockTable = (BLOCKTABLEENTRY *)SFAlloc((sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)) * (mpqOpenArc->MpqHeader.dwBlockTableSize + 1));
2245                 if(!lpnBlockTable) {
2246                         SFFree(buffer);
2247                         return FALSE;
2248                 }
2249                 if (mpqOpenArc->lpBlockTable) {
2250                         memcpy(lpnBlockTable,mpqOpenArc->lpBlockTable,sizeof(BLOCKTABLEENTRY) * mpqOpenArc->MpqHeader.dwBlockTableSize);
2251                         memcpy(lpnBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize + 1, mpqOpenArc->lpFileOffsetsHigh, sizeof(UInt16) * mpqOpenArc->MpqHeader.dwBlockTableSize);
2252                 }
2253                 mpqOpenArc->MpqHeader.dwBlockTableSize++;
2254                 if (mpqOpenArc->lpBlockTable) SFFree(mpqOpenArc->lpBlockTable);
2255                 mpqOpenArc->lpBlockTable = lpnBlockTable;
2256                 mpqOpenArc->lpFileOffsetsHigh = (UInt16 *)(mpqOpenArc->lpBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize);
2257                 IsNewBlockEntry = TRUE;
2258         }
2259         DWORD BlockIndex = mpqOpenArc->MpqHeader.dwBlockTableSize - 1;
2260         if ((hashEntry->dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE)
2261         {
2262                 hashEntry->dwNameHashA = HashString(lpFileName,HASH_NAME_A);
2263                 hashEntry->dwNameHashB = HashString(lpFileName,HASH_NAME_B);
2264                 hashEntry->lcLocale = LocaleID;
2265                 hashEntry->dwBlockTableIndex = BlockIndex;
2266                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = 0;
2267         }
2268         else
2269         {
2270                 BlockIndex = hashEntry->dwBlockTableIndex;
2271         }
2272         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset==0 || mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize<fsz)
2273         {
2274                 if (mpqOpenArc->MpqHeader.dwHashTableOffset+(mpqOpenArc->MpqHeader.dwHashTableSize * sizeof(HASHTABLEENTRY))+OldBlockTableSize==mpqOpenArc->MpqHeader.dwMPQSize)
2275                 {
2276                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwHashTableOffset)
2277                         {
2278                                 mpqOpenArc->MpqHeader.dwHashTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
2279                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
2280                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
2281                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2282                         }
2283                         else
2284                         {
2285                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwHashTableOffset;
2286                                 mpqOpenArc->MpqHeader.dwHashTableOffset += fsz;
2287                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz;
2288                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz;
2289                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2290                         }
2291                 }
2292                 else if (mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize==mpqOpenArc->MpqHeader.dwMPQSize)
2293                 {
2294                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwBlockTableOffset)
2295                         {
2296                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
2297                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz-mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize;
2298                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2299                         }
2300                         else
2301                         {
2302                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwBlockTableOffset;
2303                                 mpqOpenArc->MpqHeader.dwBlockTableOffset += fsz;
2304                                 mpqOpenArc->MpqHeader.dwMPQSize += fsz;
2305                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2306                         }
2307                 }
2308                 else
2309                 {
2310                         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize==mpqOpenArc->MpqHeader.dwMPQSize)
2311                         {
2312                                 mpqOpenArc->MpqHeader.dwBlockTableOffset = mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+fsz;
2313                                 mpqOpenArc->MpqHeader.dwMPQSize = mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize;
2314                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2315                         }
2316                         else
2317                         {
2318                                 mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset = mpqOpenArc->MpqHeader.dwMPQSize;
2319                                 mpqOpenArc->MpqHeader.dwBlockTableOffset = mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset+fsz;
2320                                 mpqOpenArc->MpqHeader.dwMPQSize = mpqOpenArc->MpqHeader.dwBlockTableOffset+OldBlockTableSize;
2321                                 if (IsNewBlockEntry==TRUE) mpqOpenArc->MpqHeader.dwMPQSize += sizeof(BLOCKTABLEENTRY);
2322                         }
2323                 }
2324         }
2325         mpqOpenArc->lpBlockTable[BlockIndex].dwCompressedSize = fsz;
2326         mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize = ucfsz;
2327         mpqOpenArc->lpBlockTable[BlockIndex].dwFlags = dwFlags|MAFA_EXISTS;
2328         MpqWriteHeaders(mpqOpenArc);
2329         if (dwFlags & MAFA_ENCRYPT) {
2330                 DWORD dwCryptKey;
2331                 if (dwFlags&MAFA_ENCRYPT) dwCryptKey = HashString(lpFileName,HASH_KEY);
2332                 if (dwFlags&MAFA_MODCRYPTKEY) dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
2333                 DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2334                 DWORD TotalBlocks = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize / blockSize;
2336                 if(mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize)
2337                         TotalBlocks++;
2338                 DWORD dwBlockEnd = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize;
2339                 if (dwBlockEnd==0) dwBlockEnd = blockSize;
2340                 if ((dwFlags & MAFA_COMPRESS)==MAFA_COMPRESS || (dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2) {
2341                         EncryptData((LPBYTE)buffer,ptsz,dwCryptKey-1);
2342                 }
2343                 for (DWORD i=0;i<TotalBlocks;i++) {
2344                         EncryptData((LPBYTE)buffer+dwBlkpt[i],dwBlkpt[i+1]-dwBlkpt[i],dwCryptKey);
2345                         dwCryptKey++;
2346                 }
2347         }
2348         SFFree(dwBlkpt);
2349         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]), FILE_BEGIN);
2350         WriteFile(mpqOpenArc->hFile,buffer,fsz,&tsz,0);
2351         SFFree(buffer);
2352         WriteHashTable(mpqOpenArc);
2353         WriteBlockTable(mpqOpenArc);
2354         AddToInternalListing(hMPQ,lpFileName);
2355         return TRUE;
2358 BOOL SFMPQAPI WINAPI MpqAddFileFromBuffer(MPQHANDLE hMPQ, LPVOID lpBuffer, DWORD dwLength, LPCSTR lpFileName, DWORD dwFlags)
2360         return MpqAddFileFromBufferEx(hMPQ,lpBuffer,dwLength,lpFileName,dwFlags,0x08,0);
2363 BOOL SFMPQAPI WINAPI MpqAddWaveFromBuffer(MPQHANDLE hMPQ, LPVOID lpBuffer, DWORD dwLength, LPCSTR lpFileName, DWORD dwFlags, DWORD dwQuality)
2365         return MpqAddFileFromBufferEx(hMPQ,lpBuffer,dwLength,lpFileName,dwFlags,0x81,dwQuality);
2368 BOOL SFMPQAPI WINAPI MpqRenameFile(MPQHANDLE hMPQ, LPCSTR lpcOldFileName, LPCSTR lpcNewFileName)
2370         return MpqRenameAndSetFileLocale(hMPQ,lpcOldFileName,lpcNewFileName,LocaleID,LocaleID);
2373 BOOL SFMPQAPI WINAPI MpqRenameAndSetFileLocale(MPQHANDLE hMPQ, LPCSTR lpcOldFileName, LPCSTR lpcNewFileName, LCID nOldLocale, LCID nNewLocale)
2375         if (!hMPQ || !lpcOldFileName || !lpcNewFileName) {
2376                 SetLastError(ERROR_INVALID_PARAMETER);
2377                 return FALSE;
2378         }
2379         if (!*lpcNewFileName) {
2380                 SetLastError(ERROR_INVALID_PARAMETER);
2381                 return FALSE;
2382         }
2384         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
2385                 SetLastError(ERROR_ACCESS_DENIED);
2386                 return FALSE;
2387         }
2388         if (stricmp(lpcOldFileName,lpcNewFileName)==0) {
2389                 SetLastError(ERROR_INVALID_PARAMETER);
2390                 return FALSE;
2391         }
2392         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
2393         HASHTABLEENTRY *oldHashEntry = (HASHTABLEENTRY *)GetHashTableEntry(hMPQ,lpcOldFileName,nOldLocale);
2394         if (oldHashEntry==0) {
2395                 SetLastError(MPQ_ERROR_FILE_NOT_FOUND);
2396                 return FALSE;
2397         }
2398         if (oldHashEntry->lcLocale!=nOldLocale) {
2399                 SetLastError(MPQ_ERROR_FILE_NOT_FOUND);
2400                 return FALSE;
2401         }
2402         HASHTABLEENTRY *newHashEntry = (HASHTABLEENTRY *)GetFreeHashTableEntry(hMPQ,lpcNewFileName,nNewLocale,TRUE);
2403         if ((newHashEntry->dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE)
2404                 return FALSE;
2405         if (newHashEntry==0) newHashEntry = oldHashEntry;
2406         DWORD tsz;
2407         newHashEntry->dwNameHashA = HashString(lpcNewFileName,HASH_NAME_A);
2408         newHashEntry->dwNameHashB = HashString(lpcNewFileName,HASH_NAME_B);
2409         newHashEntry->lcLocale = nNewLocale;
2410         newHashEntry->dwBlockTableIndex = oldHashEntry->dwBlockTableIndex;
2411         DWORD BlockIndex = oldHashEntry->dwBlockTableIndex;
2412         if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_ENCRYPT)
2413         {
2414                 DWORD dwOldCryptKey = HashString(lpcOldFileName,HASH_KEY);
2415                 DWORD dwNewCryptKey = HashString(lpcNewFileName,HASH_KEY);
2416                 if (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_MODCRYPTKEY) {
2417                         dwOldCryptKey = (dwOldCryptKey + mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
2419                         dwNewCryptKey = (dwNewCryptKey + mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
2420                 }
2421                 if (dwOldCryptKey!=dwNewCryptKey)
2422                 {
2423                         DWORD HeaderLength=0;
2424                         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
2425                         {
2426                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]), FILE_BEGIN);
2427                                 ReadFile(mpqOpenArc->hFile,&HeaderLength,4,&tsz,0);
2429                         }
2430                         DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2431                         DWORD TotalBlocks = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize / blockSize;
2432                         if(mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize % blockSize)
2433                                 TotalBlocks++;
2434                         DWORD *dwBlockPtrTable = (DWORD *)SFAlloc((TotalBlocks+1)*4);
2435                         if (dwBlockPtrTable==0) {
2436                                 return FALSE;
2437                         }
2438                         DWORD i;
2439                         if ((mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS)==MAFA_COMPRESS || (mpqOpenArc->lpBlockTable[BlockIndex].dwFlags & MAFA_COMPRESS2)==MAFA_COMPRESS2)
2440                         {
2441                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength, FILE_BEGIN);
2442                                 ReadFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&tsz,0);
2443                                 DecryptData((LPBYTE)dwBlockPtrTable,(TotalBlocks+1)*4,dwOldCryptKey-1);
2444                                 char *EncryptedTable = (char *)SFAlloc((TotalBlocks+1)*4);
2445                                 if (EncryptedTable==0) {
2446                                         SFFree(dwBlockPtrTable);
2447                                         return FALSE;
2448                                 }
2449                                 memcpy(EncryptedTable,dwBlockPtrTable,(TotalBlocks+1)*4);
2450                                 EncryptData((LPBYTE)EncryptedTable,(TotalBlocks+1)*4,dwNewCryptKey-1);
2451                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength, FILE_BEGIN);
2452                                 WriteFile(mpqOpenArc->hFile,EncryptedTable,(TotalBlocks+1)*4,&tsz,0);
2453                                 SFFree(EncryptedTable);
2454                         }
2455                         else
2456                         {
2457                                 for (i=0;i<TotalBlocks+1;i++) {
2458                                         if (i<TotalBlocks) dwBlockPtrTable[i] = i * blockSize;
2459                                         else dwBlockPtrTable[i] = mpqOpenArc->lpBlockTable[BlockIndex].dwFullSize;
2460                                 }
2461                         }
2462                         char *blkBuffer = (char *)SFAlloc(blockSize);
2463                         if (blkBuffer==0) {
2464                                 EncryptData((LPBYTE)dwBlockPtrTable,(TotalBlocks+1)*4,dwOldCryptKey-1);
2465                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength, FILE_BEGIN);
2466                                 WriteFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&tsz,0);
2467                                 SFFree(dwBlockPtrTable);
2468                                 return FALSE;
2469                         }
2470                         for (i=0;i<TotalBlocks;i++) {
2471                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength + dwBlockPtrTable[i], FILE_BEGIN);
2472                                 if (ReadFile(mpqOpenArc->hFile,blkBuffer,dwBlockPtrTable[i+1]-dwBlockPtrTable[i],&tsz,0)==0) {
2473                                         EncryptData((LPBYTE)dwBlockPtrTable,(TotalBlocks+1)*4,dwOldCryptKey-1);
2474                                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength, FILE_BEGIN);
2475                                         WriteFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&tsz,0);
2476                                         SFFree(dwBlockPtrTable);
2477                                         SFFree(blkBuffer);
2478                                         return FALSE;
2479                                 }
2480                                 DecryptData((LPBYTE)blkBuffer,dwBlockPtrTable[i+1]-dwBlockPtrTable[i],dwOldCryptKey+i);
2481                                 EncryptData((LPBYTE)blkBuffer,dwBlockPtrTable[i+1]-dwBlockPtrTable[i],dwNewCryptKey+i);
2482                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[BlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[BlockIndex]) + HeaderLength + dwBlockPtrTable[i], FILE_BEGIN);
2483                                 WriteFile(mpqOpenArc->hFile,blkBuffer,dwBlockPtrTable[i+1]-dwBlockPtrTable[i],&tsz,0);
2484                         }
2485                         SFFree(dwBlockPtrTable);
2486                         SFFree(blkBuffer);
2487                 }
2488         }
2489         if (oldHashEntry!=newHashEntry) {
2490                 oldHashEntry->dwNameHashA = 0xFFFFFFFF;
2491                 oldHashEntry->dwNameHashB = 0xFFFFFFFF;
2492                 oldHashEntry->lcLocale = 0xFFFFFFFF;
2493                 oldHashEntry->dwBlockTableIndex = 0xFFFFFFFE;
2494                 WriteHashTable(mpqOpenArc);
2495         }
2496         LCID dwOldLocale=LocaleID;
2497         LocaleID=nOldLocale;
2498         RemoveFromInternalListing(hMPQ,lpcOldFileName);
2500         LocaleID=nNewLocale;
2501         AddToInternalListing(hMPQ,lpcNewFileName);
2502         LocaleID=dwOldLocale;
2503         return TRUE;
2506 BOOL SFMPQAPI WINAPI MpqDeleteFile(MPQHANDLE hMPQ, LPCSTR lpFileName)
2508         return MpqDeleteFileWithLocale(hMPQ,lpFileName,LocaleID);
2511 BOOL SFMPQAPI WINAPI MpqDeleteFileWithLocale(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID nLocale)
2513         if (!hMPQ || !lpFileName) {
2514                 SetLastError(ERROR_INVALID_PARAMETER);
2515                 return FALSE;
2516         }
2518         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
2520                 SetLastError(ERROR_ACCESS_DENIED);
2521                 return FALSE;
2522         }
2523         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
2524         HASHTABLEENTRY *hashEntry = (HASHTABLEENTRY *)GetHashTableEntry(hMPQ,lpFileName,nLocale);
2525         if (hashEntry==0) {
2526                 SetLastError(MPQ_ERROR_FILE_NOT_FOUND);
2527                 return FALSE;
2528         }
2529         if (hashEntry->lcLocale!=nLocale) return FALSE;
2530         hashEntry->dwNameHashA = 0xFFFFFFFF;
2531         hashEntry->dwNameHashB = 0xFFFFFFFF;
2532         hashEntry->lcLocale = 0xFFFFFFFF;
2533         hashEntry->dwBlockTableIndex = 0xFFFFFFFE;
2534         WriteHashTable(mpqOpenArc);
2535         LCID dwOldLocale=LocaleID;
2536         LocaleID=nLocale;
2537         RemoveFromInternalListing(hMPQ,lpFileName);
2538         LocaleID=dwOldLocale;
2539         return TRUE;
2542 BOOL SFMPQAPI WINAPI MpqCompactArchive(MPQHANDLE hMPQ)
2544         if (!hMPQ) {
2545                 SetLastError(ERROR_INVALID_PARAMETER);
2546                 return FALSE;
2547         }
2549         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
2550                 SetLastError(ERROR_ACCESS_DENIED);
2551                 return FALSE;
2552         }
2553         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
2554         TempAlloc NewAlloc;
2555         char *lpFileName = (char *)NewAlloc.Alloc(strlen(mpqOpenArc->lpFileName)+13);
2556         sprintf(lpFileName,"%s.compact",mpqOpenArc->lpFileName);
2557         HANDLE hFile = CreateFile(lpFileName,GENERIC_READ|GENERIC_WRITE,0,0,CREATE_NEW,0,0);
2558         UInt64 i;
2559         if (hFile==INVALID_HANDLE_VALUE) {
2560                 for (i=0;i<10000;i++) {
2561                         sprintf(lpFileName,"%s.compact.%04ld",mpqOpenArc->lpFileName,i);
2563                         hFile = CreateFile(lpFileName,GENERIC_READ|GENERIC_WRITE,0,0,CREATE_NEW,0,0);
2564                         if (hFile!=INVALID_HANDLE_VALUE) break;
2565                 }
2566                 if (i==10000) return FALSE;
2567         }
2568         UInt64 qwLastOffset = mpqOpenArc->dwHeaderSize;
2569         DWORD tsz;
2570         char *buffer = (char *)SFAlloc(65536);
2571         if (buffer==0) {
2572                 CloseHandle(hFile);
2573                 DeleteFile(lpFileName);
2574                 return FALSE;
2575         }
2576         HASHTABLEENTRY *lpHashTable = (HASHTABLEENTRY *)SFAlloc(sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize);
2577         if (lpHashTable==0) {
2578                 SFFree(buffer);
2579                 CloseHandle(hFile);
2580                 DeleteFile(lpFileName);
2581                 return FALSE;
2582         }
2583         BLOCKTABLEENTRY *lpBlockTable = (BLOCKTABLEENTRY *)SFAlloc((sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)) * mpqOpenArc->MpqHeader.dwBlockTableSize);
2584         UInt16 *lpdwFileOffsetsHigh = (UInt16 *)(lpBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize);
2585         if(mpqOpenArc->lpBlockTable!=0) {
2586                 if (lpBlockTable==0) {
2587                         SFFree(lpHashTable);
2588                         SFFree(buffer);
2589                         CloseHandle(hFile);
2590                         DeleteFile(lpFileName);
2592                         return FALSE;
2593                 }
2594         }
2595         DWORD j=0,nBlkOffset=0,ReadSize=0;
2596         memcpy(lpHashTable,mpqOpenArc->lpHashTable,sizeof(HASHTABLEENTRY) * mpqOpenArc->MpqHeader.dwHashTableSize);
2597         for (i=0;i<mpqOpenArc->MpqHeader.dwBlockTableSize;i++) {
2598                 for (j=0;j<mpqOpenArc->MpqHeader.dwHashTableSize;j++) {
2599                         if (lpHashTable[j].dwBlockTableIndex==(i-nBlkOffset)) break;
2600                 }
2601                 if (j<mpqOpenArc->MpqHeader.dwHashTableSize) {
2602                         memcpy(&lpBlockTable[i-nBlkOffset],&mpqOpenArc->lpBlockTable[i],sizeof(BLOCKTABLEENTRY));
2603                         SplitUInt64(lpBlockTable[i-nBlkOffset].dwFileOffset, lpdwFileOffsetsHigh[i-nBlkOffset], qwLastOffset);
2604                         qwLastOffset += mpqOpenArc->lpBlockTable[i].dwCompressedSize;
2605                         DWORD dwWritten=FALSE;
2606                         if (mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_ENCRYPT && mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_MODCRYPTKEY && (mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_COMPRESS || mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_COMPRESS2))
2607                         {
2608                                 DWORD HeaderLength=0;
2609                                 if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
2610                                 {
2611                                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]), FILE_BEGIN);
2612                                         ReadFile(mpqOpenArc->hFile,&HeaderLength,4,&tsz,0);
2613                                 }
2614                                 DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2615                                 DWORD TotalBlocks = mpqOpenArc->lpBlockTable[i].dwFullSize / blockSize;
2616                                 if(mpqOpenArc->lpBlockTable[i].dwFullSize % blockSize)
2617                                         TotalBlocks++;
2618                                 DWORD *dwBlockPtrTable = (DWORD *)SFAlloc((TotalBlocks+1)*4);
2619                                 if (dwBlockPtrTable==0) {
2620                                         SFFree(lpBlockTable);
2621                                         SFFree(lpHashTable);
2622                                         SFFree(buffer);
2623                                         CloseHandle(hFile);
2624                                         DeleteFile(lpFileName);
2625                                         return FALSE;
2626                                 }
2627                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]) + HeaderLength, FILE_BEGIN);
2628                                 ReadFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&tsz,0);
2629                                 DWORD dwOldCryptKey = DetectFileSeed(dwBlockPtrTable,(TotalBlocks+1)*4,blockSize);
2630                                 DWORD dwNewCryptKey = (dwOldCryptKey ^ mpqOpenArc->lpBlockTable[i].dwFullSize) - mpqOpenArc->lpBlockTable[i].dwFileOffset;
2631                                 dwNewCryptKey = (dwNewCryptKey + lpBlockTable[i-nBlkOffset].dwFileOffset) ^ lpBlockTable[i-nBlkOffset].dwFullSize;
2632                                 if (dwOldCryptKey==0) {
2633                                         DWORD dwNameHashA = HashString(INTERNAL_LISTFILE,HASH_NAME_A);
2634                                         DWORD dwNameHashB = HashString(INTERNAL_LISTFILE,HASH_NAME_B);
2635                                         if (lpHashTable[j].dwNameHashA==dwNameHashA && lpHashTable[j].dwNameHashB==dwNameHashB) {
2636                                                 dwOldCryptKey = HashString(INTERNAL_LISTFILE,HASH_KEY);
2637                                                 dwNewCryptKey = dwOldCryptKey;
2638                                                 dwOldCryptKey = (dwOldCryptKey + mpqOpenArc->lpBlockTable[i].dwFileOffset) ^ mpqOpenArc->lpBlockTable[i].dwFullSize;
2639                                                 dwNewCryptKey = (dwNewCryptKey + lpBlockTable[i-nBlkOffset].dwFileOffset) ^ lpBlockTable[i-nBlkOffset].dwFullSize;
2640                                         }
2641                                         else {
2642                                                 HANDLE hlFile;
2643                                                 DWORD fsz;
2644                                                 char *listbuffer;
2645                                                 LCID lcOldLocale = LocaleID;
2646                                                 for (DWORD lcn=0;lcn<nLocales;lcn++) {
2647                                                         LocaleID = availLocales[lcn];
2648                                                         if (SFileOpenFileEx(hMPQ,INTERNAL_LISTFILE,0,&hlFile)!=0) {
2649                                                                 if (((MPQFILE *)hlFile)->lpHashEntry->lcLocale==0 && LocaleID!=0) {
2651                                                                         SFileCloseFile(hlFile);
2652                                                                         continue;
2653                                                                 }
2654                                                                 fsz = SFileGetFileSize(hlFile,0);
2655                                                                 if (fsz>0) {
2656                                                                         listbuffer = (char *)SFAlloc(fsz+1);
2657                                                                         if (listbuffer==0) {
2658                                                                                 SFileCloseFile(hlFile);
2659                                                                                 continue;
2660                                                                         }
2661                                                                         if (SFileReadFile(hlFile,listbuffer,fsz,0,0)==0) {
2662                                                                                 SFFree(listbuffer);
2663                                                                                 listbuffer = 0;
2664                                                                         }
2665                                                                 }
2666                                                                 SFileCloseFile(hlFile);
2667                                                                 if (listbuffer!=0) {
2668                                                                         char *listline;
2669                                                                         for (listline=listbuffer;listline!=0;listline=nextline(listline)) {
2670                                                                                 if (listline[0]==0) break;
2671                                                                                 DWORD lnlen=strlnlen(listline);
2672                                                                                 char prevchar=listline[lnlen];
2673                                                                                 listline[lnlen]=0;
2674                                                                                 dwNameHashA = HashString(listline,HASH_NAME_A);
2675                                                                                 dwNameHashB = HashString(listline,HASH_NAME_B);
2676                                                                                 if (lpHashTable[j].dwNameHashA==dwNameHashA && lpHashTable[j].dwNameHashB==dwNameHashB) {
2677                                                                                         dwOldCryptKey = HashString(listline,HASH_KEY);
2678                                                                                         dwNewCryptKey = dwOldCryptKey;
2679                                                                                         dwOldCryptKey = (dwOldCryptKey + mpqOpenArc->lpBlockTable[i].dwFileOffset) ^ mpqOpenArc->lpBlockTable[i].dwFullSize;
2680                                                                                         dwNewCryptKey = (dwNewCryptKey + lpBlockTable[i-nBlkOffset].dwFileOffset) ^ lpBlockTable[i-nBlkOffset].dwFullSize;
2681                                                                                         break;
2682                                                                                 }
2683                                                                                 listline[lnlen]=prevchar;
2684                                                                         }
2685                                                                         if (listline!=0) {
2686                                                                                 if (listline[0]!=0) {
2687                                                                                         SFFree(listbuffer);
2688                                                                                         break;
2689                                                                                 }
2690                                                                         }
2691                                                                         SFFree(listbuffer);
2692                                                                 }
2693                                                         }
2694                                                 }
2695                                                 LocaleID = lcOldLocale;
2696                                         }
2697                                 }
2698                                 if (dwOldCryptKey!=dwNewCryptKey)
2699                                 {
2700                                         DecryptData((LPBYTE)dwBlockPtrTable,(TotalBlocks+1)*4,dwOldCryptKey-1);
2701                                         char *EncryptedTable = (char *)SFAlloc((TotalBlocks+1)*4);
2702                                         if (EncryptedTable==0) {
2703                                                 SFFree(dwBlockPtrTable);
2704                                                 SFFree(lpBlockTable);
2705                                                 SFFree(lpHashTable);
2706                                                 SFFree(buffer);
2707                                                 CloseHandle(hFile);
2708                                                 DeleteFile(lpFileName);
2709                                                 return FALSE;
2710                                         }
2711                                         memcpy(EncryptedTable,dwBlockPtrTable,(TotalBlocks+1)*4);
2712                                         EncryptData((LPBYTE)EncryptedTable,(TotalBlocks+1)*4,dwNewCryptKey-1);
2713                                         SFSetFilePointer(hFile, MakeUInt64(lpBlockTable[i-nBlkOffset].dwFileOffset, lpdwFileOffsetsHigh[i-nBlkOffset]) + HeaderLength, FILE_BEGIN);
2714                                         WriteFile(hFile,EncryptedTable,(TotalBlocks+1)*4,&tsz,0);
2715                                         SFFree(EncryptedTable);
2716                                         char *blkBuffer = (char *)SFAlloc(blockSize);
2717                                         if (blkBuffer==0) {
2718                                                 SFFree(dwBlockPtrTable);
2719                                                 SFFree(lpBlockTable);
2720                                                 SFFree(lpHashTable);
2721                                                 SFFree(buffer);
2722                                                 CloseHandle(hFile);
2723                                                 DeleteFile(lpFileName);
2724                                                 return FALSE;
2725                                         }
2726                                         for (DWORD k=0;k<TotalBlocks;k++) {
2727                                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]) + HeaderLength + dwBlockPtrTable[k], FILE_BEGIN);
2728                                                 if (ReadFile(mpqOpenArc->hFile,blkBuffer,dwBlockPtrTable[k+1]-dwBlockPtrTable[k],&tsz,0)==0) {
2729                                                         SFFree(dwBlockPtrTable);
2730                                                         SFFree(blkBuffer);
2731                                                         SFFree(lpBlockTable);
2732                                                         SFFree(lpHashTable);
2733                                                         SFFree(buffer);
2734                                                         CloseHandle(hFile);
2735                                                         DeleteFile(lpFileName);
2736                                                         return FALSE;
2737                                                 }
2738                                                 DecryptData((LPBYTE)blkBuffer,dwBlockPtrTable[k+1]-dwBlockPtrTable[k],dwOldCryptKey+k);
2739                                                 EncryptData((LPBYTE)blkBuffer,dwBlockPtrTable[k+1]-dwBlockPtrTable[k],dwNewCryptKey+k);
2740                                                 SFSetFilePointer(hFile, MakeUInt64(lpBlockTable[i-nBlkOffset].dwFileOffset, lpdwFileOffsetsHigh[i-nBlkOffset]) + HeaderLength + dwBlockPtrTable[k], FILE_BEGIN);
2741                                                 WriteFile(hFile,blkBuffer,dwBlockPtrTable[k+1]-dwBlockPtrTable[k],&tsz,0);
2742                                         }
2743                                         SFFree(blkBuffer);
2744                                         dwWritten = TRUE;
2745                                 }
2746                                 SFFree(dwBlockPtrTable);
2747                         }
2748                         else if (mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_ENCRYPT && mpqOpenArc->lpBlockTable[i].dwFlags&MAFA_MODCRYPTKEY)
2749                         {
2750                                 DWORD HeaderLength=0;
2751                                 if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
2753                                 {
2754                                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]), FILE_BEGIN);
2755                                         ReadFile(mpqOpenArc->hFile,&HeaderLength,4,&tsz,0);
2756                                 }
2757                                 DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
2758                                 DWORD TotalBlocks = mpqOpenArc->lpBlockTable[i].dwFullSize / blockSize;
2759                                 if(mpqOpenArc->lpBlockTable[i].dwFullSize % blockSize)
2760                                         TotalBlocks++;
2761                                 DWORD dwNameHashA = HashString(INTERNAL_LISTFILE,HASH_NAME_A);
2762                                 DWORD dwNameHashB = HashString(INTERNAL_LISTFILE,HASH_NAME_B);
2763                                 DWORD dwOldCryptKey=0;
2764                                 DWORD dwNewCryptKey=0;
2765                                 if (lpHashTable[j].dwNameHashA==dwNameHashA && lpHashTable[j].dwNameHashB==dwNameHashB) {
2766                                         dwOldCryptKey = HashString(INTERNAL_LISTFILE,HASH_KEY);
2767                                         dwNewCryptKey = dwOldCryptKey;
2768                                         dwOldCryptKey = (dwOldCryptKey + mpqOpenArc->lpBlockTable[i].dwFileOffset) ^ mpqOpenArc->lpBlockTable[i].dwFullSize;
2769                                         dwNewCryptKey = (dwNewCryptKey + lpBlockTable[i-nBlkOffset].dwFileOffset) ^ lpBlockTable[i-nBlkOffset].dwFullSize;
2770                                 }
2771                                 else {
2772                                         HANDLE hlFile;
2773                                         DWORD fsz;
2775                                         char *listbuffer;
2776                                         LCID lcOldLocale = LocaleID;
2777                                         for (DWORD lcn=0;lcn<nLocales;lcn++) {
2778                                                 LocaleID = availLocales[lcn];
2779                                                 if (SFileOpenFileEx(hMPQ,INTERNAL_LISTFILE,0,&hlFile)!=0) {
2780                                                         if (((MPQFILE *)hlFile)->lpHashEntry->lcLocale==0 && LocaleID!=0) {
2781                                                                 SFileCloseFile(hlFile);
2782                                                                 continue;
2783                                                         }
2784                                                         fsz = SFileGetFileSize(hlFile,0);
2785                                                         if (fsz>0) {
2786                                                                 listbuffer = (char *)SFAlloc(fsz+1);
2787                                                                 if (listbuffer==0) {
2788                                                                         SFileCloseFile(hlFile);
2789                                                                         continue;
2790                                                                 }
2791                                                                 if (SFileReadFile(hlFile,listbuffer,fsz,0,0)==0) {
2792                                                                         SFFree(listbuffer);
2793                                                                         listbuffer = 0;
2794                                                                 }
2795                                                         }
2796                                                         SFileCloseFile(hlFile);
2797                                                         if (listbuffer!=0) {
2798                                                                 char *listline;
2799                                                                 for (listline=listbuffer;listline!=0;listline=nextline(listline)) {
2800                                                                         if (listline[0]==0) break;
2801                                                                         DWORD lnlen=strlnlen(listline);
2802                                                                         char prevchar=listline[lnlen];
2803                                                                         listline[lnlen]=0;
2804                                                                         dwNameHashA = HashString(listline,HASH_NAME_A);
2805                                                                         dwNameHashB = HashString(listline,HASH_NAME_B);
2806                                                                         if (lpHashTable[j].dwNameHashA==dwNameHashA && lpHashTable[j].dwNameHashB==dwNameHashB) {
2807                                                                                 dwOldCryptKey = HashString(listline,HASH_KEY);
2808                                                                                 dwNewCryptKey = dwOldCryptKey;
2809                                                                                 dwOldCryptKey = (dwOldCryptKey + mpqOpenArc->lpBlockTable[i].dwFileOffset) ^ mpqOpenArc->lpBlockTable[i].dwFullSize;
2810                                                                                 dwNewCryptKey = (dwNewCryptKey + lpBlockTable[i-nBlkOffset].dwFileOffset) ^ lpBlockTable[i-nBlkOffset].dwFullSize;
2811                                                                                 break;
2812                                                                         }
2813                                                                         listline[lnlen]=prevchar;
2814                                                                 }
2815                                                                 if (listline!=0) {
2816                                                                         if (listline[0]!=0) {
2817                                                                                 SFFree(listbuffer);
2818                                                                                 break;
2819                                                                         }
2820                                                                 }
2821                                                                 SFFree(listbuffer);
2822                                                         }
2823                                                 }
2824                                         }
2825                                         LocaleID = lcOldLocale;
2826                                 }
2827                                 if (dwOldCryptKey!=dwNewCryptKey)
2828                                 {
2829                                         char *blkBuffer = (char *)SFAlloc(blockSize);
2830                                         if (blkBuffer==0) {
2831                                                 SFFree(lpBlockTable);
2832                                                 SFFree(lpHashTable);
2833                                                 SFFree(buffer);
2834                                                 CloseHandle(hFile);
2835                                                 DeleteFile(lpFileName);
2836                                                 return FALSE;
2837                                         }
2838                                         for (DWORD k=0;k<mpqOpenArc->lpBlockTable[i].dwFullSize;k+=blockSize) {
2839                                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]) + HeaderLength + k, FILE_BEGIN);
2840                                                 if (k+blockSize>mpqOpenArc->lpBlockTable[i].dwFullSize) blockSize = mpqOpenArc->lpBlockTable[i].dwFullSize % blockSize;
2841                                                 if (ReadFile(mpqOpenArc->hFile,blkBuffer,blockSize,&tsz,0)==0) {
2842                                                         SFFree(blkBuffer);
2843                                                         SFFree(lpBlockTable);
2844                                                         SFFree(lpHashTable);
2845                                                         SFFree(buffer);
2846                                                         CloseHandle(hFile);
2847                                                         DeleteFile(lpFileName);
2848                                                         return FALSE;
2850                                                 }
2851                                                 DecryptData((LPBYTE)blkBuffer,blockSize,dwOldCryptKey+k);
2852                                                 EncryptData((LPBYTE)blkBuffer,blockSize,dwNewCryptKey+k);
2853                                                 SFSetFilePointer(hFile, MakeUInt64(lpBlockTable[i-nBlkOffset].dwFileOffset, lpdwFileOffsetsHigh[i-nBlkOffset]) + HeaderLength + k, FILE_BEGIN);
2854                                                 WriteFile(hFile,blkBuffer,blockSize,&tsz,0);
2855                                         }
2856                                         SFFree(blkBuffer);
2857                                         dwWritten = TRUE;
2858                                 }
2859                         }
2860                         if (dwWritten==FALSE) {
2861                                 ReadSize = 65536;
2862                                 for (j=0;j<mpqOpenArc->lpBlockTable[i].dwCompressedSize;j+=65536) {
2863                                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[i].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[i]) + j, FILE_BEGIN);
2864                                         SFSetFilePointer(hFile, MakeUInt64(lpBlockTable[i-nBlkOffset].dwFileOffset, lpdwFileOffsetsHigh[i-nBlkOffset]) + j, FILE_BEGIN);
2865                                         if (j+65536>mpqOpenArc->lpBlockTable[i].dwCompressedSize) ReadSize = mpqOpenArc->lpBlockTable[i].dwCompressedSize-j;
2866                                         if (ReadFile(mpqOpenArc->hFile,buffer,ReadSize,&tsz,0)==0) {
2867                                                 SFFree(lpBlockTable);
2868                                                 SFFree(lpHashTable);
2869                                                 SFFree(buffer);
2870                                                 CloseHandle(hFile);
2871                                                 DeleteFile(lpFileName);
2872                                                 return FALSE;
2873                                         }
2874                                         if (WriteFile(hFile,buffer,ReadSize,&tsz,0)==0) {
2875                                                 SFFree(lpBlockTable);
2876                                                 SFFree(lpHashTable);
2877                                                 SFFree(buffer);
2878                                                 CloseHandle(hFile);
2879                                                 DeleteFile(lpFileName);
2880                                                 return FALSE;
2881                                         }
2882                                 }
2883                         }
2884                 }
2885                 else {
2886                         for (j=0;j<mpqOpenArc->MpqHeader.dwHashTableSize;j++) {
2887                                 if ((lpHashTable[j].dwBlockTableIndex&0xFFFFFFFE)!=0xFFFFFFFE)
2888                                         if (lpHashTable[j].dwBlockTableIndex>(i-nBlkOffset)) lpHashTable[j].dwBlockTableIndex--;
2889                         }
2890                         nBlkOffset++;
2891                 }
2892         }
2893         mpqOpenArc->MpqHeader.dwBlockTableSize -= nBlkOffset;
2894         SplitUInt64(mpqOpenArc->MpqHeader.dwHashTableOffset, mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh, qwLastOffset);
2895         qwLastOffset += mpqOpenArc->MpqHeader.dwHashTableSize * sizeof(HASHTABLEENTRY);
2896         SplitUInt64(mpqOpenArc->MpqHeader.dwBlockTableOffset, mpqOpenArc->MpqHeader_Ex.wBlockTableOffsetHigh, qwLastOffset);
2897         qwLastOffset += mpqOpenArc->MpqHeader.dwBlockTableSize * sizeof(BLOCKTABLEENTRY);
2898         if (mpqOpenArc->MpqHeader_Ex.qwExtendedBlockOffsetTable) {
2899                 mpqOpenArc->MpqHeader_Ex.qwExtendedBlockOffsetTable = qwLastOffset;
2900                 qwLastOffset += mpqOpenArc->MpqHeader.dwBlockTableSize * sizeof(UInt16);
2901         }
2902         mpqOpenArc->MpqHeader.dwMPQSize = (UInt32)qwLastOffset;
2903         SFFree(mpqOpenArc->lpHashTable);
2904         mpqOpenArc->lpHashTable = lpHashTable;
2905         if(mpqOpenArc->lpBlockTable!=0) {
2906                 SFFree(mpqOpenArc->lpBlockTable);
2907                 mpqOpenArc->lpBlockTable = (BLOCKTABLEENTRY *)SFAlloc(mpqOpenArc->MpqHeader.dwBlockTableSize * (sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)));
2908                 mpqOpenArc->lpFileOffsetsHigh = (UInt16 *)(mpqOpenArc->lpBlockTable + mpqOpenArc->MpqHeader.dwBlockTableSize);
2909                 if (mpqOpenArc->lpBlockTable==0) {
2910                         mpqOpenArc->lpBlockTable = lpBlockTable;
2911                         mpqOpenArc->lpFileOffsetsHigh = lpdwFileOffsetsHigh;
2912                 }
2913                 else {
2914                         memcpy(mpqOpenArc->lpBlockTable, lpBlockTable, mpqOpenArc->MpqHeader.dwBlockTableSize * (sizeof(BLOCKTABLEENTRY) + sizeof(UInt16)));
2915                         SFFree(lpBlockTable);
2916                 }
2917         }
2918         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + qwLastOffset, FILE_BEGIN);
2919         SetEndOfFile(mpqOpenArc->hFile);
2920         MpqWriteHeaders(mpqOpenArc);
2921         mpqOpenArc->MpqHeader.dwHeaderSize = mpqOpenArc->dwHeaderSize;
2922         qwLastOffset = mpqOpenArc->dwHeaderSize;
2923         ReadSize = 65536;
2924         for (i=qwLastOffset;i<MakeUInt64(mpqOpenArc->MpqHeader.dwHashTableOffset, mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh);i+=65536) {
2925                 SFSetFilePointer(mpqOpenArc->hFile,mpqOpenArc->qwMPQStart+i,FILE_BEGIN);
2926                 SFSetFilePointer(hFile,i,FILE_BEGIN);
2927                 if (i+65536>MakeUInt64(mpqOpenArc->MpqHeader.dwHashTableOffset, mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh)) ReadSize = MakeUInt64(mpqOpenArc->MpqHeader.dwHashTableOffset, mpqOpenArc->MpqHeader_Ex.wHashTableOffsetHigh)-i;
2928                 ReadFile(hFile,buffer,ReadSize,&tsz,0);
2929                 WriteFile(mpqOpenArc->hFile,buffer,ReadSize,&tsz,0);
2930         }
2931         SFFree(buffer);
2932         CloseHandle(hFile);
2933         DeleteFile(lpFileName);
2934         WriteHashTable(mpqOpenArc);
2935         WriteBlockTable(mpqOpenArc);
2936         return TRUE;
2939 BOOL SFMPQAPI WINAPI MpqSetFileLocale(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID nOldLocale, LCID nNewLocale)
2941         if (!hMPQ || !lpFileName) {
2942                 SetLastError(ERROR_INVALID_PARAMETER);
2943                 return FALSE;
2944         }
2946         if (!(((MPQARCHIVE *)hMPQ)->dwFlags&SFILE_OPEN_ALLOW_WRITE)) {
2947                 SetLastError(ERROR_ACCESS_DENIED);
2948                 return FALSE;
2949         }
2950         if (nOldLocale==nNewLocale) return FALSE;
2951         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
2952         HASHTABLEENTRY *hashEntry = (HASHTABLEENTRY *)GetHashTableEntry(hMPQ,lpFileName,nOldLocale);
2953         if (hashEntry==0) return FALSE;
2954         if (hashEntry->lcLocale!=nOldLocale) return FALSE;
2955         hashEntry->lcLocale = nNewLocale;
2957         WriteHashTable(mpqOpenArc);
2958         LCID dwOldLocale=LocaleID;
2959         LocaleID=nOldLocale;
2960         RemoveFromInternalListing(hMPQ,lpFileName);
2962         LocaleID=nNewLocale;
2963         AddToInternalListing(hMPQ,lpFileName);
2964         LocaleID=dwOldLocale;
2965         return TRUE;
2968 BOOL SFMPQAPI WINAPI MpqCreateArchiveVersion(WORD wVersion)
2970         if (wVersion > 1) return FALSE;
2972         wCreationVersion = wVersion;
2973         return TRUE;
2976 DWORD SFMPQAPI WINAPI SFileFindMpqHeader(HANDLE hFile)
2978         if (hFile == INVALID_HANDLE_VALUE) {
2979                 SetLastError(ERROR_INVALID_PARAMETER);
2980                 return 0xFFFFFFFF;
2981         }
2983         DWORD dwHandleType = GetHandleType((MPQHANDLE)hFile);
2984         if (dwHandleType == SFILE_TYPE_FILE) {
2985                 MPQFILE *mpqOpenFile = (MPQFILE *)hFile;
2986                 if (mpqOpenFile->hFile == INVALID_HANDLE_VALUE) {
2987                         return mpqOpenFile->lpParentArc->qwMPQStart;
2988                 }
2989                 else {
2990                         hFile = mpqOpenFile->hFile;
2991                 }
2992         }
2993         else if (dwHandleType == SFILE_TYPE_MPQ) {
2994                 return ((MPQARCHIVE *)hFile)->qwMPQStart;
2995         }
2997         UInt64 FileLen = SFGetFileSize(hFile);
2998         if (FileLen >= 0xFFFFFFFF) FileLen = 0xFFFFFFFE;
2999         char pbuf[sizeof(MPQHEADER)];
3000         DWORD tsz;
3001         for (DWORD i=0;i<FileLen;i+=512)
3002         {
3003                 SFSetFilePointer(hFile,i,FILE_BEGIN);
3004                 if (ReadFile(hFile,pbuf,sizeof(MPQHEADER),&tsz,0)==0) return 0xFFFFFFFF;
3005                 if (tsz<sizeof(MPQHEADER)) return 0xFFFFFFFF;
3006                 if (memcmp(pbuf,ID_MPQ,4)==0 || memcmp(pbuf,ID_BN3,4)==0)
3007                 {
3008                         // Storm no longer does this, so mpq api shouldn't either
3009                         /*FileLen -= i;
3010                         if (memcmp(pbuf+8,&FileLen,4)==0)
3011                                 return i;
3012                         else
3013                                 FileLen += i;*/
3014                         return i;
3015                 }
3016         }
3017         return 0xFFFFFFFF;
3020 UInt64 WINAPI SFileFindMpqHeaderEx(HANDLE hFile, UInt64 qwStart, UInt64 qwLength)
3022         if (hFile == INVALID_HANDLE_VALUE) {
3023                 SetLastError(ERROR_INVALID_PARAMETER);
3024                 return (UInt64)-1;
3025         }
3026         UInt64 FileLen = SFGetFileSize(hFile);
3027         if (FileLen == (UInt64)-1 || qwStart >= FileLen || qwStart + qwLength > FileLen) return (UInt64)-1;
3028         if (qwLength == 0) qwLength = FileLen - qwStart;
3029         char pbuf[sizeof(MPQHEADER)];
3030         DWORD tsz;
3031         for (UInt64 i=qwStart;i<qwStart+qwLength && i<FileLen;i+=512)
3032         {
3033                 SFSetFilePointer(hFile,i,FILE_BEGIN);
3034                 if (ReadFile(hFile,pbuf,sizeof(MPQHEADER),&tsz,0)==0) return (UInt64)-1;
3035                 if (i+tsz>qwStart+qwLength) tsz = (qwStart+qwLength)-i;
3036                 if (tsz<sizeof(MPQHEADER)) return (UInt64)-1;
3037                 if (memcmp(pbuf,ID_MPQ,4)==0 || memcmp(pbuf,ID_BN3,4)==0)
3038                 {
3039                         // Storm no longer does this, so mpq api shouldn't either
3040                         /*FileLen -= i;
3041                         if (memcmp(pbuf+8,&FileLen,4)==0)
3042                                 return i;
3043                         else
3044                                 FileLen += i;*/
3045                         return i;
3046                 }
3047         }
3048         return (UInt64)-1;
3051 DWORD GetFullPath(LPCSTR lpFileName, char *lpBuffer, DWORD dwBufferLength)
3053         if (!lpFileName) return (DWORD)-1;
3054         DWORD slen = strlen(lpFileName);
3055         if (memcmp(lpFileName+1,":\\",2)==0) {
3056                 if (slen+1>dwBufferLength) return slen+1;
3057                 memcpy(lpBuffer,lpFileName,slen+1);
3058         }
3059 #ifdef _WIN32
3060         else if (lpFileName[0]=='\\') {
3061 #else
3062         else if (lpFileName[0]=='/') {
3063 #endif
3064                 if (slen+3>dwBufferLength) return slen+3;
3065                 memcpy(lpBuffer,StormBasePath,2);
3066                 memcpy(lpBuffer+2,lpFileName,slen+1);
3067         }
3068         else {
3069                 DWORD sbslen = strlen(StormBasePath);
3070                 if (sbslen+slen+1>dwBufferLength) return sbslen+slen+1;
3071                 memcpy(lpBuffer,StormBasePath,sbslen);
3072                 memcpy(lpBuffer+sbslen,lpFileName,slen);
3073                 lpBuffer[sbslen+slen]=0;
3074         }
3075         return 0;
3078 MPQHANDLE GetHashTableEntry(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID FileLocale)
3080         if (!hMPQ || !lpFileName) return 0;
3081         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
3082         DWORD dwTablePos = HashString(lpFileName,HASH_POSITION) % mpqOpenArc->MpqHeader.dwHashTableSize;
3083         DWORD dwNameHashA = HashString(lpFileName,HASH_NAME_A);
3084         DWORD dwNameHashB = HashString(lpFileName,HASH_NAME_B);
3085         StartTableSearch:
3086         DWORD i=dwTablePos;
3087         do
3088         {
3089                 if (mpqOpenArc->lpHashTable[i].dwBlockTableIndex==0xFFFFFFFF)
3090                 {
3091                         break;
3092                 }
3093                 else if (mpqOpenArc->lpHashTable[i].dwNameHashA==dwNameHashA && mpqOpenArc->lpHashTable[i].dwNameHashB==dwNameHashB && mpqOpenArc->lpHashTable[i].lcLocale==FileLocale && mpqOpenArc->lpHashTable[i].dwBlockTableIndex!=0xFFFFFFFE)
3094                 {
3095                         return (MPQHANDLE)&mpqOpenArc->lpHashTable[i];
3096                 }
3097                 i = (i + 1) % mpqOpenArc->MpqHeader.dwHashTableSize;
3098         } while (i!=dwTablePos);
3099         if (FileLocale!=0) {FileLocale=0;goto StartTableSearch;}
3100         return 0;
3103 MPQHANDLE GetHashTableEntryOfHash(MPQHANDLE hMPQ, DWORD dwTablePos, DWORD dwNameHashA, DWORD dwNameHashB, LCID FileLocale)
3105         if (!hMPQ) return 0;
3106         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
3107         StartTableSearch:
3108         DWORD i=dwTablePos;
3109         do
3110         {
3111                 if (mpqOpenArc->lpHashTable[i].dwBlockTableIndex==0xFFFFFFFF)
3112                 {
3113                         break;
3114                 }
3115                 else if (mpqOpenArc->lpHashTable[i].dwNameHashA==dwNameHashA && mpqOpenArc->lpHashTable[i].dwNameHashB==dwNameHashB && mpqOpenArc->lpHashTable[i].lcLocale==FileLocale && mpqOpenArc->lpHashTable[i].dwBlockTableIndex!=0xFFFFFFFE)
3116                 {
3117                         return (MPQHANDLE)&mpqOpenArc->lpHashTable[i];
3118                 }
3119                 i = (i + 1) % mpqOpenArc->MpqHeader.dwHashTableSize;
3120         } while (i!=dwTablePos);
3121         if (FileLocale!=0) {FileLocale=0;goto StartTableSearch;}
3122         return 0;
3125 MPQHANDLE GetFreeHashTableEntry(MPQHANDLE hMPQ, LPCSTR lpFileName, LCID FileLocale, BOOL ReturnExisting)
3127         if (!hMPQ || !lpFileName) return 0;
3128         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
3129         DWORD dwTablePos = HashString(lpFileName,HASH_POSITION) % mpqOpenArc->MpqHeader.dwHashTableSize;
3130         DWORD dwNameHashA = HashString(lpFileName,HASH_NAME_A);
3131         DWORD dwNameHashB = HashString(lpFileName,HASH_NAME_B);
3132         DWORD i=dwTablePos, nFirstFree = 0xFFFFFFFF;
3133         do
3134         {
3135                 if ((mpqOpenArc->lpHashTable[i].dwBlockTableIndex&0xFFFFFFFE)==0xFFFFFFFE && (nFirstFree == 0xFFFFFFFF || mpqOpenArc->lpHashTable[i].dwBlockTableIndex == 0xFFFFFFFF))
3136                 {
3137                         if (mpqOpenArc->lpHashTable[i].dwBlockTableIndex == 0xFFFFFFFF)
3138                         {
3139                                 if (nFirstFree == 0xFFFFFFFF)
3140                                         return (MPQHANDLE)&mpqOpenArc->lpHashTable[i];
3141                                 else
3142                                         return (MPQHANDLE)&mpqOpenArc->lpHashTable[nFirstFree];
3143                         }
3144                         else nFirstFree = i;
3145                 }
3146                 else if (mpqOpenArc->lpHashTable[i].dwNameHashA==dwNameHashA && mpqOpenArc->lpHashTable[i].dwNameHashB==dwNameHashB && mpqOpenArc->lpHashTable[i].lcLocale==FileLocale)
3147                 {
3148                         if (ReturnExisting!=FALSE)
3149                                 return (MPQHANDLE)&mpqOpenArc->lpHashTable[i];
3150                         else {
3151                                 SetLastError(MPQ_ERROR_ALREADY_EXISTS);
3152                                 return 0;
3153                         }
3154                 }
3155                 i = (i + 1) % mpqOpenArc->MpqHeader.dwHashTableSize;
3156         } while (i!=dwTablePos);
3157         SetLastError(MPQ_ERROR_HASH_TABLE_FULL);
3158         return 0;
3161 BOOL SearchOpenArchives(LPCSTR lpFileName, MPQHANDLE *hMPQ, MPQHANDLE *hFile)
3163         MPQHANDLE hnMPQ,hnFile,hndMPQ = 0,hndFile = 0;
3165         DWORD dwTablePos = HashString(lpFileName,HASH_POSITION);
3166         DWORD dwNameHashA = HashString(lpFileName,HASH_NAME_A);
3167         DWORD dwNameHashB = HashString(lpFileName,HASH_NAME_B);
3169         for (DWORD i=0;i<dwOpenMpqCount;i++) {
3170                 hnMPQ = (MPQHANDLE)lpOpenMpq[i];
3171                 hnFile = GetHashTableEntryOfHash(hnMPQ,dwTablePos % lpOpenMpq[i]->MpqHeader.dwHashTableSize,dwNameHashA,dwNameHashB,LocaleID);
3173                 if (hnFile!=0) {
3174                         if (((HASHTABLEENTRY *)hnFile)->lcLocale == LocaleID) {
3175                                 *hMPQ = hnMPQ;
3176                                 *hFile = hnFile;
3177                                 return TRUE;
3178                         }
3179                         else if (hndMPQ == 0 || hndFile == 0) {
3180                                 hndMPQ = hnMPQ;
3181                                 hndFile = hnFile;
3182                         }
3183                 }
3184         }
3186         if (hndMPQ != 0 && hndFile != 0) {
3187                 *hMPQ = hndMPQ;
3188                 *hFile = hndFile;
3189                 return TRUE;
3190         }
3192         *hMPQ = 0;
3193         *hFile = 0;
3194         return FALSE;
3197 void SortOpenArchivesByPriority()
3199         MPQARCHIVE *hMPQ1,*hMPQ2;
3200         for (DWORD i=1;i<dwOpenMpqCount;i++) {
3201                 do {
3202                         hMPQ1 = lpOpenMpq[i-1];
3203                         hMPQ2 = lpOpenMpq[i];
3204                         if (hMPQ2->dwPriority > hMPQ1->dwPriority) {
3205                                 lpOpenMpq[i-1] = hMPQ2;
3206                                 lpOpenMpq[i] = hMPQ1;
3207                                 i--;
3208                         }
3209                 } while (hMPQ2->dwPriority > hMPQ1->dwPriority && i>0);
3210         }
3213 DWORD GetHandleType(MPQHANDLE hFile)
3215         DWORD i;
3216         for (i=0;i<dwOpenFileCount;i++) {
3217                 if ((MPQHANDLE)lpOpenFile[i]==hFile) return SFILE_TYPE_FILE;
3218         }
3219         for (i=0;i<dwOpenMpqCount;i++) {
3220                 if ((MPQHANDLE)lpOpenMpq[i]==hFile) return SFILE_TYPE_MPQ;
3221         }
3222         return 0;
3225 BOOL AddToInternalListing(MPQHANDLE hMPQ, LPCSTR lpFileName)
3227         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
3228         if ((mpqOpenArc->dwFlags & MOAU_MAINTAIN_LISTFILE)==MOAU_MAINTAIN_LISTFILE && stricmp(lpFileName,INTERNAL_LISTFILE)!=0)
3229         {
3230                 MPQHANDLE hlFile;char *buffer=0;DWORD fsz,lsz;
3231                 lsz = strlen(lpFileName);
3232                 if (SFileOpenFileEx(hMPQ,INTERNAL_LISTFILE,0,&hlFile)!=0) {
3233                         if (((MPQFILE *)hlFile)->lpHashEntry->lcLocale==LocaleID) {
3234                                 fsz = SFileGetFileSize(hlFile,0);
3235                                 if (fsz==0) {
3236                                         SFileCloseFile(hlFile);
3237                                         goto AddFileName;
3238                                 }
3239                                 buffer = (char *)SFAlloc(fsz+lsz+2);
3240                                 if (buffer==0) {
3241                                         SFileCloseFile(hlFile);
3242                                         return FALSE;
3243                                 }
3244                                 if (SFileReadFile(hlFile,buffer,fsz,0,0)==0) {
3245                                         SFFree(buffer);
3246                                         buffer = 0;
3247                                 }
3248                                 buffer[fsz]=0;
3249                         }
3250                         SFileCloseFile(hlFile);
3251                 }
3252                 AddFileName:
3253                 if (buffer==0) {
3254                         fsz = 0;
3255                         buffer = (char *)SFAlloc(lsz+2);
3256                         buffer[0]=0;
3257                 }
3258                 else {
3259                         char *buffercopy = strlwr(strdup(buffer));
3260                         char *lwrFileName = strlwr(strdup(lpFileName));
3261                         char *subbuffer=buffer;
3262                         while ((subbuffer=strstr(subbuffer,lpFileName))!=NULL && subbuffer[0]!=0) {
3263                                 if (subbuffer==buffer && subbuffer[lsz]==0) {
3264                                         SFFree(lwrFileName);
3265                                         SFFree(buffercopy);
3266                                         SFFree(buffer);
3267                                         return TRUE;
3268                                 }
3269                                 else if (subbuffer==buffer && (subbuffer[lsz]=='\n' || subbuffer[lsz]=='\r')) {
3270                                         SFFree(lwrFileName);
3271                                         SFFree(buffercopy);
3272                                         SFFree(buffer);
3273                                         return TRUE;
3274                                 }
3275                                 else if (((subbuffer-1)[0]=='\n' || (subbuffer-1)[0]=='\r') && subbuffer[lsz]==0) {
3276                                         SFFree(lwrFileName);
3277                                         SFFree(buffercopy);
3278                                         SFFree(buffer);
3279                                         return TRUE;
3280                                 }
3281                                 else if (((subbuffer-1)[0]=='\n' || (subbuffer-1)[0]=='\r') && (subbuffer[lsz]=='\n' || subbuffer[lsz]=='\r')) {
3282                                         SFFree(lwrFileName);
3283                                         SFFree(buffercopy);
3284                                         SFFree(buffer);
3285                                         return TRUE;
3286                                 }
3287                                 subbuffer++;
3288                         }
3289                         SFFree(lwrFileName);
3290                         SFFree(buffercopy);
3291                 }
3292                 memcpy(buffer+fsz,lpFileName,lsz);
3293                 memcpy(buffer+fsz+lsz,"\r\n",2);
3294                 //LCID dwOldLocale=LocaleID;
3295                 //LocaleID=0;
3296                 MpqAddFileFromBuffer(hMPQ,buffer,fsz+lsz+2,INTERNAL_LISTFILE,MAFA_COMPRESS|MAFA_ENCRYPT|MAFA_MODCRYPTKEY|MAFA_REPLACE_EXISTING);
3297                 //LocaleID=dwOldLocale;
3298                 SFFree(buffer);
3299         }
3300         return TRUE;
3303 BOOL RemoveFromInternalListing(MPQHANDLE hMPQ, LPCSTR lpFileName)
3305         MPQARCHIVE *mpqOpenArc = (MPQARCHIVE *)hMPQ;
3306         if ((mpqOpenArc->dwFlags & MOAU_MAINTAIN_LISTFILE)==MOAU_MAINTAIN_LISTFILE && stricmp(lpFileName,INTERNAL_LISTFILE)!=0)
3307         {
3308                 MPQHANDLE hlFile;char *buffer=0;DWORD fsz,lsz;
3309                 lsz = strlen(lpFileName);
3310                 if (SFileOpenFileEx(hMPQ,INTERNAL_LISTFILE,0,&hlFile)!=0) {
3311                         if (((MPQFILE *)hlFile)->lpHashEntry->lcLocale==LocaleID) {
3312                                 fsz = SFileGetFileSize(hlFile,0);
3313                                 if (fsz==0) {
3314                                         SFileCloseFile(hlFile);
3316                                         return FALSE;
3317                                 }
3319                                 buffer = (char *)SFAlloc(fsz+1);
3320                                 if (buffer==0) {
3321                                         SFileCloseFile(hlFile);
3322                                         return FALSE;
3323                                 }
3324                                 buffer[fsz] = 0;
3325                                 if (SFileReadFile(hlFile,buffer,fsz,0,0)==0) {
3326                                         SFFree(buffer);
3327                                         buffer = 0;
3328                                 }
3329                         }
3330                         SFileCloseFile(hlFile);
3331                 }
3332                 if (buffer==0) {
3333                         return FALSE;
3334                 }
3335                 else {
3336                         buffer[fsz]=0;
3337                         char *buffercopy = strlwr(strdup(buffer));
3338                         char *lwrFileName = strlwr(strdup(lpFileName));
3339                         char *subbuffer=buffer;
3341                         while ((subbuffer=strstr(subbuffer,lpFileName))!=NULL && subbuffer[0]!=0) {
3342                                 if (subbuffer==buffer && subbuffer[lsz]==0) {
3343                                         SFFree(lwrFileName);
3344                                         SFFree(buffercopy);
3345                                         //LCID dwOldLocale=LocaleID;
3346                                         //LocaleID=0;
3347                                         MpqAddFileFromBuffer(hMPQ,(LPVOID)"\x00",0,INTERNAL_LISTFILE,MAFA_COMPRESS|MAFA_ENCRYPT|MAFA_MODCRYPTKEY|MAFA_REPLACE_EXISTING);
3348                                         //LocaleID=dwOldLocale;
3349                                         SFFree(buffer);
3350                                         return TRUE;
3351                                 }
3352                                 else if (subbuffer==buffer && (subbuffer[lsz]=='\n' || subbuffer[lsz]=='\r')) {
3353                                         SFFree(lwrFileName);
3354                                         SFFree(buffercopy);
3355                                         if (subbuffer[lsz+1]=='\n' || subbuffer[lsz+1]=='\r') lsz++;
3356                                         memcpy(subbuffer,subbuffer+lsz+1,strlen(subbuffer+lsz+1));
3357                                         //LCID dwOldLocale=LocaleID;
3358                                         //LocaleID=0;
3359                                         MpqAddFileFromBuffer(hMPQ,buffer,fsz-lsz-1,INTERNAL_LISTFILE,MAFA_COMPRESS|MAFA_ENCRYPT|MAFA_MODCRYPTKEY|MAFA_REPLACE_EXISTING);
3360                                         //LocaleID=dwOldLocale;
3361                                         SFFree(buffer);
3362                                         return TRUE;
3363                                 }
3364                                 else if (((subbuffer-1)[0]=='\n' || (subbuffer-1)[0]=='\r') && subbuffer[lsz]==0) {
3365                                         SFFree(lwrFileName);
3366                                         SFFree(buffercopy);
3367                                         //LCID dwOldLocale=LocaleID;
3368                                         //LocaleID=0;
3369                                         MpqAddFileFromBuffer(hMPQ,buffer,fsz-lsz,INTERNAL_LISTFILE,MAFA_COMPRESS|MAFA_ENCRYPT|MAFA_MODCRYPTKEY|MAFA_REPLACE_EXISTING);
3370                                         //LocaleID=dwOldLocale;
3371                                         SFFree(buffer);
3372                                         return TRUE;
3373                                 }
3374                                 else if (((subbuffer-1)[0]=='\n' || (subbuffer-1)[0]=='\r') && (subbuffer[lsz]=='\n' || subbuffer[lsz]=='\r')) {
3375                                         SFFree(lwrFileName);
3376                                         SFFree(buffercopy);
3377                                         if ((subbuffer-2)[0]=='\n' || (subbuffer-2)[0]=='\r') {subbuffer--;lsz++;}
3378                                         memcpy(subbuffer-1,subbuffer+lsz,strlen(subbuffer+lsz));
3379                                         //LCID dwOldLocale=LocaleID;
3380                                         //LocaleID=0;
3381                                         MpqAddFileFromBuffer(hMPQ,buffer,fsz-lsz-1,INTERNAL_LISTFILE,MAFA_COMPRESS|MAFA_ENCRYPT|MAFA_MODCRYPTKEY|MAFA_REPLACE_EXISTING);
3382                                         //LocaleID=dwOldLocale;
3383                                         SFFree(buffer);
3384                                         return TRUE;
3385                                 }
3386                                 subbuffer++;
3387                         }
3388                         SFFree(lwrFileName);
3389                         SFFree(buffercopy);
3390                 }
3391                 SFFree(buffer);
3392         }
3393         return TRUE;
3396 DWORD DetectFileSeedEx(MPQARCHIVE * mpqOpenArc, HASHTABLEENTRY * lpHashEntry, LPCSTR * lplpFileName)
3398         if (mpqOpenArc==0 || lpHashEntry==0) return 0;
3399         DWORD dwCryptKey=0;
3400         LPCSTR lpFileName = *lplpFileName;
3401         DWORD dwBlockIndex = lpHashEntry->dwBlockTableIndex;
3402         if (lpFileName)
3403         {
3404                 dwCryptKey = HashString(lpFileName,HASH_KEY);
3405                 if (mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags&MAFA_MODCRYPTKEY)
3406                         dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize;
3407         }
3408         else
3409         {
3410                 DWORD dwNameHashA = HashString(INTERNAL_LISTFILE,HASH_NAME_A);
3411                 DWORD dwNameHashB = HashString(INTERNAL_LISTFILE,HASH_NAME_B);
3412                 if (lpHashEntry->dwNameHashA==dwNameHashA && lpHashEntry->dwNameHashB==dwNameHashB) {
3413                         dwCryptKey = HashString(INTERNAL_LISTFILE,HASH_KEY);
3414                         if (mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags&MAFA_MODCRYPTKEY)
3415                                 dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize;
3416                         *lplpFileName = (char *)SFAlloc(strlen(INTERNAL_LISTFILE)+1);
3417                         if (*lplpFileName)
3418                                 strcpy((LPSTR)*lplpFileName,INTERNAL_LISTFILE);
3419                 }
3420                 else {
3421                         HANDLE hlFile,hMPQ=(HANDLE)mpqOpenArc;
3422                         DWORD fsz;
3423                         char *listbuffer;
3424                         LCID lcOldLocale = LocaleID;
3425                         for (DWORD lcn=0;lcn<nLocales;lcn++) {
3426                                 LocaleID = availLocales[lcn];
3427                                 if (SFileOpenFileEx(hMPQ,INTERNAL_LISTFILE,0,&hlFile)!=0) {
3428                                         if (((MPQFILE *)hlFile)->lpHashEntry->lcLocale==0 && LocaleID!=0) {
3429                                                 SFileCloseFile(hlFile);
3430                                                 continue;
3431                                         }
3432                                         fsz = SFileGetFileSize(hlFile,0);
3433                                         if (fsz>0) {
3434                                                 listbuffer = (char *)SFAlloc(fsz+1);
3435                                                 if (listbuffer==0) {
3436                                                         SFileCloseFile(hlFile);
3437                                                         continue;
3438                                                 }
3439                                                 if (SFileReadFile(hlFile,listbuffer,fsz,0,0)==0) {
3440                                                         SFFree(listbuffer);
3441                                                         listbuffer = 0;
3442                                                 }
3443                                         }
3444                                         SFileCloseFile(hlFile);
3445                                         if (listbuffer!=0) {
3446                                                 char *listline;
3447                                                 for (listline=listbuffer;listline!=0;listline=nextline(listline)) {
3448                                                         if (listline[0]==0) break;
3449                                                         DWORD lnlen=strlnlen(listline);
3450                                                         char prevchar=listline[lnlen];
3451                                                         listline[lnlen]=0;
3452                                                         dwNameHashA = HashString(listline,HASH_NAME_A);
3453                                                         dwNameHashB = HashString(listline,HASH_NAME_B);
3454                                                         if (lpHashEntry->dwNameHashA==dwNameHashA && lpHashEntry->dwNameHashB==dwNameHashB) {
3455                                                                 dwCryptKey = HashString(listline,HASH_KEY);
3456                                                                 if (mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags&MAFA_MODCRYPTKEY)
3457                                                                         dwCryptKey = (dwCryptKey + mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset) ^ mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize;
3458                                                                 *lplpFileName = (char *)SFAlloc(strlen(listline)+1);
3459                                                                 if (*lplpFileName)
3460                                                                         strcpy((LPSTR)*lplpFileName,listline);
3461                                                                 break;
3462                                                         }
3463                                                         listline[lnlen]=prevchar;
3464                                                 }
3465                                                 if (listline!=0) {
3466                                                         if (listline[0]!=0) {
3467                                                                 SFFree(listbuffer);
3468                                                                 break;
3469                                                         }
3470                                                 }
3471                                                 SFFree(listbuffer);
3472                                         }
3473                                 }
3474                         }
3475                         LocaleID = lcOldLocale;
3476                 }
3477                 if (dwCryptKey==0 && (mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags&MAFA_COMPRESS || mpqOpenArc->lpBlockTable[dwBlockIndex].dwFlags&MAFA_COMPRESS2))
3478                 {
3479                         DWORD HeaderLength=0,tsz;
3480                         if (memcmp(&mpqOpenArc->MpqHeader.dwMPQID,ID_BN3,4)==0)
3481                         {
3482                                 SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[dwBlockIndex]), FILE_BEGIN);
3483                                 ReadFile(mpqOpenArc->hFile,&HeaderLength,4,&tsz,0);
3484                         }
3485                         DWORD blockSize = 512 << mpqOpenArc->MpqHeader.wBlockSize;
3486                         DWORD TotalBlocks = mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize / blockSize;
3487                         if(mpqOpenArc->lpBlockTable[dwBlockIndex].dwFullSize % blockSize)
3488                                 TotalBlocks++;
3489                         DWORD *dwBlockPtrTable = (DWORD *)SFAlloc((TotalBlocks+1)*4);
3490                         if (dwBlockPtrTable==0)
3491                                 return 0;
3492                         SFSetFilePointer(mpqOpenArc->hFile, mpqOpenArc->qwMPQStart + MakeUInt64(mpqOpenArc->lpBlockTable[dwBlockIndex].dwFileOffset, mpqOpenArc->lpFileOffsetsHigh[dwBlockIndex]) + HeaderLength, FILE_BEGIN);
3493                         ReadFile(mpqOpenArc->hFile,dwBlockPtrTable,(TotalBlocks+1)*4,&tsz,0);
3494                         dwCryptKey = DetectFileSeed(dwBlockPtrTable,(TotalBlocks+1)*4,blockSize);
3496                         SFFree(dwBlockPtrTable);
3497                 }
3498         }
3499         return dwCryptKey;
3502 long SFMPQAPI __inline SFMpqCompareVersion()
3504         SFMPQVERSION ExeVersion = SFMPQ_CURRENT_VERSION;
3505         SFMPQVERSION DllVersion = SFMpqGetVersion();
3506         if (DllVersion.Major>ExeVersion.Major) return 1;
3507         else if (DllVersion.Major<ExeVersion.Major) return -1;
3508         if (DllVersion.Minor>ExeVersion.Minor) return 1;
3509         else if (DllVersion.Minor<ExeVersion.Minor) return -1;
3510         if (DllVersion.Revision>ExeVersion.Revision) return 1;
3511         else if (DllVersion.Revision<ExeVersion.Revision) return -1;
3512         if (DllVersion.Subrevision>ExeVersion.Subrevision) return 1;
3513         else if (DllVersion.Subrevision<ExeVersion.Subrevision) return -1;
3514         return 0;
3517 void LoadStorm()
3519 #ifdef _WIN32
3520         if (hStorm!=0) return;
3521         hStorm = LoadLibrary(Storm_dll);
3522         /*if (hStorm==0) {
3523                 HKEY hKey;
3524                 LPSTR lpDllPath;
3525                 DWORD dwStrLen;
3526                 if (RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Blizzard Entertainment\\Warcraft III",0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
3527                         if (RegOpenKeyEx(HKEY_USERS,".Default\\Software\\Blizzard Entertainment\\Warcraft III",0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
3528                                 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Blizzard Entertainment\\Warcraft III",0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
3529                                         return;
3530                 RegQueryValueEx(hKey,"InstallPath",0,0,0,&dwStrLen);
3531                 lpDllPath = (LPSTR)SFAlloc(dwStrLen+11);
3532                 if (lpDllPath) {
3533                         memset(lpDllPath,0,dwStrLen+11);
3534                         RegQueryValueEx(hKey,"InstallPath",0,0,(LPBYTE)lpDllPath,&dwStrLen);
3535                         LPSTR lpLastBSlash = strrchr(lpDllPath,'\\');
3536                         if (lpLastBSlash) {
3537                                 if (lpLastBSlash[1]!=0) lpLastBSlash[strlen(lpLastBSlash)]='\\';
3538                                 strcat(lpLastBSlash,Storm_dll);
3539                                 hStorm = LoadLibrary(lpDllPath);
3540                         }
3541                         SFFree(lpDllPath);
3542                 }
3543                 RegCloseKey(hKey);
3544         }*/
3545         if (hStorm==0) return;
3547         unsigned int wSCCOrdinal=0xAC30;
3548         wSCCOrdinal>>=4;
3549         wSCCOrdinal/=5;
3550         unsigned int wSCDcOrdinal=0xAC80;
3551         wSCDcOrdinal>>=4;
3552         wSCDcOrdinal/=5;
3554         stormSCompCompress = (funcSCompCompress)GetProcAddress(hStorm,(LPCSTR)wSCCOrdinal);
3555         stormSCompDecompress = (funcSCompDecompress)GetProcAddress(hStorm,(LPCSTR)wSCDcOrdinal);
3556 #endif
3559 void FreeStorm()
3561 #ifdef _WIN32
3562         if (hStorm==0) return;
3563         FreeLibrary(hStorm);
3564         hStorm = 0;
3566         stormSCompCompress = 0;
3567         stormSCompDecompress = 0;
3568 #endif