Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pefile.cpp File Reference

#include "global.h"
#include <ctime>
#include <cstring>
#include <cctype>
#include <stdexcept>
#include "rva.h"
#include "pefile.hpp"
#include "apicache.h"
#include "utils/compilecheck.hpp"

Go to the source code of this file.

Defines

#define Error(s)   { throw std::runtime_error(s); }
#define FIELD   "%-30s"
#define FIELD   "%-30s"
#define FIELD   "%-30s"

Typedefs

typedef void(* PRelocationProc )(uint32_t address, unsigned type, void *param)

Functions

void WriteBinary (const ICFile &file, int _len)
 COMPILE_CHECK (error_Only_ASCII_charset_supported,('0'==0x30 &&'9'==0x39 &&'A'==0x41 &&'Z'==(0x41+25)&&'a'==0x61 &&'z'==(0x61+25)&&''==32 &&'\t'==9 &&'\n'==10))
void out_ws (const unicode_t *s)
void WriteResource (ICFile &file, const PeResourceDirectoryData *data, const PeResourceDirectoryInfo *info, unsigned level, void *)
void ParseResourceDirectory (const RVAFileTranslator &rva, uint32_t start, PeResourceDirectoryInfo *info, unsigned level, ICFile &file, PeFile::PResourceProc proc, void *param)

Variables

const char *const szDirectoryEntries []


Define Documentation

#define Error      { throw std::runtime_error(s); }
 

Definition at line 40 of file pefile.cpp.

Referenced by PeFile::PeFile().

#define FIELD   "%-30s"
 

#define FIELD   "%-30s"
 

#define FIELD   "%-30s"
 


Typedef Documentation

typedef void(* PRelocationProc)(uint32_t address,unsigned type,void* param)
 

Definition at line 445 of file pefile.cpp.

Referenced by PeFile::ParseRelocations().


Function Documentation

COMPILE_CHECK error_Only_ASCII_charset_supported   ,
('0'==0x30 &&'9'==0x39 &&'A'==0x41 &&'Z'==(0x41+25)&&'a'==0x61 &&'z'==(0x61+25)&&''==32 &&'\t'==9 &&'\n'==10)   
 

void out_ws const unicode_t   s [static]
 

Definition at line 546 of file pefile.cpp.

References unicode_t.

Referenced by WriteResource().

00547 {
00548   for(;*s;++s)
00549   {
00550     char c;
00551     c = char((*s) & 0xff);
00552     // !!! bad conversion
00553     if ( (*s) < 256 && isprint(c) )
00554       printf("%c",c);
00555     else
00556       printf("\\x%04x",*s);
00557   }
00558 }

void ParseResourceDirectory const RVAFileTranslator   rva,
uint32_t    start,
PeResourceDirectoryInfo   info,
unsigned    level,
ICFile   file,
PeFile::PResourceProc    proc,
void *    param
[static]
 

Definition at line 672 of file pefile.cpp.

References PeResourceDirectoryData::data, PeResourceDirectoryInfo::dir, PeResourceDirectoryInfo::highLevel, PeResourceId::id, PeResourceId::name, _PE_IMAGE_RESOURCE_DIRECTORY::NumberOfIdEntries, _PE_IMAGE_RESOURCE_DIRECTORY::NumberOfNamedEntries, _PE_IMAGE_RESOURCE_DATA_ENTRY::OffsetToData, PeFile::PResourceProc, RawRead(), ICFile::ReadStringFlag(), RESOURCE_ENTRY_DATA_OFFSET, RESOURCE_ENTRY_GETID, RESOURCE_ENTRY_ISDIRECTORY, RESOURCE_ENTRY_ISSTRING, RESOURCE_ENTRY_NAME_OFFSET, RVAFileTranslator::RVA2FileSafe(), ICFile::Seek(), ICFile::strLen16, ICFile::strUcs2, ICFile::Tell(), _PE_IMAGE_RESOURCE_DIRECTORY_ENTRY::u2, and unicode_t.

Referenced by PeFile::ParseResource().

