| [1891] | 1 | /* $Id: winimagebase.h,v 1.8 1999-11-30 19:39:14 sandervl Exp $ */ | 
|---|
| [953] | 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 | #define MAGIC_WINIMAGE          0x11223344 | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #ifndef CCHMAXPATH | 
|---|
|  | 20 | #define CCHMAXPATH 260 | 
|---|
|  | 21 | #endif | 
|---|
|  | 22 |  | 
|---|
|  | 23 | #define LANG_GETFIRST           0x80000000 | 
|---|
| [1136] | 24 | #define ID_GETFIRST             LANG_GETFIRST | 
|---|
|  | 25 | #define IDLANG_GETFIRST         LANG_GETFIRST | 
|---|
| [953] | 26 |  | 
|---|
| [1872] | 27 | #ifndef ENUMRESNAMEPROC | 
|---|
|  | 28 | typedef BOOL (* CALLBACK ENUMRESTYPEPROCA)(HMODULE,LPSTR,LONG); | 
|---|
|  | 29 | typedef BOOL (* CALLBACK ENUMRESTYPEPROCW)(HMODULE,LPWSTR,LONG); | 
|---|
|  | 30 | typedef BOOL (* CALLBACK ENUMRESNAMEPROCA)(HMODULE,LPCSTR,LPSTR,LONG); | 
|---|
|  | 31 | typedef BOOL (* CALLBACK ENUMRESNAMEPROCW)(HMODULE,LPCWSTR,LPWSTR,LONG); | 
|---|
|  | 32 | typedef BOOL (* CALLBACK ENUMRESLANGPROCA)(HMODULE,LPCSTR,LPCSTR,WORD,LONG); | 
|---|
|  | 33 | typedef BOOL (* CALLBACK ENUMRESLANGPROCW)(HMODULE,LPCWSTR,LPCWSTR,WORD,LONG); | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
| [953] | 36 | class Win32Resource; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | class Win32ImageBase | 
|---|
|  | 39 | { | 
|---|
|  | 40 | #ifdef DEBUG | 
|---|
|  | 41 | protected: | 
|---|
|  | 42 | DWORD magic; | 
|---|
|  | 43 | public: | 
|---|
|  | 44 | void checkObject() | 
|---|
|  | 45 | { | 
|---|
|  | 46 | if(magic != MAGIC_WINIMAGE) { | 
|---|
|  | 47 | eprintf(("Corrupt this pointer %X %X!!", this, magic)); | 
|---|
|  | 48 | DebugInt3(); | 
|---|
|  | 49 | } | 
|---|
|  | 50 | }; | 
|---|
|  | 51 | #endif | 
|---|
|  | 52 |  | 
|---|
|  | 53 | public: | 
|---|
|  | 54 | // Constructors and destructors | 
|---|
|  | 55 | Win32ImageBase(HINSTANCE hInstance); | 
|---|
|  | 56 | virtual ~Win32ImageBase(); | 
|---|
|  | 57 |  | 
|---|
|  | 58 | ULONG     getError()          { return errorState; }; | 
|---|
|  | 59 | HINSTANCE getInstanceHandle() { return hinstance; }; | 
|---|
|  | 60 |  | 
|---|
| [1891] | 61 | //Returns required OS version for this image | 
|---|
|  | 62 | virtual ULONG     getVersion(); | 
|---|
|  | 63 |  | 
|---|
| [953] | 64 | virtual void setFullPath(char *name); | 
|---|
|  | 65 | char *getFullPath()           { return fullpath; }; | 
|---|
|  | 66 |  | 
|---|
|  | 67 | char *getModuleName()         { return szModule; }; | 
|---|
|  | 68 |  | 
|---|
| [978] | 69 | virtual HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST); | 
|---|
| [953] | 70 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST); | 
|---|
| [978] | 71 | virtual ULONG getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST); | 
|---|
| [953] | 72 | ULONG getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST); | 
|---|
| [1872] | 73 | virtual BOOL  enumResourceNamesA(HMODULE hmod, LPCTSTR  lpszType, ENUMRESNAMEPROCA lpEnumFunc, LONG lParam); | 
|---|
|  | 74 | virtual BOOL  enumResourceNamesW(HMODULE hmod, LPCWSTR  lpszType, ENUMRESNAMEPROCW lpEnumFunc, LONG lParam); | 
|---|
| [1879] | 75 | virtual BOOL  enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc, | 
|---|
|  | 76 | LONG lParam); | 
|---|
|  | 77 | virtual BOOL  enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc, | 
|---|
|  | 78 | LONG lParam); | 
|---|
| [953] | 79 |  | 
|---|
| [978] | 80 | virtual ULONG getVersionSize(); | 
|---|
|  | 81 | virtual BOOL  getVersionStruct(char *verstruct, ULONG bufLength); | 
|---|
| [953] | 82 |  | 
|---|
|  | 83 | static  BOOL  isPEImage(char *szFileName); | 
|---|
|  | 84 |  | 
|---|
|  | 85 | void  setEntryPoint(ULONG startAddress) { entryPoint = startAddress; }; | 
|---|
|  | 86 |  | 
|---|
|  | 87 | void  setTLSAddress(LPVOID dwTlsAddress)        { tlsAddress = dwTlsAddress; }; | 
|---|
|  | 88 | void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)   { tlsIndexAddr = dwTlsIndexAddr; }; | 
|---|
|  | 89 | void  setTLSInitSize(ULONG dwTlsSize)           { tlsInitSize = dwTlsSize; }; | 
|---|
|  | 90 | void  setTLSTotalSize(ULONG dwTlsSize)          { tlsTotalSize = dwTlsSize; }; | 
|---|
| [1872] | 91 | void  setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr) | 
|---|
| [953] | 92 | { | 
|---|
|  | 93 | tlsCallBackAddr = dwTlsCallBackAddr; | 
|---|
|  | 94 | }; | 
|---|
|  | 95 |  | 
|---|
|  | 96 | void  tlsAttachThread();        //setup TLS structures for new thread | 
|---|
|  | 97 | void  tlsDetachThread();        //destroy TLS structures | 
|---|
|  | 98 |  | 
|---|
| [1843] | 99 | virtual ULONG     getApi(char *name)  = 0; | 
|---|
|  | 100 | virtual ULONG     getApi(int ordinal) = 0; | 
|---|
|  | 101 |  | 
|---|
| [953] | 102 | virtual BOOL  isDll() = 0; | 
|---|
|  | 103 |  | 
|---|
| [1872] | 104 | static Win32ImageBase * findModule(HMODULE hModule); | 
|---|
|  | 105 |  | 
|---|
| [953] | 106 | protected: | 
|---|
|  | 107 | void tlsAlloc();                //Allocate TLS index for this module | 
|---|
|  | 108 | void tlsDelete();               //Destroy TLS index for this module | 
|---|
|  | 109 |  | 
|---|
|  | 110 | Win32Resource        *winres; | 
|---|
|  | 111 |  | 
|---|
|  | 112 | ULONG                 errorState, entryPoint; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | char                 *fullpath; | 
|---|
|  | 115 | char                  szModule[CCHMAXPATH]; | 
|---|
|  | 116 | char                  szFileName[CCHMAXPATH]; | 
|---|
|  | 117 |  | 
|---|
|  | 118 | HINSTANCE             hinstance; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | LPVOID                tlsAddress;               //address of TLS data | 
|---|
|  | 121 | LPDWORD               tlsIndexAddr;             //address of DWORD that receives the TLS index | 
|---|
|  | 122 | ULONG                 tlsInitSize;              //size of initialized TLS memory block | 
|---|
|  | 123 | ULONG                 tlsTotalSize;             //size of TLS memory block | 
|---|
|  | 124 | PIMAGE_TLS_CALLBACK  *tlsCallBackAddr;  //ptr to TLS callback array | 
|---|
|  | 125 | ULONG                 tlsIndex;         //module TLS index | 
|---|
|  | 126 |  | 
|---|
| [978] | 127 | ULONG getPEResourceSize(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST); | 
|---|
|  | 128 |  | 
|---|
|  | 129 | PIMAGE_RESOURCE_DATA_ENTRY getPEResourceEntry(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST); | 
|---|
|  | 130 | PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType, | 
|---|
|  | 131 | ULONG *nodeData, int level); | 
|---|
| [1872] | 132 | PIMAGE_RESOURCE_DIRECTORY  getResSubDirW(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCWSTR lpszName); | 
|---|
|  | 133 | PIMAGE_RESOURCE_DIRECTORY  getResSubDirA(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCTSTR lpszName); | 
|---|
|  | 134 |  | 
|---|
| [978] | 135 | PIMAGE_RESOURCE_DIRECTORY pResDir; | 
|---|
|  | 136 |  | 
|---|
|  | 137 | //substracted from RVA data offsets | 
|---|
| [1872] | 138 | ULONG                     ulRVAResourceSection; | 
|---|
| [978] | 139 |  | 
|---|
| [953] | 140 | private: | 
|---|
|  | 141 |  | 
|---|
|  | 142 | friend class Win32Resource; | 
|---|
|  | 143 | friend ULONG SYSTEM GetVersionSize(char *modname); | 
|---|
|  | 144 | }; | 
|---|
|  | 145 |  | 
|---|
| [1872] | 146 | //SvL: This structure is placed at the end of the first page of the image (header | 
|---|
| [953] | 147 | //     page), so we can determine the Win32Image pointer from a HINSTANCE variable | 
|---|
|  | 148 | //     (which is actually the address of the win32 module) | 
|---|
| [1872] | 149 | typedef struct | 
|---|
| [953] | 150 | { | 
|---|
| [1872] | 151 | ULONG           magic1; | 
|---|
| [953] | 152 | Win32ImageBase *image; | 
|---|
| [1872] | 153 | ULONG           magic2; | 
|---|
| [953] | 154 | } WINIMAGE_LOOKUP; | 
|---|
|  | 155 |  | 
|---|
|  | 156 | #define WINIMAGE_LOOKUPADDR(a)  (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP)) | 
|---|
|  | 157 |  | 
|---|
|  | 158 | #endif //__WINIMAGEBASE_H__ | 
|---|