1 | /* $Id: pe2lxmain.cpp,v 1.7 2001-02-10 11:11:48 bird Exp $
|
---|
2 | *
|
---|
3 | * Pe2Lx main program. (Ring 3 only!)
|
---|
4 | *
|
---|
5 | * Copyright (c) 1999 knut st. osmundsen
|
---|
6 | *
|
---|
7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*******************************************************************************
|
---|
12 | * Defined Constants And Macros *
|
---|
13 | *******************************************************************************/
|
---|
14 | #define FOR_EXEHDR 1
|
---|
15 | #define INCL_BASE
|
---|
16 | #define INCL_DOSFILEMGR
|
---|
17 | #define INCL_DOSERRORS
|
---|
18 | #define INCL_OS2KRNL_LDR
|
---|
19 |
|
---|
20 | #define DATA16_GLOBAL
|
---|
21 | #define OUTPUT_COM2 0x2f8
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <os2.h>
|
---|
27 | #include <peexe.h>
|
---|
28 | #include <neexe.h>
|
---|
29 | #include <newexe.h>
|
---|
30 | #include <exe386.h>
|
---|
31 | #include <malloc.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include "OS2Krnl.h"
|
---|
35 | #include "modulebase.h"
|
---|
36 | #include "pe2lx.h"
|
---|
37 | #include "options.h"
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <versionos2.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *******************************************************************************/
|
---|
45 | char szBackupWin32Filename[CCHMAXPATH]; /* too save stack/heap */
|
---|
46 | struct options options = DEFAULT_OPTION_ASSIGMENTS;
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Internal Functions *
|
---|
50 | *******************************************************************************/
|
---|
51 | static void syntax();
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Main function of the Pe2Lx utillity.
|
---|
56 | * @returns 0 on success.
|
---|
57 | * 1 Help.
|
---|
58 | * 2 Syntax error: Invalid argument, '<arg>'.
|
---|
59 | * 3 Syntax error: Too many filenames.
|
---|
60 | * 4 Syntax error: No Win32-file specified.
|
---|
61 | * 80 Fatal error: Can't find Win32-file, <filename>.
|
---|
62 | * 81 Fatal error: Failed to rename the Win32-file, <from> -> <to>
|
---|
63 | * 82 Fatal error: Failed to open Win32-file, <filename>. rc=<rc>
|
---|
64 | * 83 Fatal error: Failed to convertert the file. rc=<rc from init(..)>
|
---|
65 | * 84 Fatal error: Failed to write the Lx-file. rc=<rc from writeFile(..)>
|
---|
66 | * @param argc Count of arguments.
|
---|
67 | * @param argv Array of argument pointers. argc entries.
|
---|
68 | * @status completely implemented.
|
---|
69 | * @author knut st. osmundsen
|
---|
70 | */
|
---|
71 | int main(int argc, char **argv)
|
---|
72 | {
|
---|
73 | APIRET rc;
|
---|
74 | ULONG ulAction = 0;
|
---|
75 | PCSZ pszWin32Filename = NULL;
|
---|
76 | PCSZ pszOdin32Filename = NULL;
|
---|
77 | PCSZ psz;
|
---|
78 | int argi;
|
---|
79 |
|
---|
80 | /* special edition for Dave Evans */
|
---|
81 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
---|
82 |
|
---|
83 | /* read parameters */
|
---|
84 | for (argi = 1; argi < argc; argi++)
|
---|
85 | {
|
---|
86 | /* check if option or filname */
|
---|
87 | if (argv[argi][0] == '-' || argv[argi][0] == '/')
|
---|
88 | { /* option */
|
---|
89 | switch (argv[argi][1])
|
---|
90 | {
|
---|
91 | case '1': /* All-In-One-Object fix - temporary...- -1<-|+|*> */
|
---|
92 | if (argv[argi][2] == '-')
|
---|
93 | options.fPEOneObject = FLAGS_PEOO_DISABLED;
|
---|
94 | else if (argv[argi][2] == '+')
|
---|
95 | options.fPEOneObject = FLAGS_PEOO_ENABLED;
|
---|
96 | else
|
---|
97 | options.fPEOneObject = FLAGS_PEOO_FORCED;
|
---|
98 | break;
|
---|
99 |
|
---|
100 |
|
---|
101 | case 'h': /* syntax help */
|
---|
102 | case 'H':
|
---|
103 | case '?':
|
---|
104 | syntax();
|
---|
105 | return 1;
|
---|
106 |
|
---|
107 | case 'w': /* warning level */
|
---|
108 | case 'W':
|
---|
109 | psz = argv[argi] + (argv[argi][2] == ':' || argv[argi][2] == '=') + 2;
|
---|
110 | if (*psz >= '0' && *psz <= '4' && psz[1] == '\0')
|
---|
111 | {
|
---|
112 | switch (*psz)
|
---|
113 | {
|
---|
114 | case '0': ModuleBase::ulInfoLevel = ModuleBase::Quiet; break;
|
---|
115 | case '1': ModuleBase::ulInfoLevel = ModuleBase::Error; break;
|
---|
116 | case '2': ModuleBase::ulInfoLevel = ModuleBase::Warning; break;
|
---|
117 | case '3': ModuleBase::ulInfoLevel = ModuleBase::Info; break;
|
---|
118 | case '4': ModuleBase::ulInfoLevel = ModuleBase::InfoAll; break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | printf("Syntax error: Incorrect use of argument '%.2s'.\n\n", argv[argi]);
|
---|
124 | return 5;
|
---|
125 | }
|
---|
126 | break;
|
---|
127 |
|
---|
128 | default:
|
---|
129 | printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
|
---|
130 | syntax();
|
---|
131 | return 2;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | if (pszWin32Filename == NULL)
|
---|
137 | {
|
---|
138 | pszWin32Filename = argv[argi];
|
---|
139 | /* check if exists */
|
---|
140 | rc = DosQueryPathInfo(pszWin32Filename,FIL_QUERYFULLNAME,
|
---|
141 | &szBackupWin32Filename[0], sizeof(szBackupWin32Filename));
|
---|
142 | if (rc != NO_ERROR)
|
---|
143 | {
|
---|
144 | printf("Fatal error: Can't find Win32-file, '%s'.\n", pszWin32Filename);
|
---|
145 | return 80;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | else if (pszOdin32Filename == NULL)
|
---|
149 | pszOdin32Filename = argv[argi];
|
---|
150 | else
|
---|
151 | {
|
---|
152 | printf("Syntax error: Too many filenames!\n");
|
---|
153 | syntax();
|
---|
154 | return 3;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | } /* for */
|
---|
158 |
|
---|
159 | /* check if enough arguments */
|
---|
160 | if (pszWin32Filename == NULL)
|
---|
161 | {
|
---|
162 | printf("Syntax error: No Win32-file specified.\n\n");
|
---|
163 | syntax();
|
---|
164 | return 4;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* rename files? */
|
---|
168 | if (pszOdin32Filename == NULL)
|
---|
169 | {
|
---|
170 | char *pszExt = strrchr(pszWin32Filename, '.');
|
---|
171 | if (pszExt == NULL)
|
---|
172 | {
|
---|
173 | printf("warning: Win32-file don't have an extention\n");
|
---|
174 | strcpy(szBackupWin32Filename, pszWin32Filename);
|
---|
175 | strcat(szBackupWin32Filename, "wbk");
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | memset(szBackupWin32Filename, 0, sizeof(szBackupWin32Filename));
|
---|
180 | if (stricmp(pszExt + 1, "exe") == 0)
|
---|
181 | {
|
---|
182 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 1);
|
---|
183 | strcat(szBackupWin32Filename, "exf");
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 3);
|
---|
188 | strcat(szBackupWin32Filename, "k");
|
---|
189 | }
|
---|
190 | }
|
---|
191 | rc = DosMove(pszWin32Filename, szBackupWin32Filename);
|
---|
192 | if (rc != NO_ERROR)
|
---|
193 | {
|
---|
194 | printf("Fatal error: Failed to rename the Win32-file, %s -> %s\n",
|
---|
195 | pszWin32Filename, szBackupWin32Filename);
|
---|
196 | return 81;
|
---|
197 | }
|
---|
198 | printInf(("Backuped %s to %s\n", pszWin32Filename, szBackupWin32Filename));
|
---|
199 |
|
---|
200 | /* switch name */
|
---|
201 | pszOdin32Filename = pszWin32Filename;
|
---|
202 | pszWin32Filename = szBackupWin32Filename;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /* open input file */
|
---|
206 | HFILE hFileWin32;
|
---|
207 |
|
---|
208 | rc = DosOpen(pszWin32Filename, &hFileWin32, &ulAction, 0UL,
|
---|
209 | FILE_NORMAL,
|
---|
210 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
---|
211 | OPEN_FLAGS_RANDOMSEQUENTIAL | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
---|
212 | NULL);
|
---|
213 | if (rc == NO_ERROR)
|
---|
214 | {
|
---|
215 | Pe2Lx pe2lx(hFileWin32);
|
---|
216 | rc = pe2lx.init(pszOdin32Filename);
|
---|
217 | if (rc == NO_ERROR)
|
---|
218 | {
|
---|
219 | rc = pe2lx.writeFile(pszOdin32Filename);
|
---|
220 | if (rc != NO_ERROR)
|
---|
221 | {
|
---|
222 | printf("Fatal error: Failed to write the Lx-file. rc=%d\n", rc);
|
---|
223 | rc = 84;
|
---|
224 | }
|
---|
225 | pe2lx.dumpVirtualLxFile();
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | printf("Fatal error: Failed to convertert the file. rc=%d\n", rc);
|
---|
230 | rc = 83;
|
---|
231 | }
|
---|
232 | DosClose(hFileWin32);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | {
|
---|
236 | printf("Fatal error: Failed to open Win32-file, %s. rc=%d\n",
|
---|
237 | pszWin32Filename, rc);
|
---|
238 | rc = 82;
|
---|
239 | }
|
---|
240 | return (int)rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Display syntax for this program.
|
---|
246 | * @status completely implemented.
|
---|
247 | * @author knut st. osmundsen
|
---|
248 | */
|
---|
249 | static void syntax()
|
---|
250 | {
|
---|
251 | printf("Syntax: pe2lx.exe [-W<0|1|2|3>] [-1<+|-|[*]>] <Win32File> [Odin32File]\n"
|
---|
252 | "\n"
|
---|
253 | " -W<0|1|2|3|4> Message filter level.\n"
|
---|
254 | " -W0: Output only severe and unrecoverable error messages.\n"
|
---|
255 | " -W1: Output error, severe and unrecoverable error messages.\n"
|
---|
256 | " -W2: Output warning, error, severe and unrecoverable error\n"
|
---|
257 | " messages.\n"
|
---|
258 | " -W3: Output nearly all messages.\n"
|
---|
259 | " -W4: Output absolutely all messages.\n"
|
---|
260 | " Default: -W3\n"
|
---|
261 | " -1<+|-|[*]> All-in-one-object fix.\n"
|
---|
262 | " +: Fix applied when necessary.\n"
|
---|
263 | " -: Disabled. Never applied.\n"
|
---|
264 | " *: Forced. Applied every time.\n"
|
---|
265 | " Default: -1*\n"
|
---|
266 | " Win32File Input Win32 Exe, Dll or other Win32 PE file.\n"
|
---|
267 | " Odin32File Output Odin32-file. If not specified the Win32-file is\n"
|
---|
268 | " renamed and the Odin32-file will use the original name\n"
|
---|
269 | " of the Win32-file.\n"
|
---|
270 | " Pe2Lx version 0.%02d\n",
|
---|
271 | PE2LX_VERSION
|
---|
272 | );
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 | #if 0
|
---|
278 | /**
|
---|
279 | * Debug - see how much of the stack that have been used.
|
---|
280 | * Padd stack, and look in the debug storage view on program end.
|
---|
281 | * @param
|
---|
282 | * @status
|
---|
283 | * @author knut st. osmundsen
|
---|
284 | */
|
---|
285 | static void initStack()
|
---|
286 | {
|
---|
287 | PTIB pTib;
|
---|
288 | PPIB pPib;
|
---|
289 |
|
---|
290 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
291 | memset((PVOID)pTib->tib_pstack, 'k', (size_t)&pTib - 0x30 - (size_t)pTib->tib_pstack);
|
---|
292 | }
|
---|
293 | #endif
|
---|