From 37e364b2c6cc7487a1c888d256a73e5337bb7189 Mon Sep 17 00:00:00 2001 From: Ash Wolf Date: Wed, 14 Jun 2023 00:50:34 +0100 Subject: initial commit --- src/T2DLL/CResFile.cpp | 459 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 459 insertions(+) create mode 100644 src/T2DLL/CResFile.cpp (limited to 'src/T2DLL/CResFile.cpp') diff --git a/src/T2DLL/CResFile.cpp b/src/T2DLL/CResFile.cpp new file mode 100644 index 0000000..3c31ec7 --- /dev/null +++ b/src/T2DLL/CResFile.cpp @@ -0,0 +1,459 @@ +#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 strN; + if (((DWORD) name) < 0x10000) + strN.Format("(%d)", ((DWORD) name) & 0xFFFF); + else + strN = name; + + CString strT; + if (((DWORD) type) < 0x10000) + strT.Format("(%d)", ((DWORD) type) & 0xFFFF); + else + strT = type; + + CString str = "CResFile::OpenResource ERROR : " + strN + "," + strT + " @" + 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 handle = LoadResource(inst, rsrc); + if (!handle) + return false; + + void *p = LockResource(handle); + mRemaining = SizeofResource(inst, rsrc); + mBuffer = (char *) malloc(mRemaining + 1); + memcpy(mBuffer, p, mRemaining); + UnlockResource(handle); + FreeResource(handle); + + SetupResource(); + + return true; +} + +BOOL CResFile::OpenResource(const char* path, int name, int type) { + CFile *theFile = new CFile; + if (!theFile->Open(path, CFile::modeRead)) { + delete theFile; + return false; + } + + char magic[2]; + theFile->Read(magic, 2); + theFile->Close(); + delete theFile; + + if (memcmp(magic, "SP", 2) == 0) { + SUBPLUGINFSENTRY entry; + CFile *f = OpenSubPluginFS(magic, name, type, &entry); + if (!f) + return false; + + mRemaining = entry.spfse_mC; + mBuffer = (char *) malloc(mRemaining + 1); + f->Read(mBuffer, mRemaining); + f->Close(); + delete f; + + mIsSubPlugin = true; + } else if (memcmp(magic, "PE", 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 str; + unsigned short langID = GetUserDefaultLangID(); + str.Format("\\.rsrc\\%s\\#%d\\#%d", buf, name, langID); + if (!pe.Seek(str)) + return false; + + 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); +} + -- cgit v1.2.3