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

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

Rewrite for new win32 image classes

File size: 6.0 KB
Line 
1/* $Id: winimagebase.cpp,v 1.1 1999-09-15 23:39:07 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)
46{
47#ifdef DEBUG
48 magic = MAGIC_WINIMAGE;
49#endif
50
51 if(hInstance != -1) {
52 this->hinstance = hInstance;
53
54 char *name = OSLibGetDllName(hinstance);
55 strcpy(szFileName, name);
56 strupr(szFileName);
57
58 name = strrchr(szFileName, '\\')+1;
59 strcpy(szModule, name);
60
61 char *dot = strrchr(szModule, '.');
62 if(dot)
63 *dot = 0;
64 }
65 else {
66 szModule[0] = 0;
67 this->hinstance = -1;
68 }
69}
70//******************************************************************************
71//******************************************************************************
72Win32ImageBase::~Win32ImageBase()
73{
74 Win32Resource *res;
75
76 while(winres)
77 {
78 res = winres->next;
79 delete(winres);
80 winres = res;
81 }
82 if(fullpath)
83 free(fullpath);
84}
85//******************************************************************************
86//******************************************************************************
87void Win32ImageBase::setFullPath(char *name)
88{
89 dassert(name, ("setFullPath, name == NULL"));
90 fullpath = (char *)malloc(strlen(name)+1);
91 dassert(fullpath, ("setFullPath, fullpath == NULL"));
92 strcpy(fullpath, name);
93}
94//******************************************************************************
95//******************************************************************************
96BOOL Win32ImageBase::isPEImage(char *szFileName)
97{
98 char modname[CCHMAXPATH];
99 char filename[CCHMAXPATH];
100 char *syspath;
101 HFILE dllfile;
102 IMAGE_FILE_HEADER fh;
103 HFILE win32handle;
104 ULONG ulAction = 0; /* Action taken by DosOpen */
105 ULONG ulLocal = 0; /* File pointer position after DosSetFilePtr */
106 APIRET rc = NO_ERROR; /* Return code */
107 LPVOID win32file = NULL;
108 ULONG ulRead;
109 int nSections, i;
110
111 strcpy(filename, szFileName);
112 strupr(filename);
113 if(!strchr(filename, (int)'.')) {
114 strcat(filename,".DLL");
115 }
116 dllfile = OSLibDosOpen(filename, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
117 if(dllfile == NULL) {//search in libpath for dll
118 syspath = getenv("WIN32LIBPATH");
119 if(syspath) {
120 strcpy(modname, syspath);
121 if(modname[strlen(modname)-1] != '\\') {
122 strcat(modname, "\\");
123 }
124 strcat(modname, filename);
125 strcpy(filename, modname);
126 }
127 }
128 else OSLibDosClose(dllfile);
129
130 rc = DosOpen(filename, /* File path name */
131 &win32handle, /* File handle */
132 &ulAction, /* Action taken */
133 0L, /* File primary allocation */
134 0L, /* File attribute */
135 OPEN_ACTION_FAIL_IF_NEW |
136 OPEN_ACTION_OPEN_IF_EXISTS, /* Open function type */
137 OPEN_FLAGS_NOINHERIT |
138 OPEN_SHARE_DENYNONE |
139 OPEN_ACCESS_READONLY, /* Open mode of the file */
140 0L); /* No extended attribute */
141
142 if (rc != NO_ERROR)
143 {
144 dprintf(("KERNEL32:Win32ImageBase::isPEImage(%s) failed with %u\n",
145 szFileName, rc));
146 return(FALSE);
147 }
148
149 /* Move the file pointer back to the beginning of the file */
150 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
151
152 IMAGE_DOS_HEADER *pdoshdr = (IMAGE_DOS_HEADER *)malloc(sizeof(IMAGE_DOS_HEADER));
153 if(pdoshdr == NULL) {
154 DosClose(win32handle); /* Close the file */
155 return(FALSE);
156 }
157 rc = DosRead(win32handle, pdoshdr, sizeof(IMAGE_DOS_HEADER), &ulRead);
158 if(rc != NO_ERROR) {
159 DosClose(win32handle); /* Close the file */
160 return(FALSE);
161 }
162 ULONG hdrsize = pdoshdr->e_lfanew + SIZE_OF_NT_SIGNATURE + sizeof(IMAGE_FILE_HEADER);
163 free(pdoshdr);
164
165 /* Move the file pointer back to the beginning of the file */
166 DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
167
168 win32file = malloc(hdrsize);
169 if(win32file == NULL) {
170 DosClose(win32handle); /* Close the file */
171 return(FALSE);
172 }
173 rc = DosRead(win32handle, win32file, hdrsize, &ulRead);
174 if(rc != NO_ERROR) {
175 goto failure;
176 }
177
178 if(GetPEFileHeader (win32file, &fh) == FALSE) {
179 goto failure;
180 }
181
182 if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
183 goto failure;
184 }
185 if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
186 goto failure;
187 }
188 //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
189 if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
190 goto failure;
191 }
192 DosClose(win32handle);
193 return(TRUE);
194
195failure:
196 free(win32file);
197 DosClose(win32handle);
198 return(FALSE);
199}
200//******************************************************************************
201//******************************************************************************
Note: See TracBrowser for help on using the repository browser.