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