00673 {
00674   // read raw directory info
00675   RawRead(file,info->dir);
00676 
00677   proc(file,NULL,info,level,param);
00678 
00679   int i = info->dir.NumberOfNamedEntries + info->dir.NumberOfIdEntries;
00680   for(;i>0;--i)
00681   {
00682     PE_IMAGE_RESOURCE_DIRECTORY_ENTRY entry;
00683     RawRead(file,entry);
00684     long pos = file.Tell();
00685     // finish (ID, Type, Language)
00686     unicode_t *pName = NULL;
00687     if ( RESOURCE_ENTRY_ISSTRING(entry) )
00688     {
00689 
00690       file.Seek(start+RESOURCE_ENTRY_NAME_OFFSET(entry));
00691       pName = (unicode_t*)file.ReadStringFlag(
00692         ICFile::strUcs2|ICFile::strLen16);
00693     }
00694 
00695     if ( RESOURCE_ENTRY_ISDIRECTORY(entry) )
00696     {
00697       file.Seek(start+RESOURCE_ENTRY_DATA_OFFSET(entry) );
00698 
00699       // init directory
00700       PeResourceDirectoryInfo _info;
00701       _info.name = pName;
00702       _info.id   = RESOURCE_ENTRY_GETID(entry);
00703       _info.highLevel = info;
00704 
00705       ParseResourceDirectory(rva,start,&_info,level+1,file,proc,param);
00706     }
00707     else
00708     {
00709       file.Seek(start+entry.u2.OffsetToData);
00710 
00711       // init data
00712       PeResourceDirectoryData data;
00713       RawRead(file,data.data);
00714       data.id = RESOURCE_ENTRY_GETID(entry);
00715       data.name = pName;
00716 
00717       file.Seek(rva.RVA2FileSafe(data.data.OffsetToData));
00718       proc(file,&data,info,level+1,param);
00719     }
00720     delete [] pName;
00721 
00722     file.Seek(pos);
00723   }
00724 }

void WriteBinary const ICFile   file,
int    _len
[static]
 

Definition at line 503 of file pefile.cpp.

References _PRG_ASSERT, and ICFile::RawRead().

00504 {
00505   _PRG_ASSERT(_len>0);
00506   unsigned int len = (unsigned int)_len;
00507   unsigned address = 0;
00508   const unsigned nCarInRow = 16;
00509   char buffer[3*nCarInRow+nCarInRow+10];
00510   for(unsigned n=0;;++n)
00511   {
00512     unsigned part = n%nCarInRow;
00513     if (part==0)
00514     {
00515       if (n>=nCarInRow)
00516       {
00517         buffer[nCarInRow*3+nCarInRow+1] = '\0';
00518         printf("%08X %s\n",address+n-nCarInRow,buffer);
00519         if (n>=len)
00520           return;
00521       }
00522       // inizializza buffer
00523       memset(buffer,' ',sizeof(buffer));
00524     }
00525     if (n<len)
00526     {
00527       uint8_t car;
00528       file.RawRead(&car,1);
00529 
00530       // scrive il carattere in esadecimale e carattere relativo
00531       sprintf(buffer+part*3,"%02X",car);
00532       buffer[part*3+2] = part==(nCarInRow/2-1)?'-':' ';
00533       // !!! settare come opzione carattere ?
00534       buffer[nCarInRow*3+1+part] = isprint(car)?car:'.';
00535     }
00536   }
00537 }

void WriteResource ICFile   file,
const PeResourceDirectoryData   data,
const PeResourceDirectoryInfo   info,
unsigned    level,
void *   
[static]
 

Definition at line 566 of file pefile.cpp.

References _PE_IMAGE_RESOURCE_DIRECTORY::Characteristics, _PE_IMAGE_RESOURCE_DATA_ENTRY::CodePage, PeResourceDirectoryData::data, PeResourceDirectoryInfo::dir, PeResourceId::id, _PE_IMAGE_RESOURCE_DIRECTORY::MajorVersion, _PE_IMAGE_RESOURCE_DIRECTORY::MinorVersion, PeResourceId::name, _PE_IMAGE_RESOURCE_DIRECTORY::NumberOfIdEntries, _PE_IMAGE_RESOURCE_DIRECTORY::NumberOfNamedEntries, _PE_IMAGE_RESOURCE_DATA_ENTRY::OffsetToData, out_ws(), _PE_IMAGE_RESOURCE_DATA_ENTRY::Reserved, _PE_IMAGE_RESOURCE_DATA_ENTRY::Size, _PE_IMAGE_RESOURCE_DIRECTORY::TimeDateStamp, and WriteBinary().

