[C] PEFileSize function

Iniciado por The Swash, Enero 25, 2011, 02:16:11 AM

Tema anterior - Siguiente tema

0 Miembros y 2 Visitantes están viendo este tema.

Enero 25, 2011, 02:16:11 AM Ultima modificación: Febrero 08, 2014, 06:11:41 PM por Expermicid
Código: c
/* 
   [Get size of file to base of PE]
   - Programmer: The Swash
   - Dedicated: Zero, Thor Psymera, Steve10120
*/

#include <windows.h>
#include <stdio.h>
#define RB "rb"

int PEFileSize(char * fPath);


int main(void)
{
    printf("File size: %i", PEFileSize("C:\\test.exe"));
    getchar();
}

int PEFileSize(char * fPath)
{
    IMAGE_DOS_HEADER IDH;
    IMAGE_NT_HEADERS INH;
    IMAGE_SECTION_HEADER ISH;
    FILE * lpFile;
    int sTemp = 0, i;
   
    lpFile = fopen(fPath,RB);
    if (lpFile != NULL)
    {
               fseek(lpFile, 0, SEEK_SET); // Seek to begin of file
               fread(&IDH, sizeof(IDH), 1, lpFile); // Read 64 bytes to IDH struct
               if (IDH.e_magic == IMAGE_DOS_SIGNATURE) // If IDH.e_magic = (MZ)
               {
                               fseek(lpFile, IDH.e_lfanew, SEEK_SET); // Seek in file in begin of NT Headers (PE)
                               fread(&INH, sizeof(INH), 1, lpFile); // Read in structure 248 bytes
                               if (INH.Signature == IMAGE_NT_SIGNATURE) // If INH.Signature = (PE)
                               {
                                                 for (i = 0; i < INH.FileHeader.NumberOfSections; i++) // go for all sections
                                                 {
                                                     fseek(lpFile, IDH.e_lfanew + sizeof(INH) + sizeof(ISH)*i, SEEK_SET); // Seek in actual section
                                                     fread(&ISH, sizeof(ISH), 1, lpFile); // Read section
                                                     sTemp += ISH.SizeOfRawData; // Save sizeofrawdata of section
                                                 }
                                                 sTemp += INH.OptionalHeader.SizeOfHeaders;
                                                 fclose(lpFile);
                                                 return sTemp;
                               }
               }
               else
               {
                   return -1;
               }
    }
    else
    {
        return -1;
    }
}