1 | /* $Id: winimagebase.h,v 1.12 2000-06-17 09:03:36 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 IDLANG_GETFIRST LANG_GETFIRST
|
---|
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 | virtual HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
|
---|
70 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
|
---|
71 |
|
---|
72 | virtual ULONG getResourceSizeA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
|
---|
73 | ULONG getResourceSizeW(LPCWSTR lpszName, LPWSTR lpszType, ULONG lang = LANG_GETFIRST);
|
---|
74 | virtual BOOL enumResourceNamesA(HMODULE hmod, LPCTSTR lpszType, ENUMRESNAMEPROCA lpEnumFunc, LONG lParam);
|
---|
75 | virtual BOOL enumResourceNamesW(HMODULE hmod, LPCWSTR lpszType, ENUMRESNAMEPROCW lpEnumFunc, LONG lParam);
|
---|
76 | virtual BOOL enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc,
|
---|
77 | LONG lParam);
|
---|
78 | virtual BOOL enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc,
|
---|
79 | LONG lParam);
|
---|
80 |
|
---|
81 | virtual ULONG getVersionSize();
|
---|
82 | virtual 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 | ULONG getPEResourceSize(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
|
---|
143 |
|
---|
144 | PIMAGE_RESOURCE_DATA_ENTRY getPEResourceEntry(ULONG id, ULONG type, ULONG lang, int *error);
|
---|
145 | PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
|
---|
146 | ULONG *nodeData, int level, int *error);
|
---|
147 | PIMAGE_RESOURCE_DIRECTORY getResSubDirW(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCWSTR lpszName);
|
---|
148 | PIMAGE_RESOURCE_DIRECTORY getResSubDirA(PIMAGE_RESOURCE_DIRECTORY pResDir, LPCTSTR lpszName);
|
---|
149 |
|
---|
150 | PIMAGE_RESOURCE_DIRECTORY pResDir;
|
---|
151 |
|
---|
152 | //substracted from RVA data offsets
|
---|
153 | ULONG ulRVAResourceSection;
|
---|
154 |
|
---|
155 | //linked list of dlls loaded on behalf of this executable image (dll or exe)
|
---|
156 | Queue loadedDlls;
|
---|
157 | private:
|
---|
158 |
|
---|
159 | friend class Win32Resource;
|
---|
160 | friend ULONG SYSTEM GetVersionSize(char *modname);
|
---|
161 | };
|
---|
162 |
|
---|
163 | //SvL: This structure is placed at the end of the first page of the image (header
|
---|
164 | // page), so we can determine the Win32Image pointer from a HINSTANCE variable
|
---|
165 | // (which is actually the address of the win32 module)
|
---|
166 | typedef struct
|
---|
167 | {
|
---|
168 | ULONG magic1;
|
---|
169 | Win32ImageBase *image;
|
---|
170 | ULONG magic2;
|
---|
171 | } WINIMAGE_LOOKUP;
|
---|
172 |
|
---|
173 | #define WINIMAGE_LOOKUPADDR(a) (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
|
---|
174 |
|
---|
175 | #endif //__WINIMAGEBASE_H__
|
---|