| 1 | /* $Id: winimagebase.h,v 1.3 1999-10-05 13:48:40 sandervl Exp $ */
 | 
|---|
| 2 | 
 | 
|---|
| 3 | /*
 | 
|---|
| 4 |  * Win32 PE Image base class
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * Project Odin Software License can be found in LICENSE.TXT
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  */
 | 
|---|
| 12 | #ifndef __WINIMAGEBASE_H__
 | 
|---|
| 13 | #define __WINIMAGEBASE_H__
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <peexe.h>
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #ifdef DEBUG
 | 
|---|
| 18 | #define MAGIC_WINIMAGE          0x11223344
 | 
|---|
| 19 | #endif
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #ifndef CCHMAXPATH
 | 
|---|
| 22 | #define CCHMAXPATH 260
 | 
|---|
| 23 | #endif
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #define LANG_GETFIRST           0x80000000
 | 
|---|
| 26 | #define ID_GETFIRST             LANG_GETFIRST
 | 
|---|
| 27 | #define IDLANG_GETFIRST         LANG_GETFIRST
 | 
|---|
| 28 | 
 | 
|---|
| 29 | class Win32Resource;
 | 
|---|
| 30 | 
 | 
|---|
| 31 | class Win32ImageBase
 | 
|---|
| 32 | {
 | 
|---|
| 33 | #ifdef DEBUG
 | 
|---|
| 34 | protected:
 | 
|---|
| 35 |         DWORD magic;
 | 
|---|
| 36 | public:
 | 
|---|
| 37 |         void checkObject()
 | 
|---|
| 38 |         {
 | 
|---|
| 39 |                 if(magic != MAGIC_WINIMAGE) {
 | 
|---|
| 40 |                         eprintf(("Corrupt this pointer %X %X!!", this, magic));
 | 
|---|
| 41 |                         DebugInt3();
 | 
|---|
| 42 |                 }
 | 
|---|
| 43 |         };
 | 
|---|
| 44 | #endif
 | 
|---|
| 45 | 
 | 
|---|
| 46 | public:
 | 
|---|
| 47 |          // Constructors and destructors
 | 
|---|
| 48 |          Win32ImageBase(HINSTANCE hInstance);
 | 
|---|
| 49 | virtual ~Win32ImageBase();
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         ULONG     getError()          { return errorState; };
 | 
|---|
| 52 |         HINSTANCE getInstanceHandle() { return hinstance; };
 | 
|---|
| 53 | 
 | 
|---|
| 54 | virtual void setFullPath(char *name);
 | 
|---|
| 55 |         char *getFullPath()           { return fullpath; };
 | 
|---|
| 56 | 
 | 
|---|
| 57 |         char *getModuleName()         { return szModule; };
 | 
|---|
| 58 | 
 | 
|---|
| 59 | virtual HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 60 |         HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 61 | virtual ULONG getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 62 |         ULONG getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 63 | 
 | 
|---|
| 64 | virtual ULONG getVersionSize();
 | 
|---|
| 65 | virtual BOOL  getVersionStruct(char *verstruct, ULONG bufLength);
 | 
|---|
| 66 | 
 | 
|---|
| 67 | static  BOOL  isPEImage(char *szFileName);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         void  setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         void  setTLSAddress(LPVOID dwTlsAddress)        { tlsAddress = dwTlsAddress; };
 | 
|---|
| 72 |         void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)   { tlsIndexAddr = dwTlsIndexAddr; };
 | 
|---|
| 73 |         void  setTLSInitSize(ULONG dwTlsSize)           { tlsInitSize = dwTlsSize; };
 | 
|---|
| 74 |         void  setTLSTotalSize(ULONG dwTlsSize)          { tlsTotalSize = dwTlsSize; };
 | 
|---|
| 75 |         void  setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)        
 | 
|---|
| 76 |         {
 | 
|---|
| 77 |            tlsCallBackAddr = dwTlsCallBackAddr;
 | 
|---|
| 78 |         };
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         void  tlsAttachThread();        //setup TLS structures for new thread
 | 
|---|
| 81 |         void  tlsDetachThread();        //destroy TLS structures
 | 
|---|
| 82 | 
 | 
|---|
| 83 | virtual BOOL  isDll() = 0;
 | 
|---|
| 84 | 
 | 
|---|
| 85 | protected:
 | 
|---|
| 86 |         void tlsAlloc();                //Allocate TLS index for this module
 | 
|---|
| 87 |         void tlsDelete();               //Destroy TLS index for this module
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         Win32Resource        *winres;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         ULONG                 errorState, entryPoint;
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         char                 *fullpath;
 | 
|---|
| 94 |         char                  szModule[CCHMAXPATH];
 | 
|---|
| 95 |         char                  szFileName[CCHMAXPATH];
 | 
|---|
| 96 | 
 | 
|---|
| 97 |         HINSTANCE             hinstance;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |         LPVOID                tlsAddress;               //address of TLS data
 | 
|---|
| 100 |         LPDWORD               tlsIndexAddr;             //address of DWORD that receives the TLS index
 | 
|---|
| 101 |         ULONG                 tlsInitSize;              //size of initialized TLS memory block
 | 
|---|
| 102 |         ULONG                 tlsTotalSize;             //size of TLS memory block
 | 
|---|
| 103 |         PIMAGE_TLS_CALLBACK  *tlsCallBackAddr;  //ptr to TLS callback array
 | 
|---|
| 104 |         ULONG                 tlsIndex;         //module TLS index
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         ULONG getPEResourceSize(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |         PIMAGE_RESOURCE_DATA_ENTRY getPEResourceEntry(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
 | 
|---|
| 109 |         PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
 | 
|---|
| 110 |                                                     ULONG *nodeData, int level);
 | 
|---|
| 111 |         PIMAGE_RESOURCE_DIRECTORY pResDir;
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         //substracted from RVA data offsets
 | 
|---|
| 114 |         ULONG                     pResourceSectionStart;
 | 
|---|
| 115 | 
 | 
|---|
| 116 | private:
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         friend class Win32Resource;
 | 
|---|
| 119 |         friend ULONG SYSTEM GetVersionSize(char *modname);
 | 
|---|
| 120 | };
 | 
|---|
| 121 | 
 | 
|---|
| 122 | //SvL: This structure is placed at the end of the first page of the image (header 
 | 
|---|
| 123 | //     page), so we can determine the Win32Image pointer from a HINSTANCE variable
 | 
|---|
| 124 | //     (which is actually the address of the win32 module)
 | 
|---|
| 125 | typedef struct 
 | 
|---|
| 126 | {
 | 
|---|
| 127 |   Win32ImageBase *image;
 | 
|---|
| 128 |   ULONG           magic; 
 | 
|---|
| 129 | } WINIMAGE_LOOKUP;
 | 
|---|
| 130 | 
 | 
|---|
| 131 | #define WINIMAGE_LOOKUPADDR(a)  (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
 | 
|---|
| 132 | 
 | 
|---|
| 133 | #endif //__WINIMAGEBASE_H__
 | 
|---|