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