source: trunk/src/kernel32/winimagebase.cpp@ 1570

Last change on this file since 1570 was 1475, checked in by sandervl, 26 years ago

Color cursor changes + dll loading fixes

File size: 5.9 KB
Line 
1/* $Id: winimagebase.cpp,v 1.3 1999-10-27 10:35:42 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//******************************************************************************
42Win32ImageBase::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//******************************************************************************
73Win32ImageBase::~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//******************************************************************************
88void 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//******************************************************************************
97BOOL 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 strcpy(modname, kernel32Path);
120 strcat(modname, filename);
121 strcpy(filename, modname);
122 }
123 else OSLibDosClose(dllfile);
124
125 rc = DosOpen(filename, /* File path name */
126 &win32handle, /* File handle */
127 &ulAction, /* Action taken */
128 0L, /* File primary allocation */
129 0L, /* File attribute */
130 OPEN_ACTION_FAIL_IF_NEW |
131 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
132 OPEN_FLAGS_NOINHERIT |
133 OPEN_SHARE_DENYNONE |
134 OPEN_ACCESS_READONLY, /* Open mode of the file */
135 0L); /* No extended attribute */
136
137 if (rc != NO_ERROR)
138 {
139 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
140 szFileName, rc));
141 return(FALSE);
142 }
143
144 /* Move the file pointer back to the beginning of the file */
145 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
146
147 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
148 if(pdoshdr == NULL) {
149 DosClose(win32handle); /* Close the file */
150 return(FALSE);
151 }
152 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
153 if(rc != NO_ERROR) {
154 DosClose(win32handle); /* Close the file */
155 return(FALSE);
156 }
157 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
158 free(pdoshdr);
159
160 /* Move the file pointer back to the beginning of the file */
161 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
162
163 win32file = malloc(hdrsize);
164 if(win32file == NULL) {
165 DosClose(win32handle); /* Close the file */
166 return(FALSE);
167 }
168 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
169 if(rc != NO_ERROR) {
170 goto failure;
171 }
172
173 if(GetPEFileHeader (win32file, &fh) == FALSE) {
174 goto failure;
175 }
176
177 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
178 goto failure;
179 }
180 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
181 goto failure;
182 }
183 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
184 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
185 goto failure;
186 }
187 DosClose(win32handle);
188 return(TRUE);
189
190failure:
191 free(win32file);
192 DosClose(win32handle);
193 return(FALSE);
194}
195//******************************************************************************
196//******************************************************************************
Note: See TracBrowser for help on using the repository browser.