I found an interesting chunk of code while browsing forums thismorning, and im wondering if somebody who is more versed in what it does could walk me through that its doing. Im just confused because it doesnt even use the second variable it asks for, and im not sure what it does with the data once its done doing what it does. It seems like the net effect of this function is zero, everything is the same before and after it runs.
Thx for your time.
Code:
bool CPoly::DoPoly(const char *szFile, const char *szOutFile)
{
char *szBuffer;
if(!MapFile(szFile, &szBuffer))
return false;
IMAGE_DOS_HEADER *iDosHeader=(IMAGE_DOS_HEADER*)szBuffer;
if(iDosHeader->e_magic!=IMAGE_DOS_SIGNATURE)
{
UnmapFile(&szBuffer);
return false;
}
char *pTemp=(char*)iDosHeader+iDosHeader->e_lfanew;
DWORD *dwSignature=(DWORD*)pTemp;
pTemp+=sizeof(DWORD);
IMAGE_FILE_HEADER *iFileHead=(IMAGE_FILE_HEADER*)pTemp;
pTemp+=sizeof(IMAGE_FILE_HEADER);
IMAGE_OPTIONAL_HEADER *iOptHead=(IMAGE_OPTIONAL_HEADER*)pTemp;
pTemp+=sizeof(IMAGE_OPTIONAL_HEADER);
IMAGE_SECTION_HEADER *iSectHead=(IMAGE_SECTION_HEADER*)pTemp;
if(*dwSignature!=IMAGE_NT_SIGNATURE)
{
UnmapFile(&szBuffer);
return false;
}
int iSection;
IMAGE_SECTION_HEADER *iSectPtr;
for(iSection=0, iSectPtr=iSectHead; iSection<iFileHead->NumberOfSections; iSection++, iSectPtr++)
{
if(iSectPtr->Characteristics&IMAGE_SCN_CNT_CODE)
{
char *szBuf=(char*)malloc(iSectPtr->SizeOfRawData);
memcpy(szBuf, (char*)szBuffer+iSectPtr->PointerToRawData, iSectPtr->SizeOfRawData);
free(szBuf);
}
}
UnmapFile(&szBuffer);
return true;
}
The 2 functions it calls, mapfile and unmapfile are here.
Code:
bool CPoly::MapFile(const char *szFile, char **szBuffer)
{
FILE *fp=fopen(szFile, "rb");
if(!fp)
return false;
fseek(fp, 0, SEEK_END);
int iFileSize=(int)ftell(fp);
fseek(fp, 0, SEEK_SET);
*szBuffer=(char*)malloc(iFileSize);
if(!*szBuffer)
{
fclose(fp);
return false;
}
if(fread(*szBuffer, sizeof(char), iFileSize, fp)<iFileSize)
{
fclose(fp);
return false;
}
fclose(fp);
return true;
}
void CPoly::UnmapFile(char **szBuffer)
{
free(*szBuffer);
}