| 1 | /* $Id: pe2lx.cpp,v 1.1 1999-09-06 02:20:08 bird Exp $
|
|---|
| 2 | *
|
|---|
| 3 | * PE2LX - Ring-3 standard PE2LX using LXHeader.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1998-1999 knut St. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | /*******************************************************************************
|
|---|
| 10 | * Header Files *
|
|---|
| 11 | *******************************************************************************/
|
|---|
| 12 | #include "pe2lx.h"
|
|---|
| 13 | #include "misc.h"
|
|---|
| 14 | #include "options.h"
|
|---|
| 15 | #include <versionos2.h>
|
|---|
| 16 |
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 |
|
|---|
| 19 | /*******************************************************************************
|
|---|
| 20 | * Global Variables *
|
|---|
| 21 | *******************************************************************************/
|
|---|
| 22 | static char INFO_BANNER[] =
|
|---|
| 23 | "Usage: PE2LX winfile \n\
|
|---|
| 24 | OR\n\
|
|---|
| 25 | PE2LX winfile os2file\n";
|
|---|
| 26 |
|
|---|
| 27 | struct options options;
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | int main(int argc, char **argv)
|
|---|
| 31 | {
|
|---|
| 32 | if (heapInit(HEAP_SIZE) == 0)
|
|---|
| 33 | cout << "error! no heap!" << endl;
|
|---|
| 34 | LXFile *lxfile;
|
|---|
| 35 | char szbuffer[196];
|
|---|
| 36 | int offset;
|
|---|
| 37 | HFILE hFile = 0;
|
|---|
| 38 | ULONG ulAction = 0;
|
|---|
| 39 | ULONG ulRead;
|
|---|
| 40 | int rc;
|
|---|
| 41 | char szFilename[CCHMAXPATH];
|
|---|
| 42 |
|
|---|
| 43 | /* dump to file */
|
|---|
| 44 | char szBuffer[1024];
|
|---|
| 45 | FILE *outfile;
|
|---|
| 46 | ULONG ulOffset;
|
|---|
| 47 | ULONG ulBytesRead;
|
|---|
| 48 | char *pBuffer;
|
|---|
| 49 |
|
|---|
| 50 | SET_OPTIONS_PE2LX(options);
|
|---|
| 51 |
|
|---|
| 52 | cout << "PE2LX Special Edition v0.0." << PE2LX_VERSION <<"alpha (for debugging ring 0 code in ring 3 debug)"<<endl<<endl;
|
|---|
| 53 | if (argc != 2 && argc != 3)
|
|---|
| 54 | {
|
|---|
| 55 | cout << INFO_BANNER;
|
|---|
| 56 | return -1;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | rc = DosOpen(argv[1], &hFile, &ulAction, 0,
|
|---|
| 62 | FILE_NORMAL,
|
|---|
| 63 | OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
|
|---|
| 64 | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
|
|---|
| 65 | NULL);
|
|---|
| 66 | if (rc == NO_ERROR)
|
|---|
| 67 | {
|
|---|
| 68 | rc = DosRead(hFile,szbuffer,0x40,&ulRead);
|
|---|
| 69 | if (rc == NO_ERROR)
|
|---|
| 70 | {
|
|---|
| 71 | offset = *(PULONG)(szbuffer+0x3c);
|
|---|
| 72 | rc = DosSetFilePtr(hFile, offset, FILE_BEGIN, &ulRead);
|
|---|
| 73 | rc = DosRead(hFile, szbuffer, 196, &ulRead);
|
|---|
| 74 | if (rc == NO_ERROR)
|
|---|
| 75 | {
|
|---|
| 76 | rc = -1;
|
|---|
| 77 | lxfile = new LXFile;
|
|---|
| 78 | lxfile->SetModuleName(argv[1]);
|
|---|
| 79 | if (lxfile->init(hFile, (PIMAGE_SIG_FILE_HEADER)szbuffer, offset))
|
|---|
| 80 | {
|
|---|
| 81 | //ULONG read(ULONG ulOffset, PVOID pBuffer, ULONG ulBytesToRead);
|
|---|
| 82 | rc = NO_ERROR;
|
|---|
| 83 | cout << "init success fully completed" << endl;
|
|---|
| 84 |
|
|---|
| 85 | /* dump to file */
|
|---|
| 86 | if (argc == 2)
|
|---|
| 87 | {
|
|---|
| 88 | strcpy(szFilename,argv[1]);
|
|---|
| 89 | rc = strlen(szFilename);
|
|---|
| 90 | szFilename[rc+4] = szFilename[rc-0];
|
|---|
| 91 | szFilename[rc+3] = szFilename[rc-1];
|
|---|
| 92 | szFilename[rc+2] = szFilename[rc-2];
|
|---|
| 93 | szFilename[rc+1] = szFilename[rc-3];
|
|---|
| 94 | szFilename[rc+0] = szFilename[rc-4];
|
|---|
| 95 | szFilename[rc-1] = '_';
|
|---|
| 96 | szFilename[rc-2] = 'o';
|
|---|
| 97 | szFilename[rc-3] = 's';
|
|---|
| 98 | szFilename[rc-4] = '2';
|
|---|
| 99 | strcat(szFilename,"_os2.exe");
|
|---|
| 100 | }
|
|---|
| 101 | else
|
|---|
| 102 | strcpy(szFilename,argv[2]);
|
|---|
| 103 | outfile = fopen(szFilename, "wb");
|
|---|
| 104 | if (outfile == NULL)
|
|---|
| 105 | {
|
|---|
| 106 | printf("error:could not open out file %s\n.",szFilename);
|
|---|
| 107 | return -1;
|
|---|
| 108 | }
|
|---|
| 109 | #define blocksize 907
|
|---|
| 110 | #define MAGIC "Win32-OS/2"
|
|---|
| 111 | strcpy(szBuffer,MAGIC);
|
|---|
| 112 | pBuffer = szBuffer+strlen(MAGIC)+1;
|
|---|
| 113 | strcpy(pBuffer+blocksize,MAGIC);
|
|---|
| 114 |
|
|---|
| 115 | rc = NO_ERROR;
|
|---|
| 116 | ulOffset = 0;
|
|---|
| 117 | rc = lxfile->read(ulOffset,pBuffer,blocksize, &ulBytesRead,0);
|
|---|
| 118 | while(rc == NO_ERROR)
|
|---|
| 119 | {
|
|---|
| 120 | if (strcmp(pBuffer+blocksize, MAGIC) != 0)
|
|---|
| 121 | return printf("!wrote to much! +\n");
|
|---|
| 122 | if (strcmp(szBuffer, MAGIC) != 0)
|
|---|
| 123 | return printf("!wrote to much! -\n");
|
|---|
| 124 | fwrite(pBuffer,1,ulBytesRead,outfile);
|
|---|
| 125 | flushall();
|
|---|
| 126 | ulOffset += ulBytesRead;
|
|---|
| 127 | rc = lxfile->read(ulOffset,pBuffer,blocksize, &ulBytesRead,0);
|
|---|
| 128 | }
|
|---|
| 129 | if (rc == -2)
|
|---|
| 130 | fwrite(pBuffer,1,ulBytesRead,outfile);
|
|---|
| 131 | fclose(outfile);
|
|---|
| 132 | }
|
|---|
| 133 | else
|
|---|
| 134 | cout << "warning: init failed!" << endl;
|
|---|
| 135 | delete lxfile;
|
|---|
| 136 | }
|
|---|
| 137 | else
|
|---|
| 138 | cout << "error: probably not a valid PE-file" << endl;
|
|---|
| 139 |
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 | else
|
|---|
| 144 | cout << "error: could not open file" << argv[1] << endl;
|
|---|
| 145 |
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|