| 1 | /* $Id: winimagebase.cpp,v 1.2 1999-09-18 17:47:10 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Win32 PE Image base class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | * Copyright 1998 Knut St. Osmundsen
|
|---|
| 8 | *
|
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 10 | *
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | #define INCL_DOSFILEMGR /* File Manager values */
|
|---|
| 14 | #define INCL_DOSMODULEMGR
|
|---|
| 15 | #define INCL_DOSERRORS /* DOS Error values */
|
|---|
| 16 | #define INCL_DOSPROCESS /* DOS Process values */
|
|---|
| 17 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
|---|
| 18 | #define INCL_WIN
|
|---|
| 19 | #define INCL_BASE
|
|---|
| 20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <string.h>
|
|---|
| 24 | #include <stdlib.h>
|
|---|
| 25 |
|
|---|
| 26 | #include <assert.h>
|
|---|
| 27 | #include <misc.h>
|
|---|
| 28 | #include <win32type.h>
|
|---|
| 29 | #include <winimagebase.h>
|
|---|
| 30 | #include <windllbase.h>
|
|---|
| 31 | #include <winexebase.h>
|
|---|
| 32 | #include <pefile.h>
|
|---|
| 33 | #include <unicode.h>
|
|---|
| 34 | #include <winres.h>
|
|---|
| 35 | #include "oslibmisc.h"
|
|---|
| 36 | #include "oslibdos.h"
|
|---|
| 37 | #include "initterm.h"
|
|---|
| 38 | #include <win\virtual.h>
|
|---|
| 39 |
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | //******************************************************************************
|
|---|
| 42 | Win32ImageBase::Win32ImageBase(HINSTANCE hInstance) :
|
|---|
| 43 | errorState(NO_ERROR), entryPoint(0), fullpath(NULL),
|
|---|
| 44 | tlsAddress(0), tlsIndexAddr(0), tlsInitSize(0), tlsTotalSize(0),
|
|---|
| 45 | tlsCallBackAddr(0), tlsIndex(-1), winres(NULL), pResDir(NULL),
|
|---|
| 46 | pResourceSectionStart(0)
|
|---|
| 47 | {
|
|---|
| 48 | #ifdef DEBUG
|
|---|
| 49 | magic = MAGIC_WINIMAGE;
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | if(hInstance != -1) {
|
|---|
| 53 | this->hinstance = hInstance;
|
|---|
| 54 |
|
|---|
| 55 | char *name = OSLibGetDllName(hinstance);
|
|---|
| 56 | strcpy(szFileName, name);
|
|---|
| 57 | strupr(szFileName);
|
|---|
| 58 |
|
|---|
| 59 | name = strrchr(szFileName, '\\')+1;
|
|---|
| 60 | strcpy(szModule, name);
|
|---|
| 61 |
|
|---|
| 62 | char *dot = strrchr(szModule, '.');
|
|---|
| 63 | if(dot)
|
|---|
| 64 | *dot = 0;
|
|---|
| 65 | }
|
|---|
| 66 | else {
|
|---|
| 67 | szModule[0] = 0;
|
|---|
| 68 | this->hinstance = -1;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 | //******************************************************************************
|
|---|
| 72 | //******************************************************************************
|
|---|
| 73 | Win32ImageBase::~Win32ImageBase()
|
|---|
| 74 | {
|
|---|
| 75 | Win32Resource *res;
|
|---|
| 76 |
|
|---|
| 77 | while(winres)
|
|---|
| 78 | {
|
|---|
| 79 | res = winres->next;
|
|---|
| 80 | delete(winres);
|
|---|
| 81 | winres = res;
|
|---|
| 82 | }
|
|---|
| 83 | if(fullpath)
|
|---|
| 84 | free(fullpath);
|
|---|
| 85 | }
|
|---|
| 86 | //******************************************************************************
|
|---|
| 87 | //******************************************************************************
|
|---|
| 88 | void Win32ImageBase::setFullPath(char *name)
|
|---|
| 89 | {
|
|---|
| 90 | dassert(name, ("setFullPath, name == NULL"));
|
|---|
| 91 | fullpath = (char *)malloc(strlen(name)+1);
|
|---|
| 92 | dassert(fullpath, ("setFullPath, fullpath == NULL"));
|
|---|
| 93 | strcpy(fullpath, name);
|
|---|
| 94 | }
|
|---|
| 95 | //******************************************************************************
|
|---|
| 96 | //******************************************************************************
|
|---|
| 97 | BOOL Win32ImageBase::isPEImage(char *szFileName)
|
|---|
| 98 | {
|
|---|
| 99 | char modname[CCHMAXPATH];
|
|---|
| 100 | char filename[CCHMAXPATH];
|
|---|
| 101 | char *syspath;
|
|---|
| 102 | HFILE dllfile;
|
|---|
| 103 | IMAGE_FILE_HEADER fh;
|
|---|
| 104 | HFILE win32handle;
|
|---|
| 105 | ULONG ulAction = 0; /* Action taken by DosOpen */
|
|---|
| 106 | ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
|
|---|
| 107 | APIRET rc = NO_ERROR; /* Return code */
|
|---|
| 108 | LPVOID win32file = NULL;
|
|---|
| 109 | ULONG ulRead;
|
|---|
| 110 | int nSections, i;
|
|---|
| 111 |
|
|---|
| 112 | strcpy(filename, szFileName);
|
|---|
| 113 | strupr(filename);
|
|---|
| 114 | if(!strchr(filename, (int)'.')) {
|
|---|
| 115 | strcat(filename,".DLL");
|
|---|
| 116 | }
|
|---|
| 117 | dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
|
|---|
| 118 | if(dllfile == NULL) {//search in libpath for dll
|
|---|
| 119 | syspath = getenv("WIN32LIBPATH");
|
|---|
| 120 | if(syspath) {
|
|---|
| 121 | strcpy(modname, syspath);
|
|---|
| 122 | if(modname[strlen(modname)-1] != '\\') {
|
|---|
| 123 | strcat(modname, "\\");
|
|---|
| 124 | }
|
|---|
| 125 | strcat(modname, filename);
|
|---|
| 126 | strcpy(filename, modname);
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | else OSLibDosClose(dllfile);
|
|---|
| 130 |
|
|---|
| 131 | rc = DosOpen(filename, /* File path name */
|
|---|
| 132 | &win32handle, /* File handle */
|
|---|
| 133 | &ulAction, /* Action taken */
|
|---|
| 134 | 0L, /* File primary allocation */
|
|---|
| 135 | 0L, /* File attribute */
|
|---|
| 136 | OPEN_ACTION_FAIL_IF_NEW |
|
|---|
| 137 | OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
|
|---|
| 138 | OPEN_FLAGS_NOINHERIT |
|
|---|
| 139 | OPEN_SHARE_DENYNONE |
|
|---|
| 140 | OPEN_ACCESS_READONLY, /* Open mode of the file */
|
|---|
| 141 | 0L); /* No extended attribute */
|
|---|
| 142 |
|
|---|
| 143 | if (rc != NO_ERROR)
|
|---|
| 144 | {
|
|---|
| 145 | dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
|
|---|
| 146 | szFileName, rc));
|
|---|
| 147 | return(FALSE);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /* Move the file pointer back to the beginning of the file */
|
|---|
| 151 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
|---|
| 152 |
|
|---|
| 153 | IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
|
|---|
| 154 | if(pdoshdr == NULL) {
|
|---|
| 155 | DosClose(win32handle); /* Close the file */
|
|---|
| 156 | return(FALSE);
|
|---|
| 157 | }
|
|---|
| 158 | rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
|
|---|
| 159 | if(rc != NO_ERROR) {
|
|---|
| 160 | DosClose(win32handle); /* Close the file */
|
|---|
| 161 | return(FALSE);
|
|---|
| 162 | }
|
|---|
| 163 | ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
|
|---|
| 164 | free(pdoshdr);
|
|---|
| 165 |
|
|---|
| 166 | /* Move the file pointer back to the beginning of the file */
|
|---|
| 167 | DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
|
|---|
| 168 |
|
|---|
| 169 | win32file = malloc(hdrsize);
|
|---|
| 170 | if(win32file == NULL) {
|
|---|
| 171 | DosClose(win32handle); /* Close the file */
|
|---|
| 172 | return(FALSE);
|
|---|
| 173 | }
|
|---|
| 174 | rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
|
|---|
| 175 | if(rc != NO_ERROR) {
|
|---|
| 176 | goto failure;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if(GetPEFileHeader (win32file, &fh) == FALSE) {
|
|---|
| 180 | goto failure;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
|
|---|
| 184 | goto failure;
|
|---|
| 185 | }
|
|---|
| 186 | if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
|
|---|
| 187 | goto failure;
|
|---|
| 188 | }
|
|---|
| 189 | //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
|
|---|
| 190 | if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
|
|---|
| 191 | goto failure;
|
|---|
| 192 | }
|
|---|
| 193 | DosClose(win32handle);
|
|---|
| 194 | return(TRUE);
|
|---|
| 195 |
|
|---|
| 196 | failure:
|
|---|
| 197 | free(win32file);
|
|---|
| 198 | DosClose(win32handle);
|
|---|
| 199 | return(FALSE);
|
|---|
| 200 | }
|
|---|
| 201 | //******************************************************************************
|
|---|
| 202 | //******************************************************************************
|
|---|