//					PEFILES.H
//

#ifndef pefilesh
#define pefilesh

#define EXPORT_INFO 1
#define IMPORT_INFO 2
#define TLS_INFO 3
#define RESOURCE_INFO 4
#define FIXUP_INFO 5
#define DEBUG_INFO 6
#define CODE_DATA 7
#define INIT_DATA 8
#define UNINIT_DATA 9

struct header1
{ unsigned long sigbytes;
  unsigned short int cputype,objects;
  unsigned long timedatestamp;
  unsigned long reserveda[2];
  unsigned short int nt_hdr_size,flags;
  unsigned short int reserved;
  unsigned char lmajor,lminor;
  unsigned long reserved1[3];
  unsigned long entrypoint_rva;
  unsigned long reserved2[2];
  unsigned long image_base;
  unsigned long objectalign;
  unsigned long filealign;
  unsigned short int osmajor,osminor;
  unsigned short int usermajor,userminor;
  unsigned short int subsysmajor,subsysminor;
  unsigned long reserved3;
  unsigned long imagesize;
  unsigned long headersize;
  unsigned long filechecksum;
  unsigned short int subsystem,dllflags;
  unsigned long stackreserve,stackcommit;
  unsigned long heapreserve,heapcommit;
  unsigned long reserved4;
  unsigned long numintitems;
  unsigned long exporttable_rva,export_datasize;
  unsigned long importtable_rva,import_datasize;
  unsigned long resourcetable_rva,resource_datasize;
  unsigned long exceptiontable_rva,exception_datasize;
  unsigned long securitytable_rva,security_datasize;
  unsigned long fixuptable_rva,fixup_datasize;
  unsigned long debugtable_rva,debug_directory;
  unsigned long imagedesc_rva,imagedesc_datasize;
  unsigned long machspecific_rva,machspecific_datasize;
  unsigned long tls_rva,tls_datasize;
};

struct peobjdata
{ char name[8];
  unsigned long virt_size,rva;
  unsigned long phys_size,phys_offset;
  unsigned long reserved[3],obj_flags;
};

struct pe_object
{ struct peobjdata d;
  unsigned char type;
  struct pe_object *next;
};

struct codedata
{ unsigned char *dataptr;
  unsigned long size;
  unsigned long virt_size;
  unsigned long addr;
  struct codedata *next;
};

class pefile
{ private:
	 struct header1 h1;
	 SECURITY_ATTRIBUTES securityatt;
	 HANDLE efile;
	 struct pe_object *peobj;
	 struct codedata *code;
	 struct codedata *importdata,*dataimportdata;
	 struct codedata *exportdata,*dataexportdata;
    struct codedata *resourcedata,*dataresourcedata;
	 struct codedata *initdata;
    struct codedata *fixupdata;
	 unsigned long start;

  public:
	 pefile(void);
	 ~pefile(void);
	 BOOL open(char *fname);
	 void peinfo(void);
	 struct codedata *getcode(void);
	 struct codedata *getimports(void);
	 struct codedata *getexports(void);
	 struct codedata *getinitdata(void);
	 struct codedata *getresources(void);
    struct codedata *getrelocdata(void);
	 unsigned long getstart(void);
	 unsigned long getimagebase(void);
    unsigned char *findrva(unsigned long rva);
    unsigned char *finddata(unsigned long dat);

  private:
	 void readobjects(void);
	 void readobjectsdata(void);
	 void readcode(struct peobjdata *data);
	 void readimportdata(struct peobjdata *data);
	 void readexportdata(struct peobjdata *data);
	 void readinitdata(struct peobjdata *data);
	 void readfixupdata(struct peobjdata *data);
	 void readresources(struct peobjdata *data);
    void readcomfile(void);
};

#endif
