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