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