| 1 | /* $Id: winimagebase.h,v 1.13 2000-06-28 18:08:35 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 0xF0000000
|
|---|
| 25 | #define ID_GETFIRST 0xF0000000
|
|---|
| 26 |
|
|---|
| 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 |
|
|---|
| 36 | class Win32Resource;
|
|---|
| 37 | class Win32DllBase;
|
|---|
| 38 |
|
|---|
| 39 | class Win32ImageBase
|
|---|
| 40 | {
|
|---|
| 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 |
|
|---|
| 52 | public:
|
|---|
| 53 | // Constructors and destructors
|
|---|
| 54 | Win32ImageBase(HINSTANCE hInstance);
|
|---|
| 55 | virtual ~Win32ImageBase();
|
|---|
| 56 |
|
|---|
| 57 | ULONG getError() { return errorState; };
|
|---|
| 58 | HINSTANCE getInstanceHandle() { return hinstance; };
|
|---|
| 59 |
|
|---|
| 60 | //Returns required OS version for this image
|
|---|
| 61 | virtual ULONG getVersion();
|
|---|
| 62 |
|
|---|
| 63 | virtual void setFullPath(char *name);
|
|---|
| 64 | char *getFullPath() { return fullpath; };
|
|---|
| 65 |
|
|---|
| 66 | char *getModuleName() { return szModule; };
|
|---|
| 67 |
|
|---|
| 68 | //findResource returns the pointer of the resource's IMAGE_RESOURCE_DATA_ENTRY structure
|
|---|
| 69 | HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
|
|---|
| 70 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
|
|---|
| 71 |
|
|---|
| 72 | ULONG getResourceSizeA(LPSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
|
|---|
| 73 | ULONG getResourceSizeW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
|
|---|
| 74 | BOOL enumResourceNamesA(HMODULE hmod, LPCTSTR lpszType, ENUMRESNAMEPROCA lpEnumFunc, LONG lParam);
|
|---|
| 75 | BOOL enumResourceNamesW(HMODULE hmod, LPCWSTR lpszType, ENUMRESNAMEPROCW lpEnumFunc, LONG lParam);
|
|---|
| 76 | BOOL enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc,
|
|---|
| 77 | LONG lParam);
|
|---|
| 78 | BOOL enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc,
|
|---|
| 79 | LONG lParam);
|
|---|
| 80 |
|
|---|
| 81 | ULONG getVersionSize();
|
|---|
| 82 | BOOL getVersionStruct(char *verstruct, ULONG bufLength);
|
|---|
| 83 |
|
|---|
| 84 | //Returns pointer to data of resource handle
|
|---|
| 85 | char *getResourceAddr(HRSRC hResource);
|
|---|
| 86 | ULONG getResourceSize(HRSRC hResource);
|
|---|
| 87 |
|
|---|
| 88 | //returns ERROR_SUCCESS or error code
|
|---|
| 89 | static ULONG isPEImage(char *szFileName);
|
|---|
| 90 | static BOOL findDll(const char *pszFileName, char *pszFullName,
|
|---|
| 91 | int cchFullName, const char *pszAltPath = NULL);
|
|---|
| 92 |
|
|---|
| 93 | void setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
|
|---|
| 94 |
|
|---|
| 95 | void setTLSAddress(LPVOID dwTlsAddress) { tlsAddress = dwTlsAddress; };
|
|---|
| 96 | void setTLSIndexAddr(LPDWORD dwTlsIndexAddr) { tlsIndexAddr = dwTlsIndexAddr; };
|
|---|
| 97 | void setTLSInitSize(ULONG dwTlsSize) { tlsInitSize = dwTlsSize; };
|
|---|
| 98 | void setTLSTotalSize(ULONG dwTlsSize) { tlsTotalSize = dwTlsSize; };
|
|---|
| 99 | void setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)
|
|---|
| 100 | {
|
|---|
| 101 | tlsCallBackAddr = dwTlsCallBackAddr;
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | void tlsAttachThread(); //setup TLS structures for new thread
|
|---|
| 105 | void tlsDetachThread(); //destroy TLS structures
|
|---|
| 106 |
|
|---|
| 107 | virtual BOOL insideModule(ULONG address);
|
|---|
| 108 | virtual BOOL insideModuleCode(ULONG address);
|
|---|
| 109 |
|
|---|
| 110 | virtual ULONG getApi(char *name) = 0;
|
|---|
| 111 | virtual ULONG getApi(int ordinal) = 0;
|
|---|
| 112 |
|
|---|
| 113 | virtual ULONG getImageSize();
|
|---|
| 114 |
|
|---|
| 115 | virtual BOOL isDll() = 0;
|
|---|
| 116 |
|
|---|
| 117 | static Win32ImageBase * findModule(HMODULE hModule);
|
|---|
| 118 |
|
|---|
| 119 | //Add image to dependency list of this image
|
|---|
| 120 | void addDependency(Win32DllBase *dll);
|
|---|
| 121 | BOOL dependsOn(Win32DllBase *dll);
|
|---|
| 122 |
|
|---|
| 123 | protected:
|
|---|
| 124 | void tlsAlloc(); //Allocate TLS index for this module
|
|---|
| 125 | void tlsDelete(); //Destroy TLS index for this module
|
|---|
| 126 |
|
|---|
| 127 | ULONG errorState, entryPoint;
|
|---|
| 128 |
|
|---|
| 129 | char *fullpath;
|
|---|
| 130 | char szModule[CCHMAXPATH];
|
|---|
| 131 | char szFileName[CCHMAXPATH];
|
|---|
| 132 |
|
|---|
| 133 | HINSTANCE hinstance;
|
|---|
| 134 |
|
|---|
| 135 | LPVOID tlsAddress; //address of TLS data
|
|---|
| 136 | LPDWORD tlsIndexAddr; //address of DWORD that receives the TLS index
|
|---|
| 137 | ULONG tlsInitSize; //size of initialized TLS memory block
|
|---|
| 138 | ULONG tlsTotalSize; //size of TLS memory block
|
|---|
| 139 | PIMAGE_TLS_CALLBACK *tlsCallBackAddr; //ptr to TLS callback array
|
|---|
| 140 | ULONG tlsIndex; //module TLS index
|
|---|
| 141 |
|
|---|
| 142 | PIMAGE_RESOURCE_DIRECTORY getResSubDirW(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCWSTR lpszName);
|
|---|
| 143 | PIMAGE_RESOURCE_DIRECTORY getResSubDirA(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCTSTR lpszName);
|
|---|
| 144 |
|
|---|
| 145 | PIMAGE_RESOURCE_DATA_ENTRY getResDataLang(PIMAGE_RESOURCE_DIRECTORY pResDir, ULONG language, BOOL fGetDefault = FALSE);
|
|---|
| 146 |
|
|---|
| 147 | PIMAGE_RESOURCE_DIRECTORY pResRootDir;
|
|---|
| 148 |
|
|---|
| 149 | //substracted from RVA data offsets
|
|---|
| 150 | ULONG ulRVAResourceSection;
|
|---|
| 151 |
|
|---|
| 152 | //linked list of dlls loaded on behalf of this executable image (dll or exe)
|
|---|
| 153 | Queue loadedDlls;
|
|---|
| 154 | private:
|
|---|
| 155 |
|
|---|
| 156 | friend class Win32Resource;
|
|---|
| 157 | friend ULONG SYSTEM GetVersionSize(char *modname);
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| 160 | //SvL: This structure is placed at the end of the first page of the image (header
|
|---|
| 161 | // page), so we can determine the Win32Image pointer from a HINSTANCE variable
|
|---|
| 162 | // (which is actually the address of the win32 module)
|
|---|
| 163 | typedef struct
|
|---|
| 164 | {
|
|---|
| 165 | ULONG magic1;
|
|---|
| 166 | Win32ImageBase *image;
|
|---|
| 167 | ULONG magic2;
|
|---|
| 168 | } WINIMAGE_LOOKUP;
|
|---|
| 169 |
|
|---|
| 170 | #define WINIMAGE_LOOKUPADDR(a) (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
|
|---|
| 171 |
|
|---|
| 172 | #endif //__WINIMAGEBASE_H__
|
|---|