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