| 1 | /* $Id: winimagebase.h,v 1.20 2001-10-09 20:25:20 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 PE Image base class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright 1999-2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 8 | * | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 | #ifndef __WINIMAGEBASE_H__ | 
|---|
| 14 | #define __WINIMAGEBASE_H__ | 
|---|
| 15 |  | 
|---|
| 16 | #include <peexe.h> | 
|---|
| 17 | #include "queue.h" | 
|---|
| 18 | #ifdef OS2_INCLUDED | 
|---|
| 19 | #include <winconst.h> | 
|---|
| 20 | #else | 
|---|
| 21 | #include <win\winnls.h> | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | #define MAGIC_WINIMAGE          0x11223344 | 
|---|
| 25 |  | 
|---|
| 26 | #ifndef CCHMAXPATH | 
|---|
| 27 | #define CCHMAXPATH 260 | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | #define ID_GETFIRST             0xF0000000 | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef ENUMRESNAMEPROC | 
|---|
| 33 | typedef BOOL (* CALLBACK ENUMRESTYPEPROCA)(HMODULE,LPSTR,LONG); | 
|---|
| 34 | typedef BOOL (* CALLBACK ENUMRESTYPEPROCW)(HMODULE,LPWSTR,LONG); | 
|---|
| 35 | typedef BOOL (* CALLBACK ENUMRESNAMEPROCA)(HMODULE,LPCSTR,LPSTR,LONG); | 
|---|
| 36 | typedef BOOL (* CALLBACK ENUMRESNAMEPROCW)(HMODULE,LPCWSTR,LPWSTR,LONG); | 
|---|
| 37 | typedef BOOL (* CALLBACK ENUMRESLANGPROCA)(HMODULE,LPCSTR,LPCSTR,WORD,LONG); | 
|---|
| 38 | typedef BOOL (* CALLBACK ENUMRESLANGPROCW)(HMODULE,LPCWSTR,LPCWSTR,WORD,LONG); | 
|---|
| 39 | #endif | 
|---|
| 40 |  | 
|---|
| 41 | class Win32Resource; | 
|---|
| 42 | class Win32DllBase; | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | class Win32ImageBase | 
|---|
| 46 | { | 
|---|
| 47 | protected: | 
|---|
| 48 | DWORD magic; | 
|---|
| 49 | public: | 
|---|
| 50 | void checkObject() | 
|---|
| 51 | { | 
|---|
| 52 | if (magic != MAGIC_WINIMAGE) { | 
|---|
| 53 | eprintf(("Corrupt this pointer %X %X!!", this, magic)); | 
|---|
| 54 | DebugInt3(); | 
|---|
| 55 | } | 
|---|
| 56 | }; | 
|---|
| 57 |  | 
|---|
| 58 | public: | 
|---|
| 59 | // Constructors and destructors | 
|---|
| 60 | Win32ImageBase(HINSTANCE hInstance); | 
|---|
| 61 | virtual ~Win32ImageBase(); | 
|---|
| 62 |  | 
|---|
| 63 | ULONG     getError()          { return errorState; }; | 
|---|
| 64 | HINSTANCE getInstanceHandle() { return hinstance; }; | 
|---|
| 65 |  | 
|---|
| 66 | //Returns required OS version for this image | 
|---|
| 67 | virtual ULONG getVersion(); | 
|---|
| 68 |  | 
|---|
| 69 | virtual void  setFullPath(char *name); | 
|---|
| 70 | char *getFullPath()           { return fullpath; }; | 
|---|
| 71 | char *getModuleName()         { return szModule; }; | 
|---|
| 72 |  | 
|---|
| 73 | //findResource returns the pointer of the resource's IMAGE_RESOURCE_DATA_ENTRY structure | 
|---|
| 74 | HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)); | 
|---|
| 75 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)); | 
|---|
| 76 |  | 
|---|
| 77 | ULONG getResourceSizeA(LPSTR lpszName, LPSTR lpszType, ULONG lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)); | 
|---|
| 78 | ULONG getResourceSizeW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)); | 
|---|
| 79 | BOOL  enumResourceNamesA(HMODULE hmod, LPCTSTR  lpszType, ENUMRESNAMEPROCA lpEnumFunc, LONG lParam); | 
|---|
| 80 | BOOL  enumResourceNamesW(HMODULE hmod, LPCWSTR  lpszType, ENUMRESNAMEPROCW lpEnumFunc, LONG lParam); | 
|---|
| 81 | BOOL  enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc, | 
|---|
| 82 | LONG lParam); | 
|---|
| 83 | BOOL  enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc, | 
|---|
| 84 | LONG lParam); | 
|---|
| 85 | BOOL  enumResourceLanguagesA(HMODULE hmod, LPCSTR lpType, LPCSTR lpName, | 
|---|
| 86 | ENUMRESLANGPROCA lpEnumFunc, LONG lParam); | 
|---|
| 87 | BOOL  enumResourceLanguagesW(HMODULE hmod, LPCWSTR lpType, LPCWSTR lpName, | 
|---|
| 88 | ENUMRESLANGPROCW lpEnumFunc, LONG lParam); | 
|---|
| 89 |  | 
|---|
| 90 | ULONG getVersionSize(); | 
|---|
| 91 | BOOL  getVersionStruct(char *verstruct, ULONG bufLength); | 
|---|
| 92 |  | 
|---|
| 93 | //Returns pointer to data of resource handle | 
|---|
| 94 | char *getResourceAddr(HRSRC hResource); | 
|---|
| 95 | ULONG getResourceSize(HRSRC hResource); | 
|---|
| 96 |  | 
|---|
| 97 | //returns ERROR_SUCCESS or error code (Characteristics will contain | 
|---|
| 98 | //the Characteristics member of the file header structure) | 
|---|
| 99 | static  ULONG isPEImage(char *szFileName, DWORD *Characteristics, DWORD *subsystem, DWORD *fNEExe = NULL); | 
|---|
| 100 | static  BOOL  findDll(const char *pszFileName, char *pszFullName, | 
|---|
| 101 | int cchFullName, const char *pszAltPath = NULL); | 
|---|
| 102 |  | 
|---|
| 103 | void  setEntryPoint(ULONG startAddress)         { entryPoint = startAddress; }; | 
|---|
| 104 |  | 
|---|
| 105 | void  setTLSAddress(LPVOID dwTlsAddress)        { tlsAddress = dwTlsAddress; }; | 
|---|
| 106 | void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)   { tlsIndexAddr = dwTlsIndexAddr; }; | 
|---|
| 107 | void  setTLSInitSize(ULONG dwTlsSize)           { tlsInitSize = dwTlsSize; }; | 
|---|
| 108 | void  setTLSTotalSize(ULONG dwTlsSize)          { tlsTotalSize = dwTlsSize; }; | 
|---|
| 109 | void  setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr) | 
|---|
| 110 | { | 
|---|
| 111 | tlsCallBackAddr = dwTlsCallBackAddr; | 
|---|
| 112 | }; | 
|---|
| 113 |  | 
|---|
| 114 | void  tlsAttachThread();    //setup TLS structures for new thread | 
|---|
| 115 | void  tlsDetachThread();    //destroy TLS structures | 
|---|
| 116 |  | 
|---|
| 117 | virtual BOOL  insideModule(ULONG address); | 
|---|
| 118 | virtual BOOL  insideModuleCode(ULONG address); | 
|---|
| 119 |  | 
|---|
| 120 | virtual ULONG getApi(char *name)  = 0; | 
|---|
| 121 | virtual ULONG getApi(int ordinal) = 0; | 
|---|
| 122 |  | 
|---|
| 123 | virtual ULONG getImageSize(); | 
|---|
| 124 |  | 
|---|
| 125 | virtual BOOL  isDll() = 0; | 
|---|
| 126 |  | 
|---|
| 127 | static Win32ImageBase * findModule(HMODULE hModule); | 
|---|
| 128 | BOOL  matchModName(const char *pszFilename) const; | 
|---|
| 129 |  | 
|---|
| 130 | /* @cat Depencies */ | 
|---|
| 131 | //Add image to dependency list of this image | 
|---|
| 132 | void  addDependency(Win32DllBase *dll); | 
|---|
| 133 | BOOL  dependsOn(Win32DllBase *dll); | 
|---|
| 134 |  | 
|---|
| 135 | protected: | 
|---|
| 136 | void tlsAlloc();        //Allocate TLS index for this module | 
|---|
| 137 | void tlsDelete();       //Destroy TLS index for this module | 
|---|
| 138 |  | 
|---|
| 139 | ULONG                   errorState, | 
|---|
| 140 | entryPoint; | 
|---|
| 141 |  | 
|---|
| 142 | char *                  fullpath; | 
|---|
| 143 | char                    szModule[CCHMAXPATH]; | 
|---|
| 144 | char                    szFileName[CCHMAXPATH]; | 
|---|
| 145 |  | 
|---|
| 146 | HINSTANCE               hinstance; | 
|---|
| 147 |  | 
|---|
| 148 | LPVOID                  tlsAddress;         //address of TLS data | 
|---|
| 149 | LPDWORD                 tlsIndexAddr;       //address of DWORD that receives the TLS index | 
|---|
| 150 | ULONG                   tlsInitSize;        //size of initialized TLS memory block | 
|---|
| 151 | ULONG                   tlsTotalSize;       //size of TLS memory block | 
|---|
| 152 | PIMAGE_TLS_CALLBACK *   tlsCallBackAddr;    //ptr to TLS callback array | 
|---|
| 153 | ULONG                   tlsIndex;           //module TLS index | 
|---|
| 154 |  | 
|---|
| 155 | PIMAGE_RESOURCE_DIRECTORY  getResSubDirW(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCWSTR lpszName); | 
|---|
| 156 | PIMAGE_RESOURCE_DIRECTORY  getResSubDirA(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCTSTR lpszName); | 
|---|
| 157 |  | 
|---|
| 158 | PIMAGE_RESOURCE_DATA_ENTRY getResDataLang(PIMAGE_RESOURCE_DIRECTORY pResDir, ULONG language, BOOL fGetDefault = FALSE); | 
|---|
| 159 |  | 
|---|
| 160 | HRSRC                      getResourceLang(PIMAGE_RESOURCE_DIRECTORY pResDirToSearch); | 
|---|
| 161 | HRSRC                      getResourceLangEx(PIMAGE_RESOURCE_DIRECTORY pResDirToSearch, | 
|---|
| 162 | DWORD lang); | 
|---|
| 163 |  | 
|---|
| 164 | PIMAGE_RESOURCE_DIRECTORY   pResRootDir; | 
|---|
| 165 |  | 
|---|
| 166 | //substracted from RVA data offsets | 
|---|
| 167 | ULONG                   ulRVAResourceSection; | 
|---|
| 168 |  | 
|---|
| 169 | //linked list of dlls loaded on behalf of this executable image (dll or exe) | 
|---|
| 170 | Queue                   loadedDlls; | 
|---|
| 171 |  | 
|---|
| 172 | private: | 
|---|
| 173 | friend class Win32Resource; | 
|---|
| 174 | friend ULONG SYSTEM GetVersionSize(char *modname); | 
|---|
| 175 | }; | 
|---|
| 176 |  | 
|---|
| 177 | #endif //__WINIMAGEBASE_H__ | 
|---|