#include "GwArchive.h" #include "xentax.h" #include #include bool compareH(MftExpansion& a, MftExpansion b); char atex[4]={'A','T','E','X'}; char attx[4]={'A','T','T','X'}; char ffna[4]={'f','f','n','a'}; int CheckMajorType(unsigned char* pDecompressed) { if (memcmp(pDecompressed, ffna, 4) == 0){ return FFNA; } else if (memcmp(pDecompressed, atex, 4) == 0){ return ATEX; } else if (memcmp(pDecompressed, attx, 4) == 0){ return ATTX; } else { return NO_TYPE; } } unsigned char* GwArchive::GetFile(unsigned long fileHash, MftEntry* pMftEntry) { //Find the MftEntry with this hash stdext::hash_map::iterator itr=mFilesTable.find(fileHash); if (itr == mFilesTable.end()) { return NULL; } MftEntry& m = *itr->second; if (!m.b) { m.uncompressedSize = 0; return NULL; } unsigned char *Input = new unsigned char[m.storedFileSize]; unsigned char *Output = NULL; int OutSize = 0; FileSeek(m.Offset, 0); FileRead(Input, m.storedFileSize, 1); //If the file data is compressed if (m.a) { //If we don't have this file's type...get it if (itr->second->type == -1) { UnpackGWDat(Input, m.storedFileSize, Output, OutSize); itr->second->type=CheckMajorType(Output); delete [] Output; itr->second->uncompressedSize=OutSize; } Output = new unsigned char[m.storedFileSize]; memcpy(Output, Input, m.storedFileSize); //Remove our trailing footer from the size, the data can still be accessed though itr->second->compressedSize=m.storedFileSize-8; } //File is not stored compressed else { Output = new unsigned char[m.storedFileSize]; memcpy(Output, Input, m.storedFileSize); itr->second->type=CheckMajorType(Output); itr->second->uncompressedSize=m.storedFileSize; itr->second->compressedSize=m.storedFileSize; } delete [] Input; //Copies the file's MFT entry to our MFT buffer if (pMftEntry != NULL) { memcpy(pMftEntry, itr->second, sizeof(MftEntry)); } else { delete [] Output; return NULL; } return Output; } void GwArchive::FileSeek(__int64 offset, int origin) { LARGE_INTEGER i; i.QuadPart=offset; SetFilePointerEx(mpFileHandle,i,NULL,FILE_BEGIN); } void GwArchive::FileRead(void *buffer, int size, int count) { DWORD bread; int errcde = ReadFile(mpFileHandle,buffer,size*count,&bread,NULL); if (!errcde) { TCHAR text[2048]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),0,text,1024,NULL); printf("%s\n",text); } } int GwArchive::Close() { if (mpFileHandle != NULL) { CloseHandle(mpFileHandle); } mFilesTable.clear(); mMft.clear(); mMftX.clear(); return 0; } //Opens and reads the MFT of the archive int GwArchive::Open(const char* pArchiveFile) { mpFileHandle = CreateFile(pArchiveFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (mpFileHandle == INVALID_HANDLE_VALUE) { char text[2048]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),0,text,2048,NULL); printf("Error while opening \"%s\": %s", pArchiveFile, text); return 0; } FileRead(&mGwHeader,sizeof(mGwHeader),1); if (!(mGwHeader.ID[0]==0x33 && mGwHeader.ID[1]==0x41 && mGwHeader.ID[2]==0x4e && mGwHeader.ID[3]==0x1a)) { printf("The input file \"%s\"is not a Guild Wars datafile!", pArchiveFile); CloseHandle(mpFileHandle); return 0; } //read reserved MFT entries FileSeek(mGwHeader.MFTOffset,SEEK_SET); FileRead(&mMftHeader,sizeof(mMftHeader),1); for (int x = 0; x < 15; ++x) { MftEntry ME; FileRead(&ME,0x18,1); //ME.type = NOTREAD; ME.uncompressedSize = -1; ME.Hash = 0; mMft.push_back(ME); } //read Hashlist FileSeek(mMft[1].Offset,SEEK_SET); int mftxsize=mMft[1].storedFileSize/sizeof(MftExpansion); for (unsigned int x=0; x hpair; mFilesTable.insert(hpair(pMft->Hash, pMft)); } return (unsigned int)mMft.size(); } bool compareH(MftExpansion& a, MftExpansion b) { return a.FileOffset < b.FileOffset; }