#include "StdAfx.h" #define ATOI_KLUDGE #include "CPEFile.h" #include "CResFile.h" #include "GlobalFunc.h" CResFile::CResFile() { mBuffer = NULL; mPos = NULL; mIsBinaryMode = false; mIsSubPlugin = false; } /*virtual*/ CResFile::~CResFile() { Reset(); } BOOL CResFile::OpenResource(HINSTANCE inst, int name, int type) { return OpenResource(inst, MAKEINTATOM(name), type); } BOOL CResFile::OpenResource(HINSTANCE inst, const char* name, int type) { char buf[5], a; *((int *) &buf[0]) = type; a = buf[0]; buf[0] = buf[3]; buf[3] = a; a = buf[1]; buf[1] = buf[2]; buf[2] = a; buf[4] = 0; return OpenResource(inst, name, buf); } BOOL CResFile::OpenResource(HINSTANCE inst, int name, const char* type) { return OpenResource(inst, MAKEINTATOM(name), type); } BOOL CResFile::OpenResource(HINSTANCE inst, const char* name, const char* type) { HRSRC h = FindResource(inst, name, type); if (!h) { CString theNameString; if (((DWORD) name) < 0x10000) theNameString.Format("(%d)", ((DWORD) name) & 0xFFFF); else theNameString = name; CString theTypeString; if (((DWORD) type) < 0x10000) theTypeString.Format("(%d)", ((DWORD) type) & 0xFFFF); else theTypeString = type; CString str = "CResFile::OpenResource ERROR : " + theNameString + "," + theTypeString + " @" + GetModuleName(inst) + "\n"; OutputDebugString(str); return false; } return OpenResource(inst, h); } BOOL CResFile::OpenResource(HINSTANCE inst, HRSRC rsrc) { if (mBuffer) Reset(); mInstance = inst; mRsrc = rsrc; HANDLE theHandle = LoadResource(inst, rsrc); if (!theHandle) return false; void *theData = LockResource(theHandle); mRemaining = SizeofResource(inst, rsrc); mBuffer = (char *) malloc(mRemaining + 1); memcpy(mBuffer, theData, mRemaining); UnlockResource(theHandle); FreeResource(theHandle); SetupResource(); return true; } BOOL CResFile::OpenResource(const char* path, int name, int type) { CFile *file = new CFile; if (!file->Open(path, CFile::modeRead)) { delete file; return false; } char magic[2]; file->Read(magic, 2); file->Close(); delete file; if (memcmp(magic, "SP", 2) == 0) { SUBPLUGINFSENTRY entry; CFile *subPluginFile = OpenSubPluginFS(path, name, type, &entry); if (!subPluginFile) return false; mRemaining = entry.spfse_mC; mBuffer = (char *) malloc(mRemaining + 1); subPluginFile->Read(mBuffer, mRemaining); subPluginFile->Close(); delete subPluginFile; mIsSubPlugin = true; } else if (memcmp(magic, "MZ", 2) == 0) { CPEFile pe; if (!pe.Open(path, CFile::modeRead)) return false; char buf[8]; buf[0] = ((char *) &type)[3]; buf[1] = ((char *) &type)[2]; buf[2] = ((char *) &type)[1]; buf[3] = ((char *) &type)[0]; buf[4] = 0; CString path; #ifdef HACKS unsigned short langID = 1041; path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID); if (!pe.Seek(path)) { langID = 2057; path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID); if (!pe.Seek(path)) return false; } #else unsigned short langID = GetUserDefaultLangID(); path.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID); if (!pe.Seek(path)) return false; #endif mRemaining = pe.GetLength(); mBuffer = (char *) malloc(mRemaining + 1); pe.Read(mBuffer, mRemaining); pe.Close(); mIsSubPlugin = false; } SetupResource(); return true; } void CResFile::SetupResource() { mBuffer[mRemaining] = 0; mPos = mBuffer; if (*mPos == 1) { mPos++; mRemaining--; mIsBinaryMode = true; } else { mIsBinaryMode = false; } } void CResFile::Reset() { if (mBuffer) free(mBuffer); mBuffer = NULL; mPos = NULL; } BOOL CResFile::Token(char* outStr) { while (*mPos == ' ' || *mPos == '\t' || *mPos == '\n' || *mPos == '\r' || *mPos == ',' || *mPos == '(') { if (*mPos == '(') { while (*mPos != ')') mPos++; } mPos++; } if (*mPos == '\0') { #line 180 _ASSERT(false); return false; } char *p = outStr; int len = 0; while (*mPos != ' ' && *mPos != '\t' && *mPos != '\n' && *mPos != '\r' && *mPos != ',' && *mPos != '(' && *mPos != '\0') { *p = *mPos; p++; mPos++; len++; #line 192 _ASSERT(len != 256); } *p = 0; return true; } BOOL CResFile::operator>>(unsigned char& out) { #line 200 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 204 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((unsigned char *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; out = atoi(buf); } return true; } BOOL CResFile::operator>>(int& out) { #line 222 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 226 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((int *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; out = atoi(buf); } return true; } BOOL CResFile::operator>>(short& out) { #line 244 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 248 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((short *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; out = atoi(buf); } return true; } BOOL CResFile::operator>>(long& out) { #line 266 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 270 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((long *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; out = atol(buf); } return true; } BOOL CResFile::operator>>(unsigned int& out) { #line 288 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 292 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((unsigned int *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; sscanf(buf, "%u", &out); } return true; } BOOL CResFile::operator>>(unsigned long& out) { #line 310 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 314 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((unsigned long *) mPos); mPos += 4; } else { char buf[256]; if (!Token(buf)) return false; sscanf(buf, "%u", &out); } return true; } BOOL CResFile::operator>>(unsigned short& out) { #line 332 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 336 _ASSERT(mRemaining >= 4); mRemaining -= 4; out = *((unsigned short *) mPos); mPos += 4; } else { char buf[256]; unsigned int val; if (!Token(buf)) return false; sscanf(buf, "%u", &val); out = val; } return true; } BOOL CResFile::operator>>(POINT& outPoint) { #line 356 _ASSERT(mBuffer); if (mIsBinaryMode) { if (!(*this >> outPoint.y)) return false; if (!(*this >> outPoint.x)) return false; } else { if (!(*this >> outPoint.x)) return false; if (!(*this >> outPoint.y)) return false; } return true; } BOOL CResFile::operator>>(RECT& outRect) { #line 377 _ASSERT(mBuffer); if (mIsBinaryMode) { if (!(*this >> outRect.top)) return false; if (!(*this >> outRect.left)) return false; if (!(*this >> outRect.bottom)) return false; if (!(*this >> outRect.right)) return false; } else { if (!(*this >> outRect.left)) return false; if (!(*this >> outRect.top)) return false; if (!(*this >> outRect.right)) return false; if (!(*this >> outRect.bottom)) return false; } return true; } BOOL CResFile::operator>>(SIZE& outSize) { #line 406 _ASSERT(mBuffer); if (mIsBinaryMode) { if (!(*this >> outSize.cy)) return false; if (!(*this >> outSize.cx)) return false; } else { if (!(*this >> outSize.cx)) return false; if (!(*this >> outSize.cy)) return false; } return true; } BOOL CResFile::operator>>(char* outStr) { #line 427 _ASSERT(mBuffer); if (mIsBinaryMode) { #line 431 _ASSERT(mRemaining >= 1); mRemaining--; int len = mPos[0] + 1; mPos++; #line 435 _ASSERT(mRemaining >= len); mRemaining -= len; memcpy(outStr, mPos, len); mPos += len; } else { char buf[256]; char *p = buf; while (*mPos == ' ' || *mPos == '\t' || *mPos == '\n' || *mPos == '\r' || *mPos == ',' || *mPos == '(') { if (*mPos == '(') { while (*mPos != ')') mPos++; } mPos++; } if (*mPos == '\0') return false; while (*mPos != '\n' && *mPos != '\r' && *mPos != '\0') { *p = *mPos; p++; mPos++; } *p = 0; strcpy(outStr, buf); } return true; } BOOL CResFile::operator>>(CString& outStr) { #line 469 _ASSERT(mBuffer); char buf[256]; *this >> buf; outStr = buf; return true; } void CResFile::SkipDescriptor() { if (mIsBinaryMode) { #line 481 _ASSERT(mRemaining >= 1); mRemaining--; mPos++; } } void CResFile::End() { #line 489 _ASSERT(mRemaining == 0); }