00567 {
00568   // se siamo all'apice dell'albero
00569   if (level == 0 && data == NULL)
00570   {
00571     printf("Resources\n");
00572     printf("%*s%-16s%08X\n",level*4,"","Characteristics",info->dir.Characteristics);
00573     time_t t = (time_t)info->dir.TimeDateStamp;
00574     printf("%*s%-16s%08X %s",level*4,"","TimeDateStamp",info->dir.TimeDateStamp,ctime(&t));
00575     printf("%*s%-16s%i:%i\n",level*4,"","Version",info->dir.MajorVersion,info->dir.MinorVersion);
00576   }
00577   else
00578   {
00579   // se c'e' un livello superiore stampa id o nome
00580   // se dati bisogna prendere il livello corrente
00581   const PeResourceId* p = data?(const PeResourceId*)data:(const PeResourceId*)info;
00582   if (p)
00583   {
00584     printf("%*s",(level)*4,"");
00585     switch(level-1)
00586     {
00587     case 0:
00588       printf("Type ");
00589       switch(p->id)
00590       {
00591       case 1: printf("cursor"); break;
00592       case 2: printf("bitmap"); break;
00593       case 3: printf("icon"); break;
00594       case 4: printf("menu"); break;
00595       case 5: printf("dialog"); break;
00596       case 6: printf("string table"); break;
00597       case 7: printf("font directory"); break;
00598       case 8: printf("font"); break;
00599       case 9: printf("accelerators"); break;
00600       case 10: printf("unformatted resource data"); break;
00601       case 11: printf("message table"); break;
00602       case 12: printf("group cursor"); break;
00603       case 14: printf("group icon"); break;
00604       case 16: printf("version information"); break;
00605       case 17: printf("dialog include"); break;
00606       case 19: printf("plug & play"); break;
00607       case 20: printf("vxd"); break;
00608       case 21: printf("animate cursor"); break;
00609       case 22: printf("animate icon"); break;
00610       case 23: printf("html"); break;
00611       default: printf("unknown"); break;
00612       }
00613       if (p->name)
00614       {
00615         printf(" (");
00616         out_ws(p->name);
00617         printf(")\n");
00618       }
00619       else
00620         printf(" (%i)\n",p->id);
00621       break;
00622     case 1:
00623       if (p->name)
00624       {
00625         printf("ID ");
00626         out_ws(p->name);
00627         printf("\n");
00628       }
00629       else
00630         printf("ID %i\n",p->id);
00631       break;
00632     case 2:
00633       if (p->name)
00634       {
00635         printf("LangID ");
00636         out_ws(p->name);
00637         printf("\n");
00638       }
00639       else
00640         printf("LangID %i\n",p->id);
00641       break;
00642     default:
00643       if (p->name)
00644       {
00645         printf("Name ");
00646         out_ws(p->name);
00647         printf("\n");
00648       }
00649       else
00650         printf("Name %08X\n",p->id);
00651       break;
00652     }
00653   }
00654   }
00655 
00656   // for directory write available entries
00657   if (data == NULL)
00658     printf("%*s%-16s%i named %i id\n",level*4,"","Entryes",info->dir.NumberOfNamedEntries,info->dir.NumberOfIdEntries);
00659 
00660   // for data write data information
00661   if (data)
00662   {
00663       printf("%*s%-16s%08X\n",(level)*4,"","OffsetToData",data->data.OffsetToData);
00664       printf("%*s%-16s%08X\n",(level)*4,"","Size",data->data.Size);
00665       printf("%*s%-16s%08X\n",(level)*4,"","CodePage",data->data.CodePage);
00666       printf("%*s%-16s%08X\n",(level)*4,"","Reserved",data->data.Reserved);
00667       // !!! use best method, save file ??
00668       WriteBinary(file,data->data.Size);
00669   }
00670 }


Variable Documentation

const char* const szDirectoryEntries[] [static]
 

Initial value:

{
  "Export",
  "Import",
  "Resource",
  "Exception",
  "Security",
  "Relocation",
  "Debug",
  "Copyright",
  "GlobalPtr",
  "TLS",
  "Load Configuration",
  "Bound Import",
  "IAT",
  "Delayed Imports",
  "COM Runtime",
  "Reserved"
}

Definition at line 42 of file pefile.cpp.

Referenced by PeFile::WriteOptionalHdr().


Generated on Mon Jan 13 22:20:35 2003 for perdr by doxygen1.2.15