[4] | 1 | /* $Id: winimage.h,v 1.1 1999-05-24 20:19:07 ktk Exp $ */
|
---|
| 2 |
|
---|
| 3 | /*
|
---|
| 4 | *
|
---|
| 5 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 6 | *
|
---|
| 7 | */
|
---|
| 8 | /*
|
---|
| 9 | * Win32 PE Image class
|
---|
| 10 | *
|
---|
| 11 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 12 | *
|
---|
| 13 | */
|
---|
| 14 | #ifndef __WINIMAGE_H__
|
---|
| 15 | #define __WINIMAGE_H__
|
---|
| 16 |
|
---|
| 17 | #include <peexe.h>
|
---|
| 18 |
|
---|
| 19 | #define MAX_RES 17
|
---|
| 20 | extern char *ResTypes[MAX_RES];
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | #ifndef __PE2LX__
|
---|
| 24 |
|
---|
| 25 | #ifdef DEBUG
|
---|
| 26 | #define MAGIC_WINIMAGE 0x11223344
|
---|
| 27 | #endif
|
---|
| 28 |
|
---|
| 29 | #pragma pack(1)
|
---|
| 30 | typedef struct {
|
---|
| 31 | int id;
|
---|
| 32 | char name[1];
|
---|
| 33 | } NameId;
|
---|
| 34 | #pragma pack()
|
---|
| 35 |
|
---|
| 36 | #define ERROR_INTERNAL 1
|
---|
| 37 |
|
---|
| 38 | #define SECTION_CODE 1
|
---|
| 39 | #define SECTION_INITDATA 2
|
---|
| 40 | #define SECTION_UNINITDATA 4
|
---|
| 41 | #define SECTION_READONLYDATA 8
|
---|
| 42 | #define SECTION_IMPORT 16
|
---|
| 43 | #define SECTION_RESOURCE 32
|
---|
| 44 | #define SECTION_RELOC 64
|
---|
| 45 | #define SECTION_EXPORT 128
|
---|
| 46 | #define SECTION_DEBUG 256
|
---|
| 47 |
|
---|
| 48 | #define PAGE_SIZE 4096
|
---|
| 49 |
|
---|
| 50 | #define MAX_SECTION 64 /*PLF Mon 98-02-09 23:47:16*/
|
---|
| 51 |
|
---|
| 52 | #define LANG_GETFIRST 0x80000000
|
---|
| 53 |
|
---|
| 54 | #define NO_NAMETABLE 0x77777777
|
---|
| 55 | #define NO_LOOKUPTABLE 0x888888
|
---|
| 56 | #define GET_CONSOLE(a) (a >> 24)
|
---|
| 57 | #define SET_CONSOLE(a) (a << 24)
|
---|
| 58 |
|
---|
| 59 | typedef struct {
|
---|
| 60 | char *rawdata;
|
---|
| 61 | ULONG rawsize;
|
---|
| 62 | ULONG virtaddr;
|
---|
| 63 | ULONG realvirtaddr; //as allocated in OS/2
|
---|
| 64 | ULONG virtualsize;
|
---|
| 65 | ULONG type;
|
---|
| 66 | } Section;
|
---|
| 67 |
|
---|
| 68 | typedef struct {
|
---|
| 69 | ULONG virtaddr;
|
---|
| 70 | ULONG ordinal;
|
---|
| 71 | ULONG nlength;
|
---|
| 72 | char name[4];
|
---|
| 73 | } NameExport;
|
---|
| 74 |
|
---|
| 75 | typedef struct {
|
---|
| 76 | ULONG virtaddr;
|
---|
| 77 | ULONG ordinal;
|
---|
| 78 | } OrdExport;
|
---|
| 79 |
|
---|
| 80 | class Win32Dll;
|
---|
| 81 | class Win32Resource;
|
---|
| 82 |
|
---|
| 83 | class Win32Image
|
---|
| 84 | {
|
---|
| 85 | #ifdef DEBUG
|
---|
| 86 | protected:
|
---|
| 87 | DWORD magic;
|
---|
| 88 | public:
|
---|
| 89 | void checkObject()
|
---|
| 90 | {
|
---|
| 91 | if(magic != MAGIC_WINIMAGE) {
|
---|
| 92 | eprintf(("Corrupt this pointer %X %X!!", this, magic));
|
---|
| 93 | DebugInt3();
|
---|
| 94 | }
|
---|
| 95 | };
|
---|
| 96 | #endif
|
---|
| 97 |
|
---|
| 98 | public:
|
---|
| 99 | // Constructors and destructors
|
---|
| 100 | Win32Image(HINSTANCE hinstance, int NameTableId, int Win32TableId);
|
---|
| 101 | Win32Image(char *szFileName);
|
---|
| 102 | virtual ~Win32Image();
|
---|
| 103 |
|
---|
| 104 | //called to reset object to native OS/2 or converted win32 dll
|
---|
| 105 | void OS2ImageInit(HINSTANCE hinstance, int NameTableId, int Win32TableId);
|
---|
| 106 |
|
---|
| 107 | virtual BOOL init();
|
---|
| 108 |
|
---|
| 109 | ULONG getError() { return errorState; };
|
---|
| 110 | HINSTANCE getInstanceHandle() { return hinstance; };
|
---|
| 111 |
|
---|
| 112 | virtual void setFullPath(char *name);
|
---|
| 113 | char *getFullPath() { return fullpath; };
|
---|
| 114 |
|
---|
| 115 | HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType);
|
---|
| 116 | HRSRC findResourceW(LPWSTR lpszName, LPWSTR lpszType);
|
---|
| 117 |
|
---|
| 118 | int getWin32ResourceId(int id);
|
---|
| 119 | int convertNameId(char *lpszName);
|
---|
| 120 |
|
---|
| 121 | static BOOL isPEImage(char *szFileName);
|
---|
| 122 |
|
---|
| 123 | void setVersionId(int id) { VersionId = id; };
|
---|
| 124 | int getVersionId() { return VersionId; };
|
---|
| 125 |
|
---|
| 126 | protected:
|
---|
| 127 | void StoreImportByOrd(Win32Dll *WinDll, ULONG ordinal, ULONG impaddr);
|
---|
| 128 | void StoreImportByName(Win32Dll *WinDll, char *impname, ULONG impaddr);
|
---|
| 129 |
|
---|
| 130 | void addSection(ULONG type, char *rawdata, ULONG rawsize, ULONG virtaddress, ULONG virtsize);
|
---|
| 131 | BOOL allocSections();
|
---|
| 132 | BOOL allocFixedMem();
|
---|
| 133 |
|
---|
| 134 | BOOL storeSections();
|
---|
| 135 | BOOL setMemFlags();
|
---|
| 136 | BOOL setFixups(PIMAGE_BASE_RELOCATION prel);
|
---|
| 137 | void AddOff32Fixup(ULONG fixupaddr);
|
---|
| 138 | void AddOff16Fixup(ULONG fixupaddr, BOOL fHighFixup);
|
---|
| 139 |
|
---|
| 140 | BOOL processImports(char *win32file);
|
---|
| 141 |
|
---|
| 142 | BOOL processExports(char *win32file);
|
---|
| 143 | void AddNameExport(ULONG virtaddr, char *apiname, ULONG ordinal);
|
---|
| 144 | void AddOrdExport(ULONG virtaddr, ULONG ordinal);
|
---|
| 145 |
|
---|
| 146 | Win32Resource *getPEResource(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
|
---|
| 147 | PIMAGE_RESOURCE_DATA_ENTRY ProcessResSubDir(PIMAGE_RESOURCE_DIRECTORY prdType,
|
---|
| 148 | ULONG *nodeData);
|
---|
| 149 | PIMAGE_RESOURCE_DIRECTORY pResDir;
|
---|
| 150 | Section *pResSection;
|
---|
| 151 | Win32Resource *winres;
|
---|
| 152 |
|
---|
| 153 | IMAGE_OPTIONAL_HEADER oh;
|
---|
| 154 | IMAGE_FILE_HEADER fh;
|
---|
| 155 |
|
---|
| 156 | ULONG errorState, entryPoint;
|
---|
| 157 | ULONG nrNameExports, nameExportSize;
|
---|
| 158 | ULONG nrOrdExports;
|
---|
| 159 | NameExport *nameexports, *curnameexport;
|
---|
| 160 | OrdExport *ordexports, *curordexport;
|
---|
| 161 |
|
---|
| 162 | ULONG nrsections, imageSize, imageVirtBase, imageVirtEnd;
|
---|
| 163 | //OS/2 virtual base address
|
---|
| 164 | ULONG baseAddress, realBaseAddress;
|
---|
| 165 | Section section[MAX_SECTION];
|
---|
| 166 |
|
---|
| 167 | char *szFileName, *fullpath;
|
---|
| 168 |
|
---|
| 169 | HINSTANCE hinstance;
|
---|
| 170 |
|
---|
| 171 | int NameTableId;
|
---|
| 172 | int Win32TableId;
|
---|
| 173 | int VersionId;
|
---|
| 174 |
|
---|
| 175 | ULONG *Win32Table;
|
---|
| 176 | NameId *NameTable;
|
---|
| 177 |
|
---|
| 178 | BOOL fNativePEImage;
|
---|
| 179 | private:
|
---|
| 180 |
|
---|
| 181 | friend class Win32Resource;
|
---|
| 182 | friend ULONG SYSTEM GetVersionSize(char *modname);
|
---|
| 183 | };
|
---|
| 184 |
|
---|
| 185 | #include <iostream.h>
|
---|
| 186 | #include <fstream.h>
|
---|
| 187 | extern ofstream fout;
|
---|
| 188 |
|
---|
| 189 | #endif //__PE2LX__
|
---|
| 190 |
|
---|
| 191 | #endif
|
---|